blob: 9be6764a50efc9279b66ec4c0fd5d9d762ec42ca [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050059#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040063#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050068 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050069 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040070 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040071 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040072 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040073 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050074 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020075 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020076 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040077
78 int display_fd;
79 uint32_t mask;
80 struct task display_task;
81
82 int epoll_fd;
83 struct wl_list deferred_list;
84
Pekka Paalanen826d7952011-12-15 10:14:07 +020085 int running;
86
Kristian Høgsberg478d9262010-06-08 20:34:11 -040087 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040088 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050089 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040090 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050091 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040092 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040093 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040094
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050095 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
96 PFNEGLCREATEIMAGEKHRPROC create_image;
97 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020098
99 display_output_handler_t output_configure_handler;
100
101 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500102};
103
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400106 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500107 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400108 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500109 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110 TYPE_CUSTOM
111};
112
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500113struct window {
114 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500115 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500118 struct wl_region *input_region;
119 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500120 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500121 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500122 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500123 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400124 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400125 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400126 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400127 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400128 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400129 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400130 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400131 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400132 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500133
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400134 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500135
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400136 struct wl_shm_pool *pool;
137 size_t pool_size;
138 void *pool_data;
139
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500140 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500141 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400142 window_data_handler_t data_handler;
143 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500144 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400145
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200146 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500147 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500148
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500149 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400150 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500151};
152
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500153struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500154 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500155 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400156 struct wl_list link;
157 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500158 widget_resize_handler_t resize_handler;
159 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500160 widget_enter_handler_t enter_handler;
161 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500162 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500163 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400164 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500165 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400166};
167
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400168struct input {
169 struct display *display;
170 struct wl_input_device *input_device;
171 struct window *pointer_focus;
172 struct window *keyboard_focus;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400173 int current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400174 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200175 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400176 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400177
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500178 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500179 struct widget *grab;
180 uint32_t grab_button;
181
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400182 struct wl_data_device *data_device;
183 struct data_offer *drag_offer;
184 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400185};
186
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500187struct output {
188 struct display *display;
189 struct wl_output *output;
190 struct rectangle allocation;
191 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200192
193 display_output_handler_t destroy_handler;
194 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500195};
196
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500197struct frame {
198 struct widget *widget;
199 struct widget *child;
200 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400201 int width;
202 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500203};
204
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500205struct menu {
206 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500207 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500208 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500209 const char **entries;
210 uint32_t time;
211 int current;
212 int count;
213 menu_func_t func;
214};
215
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400216enum {
217 POINTER_DEFAULT = 100,
218 POINTER_UNSET
219};
220
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500221enum window_location {
222 WINDOW_INTERIOR = 0,
223 WINDOW_RESIZING_TOP = 1,
224 WINDOW_RESIZING_BOTTOM = 2,
225 WINDOW_RESIZING_LEFT = 4,
226 WINDOW_RESIZING_TOP_LEFT = 5,
227 WINDOW_RESIZING_BOTTOM_LEFT = 6,
228 WINDOW_RESIZING_RIGHT = 8,
229 WINDOW_RESIZING_TOP_RIGHT = 9,
230 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
231 WINDOW_RESIZING_MASK = 15,
232 WINDOW_EXTERIOR = 16,
233 WINDOW_TITLEBAR = 17,
234 WINDOW_CLIENT_AREA = 18,
235};
236
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400237const char *option_xkb_layout = "us";
238const char *option_xkb_variant = "";
239const char *option_xkb_options = "";
240
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400241static const struct weston_option xkb_options[] = {
242 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
243 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
244 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400245};
246
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400247static const cairo_user_data_key_t surface_data_key;
248struct surface_data {
249 struct wl_buffer *buffer;
250};
251
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500252#define MULT(_d,c,a,t) \
253 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
254
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500255#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400256
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100257struct egl_window_surface_data {
258 struct display *display;
259 struct wl_surface *surface;
260 struct wl_egl_window *window;
261 EGLSurface surf;
262};
263
264static void
265egl_window_surface_data_destroy(void *p)
266{
267 struct egl_window_surface_data *data = p;
268 struct display *d = data->display;
269
270 eglDestroySurface(d->dpy, data->surf);
271 wl_egl_window_destroy(data->window);
272 data->surface = NULL;
273
274 free(p);
275}
276
277static cairo_surface_t *
278display_create_egl_window_surface(struct display *display,
279 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400280 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100281 struct rectangle *rectangle)
282{
283 cairo_surface_t *cairo_surface;
284 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400285 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200286 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100287
288 data = malloc(sizeof *data);
289 if (data == NULL)
290 return NULL;
291
292 data->display = display;
293 data->surface = surface;
294
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500295 config = display->argb_config;
296 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400298 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400300 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400302 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500303 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100304
Benjamin Franzke0c991632011-09-27 21:57:31 +0200305 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100306 data->surf,
307 rectangle->width,
308 rectangle->height);
309
310 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
311 data, egl_window_surface_data_destroy);
312
313 return cairo_surface;
314}
315
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400317 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200318 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319 EGLImageKHR image;
320 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800321 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400323};
324
325static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500326egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400327{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500328 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800329 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330
Benjamin Franzke0c991632011-09-27 21:57:31 +0200331 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200333 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800334
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500335 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500337 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400339}
340
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500341EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500342display_get_image_for_egl_image_surface(struct display *display,
343 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500344{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500345 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500346
347 data = cairo_surface_get_user_data (surface, &surface_data_key);
348
349 return data->image;
350}
351
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500352static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500353display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400354 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500355 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400356{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500357 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358 EGLDisplay dpy = display->dpy;
359 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200360 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400361
362 data = malloc(sizeof *data);
363 if (data == NULL)
364 return NULL;
365
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800366 data->display = display;
367
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400368 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400369 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500370 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000371 free(data);
372 return NULL;
373 }
374
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500375 data->device = display->argb_device;
376 content = CAIRO_CONTENT_COLOR_ALPHA;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500378 data->image = display->create_image(dpy, NULL,
379 EGL_NATIVE_PIXMAP_KHR,
380 (EGLClientBuffer) data->pixmap,
381 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500383 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500384 free(data);
385 return NULL;
386 }
387
388 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400389 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500390
Benjamin Franzke0c991632011-09-27 21:57:31 +0200391 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500394 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200395 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396
Benjamin Franzke0c991632011-09-27 21:57:31 +0200397 surface = cairo_gl_surface_create_for_texture(data->device,
398 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399 data->texture,
400 rectangle->width,
401 rectangle->height);
402
403 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500404 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405
406 return surface;
407}
408
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500409static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500410display_create_egl_image_surface_from_file(struct display *display,
411 const char *filename,
412 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400413{
414 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400415 pixman_image_t *image;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500416 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400417
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400418 image = load_image(filename);
Kristian Høgsberg610a3f22012-04-03 14:49:50 -0400419 if (image == NULL)
420 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200422 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000423 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400424 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000425 return NULL;
426 }
427
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800428 data = cairo_surface_get_user_data(surface, &surface_data_key);
429
Benjamin Franzke0c991632011-09-27 21:57:31 +0200430 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800431 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800432 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400433 GL_RGBA, GL_UNSIGNED_BYTE,
434 pixman_image_get_data(image));
Benjamin Franzke0c991632011-09-27 21:57:31 +0200435 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400436
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400437 pixman_image_unref(image);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400438
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400439 return surface;
440}
441
442#endif
443
444struct wl_buffer *
445display_get_buffer_for_surface(struct display *display,
446 cairo_surface_t *surface)
447{
448 struct surface_data *data;
449
450 data = cairo_surface_get_user_data (surface, &surface_data_key);
451
452 return data->buffer;
453}
454
455struct shm_surface_data {
456 struct surface_data data;
457 void *map;
458 size_t length;
459};
460
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500461static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400462shm_surface_data_destroy(void *p)
463{
464 struct shm_surface_data *data = p;
465
466 wl_buffer_destroy(data->data.buffer);
467 munmap(data->map, data->length);
468}
469
Kristian Høgsberg16626282012-04-03 11:21:27 -0400470static struct wl_shm_pool *
471make_shm_pool(struct display *display, int size, void **data)
472{
473 char filename[] = "/tmp/wayland-shm-XXXXXX";
474 struct wl_shm_pool *pool;
475 int fd;
476
477 fd = mkstemp(filename);
478 if (fd < 0) {
479 fprintf(stderr, "open %s failed: %m\n", filename);
480 return NULL;
481 }
482 if (ftruncate(fd, size) < 0) {
483 fprintf(stderr, "ftruncate failed: %m\n");
484 close(fd);
485 return NULL;
486 }
487
488 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
489 unlink(filename);
490
491 if (*data == MAP_FAILED) {
492 fprintf(stderr, "mmap failed: %m\n");
493 close(fd);
494 return NULL;
495 }
496
497 pool = wl_shm_create_pool(display->shm, fd, size);
498
499 close(fd);
500
501 return pool;
502}
503
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500504static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505display_create_shm_surface(struct display *display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400506 struct rectangle *rectangle, uint32_t flags,
507 struct window *window)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508{
509 struct shm_surface_data *data;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400510 struct wl_shm_pool *pool = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400511 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400512 cairo_surface_t *surface;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400513 void *map;
Kristian Høgsberg16626282012-04-03 11:21:27 -0400514 int stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400515
516 data = malloc(sizeof *data);
517 if (data == NULL)
518 return NULL;
519
520 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
521 rectangle->width);
522 data->length = stride * rectangle->height;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400523 if (window && window->pool && data->length < window->pool_size) {
524 pool = window->pool;
525 map = window->pool_data;
526 } else {
527 pool = make_shm_pool(display, data->length, &data->map);
528 map = data->map;
529 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400531 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532 CAIRO_FORMAT_ARGB32,
533 rectangle->width,
534 rectangle->height,
535 stride);
536
537 cairo_surface_set_user_data (surface, &surface_data_key,
538 data, shm_surface_data_destroy);
539
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400540 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500541 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400542 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500543 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400544
Kristian Høgsberg16626282012-04-03 11:21:27 -0400545 data->data.buffer = wl_shm_pool_create_buffer(pool, 0,
546 rectangle->width,
547 rectangle->height,
548 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400550 if (map == data->map)
551 wl_shm_pool_destroy(pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400552
553 return surface;
554}
555
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500556static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557display_create_shm_surface_from_file(struct display *display,
558 const char *filename,
559 struct rectangle *rect)
560{
561 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400562 pixman_image_t *image;
563 void *dest;
564 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400565
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400566 image = load_image(filename);
Kristian Høgsberg610a3f22012-04-03 14:49:50 -0400567 if (image == NULL)
568 return NULL;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400569
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400570 surface = display_create_shm_surface(display, rect, 0, NULL);
nobled7b87cb02011-02-01 18:51:47 +0000571 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400572 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000573 return NULL;
574 }
575
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400576 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
577 dest = cairo_image_surface_get_data(surface);
578 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400579
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400580 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581
582 return surface;
583}
584
nobled7b87cb02011-02-01 18:51:47 +0000585static int
586check_size(struct rectangle *rect)
587{
588 if (rect->width && rect->height)
589 return 0;
590
591 fprintf(stderr, "tried to create surface of "
592 "width: %d, height: %d\n", rect->width, rect->height);
593 return -1;
594}
595
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596cairo_surface_t *
597display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100598 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400599 struct rectangle *rectangle,
600 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400601{
nobled7b87cb02011-02-01 18:51:47 +0000602 if (check_size(rectangle) < 0)
603 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500604#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500605 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100606 if (surface)
607 return display_create_egl_window_surface(display,
608 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400609 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100610 rectangle);
611 else
612 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400613 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100614 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500615 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400617 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618}
619
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500620static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400621display_create_surface_from_file(struct display *display,
622 const char *filename,
623 struct rectangle *rectangle)
624{
nobled7b87cb02011-02-01 18:51:47 +0000625 if (check_size(rectangle) < 0)
626 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500627#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500628 if (display->dpy) {
629 return display_create_egl_image_surface_from_file(display,
630 filename,
631 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500632 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500634 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400635}
Yuval Fledel45568f62010-12-06 09:18:12 -0500636 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400637 const char *filename;
638 int hotspot_x, hotspot_y;
639} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500640 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
641 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
642 { DATADIR "/weston/bottom_side.png", 16, 20 },
643 { DATADIR "/weston/grabbing.png", 20, 17 },
644 { DATADIR "/weston/left_ptr.png", 10, 5 },
645 { DATADIR "/weston/left_side.png", 10, 20 },
646 { DATADIR "/weston/right_side.png", 30, 19 },
647 { DATADIR "/weston/top_left_corner.png", 8, 8 },
648 { DATADIR "/weston/top_right_corner.png", 26, 8 },
649 { DATADIR "/weston/top_side.png", 18, 8 },
650 { DATADIR "/weston/xterm.png", 15, 15 },
651 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400652};
653
654static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400655create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400656{
657 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400658 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400659 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400660
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400661 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400662 display->pointer_surfaces =
663 malloc(count * sizeof *display->pointer_surfaces);
664 rect.width = width;
665 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400666 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400667 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400668 display_create_surface_from_file(display,
669 pointer_images[i].filename,
670 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100671 if (!display->pointer_surfaces[i]) {
672 fprintf(stderr, "Error loading pointer image: %s\n",
673 pointer_images[i].filename);
674 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400675 }
676
677}
678
Pekka Paalanen325bb602011-12-19 10:31:45 +0200679static void
680destroy_pointer_surfaces(struct display *display)
681{
682 int i, count;
683
684 count = ARRAY_LENGTH(pointer_images);
685 for (i = 0; i < count; ++i) {
686 if (display->pointer_surfaces[i])
687 cairo_surface_destroy(display->pointer_surfaces[i]);
688 }
689 free(display->pointer_surfaces);
690}
691
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400692cairo_surface_t *
693display_get_pointer_surface(struct display *display, int pointer,
694 int *width, int *height,
695 int *hotspot_x, int *hotspot_y)
696{
697 cairo_surface_t *surface;
698
699 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500700#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400701 *width = cairo_gl_surface_get_width(surface);
702 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000703#else
704 *width = cairo_image_surface_get_width(surface);
705 *height = cairo_image_surface_get_height(surface);
706#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400707 *hotspot_x = pointer_images[pointer].hotspot_x;
708 *hotspot_y = pointer_images[pointer].hotspot_y;
709
710 return cairo_surface_reference(surface);
711}
712
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400713static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200714window_get_resize_dx_dy(struct window *window, int *x, int *y)
715{
716 if (window->resize_edges & WINDOW_RESIZING_LEFT)
717 *x = window->server_allocation.width - window->allocation.width;
718 else
719 *x = 0;
720
721 if (window->resize_edges & WINDOW_RESIZING_TOP)
722 *y = window->server_allocation.height -
723 window->allocation.height;
724 else
725 *y = 0;
726
727 window->resize_edges = 0;
728}
729
730static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500731window_attach_surface(struct window *window)
732{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400733 struct display *display = window->display;
734 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000735#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000737#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500738 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739
740 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000741#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
743 data = cairo_surface_get_user_data(window->cairo_surface,
744 &surface_data_key);
745
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100746 cairo_gl_surface_swapbuffers(window->cairo_surface);
747 wl_egl_window_get_attached_size(data->window,
748 &window->server_allocation.width,
749 &window->server_allocation.height);
750 break;
751 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000752#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100753 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100754 buffer =
755 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400756 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100757
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200758 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100759 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400760 wl_surface_damage(window->surface, 0, 0,
761 window->allocation.width,
762 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100763 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400764 cairo_surface_destroy(window->cairo_surface);
765 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100766 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000767 default:
768 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100769 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500770
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500771 if (window->input_region) {
772 wl_surface_set_input_region(window->surface,
773 window->input_region);
774 wl_region_destroy(window->input_region);
775 window->input_region = NULL;
776 }
777
778 if (window->opaque_region) {
779 wl_surface_set_opaque_region(window->surface,
780 window->opaque_region);
781 wl_region_destroy(window->opaque_region);
782 window->opaque_region = NULL;
783 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500784}
785
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500786void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400787window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500788{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400789 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100790 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500791}
792
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400793void
794window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400795{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500796 cairo_surface_reference(surface);
797
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400798 if (window->cairo_surface != NULL)
799 cairo_surface_destroy(window->cairo_surface);
800
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500801 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400802}
803
Benjamin Franzke22d54812011-07-16 19:50:32 +0000804#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100805static void
806window_resize_cairo_window_surface(struct window *window)
807{
808 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200809 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100810
811 data = cairo_surface_get_user_data(window->cairo_surface,
812 &surface_data_key);
813
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200814 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 wl_egl_window_resize(data->window,
816 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200817 window->allocation.height,
818 x,y);
819
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 cairo_gl_surface_set_size(window->cairo_surface,
821 window->allocation.width,
822 window->allocation.height);
823}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000824#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400826struct display *
827window_get_display(struct window *window)
828{
829 return window->display;
830}
831
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400832void
833window_create_surface(struct window *window)
834{
835 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400836 uint32_t flags = 0;
837
838 if (!window->transparent)
839 flags = SURFACE_OPAQUE;
840
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400841 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500842#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
844 if (window->cairo_surface) {
845 window_resize_cairo_window_surface(window);
846 return;
847 }
848 surface = display_create_surface(window->display,
849 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400850 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500852 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400853 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400855 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856 break;
857#endif
858 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400859 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400860 &window->allocation,
861 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400862 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800863 default:
864 surface = NULL;
865 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400866 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400867
868 window_set_surface(window, surface);
869 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400870}
871
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200872static void frame_destroy(struct frame *frame);
873
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400874void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500875window_destroy(struct window *window)
876{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200877 struct display *display = window->display;
878 struct input *input;
879
880 if (window->redraw_scheduled)
881 wl_list_remove(&window->redraw_task.link);
882
883 wl_list_for_each(input, &display->input_list, link) {
884 if (input->pointer_focus == window)
885 input->pointer_focus = NULL;
886 if (input->keyboard_focus == window)
887 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500888 if (input->focus_widget &&
889 input->focus_widget->window == window)
890 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200891 }
892
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500893 if (window->input_region)
894 wl_region_destroy(window->input_region);
895 if (window->opaque_region)
896 wl_region_destroy(window->opaque_region);
897
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200898 if (window->frame)
899 frame_destroy(window->frame);
900
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200901 if (window->shell_surface)
902 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500903 wl_surface_destroy(window->surface);
904 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200905
906 if (window->cairo_surface != NULL)
907 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200908
909 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500910 free(window);
911}
912
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500913static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500914widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400915{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500916 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400917
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500918 wl_list_for_each(child, &widget->child_list, link) {
919 target = widget_find_widget(child, x, y);
920 if (target)
921 return target;
922 }
923
924 if (widget->allocation.x <= x &&
925 x < widget->allocation.x + widget->allocation.width &&
926 widget->allocation.y <= y &&
927 y < widget->allocation.y + widget->allocation.height) {
928 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400929 }
930
931 return NULL;
932}
933
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500934static struct widget *
935widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400936{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500937 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400938
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500939 widget = malloc(sizeof *widget);
940 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500941 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500942 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500943 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500944 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500945 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500946
947 return widget;
948}
949
950struct widget *
951window_add_widget(struct window *window, void *data)
952{
953 window->widget = widget_create(window, data);
954 wl_list_init(&window->widget->link);
955
956 return window->widget;
957}
958
959struct widget *
960widget_add_widget(struct widget *parent, void *data)
961{
962 struct widget *widget;
963
964 widget = widget_create(parent->window, data);
965 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500967 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400968}
969
970void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500971widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400972{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200973 struct display *display = widget->window->display;
974 struct input *input;
975
976 wl_list_for_each(input, &display->input_list, link) {
977 if (input->focus_widget == widget)
978 input->focus_widget = NULL;
979 }
980
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500981 wl_list_remove(&widget->link);
982 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400983}
984
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400985void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500986widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400987{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500988 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400989}
990
991void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500992widget_set_size(struct widget *widget, int32_t width, int32_t height)
993{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500994 widget->allocation.width = width;
995 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500996}
997
998void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999widget_set_allocation(struct widget *widget,
1000 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001001{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001002 widget->allocation.x = x;
1003 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001004 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001005}
1006
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001007void
1008widget_set_transparent(struct widget *widget, int transparent)
1009{
1010 widget->opaque = !transparent;
1011}
1012
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001013void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001014widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001015{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001016 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001017}
1018
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001019void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001020widget_set_resize_handler(struct widget *widget,
1021 widget_resize_handler_t handler)
1022{
1023 widget->resize_handler = handler;
1024}
1025
1026void
1027widget_set_redraw_handler(struct widget *widget,
1028 widget_redraw_handler_t handler)
1029{
1030 widget->redraw_handler = handler;
1031}
1032
1033void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001034widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001035{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001036 widget->enter_handler = handler;
1037}
1038
1039void
1040widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1041{
1042 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001043}
1044
1045void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001046widget_set_motion_handler(struct widget *widget,
1047 widget_motion_handler_t handler)
1048{
1049 widget->motion_handler = handler;
1050}
1051
1052void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001053widget_set_button_handler(struct widget *widget,
1054 widget_button_handler_t handler)
1055{
1056 widget->button_handler = handler;
1057}
1058
1059void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001060widget_schedule_redraw(struct widget *widget)
1061{
1062 window_schedule_redraw(widget->window);
1063}
1064
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001065cairo_surface_t *
1066window_get_surface(struct window *window)
1067{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001068 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001069}
1070
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001071struct wl_surface *
1072window_get_wl_surface(struct window *window)
1073{
1074 return window->surface;
1075}
1076
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001077struct wl_shell_surface *
1078window_get_wl_shell_surface(struct window *window)
1079{
1080 return window->shell_surface;
1081}
1082
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001083static void
1084frame_resize_handler(struct widget *widget,
1085 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001086{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001087 struct frame *frame = data;
1088 struct widget *child = frame->child;
1089 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001090 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001091 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001092 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001093
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001094 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001095 decoration_width = (frame->width + frame->margin) * 2;
1096 decoration_height = frame->width +
1097 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001098
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001099 allocation.x = frame->width + frame->margin;
1100 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001101 allocation.width = width - decoration_width;
1102 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001103
1104 widget->window->input_region =
1105 wl_compositor_create_region(display->compositor);
1106 wl_region_add(widget->window->input_region,
1107 frame->margin, frame->margin,
1108 width - 2 * frame->margin,
1109 height - 2 * frame->margin);
1110
1111 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001112 } else {
1113 decoration_width = 0;
1114 decoration_height = 0;
1115
1116 allocation.x = 0;
1117 allocation.y = 0;
1118 allocation.width = width;
1119 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001120 opaque_margin = 0;
1121 }
1122
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001123 widget_set_allocation(child, allocation.x, allocation.y,
1124 allocation.width, allocation.height);
1125
1126 if (child->resize_handler)
1127 child->resize_handler(child,
1128 allocation.width,
1129 allocation.height,
1130 child->user_data);
1131
1132 widget_set_allocation(widget, 0, 0,
1133 child->allocation.width + decoration_width,
1134 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001135
1136 if (child->opaque) {
1137 widget->window->opaque_region =
1138 wl_compositor_create_region(display->compositor);
1139 wl_region_add(widget->window->opaque_region,
1140 opaque_margin, opaque_margin,
1141 widget->allocation.width - 2 * opaque_margin,
1142 widget->allocation.height - 2 * opaque_margin);
1143 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001144}
1145
1146static void
1147frame_redraw_handler(struct widget *widget, void *data)
1148{
1149 struct frame *frame = data;
1150 cairo_t *cr;
1151 cairo_text_extents_t extents;
1152 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001153 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001154 struct window *window = widget->window;
1155
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001156 if (window->type == TYPE_FULLSCREEN)
1157 return;
1158
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001159 width = widget->allocation.width;
1160 height = widget->allocation.height;
1161
1162 cr = cairo_create(window->cairo_surface);
1163
1164 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1165 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1166 cairo_paint(cr);
1167
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001168 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001170 2, 2, width + 8, height + 8,
1171 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001172
1173 if (window->keyboard_device)
1174 source = window->display->active_frame;
1175 else
1176 source = window->display->inactive_frame;
1177
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001178 tile_source(cr, source,
1179 frame->margin, frame->margin,
1180 width - frame->margin * 2, height - frame->margin * 2,
1181 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001182
1183 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001184 cairo_select_font_face(cr, "sans",
1185 CAIRO_FONT_SLANT_NORMAL,
1186 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001187 cairo_set_font_size(cr, 14);
1188 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001189 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001190 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001191 if (window->keyboard_device) {
1192 cairo_move_to(cr, x + 1, y + 1);
1193 cairo_set_source_rgb(cr, 1, 1, 1);
1194 cairo_show_text(cr, window->title);
1195 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001196 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001197 cairo_show_text(cr, window->title);
1198 } else {
1199 cairo_move_to(cr, x, y);
1200 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1201 cairo_show_text(cr, window->title);
1202 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001203
1204 cairo_destroy(cr);
1205}
1206
1207static int
1208frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1209{
1210 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001211 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001212 const int grip_size = 8;
1213
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001214 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001215 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001216 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001217 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001218 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001219 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001220 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001221 hlocation = WINDOW_RESIZING_RIGHT;
1222 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001223 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001224
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001225 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001226 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001227 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001228 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001229 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001230 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001231 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232 vlocation = WINDOW_RESIZING_BOTTOM;
1233 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001234 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001235
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001236 location = vlocation | hlocation;
1237 if (location & WINDOW_EXTERIOR)
1238 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001240 location = WINDOW_TITLEBAR;
1241 else if (location == WINDOW_INTERIOR)
1242 location = WINDOW_CLIENT_AREA;
1243
1244 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001245}
1246
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001247static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001248frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001249{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001250 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001251
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001252 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001253 switch (location) {
1254 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001255 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001257 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001258 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001259 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001261 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001262 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001263 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001264 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001265 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001266 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001267 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001268 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001269 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001270 case WINDOW_EXTERIOR:
1271 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001272 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001273 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001275}
1276
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277static void
1278frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001279{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001280 switch (index) {
1281 case 0: /* close */
1282 if (window->close_handler)
1283 window->close_handler(window->parent,
1284 window->user_data);
1285 else
1286 display_exit(window->display);
1287 break;
1288 case 1: /* fullscreen */
1289 /* we don't have a way to get out of fullscreen for now */
1290 window_set_fullscreen(window, 1);
1291 break;
1292 case 2: /* rotate */
1293 case 3: /* scale */
1294 break;
1295 }
1296}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001298void
1299window_show_frame_menu(struct window *window,
1300 struct input *input, uint32_t time)
1301{
1302 int32_t x, y;
1303
1304 static const char *entries[] = {
1305 "Close", "Fullscreen", "Rotate", "Scale"
1306 };
1307
1308 input_get_position(input, &x, &y);
1309 window_show_menu(window->display, input, time, window,
1310 x - 10, y - 10, frame_menu_func, entries, 4);
1311}
1312
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001313static int
1314frame_enter_handler(struct widget *widget,
1315 struct input *input, uint32_t time,
1316 int32_t x, int32_t y, void *data)
1317{
1318 return frame_get_pointer_image_for_location(data, input);
1319}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001320
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001321static int
1322frame_motion_handler(struct widget *widget,
1323 struct input *input, uint32_t time,
1324 int32_t x, int32_t y, void *data)
1325{
1326 return frame_get_pointer_image_for_location(data, input);
1327}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001328
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001329static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001330frame_button_handler(struct widget *widget,
1331 struct input *input, uint32_t time,
1332 int button, int state, void *data)
1333
1334{
1335 struct frame *frame = data;
1336 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001337 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001338 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001339
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001340 location = frame_get_pointer_location(frame, input->sx, input->sy);
1341
1342 if (window->display->shell && button == BTN_LEFT && state == 1) {
1343 switch (location) {
1344 case WINDOW_TITLEBAR:
1345 if (!window->shell_surface)
1346 break;
1347 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001348 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001349 wl_shell_surface_move(window->shell_surface,
1350 input_get_input_device(input),
1351 time);
1352 break;
1353 case WINDOW_RESIZING_TOP:
1354 case WINDOW_RESIZING_BOTTOM:
1355 case WINDOW_RESIZING_LEFT:
1356 case WINDOW_RESIZING_RIGHT:
1357 case WINDOW_RESIZING_TOP_LEFT:
1358 case WINDOW_RESIZING_TOP_RIGHT:
1359 case WINDOW_RESIZING_BOTTOM_LEFT:
1360 case WINDOW_RESIZING_BOTTOM_RIGHT:
1361 if (!window->shell_surface)
1362 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001363 input_ungrab(input, time);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001364
1365 if (!display->dpy) {
1366 /* If we're using shm, allocate a big
1367 pool to create buffers out of while
1368 we resize. We should probably base
1369 this number on the size of the output. */
1370 window->pool_size = 6 * 1024 * 1024;
1371 window->pool = make_shm_pool(display,
1372 window->pool_size,
1373 &window->pool_data);
1374 }
1375
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001376 wl_shell_surface_resize(window->shell_surface,
1377 input_get_input_device(input),
1378 time, location);
1379 break;
1380 }
1381 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001382 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001383 }
1384}
1385
1386struct widget *
1387frame_create(struct window *window, void *data)
1388{
1389 struct frame *frame;
1390
1391 frame = malloc(sizeof *frame);
1392 memset(frame, 0, sizeof *frame);
1393
1394 frame->widget = window_add_widget(window, frame);
1395 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001396 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001397 frame->width = 4;
1398 frame->titlebar_height = 30
1399;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001400 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1401 widget_set_resize_handler(frame->widget, frame_resize_handler);
1402 widget_set_enter_handler(frame->widget, frame_enter_handler);
1403 widget_set_motion_handler(frame->widget, frame_motion_handler);
1404 widget_set_button_handler(frame->widget, frame_button_handler);
1405
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001406 window->frame = frame;
1407
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001408 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001409}
1410
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001411static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001412frame_destroy(struct frame *frame)
1413{
1414 /* frame->child must be destroyed by the application */
1415 widget_destroy(frame->widget);
1416 free(frame);
1417}
1418
1419static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001420input_set_focus_widget(struct input *input, struct widget *focus,
1421 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001422{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001423 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001424 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001425
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001426 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001427 return;
1428
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001429 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001430 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001431 widget = old;
1432 if (input->grab)
1433 widget = input->grab;
1434 if (widget->leave_handler)
1435 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001436 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001437 }
1438
1439 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001440 widget = focus;
1441 if (input->grab)
1442 widget = input->grab;
1443 if (widget->enter_handler)
1444 pointer = widget->enter_handler(focus, input, time,
1445 x, y,
1446 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001447 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001448
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001449 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001450 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001451}
1452
1453static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001454input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001455 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001456{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001457 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001458 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001459 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001460 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001461
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001462 input->sx = sx;
1463 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001464
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001465 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001466 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001467 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001468 }
1469
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001470 if (input->grab)
1471 widget = input->grab;
1472 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001473 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001474 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001475 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001476 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001477 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001478
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001479 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001480}
1481
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001482void
1483input_grab(struct input *input, struct widget *widget, uint32_t button)
1484{
1485 input->grab = widget;
1486 input->grab_button = button;
1487}
1488
1489void
1490input_ungrab(struct input *input, uint32_t time)
1491{
1492 struct widget *widget;
1493
1494 input->grab = NULL;
1495 if (input->pointer_focus) {
1496 widget = widget_find_widget(input->pointer_focus->widget,
1497 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001498 input_set_focus_widget(input, widget,
1499 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001500 }
1501}
1502
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001503static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001504input_handle_button(void *data,
1505 struct wl_input_device *input_device,
1506 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001507{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001508 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001509 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001510
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001511 if (input->focus_widget && input->grab == NULL && state)
1512 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001513
Neil Roberts6b28aad2012-01-23 19:11:18 +00001514 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001515 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001516 (*widget->button_handler)(widget,
1517 input, time,
1518 button, state,
1519 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001520
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001521 if (input->grab && input->grab_button == button && !state)
1522 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001523}
1524
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001525static void
Scott Moreau210d0792012-03-22 10:47:01 -06001526input_handle_axis(void *data,
1527 struct wl_input_device *input_device,
1528 uint32_t time, uint32_t axis, int32_t value)
1529{
1530}
1531
1532static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001533input_handle_key(void *data, struct wl_input_device *input_device,
1534 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001535{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001536 struct input *input = data;
1537 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001538 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001539 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001540
Daniel Stone0d5a5092012-02-16 12:48:00 +00001541 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001542 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001543 return;
1544
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001545 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001546 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001547 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1548 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001549
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001550 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1551
1552 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001553 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001554 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001555 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001556
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001557 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1558 if (state)
1559 window_set_maximized(window,
1560 window->type != TYPE_MAXIMIZED);
1561 } else if (window->key_handler) {
1562 (*window->key_handler)(window, input, time, key,
1563 sym, state, window->user_data);
1564 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001565}
1566
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001567static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001568input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001569{
1570 struct window *window = input->pointer_focus;
1571
1572 if (!window)
1573 return;
1574
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001575 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001576
Pekka Paalanene1207c72011-12-16 12:02:09 +02001577 input->pointer_focus = NULL;
1578 input->current_pointer_image = POINTER_UNSET;
1579}
1580
1581static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001582input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001583 struct wl_input_device *input_device,
1584 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001585 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001586{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001587 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001588 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001589 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001590
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001591 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001592 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001593
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001594 if (window->pool) {
1595 wl_shm_pool_destroy(window->pool);
1596 munmap(window->pool_data, window->pool_size);
1597 window->pool = NULL;
1598 /* Schedule a redraw to free the pool */
1599 window_schedule_redraw(window);
1600 }
1601
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001602 input->sx = sx;
1603 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001604
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001605 widget = widget_find_widget(window->widget, sx, sy);
1606 input_set_focus_widget(input, widget, time, sx, sy);
1607}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001608
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001609static void
1610input_handle_pointer_leave(void *data,
1611 struct wl_input_device *input_device,
1612 uint32_t time, struct wl_surface *surface)
1613{
1614 struct input *input = data;
1615
1616 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001617}
1618
1619static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001620input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001621{
1622 struct window *window = input->keyboard_focus;
1623
1624 if (!window)
1625 return;
1626
1627 window->keyboard_device = NULL;
1628 if (window->keyboard_focus_handler)
1629 (*window->keyboard_focus_handler)(window, NULL,
1630 window->user_data);
1631
1632 input->keyboard_focus = NULL;
1633}
1634
1635static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001636input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001637 struct wl_input_device *input_device,
1638 uint32_t time,
1639 struct wl_surface *surface,
1640 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001641{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001642 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001643 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001644 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001645 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001646
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001647 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001648
1649 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001650 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001651 for (k = keys->data; k < end; k++)
1652 input->modifiers |= d->xkb->map->modmap[*k];
1653
1654 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001655 window->keyboard_device = input;
1656 if (window->keyboard_focus_handler)
1657 (*window->keyboard_focus_handler)(window,
1658 window->keyboard_device,
1659 window->user_data);
1660}
1661
1662static void
1663input_handle_keyboard_leave(void *data,
1664 struct wl_input_device *input_device,
1665 uint32_t time,
1666 struct wl_surface *surface)
1667{
1668 struct input *input = data;
1669
1670 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001671}
1672
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001673static void
1674input_handle_touch_down(void *data,
1675 struct wl_input_device *wl_input_device,
1676 uint32_t time, struct wl_surface *surface,
1677 int32_t id, int32_t x, int32_t y)
1678{
1679}
1680
1681static void
1682input_handle_touch_up(void *data,
1683 struct wl_input_device *wl_input_device,
1684 uint32_t time, int32_t id)
1685{
1686}
1687
1688static void
1689input_handle_touch_motion(void *data,
1690 struct wl_input_device *wl_input_device,
1691 uint32_t time, int32_t id, int32_t x, int32_t y)
1692{
1693}
1694
1695static void
1696input_handle_touch_frame(void *data,
1697 struct wl_input_device *wl_input_device)
1698{
1699}
1700
1701static void
1702input_handle_touch_cancel(void *data,
1703 struct wl_input_device *wl_input_device)
1704{
1705}
1706
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001707static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001708 input_handle_motion,
1709 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001710 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001711 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001712 input_handle_pointer_enter,
1713 input_handle_pointer_leave,
1714 input_handle_keyboard_enter,
1715 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001716 input_handle_touch_down,
1717 input_handle_touch_up,
1718 input_handle_touch_motion,
1719 input_handle_touch_frame,
1720 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001721};
1722
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001723void
1724input_get_position(struct input *input, int32_t *x, int32_t *y)
1725{
1726 *x = input->sx;
1727 *y = input->sy;
1728}
1729
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001730struct wl_input_device *
1731input_get_input_device(struct input *input)
1732{
1733 return input->input_device;
1734}
1735
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001736uint32_t
1737input_get_modifiers(struct input *input)
1738{
1739 return input->modifiers;
1740}
1741
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001742struct widget *
1743input_get_focus_widget(struct input *input)
1744{
1745 return input->focus_widget;
1746}
1747
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001748struct data_offer {
1749 struct wl_data_offer *offer;
1750 struct input *input;
1751 struct wl_array types;
1752 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001753
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001754 struct task io_task;
1755 int fd;
1756 data_func_t func;
1757 int32_t x, y;
1758 void *user_data;
1759};
1760
1761static void
1762data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1763{
1764 struct data_offer *offer = data;
1765 char **p;
1766
1767 p = wl_array_add(&offer->types, sizeof *p);
1768 *p = strdup(type);
1769}
1770
1771static const struct wl_data_offer_listener data_offer_listener = {
1772 data_offer_offer,
1773};
1774
1775static void
1776data_offer_destroy(struct data_offer *offer)
1777{
1778 char **p;
1779
1780 offer->refcount--;
1781 if (offer->refcount == 0) {
1782 wl_data_offer_destroy(offer->offer);
1783 for (p = offer->types.data; *p; p++)
1784 free(*p);
1785 wl_array_release(&offer->types);
1786 free(offer);
1787 }
1788}
1789
1790static void
1791data_device_data_offer(void *data,
1792 struct wl_data_device *data_device, uint32_t id)
1793{
1794 struct data_offer *offer;
1795
1796 offer = malloc(sizeof *offer);
1797
1798 wl_array_init(&offer->types);
1799 offer->refcount = 1;
1800 offer->input = data;
1801
1802 /* FIXME: Generate typesafe wrappers for this */
1803 offer->offer = (struct wl_data_offer *)
1804 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1805 id, &wl_data_offer_interface);
1806
1807 wl_data_offer_add_listener(offer->offer,
1808 &data_offer_listener, offer);
1809}
1810
1811static void
1812data_device_enter(void *data, struct wl_data_device *data_device,
1813 uint32_t time, struct wl_surface *surface,
1814 int32_t x, int32_t y, struct wl_data_offer *offer)
1815{
1816 struct input *input = data;
1817 struct window *window;
1818 char **p;
1819
1820 input->drag_offer = wl_data_offer_get_user_data(offer);
1821 window = wl_surface_get_user_data(surface);
1822 input->pointer_focus = window;
1823
1824 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1825 *p = NULL;
1826
1827 window = input->pointer_focus;
1828 if (window->data_handler)
1829 window->data_handler(window, input, time, x, y,
1830 input->drag_offer->types.data,
1831 window->user_data);
1832}
1833
1834static void
1835data_device_leave(void *data, struct wl_data_device *data_device)
1836{
1837 struct input *input = data;
1838
1839 data_offer_destroy(input->drag_offer);
1840 input->drag_offer = NULL;
1841}
1842
1843static void
1844data_device_motion(void *data, struct wl_data_device *data_device,
1845 uint32_t time, int32_t x, int32_t y)
1846{
1847 struct input *input = data;
1848 struct window *window = input->pointer_focus;
1849
1850 input->sx = x;
1851 input->sy = y;
1852
1853 if (window->data_handler)
1854 window->data_handler(window, input, time, x, y,
1855 input->drag_offer->types.data,
1856 window->user_data);
1857}
1858
1859static void
1860data_device_drop(void *data, struct wl_data_device *data_device)
1861{
1862 struct input *input = data;
1863 struct window *window = input->pointer_focus;
1864
1865 if (window->drop_handler)
1866 window->drop_handler(window, input,
1867 input->sx, input->sy, window->user_data);
1868}
1869
1870static void
1871data_device_selection(void *data,
1872 struct wl_data_device *wl_data_device,
1873 struct wl_data_offer *offer)
1874{
1875 struct input *input = data;
1876 char **p;
1877
1878 if (input->selection_offer)
1879 data_offer_destroy(input->selection_offer);
1880
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001881 if (offer) {
1882 input->selection_offer = wl_data_offer_get_user_data(offer);
1883 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1884 *p = NULL;
1885 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001886}
1887
1888static const struct wl_data_device_listener data_device_listener = {
1889 data_device_data_offer,
1890 data_device_enter,
1891 data_device_leave,
1892 data_device_motion,
1893 data_device_drop,
1894 data_device_selection
1895};
1896
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001897void
1898input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1899{
1900 struct display *display = input->display;
1901 struct wl_buffer *buffer;
1902 cairo_surface_t *surface;
1903
1904 if (pointer == input->current_pointer_image)
1905 return;
1906
1907 input->current_pointer_image = pointer;
1908 surface = display->pointer_surfaces[pointer];
1909
1910 if (!surface)
1911 return;
1912
1913 buffer = display_get_buffer_for_surface(display, surface);
1914 wl_input_device_attach(input->input_device, time, buffer,
1915 pointer_images[pointer].hotspot_x,
1916 pointer_images[pointer].hotspot_y);
1917}
1918
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001919struct wl_data_device *
1920input_get_data_device(struct input *input)
1921{
1922 return input->data_device;
1923}
1924
1925void
1926input_set_selection(struct input *input,
1927 struct wl_data_source *source, uint32_t time)
1928{
1929 wl_data_device_set_selection(input->data_device, source, time);
1930}
1931
1932void
1933input_accept(struct input *input, uint32_t time, const char *type)
1934{
1935 wl_data_offer_accept(input->drag_offer->offer, time, type);
1936}
1937
1938static void
1939offer_io_func(struct task *task, uint32_t events)
1940{
1941 struct data_offer *offer =
1942 container_of(task, struct data_offer, io_task);
1943 unsigned int len;
1944 char buffer[4096];
1945
1946 len = read(offer->fd, buffer, sizeof buffer);
1947 offer->func(buffer, len,
1948 offer->x, offer->y, offer->user_data);
1949
1950 if (len == 0) {
1951 close(offer->fd);
1952 data_offer_destroy(offer);
1953 }
1954}
1955
1956static void
1957data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1958 data_func_t func, void *user_data)
1959{
1960 int p[2];
1961
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02001962 if (pipe2(p, O_CLOEXEC) == -1)
1963 return;
1964
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001965 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1966 close(p[1]);
1967
1968 offer->io_task.run = offer_io_func;
1969 offer->fd = p[0];
1970 offer->func = func;
1971 offer->refcount++;
1972 offer->user_data = user_data;
1973
1974 display_watch_fd(offer->input->display,
1975 offer->fd, EPOLLIN, &offer->io_task);
1976}
1977
1978void
1979input_receive_drag_data(struct input *input, const char *mime_type,
1980 data_func_t func, void *data)
1981{
1982 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1983 input->drag_offer->x = input->sx;
1984 input->drag_offer->y = input->sy;
1985}
1986
1987int
1988input_receive_selection_data(struct input *input, const char *mime_type,
1989 data_func_t func, void *data)
1990{
1991 char **p;
1992
1993 if (input->selection_offer == NULL)
1994 return -1;
1995
1996 for (p = input->selection_offer->types.data; *p; p++)
1997 if (strcmp(mime_type, *p) == 0)
1998 break;
1999
2000 if (*p == NULL)
2001 return -1;
2002
2003 data_offer_receive_data(input->selection_offer,
2004 mime_type, func, data);
2005 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002006}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002007
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002008int
2009input_receive_selection_data_to_fd(struct input *input,
2010 const char *mime_type, int fd)
2011{
2012 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2013
2014 return 0;
2015}
2016
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002017void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002018window_move(struct window *window, struct input *input, uint32_t time)
2019{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002020 if (!window->shell_surface)
2021 return;
2022
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002023 wl_shell_surface_move(window->shell_surface,
2024 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002025}
2026
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002027static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002028idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002029{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002030 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002031
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002032 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002033 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002034 widget_set_allocation(widget,
2035 window->pending_allocation.x,
2036 window->pending_allocation.y,
2037 window->pending_allocation.width,
2038 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002039
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002040 if (window->input_region) {
2041 wl_region_destroy(window->input_region);
2042 window->input_region = NULL;
2043 }
2044
2045 if (window->opaque_region) {
2046 wl_region_destroy(window->opaque_region);
2047 window->opaque_region = NULL;
2048 }
2049
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002050 if (widget->resize_handler)
2051 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002052 widget->allocation.width,
2053 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002054 widget->user_data);
2055
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002056 if (window->allocation.width != widget->allocation.width ||
2057 window->allocation.height != widget->allocation.height) {
2058 window->allocation = widget->allocation;
2059 window_schedule_redraw(window);
2060 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002061}
2062
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002063void
2064window_schedule_resize(struct window *window, int width, int height)
2065{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002066 window->pending_allocation.x = 0;
2067 window->pending_allocation.y = 0;
2068 window->pending_allocation.width = width;
2069 window->pending_allocation.height = height;
2070
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002071 window->resize_needed = 1;
2072 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002073}
2074
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002075void
2076widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2077{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002078 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002079}
2080
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002081static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002082handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002083 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002084 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002085{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002086 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002087
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002088 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002089 return;
2090
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002091 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002092 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002093}
2094
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002095static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002096menu_destroy(struct menu *menu)
2097{
2098 widget_destroy(menu->widget);
2099 window_destroy(menu->window);
2100 free(menu);
2101}
2102
2103static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002104handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2105{
2106 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002107 struct menu *menu = window->widget->user_data;
2108
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002109 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002110 * device. Or just use wl_callback. And this really needs to
2111 * be a window vfunc that the menu can set. And we need the
2112 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002113
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002114 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002115 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002116 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002117}
2118
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002119static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002120 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002121 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002122};
2123
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002124void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002125window_get_allocation(struct window *window,
2126 struct rectangle *allocation)
2127{
2128 *allocation = window->allocation;
2129}
2130
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002131static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002132widget_redraw(struct widget *widget)
2133{
2134 struct widget *child;
2135
2136 if (widget->redraw_handler)
2137 widget->redraw_handler(widget, widget->user_data);
2138 wl_list_for_each(child, &widget->child_list, link)
2139 widget_redraw(child);
2140}
2141
2142static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002143frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2144{
2145 struct window *window = data;
2146
2147 wl_callback_destroy(callback);
2148 window->redraw_scheduled = 0;
2149 if (window->redraw_needed)
2150 window_schedule_redraw(window);
2151}
2152
2153static const struct wl_callback_listener listener = {
2154 frame_callback
2155};
2156
2157static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002158idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002159{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002160 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002161 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002162
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002163 if (window->resize_needed)
2164 idle_resize(window);
2165
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002166 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002167 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002168 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002169 window->redraw_needed = 0;
2170
2171 callback = wl_surface_frame(window->surface);
2172 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002173}
2174
2175void
2176window_schedule_redraw(struct window *window)
2177{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002178 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002179 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002180 window->redraw_task.run = idle_redraw;
2181 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002182 window->redraw_scheduled = 1;
2183 }
2184}
2185
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002186void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002187window_set_custom(struct window *window)
2188{
2189 window->type = TYPE_CUSTOM;
2190}
2191
2192void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002193window_set_fullscreen(struct window *window, int fullscreen)
2194{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002195 if (!window->display->shell)
2196 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002197
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002198 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002199 return;
2200
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002201 if (fullscreen) {
2202 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002203 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002204 wl_shell_surface_set_fullscreen(window->shell_surface,
2205 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2206 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002207 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002208 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002209 wl_shell_surface_set_toplevel(window->shell_surface);
2210 window_schedule_resize(window,
2211 window->saved_allocation.width,
2212 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002213 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002214}
2215
2216void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002217window_set_maximized(struct window *window, int maximized)
2218{
2219 if (!window->display->shell)
2220 return;
2221
2222 if ((window->type == TYPE_MAXIMIZED) == maximized)
2223 return;
2224
2225 if (window->type == TYPE_TOPLEVEL) {
2226 window->saved_allocation = window->allocation;
2227 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2228 window->type = TYPE_MAXIMIZED;
2229 } else {
2230 wl_shell_surface_set_toplevel(window->shell_surface);
2231 window->type = TYPE_TOPLEVEL;
2232 window_schedule_resize(window,
2233 window->saved_allocation.width,
2234 window->saved_allocation.height);
2235 }
2236}
2237
2238void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002239window_set_user_data(struct window *window, void *data)
2240{
2241 window->user_data = data;
2242}
2243
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002244void *
2245window_get_user_data(struct window *window)
2246{
2247 return window->user_data;
2248}
2249
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002250void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002251window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002252 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002253{
2254 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002255}
2256
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002257void
2258window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002259 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002260{
2261 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002262}
2263
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002264void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002265window_set_data_handler(struct window *window, window_data_handler_t handler)
2266{
2267 window->data_handler = handler;
2268}
2269
2270void
2271window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2272{
2273 window->drop_handler = handler;
2274}
2275
2276void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002277window_set_close_handler(struct window *window,
2278 window_close_handler_t handler)
2279{
2280 window->close_handler = handler;
2281}
2282
2283void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002284window_set_title(struct window *window, const char *title)
2285{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002286 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002287 window->title = strdup(title);
2288}
2289
2290const char *
2291window_get_title(struct window *window)
2292{
2293 return window->title;
2294}
2295
2296void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002297window_damage(struct window *window, int32_t x, int32_t y,
2298 int32_t width, int32_t height)
2299{
2300 wl_surface_damage(window->surface, x, y, width, height);
2301}
2302
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002303static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002304window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002305{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002306 struct window *window;
2307
2308 window = malloc(sizeof *window);
2309 if (window == NULL)
2310 return NULL;
2311
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002312 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002313 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002314 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002315 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002316 if (display->shell) {
2317 window->shell_surface =
2318 wl_shell_get_shell_surface(display->shell,
2319 window->surface);
2320 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002321 window->allocation.x = 0;
2322 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002323 window->allocation.width = 0;
2324 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002325 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002326 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002327 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002328 window->input_region = NULL;
2329 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002330
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002331 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002332#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002333 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2334 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002335#else
2336 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2337#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002338 else
2339 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002340
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002341 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002342 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002343
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002344 if (window->shell_surface) {
2345 wl_shell_surface_set_user_data(window->shell_surface, window);
2346 wl_shell_surface_add_listener(window->shell_surface,
2347 &shell_surface_listener, window);
2348 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002349
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002350 return window;
2351}
2352
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002353struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002354window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002355{
2356 struct window *window;
2357
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002358 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002359 if (!window)
2360 return NULL;
2361
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002362 window->type = TYPE_TOPLEVEL;
2363 if (display->shell)
2364 wl_shell_surface_set_toplevel(window->shell_surface);
2365
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002366 return window;
2367}
2368
2369struct window *
2370window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002371 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002372{
2373 struct window *window;
2374
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002375 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002376 if (!window)
2377 return NULL;
2378
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002379 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002380 window->x = x;
2381 window->y = y;
2382
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002383 if (display->shell)
2384 wl_shell_surface_set_transient(window->shell_surface,
2385 window->parent->shell_surface,
2386 window->x, window->y, 0);
2387
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002388 return window;
2389}
2390
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002391static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002392menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002393{
2394 int next;
2395
2396 next = (sy - 8) / 20;
2397 if (menu->current != next) {
2398 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002399 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002400 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002401}
2402
2403static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002404menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002405 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002406 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002407{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002408 struct menu *menu = data;
2409
2410 if (widget == menu->widget)
2411 menu_set_item(data, y);
2412
2413 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002414}
2415
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002416static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002417menu_enter_handler(struct widget *widget,
2418 struct input *input, uint32_t time,
2419 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002420{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002421 struct menu *menu = data;
2422
2423 if (widget == menu->widget)
2424 menu_set_item(data, y);
2425
2426 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002427}
2428
2429static void
2430menu_leave_handler(struct widget *widget, struct input *input, void *data)
2431{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002432 struct menu *menu = data;
2433
2434 if (widget == menu->widget)
2435 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002436}
2437
2438static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002439menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002440 struct input *input, uint32_t time,
2441 int button, int state, void *data)
2442
2443{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002444 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002445
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002446 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002447 /* Either relase after press-drag-release or
2448 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002449 menu->func(menu->window->parent,
2450 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002451 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002452 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002453 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002454}
2455
2456static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002457menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002458{
2459 cairo_t *cr;
2460 const int32_t r = 3, margin = 3;
2461 struct menu *menu = data;
2462 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002463 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002464
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002465 cr = cairo_create(window->cairo_surface);
2466 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2467 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2468 cairo_paint(cr);
2469
2470 width = window->allocation.width;
2471 height = window->allocation.height;
2472 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002473
2474 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002475 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2476 cairo_fill(cr);
2477
2478 for (i = 0; i < menu->count; i++) {
2479 if (i == menu->current) {
2480 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2481 cairo_rectangle(cr, margin, i * 20 + margin,
2482 width - 2 * margin, 20);
2483 cairo_fill(cr);
2484 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2485 cairo_move_to(cr, 10, i * 20 + 16);
2486 cairo_show_text(cr, menu->entries[i]);
2487 } else {
2488 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2489 cairo_move_to(cr, 10, i * 20 + 16);
2490 cairo_show_text(cr, menu->entries[i]);
2491 }
2492 }
2493
2494 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002495}
2496
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002497void
2498window_show_menu(struct display *display,
2499 struct input *input, uint32_t time, struct window *parent,
2500 int32_t x, int32_t y,
2501 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002502{
2503 struct window *window;
2504 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002505 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002506
2507 menu = malloc(sizeof *menu);
2508 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002509 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002510
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002511 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002512 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002513 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002514
2515 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002516 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002517 menu->entries = entries;
2518 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002519 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002520 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002521 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002522 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002523 window->type = TYPE_MENU;
2524 window->x = x;
2525 window->y = y;
2526
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002527 wl_shell_surface_set_popup(window->shell_surface,
2528 input->input_device, time,
2529 window->parent->shell_surface,
2530 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002531
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002532 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002533 widget_set_enter_handler(menu->widget, menu_enter_handler);
2534 widget_set_leave_handler(menu->widget, menu_leave_handler);
2535 widget_set_motion_handler(menu->widget, menu_motion_handler);
2536 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002537
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002538 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002539 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002540}
2541
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002542void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002543window_set_buffer_type(struct window *window, enum window_buffer_type type)
2544{
2545 window->buffer_type = type;
2546}
2547
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002548
2549static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002550display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002551 struct wl_output *wl_output,
2552 int x, int y,
2553 int physical_width,
2554 int physical_height,
2555 int subpixel,
2556 const char *make,
2557 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002558{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002559 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002560
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002561 output->allocation.x = x;
2562 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002563}
2564
2565static void
2566display_handle_mode(void *data,
2567 struct wl_output *wl_output,
2568 uint32_t flags,
2569 int width,
2570 int height,
2571 int refresh)
2572{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002573 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002574 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002575
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002576 if (flags & WL_OUTPUT_MODE_CURRENT) {
2577 output->allocation.width = width;
2578 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002579 if (display->output_configure_handler)
2580 (*display->output_configure_handler)(
2581 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002582 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002583}
2584
2585static const struct wl_output_listener output_listener = {
2586 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002587 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002588};
2589
2590static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002591display_add_output(struct display *d, uint32_t id)
2592{
2593 struct output *output;
2594
2595 output = malloc(sizeof *output);
2596 if (output == NULL)
2597 return;
2598
2599 memset(output, 0, sizeof *output);
2600 output->display = d;
2601 output->output =
2602 wl_display_bind(d->display, id, &wl_output_interface);
2603 wl_list_insert(d->output_list.prev, &output->link);
2604
2605 wl_output_add_listener(output->output, &output_listener, output);
2606}
2607
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002608static void
2609output_destroy(struct output *output)
2610{
2611 if (output->destroy_handler)
2612 (*output->destroy_handler)(output, output->user_data);
2613
2614 wl_output_destroy(output->output);
2615 wl_list_remove(&output->link);
2616 free(output);
2617}
2618
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002619void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002620display_set_output_configure_handler(struct display *display,
2621 display_output_handler_t handler)
2622{
2623 struct output *output;
2624
2625 display->output_configure_handler = handler;
2626 if (!handler)
2627 return;
2628
2629 wl_list_for_each(output, &display->output_list, link)
2630 (*display->output_configure_handler)(output,
2631 display->user_data);
2632}
2633
2634void
2635output_set_user_data(struct output *output, void *data)
2636{
2637 output->user_data = data;
2638}
2639
2640void *
2641output_get_user_data(struct output *output)
2642{
2643 return output->user_data;
2644}
2645
2646void
2647output_set_destroy_handler(struct output *output,
2648 display_output_handler_t handler)
2649{
2650 output->destroy_handler = handler;
2651 /* FIXME: implement this, once we have way to remove outputs */
2652}
2653
2654void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002655output_get_allocation(struct output *output, struct rectangle *allocation)
2656{
2657 *allocation = output->allocation;
2658}
2659
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002660struct wl_output *
2661output_get_wl_output(struct output *output)
2662{
2663 return output->output;
2664}
2665
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002666static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002667display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002668{
2669 struct input *input;
2670
2671 input = malloc(sizeof *input);
2672 if (input == NULL)
2673 return;
2674
2675 memset(input, 0, sizeof *input);
2676 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002677 input->input_device =
2678 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002679 input->pointer_focus = NULL;
2680 input->keyboard_focus = NULL;
2681 wl_list_insert(d->input_list.prev, &input->link);
2682
2683 wl_input_device_add_listener(input->input_device,
2684 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002685 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002686
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002687 input->data_device =
2688 wl_data_device_manager_get_data_device(d->data_device_manager,
2689 input->input_device);
2690 wl_data_device_add_listener(input->data_device,
2691 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002692}
2693
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002694static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002695input_destroy(struct input *input)
2696{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002697 input_remove_keyboard_focus(input);
2698 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002699
2700 if (input->drag_offer)
2701 data_offer_destroy(input->drag_offer);
2702
2703 if (input->selection_offer)
2704 data_offer_destroy(input->selection_offer);
2705
2706 wl_data_device_destroy(input->data_device);
2707 wl_list_remove(&input->link);
2708 wl_input_device_destroy(input->input_device);
2709 free(input);
2710}
2711
2712static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002713display_handle_global(struct wl_display *display, uint32_t id,
2714 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002715{
2716 struct display *d = data;
2717
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002718 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002719 d->compositor =
2720 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002721 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002722 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002723 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002724 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002725 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002726 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002727 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002728 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002729 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2730 d->data_device_manager =
2731 wl_display_bind(display, id,
2732 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002733 }
2734}
2735
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002736static void
2737display_render_frame(struct display *d)
2738{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002739 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002740 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002741
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002742 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002743 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2744 cr = cairo_create(d->shadow);
2745 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2746 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002747 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002748 cairo_fill(cr);
2749 cairo_destroy(cr);
2750 blur_surface(d->shadow, 64);
2751
2752 d->active_frame =
2753 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2754 cr = cairo_create(d->active_frame);
2755 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002756
2757 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2758 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2759 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2760 cairo_set_source(cr, pattern);
2761 cairo_pattern_destroy(pattern);
2762
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002763 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002764 cairo_fill(cr);
2765 cairo_destroy(cr);
2766
2767 d->inactive_frame =
2768 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2769 cr = cairo_create(d->inactive_frame);
2770 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002771 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002772 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002773 cairo_fill(cr);
2774 cairo_destroy(cr);
2775}
2776
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002777static void
2778init_xkb(struct display *d)
2779{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002780 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002781
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002782 names.rules = "evdev";
2783 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002784 names.layout = option_xkb_layout;
2785 names.variant = option_xkb_variant;
2786 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002787
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002788 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002789 if (!d->xkb) {
2790 fprintf(stderr, "Failed to compile keymap\n");
2791 exit(1);
2792 }
2793}
2794
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002795static void
2796fini_xkb(struct display *display)
2797{
2798 xkb_free_keymap(display->xkb);
2799}
2800
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002801#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002802static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002803init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002804{
2805 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002806 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002807
Rob Clark6396ed32012-03-11 19:48:41 -05002808#ifdef USE_CAIRO_GLESV2
2809# define GL_BIT EGL_OPENGL_ES2_BIT
2810#else
2811# define GL_BIT EGL_OPENGL_BIT
2812#endif
2813
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002814 static const EGLint argb_cfg_attribs[] = {
2815 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002816 EGL_RED_SIZE, 1,
2817 EGL_GREEN_SIZE, 1,
2818 EGL_BLUE_SIZE, 1,
2819 EGL_ALPHA_SIZE, 1,
2820 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002821 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002822 EGL_NONE
2823 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002824
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002825#ifdef USE_CAIRO_GLESV2
2826 static const EGLint context_attribs[] = {
2827 EGL_CONTEXT_CLIENT_VERSION, 2,
2828 EGL_NONE
2829 };
2830 EGLint api = EGL_OPENGL_ES_API;
2831#else
2832 EGLint *context_attribs = NULL;
2833 EGLint api = EGL_OPENGL_API;
2834#endif
2835
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002836 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002837 if (!eglInitialize(d->dpy, &major, &minor)) {
2838 fprintf(stderr, "failed to initialize display\n");
2839 return -1;
2840 }
2841
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002842 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002843 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2844 return -1;
2845 }
2846
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002847 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2848 &d->argb_config, 1, &n) || n != 1) {
2849 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002850 return -1;
2851 }
2852
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002853 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002854 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002855 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002856 fprintf(stderr, "failed to create context\n");
2857 return -1;
2858 }
2859
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002860 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002861 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002862 return -1;
2863 }
2864
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002865#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002866 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2867 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2868 fprintf(stderr, "failed to get cairo egl argb device\n");
2869 return -1;
2870 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002871#endif
2872
2873 return 0;
2874}
2875
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002876static void
2877fini_egl(struct display *display)
2878{
2879#ifdef HAVE_CAIRO_EGL
2880 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002881#endif
2882
2883 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2884 EGL_NO_CONTEXT);
2885
2886 eglTerminate(display->dpy);
2887 eglReleaseThread();
2888}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002889#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002890
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002891static int
2892event_mask_update(uint32_t mask, void *data)
2893{
2894 struct display *d = data;
2895
2896 d->mask = mask;
2897
2898 return 0;
2899}
2900
2901static void
2902handle_display_data(struct task *task, uint32_t events)
2903{
2904 struct display *display =
2905 container_of(task, struct display, display_task);
2906
2907 wl_display_iterate(display->display, display->mask);
2908}
2909
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002910struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002911display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002912{
2913 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002914
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002915 argc = parse_options(xkb_options,
2916 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002917
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002918 d = malloc(sizeof *d);
2919 if (d == NULL)
2920 return NULL;
2921
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002922 memset(d, 0, sizeof *d);
2923
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002924 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002925 if (d->display == NULL) {
2926 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002927 return NULL;
2928 }
2929
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002930 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2931 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2932 d->display_task.run = handle_display_data;
2933 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2934
2935 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002936 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002937 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002938
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002939 /* Set up listener so we'll catch all events. */
2940 wl_display_add_global_listener(d->display,
2941 display_handle_global, d);
2942
2943 /* Process connection events. */
2944 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002945#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002946 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002947 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002948#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002949
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002950 d->image_target_texture_2d =
2951 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2952 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2953 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2954
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002955 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002956
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002957 display_render_frame(d);
2958
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002959 wl_list_init(&d->window_list);
2960
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002961 init_xkb(d);
2962
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002963 return d;
2964}
2965
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002966static void
2967display_destroy_outputs(struct display *display)
2968{
2969 struct output *tmp;
2970 struct output *output;
2971
2972 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2973 output_destroy(output);
2974}
2975
Pekka Paalanene1207c72011-12-16 12:02:09 +02002976static void
2977display_destroy_inputs(struct display *display)
2978{
2979 struct input *tmp;
2980 struct input *input;
2981
2982 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2983 input_destroy(input);
2984}
2985
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002986void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002987display_destroy(struct display *display)
2988{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002989 if (!wl_list_empty(&display->window_list))
2990 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2991
2992 if (!wl_list_empty(&display->deferred_list))
2993 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2994
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002995 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002996 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002997
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002998 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002999
3000 cairo_surface_destroy(display->active_frame);
3001 cairo_surface_destroy(display->inactive_frame);
3002 cairo_surface_destroy(display->shadow);
3003 destroy_pointer_surfaces(display);
3004
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003005#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003006 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003007#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003008
Pekka Paalanenc2052982011-12-16 11:41:32 +02003009 if (display->shell)
3010 wl_shell_destroy(display->shell);
3011
3012 if (display->shm)
3013 wl_shm_destroy(display->shm);
3014
3015 if (display->data_device_manager)
3016 wl_data_device_manager_destroy(display->data_device_manager);
3017
3018 wl_compositor_destroy(display->compositor);
3019
3020 close(display->epoll_fd);
3021
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003022 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003023 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003024 free(display);
3025}
3026
3027void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003028display_set_user_data(struct display *display, void *data)
3029{
3030 display->user_data = data;
3031}
3032
3033void *
3034display_get_user_data(struct display *display)
3035{
3036 return display->user_data;
3037}
3038
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003039struct wl_display *
3040display_get_display(struct display *display)
3041{
3042 return display->display;
3043}
3044
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003045struct output *
3046display_get_output(struct display *display)
3047{
3048 return container_of(display->output_list.next, struct output, link);
3049}
3050
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003051struct wl_compositor *
3052display_get_compositor(struct display *display)
3053{
3054 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003055}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003056
3057EGLDisplay
3058display_get_egl_display(struct display *d)
3059{
3060 return d->dpy;
3061}
3062
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003063struct wl_data_source *
3064display_create_data_source(struct display *display)
3065{
3066 return wl_data_device_manager_create_data_source(display->data_device_manager);
3067}
3068
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003069EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003070display_get_argb_egl_config(struct display *d)
3071{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003072 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003073}
3074
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003075struct wl_shell *
3076display_get_shell(struct display *display)
3077{
3078 return display->shell;
3079}
3080
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003081int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003082display_acquire_window_surface(struct display *display,
3083 struct window *window,
3084 EGLContext ctx)
3085{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003086#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003087 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003088 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003089
3090 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003091 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003092 device = cairo_surface_get_device(window->cairo_surface);
3093 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003094 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003095
Benjamin Franzke0c991632011-09-27 21:57:31 +02003096 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003097 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003098 ctx = display->argb_ctx;
3099 else
3100 assert(0);
3101 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003102
3103 data = cairo_surface_get_user_data(window->cairo_surface,
3104 &surface_data_key);
3105
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003106 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003107 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003108 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3109 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003110
3111 return 0;
3112#else
3113 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003114#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003115}
3116
3117void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003118display_release_window_surface(struct display *display,
3119 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003120{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003121#ifdef HAVE_CAIRO_EGL
3122 cairo_device_t *device;
3123
3124 device = cairo_surface_get_device(window->cairo_surface);
3125 if (!device)
3126 return;
3127
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003128 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003129 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003130 cairo_device_release(device);
3131#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003132}
3133
3134void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003135display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003136{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003137 wl_list_insert(&display->deferred_list, &task->link);
3138}
3139
3140void
3141display_watch_fd(struct display *display,
3142 int fd, uint32_t events, struct task *task)
3143{
3144 struct epoll_event ep;
3145
3146 ep.events = events;
3147 ep.data.ptr = task;
3148 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3149}
3150
3151void
3152display_run(struct display *display)
3153{
3154 struct task *task;
3155 struct epoll_event ep[16];
3156 int i, count;
3157
Pekka Paalanen826d7952011-12-15 10:14:07 +02003158 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003159 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003160 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003161
3162 while (!wl_list_empty(&display->deferred_list)) {
3163 task = container_of(display->deferred_list.next,
3164 struct task, link);
3165 wl_list_remove(&task->link);
3166 task->run(task, 0);
3167 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003168
3169 if (!display->running)
3170 break;
3171
3172 wl_display_flush(display->display);
3173
3174 count = epoll_wait(display->epoll_fd,
3175 ep, ARRAY_LENGTH(ep), -1);
3176 for (i = 0; i < count; i++) {
3177 task = ep[i].data.ptr;
3178 task->run(task, ep[i].events);
3179 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003180 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003181}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003182
3183void
3184display_exit(struct display *display)
3185{
3186 display->running = 0;
3187}