blob: 74ddeddb057e528ad3f343966d8e2da3d47a5ef3 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050059#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040063#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050068 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050069 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040070 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040071 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040072 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040073 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050074 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020075 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020076 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040077
78 int display_fd;
79 uint32_t mask;
80 struct task display_task;
81
82 int epoll_fd;
83 struct wl_list deferred_list;
84
Pekka Paalanen826d7952011-12-15 10:14:07 +020085 int running;
86
Kristian Høgsberg478d9262010-06-08 20:34:11 -040087 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040088 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050089 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040090 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050091 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040092 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040093 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040094
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050095 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
96 PFNEGLCREATEIMAGEKHRPROC create_image;
97 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020098
99 display_output_handler_t output_configure_handler;
100
101 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500102};
103
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400106 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500107 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400108 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500109 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110 TYPE_CUSTOM
111};
112
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500113struct window {
114 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500115 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500118 struct wl_region *input_region;
119 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500120 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500121 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500122 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500123 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400124 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400125 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400126 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400127 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400128 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400129 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400130 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400131 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400132 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500133
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500134 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500135
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500136 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500137 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400138 window_data_handler_t data_handler;
139 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500140 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400141
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200142 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500143 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500144
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500145 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400146 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500147};
148
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500149struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500150 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500151 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400152 struct wl_list link;
153 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500154 widget_resize_handler_t resize_handler;
155 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500156 widget_enter_handler_t enter_handler;
157 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500158 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500159 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400160 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500161 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400162};
163
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400164struct input {
165 struct display *display;
166 struct wl_input_device *input_device;
167 struct window *pointer_focus;
168 struct window *keyboard_focus;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400169 int current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400170 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200171 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400172 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400173
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500174 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500175 struct widget *grab;
176 uint32_t grab_button;
177
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400178 struct wl_data_device *data_device;
179 struct data_offer *drag_offer;
180 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400181};
182
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500183struct output {
184 struct display *display;
185 struct wl_output *output;
186 struct rectangle allocation;
187 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200188
189 display_output_handler_t destroy_handler;
190 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500191};
192
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500193struct frame {
194 struct widget *widget;
195 struct widget *child;
196 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400197 int width;
198 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500199};
200
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500201struct menu {
202 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500203 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500204 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500205 const char **entries;
206 uint32_t time;
207 int current;
208 int count;
209 menu_func_t func;
210};
211
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400212enum {
213 POINTER_DEFAULT = 100,
214 POINTER_UNSET
215};
216
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500217enum window_location {
218 WINDOW_INTERIOR = 0,
219 WINDOW_RESIZING_TOP = 1,
220 WINDOW_RESIZING_BOTTOM = 2,
221 WINDOW_RESIZING_LEFT = 4,
222 WINDOW_RESIZING_TOP_LEFT = 5,
223 WINDOW_RESIZING_BOTTOM_LEFT = 6,
224 WINDOW_RESIZING_RIGHT = 8,
225 WINDOW_RESIZING_TOP_RIGHT = 9,
226 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
227 WINDOW_RESIZING_MASK = 15,
228 WINDOW_EXTERIOR = 16,
229 WINDOW_TITLEBAR = 17,
230 WINDOW_CLIENT_AREA = 18,
231};
232
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400233const char *option_xkb_layout = "us";
234const char *option_xkb_variant = "";
235const char *option_xkb_options = "";
236
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400237static const struct weston_option xkb_options[] = {
238 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
239 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
240 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400241};
242
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400243static const cairo_user_data_key_t surface_data_key;
244struct surface_data {
245 struct wl_buffer *buffer;
246};
247
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500248#define MULT(_d,c,a,t) \
249 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
250
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500251#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400252
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100253struct egl_window_surface_data {
254 struct display *display;
255 struct wl_surface *surface;
256 struct wl_egl_window *window;
257 EGLSurface surf;
258};
259
260static void
261egl_window_surface_data_destroy(void *p)
262{
263 struct egl_window_surface_data *data = p;
264 struct display *d = data->display;
265
266 eglDestroySurface(d->dpy, data->surf);
267 wl_egl_window_destroy(data->window);
268 data->surface = NULL;
269
270 free(p);
271}
272
273static cairo_surface_t *
274display_create_egl_window_surface(struct display *display,
275 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400276 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100277 struct rectangle *rectangle)
278{
279 cairo_surface_t *cairo_surface;
280 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400281 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200282 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100283
284 data = malloc(sizeof *data);
285 if (data == NULL)
286 return NULL;
287
288 data->display = display;
289 data->surface = surface;
290
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500291 config = display->argb_config;
292 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400293
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400294 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100295 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100297
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400298 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500299 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300
Benjamin Franzke0c991632011-09-27 21:57:31 +0200301 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100302 data->surf,
303 rectangle->width,
304 rectangle->height);
305
306 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
307 data, egl_window_surface_data_destroy);
308
309 return cairo_surface;
310}
311
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200314 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400315 EGLImageKHR image;
316 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800317 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500318 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319};
320
321static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400323{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500324 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800325 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326
Benjamin Franzke0c991632011-09-27 21:57:31 +0200327 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200329 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800330
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500331 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500333 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500334 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335}
336
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500337EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338display_get_image_for_egl_image_surface(struct display *display,
339 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500340{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500341 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500342
343 data = cairo_surface_get_user_data (surface, &surface_data_key);
344
345 return data->image;
346}
347
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500348static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400350 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500353 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354 EGLDisplay dpy = display->dpy;
355 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200356 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357
358 data = malloc(sizeof *data);
359 if (data == NULL)
360 return NULL;
361
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800362 data->display = display;
363
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400364 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400365 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500366 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000367 free(data);
368 return NULL;
369 }
370
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500371 data->device = display->argb_device;
372 content = CAIRO_CONTENT_COLOR_ALPHA;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400373
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500374 data->image = display->create_image(dpy, NULL,
375 EGL_NATIVE_PIXMAP_KHR,
376 (EGLClientBuffer) data->pixmap,
377 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500378 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500379 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500380 free(data);
381 return NULL;
382 }
383
384 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400385 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500386
Benjamin Franzke0c991632011-09-27 21:57:31 +0200387 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400388 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400389 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500390 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200391 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 surface = cairo_gl_surface_create_for_texture(data->device,
394 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395 data->texture,
396 rectangle->width,
397 rectangle->height);
398
399 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500400 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401
402 return surface;
403}
404
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500405static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500406display_create_egl_image_surface_from_file(struct display *display,
407 const char *filename,
408 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409{
410 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400411 pixman_image_t *image;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500412 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400414 image = load_image(filename);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400415
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200416 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000417 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400418 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000419 return NULL;
420 }
421
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800422 data = cairo_surface_get_user_data(surface, &surface_data_key);
423
Benjamin Franzke0c991632011-09-27 21:57:31 +0200424 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800425 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800426 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400427 GL_RGBA, GL_UNSIGNED_BYTE,
428 pixman_image_get_data(image));
Benjamin Franzke0c991632011-09-27 21:57:31 +0200429 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400430
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400431 pixman_image_unref(image);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400432
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400433 return surface;
434}
435
436#endif
437
438struct wl_buffer *
439display_get_buffer_for_surface(struct display *display,
440 cairo_surface_t *surface)
441{
442 struct surface_data *data;
443
444 data = cairo_surface_get_user_data (surface, &surface_data_key);
445
446 return data->buffer;
447}
448
449struct shm_surface_data {
450 struct surface_data data;
451 void *map;
452 size_t length;
453};
454
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500455static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400456shm_surface_data_destroy(void *p)
457{
458 struct shm_surface_data *data = p;
459
460 wl_buffer_destroy(data->data.buffer);
461 munmap(data->map, data->length);
462}
463
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500464static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400465display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400466 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400467{
468 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400469 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400470 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800471 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400472 char filename[] = "/tmp/wayland-shm-XXXXXX";
473
474 data = malloc(sizeof *data);
475 if (data == NULL)
476 return NULL;
477
478 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
479 rectangle->width);
480 data->length = stride * rectangle->height;
481 fd = mkstemp(filename);
482 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000483 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400484 return NULL;
485 }
486 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000487 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488 close(fd);
489 return NULL;
490 }
491
492 data->map = mmap(NULL, data->length,
493 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
494 unlink(filename);
495
496 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000497 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498 close(fd);
499 return NULL;
500 }
501
502 surface = cairo_image_surface_create_for_data (data->map,
503 CAIRO_FORMAT_ARGB32,
504 rectangle->width,
505 rectangle->height,
506 stride);
507
508 cairo_surface_set_user_data (surface, &surface_data_key,
509 data, shm_surface_data_destroy);
510
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400511 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500512 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400513 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500514 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400515
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516 data->data.buffer = wl_shm_create_buffer(display->shm,
517 fd,
518 rectangle->width,
519 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400520 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521
522 close(fd);
523
524 return surface;
525}
526
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500527static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528display_create_shm_surface_from_file(struct display *display,
529 const char *filename,
530 struct rectangle *rect)
531{
532 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400533 pixman_image_t *image;
534 void *dest;
535 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400536
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400537 image = load_image(filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400539 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000540 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400541 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000542 return NULL;
543 }
544
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400545 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
546 dest = cairo_image_surface_get_data(surface);
547 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400549 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550
551 return surface;
552}
553
nobled7b87cb02011-02-01 18:51:47 +0000554static int
555check_size(struct rectangle *rect)
556{
557 if (rect->width && rect->height)
558 return 0;
559
560 fprintf(stderr, "tried to create surface of "
561 "width: %d, height: %d\n", rect->width, rect->height);
562 return -1;
563}
564
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400565cairo_surface_t *
566display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100567 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400568 struct rectangle *rectangle,
569 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400570{
nobled7b87cb02011-02-01 18:51:47 +0000571 if (check_size(rectangle) < 0)
572 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500573#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500574 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100575 if (surface)
576 return display_create_egl_window_surface(display,
577 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400578 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100579 rectangle);
580 else
581 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400582 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100583 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500584 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400586 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400587}
588
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500589static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400590display_create_surface_from_file(struct display *display,
591 const char *filename,
592 struct rectangle *rectangle)
593{
nobled7b87cb02011-02-01 18:51:47 +0000594 if (check_size(rectangle) < 0)
595 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500596#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500597 if (display->dpy) {
598 return display_create_egl_image_surface_from_file(display,
599 filename,
600 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500601 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400602#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500603 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400604}
Yuval Fledel45568f62010-12-06 09:18:12 -0500605 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400606 const char *filename;
607 int hotspot_x, hotspot_y;
608} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500609 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
610 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
611 { DATADIR "/weston/bottom_side.png", 16, 20 },
612 { DATADIR "/weston/grabbing.png", 20, 17 },
613 { DATADIR "/weston/left_ptr.png", 10, 5 },
614 { DATADIR "/weston/left_side.png", 10, 20 },
615 { DATADIR "/weston/right_side.png", 30, 19 },
616 { DATADIR "/weston/top_left_corner.png", 8, 8 },
617 { DATADIR "/weston/top_right_corner.png", 26, 8 },
618 { DATADIR "/weston/top_side.png", 18, 8 },
619 { DATADIR "/weston/xterm.png", 15, 15 },
620 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400621};
622
623static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400624create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400625{
626 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400627 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400628 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400629
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400630 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400631 display->pointer_surfaces =
632 malloc(count * sizeof *display->pointer_surfaces);
633 rect.width = width;
634 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400635 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400636 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637 display_create_surface_from_file(display,
638 pointer_images[i].filename,
639 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100640 if (!display->pointer_surfaces[i]) {
641 fprintf(stderr, "Error loading pointer image: %s\n",
642 pointer_images[i].filename);
643 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400644 }
645
646}
647
Pekka Paalanen325bb602011-12-19 10:31:45 +0200648static void
649destroy_pointer_surfaces(struct display *display)
650{
651 int i, count;
652
653 count = ARRAY_LENGTH(pointer_images);
654 for (i = 0; i < count; ++i) {
655 if (display->pointer_surfaces[i])
656 cairo_surface_destroy(display->pointer_surfaces[i]);
657 }
658 free(display->pointer_surfaces);
659}
660
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400661cairo_surface_t *
662display_get_pointer_surface(struct display *display, int pointer,
663 int *width, int *height,
664 int *hotspot_x, int *hotspot_y)
665{
666 cairo_surface_t *surface;
667
668 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500669#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400670 *width = cairo_gl_surface_get_width(surface);
671 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000672#else
673 *width = cairo_image_surface_get_width(surface);
674 *height = cairo_image_surface_get_height(surface);
675#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400676 *hotspot_x = pointer_images[pointer].hotspot_x;
677 *hotspot_y = pointer_images[pointer].hotspot_y;
678
679 return cairo_surface_reference(surface);
680}
681
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400682static void
683window_attach_surface(struct window *window);
684
685static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400686free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400687{
688 struct window *window = data;
689
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400690 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400691 cairo_surface_destroy(window->pending_surface);
692 window->pending_surface = NULL;
693 if (window->cairo_surface)
694 window_attach_surface(window);
695}
696
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400697static const struct wl_callback_listener free_surface_listener = {
698 free_surface
699};
700
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500701static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200702window_get_resize_dx_dy(struct window *window, int *x, int *y)
703{
704 if (window->resize_edges & WINDOW_RESIZING_LEFT)
705 *x = window->server_allocation.width - window->allocation.width;
706 else
707 *x = 0;
708
709 if (window->resize_edges & WINDOW_RESIZING_TOP)
710 *y = window->server_allocation.height -
711 window->allocation.height;
712 else
713 *y = 0;
714
715 window->resize_edges = 0;
716}
717
718static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500719window_attach_surface(struct window *window)
720{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400721 struct display *display = window->display;
722 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400723 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000724#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000726#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500727 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728
729 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000730#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
732 data = cairo_surface_get_user_data(window->cairo_surface,
733 &surface_data_key);
734
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735 cairo_gl_surface_swapbuffers(window->cairo_surface);
736 wl_egl_window_get_attached_size(data->window,
737 &window->server_allocation.width,
738 &window->server_allocation.height);
739 break;
740 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000741#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742 case WINDOW_BUFFER_TYPE_SHM:
743 if (window->pending_surface != NULL)
744 return;
745
746 window->pending_surface = window->cairo_surface;
747 window->cairo_surface = NULL;
748
749 buffer =
750 display_get_buffer_for_surface(display,
751 window->pending_surface);
752
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200753 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100754 wl_surface_attach(window->surface, buffer, x, y);
755 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400756 cb = wl_display_sync(display->display);
757 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100758 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000759 default:
760 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100761 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500762
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500763 if (window->input_region) {
764 wl_surface_set_input_region(window->surface,
765 window->input_region);
766 wl_region_destroy(window->input_region);
767 window->input_region = NULL;
768 }
769
770 if (window->opaque_region) {
771 wl_surface_set_opaque_region(window->surface,
772 window->opaque_region);
773 wl_region_destroy(window->opaque_region);
774 window->opaque_region = NULL;
775 }
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500776
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500777 wl_surface_damage(window->surface, 0, 0,
778 window->allocation.width,
779 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500780}
781
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500782void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400783window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500784{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400785 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100786 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500787}
788
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400789void
790window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400791{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500792 cairo_surface_reference(surface);
793
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400794 if (window->cairo_surface != NULL)
795 cairo_surface_destroy(window->cairo_surface);
796
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500797 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400798}
799
Benjamin Franzke22d54812011-07-16 19:50:32 +0000800#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100801static void
802window_resize_cairo_window_surface(struct window *window)
803{
804 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200805 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100806
807 data = cairo_surface_get_user_data(window->cairo_surface,
808 &surface_data_key);
809
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200810 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811 wl_egl_window_resize(data->window,
812 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200813 window->allocation.height,
814 x,y);
815
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816 cairo_gl_surface_set_size(window->cairo_surface,
817 window->allocation.width,
818 window->allocation.height);
819}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000820#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400822struct display *
823window_get_display(struct window *window)
824{
825 return window->display;
826}
827
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400828void
829window_create_surface(struct window *window)
830{
831 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400832 uint32_t flags = 0;
833
834 if (!window->transparent)
835 flags = SURFACE_OPAQUE;
836
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500838#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
840 if (window->cairo_surface) {
841 window_resize_cairo_window_surface(window);
842 return;
843 }
844 surface = display_create_surface(window->display,
845 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400846 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500848 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400849 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100850 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400851 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400852 break;
853#endif
854 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400855 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400856 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400857 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800858 default:
859 surface = NULL;
860 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400861 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400862
863 window_set_surface(window, surface);
864 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400865}
866
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200867static void frame_destroy(struct frame *frame);
868
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400869void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500870window_destroy(struct window *window)
871{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200872 struct display *display = window->display;
873 struct input *input;
874
875 if (window->redraw_scheduled)
876 wl_list_remove(&window->redraw_task.link);
877
878 wl_list_for_each(input, &display->input_list, link) {
879 if (input->pointer_focus == window)
880 input->pointer_focus = NULL;
881 if (input->keyboard_focus == window)
882 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500883 if (input->focus_widget &&
884 input->focus_widget->window == window)
885 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200886 }
887
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500888 if (window->input_region)
889 wl_region_destroy(window->input_region);
890 if (window->opaque_region)
891 wl_region_destroy(window->opaque_region);
892
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200893 if (window->frame)
894 frame_destroy(window->frame);
895
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200896 if (window->shell_surface)
897 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500898 wl_surface_destroy(window->surface);
899 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200900
901 if (window->cairo_surface != NULL)
902 cairo_surface_destroy(window->cairo_surface);
903 if (window->pending_surface != NULL)
904 cairo_surface_destroy(window->pending_surface);
905
906 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500907 free(window);
908}
909
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500910static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500911widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400912{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500913 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400914
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500915 wl_list_for_each(child, &widget->child_list, link) {
916 target = widget_find_widget(child, x, y);
917 if (target)
918 return target;
919 }
920
921 if (widget->allocation.x <= x &&
922 x < widget->allocation.x + widget->allocation.width &&
923 widget->allocation.y <= y &&
924 y < widget->allocation.y + widget->allocation.height) {
925 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400926 }
927
928 return NULL;
929}
930
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500931static struct widget *
932widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400933{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500934 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400935
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500936 widget = malloc(sizeof *widget);
937 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500938 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500939 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500940 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500941 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500942 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500943
944 return widget;
945}
946
947struct widget *
948window_add_widget(struct window *window, void *data)
949{
950 window->widget = widget_create(window, data);
951 wl_list_init(&window->widget->link);
952
953 return window->widget;
954}
955
956struct widget *
957widget_add_widget(struct widget *parent, void *data)
958{
959 struct widget *widget;
960
961 widget = widget_create(parent->window, data);
962 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400963
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500964 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400965}
966
967void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500968widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400969{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200970 struct display *display = widget->window->display;
971 struct input *input;
972
973 wl_list_for_each(input, &display->input_list, link) {
974 if (input->focus_widget == widget)
975 input->focus_widget = NULL;
976 }
977
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500978 wl_list_remove(&widget->link);
979 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400980}
981
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400982void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500983widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400984{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500985 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400986}
987
988void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500989widget_set_size(struct widget *widget, int32_t width, int32_t height)
990{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500991 widget->allocation.width = width;
992 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500993}
994
995void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500996widget_set_allocation(struct widget *widget,
997 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999 widget->allocation.x = x;
1000 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001001 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001002}
1003
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001004void
1005widget_set_transparent(struct widget *widget, int transparent)
1006{
1007 widget->opaque = !transparent;
1008}
1009
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001010void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001011widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001012{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001013 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001014}
1015
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001016void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001017widget_set_resize_handler(struct widget *widget,
1018 widget_resize_handler_t handler)
1019{
1020 widget->resize_handler = handler;
1021}
1022
1023void
1024widget_set_redraw_handler(struct widget *widget,
1025 widget_redraw_handler_t handler)
1026{
1027 widget->redraw_handler = handler;
1028}
1029
1030void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001031widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001032{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001033 widget->enter_handler = handler;
1034}
1035
1036void
1037widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1038{
1039 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001040}
1041
1042void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001043widget_set_motion_handler(struct widget *widget,
1044 widget_motion_handler_t handler)
1045{
1046 widget->motion_handler = handler;
1047}
1048
1049void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001050widget_set_button_handler(struct widget *widget,
1051 widget_button_handler_t handler)
1052{
1053 widget->button_handler = handler;
1054}
1055
1056void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001057widget_schedule_redraw(struct widget *widget)
1058{
1059 window_schedule_redraw(widget->window);
1060}
1061
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001062cairo_surface_t *
1063window_get_surface(struct window *window)
1064{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001065 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001066}
1067
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001068struct wl_surface *
1069window_get_wl_surface(struct window *window)
1070{
1071 return window->surface;
1072}
1073
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001074struct wl_shell_surface *
1075window_get_wl_shell_surface(struct window *window)
1076{
1077 return window->shell_surface;
1078}
1079
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001080static void
1081frame_resize_handler(struct widget *widget,
1082 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001083{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001084 struct frame *frame = data;
1085 struct widget *child = frame->child;
1086 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001087 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001088 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001089 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001090
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001091 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001092 decoration_width = (frame->width + frame->margin) * 2;
1093 decoration_height = frame->width +
1094 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001095
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001096 allocation.x = frame->width + frame->margin;
1097 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001098 allocation.width = width - decoration_width;
1099 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001100
1101 widget->window->input_region =
1102 wl_compositor_create_region(display->compositor);
1103 wl_region_add(widget->window->input_region,
1104 frame->margin, frame->margin,
1105 width - 2 * frame->margin,
1106 height - 2 * frame->margin);
1107
1108 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001109 } else {
1110 decoration_width = 0;
1111 decoration_height = 0;
1112
1113 allocation.x = 0;
1114 allocation.y = 0;
1115 allocation.width = width;
1116 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001117 opaque_margin = 0;
1118 }
1119
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001120 widget_set_allocation(child, allocation.x, allocation.y,
1121 allocation.width, allocation.height);
1122
1123 if (child->resize_handler)
1124 child->resize_handler(child,
1125 allocation.width,
1126 allocation.height,
1127 child->user_data);
1128
1129 widget_set_allocation(widget, 0, 0,
1130 child->allocation.width + decoration_width,
1131 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001132
1133 if (child->opaque) {
1134 widget->window->opaque_region =
1135 wl_compositor_create_region(display->compositor);
1136 wl_region_add(widget->window->opaque_region,
1137 opaque_margin, opaque_margin,
1138 widget->allocation.width - 2 * opaque_margin,
1139 widget->allocation.height - 2 * opaque_margin);
1140 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001141}
1142
1143static void
1144frame_redraw_handler(struct widget *widget, void *data)
1145{
1146 struct frame *frame = data;
1147 cairo_t *cr;
1148 cairo_text_extents_t extents;
1149 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001150 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001151 struct window *window = widget->window;
1152
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001153 if (window->type == TYPE_FULLSCREEN)
1154 return;
1155
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001156 width = widget->allocation.width;
1157 height = widget->allocation.height;
1158
1159 cr = cairo_create(window->cairo_surface);
1160
1161 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1162 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1163 cairo_paint(cr);
1164
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001165 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001166 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001167 2, 2, width + 8, height + 8,
1168 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169
1170 if (window->keyboard_device)
1171 source = window->display->active_frame;
1172 else
1173 source = window->display->inactive_frame;
1174
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001175 tile_source(cr, source,
1176 frame->margin, frame->margin,
1177 width - frame->margin * 2, height - frame->margin * 2,
1178 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001179
1180 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001181 cairo_select_font_face(cr, "sans",
1182 CAIRO_FONT_SLANT_NORMAL,
1183 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001184 cairo_set_font_size(cr, 14);
1185 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001186 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001187 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001188 if (window->keyboard_device) {
1189 cairo_move_to(cr, x + 1, y + 1);
1190 cairo_set_source_rgb(cr, 1, 1, 1);
1191 cairo_show_text(cr, window->title);
1192 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001193 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001194 cairo_show_text(cr, window->title);
1195 } else {
1196 cairo_move_to(cr, x, y);
1197 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1198 cairo_show_text(cr, window->title);
1199 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001200
1201 cairo_destroy(cr);
1202}
1203
1204static int
1205frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1206{
1207 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001208 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209 const int grip_size = 8;
1210
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001211 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001212 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001213 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001214 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001215 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001216 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001217 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001218 hlocation = WINDOW_RESIZING_RIGHT;
1219 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001220 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001221
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001222 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001223 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001224 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001225 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001226 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001227 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001228 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001229 vlocation = WINDOW_RESIZING_BOTTOM;
1230 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001231 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001233 location = vlocation | hlocation;
1234 if (location & WINDOW_EXTERIOR)
1235 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001236 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001237 location = WINDOW_TITLEBAR;
1238 else if (location == WINDOW_INTERIOR)
1239 location = WINDOW_CLIENT_AREA;
1240
1241 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001242}
1243
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001244static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001246{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001247 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001248
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001250 switch (location) {
1251 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001252 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001253 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001254 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001255 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001256 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001258 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001260 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001261 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001262 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001264 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001265 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001266 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001267 case WINDOW_EXTERIOR:
1268 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001269 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001270 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001272}
1273
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001274static void
1275frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001276{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277 switch (index) {
1278 case 0: /* close */
1279 if (window->close_handler)
1280 window->close_handler(window->parent,
1281 window->user_data);
1282 else
1283 display_exit(window->display);
1284 break;
1285 case 1: /* fullscreen */
1286 /* we don't have a way to get out of fullscreen for now */
1287 window_set_fullscreen(window, 1);
1288 break;
1289 case 2: /* rotate */
1290 case 3: /* scale */
1291 break;
1292 }
1293}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001294
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001295void
1296window_show_frame_menu(struct window *window,
1297 struct input *input, uint32_t time)
1298{
1299 int32_t x, y;
1300
1301 static const char *entries[] = {
1302 "Close", "Fullscreen", "Rotate", "Scale"
1303 };
1304
1305 input_get_position(input, &x, &y);
1306 window_show_menu(window->display, input, time, window,
1307 x - 10, y - 10, frame_menu_func, entries, 4);
1308}
1309
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001310static int
1311frame_enter_handler(struct widget *widget,
1312 struct input *input, uint32_t time,
1313 int32_t x, int32_t y, void *data)
1314{
1315 return frame_get_pointer_image_for_location(data, input);
1316}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001317
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001318static int
1319frame_motion_handler(struct widget *widget,
1320 struct input *input, uint32_t time,
1321 int32_t x, int32_t y, void *data)
1322{
1323 return frame_get_pointer_image_for_location(data, input);
1324}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001325
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001326static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001327frame_button_handler(struct widget *widget,
1328 struct input *input, uint32_t time,
1329 int button, int state, void *data)
1330
1331{
1332 struct frame *frame = data;
1333 struct window *window = widget->window;
1334 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001335 location = frame_get_pointer_location(frame, input->sx, input->sy);
1336
1337 if (window->display->shell && button == BTN_LEFT && state == 1) {
1338 switch (location) {
1339 case WINDOW_TITLEBAR:
1340 if (!window->shell_surface)
1341 break;
1342 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001343 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001344 wl_shell_surface_move(window->shell_surface,
1345 input_get_input_device(input),
1346 time);
1347 break;
1348 case WINDOW_RESIZING_TOP:
1349 case WINDOW_RESIZING_BOTTOM:
1350 case WINDOW_RESIZING_LEFT:
1351 case WINDOW_RESIZING_RIGHT:
1352 case WINDOW_RESIZING_TOP_LEFT:
1353 case WINDOW_RESIZING_TOP_RIGHT:
1354 case WINDOW_RESIZING_BOTTOM_LEFT:
1355 case WINDOW_RESIZING_BOTTOM_RIGHT:
1356 if (!window->shell_surface)
1357 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001358 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001359 wl_shell_surface_resize(window->shell_surface,
1360 input_get_input_device(input),
1361 time, location);
1362 break;
1363 }
1364 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001365 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001366 }
1367}
1368
1369struct widget *
1370frame_create(struct window *window, void *data)
1371{
1372 struct frame *frame;
1373
1374 frame = malloc(sizeof *frame);
1375 memset(frame, 0, sizeof *frame);
1376
1377 frame->widget = window_add_widget(window, frame);
1378 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001379 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001380 frame->width = 4;
1381 frame->titlebar_height = 30
1382;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001383 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1384 widget_set_resize_handler(frame->widget, frame_resize_handler);
1385 widget_set_enter_handler(frame->widget, frame_enter_handler);
1386 widget_set_motion_handler(frame->widget, frame_motion_handler);
1387 widget_set_button_handler(frame->widget, frame_button_handler);
1388
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001389 window->frame = frame;
1390
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001391 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001392}
1393
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001394static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001395frame_destroy(struct frame *frame)
1396{
1397 /* frame->child must be destroyed by the application */
1398 widget_destroy(frame->widget);
1399 free(frame);
1400}
1401
1402static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001403input_set_focus_widget(struct input *input, struct widget *focus,
1404 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001405{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001406 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001407 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001408
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001409 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001410 return;
1411
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001412 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001413 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001414 widget = old;
1415 if (input->grab)
1416 widget = input->grab;
1417 if (widget->leave_handler)
1418 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001419 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001420 }
1421
1422 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001423 widget = focus;
1424 if (input->grab)
1425 widget = input->grab;
1426 if (widget->enter_handler)
1427 pointer = widget->enter_handler(focus, input, time,
1428 x, y,
1429 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001430 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001431
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001432 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001433 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001434}
1435
1436static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001437input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001438 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001439{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001440 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001441 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001442 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001443 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001444
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001445 input->sx = sx;
1446 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001447
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001448 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001449 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001450 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001451 }
1452
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001453 if (input->grab)
1454 widget = input->grab;
1455 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001456 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001457 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001458 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001459 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001460 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001461
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001462 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001463}
1464
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001465void
1466input_grab(struct input *input, struct widget *widget, uint32_t button)
1467{
1468 input->grab = widget;
1469 input->grab_button = button;
1470}
1471
1472void
1473input_ungrab(struct input *input, uint32_t time)
1474{
1475 struct widget *widget;
1476
1477 input->grab = NULL;
1478 if (input->pointer_focus) {
1479 widget = widget_find_widget(input->pointer_focus->widget,
1480 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001481 input_set_focus_widget(input, widget,
1482 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001483 }
1484}
1485
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001486static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001487input_handle_button(void *data,
1488 struct wl_input_device *input_device,
1489 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001490{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001491 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001492 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001493
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001494 if (input->focus_widget && input->grab == NULL && state)
1495 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001496
Neil Roberts6b28aad2012-01-23 19:11:18 +00001497 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001498 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001499 (*widget->button_handler)(widget,
1500 input, time,
1501 button, state,
1502 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001503
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001504 if (input->grab && input->grab_button == button && !state)
1505 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001506}
1507
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001508static void
Scott Moreau210d0792012-03-22 10:47:01 -06001509input_handle_axis(void *data,
1510 struct wl_input_device *input_device,
1511 uint32_t time, uint32_t axis, int32_t value)
1512{
1513}
1514
1515static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001516input_handle_key(void *data, struct wl_input_device *input_device,
1517 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001518{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001519 struct input *input = data;
1520 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001521 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001522 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001523
Daniel Stone0d5a5092012-02-16 12:48:00 +00001524 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001525 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001526 return;
1527
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001528 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001529 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001530 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1531 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001532
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001533 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1534
1535 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001536 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001537 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001538 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001539
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001540 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1541 if (state)
1542 window_set_maximized(window,
1543 window->type != TYPE_MAXIMIZED);
1544 } else if (window->key_handler) {
1545 (*window->key_handler)(window, input, time, key,
1546 sym, state, window->user_data);
1547 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001548}
1549
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001550static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001551input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001552{
1553 struct window *window = input->pointer_focus;
1554
1555 if (!window)
1556 return;
1557
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001558 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001559
Pekka Paalanene1207c72011-12-16 12:02:09 +02001560 input->pointer_focus = NULL;
1561 input->current_pointer_image = POINTER_UNSET;
1562}
1563
1564static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001565input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001566 struct wl_input_device *input_device,
1567 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001568 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001569{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001570 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001571 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001572 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001573
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001574 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001575 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001576
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001577 input->sx = sx;
1578 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001579
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001580 widget = widget_find_widget(window->widget, sx, sy);
1581 input_set_focus_widget(input, widget, time, sx, sy);
1582}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001583
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001584static void
1585input_handle_pointer_leave(void *data,
1586 struct wl_input_device *input_device,
1587 uint32_t time, struct wl_surface *surface)
1588{
1589 struct input *input = data;
1590
1591 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001592}
1593
1594static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001595input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001596{
1597 struct window *window = input->keyboard_focus;
1598
1599 if (!window)
1600 return;
1601
1602 window->keyboard_device = NULL;
1603 if (window->keyboard_focus_handler)
1604 (*window->keyboard_focus_handler)(window, NULL,
1605 window->user_data);
1606
1607 input->keyboard_focus = NULL;
1608}
1609
1610static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001611input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001612 struct wl_input_device *input_device,
1613 uint32_t time,
1614 struct wl_surface *surface,
1615 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001616{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001617 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001618 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001619 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001620 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001621
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001622 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001623
1624 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001625 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001626 for (k = keys->data; k < end; k++)
1627 input->modifiers |= d->xkb->map->modmap[*k];
1628
1629 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001630 window->keyboard_device = input;
1631 if (window->keyboard_focus_handler)
1632 (*window->keyboard_focus_handler)(window,
1633 window->keyboard_device,
1634 window->user_data);
1635}
1636
1637static void
1638input_handle_keyboard_leave(void *data,
1639 struct wl_input_device *input_device,
1640 uint32_t time,
1641 struct wl_surface *surface)
1642{
1643 struct input *input = data;
1644
1645 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001646}
1647
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001648static void
1649input_handle_touch_down(void *data,
1650 struct wl_input_device *wl_input_device,
1651 uint32_t time, struct wl_surface *surface,
1652 int32_t id, int32_t x, int32_t y)
1653{
1654}
1655
1656static void
1657input_handle_touch_up(void *data,
1658 struct wl_input_device *wl_input_device,
1659 uint32_t time, int32_t id)
1660{
1661}
1662
1663static void
1664input_handle_touch_motion(void *data,
1665 struct wl_input_device *wl_input_device,
1666 uint32_t time, int32_t id, int32_t x, int32_t y)
1667{
1668}
1669
1670static void
1671input_handle_touch_frame(void *data,
1672 struct wl_input_device *wl_input_device)
1673{
1674}
1675
1676static void
1677input_handle_touch_cancel(void *data,
1678 struct wl_input_device *wl_input_device)
1679{
1680}
1681
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001682static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001683 input_handle_motion,
1684 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001685 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001686 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001687 input_handle_pointer_enter,
1688 input_handle_pointer_leave,
1689 input_handle_keyboard_enter,
1690 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001691 input_handle_touch_down,
1692 input_handle_touch_up,
1693 input_handle_touch_motion,
1694 input_handle_touch_frame,
1695 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001696};
1697
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001698void
1699input_get_position(struct input *input, int32_t *x, int32_t *y)
1700{
1701 *x = input->sx;
1702 *y = input->sy;
1703}
1704
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001705struct wl_input_device *
1706input_get_input_device(struct input *input)
1707{
1708 return input->input_device;
1709}
1710
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001711uint32_t
1712input_get_modifiers(struct input *input)
1713{
1714 return input->modifiers;
1715}
1716
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001717struct widget *
1718input_get_focus_widget(struct input *input)
1719{
1720 return input->focus_widget;
1721}
1722
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001723struct data_offer {
1724 struct wl_data_offer *offer;
1725 struct input *input;
1726 struct wl_array types;
1727 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001728
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001729 struct task io_task;
1730 int fd;
1731 data_func_t func;
1732 int32_t x, y;
1733 void *user_data;
1734};
1735
1736static void
1737data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1738{
1739 struct data_offer *offer = data;
1740 char **p;
1741
1742 p = wl_array_add(&offer->types, sizeof *p);
1743 *p = strdup(type);
1744}
1745
1746static const struct wl_data_offer_listener data_offer_listener = {
1747 data_offer_offer,
1748};
1749
1750static void
1751data_offer_destroy(struct data_offer *offer)
1752{
1753 char **p;
1754
1755 offer->refcount--;
1756 if (offer->refcount == 0) {
1757 wl_data_offer_destroy(offer->offer);
1758 for (p = offer->types.data; *p; p++)
1759 free(*p);
1760 wl_array_release(&offer->types);
1761 free(offer);
1762 }
1763}
1764
1765static void
1766data_device_data_offer(void *data,
1767 struct wl_data_device *data_device, uint32_t id)
1768{
1769 struct data_offer *offer;
1770
1771 offer = malloc(sizeof *offer);
1772
1773 wl_array_init(&offer->types);
1774 offer->refcount = 1;
1775 offer->input = data;
1776
1777 /* FIXME: Generate typesafe wrappers for this */
1778 offer->offer = (struct wl_data_offer *)
1779 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1780 id, &wl_data_offer_interface);
1781
1782 wl_data_offer_add_listener(offer->offer,
1783 &data_offer_listener, offer);
1784}
1785
1786static void
1787data_device_enter(void *data, struct wl_data_device *data_device,
1788 uint32_t time, struct wl_surface *surface,
1789 int32_t x, int32_t y, struct wl_data_offer *offer)
1790{
1791 struct input *input = data;
1792 struct window *window;
1793 char **p;
1794
1795 input->drag_offer = wl_data_offer_get_user_data(offer);
1796 window = wl_surface_get_user_data(surface);
1797 input->pointer_focus = window;
1798
1799 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1800 *p = NULL;
1801
1802 window = input->pointer_focus;
1803 if (window->data_handler)
1804 window->data_handler(window, input, time, x, y,
1805 input->drag_offer->types.data,
1806 window->user_data);
1807}
1808
1809static void
1810data_device_leave(void *data, struct wl_data_device *data_device)
1811{
1812 struct input *input = data;
1813
1814 data_offer_destroy(input->drag_offer);
1815 input->drag_offer = NULL;
1816}
1817
1818static void
1819data_device_motion(void *data, struct wl_data_device *data_device,
1820 uint32_t time, int32_t x, int32_t y)
1821{
1822 struct input *input = data;
1823 struct window *window = input->pointer_focus;
1824
1825 input->sx = x;
1826 input->sy = y;
1827
1828 if (window->data_handler)
1829 window->data_handler(window, input, time, x, y,
1830 input->drag_offer->types.data,
1831 window->user_data);
1832}
1833
1834static void
1835data_device_drop(void *data, struct wl_data_device *data_device)
1836{
1837 struct input *input = data;
1838 struct window *window = input->pointer_focus;
1839
1840 if (window->drop_handler)
1841 window->drop_handler(window, input,
1842 input->sx, input->sy, window->user_data);
1843}
1844
1845static void
1846data_device_selection(void *data,
1847 struct wl_data_device *wl_data_device,
1848 struct wl_data_offer *offer)
1849{
1850 struct input *input = data;
1851 char **p;
1852
1853 if (input->selection_offer)
1854 data_offer_destroy(input->selection_offer);
1855
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001856 if (offer) {
1857 input->selection_offer = wl_data_offer_get_user_data(offer);
1858 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1859 *p = NULL;
1860 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001861}
1862
1863static const struct wl_data_device_listener data_device_listener = {
1864 data_device_data_offer,
1865 data_device_enter,
1866 data_device_leave,
1867 data_device_motion,
1868 data_device_drop,
1869 data_device_selection
1870};
1871
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001872void
1873input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1874{
1875 struct display *display = input->display;
1876 struct wl_buffer *buffer;
1877 cairo_surface_t *surface;
1878
1879 if (pointer == input->current_pointer_image)
1880 return;
1881
1882 input->current_pointer_image = pointer;
1883 surface = display->pointer_surfaces[pointer];
1884
1885 if (!surface)
1886 return;
1887
1888 buffer = display_get_buffer_for_surface(display, surface);
1889 wl_input_device_attach(input->input_device, time, buffer,
1890 pointer_images[pointer].hotspot_x,
1891 pointer_images[pointer].hotspot_y);
1892}
1893
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001894struct wl_data_device *
1895input_get_data_device(struct input *input)
1896{
1897 return input->data_device;
1898}
1899
1900void
1901input_set_selection(struct input *input,
1902 struct wl_data_source *source, uint32_t time)
1903{
1904 wl_data_device_set_selection(input->data_device, source, time);
1905}
1906
1907void
1908input_accept(struct input *input, uint32_t time, const char *type)
1909{
1910 wl_data_offer_accept(input->drag_offer->offer, time, type);
1911}
1912
1913static void
1914offer_io_func(struct task *task, uint32_t events)
1915{
1916 struct data_offer *offer =
1917 container_of(task, struct data_offer, io_task);
1918 unsigned int len;
1919 char buffer[4096];
1920
1921 len = read(offer->fd, buffer, sizeof buffer);
1922 offer->func(buffer, len,
1923 offer->x, offer->y, offer->user_data);
1924
1925 if (len == 0) {
1926 close(offer->fd);
1927 data_offer_destroy(offer);
1928 }
1929}
1930
1931static void
1932data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1933 data_func_t func, void *user_data)
1934{
1935 int p[2];
1936
1937 pipe2(p, O_CLOEXEC);
1938 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1939 close(p[1]);
1940
1941 offer->io_task.run = offer_io_func;
1942 offer->fd = p[0];
1943 offer->func = func;
1944 offer->refcount++;
1945 offer->user_data = user_data;
1946
1947 display_watch_fd(offer->input->display,
1948 offer->fd, EPOLLIN, &offer->io_task);
1949}
1950
1951void
1952input_receive_drag_data(struct input *input, const char *mime_type,
1953 data_func_t func, void *data)
1954{
1955 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1956 input->drag_offer->x = input->sx;
1957 input->drag_offer->y = input->sy;
1958}
1959
1960int
1961input_receive_selection_data(struct input *input, const char *mime_type,
1962 data_func_t func, void *data)
1963{
1964 char **p;
1965
1966 if (input->selection_offer == NULL)
1967 return -1;
1968
1969 for (p = input->selection_offer->types.data; *p; p++)
1970 if (strcmp(mime_type, *p) == 0)
1971 break;
1972
1973 if (*p == NULL)
1974 return -1;
1975
1976 data_offer_receive_data(input->selection_offer,
1977 mime_type, func, data);
1978 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001979}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001980
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001981int
1982input_receive_selection_data_to_fd(struct input *input,
1983 const char *mime_type, int fd)
1984{
1985 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1986
1987 return 0;
1988}
1989
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001990void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001991window_move(struct window *window, struct input *input, uint32_t time)
1992{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001993 if (!window->shell_surface)
1994 return;
1995
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001996 wl_shell_surface_move(window->shell_surface,
1997 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001998}
1999
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002000static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002001idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002002{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002003 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002004
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002005 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002006 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002007 widget_set_allocation(widget,
2008 window->pending_allocation.x,
2009 window->pending_allocation.y,
2010 window->pending_allocation.width,
2011 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002012
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002013 if (window->input_region) {
2014 wl_region_destroy(window->input_region);
2015 window->input_region = NULL;
2016 }
2017
2018 if (window->opaque_region) {
2019 wl_region_destroy(window->opaque_region);
2020 window->opaque_region = NULL;
2021 }
2022
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002023 if (widget->resize_handler)
2024 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002025 widget->allocation.width,
2026 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002027 widget->user_data);
2028
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002029 if (window->allocation.width != widget->allocation.width ||
2030 window->allocation.height != widget->allocation.height) {
2031 window->allocation = widget->allocation;
2032 window_schedule_redraw(window);
2033 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002034}
2035
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002036void
2037window_schedule_resize(struct window *window, int width, int height)
2038{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002039 window->pending_allocation.x = 0;
2040 window->pending_allocation.y = 0;
2041 window->pending_allocation.width = width;
2042 window->pending_allocation.height = height;
2043
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002044 window->resize_needed = 1;
2045 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002046}
2047
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002048void
2049widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2050{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002051 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002052}
2053
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002054static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002055handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002056 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002057 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002058{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002059 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002060
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002061 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002062 return;
2063
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002064 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002065 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002066}
2067
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002068static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002069menu_destroy(struct menu *menu)
2070{
2071 widget_destroy(menu->widget);
2072 window_destroy(menu->window);
2073 free(menu);
2074}
2075
2076static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002077handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2078{
2079 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002080 struct menu *menu = window->widget->user_data;
2081
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002082 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002083 * device. Or just use wl_callback. And this really needs to
2084 * be a window vfunc that the menu can set. And we need the
2085 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002086
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002087 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002088 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002089 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002090}
2091
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002092static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002093 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002094 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002095};
2096
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002097void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002098window_get_allocation(struct window *window,
2099 struct rectangle *allocation)
2100{
2101 *allocation = window->allocation;
2102}
2103
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002104static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002105widget_redraw(struct widget *widget)
2106{
2107 struct widget *child;
2108
2109 if (widget->redraw_handler)
2110 widget->redraw_handler(widget, widget->user_data);
2111 wl_list_for_each(child, &widget->child_list, link)
2112 widget_redraw(child);
2113}
2114
2115static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002116frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2117{
2118 struct window *window = data;
2119
2120 wl_callback_destroy(callback);
2121 window->redraw_scheduled = 0;
2122 if (window->redraw_needed)
2123 window_schedule_redraw(window);
2124}
2125
2126static const struct wl_callback_listener listener = {
2127 frame_callback
2128};
2129
2130static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002131idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002132{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002133 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002134 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002135
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002136 if (window->resize_needed)
2137 idle_resize(window);
2138
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002139 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002140 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002141 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002142 window->redraw_needed = 0;
2143
2144 callback = wl_surface_frame(window->surface);
2145 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002146}
2147
2148void
2149window_schedule_redraw(struct window *window)
2150{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002151 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002152 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002153 window->redraw_task.run = idle_redraw;
2154 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002155 window->redraw_scheduled = 1;
2156 }
2157}
2158
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002159void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002160window_set_custom(struct window *window)
2161{
2162 window->type = TYPE_CUSTOM;
2163}
2164
2165void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002166window_set_fullscreen(struct window *window, int fullscreen)
2167{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002168 if (!window->display->shell)
2169 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002170
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002171 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002172 return;
2173
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002174 if (fullscreen) {
2175 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002176 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002177 wl_shell_surface_set_fullscreen(window->shell_surface,
2178 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2179 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002180 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002181 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002182 wl_shell_surface_set_toplevel(window->shell_surface);
2183 window_schedule_resize(window,
2184 window->saved_allocation.width,
2185 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002186 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002187}
2188
2189void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002190window_set_maximized(struct window *window, int maximized)
2191{
2192 if (!window->display->shell)
2193 return;
2194
2195 if ((window->type == TYPE_MAXIMIZED) == maximized)
2196 return;
2197
2198 if (window->type == TYPE_TOPLEVEL) {
2199 window->saved_allocation = window->allocation;
2200 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2201 window->type = TYPE_MAXIMIZED;
2202 } else {
2203 wl_shell_surface_set_toplevel(window->shell_surface);
2204 window->type = TYPE_TOPLEVEL;
2205 window_schedule_resize(window,
2206 window->saved_allocation.width,
2207 window->saved_allocation.height);
2208 }
2209}
2210
2211void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002212window_set_user_data(struct window *window, void *data)
2213{
2214 window->user_data = data;
2215}
2216
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002217void *
2218window_get_user_data(struct window *window)
2219{
2220 return window->user_data;
2221}
2222
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002223void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002224window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002225 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002226{
2227 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002228}
2229
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002230void
2231window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002232 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002233{
2234 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002235}
2236
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002237void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002238window_set_data_handler(struct window *window, window_data_handler_t handler)
2239{
2240 window->data_handler = handler;
2241}
2242
2243void
2244window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2245{
2246 window->drop_handler = handler;
2247}
2248
2249void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002250window_set_close_handler(struct window *window,
2251 window_close_handler_t handler)
2252{
2253 window->close_handler = handler;
2254}
2255
2256void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002257window_set_title(struct window *window, const char *title)
2258{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002259 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002260 window->title = strdup(title);
2261}
2262
2263const char *
2264window_get_title(struct window *window)
2265{
2266 return window->title;
2267}
2268
2269void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002270window_damage(struct window *window, int32_t x, int32_t y,
2271 int32_t width, int32_t height)
2272{
2273 wl_surface_damage(window->surface, x, y, width, height);
2274}
2275
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002276static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002277window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002278{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002279 struct window *window;
2280
2281 window = malloc(sizeof *window);
2282 if (window == NULL)
2283 return NULL;
2284
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002285 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002286 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002287 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002288 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002289 if (display->shell) {
2290 window->shell_surface =
2291 wl_shell_get_shell_surface(display->shell,
2292 window->surface);
2293 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002294 window->allocation.x = 0;
2295 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002296 window->allocation.width = 0;
2297 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002298 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002299 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002300 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002301 window->input_region = NULL;
2302 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002303
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002304 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002305#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002306 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2307 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002308#else
2309 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2310#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002311 else
2312 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002313
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002314 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002315 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002316
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002317 if (window->shell_surface) {
2318 wl_shell_surface_set_user_data(window->shell_surface, window);
2319 wl_shell_surface_add_listener(window->shell_surface,
2320 &shell_surface_listener, window);
2321 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002322
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002323 return window;
2324}
2325
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002326struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002327window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002328{
2329 struct window *window;
2330
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002331 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002332 if (!window)
2333 return NULL;
2334
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002335 window->type = TYPE_TOPLEVEL;
2336 if (display->shell)
2337 wl_shell_surface_set_toplevel(window->shell_surface);
2338
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002339 return window;
2340}
2341
2342struct window *
2343window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002344 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002345{
2346 struct window *window;
2347
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002348 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002349 if (!window)
2350 return NULL;
2351
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002352 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002353 window->x = x;
2354 window->y = y;
2355
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002356 if (display->shell)
2357 wl_shell_surface_set_transient(window->shell_surface,
2358 window->parent->shell_surface,
2359 window->x, window->y, 0);
2360
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002361 return window;
2362}
2363
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002364static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002365menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002366{
2367 int next;
2368
2369 next = (sy - 8) / 20;
2370 if (menu->current != next) {
2371 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002372 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002373 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002374}
2375
2376static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002377menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002378 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002379 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002380{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002381 struct menu *menu = data;
2382
2383 if (widget == menu->widget)
2384 menu_set_item(data, y);
2385
2386 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002387}
2388
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002389static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002390menu_enter_handler(struct widget *widget,
2391 struct input *input, uint32_t time,
2392 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002393{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002394 struct menu *menu = data;
2395
2396 if (widget == menu->widget)
2397 menu_set_item(data, y);
2398
2399 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002400}
2401
2402static void
2403menu_leave_handler(struct widget *widget, struct input *input, void *data)
2404{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002405 struct menu *menu = data;
2406
2407 if (widget == menu->widget)
2408 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002409}
2410
2411static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002412menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002413 struct input *input, uint32_t time,
2414 int button, int state, void *data)
2415
2416{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002417 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002418
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002419 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002420 /* Either relase after press-drag-release or
2421 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002422 menu->func(menu->window->parent,
2423 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002424 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002425 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002426 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002427}
2428
2429static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002430menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002431{
2432 cairo_t *cr;
2433 const int32_t r = 3, margin = 3;
2434 struct menu *menu = data;
2435 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002436 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002437
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002438 cr = cairo_create(window->cairo_surface);
2439 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2440 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2441 cairo_paint(cr);
2442
2443 width = window->allocation.width;
2444 height = window->allocation.height;
2445 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002446
2447 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002448 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2449 cairo_fill(cr);
2450
2451 for (i = 0; i < menu->count; i++) {
2452 if (i == menu->current) {
2453 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2454 cairo_rectangle(cr, margin, i * 20 + margin,
2455 width - 2 * margin, 20);
2456 cairo_fill(cr);
2457 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2458 cairo_move_to(cr, 10, i * 20 + 16);
2459 cairo_show_text(cr, menu->entries[i]);
2460 } else {
2461 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2462 cairo_move_to(cr, 10, i * 20 + 16);
2463 cairo_show_text(cr, menu->entries[i]);
2464 }
2465 }
2466
2467 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002468}
2469
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002470void
2471window_show_menu(struct display *display,
2472 struct input *input, uint32_t time, struct window *parent,
2473 int32_t x, int32_t y,
2474 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002475{
2476 struct window *window;
2477 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002478 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002479
2480 menu = malloc(sizeof *menu);
2481 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002482 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002483
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002484 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002485 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002486 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002487
2488 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002489 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002490 menu->entries = entries;
2491 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002492 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002493 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002494 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002495 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002496 window->type = TYPE_MENU;
2497 window->x = x;
2498 window->y = y;
2499
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002500 wl_shell_surface_set_popup(window->shell_surface,
2501 input->input_device, time,
2502 window->parent->shell_surface,
2503 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002504
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002505 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002506 widget_set_enter_handler(menu->widget, menu_enter_handler);
2507 widget_set_leave_handler(menu->widget, menu_leave_handler);
2508 widget_set_motion_handler(menu->widget, menu_motion_handler);
2509 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002510
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002511 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002512 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002513}
2514
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002515void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002516window_set_buffer_type(struct window *window, enum window_buffer_type type)
2517{
2518 window->buffer_type = type;
2519}
2520
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002521
2522static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002523display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002524 struct wl_output *wl_output,
2525 int x, int y,
2526 int physical_width,
2527 int physical_height,
2528 int subpixel,
2529 const char *make,
2530 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002531{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002532 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002533
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002534 output->allocation.x = x;
2535 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002536}
2537
2538static void
2539display_handle_mode(void *data,
2540 struct wl_output *wl_output,
2541 uint32_t flags,
2542 int width,
2543 int height,
2544 int refresh)
2545{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002546 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002547 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002548
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002549 if (flags & WL_OUTPUT_MODE_CURRENT) {
2550 output->allocation.width = width;
2551 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002552 if (display->output_configure_handler)
2553 (*display->output_configure_handler)(
2554 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002555 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002556}
2557
2558static const struct wl_output_listener output_listener = {
2559 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002560 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002561};
2562
2563static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002564display_add_output(struct display *d, uint32_t id)
2565{
2566 struct output *output;
2567
2568 output = malloc(sizeof *output);
2569 if (output == NULL)
2570 return;
2571
2572 memset(output, 0, sizeof *output);
2573 output->display = d;
2574 output->output =
2575 wl_display_bind(d->display, id, &wl_output_interface);
2576 wl_list_insert(d->output_list.prev, &output->link);
2577
2578 wl_output_add_listener(output->output, &output_listener, output);
2579}
2580
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002581static void
2582output_destroy(struct output *output)
2583{
2584 if (output->destroy_handler)
2585 (*output->destroy_handler)(output, output->user_data);
2586
2587 wl_output_destroy(output->output);
2588 wl_list_remove(&output->link);
2589 free(output);
2590}
2591
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002592void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002593display_set_output_configure_handler(struct display *display,
2594 display_output_handler_t handler)
2595{
2596 struct output *output;
2597
2598 display->output_configure_handler = handler;
2599 if (!handler)
2600 return;
2601
2602 wl_list_for_each(output, &display->output_list, link)
2603 (*display->output_configure_handler)(output,
2604 display->user_data);
2605}
2606
2607void
2608output_set_user_data(struct output *output, void *data)
2609{
2610 output->user_data = data;
2611}
2612
2613void *
2614output_get_user_data(struct output *output)
2615{
2616 return output->user_data;
2617}
2618
2619void
2620output_set_destroy_handler(struct output *output,
2621 display_output_handler_t handler)
2622{
2623 output->destroy_handler = handler;
2624 /* FIXME: implement this, once we have way to remove outputs */
2625}
2626
2627void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002628output_get_allocation(struct output *output, struct rectangle *allocation)
2629{
2630 *allocation = output->allocation;
2631}
2632
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002633struct wl_output *
2634output_get_wl_output(struct output *output)
2635{
2636 return output->output;
2637}
2638
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002639static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002640display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002641{
2642 struct input *input;
2643
2644 input = malloc(sizeof *input);
2645 if (input == NULL)
2646 return;
2647
2648 memset(input, 0, sizeof *input);
2649 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002650 input->input_device =
2651 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002652 input->pointer_focus = NULL;
2653 input->keyboard_focus = NULL;
2654 wl_list_insert(d->input_list.prev, &input->link);
2655
2656 wl_input_device_add_listener(input->input_device,
2657 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002658 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002659
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002660 input->data_device =
2661 wl_data_device_manager_get_data_device(d->data_device_manager,
2662 input->input_device);
2663 wl_data_device_add_listener(input->data_device,
2664 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002665}
2666
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002667static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002668input_destroy(struct input *input)
2669{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002670 input_remove_keyboard_focus(input);
2671 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002672
2673 if (input->drag_offer)
2674 data_offer_destroy(input->drag_offer);
2675
2676 if (input->selection_offer)
2677 data_offer_destroy(input->selection_offer);
2678
2679 wl_data_device_destroy(input->data_device);
2680 wl_list_remove(&input->link);
2681 wl_input_device_destroy(input->input_device);
2682 free(input);
2683}
2684
2685static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002686display_handle_global(struct wl_display *display, uint32_t id,
2687 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002688{
2689 struct display *d = data;
2690
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002691 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002692 d->compositor =
2693 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002694 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002695 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002696 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002697 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002698 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002699 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002700 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002701 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002702 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2703 d->data_device_manager =
2704 wl_display_bind(display, id,
2705 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002706 }
2707}
2708
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002709static void
2710display_render_frame(struct display *d)
2711{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002712 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002713 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002714
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002715 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002716 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2717 cr = cairo_create(d->shadow);
2718 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2719 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002720 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002721 cairo_fill(cr);
2722 cairo_destroy(cr);
2723 blur_surface(d->shadow, 64);
2724
2725 d->active_frame =
2726 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2727 cr = cairo_create(d->active_frame);
2728 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002729
2730 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2731 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2732 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2733 cairo_set_source(cr, pattern);
2734 cairo_pattern_destroy(pattern);
2735
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002736 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002737 cairo_fill(cr);
2738 cairo_destroy(cr);
2739
2740 d->inactive_frame =
2741 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2742 cr = cairo_create(d->inactive_frame);
2743 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002744 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002745 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002746 cairo_fill(cr);
2747 cairo_destroy(cr);
2748}
2749
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002750static void
2751init_xkb(struct display *d)
2752{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002753 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002754
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002755 names.rules = "evdev";
2756 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002757 names.layout = option_xkb_layout;
2758 names.variant = option_xkb_variant;
2759 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002760
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002761 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002762 if (!d->xkb) {
2763 fprintf(stderr, "Failed to compile keymap\n");
2764 exit(1);
2765 }
2766}
2767
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002768static void
2769fini_xkb(struct display *display)
2770{
2771 xkb_free_keymap(display->xkb);
2772}
2773
Yuval Fledel45568f62010-12-06 09:18:12 -05002774static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002775init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002776{
2777 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002778 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002779
Rob Clark6396ed32012-03-11 19:48:41 -05002780#ifdef USE_CAIRO_GLESV2
2781# define GL_BIT EGL_OPENGL_ES2_BIT
2782#else
2783# define GL_BIT EGL_OPENGL_BIT
2784#endif
2785
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002786 static const EGLint argb_cfg_attribs[] = {
2787 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002788 EGL_RED_SIZE, 1,
2789 EGL_GREEN_SIZE, 1,
2790 EGL_BLUE_SIZE, 1,
2791 EGL_ALPHA_SIZE, 1,
2792 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002793 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002794 EGL_NONE
2795 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002796
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002797#ifdef USE_CAIRO_GLESV2
2798 static const EGLint context_attribs[] = {
2799 EGL_CONTEXT_CLIENT_VERSION, 2,
2800 EGL_NONE
2801 };
2802 EGLint api = EGL_OPENGL_ES_API;
2803#else
2804 EGLint *context_attribs = NULL;
2805 EGLint api = EGL_OPENGL_API;
2806#endif
2807
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002808 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002809 if (!eglInitialize(d->dpy, &major, &minor)) {
2810 fprintf(stderr, "failed to initialize display\n");
2811 return -1;
2812 }
2813
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002814 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002815 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2816 return -1;
2817 }
2818
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002819 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2820 &d->argb_config, 1, &n) || n != 1) {
2821 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002822 return -1;
2823 }
2824
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002825 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002826 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002827 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002828 fprintf(stderr, "failed to create context\n");
2829 return -1;
2830 }
2831
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002832 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002833 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002834 return -1;
2835 }
2836
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002837#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002838 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2839 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2840 fprintf(stderr, "failed to get cairo egl argb device\n");
2841 return -1;
2842 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002843#endif
2844
2845 return 0;
2846}
2847
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002848static void
2849fini_egl(struct display *display)
2850{
2851#ifdef HAVE_CAIRO_EGL
2852 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002853#endif
2854
2855 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2856 EGL_NO_CONTEXT);
2857
2858 eglTerminate(display->dpy);
2859 eglReleaseThread();
2860}
2861
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002862static int
2863event_mask_update(uint32_t mask, void *data)
2864{
2865 struct display *d = data;
2866
2867 d->mask = mask;
2868
2869 return 0;
2870}
2871
2872static void
2873handle_display_data(struct task *task, uint32_t events)
2874{
2875 struct display *display =
2876 container_of(task, struct display, display_task);
2877
2878 wl_display_iterate(display->display, display->mask);
2879}
2880
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002881struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002882display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002883{
2884 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002885
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002886 argc = parse_options(xkb_options,
2887 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002888
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002889 d = malloc(sizeof *d);
2890 if (d == NULL)
2891 return NULL;
2892
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002893 memset(d, 0, sizeof *d);
2894
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002895 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002896 if (d->display == NULL) {
2897 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002898 return NULL;
2899 }
2900
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002901 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2902 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2903 d->display_task.run = handle_display_data;
2904 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2905
2906 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002907 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002908 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002909
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002910 /* Set up listener so we'll catch all events. */
2911 wl_display_add_global_listener(d->display,
2912 display_handle_global, d);
2913
2914 /* Process connection events. */
2915 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002916 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002917 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002918
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002919 d->image_target_texture_2d =
2920 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2921 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2922 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2923
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002924 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002925
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002926 display_render_frame(d);
2927
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002928 wl_list_init(&d->window_list);
2929
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002930 init_xkb(d);
2931
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002932 return d;
2933}
2934
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002935static void
2936display_destroy_outputs(struct display *display)
2937{
2938 struct output *tmp;
2939 struct output *output;
2940
2941 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2942 output_destroy(output);
2943}
2944
Pekka Paalanene1207c72011-12-16 12:02:09 +02002945static void
2946display_destroy_inputs(struct display *display)
2947{
2948 struct input *tmp;
2949 struct input *input;
2950
2951 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2952 input_destroy(input);
2953}
2954
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002955void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002956display_destroy(struct display *display)
2957{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002958 if (!wl_list_empty(&display->window_list))
2959 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2960
2961 if (!wl_list_empty(&display->deferred_list))
2962 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2963
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002964 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002965 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002966
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002967 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002968
2969 cairo_surface_destroy(display->active_frame);
2970 cairo_surface_destroy(display->inactive_frame);
2971 cairo_surface_destroy(display->shadow);
2972 destroy_pointer_surfaces(display);
2973
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002974 fini_egl(display);
2975
Pekka Paalanenc2052982011-12-16 11:41:32 +02002976 if (display->shell)
2977 wl_shell_destroy(display->shell);
2978
2979 if (display->shm)
2980 wl_shm_destroy(display->shm);
2981
2982 if (display->data_device_manager)
2983 wl_data_device_manager_destroy(display->data_device_manager);
2984
2985 wl_compositor_destroy(display->compositor);
2986
2987 close(display->epoll_fd);
2988
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002989 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05002990 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002991 free(display);
2992}
2993
2994void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002995display_set_user_data(struct display *display, void *data)
2996{
2997 display->user_data = data;
2998}
2999
3000void *
3001display_get_user_data(struct display *display)
3002{
3003 return display->user_data;
3004}
3005
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003006struct wl_display *
3007display_get_display(struct display *display)
3008{
3009 return display->display;
3010}
3011
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003012struct output *
3013display_get_output(struct display *display)
3014{
3015 return container_of(display->output_list.next, struct output, link);
3016}
3017
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003018struct wl_compositor *
3019display_get_compositor(struct display *display)
3020{
3021 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003022}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003023
3024EGLDisplay
3025display_get_egl_display(struct display *d)
3026{
3027 return d->dpy;
3028}
3029
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003030struct wl_data_source *
3031display_create_data_source(struct display *display)
3032{
3033 return wl_data_device_manager_create_data_source(display->data_device_manager);
3034}
3035
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003036EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003037display_get_argb_egl_config(struct display *d)
3038{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003039 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003040}
3041
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003042struct wl_shell *
3043display_get_shell(struct display *display)
3044{
3045 return display->shell;
3046}
3047
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003048int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003049display_acquire_window_surface(struct display *display,
3050 struct window *window,
3051 EGLContext ctx)
3052{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003053#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003054 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003055 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003056
3057 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003058 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003059 device = cairo_surface_get_device(window->cairo_surface);
3060 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003061 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003062
Benjamin Franzke0c991632011-09-27 21:57:31 +02003063 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003064 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003065 ctx = display->argb_ctx;
3066 else
3067 assert(0);
3068 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003069
3070 data = cairo_surface_get_user_data(window->cairo_surface,
3071 &surface_data_key);
3072
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003073 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003074 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003075 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3076 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003077
3078 return 0;
3079#else
3080 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003081#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003082}
3083
3084void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003085display_release_window_surface(struct display *display,
3086 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003087{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003088#ifdef HAVE_CAIRO_EGL
3089 cairo_device_t *device;
3090
3091 device = cairo_surface_get_device(window->cairo_surface);
3092 if (!device)
3093 return;
3094
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003095 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003096 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003097 cairo_device_release(device);
3098#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003099}
3100
3101void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003102display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003103{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003104 wl_list_insert(&display->deferred_list, &task->link);
3105}
3106
3107void
3108display_watch_fd(struct display *display,
3109 int fd, uint32_t events, struct task *task)
3110{
3111 struct epoll_event ep;
3112
3113 ep.events = events;
3114 ep.data.ptr = task;
3115 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3116}
3117
3118void
3119display_run(struct display *display)
3120{
3121 struct task *task;
3122 struct epoll_event ep[16];
3123 int i, count;
3124
Pekka Paalanen826d7952011-12-15 10:14:07 +02003125 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003126 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003127 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003128
3129 while (!wl_list_empty(&display->deferred_list)) {
3130 task = container_of(display->deferred_list.next,
3131 struct task, link);
3132 wl_list_remove(&task->link);
3133 task->run(task, 0);
3134 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003135
3136 if (!display->running)
3137 break;
3138
3139 wl_display_flush(display->display);
3140
3141 count = epoll_wait(display->epoll_fd,
3142 ep, ARRAY_LENGTH(ep), -1);
3143 for (i = 0; i < count; i++) {
3144 task = ep[i].data.ptr;
3145 task->run(task, ep[i].events);
3146 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003147 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003148}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003149
3150void
3151display_exit(struct display *display)
3152{
3153 display->running = 0;
3154}