blob: 09d5ab14f92631fda49d9882e06f34f0f5113901 [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øgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Rob Clark6396ed32012-03-11 19:48:41 -050044#ifdef USE_CAIRO_GLESV2
45#include <GLES2/gl2.h>
46#include <GLES2/gl2ext.h>
47#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040048#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050049#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <EGL/egl.h>
51#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040052
Kristian Høgsberg8def2642011-01-14 17:41:33 -050053#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050056
Daniel Stone9d4f0302012-02-15 16:33:21 +000057#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050058#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040059
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020061#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040062#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050065
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050066struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050067 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050068 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040069 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040070 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040071 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040072 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050073 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020074 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020075 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040076
77 int display_fd;
78 uint32_t mask;
79 struct task display_task;
80
81 int epoll_fd;
82 struct wl_list deferred_list;
83
Pekka Paalanen826d7952011-12-15 10:14:07 +020084 int running;
85
Kristian Høgsberg478d9262010-06-08 20:34:11 -040086 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040087 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050088 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040089 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050090 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040091 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040092 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040093
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050094 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
95 PFNEGLCREATEIMAGEKHRPROC create_image;
96 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020097
98 display_output_handler_t output_configure_handler;
99
100 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500101};
102
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400103enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400105 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500106 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400107 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500108 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400109 TYPE_CUSTOM
110};
111
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500112struct window {
113 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500114 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500115 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200116 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500117 struct wl_region *input_region;
118 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500119 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500120 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500121 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500122 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400123 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400124 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400125 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500126 int resize_scheduled;
127 struct task resize_task;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400128 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400129 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400130 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400131 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500132
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500133 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500134
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500135 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500136 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400137 window_data_handler_t data_handler;
138 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500139 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400140
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200141 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500142 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500143
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500144 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400145 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500146};
147
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500148struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500149 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500150 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400151 struct wl_list link;
152 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500153 widget_resize_handler_t resize_handler;
154 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500155 widget_enter_handler_t enter_handler;
156 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500157 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500158 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400159 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500160 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161};
162
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400163struct input {
164 struct display *display;
165 struct wl_input_device *input_device;
166 struct window *pointer_focus;
167 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400168 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400169 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200170 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400171 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400172
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500173 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500174 struct widget *grab;
175 uint32_t grab_button;
176
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400177 struct wl_data_device *data_device;
178 struct data_offer *drag_offer;
179 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400180};
181
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500182struct output {
183 struct display *display;
184 struct wl_output *output;
185 struct rectangle allocation;
186 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200187
188 display_output_handler_t destroy_handler;
189 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500190};
191
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500192struct frame {
193 struct widget *widget;
194 struct widget *child;
195 int margin;
196};
197
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500198struct menu {
199 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500200 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500201 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500202 const char **entries;
203 uint32_t time;
204 int current;
205 int count;
206 menu_func_t func;
207};
208
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400209enum {
210 POINTER_DEFAULT = 100,
211 POINTER_UNSET
212};
213
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500214enum window_location {
215 WINDOW_INTERIOR = 0,
216 WINDOW_RESIZING_TOP = 1,
217 WINDOW_RESIZING_BOTTOM = 2,
218 WINDOW_RESIZING_LEFT = 4,
219 WINDOW_RESIZING_TOP_LEFT = 5,
220 WINDOW_RESIZING_BOTTOM_LEFT = 6,
221 WINDOW_RESIZING_RIGHT = 8,
222 WINDOW_RESIZING_TOP_RIGHT = 9,
223 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
224 WINDOW_RESIZING_MASK = 15,
225 WINDOW_EXTERIOR = 16,
226 WINDOW_TITLEBAR = 17,
227 WINDOW_CLIENT_AREA = 18,
228};
229
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400230const char *option_xkb_layout = "us";
231const char *option_xkb_variant = "";
232const char *option_xkb_options = "";
233
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400234static const struct weston_option xkb_options[] = {
235 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
236 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
237 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400238};
239
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400240static const cairo_user_data_key_t surface_data_key;
241struct surface_data {
242 struct wl_buffer *buffer;
243};
244
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500245#define MULT(_d,c,a,t) \
246 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
247
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500248#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400249
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100250struct egl_window_surface_data {
251 struct display *display;
252 struct wl_surface *surface;
253 struct wl_egl_window *window;
254 EGLSurface surf;
255};
256
257static void
258egl_window_surface_data_destroy(void *p)
259{
260 struct egl_window_surface_data *data = p;
261 struct display *d = data->display;
262
263 eglDestroySurface(d->dpy, data->surf);
264 wl_egl_window_destroy(data->window);
265 data->surface = NULL;
266
267 free(p);
268}
269
270static cairo_surface_t *
271display_create_egl_window_surface(struct display *display,
272 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400273 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100274 struct rectangle *rectangle)
275{
276 cairo_surface_t *cairo_surface;
277 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400278 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200279 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100280
281 data = malloc(sizeof *data);
282 if (data == NULL)
283 return NULL;
284
285 data->display = display;
286 data->surface = surface;
287
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500288 config = display->argb_config;
289 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400290
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400291 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100292 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400293 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100294
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400295 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500296 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100297
Benjamin Franzke0c991632011-09-27 21:57:31 +0200298 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299 data->surf,
300 rectangle->width,
301 rectangle->height);
302
303 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
304 data, egl_window_surface_data_destroy);
305
306 return cairo_surface;
307}
308
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500309struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400310 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200311 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312 EGLImageKHR image;
313 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800314 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500315 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316};
317
318static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400320{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800322 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400323
Benjamin Franzke0c991632011-09-27 21:57:31 +0200324 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200326 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800327
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500328 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500330 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500331 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332}
333
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500334EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335display_get_image_for_egl_image_surface(struct display *display,
336 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500337{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500339
340 data = cairo_surface_get_user_data (surface, &surface_data_key);
341
342 return data->image;
343}
344
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500345static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500346display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400347 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500348 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400349{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351 EGLDisplay dpy = display->dpy;
352 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200353 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354
355 data = malloc(sizeof *data);
356 if (data == NULL)
357 return NULL;
358
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800359 data->display = display;
360
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400361 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400362 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500363 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000364 free(data);
365 return NULL;
366 }
367
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500368 data->device = display->argb_device;
369 content = CAIRO_CONTENT_COLOR_ALPHA;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400370
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500371 data->image = display->create_image(dpy, NULL,
372 EGL_NATIVE_PIXMAP_KHR,
373 (EGLClientBuffer) data->pixmap,
374 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500375 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500376 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500377 free(data);
378 return NULL;
379 }
380
381 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400382 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383
Benjamin Franzke0c991632011-09-27 21:57:31 +0200384 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400385 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400386 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500387 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200388 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400389
Benjamin Franzke0c991632011-09-27 21:57:31 +0200390 surface = cairo_gl_surface_create_for_texture(data->device,
391 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 data->texture,
393 rectangle->width,
394 rectangle->height);
395
396 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500397 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398
399 return surface;
400}
401
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500402static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500403display_create_egl_image_surface_from_file(struct display *display,
404 const char *filename,
405 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406{
407 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400408 GdkPixbuf *pixbuf;
409 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400410 int stride, i;
411 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500412 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413
414 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400416 FALSE, &error);
417 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400419
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400420 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
421 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500422 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400424 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400425
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400426
427 stride = gdk_pixbuf_get_rowstride(pixbuf);
428 pixels = gdk_pixbuf_get_pixels(pixbuf);
429
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400430 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400431 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400432 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400433 while (p < end) {
434 unsigned int t;
435
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400436 MULT(p[0], p[0], p[3], t);
437 MULT(p[1], p[1], p[3], t);
438 MULT(p[2], p[2], p[3], t);
439 p += 4;
440
441 }
442 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400443
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200444 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000445 if (surface == NULL) {
446 g_object_unref(pixbuf);
447 return NULL;
448 }
449
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800450 data = cairo_surface_get_user_data(surface, &surface_data_key);
451
Benjamin Franzke0c991632011-09-27 21:57:31 +0200452 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800453 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800454 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
455 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200456 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400457
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500458 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400459
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400460 return surface;
461}
462
463#endif
464
465struct wl_buffer *
466display_get_buffer_for_surface(struct display *display,
467 cairo_surface_t *surface)
468{
469 struct surface_data *data;
470
471 data = cairo_surface_get_user_data (surface, &surface_data_key);
472
473 return data->buffer;
474}
475
476struct shm_surface_data {
477 struct surface_data data;
478 void *map;
479 size_t length;
480};
481
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500482static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400483shm_surface_data_destroy(void *p)
484{
485 struct shm_surface_data *data = p;
486
487 wl_buffer_destroy(data->data.buffer);
488 munmap(data->map, data->length);
489}
490
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500491static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400492display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400493 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400494{
495 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400496 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400497 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800498 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499 char filename[] = "/tmp/wayland-shm-XXXXXX";
500
501 data = malloc(sizeof *data);
502 if (data == NULL)
503 return NULL;
504
505 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
506 rectangle->width);
507 data->length = stride * rectangle->height;
508 fd = mkstemp(filename);
509 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000510 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400511 return NULL;
512 }
513 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000514 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400515 close(fd);
516 return NULL;
517 }
518
519 data->map = mmap(NULL, data->length,
520 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
521 unlink(filename);
522
523 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000524 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400525 close(fd);
526 return NULL;
527 }
528
529 surface = cairo_image_surface_create_for_data (data->map,
530 CAIRO_FORMAT_ARGB32,
531 rectangle->width,
532 rectangle->height,
533 stride);
534
535 cairo_surface_set_user_data (surface, &surface_data_key,
536 data, shm_surface_data_destroy);
537
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400538 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500539 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400540 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500541 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400542
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400543 data->data.buffer = wl_shm_create_buffer(display->shm,
544 fd,
545 rectangle->width,
546 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400547 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548
549 close(fd);
550
551 return surface;
552}
553
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500554static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400555display_create_shm_surface_from_file(struct display *display,
556 const char *filename,
557 struct rectangle *rect)
558{
559 cairo_surface_t *surface;
560 GdkPixbuf *pixbuf;
561 GError *error = NULL;
562 int stride, i;
563 unsigned char *pixels, *p, *end, *dest_data;
564 int dest_stride;
565 uint32_t *d;
566
567 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
568 rect->width, rect->height,
569 FALSE, &error);
570 if (error != NULL)
571 return NULL;
572
573 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
574 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500575 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576 return NULL;
577 }
578
579 stride = gdk_pixbuf_get_rowstride(pixbuf);
580 pixels = gdk_pixbuf_get_pixels(pixbuf);
581
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400582 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000583 if (surface == NULL) {
584 g_object_unref(pixbuf);
585 return NULL;
586 }
587
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400588 dest_data = cairo_image_surface_get_data (surface);
589 dest_stride = cairo_image_surface_get_stride (surface);
590
591 for (i = 0; i < rect->height; i++) {
592 d = (uint32_t *) (dest_data + i * dest_stride);
593 p = pixels + i * stride;
594 end = p + rect->width * 4;
595 while (p < end) {
596 unsigned int t;
597 unsigned char a, r, g, b;
598
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400599 a = p[3];
600 MULT(r, p[0], a, t);
601 MULT(g, p[1], a, t);
602 MULT(b, p[2], a, t);
603 p += 4;
604 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
605 }
606 }
607
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500608 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400609
610 return surface;
611}
612
nobled7b87cb02011-02-01 18:51:47 +0000613static int
614check_size(struct rectangle *rect)
615{
616 if (rect->width && rect->height)
617 return 0;
618
619 fprintf(stderr, "tried to create surface of "
620 "width: %d, height: %d\n", rect->width, rect->height);
621 return -1;
622}
623
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624cairo_surface_t *
625display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100626 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400627 struct rectangle *rectangle,
628 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629{
nobled7b87cb02011-02-01 18:51:47 +0000630 if (check_size(rectangle) < 0)
631 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500632#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500633 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634 if (surface)
635 return display_create_egl_window_surface(display,
636 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400637 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638 rectangle);
639 else
640 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400641 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500643 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400644#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400645 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400646}
647
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500648static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649display_create_surface_from_file(struct display *display,
650 const char *filename,
651 struct rectangle *rectangle)
652{
nobled7b87cb02011-02-01 18:51:47 +0000653 if (check_size(rectangle) < 0)
654 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500655#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500656 if (display->dpy) {
657 return display_create_egl_image_surface_from_file(display,
658 filename,
659 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500660 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400661#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500662 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400663}
Yuval Fledel45568f62010-12-06 09:18:12 -0500664 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400665 const char *filename;
666 int hotspot_x, hotspot_y;
667} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500668 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
669 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
670 { DATADIR "/weston/bottom_side.png", 16, 20 },
671 { DATADIR "/weston/grabbing.png", 20, 17 },
672 { DATADIR "/weston/left_ptr.png", 10, 5 },
673 { DATADIR "/weston/left_side.png", 10, 20 },
674 { DATADIR "/weston/right_side.png", 30, 19 },
675 { DATADIR "/weston/top_left_corner.png", 8, 8 },
676 { DATADIR "/weston/top_right_corner.png", 26, 8 },
677 { DATADIR "/weston/top_side.png", 18, 8 },
678 { DATADIR "/weston/xterm.png", 15, 15 },
679 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400680};
681
682static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400683create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400684{
685 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400686 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400687 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400689 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400690 display->pointer_surfaces =
691 malloc(count * sizeof *display->pointer_surfaces);
692 rect.width = width;
693 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400695 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400696 display_create_surface_from_file(display,
697 pointer_images[i].filename,
698 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100699 if (!display->pointer_surfaces[i]) {
700 fprintf(stderr, "Error loading pointer image: %s\n",
701 pointer_images[i].filename);
702 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400703 }
704
705}
706
Pekka Paalanen325bb602011-12-19 10:31:45 +0200707static void
708destroy_pointer_surfaces(struct display *display)
709{
710 int i, count;
711
712 count = ARRAY_LENGTH(pointer_images);
713 for (i = 0; i < count; ++i) {
714 if (display->pointer_surfaces[i])
715 cairo_surface_destroy(display->pointer_surfaces[i]);
716 }
717 free(display->pointer_surfaces);
718}
719
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400720cairo_surface_t *
721display_get_pointer_surface(struct display *display, int pointer,
722 int *width, int *height,
723 int *hotspot_x, int *hotspot_y)
724{
725 cairo_surface_t *surface;
726
727 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500728#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400729 *width = cairo_gl_surface_get_width(surface);
730 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000731#else
732 *width = cairo_image_surface_get_width(surface);
733 *height = cairo_image_surface_get_height(surface);
734#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400735 *hotspot_x = pointer_images[pointer].hotspot_x;
736 *hotspot_y = pointer_images[pointer].hotspot_y;
737
738 return cairo_surface_reference(surface);
739}
740
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400741static void
742window_attach_surface(struct window *window);
743
744static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400745free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400746{
747 struct window *window = data;
748
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400749 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400750 cairo_surface_destroy(window->pending_surface);
751 window->pending_surface = NULL;
752 if (window->cairo_surface)
753 window_attach_surface(window);
754}
755
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400756static const struct wl_callback_listener free_surface_listener = {
757 free_surface
758};
759
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500760static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200761window_get_resize_dx_dy(struct window *window, int *x, int *y)
762{
763 if (window->resize_edges & WINDOW_RESIZING_LEFT)
764 *x = window->server_allocation.width - window->allocation.width;
765 else
766 *x = 0;
767
768 if (window->resize_edges & WINDOW_RESIZING_TOP)
769 *y = window->server_allocation.height -
770 window->allocation.height;
771 else
772 *y = 0;
773
774 window->resize_edges = 0;
775}
776
777static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500778window_attach_surface(struct window *window)
779{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400780 struct display *display = window->display;
781 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400782 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000783#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000785#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500786 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787
788 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000789#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100790 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
791 data = cairo_surface_get_user_data(window->cairo_surface,
792 &surface_data_key);
793
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794 cairo_gl_surface_swapbuffers(window->cairo_surface);
795 wl_egl_window_get_attached_size(data->window,
796 &window->server_allocation.width,
797 &window->server_allocation.height);
798 break;
799 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000800#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100801 case WINDOW_BUFFER_TYPE_SHM:
802 if (window->pending_surface != NULL)
803 return;
804
805 window->pending_surface = window->cairo_surface;
806 window->cairo_surface = NULL;
807
808 buffer =
809 display_get_buffer_for_surface(display,
810 window->pending_surface);
811
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200812 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100813 wl_surface_attach(window->surface, buffer, x, y);
814 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400815 cb = wl_display_sync(display->display);
816 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000818 default:
819 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500821
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500822 if (window->input_region) {
823 wl_surface_set_input_region(window->surface,
824 window->input_region);
825 wl_region_destroy(window->input_region);
826 window->input_region = NULL;
827 }
828
829 if (window->opaque_region) {
830 wl_surface_set_opaque_region(window->surface,
831 window->opaque_region);
832 wl_region_destroy(window->opaque_region);
833 window->opaque_region = NULL;
834 }
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500835
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500836 wl_surface_damage(window->surface, 0, 0,
837 window->allocation.width,
838 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500839}
840
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500841void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400842window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500843{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100844 if (window->cairo_surface) {
845 switch (window->buffer_type) {
846 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
847 case WINDOW_BUFFER_TYPE_SHM:
848 display_surface_damage(window->display,
849 window->cairo_surface,
850 0, 0,
851 window->allocation.width,
852 window->allocation.height);
853 break;
854 default:
855 break;
856 }
857 window_attach_surface(window);
858 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500859}
860
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400861void
862window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400863{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500864 cairo_surface_reference(surface);
865
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400866 if (window->cairo_surface != NULL)
867 cairo_surface_destroy(window->cairo_surface);
868
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500869 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400870}
871
Benjamin Franzke22d54812011-07-16 19:50:32 +0000872#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100873static void
874window_resize_cairo_window_surface(struct window *window)
875{
876 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200877 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100878
879 data = cairo_surface_get_user_data(window->cairo_surface,
880 &surface_data_key);
881
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200882 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100883 wl_egl_window_resize(data->window,
884 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200885 window->allocation.height,
886 x,y);
887
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100888 cairo_gl_surface_set_size(window->cairo_surface,
889 window->allocation.width,
890 window->allocation.height);
891}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000892#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100893
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400894struct display *
895window_get_display(struct window *window)
896{
897 return window->display;
898}
899
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400900void
901window_create_surface(struct window *window)
902{
903 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400904 uint32_t flags = 0;
905
906 if (!window->transparent)
907 flags = SURFACE_OPAQUE;
908
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500910#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
912 if (window->cairo_surface) {
913 window_resize_cairo_window_surface(window);
914 return;
915 }
916 surface = display_create_surface(window->display,
917 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400918 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100919 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500920 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400921 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100922 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400923 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400924 break;
925#endif
926 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400927 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400928 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400929 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800930 default:
931 surface = NULL;
932 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400933 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400934
935 window_set_surface(window, surface);
936 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400937}
938
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200939static void frame_destroy(struct frame *frame);
940
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400941void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500942window_destroy(struct window *window)
943{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200944 struct display *display = window->display;
945 struct input *input;
946
947 if (window->redraw_scheduled)
948 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500949 if (window->resize_scheduled)
950 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200951
952 wl_list_for_each(input, &display->input_list, link) {
953 if (input->pointer_focus == window)
954 input->pointer_focus = NULL;
955 if (input->keyboard_focus == window)
956 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500957 if (input->focus_widget &&
958 input->focus_widget->window == window)
959 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200960 }
961
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500962 if (window->input_region)
963 wl_region_destroy(window->input_region);
964 if (window->opaque_region)
965 wl_region_destroy(window->opaque_region);
966
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200967 if (window->frame)
968 frame_destroy(window->frame);
969
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200970 if (window->shell_surface)
971 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500972 wl_surface_destroy(window->surface);
973 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200974
975 if (window->cairo_surface != NULL)
976 cairo_surface_destroy(window->cairo_surface);
977 if (window->pending_surface != NULL)
978 cairo_surface_destroy(window->pending_surface);
979
980 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500981 free(window);
982}
983
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500984static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500985widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400986{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500987 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400988
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500989 wl_list_for_each(child, &widget->child_list, link) {
990 target = widget_find_widget(child, x, y);
991 if (target)
992 return target;
993 }
994
995 if (widget->allocation.x <= x &&
996 x < widget->allocation.x + widget->allocation.width &&
997 widget->allocation.y <= y &&
998 y < widget->allocation.y + widget->allocation.height) {
999 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000 }
1001
1002 return NULL;
1003}
1004
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001005static struct widget *
1006widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001008 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001009
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001010 widget = malloc(sizeof *widget);
1011 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001012 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001013 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001014 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001015 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001016 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001017
1018 return widget;
1019}
1020
1021struct widget *
1022window_add_widget(struct window *window, void *data)
1023{
1024 window->widget = widget_create(window, data);
1025 wl_list_init(&window->widget->link);
1026
1027 return window->widget;
1028}
1029
1030struct widget *
1031widget_add_widget(struct widget *parent, void *data)
1032{
1033 struct widget *widget;
1034
1035 widget = widget_create(parent->window, data);
1036 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001037
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001038 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001039}
1040
1041void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001042widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001043{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001044 struct display *display = widget->window->display;
1045 struct input *input;
1046
1047 wl_list_for_each(input, &display->input_list, link) {
1048 if (input->focus_widget == widget)
1049 input->focus_widget = NULL;
1050 }
1051
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001052 wl_list_remove(&widget->link);
1053 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001054}
1055
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001056void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001057widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001059 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060}
1061
1062void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001063widget_set_size(struct widget *widget, int32_t width, int32_t height)
1064{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001065 widget->allocation.width = width;
1066 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001067}
1068
1069void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001070widget_set_allocation(struct widget *widget,
1071 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001072{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001073 widget->allocation.x = x;
1074 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001075 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001076}
1077
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001078void
1079widget_set_transparent(struct widget *widget, int transparent)
1080{
1081 widget->opaque = !transparent;
1082}
1083
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001084void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001085widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001086{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001087 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001088}
1089
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001090void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001091widget_set_resize_handler(struct widget *widget,
1092 widget_resize_handler_t handler)
1093{
1094 widget->resize_handler = handler;
1095}
1096
1097void
1098widget_set_redraw_handler(struct widget *widget,
1099 widget_redraw_handler_t handler)
1100{
1101 widget->redraw_handler = handler;
1102}
1103
1104void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001105widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001106{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001107 widget->enter_handler = handler;
1108}
1109
1110void
1111widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1112{
1113 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001114}
1115
1116void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001117widget_set_motion_handler(struct widget *widget,
1118 widget_motion_handler_t handler)
1119{
1120 widget->motion_handler = handler;
1121}
1122
1123void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001124widget_set_button_handler(struct widget *widget,
1125 widget_button_handler_t handler)
1126{
1127 widget->button_handler = handler;
1128}
1129
1130void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001131widget_schedule_redraw(struct widget *widget)
1132{
1133 window_schedule_redraw(widget->window);
1134}
1135
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001136cairo_surface_t *
1137window_get_surface(struct window *window)
1138{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001139 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001140}
1141
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001142struct wl_surface *
1143window_get_wl_surface(struct window *window)
1144{
1145 return window->surface;
1146}
1147
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001148struct wl_shell_surface *
1149window_get_wl_shell_surface(struct window *window)
1150{
1151 return window->shell_surface;
1152}
1153
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001154static void
1155frame_resize_handler(struct widget *widget,
1156 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001157{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001158 struct frame *frame = data;
1159 struct widget *child = frame->child;
1160 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001161 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001162 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001163 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001164
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001165 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001166 decoration_width = 20 + frame->margin * 2;
1167 decoration_height = 60 + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001168
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001169 allocation.x = 10 + frame->margin;
1170 allocation.y = 50 + frame->margin;
1171 allocation.width = width - decoration_width;
1172 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001173
1174 widget->window->input_region =
1175 wl_compositor_create_region(display->compositor);
1176 wl_region_add(widget->window->input_region,
1177 frame->margin, frame->margin,
1178 width - 2 * frame->margin,
1179 height - 2 * frame->margin);
1180
1181 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001182 } else {
1183 decoration_width = 0;
1184 decoration_height = 0;
1185
1186 allocation.x = 0;
1187 allocation.y = 0;
1188 allocation.width = width;
1189 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001190 opaque_margin = 0;
1191 }
1192
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001193 widget_set_allocation(child, allocation.x, allocation.y,
1194 allocation.width, allocation.height);
1195
1196 if (child->resize_handler)
1197 child->resize_handler(child,
1198 allocation.width,
1199 allocation.height,
1200 child->user_data);
1201
1202 widget_set_allocation(widget, 0, 0,
1203 child->allocation.width + decoration_width,
1204 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001205
1206 if (child->opaque) {
1207 widget->window->opaque_region =
1208 wl_compositor_create_region(display->compositor);
1209 wl_region_add(widget->window->opaque_region,
1210 opaque_margin, opaque_margin,
1211 widget->allocation.width - 2 * opaque_margin,
1212 widget->allocation.height - 2 * opaque_margin);
1213 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001214}
1215
1216static void
1217frame_redraw_handler(struct widget *widget, void *data)
1218{
1219 struct frame *frame = data;
1220 cairo_t *cr;
1221 cairo_text_extents_t extents;
1222 cairo_surface_t *source;
1223 int width, height, shadow_dx = 3, shadow_dy = 3;
1224 struct window *window = widget->window;
1225
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001226 if (window->type == TYPE_FULLSCREEN)
1227 return;
1228
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001229 width = widget->allocation.width;
1230 height = widget->allocation.height;
1231
1232 cr = cairo_create(window->cairo_surface);
1233
1234 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1235 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1236 cairo_paint(cr);
1237
1238 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1239 tile_mask(cr, window->display->shadow,
1240 shadow_dx, shadow_dy, width, height,
1241 frame->margin + 10 - shadow_dx,
1242 frame->margin + 10 - shadow_dy);
1243
1244 if (window->keyboard_device)
1245 source = window->display->active_frame;
1246 else
1247 source = window->display->inactive_frame;
1248
1249 tile_source(cr, source, 0, 0, width, height,
1250 frame->margin + 10, frame->margin + 50);
1251
1252 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1253 cairo_set_font_size(cr, 14);
1254 cairo_text_extents(cr, window->title, &extents);
1255 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1256 if (window->keyboard_device)
1257 cairo_set_source_rgb(cr, 0, 0, 0);
1258 else
1259 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1260 cairo_show_text(cr, window->title);
1261
1262 cairo_destroy(cr);
1263}
1264
1265static int
1266frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1267{
1268 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001269 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270 const int grip_size = 8;
1271
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001272 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001273 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001274 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001275 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001276 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001277 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001278 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001279 hlocation = WINDOW_RESIZING_RIGHT;
1280 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001281 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001282
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001283 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001284 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001285 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001287 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001288 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001289 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001290 vlocation = WINDOW_RESIZING_BOTTOM;
1291 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001292 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001293
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001294 location = vlocation | hlocation;
1295 if (location & WINDOW_EXTERIOR)
1296 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001297 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001298 location = WINDOW_TITLEBAR;
1299 else if (location == WINDOW_INTERIOR)
1300 location = WINDOW_CLIENT_AREA;
1301
1302 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001303}
1304
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001305static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001306frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001307{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001308 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001309
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001310 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001311 switch (location) {
1312 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001313 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001314 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001315 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001316 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001317 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001318 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001319 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001320 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001321 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001322 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001323 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001324 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001325 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001326 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001327 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001328 case WINDOW_EXTERIOR:
1329 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001330 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001331 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001332 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001333}
1334
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001335static void
1336frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001337{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001338 switch (index) {
1339 case 0: /* close */
1340 if (window->close_handler)
1341 window->close_handler(window->parent,
1342 window->user_data);
1343 else
1344 display_exit(window->display);
1345 break;
1346 case 1: /* fullscreen */
1347 /* we don't have a way to get out of fullscreen for now */
1348 window_set_fullscreen(window, 1);
1349 break;
1350 case 2: /* rotate */
1351 case 3: /* scale */
1352 break;
1353 }
1354}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001355
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001356void
1357window_show_frame_menu(struct window *window,
1358 struct input *input, uint32_t time)
1359{
1360 int32_t x, y;
1361
1362 static const char *entries[] = {
1363 "Close", "Fullscreen", "Rotate", "Scale"
1364 };
1365
1366 input_get_position(input, &x, &y);
1367 window_show_menu(window->display, input, time, window,
1368 x - 10, y - 10, frame_menu_func, entries, 4);
1369}
1370
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001371static int
1372frame_enter_handler(struct widget *widget,
1373 struct input *input, uint32_t time,
1374 int32_t x, int32_t y, void *data)
1375{
1376 return frame_get_pointer_image_for_location(data, input);
1377}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001378
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001379static int
1380frame_motion_handler(struct widget *widget,
1381 struct input *input, uint32_t time,
1382 int32_t x, int32_t y, void *data)
1383{
1384 return frame_get_pointer_image_for_location(data, input);
1385}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001386
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001387static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001388frame_button_handler(struct widget *widget,
1389 struct input *input, uint32_t time,
1390 int button, int state, void *data)
1391
1392{
1393 struct frame *frame = data;
1394 struct window *window = widget->window;
1395 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001396 location = frame_get_pointer_location(frame, input->sx, input->sy);
1397
1398 if (window->display->shell && button == BTN_LEFT && state == 1) {
1399 switch (location) {
1400 case WINDOW_TITLEBAR:
1401 if (!window->shell_surface)
1402 break;
1403 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001404 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001405 wl_shell_surface_move(window->shell_surface,
1406 input_get_input_device(input),
1407 time);
1408 break;
1409 case WINDOW_RESIZING_TOP:
1410 case WINDOW_RESIZING_BOTTOM:
1411 case WINDOW_RESIZING_LEFT:
1412 case WINDOW_RESIZING_RIGHT:
1413 case WINDOW_RESIZING_TOP_LEFT:
1414 case WINDOW_RESIZING_TOP_RIGHT:
1415 case WINDOW_RESIZING_BOTTOM_LEFT:
1416 case WINDOW_RESIZING_BOTTOM_RIGHT:
1417 if (!window->shell_surface)
1418 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001419 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001420 wl_shell_surface_resize(window->shell_surface,
1421 input_get_input_device(input),
1422 time, location);
1423 break;
1424 }
1425 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001426 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001427 }
1428}
1429
1430struct widget *
1431frame_create(struct window *window, void *data)
1432{
1433 struct frame *frame;
1434
1435 frame = malloc(sizeof *frame);
1436 memset(frame, 0, sizeof *frame);
1437
1438 frame->widget = window_add_widget(window, frame);
1439 frame->child = widget_add_widget(frame->widget, data);
1440 frame->margin = 16;
1441
1442 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1443 widget_set_resize_handler(frame->widget, frame_resize_handler);
1444 widget_set_enter_handler(frame->widget, frame_enter_handler);
1445 widget_set_motion_handler(frame->widget, frame_motion_handler);
1446 widget_set_button_handler(frame->widget, frame_button_handler);
1447
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001448 window->frame = frame;
1449
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001450 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001451}
1452
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001453static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001454frame_destroy(struct frame *frame)
1455{
1456 /* frame->child must be destroyed by the application */
1457 widget_destroy(frame->widget);
1458 free(frame);
1459}
1460
1461static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001462input_set_focus_widget(struct input *input, struct widget *focus,
1463 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001464{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001465 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001466 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001467
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001468 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001469 return;
1470
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001471 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001472 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001473 widget = old;
1474 if (input->grab)
1475 widget = input->grab;
1476 if (widget->leave_handler)
1477 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001478 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001479 }
1480
1481 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001482 widget = focus;
1483 if (input->grab)
1484 widget = input->grab;
1485 if (widget->enter_handler)
1486 pointer = widget->enter_handler(focus, input, time,
1487 x, y,
1488 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001489 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001490
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001491 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001492 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001493}
1494
1495static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001496input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001497 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001498{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001499 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001500 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001501 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001502 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001503
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001504 input->sx = sx;
1505 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001506
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001507 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001508 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001509 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001510 }
1511
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001512 if (input->grab)
1513 widget = input->grab;
1514 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001515 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001516 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001517 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001518 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001519 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001520
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001521 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001522}
1523
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001524void
1525input_grab(struct input *input, struct widget *widget, uint32_t button)
1526{
1527 input->grab = widget;
1528 input->grab_button = button;
1529}
1530
1531void
1532input_ungrab(struct input *input, uint32_t time)
1533{
1534 struct widget *widget;
1535
1536 input->grab = NULL;
1537 if (input->pointer_focus) {
1538 widget = widget_find_widget(input->pointer_focus->widget,
1539 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001540 input_set_focus_widget(input, widget,
1541 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001542 }
1543}
1544
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001545static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001546input_handle_button(void *data,
1547 struct wl_input_device *input_device,
1548 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001549{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001550 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001551 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001552
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001553 if (input->focus_widget && input->grab == NULL && state)
1554 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001555
Neil Roberts6b28aad2012-01-23 19:11:18 +00001556 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001557 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001558 (*widget->button_handler)(widget,
1559 input, time,
1560 button, state,
1561 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001562
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001563 if (input->grab && input->grab_button == button && !state)
1564 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001565}
1566
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001567static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001568input_handle_key(void *data, struct wl_input_device *input_device,
1569 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001570{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001571 struct input *input = data;
1572 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001573 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001574 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001575
Daniel Stone0d5a5092012-02-16 12:48:00 +00001576 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001577 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001578 return;
1579
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001580 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001581 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001582 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1583 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001584
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001585 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1586
1587 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001588 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001589 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001590 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001591
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001592 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1593 if (state)
1594 window_set_maximized(window,
1595 window->type != TYPE_MAXIMIZED);
1596 } else if (window->key_handler) {
1597 (*window->key_handler)(window, input, time, key,
1598 sym, state, window->user_data);
1599 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001600}
1601
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001602static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001603input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001604{
1605 struct window *window = input->pointer_focus;
1606
1607 if (!window)
1608 return;
1609
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001610 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001611
Pekka Paalanene1207c72011-12-16 12:02:09 +02001612 input->pointer_focus = NULL;
1613 input->current_pointer_image = POINTER_UNSET;
1614}
1615
1616static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001617input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001618 struct wl_input_device *input_device,
1619 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001620 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001621{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001622 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001623 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001624 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001625
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001626 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001627 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001628
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001629 input->sx = sx;
1630 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001631
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001632 widget = widget_find_widget(window->widget, sx, sy);
1633 input_set_focus_widget(input, widget, time, sx, sy);
1634}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001635
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001636static void
1637input_handle_pointer_leave(void *data,
1638 struct wl_input_device *input_device,
1639 uint32_t time, struct wl_surface *surface)
1640{
1641 struct input *input = data;
1642
1643 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001644}
1645
1646static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001647input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001648{
1649 struct window *window = input->keyboard_focus;
1650
1651 if (!window)
1652 return;
1653
1654 window->keyboard_device = NULL;
1655 if (window->keyboard_focus_handler)
1656 (*window->keyboard_focus_handler)(window, NULL,
1657 window->user_data);
1658
1659 input->keyboard_focus = NULL;
1660}
1661
1662static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001663input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001664 struct wl_input_device *input_device,
1665 uint32_t time,
1666 struct wl_surface *surface,
1667 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001668{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001669 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001670 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001671 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001672 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001673
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001674 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001675
1676 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001677 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001678 for (k = keys->data; k < end; k++)
1679 input->modifiers |= d->xkb->map->modmap[*k];
1680
1681 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001682 window->keyboard_device = input;
1683 if (window->keyboard_focus_handler)
1684 (*window->keyboard_focus_handler)(window,
1685 window->keyboard_device,
1686 window->user_data);
1687}
1688
1689static void
1690input_handle_keyboard_leave(void *data,
1691 struct wl_input_device *input_device,
1692 uint32_t time,
1693 struct wl_surface *surface)
1694{
1695 struct input *input = data;
1696
1697 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001698}
1699
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001700static void
1701input_handle_touch_down(void *data,
1702 struct wl_input_device *wl_input_device,
1703 uint32_t time, struct wl_surface *surface,
1704 int32_t id, int32_t x, int32_t y)
1705{
1706}
1707
1708static void
1709input_handle_touch_up(void *data,
1710 struct wl_input_device *wl_input_device,
1711 uint32_t time, int32_t id)
1712{
1713}
1714
1715static void
1716input_handle_touch_motion(void *data,
1717 struct wl_input_device *wl_input_device,
1718 uint32_t time, int32_t id, int32_t x, int32_t y)
1719{
1720}
1721
1722static void
1723input_handle_touch_frame(void *data,
1724 struct wl_input_device *wl_input_device)
1725{
1726}
1727
1728static void
1729input_handle_touch_cancel(void *data,
1730 struct wl_input_device *wl_input_device)
1731{
1732}
1733
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001734static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001735 input_handle_motion,
1736 input_handle_button,
1737 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001738 input_handle_pointer_enter,
1739 input_handle_pointer_leave,
1740 input_handle_keyboard_enter,
1741 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001742 input_handle_touch_down,
1743 input_handle_touch_up,
1744 input_handle_touch_motion,
1745 input_handle_touch_frame,
1746 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001747};
1748
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001749void
1750input_get_position(struct input *input, int32_t *x, int32_t *y)
1751{
1752 *x = input->sx;
1753 *y = input->sy;
1754}
1755
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001756struct wl_input_device *
1757input_get_input_device(struct input *input)
1758{
1759 return input->input_device;
1760}
1761
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001762uint32_t
1763input_get_modifiers(struct input *input)
1764{
1765 return input->modifiers;
1766}
1767
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001768struct widget *
1769input_get_focus_widget(struct input *input)
1770{
1771 return input->focus_widget;
1772}
1773
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001774struct data_offer {
1775 struct wl_data_offer *offer;
1776 struct input *input;
1777 struct wl_array types;
1778 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001779
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001780 struct task io_task;
1781 int fd;
1782 data_func_t func;
1783 int32_t x, y;
1784 void *user_data;
1785};
1786
1787static void
1788data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1789{
1790 struct data_offer *offer = data;
1791 char **p;
1792
1793 p = wl_array_add(&offer->types, sizeof *p);
1794 *p = strdup(type);
1795}
1796
1797static const struct wl_data_offer_listener data_offer_listener = {
1798 data_offer_offer,
1799};
1800
1801static void
1802data_offer_destroy(struct data_offer *offer)
1803{
1804 char **p;
1805
1806 offer->refcount--;
1807 if (offer->refcount == 0) {
1808 wl_data_offer_destroy(offer->offer);
1809 for (p = offer->types.data; *p; p++)
1810 free(*p);
1811 wl_array_release(&offer->types);
1812 free(offer);
1813 }
1814}
1815
1816static void
1817data_device_data_offer(void *data,
1818 struct wl_data_device *data_device, uint32_t id)
1819{
1820 struct data_offer *offer;
1821
1822 offer = malloc(sizeof *offer);
1823
1824 wl_array_init(&offer->types);
1825 offer->refcount = 1;
1826 offer->input = data;
1827
1828 /* FIXME: Generate typesafe wrappers for this */
1829 offer->offer = (struct wl_data_offer *)
1830 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1831 id, &wl_data_offer_interface);
1832
1833 wl_data_offer_add_listener(offer->offer,
1834 &data_offer_listener, offer);
1835}
1836
1837static void
1838data_device_enter(void *data, struct wl_data_device *data_device,
1839 uint32_t time, struct wl_surface *surface,
1840 int32_t x, int32_t y, struct wl_data_offer *offer)
1841{
1842 struct input *input = data;
1843 struct window *window;
1844 char **p;
1845
1846 input->drag_offer = wl_data_offer_get_user_data(offer);
1847 window = wl_surface_get_user_data(surface);
1848 input->pointer_focus = window;
1849
1850 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1851 *p = NULL;
1852
1853 window = input->pointer_focus;
1854 if (window->data_handler)
1855 window->data_handler(window, input, time, x, y,
1856 input->drag_offer->types.data,
1857 window->user_data);
1858}
1859
1860static void
1861data_device_leave(void *data, struct wl_data_device *data_device)
1862{
1863 struct input *input = data;
1864
1865 data_offer_destroy(input->drag_offer);
1866 input->drag_offer = NULL;
1867}
1868
1869static void
1870data_device_motion(void *data, struct wl_data_device *data_device,
1871 uint32_t time, int32_t x, int32_t y)
1872{
1873 struct input *input = data;
1874 struct window *window = input->pointer_focus;
1875
1876 input->sx = x;
1877 input->sy = y;
1878
1879 if (window->data_handler)
1880 window->data_handler(window, input, time, x, y,
1881 input->drag_offer->types.data,
1882 window->user_data);
1883}
1884
1885static void
1886data_device_drop(void *data, struct wl_data_device *data_device)
1887{
1888 struct input *input = data;
1889 struct window *window = input->pointer_focus;
1890
1891 if (window->drop_handler)
1892 window->drop_handler(window, input,
1893 input->sx, input->sy, window->user_data);
1894}
1895
1896static void
1897data_device_selection(void *data,
1898 struct wl_data_device *wl_data_device,
1899 struct wl_data_offer *offer)
1900{
1901 struct input *input = data;
1902 char **p;
1903
1904 if (input->selection_offer)
1905 data_offer_destroy(input->selection_offer);
1906
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001907 if (offer) {
1908 input->selection_offer = wl_data_offer_get_user_data(offer);
1909 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1910 *p = NULL;
1911 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001912}
1913
1914static const struct wl_data_device_listener data_device_listener = {
1915 data_device_data_offer,
1916 data_device_enter,
1917 data_device_leave,
1918 data_device_motion,
1919 data_device_drop,
1920 data_device_selection
1921};
1922
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001923void
1924input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1925{
1926 struct display *display = input->display;
1927 struct wl_buffer *buffer;
1928 cairo_surface_t *surface;
1929
1930 if (pointer == input->current_pointer_image)
1931 return;
1932
1933 input->current_pointer_image = pointer;
1934 surface = display->pointer_surfaces[pointer];
1935
1936 if (!surface)
1937 return;
1938
1939 buffer = display_get_buffer_for_surface(display, surface);
1940 wl_input_device_attach(input->input_device, time, buffer,
1941 pointer_images[pointer].hotspot_x,
1942 pointer_images[pointer].hotspot_y);
1943}
1944
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001945struct wl_data_device *
1946input_get_data_device(struct input *input)
1947{
1948 return input->data_device;
1949}
1950
1951void
1952input_set_selection(struct input *input,
1953 struct wl_data_source *source, uint32_t time)
1954{
1955 wl_data_device_set_selection(input->data_device, source, time);
1956}
1957
1958void
1959input_accept(struct input *input, uint32_t time, const char *type)
1960{
1961 wl_data_offer_accept(input->drag_offer->offer, time, type);
1962}
1963
1964static void
1965offer_io_func(struct task *task, uint32_t events)
1966{
1967 struct data_offer *offer =
1968 container_of(task, struct data_offer, io_task);
1969 unsigned int len;
1970 char buffer[4096];
1971
1972 len = read(offer->fd, buffer, sizeof buffer);
1973 offer->func(buffer, len,
1974 offer->x, offer->y, offer->user_data);
1975
1976 if (len == 0) {
1977 close(offer->fd);
1978 data_offer_destroy(offer);
1979 }
1980}
1981
1982static void
1983data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1984 data_func_t func, void *user_data)
1985{
1986 int p[2];
1987
1988 pipe2(p, O_CLOEXEC);
1989 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1990 close(p[1]);
1991
1992 offer->io_task.run = offer_io_func;
1993 offer->fd = p[0];
1994 offer->func = func;
1995 offer->refcount++;
1996 offer->user_data = user_data;
1997
1998 display_watch_fd(offer->input->display,
1999 offer->fd, EPOLLIN, &offer->io_task);
2000}
2001
2002void
2003input_receive_drag_data(struct input *input, const char *mime_type,
2004 data_func_t func, void *data)
2005{
2006 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2007 input->drag_offer->x = input->sx;
2008 input->drag_offer->y = input->sy;
2009}
2010
2011int
2012input_receive_selection_data(struct input *input, const char *mime_type,
2013 data_func_t func, void *data)
2014{
2015 char **p;
2016
2017 if (input->selection_offer == NULL)
2018 return -1;
2019
2020 for (p = input->selection_offer->types.data; *p; p++)
2021 if (strcmp(mime_type, *p) == 0)
2022 break;
2023
2024 if (*p == NULL)
2025 return -1;
2026
2027 data_offer_receive_data(input->selection_offer,
2028 mime_type, func, data);
2029 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002030}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002031
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002032int
2033input_receive_selection_data_to_fd(struct input *input,
2034 const char *mime_type, int fd)
2035{
2036 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2037
2038 return 0;
2039}
2040
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002041void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002042window_move(struct window *window, struct input *input, uint32_t time)
2043{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002044 if (!window->shell_surface)
2045 return;
2046
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002047 wl_shell_surface_move(window->shell_surface,
2048 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002049}
2050
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002051static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002052idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002053{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002054 struct window *window =
2055 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002056 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002057
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002058 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002059 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002060 widget_set_allocation(widget,
2061 window->pending_allocation.x,
2062 window->pending_allocation.y,
2063 window->pending_allocation.width,
2064 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002065
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002066 if (window->input_region) {
2067 wl_region_destroy(window->input_region);
2068 window->input_region = NULL;
2069 }
2070
2071 if (window->opaque_region) {
2072 wl_region_destroy(window->opaque_region);
2073 window->opaque_region = NULL;
2074 }
2075
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002076 if (widget->resize_handler)
2077 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002078 widget->allocation.width,
2079 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002080 widget->user_data);
2081
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002082 if (window->allocation.width != widget->allocation.width ||
2083 window->allocation.height != widget->allocation.height) {
2084 window->allocation = widget->allocation;
2085 window_schedule_redraw(window);
2086 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002087}
2088
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002089void
2090window_schedule_resize(struct window *window, int width, int height)
2091{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002092 window->pending_allocation.x = 0;
2093 window->pending_allocation.y = 0;
2094 window->pending_allocation.width = width;
2095 window->pending_allocation.height = height;
2096
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002097 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002098 window->resize_task.run = idle_resize;
2099 display_defer(window->display, &window->resize_task);
2100 window->resize_scheduled = 1;
2101 }
2102}
2103
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002104void
2105widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2106{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002107 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002108}
2109
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002110static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002111handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002112 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002113 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002114{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002115 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002116
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002117 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002118 return;
2119
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002120 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002121 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002122}
2123
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002124static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002125menu_destroy(struct menu *menu)
2126{
2127 widget_destroy(menu->widget);
2128 window_destroy(menu->window);
2129 free(menu);
2130}
2131
2132static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002133handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2134{
2135 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002136 struct menu *menu = window->widget->user_data;
2137
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002138 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002139 * device. Or just use wl_callback. And this really needs to
2140 * be a window vfunc that the menu can set. And we need the
2141 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002142
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002143 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002144 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002145 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002146}
2147
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002148static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002149 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002150 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002151};
2152
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002153void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002154window_get_allocation(struct window *window,
2155 struct rectangle *allocation)
2156{
2157 *allocation = window->allocation;
2158}
2159
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002160static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002161widget_redraw(struct widget *widget)
2162{
2163 struct widget *child;
2164
2165 if (widget->redraw_handler)
2166 widget->redraw_handler(widget, widget->user_data);
2167 wl_list_for_each(child, &widget->child_list, link)
2168 widget_redraw(child);
2169}
2170
2171static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002172idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002173{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002174 struct window *window =
2175 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002176
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002177 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002178 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002179 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002180 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002181}
2182
2183void
2184window_schedule_redraw(struct window *window)
2185{
2186 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002187 window->redraw_task.run = idle_redraw;
2188 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002189 window->redraw_scheduled = 1;
2190 }
2191}
2192
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002193void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002194window_set_custom(struct window *window)
2195{
2196 window->type = TYPE_CUSTOM;
2197}
2198
2199void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002200window_set_fullscreen(struct window *window, int fullscreen)
2201{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002202 if (!window->display->shell)
2203 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002204
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002205 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002206 return;
2207
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002208 if (fullscreen) {
2209 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002210 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002211 wl_shell_surface_set_fullscreen(window->shell_surface,
2212 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2213 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002214 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002215 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002216 wl_shell_surface_set_toplevel(window->shell_surface);
2217 window_schedule_resize(window,
2218 window->saved_allocation.width,
2219 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002220 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002221}
2222
2223void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002224window_set_maximized(struct window *window, int maximized)
2225{
2226 if (!window->display->shell)
2227 return;
2228
2229 if ((window->type == TYPE_MAXIMIZED) == maximized)
2230 return;
2231
2232 if (window->type == TYPE_TOPLEVEL) {
2233 window->saved_allocation = window->allocation;
2234 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2235 window->type = TYPE_MAXIMIZED;
2236 } else {
2237 wl_shell_surface_set_toplevel(window->shell_surface);
2238 window->type = TYPE_TOPLEVEL;
2239 window_schedule_resize(window,
2240 window->saved_allocation.width,
2241 window->saved_allocation.height);
2242 }
2243}
2244
2245void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002246window_set_user_data(struct window *window, void *data)
2247{
2248 window->user_data = data;
2249}
2250
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002251void *
2252window_get_user_data(struct window *window)
2253{
2254 return window->user_data;
2255}
2256
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002257void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002258window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002259 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002260{
2261 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002262}
2263
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002264void
2265window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002266 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002267{
2268 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002269}
2270
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002271void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002272window_set_data_handler(struct window *window, window_data_handler_t handler)
2273{
2274 window->data_handler = handler;
2275}
2276
2277void
2278window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2279{
2280 window->drop_handler = handler;
2281}
2282
2283void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002284window_set_close_handler(struct window *window,
2285 window_close_handler_t handler)
2286{
2287 window->close_handler = handler;
2288}
2289
2290void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002291window_set_title(struct window *window, const char *title)
2292{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002293 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002294 window->title = strdup(title);
2295}
2296
2297const char *
2298window_get_title(struct window *window)
2299{
2300 return window->title;
2301}
2302
2303void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002304display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2305 int32_t x, int32_t y, int32_t width, int32_t height)
2306{
2307 struct wl_buffer *buffer;
2308
2309 buffer = display_get_buffer_for_surface(display, cairo_surface);
2310
2311 wl_buffer_damage(buffer, x, y, width, height);
2312}
2313
2314void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002315window_damage(struct window *window, int32_t x, int32_t y,
2316 int32_t width, int32_t height)
2317{
2318 wl_surface_damage(window->surface, x, y, width, height);
2319}
2320
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002321static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002322window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002323{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002324 struct window *window;
2325
2326 window = malloc(sizeof *window);
2327 if (window == NULL)
2328 return NULL;
2329
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002330 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002331 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002332 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002333 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002334 if (display->shell) {
2335 window->shell_surface =
2336 wl_shell_get_shell_surface(display->shell,
2337 window->surface);
2338 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002339 window->allocation.x = 0;
2340 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002341 window->allocation.width = 0;
2342 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002343 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002344 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002345 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002346 window->input_region = NULL;
2347 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002348
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002349 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002350#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002351 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2352 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002353#else
2354 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2355#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002356 else
2357 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002358
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002359 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002360 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002361
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002362 if (window->shell_surface) {
2363 wl_shell_surface_set_user_data(window->shell_surface, window);
2364 wl_shell_surface_add_listener(window->shell_surface,
2365 &shell_surface_listener, window);
2366 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002367
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002368 return window;
2369}
2370
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002371struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002372window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002373{
2374 struct window *window;
2375
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002376 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002377 if (!window)
2378 return NULL;
2379
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002380 window->type = TYPE_TOPLEVEL;
2381 if (display->shell)
2382 wl_shell_surface_set_toplevel(window->shell_surface);
2383
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002384 return window;
2385}
2386
2387struct window *
2388window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002389 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002390{
2391 struct window *window;
2392
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002393 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002394 if (!window)
2395 return NULL;
2396
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002397 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002398 window->x = x;
2399 window->y = y;
2400
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002401 if (display->shell)
2402 wl_shell_surface_set_transient(window->shell_surface,
2403 window->parent->shell_surface,
2404 window->x, window->y, 0);
2405
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002406 return window;
2407}
2408
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002409static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002410menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002411{
2412 int next;
2413
2414 next = (sy - 8) / 20;
2415 if (menu->current != next) {
2416 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002417 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002418 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002419}
2420
2421static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002422menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002423 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002424 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002425{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002426 struct menu *menu = data;
2427
2428 if (widget == menu->widget)
2429 menu_set_item(data, y);
2430
2431 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002432}
2433
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002434static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002435menu_enter_handler(struct widget *widget,
2436 struct input *input, uint32_t time,
2437 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002438{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002439 struct menu *menu = data;
2440
2441 if (widget == menu->widget)
2442 menu_set_item(data, y);
2443
2444 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002445}
2446
2447static void
2448menu_leave_handler(struct widget *widget, struct input *input, void *data)
2449{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002450 struct menu *menu = data;
2451
2452 if (widget == menu->widget)
2453 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002454}
2455
2456static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002457menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002458 struct input *input, uint32_t time,
2459 int button, int state, void *data)
2460
2461{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002462 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002463
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002464 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002465 /* Either relase after press-drag-release or
2466 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002467 menu->func(menu->window->parent,
2468 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002469 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002470 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002471 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002472}
2473
2474static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002475menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002476{
2477 cairo_t *cr;
2478 const int32_t r = 3, margin = 3;
2479 struct menu *menu = data;
2480 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002481 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002482
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002483 cr = cairo_create(window->cairo_surface);
2484 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2485 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2486 cairo_paint(cr);
2487
2488 width = window->allocation.width;
2489 height = window->allocation.height;
2490 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002491
2492 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002493 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2494 cairo_fill(cr);
2495
2496 for (i = 0; i < menu->count; i++) {
2497 if (i == menu->current) {
2498 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2499 cairo_rectangle(cr, margin, i * 20 + margin,
2500 width - 2 * margin, 20);
2501 cairo_fill(cr);
2502 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2503 cairo_move_to(cr, 10, i * 20 + 16);
2504 cairo_show_text(cr, menu->entries[i]);
2505 } else {
2506 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2507 cairo_move_to(cr, 10, i * 20 + 16);
2508 cairo_show_text(cr, menu->entries[i]);
2509 }
2510 }
2511
2512 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002513}
2514
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002515void
2516window_show_menu(struct display *display,
2517 struct input *input, uint32_t time, struct window *parent,
2518 int32_t x, int32_t y,
2519 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002520{
2521 struct window *window;
2522 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002523 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002524
2525 menu = malloc(sizeof *menu);
2526 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002527 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002528
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002529 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002530 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002531 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002532
2533 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002534 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002535 menu->entries = entries;
2536 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002537 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002538 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002539 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002540 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002541 window->type = TYPE_MENU;
2542 window->x = x;
2543 window->y = y;
2544
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002545 wl_shell_surface_set_popup(window->shell_surface,
2546 input->input_device, time,
2547 window->parent->shell_surface,
2548 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002549
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002550 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002551 widget_set_enter_handler(menu->widget, menu_enter_handler);
2552 widget_set_leave_handler(menu->widget, menu_leave_handler);
2553 widget_set_motion_handler(menu->widget, menu_motion_handler);
2554 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002555
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002556 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002557 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002558}
2559
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002560void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002561window_set_buffer_type(struct window *window, enum window_buffer_type type)
2562{
2563 window->buffer_type = type;
2564}
2565
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002566
2567static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002568display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002569 struct wl_output *wl_output,
2570 int x, int y,
2571 int physical_width,
2572 int physical_height,
2573 int subpixel,
2574 const char *make,
2575 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002576{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002577 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002578
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002579 output->allocation.x = x;
2580 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002581}
2582
2583static void
2584display_handle_mode(void *data,
2585 struct wl_output *wl_output,
2586 uint32_t flags,
2587 int width,
2588 int height,
2589 int refresh)
2590{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002591 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002592 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002593
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002594 if (flags & WL_OUTPUT_MODE_CURRENT) {
2595 output->allocation.width = width;
2596 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002597 if (display->output_configure_handler)
2598 (*display->output_configure_handler)(
2599 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002600 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002601}
2602
2603static const struct wl_output_listener output_listener = {
2604 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002605 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002606};
2607
2608static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002609display_add_output(struct display *d, uint32_t id)
2610{
2611 struct output *output;
2612
2613 output = malloc(sizeof *output);
2614 if (output == NULL)
2615 return;
2616
2617 memset(output, 0, sizeof *output);
2618 output->display = d;
2619 output->output =
2620 wl_display_bind(d->display, id, &wl_output_interface);
2621 wl_list_insert(d->output_list.prev, &output->link);
2622
2623 wl_output_add_listener(output->output, &output_listener, output);
2624}
2625
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002626static void
2627output_destroy(struct output *output)
2628{
2629 if (output->destroy_handler)
2630 (*output->destroy_handler)(output, output->user_data);
2631
2632 wl_output_destroy(output->output);
2633 wl_list_remove(&output->link);
2634 free(output);
2635}
2636
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002637void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002638display_set_output_configure_handler(struct display *display,
2639 display_output_handler_t handler)
2640{
2641 struct output *output;
2642
2643 display->output_configure_handler = handler;
2644 if (!handler)
2645 return;
2646
2647 wl_list_for_each(output, &display->output_list, link)
2648 (*display->output_configure_handler)(output,
2649 display->user_data);
2650}
2651
2652void
2653output_set_user_data(struct output *output, void *data)
2654{
2655 output->user_data = data;
2656}
2657
2658void *
2659output_get_user_data(struct output *output)
2660{
2661 return output->user_data;
2662}
2663
2664void
2665output_set_destroy_handler(struct output *output,
2666 display_output_handler_t handler)
2667{
2668 output->destroy_handler = handler;
2669 /* FIXME: implement this, once we have way to remove outputs */
2670}
2671
2672void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002673output_get_allocation(struct output *output, struct rectangle *allocation)
2674{
2675 *allocation = output->allocation;
2676}
2677
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002678struct wl_output *
2679output_get_wl_output(struct output *output)
2680{
2681 return output->output;
2682}
2683
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002684static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002685display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002686{
2687 struct input *input;
2688
2689 input = malloc(sizeof *input);
2690 if (input == NULL)
2691 return;
2692
2693 memset(input, 0, sizeof *input);
2694 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002695 input->input_device =
2696 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002697 input->pointer_focus = NULL;
2698 input->keyboard_focus = NULL;
2699 wl_list_insert(d->input_list.prev, &input->link);
2700
2701 wl_input_device_add_listener(input->input_device,
2702 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002703 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002704
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002705 input->data_device =
2706 wl_data_device_manager_get_data_device(d->data_device_manager,
2707 input->input_device);
2708 wl_data_device_add_listener(input->data_device,
2709 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002710}
2711
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002712static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002713input_destroy(struct input *input)
2714{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002715 input_remove_keyboard_focus(input);
2716 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002717
2718 if (input->drag_offer)
2719 data_offer_destroy(input->drag_offer);
2720
2721 if (input->selection_offer)
2722 data_offer_destroy(input->selection_offer);
2723
2724 wl_data_device_destroy(input->data_device);
2725 wl_list_remove(&input->link);
2726 wl_input_device_destroy(input->input_device);
2727 free(input);
2728}
2729
2730static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002731display_handle_global(struct wl_display *display, uint32_t id,
2732 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002733{
2734 struct display *d = data;
2735
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002736 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002737 d->compositor =
2738 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002739 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002740 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002741 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002742 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002743 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002744 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002745 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002746 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002747 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2748 d->data_device_manager =
2749 wl_display_bind(display, id,
2750 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002751 }
2752}
2753
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002754static void
2755display_render_frame(struct display *d)
2756{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002757 cairo_t *cr;
2758
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002759 d->frame_radius = 8;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002760 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2761 cr = cairo_create(d->shadow);
2762 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2763 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002764 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002765 cairo_fill(cr);
2766 cairo_destroy(cr);
2767 blur_surface(d->shadow, 64);
2768
2769 d->active_frame =
2770 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2771 cr = cairo_create(d->active_frame);
2772 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2773 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002774 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002775 cairo_fill(cr);
2776 cairo_destroy(cr);
2777
2778 d->inactive_frame =
2779 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2780 cr = cairo_create(d->inactive_frame);
2781 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2782 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002783 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002784 cairo_fill(cr);
2785 cairo_destroy(cr);
2786}
2787
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002788static void
2789init_xkb(struct display *d)
2790{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002791 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002792
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002793 names.rules = "evdev";
2794 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002795 names.layout = option_xkb_layout;
2796 names.variant = option_xkb_variant;
2797 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002798
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002799 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002800 if (!d->xkb) {
2801 fprintf(stderr, "Failed to compile keymap\n");
2802 exit(1);
2803 }
2804}
2805
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002806static void
2807fini_xkb(struct display *display)
2808{
2809 xkb_free_keymap(display->xkb);
2810}
2811
Yuval Fledel45568f62010-12-06 09:18:12 -05002812static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002813init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002814{
2815 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002816 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002817
Rob Clark6396ed32012-03-11 19:48:41 -05002818#ifdef USE_CAIRO_GLESV2
2819# define GL_BIT EGL_OPENGL_ES2_BIT
2820#else
2821# define GL_BIT EGL_OPENGL_BIT
2822#endif
2823
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002824 static const EGLint argb_cfg_attribs[] = {
2825 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002826 EGL_RED_SIZE, 1,
2827 EGL_GREEN_SIZE, 1,
2828 EGL_BLUE_SIZE, 1,
2829 EGL_ALPHA_SIZE, 1,
2830 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002831 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002832 EGL_NONE
2833 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002834
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002835#ifdef USE_CAIRO_GLESV2
2836 static const EGLint context_attribs[] = {
2837 EGL_CONTEXT_CLIENT_VERSION, 2,
2838 EGL_NONE
2839 };
2840 EGLint api = EGL_OPENGL_ES_API;
2841#else
2842 EGLint *context_attribs = NULL;
2843 EGLint api = EGL_OPENGL_API;
2844#endif
2845
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002846 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002847 if (!eglInitialize(d->dpy, &major, &minor)) {
2848 fprintf(stderr, "failed to initialize display\n");
2849 return -1;
2850 }
2851
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002852 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002853 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2854 return -1;
2855 }
2856
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002857 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2858 &d->argb_config, 1, &n) || n != 1) {
2859 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002860 return -1;
2861 }
2862
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002863 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002864 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002865 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002866 fprintf(stderr, "failed to create context\n");
2867 return -1;
2868 }
2869
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002870 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002871 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002872 return -1;
2873 }
2874
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002875#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002876 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2877 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2878 fprintf(stderr, "failed to get cairo egl argb device\n");
2879 return -1;
2880 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002881#endif
2882
2883 return 0;
2884}
2885
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002886static void
2887fini_egl(struct display *display)
2888{
2889#ifdef HAVE_CAIRO_EGL
2890 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002891#endif
2892
2893 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2894 EGL_NO_CONTEXT);
2895
2896 eglTerminate(display->dpy);
2897 eglReleaseThread();
2898}
2899
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002900static int
2901event_mask_update(uint32_t mask, void *data)
2902{
2903 struct display *d = data;
2904
2905 d->mask = mask;
2906
2907 return 0;
2908}
2909
2910static void
2911handle_display_data(struct task *task, uint32_t events)
2912{
2913 struct display *display =
2914 container_of(task, struct display, display_task);
2915
2916 wl_display_iterate(display->display, display->mask);
2917}
2918
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002919struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002920display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002921{
2922 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002923
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002924 g_type_init();
2925
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002926 argc = parse_options(xkb_options,
2927 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002928
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002929 d = malloc(sizeof *d);
2930 if (d == NULL)
2931 return NULL;
2932
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002933 memset(d, 0, sizeof *d);
2934
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002935 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002936 if (d->display == NULL) {
2937 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002938 return NULL;
2939 }
2940
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002941 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2942 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2943 d->display_task.run = handle_display_data;
2944 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2945
2946 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002947 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002948 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002949
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002950 /* Set up listener so we'll catch all events. */
2951 wl_display_add_global_listener(d->display,
2952 display_handle_global, d);
2953
2954 /* Process connection events. */
2955 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002956 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002957 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002958
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002959 d->image_target_texture_2d =
2960 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2961 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2962 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2963
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002964 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002965
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002966 display_render_frame(d);
2967
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002968 wl_list_init(&d->window_list);
2969
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002970 init_xkb(d);
2971
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002972 return d;
2973}
2974
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002975static void
2976display_destroy_outputs(struct display *display)
2977{
2978 struct output *tmp;
2979 struct output *output;
2980
2981 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2982 output_destroy(output);
2983}
2984
Pekka Paalanene1207c72011-12-16 12:02:09 +02002985static void
2986display_destroy_inputs(struct display *display)
2987{
2988 struct input *tmp;
2989 struct input *input;
2990
2991 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2992 input_destroy(input);
2993}
2994
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002995void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002996display_destroy(struct display *display)
2997{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002998 if (!wl_list_empty(&display->window_list))
2999 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3000
3001 if (!wl_list_empty(&display->deferred_list))
3002 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3003
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003004 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003005 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003006
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003007 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003008
3009 cairo_surface_destroy(display->active_frame);
3010 cairo_surface_destroy(display->inactive_frame);
3011 cairo_surface_destroy(display->shadow);
3012 destroy_pointer_surfaces(display);
3013
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003014 fini_egl(display);
3015
Pekka Paalanenc2052982011-12-16 11:41:32 +02003016 if (display->shell)
3017 wl_shell_destroy(display->shell);
3018
3019 if (display->shm)
3020 wl_shm_destroy(display->shm);
3021
3022 if (display->data_device_manager)
3023 wl_data_device_manager_destroy(display->data_device_manager);
3024
3025 wl_compositor_destroy(display->compositor);
3026
3027 close(display->epoll_fd);
3028
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003029 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003030 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003031 free(display);
3032}
3033
3034void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003035display_set_user_data(struct display *display, void *data)
3036{
3037 display->user_data = data;
3038}
3039
3040void *
3041display_get_user_data(struct display *display)
3042{
3043 return display->user_data;
3044}
3045
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003046struct wl_display *
3047display_get_display(struct display *display)
3048{
3049 return display->display;
3050}
3051
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003052struct output *
3053display_get_output(struct display *display)
3054{
3055 return container_of(display->output_list.next, struct output, link);
3056}
3057
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003058struct wl_compositor *
3059display_get_compositor(struct display *display)
3060{
3061 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003062}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003063
3064EGLDisplay
3065display_get_egl_display(struct display *d)
3066{
3067 return d->dpy;
3068}
3069
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003070struct wl_data_source *
3071display_create_data_source(struct display *display)
3072{
3073 return wl_data_device_manager_create_data_source(display->data_device_manager);
3074}
3075
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003076EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003077display_get_argb_egl_config(struct display *d)
3078{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003079 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003080}
3081
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003082struct wl_shell *
3083display_get_shell(struct display *display)
3084{
3085 return display->shell;
3086}
3087
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003088int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003089display_acquire_window_surface(struct display *display,
3090 struct window *window,
3091 EGLContext ctx)
3092{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003093#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003094 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003095 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003096
3097 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003098 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003099 device = cairo_surface_get_device(window->cairo_surface);
3100 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003101 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003102
Benjamin Franzke0c991632011-09-27 21:57:31 +02003103 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003104 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003105 ctx = display->argb_ctx;
3106 else
3107 assert(0);
3108 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003109
3110 data = cairo_surface_get_user_data(window->cairo_surface,
3111 &surface_data_key);
3112
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003113 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003114 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003115 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3116 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003117
3118 return 0;
3119#else
3120 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003121#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003122}
3123
3124void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003125display_release_window_surface(struct display *display,
3126 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003127{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003128#ifdef HAVE_CAIRO_EGL
3129 cairo_device_t *device;
3130
3131 device = cairo_surface_get_device(window->cairo_surface);
3132 if (!device)
3133 return;
3134
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003135 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003136 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003137 cairo_device_release(device);
3138#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003139}
3140
3141void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003142display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003143{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003144 wl_list_insert(&display->deferred_list, &task->link);
3145}
3146
3147void
3148display_watch_fd(struct display *display,
3149 int fd, uint32_t events, struct task *task)
3150{
3151 struct epoll_event ep;
3152
3153 ep.events = events;
3154 ep.data.ptr = task;
3155 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3156}
3157
3158void
3159display_run(struct display *display)
3160{
3161 struct task *task;
3162 struct epoll_event ep[16];
3163 int i, count;
3164
Pekka Paalanen826d7952011-12-15 10:14:07 +02003165 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003166 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003167 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003168
3169 while (!wl_list_empty(&display->deferred_list)) {
3170 task = container_of(display->deferred_list.next,
3171 struct task, link);
3172 wl_list_remove(&task->link);
3173 task->run(task, 0);
3174 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003175
3176 if (!display->running)
3177 break;
3178
3179 wl_display_flush(display->display);
3180
3181 count = epoll_wait(display->epoll_fd,
3182 ep, ARRAY_LENGTH(ep), -1);
3183 for (i = 0; i < count; i++) {
3184 task = ep[i].data.ptr;
3185 task->run(task, ep[i].events);
3186 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003187 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003188}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003189
3190void
3191display_exit(struct display *display)
3192{
3193 display->running = 0;
3194}