blob: 49b0188f08286792fbecacfa374399c4ea0be485 [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øgsberg3a696272011-09-14 17:33:48 -0400126 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500127 int resize_scheduled;
128 struct task resize_task;
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øgsberg7d804062010-09-07 21:50:06 -0400169 uint32_t 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{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100785 if (window->cairo_surface) {
786 switch (window->buffer_type) {
787 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
788 case WINDOW_BUFFER_TYPE_SHM:
789 display_surface_damage(window->display,
790 window->cairo_surface,
791 0, 0,
792 window->allocation.width,
793 window->allocation.height);
794 break;
795 default:
796 break;
797 }
798 window_attach_surface(window);
799 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500800}
801
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400802void
803window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500805 cairo_surface_reference(surface);
806
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400807 if (window->cairo_surface != NULL)
808 cairo_surface_destroy(window->cairo_surface);
809
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500810 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400811}
812
Benjamin Franzke22d54812011-07-16 19:50:32 +0000813#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814static void
815window_resize_cairo_window_surface(struct window *window)
816{
817 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200818 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819
820 data = cairo_surface_get_user_data(window->cairo_surface,
821 &surface_data_key);
822
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200823 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 wl_egl_window_resize(data->window,
825 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200826 window->allocation.height,
827 x,y);
828
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 cairo_gl_surface_set_size(window->cairo_surface,
830 window->allocation.width,
831 window->allocation.height);
832}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000833#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400835struct display *
836window_get_display(struct window *window)
837{
838 return window->display;
839}
840
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400841void
842window_create_surface(struct window *window)
843{
844 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400845 uint32_t flags = 0;
846
847 if (!window->transparent)
848 flags = SURFACE_OPAQUE;
849
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500851#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100852 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
853 if (window->cairo_surface) {
854 window_resize_cairo_window_surface(window);
855 return;
856 }
857 surface = display_create_surface(window->display,
858 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400859 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100860 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500861 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400862 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400864 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400865 break;
866#endif
867 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400868 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400869 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400870 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800871 default:
872 surface = NULL;
873 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400874 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400875
876 window_set_surface(window, surface);
877 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400878}
879
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200880static void frame_destroy(struct frame *frame);
881
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400882void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500883window_destroy(struct window *window)
884{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200885 struct display *display = window->display;
886 struct input *input;
887
888 if (window->redraw_scheduled)
889 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500890 if (window->resize_scheduled)
891 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200892
893 wl_list_for_each(input, &display->input_list, link) {
894 if (input->pointer_focus == window)
895 input->pointer_focus = NULL;
896 if (input->keyboard_focus == window)
897 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500898 if (input->focus_widget &&
899 input->focus_widget->window == window)
900 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200901 }
902
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500903 if (window->input_region)
904 wl_region_destroy(window->input_region);
905 if (window->opaque_region)
906 wl_region_destroy(window->opaque_region);
907
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200908 if (window->frame)
909 frame_destroy(window->frame);
910
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200911 if (window->shell_surface)
912 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500913 wl_surface_destroy(window->surface);
914 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200915
916 if (window->cairo_surface != NULL)
917 cairo_surface_destroy(window->cairo_surface);
918 if (window->pending_surface != NULL)
919 cairo_surface_destroy(window->pending_surface);
920
921 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500922 free(window);
923}
924
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500925static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500926widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400927{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500928 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400929
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500930 wl_list_for_each(child, &widget->child_list, link) {
931 target = widget_find_widget(child, x, y);
932 if (target)
933 return target;
934 }
935
936 if (widget->allocation.x <= x &&
937 x < widget->allocation.x + widget->allocation.width &&
938 widget->allocation.y <= y &&
939 y < widget->allocation.y + widget->allocation.height) {
940 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400941 }
942
943 return NULL;
944}
945
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500946static struct widget *
947widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400948{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500949 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400950
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500951 widget = malloc(sizeof *widget);
952 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500953 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500954 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500955 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500956 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500957 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500958
959 return widget;
960}
961
962struct widget *
963window_add_widget(struct window *window, void *data)
964{
965 window->widget = widget_create(window, data);
966 wl_list_init(&window->widget->link);
967
968 return window->widget;
969}
970
971struct widget *
972widget_add_widget(struct widget *parent, void *data)
973{
974 struct widget *widget;
975
976 widget = widget_create(parent->window, data);
977 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400978
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500979 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400980}
981
982void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500983widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400984{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200985 struct display *display = widget->window->display;
986 struct input *input;
987
988 wl_list_for_each(input, &display->input_list, link) {
989 if (input->focus_widget == widget)
990 input->focus_widget = NULL;
991 }
992
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500993 wl_list_remove(&widget->link);
994 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995}
996
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400997void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500998widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001000 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001001}
1002
1003void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001004widget_set_size(struct widget *widget, int32_t width, int32_t height)
1005{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001006 widget->allocation.width = width;
1007 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001008}
1009
1010void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001011widget_set_allocation(struct widget *widget,
1012 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001013{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001014 widget->allocation.x = x;
1015 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001016 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001017}
1018
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001019void
1020widget_set_transparent(struct widget *widget, int transparent)
1021{
1022 widget->opaque = !transparent;
1023}
1024
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001025void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001026widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001027{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001028 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001029}
1030
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001031void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001032widget_set_resize_handler(struct widget *widget,
1033 widget_resize_handler_t handler)
1034{
1035 widget->resize_handler = handler;
1036}
1037
1038void
1039widget_set_redraw_handler(struct widget *widget,
1040 widget_redraw_handler_t handler)
1041{
1042 widget->redraw_handler = handler;
1043}
1044
1045void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001046widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001047{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001048 widget->enter_handler = handler;
1049}
1050
1051void
1052widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1053{
1054 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001055}
1056
1057void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001058widget_set_motion_handler(struct widget *widget,
1059 widget_motion_handler_t handler)
1060{
1061 widget->motion_handler = handler;
1062}
1063
1064void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001065widget_set_button_handler(struct widget *widget,
1066 widget_button_handler_t handler)
1067{
1068 widget->button_handler = handler;
1069}
1070
1071void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001072widget_schedule_redraw(struct widget *widget)
1073{
1074 window_schedule_redraw(widget->window);
1075}
1076
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001077cairo_surface_t *
1078window_get_surface(struct window *window)
1079{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001080 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001081}
1082
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001083struct wl_surface *
1084window_get_wl_surface(struct window *window)
1085{
1086 return window->surface;
1087}
1088
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001089struct wl_shell_surface *
1090window_get_wl_shell_surface(struct window *window)
1091{
1092 return window->shell_surface;
1093}
1094
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001095static void
1096frame_resize_handler(struct widget *widget,
1097 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001098{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001099 struct frame *frame = data;
1100 struct widget *child = frame->child;
1101 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001102 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001103 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001104 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001105
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001106 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001107 decoration_width = (frame->width + frame->margin) * 2;
1108 decoration_height = frame->width +
1109 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001110
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001111 allocation.x = frame->width + frame->margin;
1112 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001113 allocation.width = width - decoration_width;
1114 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001115
1116 widget->window->input_region =
1117 wl_compositor_create_region(display->compositor);
1118 wl_region_add(widget->window->input_region,
1119 frame->margin, frame->margin,
1120 width - 2 * frame->margin,
1121 height - 2 * frame->margin);
1122
1123 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001124 } else {
1125 decoration_width = 0;
1126 decoration_height = 0;
1127
1128 allocation.x = 0;
1129 allocation.y = 0;
1130 allocation.width = width;
1131 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001132 opaque_margin = 0;
1133 }
1134
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001135 widget_set_allocation(child, allocation.x, allocation.y,
1136 allocation.width, allocation.height);
1137
1138 if (child->resize_handler)
1139 child->resize_handler(child,
1140 allocation.width,
1141 allocation.height,
1142 child->user_data);
1143
1144 widget_set_allocation(widget, 0, 0,
1145 child->allocation.width + decoration_width,
1146 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001147
1148 if (child->opaque) {
1149 widget->window->opaque_region =
1150 wl_compositor_create_region(display->compositor);
1151 wl_region_add(widget->window->opaque_region,
1152 opaque_margin, opaque_margin,
1153 widget->allocation.width - 2 * opaque_margin,
1154 widget->allocation.height - 2 * opaque_margin);
1155 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001156}
1157
1158static void
1159frame_redraw_handler(struct widget *widget, void *data)
1160{
1161 struct frame *frame = data;
1162 cairo_t *cr;
1163 cairo_text_extents_t extents;
1164 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001165 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001166 struct window *window = widget->window;
1167
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001168 if (window->type == TYPE_FULLSCREEN)
1169 return;
1170
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001171 width = widget->allocation.width;
1172 height = widget->allocation.height;
1173
1174 cr = cairo_create(window->cairo_surface);
1175
1176 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1177 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1178 cairo_paint(cr);
1179
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001180 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001181 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001182 2, 2, width + 8, height + 8,
1183 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001184
1185 if (window->keyboard_device)
1186 source = window->display->active_frame;
1187 else
1188 source = window->display->inactive_frame;
1189
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001190 tile_source(cr, source,
1191 frame->margin, frame->margin,
1192 width - frame->margin * 2, height - frame->margin * 2,
1193 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001194
1195 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001196 cairo_select_font_face(cr, "sans",
1197 CAIRO_FONT_SLANT_NORMAL,
1198 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001199 cairo_set_font_size(cr, 14);
1200 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001201 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001202 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001203 if (window->keyboard_device) {
1204 cairo_move_to(cr, x + 1, y + 1);
1205 cairo_set_source_rgb(cr, 1, 1, 1);
1206 cairo_show_text(cr, window->title);
1207 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001208 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001209 cairo_show_text(cr, window->title);
1210 } else {
1211 cairo_move_to(cr, x, y);
1212 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1213 cairo_show_text(cr, window->title);
1214 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001215
1216 cairo_destroy(cr);
1217}
1218
1219static int
1220frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1221{
1222 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001223 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001224 const int grip_size = 8;
1225
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001226 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001227 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001228 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001229 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001230 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001231 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001232 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001233 hlocation = WINDOW_RESIZING_RIGHT;
1234 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001235 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001236
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001237 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001238 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001240 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001241 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001242 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001243 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001244 vlocation = WINDOW_RESIZING_BOTTOM;
1245 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001246 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001247
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001248 location = vlocation | hlocation;
1249 if (location & WINDOW_EXTERIOR)
1250 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001252 location = WINDOW_TITLEBAR;
1253 else if (location == WINDOW_INTERIOR)
1254 location = WINDOW_CLIENT_AREA;
1255
1256 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257}
1258
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001259static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001260frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001261{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001262 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001264 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001265 switch (location) {
1266 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001267 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001268 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001269 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001271 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001272 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001273 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001275 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001276 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001277 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001278 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001279 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001280 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001281 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001282 case WINDOW_EXTERIOR:
1283 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001285 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001287}
1288
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001289static void
1290frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001291{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001292 switch (index) {
1293 case 0: /* close */
1294 if (window->close_handler)
1295 window->close_handler(window->parent,
1296 window->user_data);
1297 else
1298 display_exit(window->display);
1299 break;
1300 case 1: /* fullscreen */
1301 /* we don't have a way to get out of fullscreen for now */
1302 window_set_fullscreen(window, 1);
1303 break;
1304 case 2: /* rotate */
1305 case 3: /* scale */
1306 break;
1307 }
1308}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001309
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001310void
1311window_show_frame_menu(struct window *window,
1312 struct input *input, uint32_t time)
1313{
1314 int32_t x, y;
1315
1316 static const char *entries[] = {
1317 "Close", "Fullscreen", "Rotate", "Scale"
1318 };
1319
1320 input_get_position(input, &x, &y);
1321 window_show_menu(window->display, input, time, window,
1322 x - 10, y - 10, frame_menu_func, entries, 4);
1323}
1324
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001325static int
1326frame_enter_handler(struct widget *widget,
1327 struct input *input, uint32_t time,
1328 int32_t x, int32_t y, void *data)
1329{
1330 return frame_get_pointer_image_for_location(data, input);
1331}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001332
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001333static int
1334frame_motion_handler(struct widget *widget,
1335 struct input *input, uint32_t time,
1336 int32_t x, int32_t y, void *data)
1337{
1338 return frame_get_pointer_image_for_location(data, input);
1339}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001340
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001341static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001342frame_button_handler(struct widget *widget,
1343 struct input *input, uint32_t time,
1344 int button, int state, void *data)
1345
1346{
1347 struct frame *frame = data;
1348 struct window *window = widget->window;
1349 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001350 location = frame_get_pointer_location(frame, input->sx, input->sy);
1351
1352 if (window->display->shell && button == BTN_LEFT && state == 1) {
1353 switch (location) {
1354 case WINDOW_TITLEBAR:
1355 if (!window->shell_surface)
1356 break;
1357 input_set_pointer_image(input, time, POINTER_DRAGGING);
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_move(window->shell_surface,
1360 input_get_input_device(input),
1361 time);
1362 break;
1363 case WINDOW_RESIZING_TOP:
1364 case WINDOW_RESIZING_BOTTOM:
1365 case WINDOW_RESIZING_LEFT:
1366 case WINDOW_RESIZING_RIGHT:
1367 case WINDOW_RESIZING_TOP_LEFT:
1368 case WINDOW_RESIZING_TOP_RIGHT:
1369 case WINDOW_RESIZING_BOTTOM_LEFT:
1370 case WINDOW_RESIZING_BOTTOM_RIGHT:
1371 if (!window->shell_surface)
1372 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001373 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001374 wl_shell_surface_resize(window->shell_surface,
1375 input_get_input_device(input),
1376 time, location);
1377 break;
1378 }
1379 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001380 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001381 }
1382}
1383
1384struct widget *
1385frame_create(struct window *window, void *data)
1386{
1387 struct frame *frame;
1388
1389 frame = malloc(sizeof *frame);
1390 memset(frame, 0, sizeof *frame);
1391
1392 frame->widget = window_add_widget(window, frame);
1393 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001394 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001395 frame->width = 4;
1396 frame->titlebar_height = 30
1397;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001398 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1399 widget_set_resize_handler(frame->widget, frame_resize_handler);
1400 widget_set_enter_handler(frame->widget, frame_enter_handler);
1401 widget_set_motion_handler(frame->widget, frame_motion_handler);
1402 widget_set_button_handler(frame->widget, frame_button_handler);
1403
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001404 window->frame = frame;
1405
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001406 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001407}
1408
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001409static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001410frame_destroy(struct frame *frame)
1411{
1412 /* frame->child must be destroyed by the application */
1413 widget_destroy(frame->widget);
1414 free(frame);
1415}
1416
1417static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001418input_set_focus_widget(struct input *input, struct widget *focus,
1419 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001420{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001421 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001422 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001423
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001424 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001425 return;
1426
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001427 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001428 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001429 widget = old;
1430 if (input->grab)
1431 widget = input->grab;
1432 if (widget->leave_handler)
1433 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001434 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001435 }
1436
1437 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001438 widget = focus;
1439 if (input->grab)
1440 widget = input->grab;
1441 if (widget->enter_handler)
1442 pointer = widget->enter_handler(focus, input, time,
1443 x, y,
1444 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001445 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001446
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001447 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001448 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001449}
1450
1451static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001452input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001453 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001454{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001455 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001456 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001457 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001458 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001459
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001460 input->sx = sx;
1461 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001462
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001463 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001464 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001465 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001466 }
1467
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001468 if (input->grab)
1469 widget = input->grab;
1470 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001471 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001472 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001473 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001474 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001475 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001476
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001477 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001478}
1479
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001480void
1481input_grab(struct input *input, struct widget *widget, uint32_t button)
1482{
1483 input->grab = widget;
1484 input->grab_button = button;
1485}
1486
1487void
1488input_ungrab(struct input *input, uint32_t time)
1489{
1490 struct widget *widget;
1491
1492 input->grab = NULL;
1493 if (input->pointer_focus) {
1494 widget = widget_find_widget(input->pointer_focus->widget,
1495 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001496 input_set_focus_widget(input, widget,
1497 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001498 }
1499}
1500
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001501static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001502input_handle_button(void *data,
1503 struct wl_input_device *input_device,
1504 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001505{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001506 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001507 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001508
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001509 if (input->focus_widget && input->grab == NULL && state)
1510 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001511
Neil Roberts6b28aad2012-01-23 19:11:18 +00001512 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001513 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001514 (*widget->button_handler)(widget,
1515 input, time,
1516 button, state,
1517 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001518
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001519 if (input->grab && input->grab_button == button && !state)
1520 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001521}
1522
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001523static void
Scott Moreau210d0792012-03-22 10:47:01 -06001524input_handle_axis(void *data,
1525 struct wl_input_device *input_device,
1526 uint32_t time, uint32_t axis, int32_t value)
1527{
1528}
1529
1530static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001531input_handle_key(void *data, struct wl_input_device *input_device,
1532 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001533{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001534 struct input *input = data;
1535 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001536 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001537 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001538
Daniel Stone0d5a5092012-02-16 12:48:00 +00001539 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001540 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001541 return;
1542
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001543 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001544 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001545 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1546 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001547
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001548 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1549
1550 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001551 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001552 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001553 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001554
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001555 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1556 if (state)
1557 window_set_maximized(window,
1558 window->type != TYPE_MAXIMIZED);
1559 } else if (window->key_handler) {
1560 (*window->key_handler)(window, input, time, key,
1561 sym, state, window->user_data);
1562 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001563}
1564
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001565static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001566input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001567{
1568 struct window *window = input->pointer_focus;
1569
1570 if (!window)
1571 return;
1572
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001573 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001574
Pekka Paalanene1207c72011-12-16 12:02:09 +02001575 input->pointer_focus = NULL;
1576 input->current_pointer_image = POINTER_UNSET;
1577}
1578
1579static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001580input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001581 struct wl_input_device *input_device,
1582 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001583 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001584{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001585 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001586 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001587 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001588
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001589 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001590 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001591
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001592 input->sx = sx;
1593 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001594
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001595 widget = widget_find_widget(window->widget, sx, sy);
1596 input_set_focus_widget(input, widget, time, sx, sy);
1597}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001598
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001599static void
1600input_handle_pointer_leave(void *data,
1601 struct wl_input_device *input_device,
1602 uint32_t time, struct wl_surface *surface)
1603{
1604 struct input *input = data;
1605
1606 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001607}
1608
1609static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001610input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001611{
1612 struct window *window = input->keyboard_focus;
1613
1614 if (!window)
1615 return;
1616
1617 window->keyboard_device = NULL;
1618 if (window->keyboard_focus_handler)
1619 (*window->keyboard_focus_handler)(window, NULL,
1620 window->user_data);
1621
1622 input->keyboard_focus = NULL;
1623}
1624
1625static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001626input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001627 struct wl_input_device *input_device,
1628 uint32_t time,
1629 struct wl_surface *surface,
1630 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001631{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001632 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001633 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001634 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001635 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001636
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001637 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001638
1639 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001640 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001641 for (k = keys->data; k < end; k++)
1642 input->modifiers |= d->xkb->map->modmap[*k];
1643
1644 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001645 window->keyboard_device = input;
1646 if (window->keyboard_focus_handler)
1647 (*window->keyboard_focus_handler)(window,
1648 window->keyboard_device,
1649 window->user_data);
1650}
1651
1652static void
1653input_handle_keyboard_leave(void *data,
1654 struct wl_input_device *input_device,
1655 uint32_t time,
1656 struct wl_surface *surface)
1657{
1658 struct input *input = data;
1659
1660 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001661}
1662
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001663static void
1664input_handle_touch_down(void *data,
1665 struct wl_input_device *wl_input_device,
1666 uint32_t time, struct wl_surface *surface,
1667 int32_t id, int32_t x, int32_t y)
1668{
1669}
1670
1671static void
1672input_handle_touch_up(void *data,
1673 struct wl_input_device *wl_input_device,
1674 uint32_t time, int32_t id)
1675{
1676}
1677
1678static void
1679input_handle_touch_motion(void *data,
1680 struct wl_input_device *wl_input_device,
1681 uint32_t time, int32_t id, int32_t x, int32_t y)
1682{
1683}
1684
1685static void
1686input_handle_touch_frame(void *data,
1687 struct wl_input_device *wl_input_device)
1688{
1689}
1690
1691static void
1692input_handle_touch_cancel(void *data,
1693 struct wl_input_device *wl_input_device)
1694{
1695}
1696
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001697static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001698 input_handle_motion,
1699 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001700 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001701 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001702 input_handle_pointer_enter,
1703 input_handle_pointer_leave,
1704 input_handle_keyboard_enter,
1705 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001706 input_handle_touch_down,
1707 input_handle_touch_up,
1708 input_handle_touch_motion,
1709 input_handle_touch_frame,
1710 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001711};
1712
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001713void
1714input_get_position(struct input *input, int32_t *x, int32_t *y)
1715{
1716 *x = input->sx;
1717 *y = input->sy;
1718}
1719
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001720struct wl_input_device *
1721input_get_input_device(struct input *input)
1722{
1723 return input->input_device;
1724}
1725
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001726uint32_t
1727input_get_modifiers(struct input *input)
1728{
1729 return input->modifiers;
1730}
1731
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001732struct widget *
1733input_get_focus_widget(struct input *input)
1734{
1735 return input->focus_widget;
1736}
1737
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001738struct data_offer {
1739 struct wl_data_offer *offer;
1740 struct input *input;
1741 struct wl_array types;
1742 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001743
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001744 struct task io_task;
1745 int fd;
1746 data_func_t func;
1747 int32_t x, y;
1748 void *user_data;
1749};
1750
1751static void
1752data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1753{
1754 struct data_offer *offer = data;
1755 char **p;
1756
1757 p = wl_array_add(&offer->types, sizeof *p);
1758 *p = strdup(type);
1759}
1760
1761static const struct wl_data_offer_listener data_offer_listener = {
1762 data_offer_offer,
1763};
1764
1765static void
1766data_offer_destroy(struct data_offer *offer)
1767{
1768 char **p;
1769
1770 offer->refcount--;
1771 if (offer->refcount == 0) {
1772 wl_data_offer_destroy(offer->offer);
1773 for (p = offer->types.data; *p; p++)
1774 free(*p);
1775 wl_array_release(&offer->types);
1776 free(offer);
1777 }
1778}
1779
1780static void
1781data_device_data_offer(void *data,
1782 struct wl_data_device *data_device, uint32_t id)
1783{
1784 struct data_offer *offer;
1785
1786 offer = malloc(sizeof *offer);
1787
1788 wl_array_init(&offer->types);
1789 offer->refcount = 1;
1790 offer->input = data;
1791
1792 /* FIXME: Generate typesafe wrappers for this */
1793 offer->offer = (struct wl_data_offer *)
1794 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1795 id, &wl_data_offer_interface);
1796
1797 wl_data_offer_add_listener(offer->offer,
1798 &data_offer_listener, offer);
1799}
1800
1801static void
1802data_device_enter(void *data, struct wl_data_device *data_device,
1803 uint32_t time, struct wl_surface *surface,
1804 int32_t x, int32_t y, struct wl_data_offer *offer)
1805{
1806 struct input *input = data;
1807 struct window *window;
1808 char **p;
1809
1810 input->drag_offer = wl_data_offer_get_user_data(offer);
1811 window = wl_surface_get_user_data(surface);
1812 input->pointer_focus = window;
1813
1814 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1815 *p = NULL;
1816
1817 window = input->pointer_focus;
1818 if (window->data_handler)
1819 window->data_handler(window, input, time, x, y,
1820 input->drag_offer->types.data,
1821 window->user_data);
1822}
1823
1824static void
1825data_device_leave(void *data, struct wl_data_device *data_device)
1826{
1827 struct input *input = data;
1828
1829 data_offer_destroy(input->drag_offer);
1830 input->drag_offer = NULL;
1831}
1832
1833static void
1834data_device_motion(void *data, struct wl_data_device *data_device,
1835 uint32_t time, int32_t x, int32_t y)
1836{
1837 struct input *input = data;
1838 struct window *window = input->pointer_focus;
1839
1840 input->sx = x;
1841 input->sy = y;
1842
1843 if (window->data_handler)
1844 window->data_handler(window, input, time, x, y,
1845 input->drag_offer->types.data,
1846 window->user_data);
1847}
1848
1849static void
1850data_device_drop(void *data, struct wl_data_device *data_device)
1851{
1852 struct input *input = data;
1853 struct window *window = input->pointer_focus;
1854
1855 if (window->drop_handler)
1856 window->drop_handler(window, input,
1857 input->sx, input->sy, window->user_data);
1858}
1859
1860static void
1861data_device_selection(void *data,
1862 struct wl_data_device *wl_data_device,
1863 struct wl_data_offer *offer)
1864{
1865 struct input *input = data;
1866 char **p;
1867
1868 if (input->selection_offer)
1869 data_offer_destroy(input->selection_offer);
1870
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001871 if (offer) {
1872 input->selection_offer = wl_data_offer_get_user_data(offer);
1873 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1874 *p = NULL;
1875 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001876}
1877
1878static const struct wl_data_device_listener data_device_listener = {
1879 data_device_data_offer,
1880 data_device_enter,
1881 data_device_leave,
1882 data_device_motion,
1883 data_device_drop,
1884 data_device_selection
1885};
1886
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001887void
1888input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1889{
1890 struct display *display = input->display;
1891 struct wl_buffer *buffer;
1892 cairo_surface_t *surface;
1893
1894 if (pointer == input->current_pointer_image)
1895 return;
1896
1897 input->current_pointer_image = pointer;
1898 surface = display->pointer_surfaces[pointer];
1899
1900 if (!surface)
1901 return;
1902
1903 buffer = display_get_buffer_for_surface(display, surface);
1904 wl_input_device_attach(input->input_device, time, buffer,
1905 pointer_images[pointer].hotspot_x,
1906 pointer_images[pointer].hotspot_y);
1907}
1908
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001909struct wl_data_device *
1910input_get_data_device(struct input *input)
1911{
1912 return input->data_device;
1913}
1914
1915void
1916input_set_selection(struct input *input,
1917 struct wl_data_source *source, uint32_t time)
1918{
1919 wl_data_device_set_selection(input->data_device, source, time);
1920}
1921
1922void
1923input_accept(struct input *input, uint32_t time, const char *type)
1924{
1925 wl_data_offer_accept(input->drag_offer->offer, time, type);
1926}
1927
1928static void
1929offer_io_func(struct task *task, uint32_t events)
1930{
1931 struct data_offer *offer =
1932 container_of(task, struct data_offer, io_task);
1933 unsigned int len;
1934 char buffer[4096];
1935
1936 len = read(offer->fd, buffer, sizeof buffer);
1937 offer->func(buffer, len,
1938 offer->x, offer->y, offer->user_data);
1939
1940 if (len == 0) {
1941 close(offer->fd);
1942 data_offer_destroy(offer);
1943 }
1944}
1945
1946static void
1947data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1948 data_func_t func, void *user_data)
1949{
1950 int p[2];
1951
1952 pipe2(p, O_CLOEXEC);
1953 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1954 close(p[1]);
1955
1956 offer->io_task.run = offer_io_func;
1957 offer->fd = p[0];
1958 offer->func = func;
1959 offer->refcount++;
1960 offer->user_data = user_data;
1961
1962 display_watch_fd(offer->input->display,
1963 offer->fd, EPOLLIN, &offer->io_task);
1964}
1965
1966void
1967input_receive_drag_data(struct input *input, const char *mime_type,
1968 data_func_t func, void *data)
1969{
1970 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1971 input->drag_offer->x = input->sx;
1972 input->drag_offer->y = input->sy;
1973}
1974
1975int
1976input_receive_selection_data(struct input *input, const char *mime_type,
1977 data_func_t func, void *data)
1978{
1979 char **p;
1980
1981 if (input->selection_offer == NULL)
1982 return -1;
1983
1984 for (p = input->selection_offer->types.data; *p; p++)
1985 if (strcmp(mime_type, *p) == 0)
1986 break;
1987
1988 if (*p == NULL)
1989 return -1;
1990
1991 data_offer_receive_data(input->selection_offer,
1992 mime_type, func, data);
1993 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001994}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001995
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001996int
1997input_receive_selection_data_to_fd(struct input *input,
1998 const char *mime_type, int fd)
1999{
2000 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2001
2002 return 0;
2003}
2004
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002005void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002006window_move(struct window *window, struct input *input, uint32_t time)
2007{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002008 if (!window->shell_surface)
2009 return;
2010
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002011 wl_shell_surface_move(window->shell_surface,
2012 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002013}
2014
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002015static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002016idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002017{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002018 struct window *window =
2019 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002020 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002021
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002022 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002023 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002024 widget_set_allocation(widget,
2025 window->pending_allocation.x,
2026 window->pending_allocation.y,
2027 window->pending_allocation.width,
2028 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002029
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002030 if (window->input_region) {
2031 wl_region_destroy(window->input_region);
2032 window->input_region = NULL;
2033 }
2034
2035 if (window->opaque_region) {
2036 wl_region_destroy(window->opaque_region);
2037 window->opaque_region = NULL;
2038 }
2039
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002040 if (widget->resize_handler)
2041 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002042 widget->allocation.width,
2043 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002044 widget->user_data);
2045
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002046 if (window->allocation.width != widget->allocation.width ||
2047 window->allocation.height != widget->allocation.height) {
2048 window->allocation = widget->allocation;
2049 window_schedule_redraw(window);
2050 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002051}
2052
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002053void
2054window_schedule_resize(struct window *window, int width, int height)
2055{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002056 window->pending_allocation.x = 0;
2057 window->pending_allocation.y = 0;
2058 window->pending_allocation.width = width;
2059 window->pending_allocation.height = height;
2060
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002061 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002062 window->resize_task.run = idle_resize;
2063 display_defer(window->display, &window->resize_task);
2064 window->resize_scheduled = 1;
2065 }
2066}
2067
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002068void
2069widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2070{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002071 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002072}
2073
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002074static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002075handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002076 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002077 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002078{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002079 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002080
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002081 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002082 return;
2083
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002084 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002085 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002086}
2087
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002088static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002089menu_destroy(struct menu *menu)
2090{
2091 widget_destroy(menu->widget);
2092 window_destroy(menu->window);
2093 free(menu);
2094}
2095
2096static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002097handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2098{
2099 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002100 struct menu *menu = window->widget->user_data;
2101
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002102 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002103 * device. Or just use wl_callback. And this really needs to
2104 * be a window vfunc that the menu can set. And we need the
2105 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002106
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002107 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002108 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002109 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002110}
2111
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002112static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002113 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002114 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002115};
2116
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002117void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002118window_get_allocation(struct window *window,
2119 struct rectangle *allocation)
2120{
2121 *allocation = window->allocation;
2122}
2123
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002124static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002125widget_redraw(struct widget *widget)
2126{
2127 struct widget *child;
2128
2129 if (widget->redraw_handler)
2130 widget->redraw_handler(widget, widget->user_data);
2131 wl_list_for_each(child, &widget->child_list, link)
2132 widget_redraw(child);
2133}
2134
2135static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002136idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002137{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002138 struct window *window =
2139 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002140
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002141 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002142 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002143 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002144 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002145}
2146
2147void
2148window_schedule_redraw(struct window *window)
2149{
2150 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002151 window->redraw_task.run = idle_redraw;
2152 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002153 window->redraw_scheduled = 1;
2154 }
2155}
2156
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002157void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002158window_set_custom(struct window *window)
2159{
2160 window->type = TYPE_CUSTOM;
2161}
2162
2163void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002164window_set_fullscreen(struct window *window, int fullscreen)
2165{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002166 if (!window->display->shell)
2167 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002168
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002169 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002170 return;
2171
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002172 if (fullscreen) {
2173 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002174 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002175 wl_shell_surface_set_fullscreen(window->shell_surface,
2176 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2177 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002178 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002179 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002180 wl_shell_surface_set_toplevel(window->shell_surface);
2181 window_schedule_resize(window,
2182 window->saved_allocation.width,
2183 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002184 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002185}
2186
2187void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002188window_set_maximized(struct window *window, int maximized)
2189{
2190 if (!window->display->shell)
2191 return;
2192
2193 if ((window->type == TYPE_MAXIMIZED) == maximized)
2194 return;
2195
2196 if (window->type == TYPE_TOPLEVEL) {
2197 window->saved_allocation = window->allocation;
2198 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2199 window->type = TYPE_MAXIMIZED;
2200 } else {
2201 wl_shell_surface_set_toplevel(window->shell_surface);
2202 window->type = TYPE_TOPLEVEL;
2203 window_schedule_resize(window,
2204 window->saved_allocation.width,
2205 window->saved_allocation.height);
2206 }
2207}
2208
2209void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002210window_set_user_data(struct window *window, void *data)
2211{
2212 window->user_data = data;
2213}
2214
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002215void *
2216window_get_user_data(struct window *window)
2217{
2218 return window->user_data;
2219}
2220
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002221void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002222window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002223 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002224{
2225 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002226}
2227
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002228void
2229window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002230 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002231{
2232 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002233}
2234
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002235void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002236window_set_data_handler(struct window *window, window_data_handler_t handler)
2237{
2238 window->data_handler = handler;
2239}
2240
2241void
2242window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2243{
2244 window->drop_handler = handler;
2245}
2246
2247void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002248window_set_close_handler(struct window *window,
2249 window_close_handler_t handler)
2250{
2251 window->close_handler = handler;
2252}
2253
2254void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002255window_set_title(struct window *window, const char *title)
2256{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002257 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002258 window->title = strdup(title);
2259}
2260
2261const char *
2262window_get_title(struct window *window)
2263{
2264 return window->title;
2265}
2266
2267void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002268display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2269 int32_t x, int32_t y, int32_t width, int32_t height)
2270{
2271 struct wl_buffer *buffer;
2272
2273 buffer = display_get_buffer_for_surface(display, cairo_surface);
2274
2275 wl_buffer_damage(buffer, x, y, width, height);
2276}
2277
2278void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002279window_damage(struct window *window, int32_t x, int32_t y,
2280 int32_t width, int32_t height)
2281{
2282 wl_surface_damage(window->surface, x, y, width, height);
2283}
2284
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002285static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002286window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002287{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002288 struct window *window;
2289
2290 window = malloc(sizeof *window);
2291 if (window == NULL)
2292 return NULL;
2293
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002294 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002295 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002296 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002297 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002298 if (display->shell) {
2299 window->shell_surface =
2300 wl_shell_get_shell_surface(display->shell,
2301 window->surface);
2302 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002303 window->allocation.x = 0;
2304 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002305 window->allocation.width = 0;
2306 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002307 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002308 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002309 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002310 window->input_region = NULL;
2311 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002312
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002313 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002314#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002315 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2316 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002317#else
2318 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2319#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002320 else
2321 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002322
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002323 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002324 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002325
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002326 if (window->shell_surface) {
2327 wl_shell_surface_set_user_data(window->shell_surface, window);
2328 wl_shell_surface_add_listener(window->shell_surface,
2329 &shell_surface_listener, window);
2330 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002331
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002332 return window;
2333}
2334
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002335struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002336window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002337{
2338 struct window *window;
2339
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002340 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002341 if (!window)
2342 return NULL;
2343
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002344 window->type = TYPE_TOPLEVEL;
2345 if (display->shell)
2346 wl_shell_surface_set_toplevel(window->shell_surface);
2347
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002348 return window;
2349}
2350
2351struct window *
2352window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002353 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002354{
2355 struct window *window;
2356
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002357 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002358 if (!window)
2359 return NULL;
2360
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002361 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002362 window->x = x;
2363 window->y = y;
2364
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002365 if (display->shell)
2366 wl_shell_surface_set_transient(window->shell_surface,
2367 window->parent->shell_surface,
2368 window->x, window->y, 0);
2369
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002370 return window;
2371}
2372
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002373static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002374menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002375{
2376 int next;
2377
2378 next = (sy - 8) / 20;
2379 if (menu->current != next) {
2380 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002381 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002382 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002383}
2384
2385static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002386menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002387 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002388 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002389{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002390 struct menu *menu = data;
2391
2392 if (widget == menu->widget)
2393 menu_set_item(data, y);
2394
2395 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002396}
2397
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002398static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002399menu_enter_handler(struct widget *widget,
2400 struct input *input, uint32_t time,
2401 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002402{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002403 struct menu *menu = data;
2404
2405 if (widget == menu->widget)
2406 menu_set_item(data, y);
2407
2408 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002409}
2410
2411static void
2412menu_leave_handler(struct widget *widget, struct input *input, void *data)
2413{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002414 struct menu *menu = data;
2415
2416 if (widget == menu->widget)
2417 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002418}
2419
2420static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002421menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002422 struct input *input, uint32_t time,
2423 int button, int state, void *data)
2424
2425{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002426 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002427
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002428 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002429 /* Either relase after press-drag-release or
2430 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002431 menu->func(menu->window->parent,
2432 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002433 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002434 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002435 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002436}
2437
2438static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002439menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002440{
2441 cairo_t *cr;
2442 const int32_t r = 3, margin = 3;
2443 struct menu *menu = data;
2444 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002445 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002446
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002447 cr = cairo_create(window->cairo_surface);
2448 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2449 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2450 cairo_paint(cr);
2451
2452 width = window->allocation.width;
2453 height = window->allocation.height;
2454 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002455
2456 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002457 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2458 cairo_fill(cr);
2459
2460 for (i = 0; i < menu->count; i++) {
2461 if (i == menu->current) {
2462 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2463 cairo_rectangle(cr, margin, i * 20 + margin,
2464 width - 2 * margin, 20);
2465 cairo_fill(cr);
2466 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2467 cairo_move_to(cr, 10, i * 20 + 16);
2468 cairo_show_text(cr, menu->entries[i]);
2469 } else {
2470 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2471 cairo_move_to(cr, 10, i * 20 + 16);
2472 cairo_show_text(cr, menu->entries[i]);
2473 }
2474 }
2475
2476 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002477}
2478
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002479void
2480window_show_menu(struct display *display,
2481 struct input *input, uint32_t time, struct window *parent,
2482 int32_t x, int32_t y,
2483 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002484{
2485 struct window *window;
2486 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002487 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002488
2489 menu = malloc(sizeof *menu);
2490 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002491 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002492
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002493 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002494 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002495 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002496
2497 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002498 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002499 menu->entries = entries;
2500 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002501 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002502 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002503 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002504 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002505 window->type = TYPE_MENU;
2506 window->x = x;
2507 window->y = y;
2508
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002509 wl_shell_surface_set_popup(window->shell_surface,
2510 input->input_device, time,
2511 window->parent->shell_surface,
2512 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002513
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002514 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002515 widget_set_enter_handler(menu->widget, menu_enter_handler);
2516 widget_set_leave_handler(menu->widget, menu_leave_handler);
2517 widget_set_motion_handler(menu->widget, menu_motion_handler);
2518 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002519
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002520 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002521 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002522}
2523
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002524void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002525window_set_buffer_type(struct window *window, enum window_buffer_type type)
2526{
2527 window->buffer_type = type;
2528}
2529
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002530
2531static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002532display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002533 struct wl_output *wl_output,
2534 int x, int y,
2535 int physical_width,
2536 int physical_height,
2537 int subpixel,
2538 const char *make,
2539 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002540{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002541 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002542
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002543 output->allocation.x = x;
2544 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002545}
2546
2547static void
2548display_handle_mode(void *data,
2549 struct wl_output *wl_output,
2550 uint32_t flags,
2551 int width,
2552 int height,
2553 int refresh)
2554{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002555 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002556 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002557
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002558 if (flags & WL_OUTPUT_MODE_CURRENT) {
2559 output->allocation.width = width;
2560 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002561 if (display->output_configure_handler)
2562 (*display->output_configure_handler)(
2563 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002564 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002565}
2566
2567static const struct wl_output_listener output_listener = {
2568 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002569 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002570};
2571
2572static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002573display_add_output(struct display *d, uint32_t id)
2574{
2575 struct output *output;
2576
2577 output = malloc(sizeof *output);
2578 if (output == NULL)
2579 return;
2580
2581 memset(output, 0, sizeof *output);
2582 output->display = d;
2583 output->output =
2584 wl_display_bind(d->display, id, &wl_output_interface);
2585 wl_list_insert(d->output_list.prev, &output->link);
2586
2587 wl_output_add_listener(output->output, &output_listener, output);
2588}
2589
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002590static void
2591output_destroy(struct output *output)
2592{
2593 if (output->destroy_handler)
2594 (*output->destroy_handler)(output, output->user_data);
2595
2596 wl_output_destroy(output->output);
2597 wl_list_remove(&output->link);
2598 free(output);
2599}
2600
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002601void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002602display_set_output_configure_handler(struct display *display,
2603 display_output_handler_t handler)
2604{
2605 struct output *output;
2606
2607 display->output_configure_handler = handler;
2608 if (!handler)
2609 return;
2610
2611 wl_list_for_each(output, &display->output_list, link)
2612 (*display->output_configure_handler)(output,
2613 display->user_data);
2614}
2615
2616void
2617output_set_user_data(struct output *output, void *data)
2618{
2619 output->user_data = data;
2620}
2621
2622void *
2623output_get_user_data(struct output *output)
2624{
2625 return output->user_data;
2626}
2627
2628void
2629output_set_destroy_handler(struct output *output,
2630 display_output_handler_t handler)
2631{
2632 output->destroy_handler = handler;
2633 /* FIXME: implement this, once we have way to remove outputs */
2634}
2635
2636void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002637output_get_allocation(struct output *output, struct rectangle *allocation)
2638{
2639 *allocation = output->allocation;
2640}
2641
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002642struct wl_output *
2643output_get_wl_output(struct output *output)
2644{
2645 return output->output;
2646}
2647
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002648static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002649display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002650{
2651 struct input *input;
2652
2653 input = malloc(sizeof *input);
2654 if (input == NULL)
2655 return;
2656
2657 memset(input, 0, sizeof *input);
2658 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002659 input->input_device =
2660 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002661 input->pointer_focus = NULL;
2662 input->keyboard_focus = NULL;
2663 wl_list_insert(d->input_list.prev, &input->link);
2664
2665 wl_input_device_add_listener(input->input_device,
2666 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002667 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002668
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002669 input->data_device =
2670 wl_data_device_manager_get_data_device(d->data_device_manager,
2671 input->input_device);
2672 wl_data_device_add_listener(input->data_device,
2673 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002674}
2675
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002676static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002677input_destroy(struct input *input)
2678{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002679 input_remove_keyboard_focus(input);
2680 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002681
2682 if (input->drag_offer)
2683 data_offer_destroy(input->drag_offer);
2684
2685 if (input->selection_offer)
2686 data_offer_destroy(input->selection_offer);
2687
2688 wl_data_device_destroy(input->data_device);
2689 wl_list_remove(&input->link);
2690 wl_input_device_destroy(input->input_device);
2691 free(input);
2692}
2693
2694static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002695display_handle_global(struct wl_display *display, uint32_t id,
2696 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002697{
2698 struct display *d = data;
2699
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002700 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002701 d->compositor =
2702 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002703 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002704 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002705 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002706 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002707 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002708 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002709 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002710 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002711 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2712 d->data_device_manager =
2713 wl_display_bind(display, id,
2714 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002715 }
2716}
2717
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002718static void
2719display_render_frame(struct display *d)
2720{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002721 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002722 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002723
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002724 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002725 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2726 cr = cairo_create(d->shadow);
2727 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2728 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002729 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002730 cairo_fill(cr);
2731 cairo_destroy(cr);
2732 blur_surface(d->shadow, 64);
2733
2734 d->active_frame =
2735 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2736 cr = cairo_create(d->active_frame);
2737 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002738
2739 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2740 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2741 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2742 cairo_set_source(cr, pattern);
2743 cairo_pattern_destroy(pattern);
2744
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 d->inactive_frame =
2750 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2751 cr = cairo_create(d->inactive_frame);
2752 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002753 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002754 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002755 cairo_fill(cr);
2756 cairo_destroy(cr);
2757}
2758
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002759static void
2760init_xkb(struct display *d)
2761{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002762 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002763
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002764 names.rules = "evdev";
2765 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002766 names.layout = option_xkb_layout;
2767 names.variant = option_xkb_variant;
2768 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002769
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002770 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002771 if (!d->xkb) {
2772 fprintf(stderr, "Failed to compile keymap\n");
2773 exit(1);
2774 }
2775}
2776
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002777static void
2778fini_xkb(struct display *display)
2779{
2780 xkb_free_keymap(display->xkb);
2781}
2782
Yuval Fledel45568f62010-12-06 09:18:12 -05002783static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002784init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002785{
2786 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002787 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002788
Rob Clark6396ed32012-03-11 19:48:41 -05002789#ifdef USE_CAIRO_GLESV2
2790# define GL_BIT EGL_OPENGL_ES2_BIT
2791#else
2792# define GL_BIT EGL_OPENGL_BIT
2793#endif
2794
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002795 static const EGLint argb_cfg_attribs[] = {
2796 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002797 EGL_RED_SIZE, 1,
2798 EGL_GREEN_SIZE, 1,
2799 EGL_BLUE_SIZE, 1,
2800 EGL_ALPHA_SIZE, 1,
2801 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002802 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002803 EGL_NONE
2804 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002805
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002806#ifdef USE_CAIRO_GLESV2
2807 static const EGLint context_attribs[] = {
2808 EGL_CONTEXT_CLIENT_VERSION, 2,
2809 EGL_NONE
2810 };
2811 EGLint api = EGL_OPENGL_ES_API;
2812#else
2813 EGLint *context_attribs = NULL;
2814 EGLint api = EGL_OPENGL_API;
2815#endif
2816
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002817 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002818 if (!eglInitialize(d->dpy, &major, &minor)) {
2819 fprintf(stderr, "failed to initialize display\n");
2820 return -1;
2821 }
2822
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002823 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002824 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2825 return -1;
2826 }
2827
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002828 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2829 &d->argb_config, 1, &n) || n != 1) {
2830 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002831 return -1;
2832 }
2833
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002834 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002835 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002836 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002837 fprintf(stderr, "failed to create context\n");
2838 return -1;
2839 }
2840
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002841 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002842 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002843 return -1;
2844 }
2845
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002846#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002847 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2848 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2849 fprintf(stderr, "failed to get cairo egl argb device\n");
2850 return -1;
2851 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002852#endif
2853
2854 return 0;
2855}
2856
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002857static void
2858fini_egl(struct display *display)
2859{
2860#ifdef HAVE_CAIRO_EGL
2861 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002862#endif
2863
2864 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2865 EGL_NO_CONTEXT);
2866
2867 eglTerminate(display->dpy);
2868 eglReleaseThread();
2869}
2870
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002871static int
2872event_mask_update(uint32_t mask, void *data)
2873{
2874 struct display *d = data;
2875
2876 d->mask = mask;
2877
2878 return 0;
2879}
2880
2881static void
2882handle_display_data(struct task *task, uint32_t events)
2883{
2884 struct display *display =
2885 container_of(task, struct display, display_task);
2886
2887 wl_display_iterate(display->display, display->mask);
2888}
2889
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002890struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002891display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002892{
2893 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002894
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002895 argc = parse_options(xkb_options,
2896 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002897
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002898 d = malloc(sizeof *d);
2899 if (d == NULL)
2900 return NULL;
2901
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002902 memset(d, 0, sizeof *d);
2903
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002904 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002905 if (d->display == NULL) {
2906 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002907 return NULL;
2908 }
2909
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002910 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2911 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2912 d->display_task.run = handle_display_data;
2913 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2914
2915 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002916 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002917 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002918
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002919 /* Set up listener so we'll catch all events. */
2920 wl_display_add_global_listener(d->display,
2921 display_handle_global, d);
2922
2923 /* Process connection events. */
2924 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002925 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002926 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002927
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002928 d->image_target_texture_2d =
2929 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2930 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2931 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2932
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002933 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002934
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002935 display_render_frame(d);
2936
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002937 wl_list_init(&d->window_list);
2938
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002939 init_xkb(d);
2940
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002941 return d;
2942}
2943
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002944static void
2945display_destroy_outputs(struct display *display)
2946{
2947 struct output *tmp;
2948 struct output *output;
2949
2950 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2951 output_destroy(output);
2952}
2953
Pekka Paalanene1207c72011-12-16 12:02:09 +02002954static void
2955display_destroy_inputs(struct display *display)
2956{
2957 struct input *tmp;
2958 struct input *input;
2959
2960 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2961 input_destroy(input);
2962}
2963
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002964void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002965display_destroy(struct display *display)
2966{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002967 if (!wl_list_empty(&display->window_list))
2968 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2969
2970 if (!wl_list_empty(&display->deferred_list))
2971 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2972
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002973 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002974 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002975
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002976 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002977
2978 cairo_surface_destroy(display->active_frame);
2979 cairo_surface_destroy(display->inactive_frame);
2980 cairo_surface_destroy(display->shadow);
2981 destroy_pointer_surfaces(display);
2982
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002983 fini_egl(display);
2984
Pekka Paalanenc2052982011-12-16 11:41:32 +02002985 if (display->shell)
2986 wl_shell_destroy(display->shell);
2987
2988 if (display->shm)
2989 wl_shm_destroy(display->shm);
2990
2991 if (display->data_device_manager)
2992 wl_data_device_manager_destroy(display->data_device_manager);
2993
2994 wl_compositor_destroy(display->compositor);
2995
2996 close(display->epoll_fd);
2997
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002998 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05002999 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003000 free(display);
3001}
3002
3003void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003004display_set_user_data(struct display *display, void *data)
3005{
3006 display->user_data = data;
3007}
3008
3009void *
3010display_get_user_data(struct display *display)
3011{
3012 return display->user_data;
3013}
3014
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003015struct wl_display *
3016display_get_display(struct display *display)
3017{
3018 return display->display;
3019}
3020
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003021struct output *
3022display_get_output(struct display *display)
3023{
3024 return container_of(display->output_list.next, struct output, link);
3025}
3026
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003027struct wl_compositor *
3028display_get_compositor(struct display *display)
3029{
3030 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003031}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003032
3033EGLDisplay
3034display_get_egl_display(struct display *d)
3035{
3036 return d->dpy;
3037}
3038
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003039struct wl_data_source *
3040display_create_data_source(struct display *display)
3041{
3042 return wl_data_device_manager_create_data_source(display->data_device_manager);
3043}
3044
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003045EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003046display_get_argb_egl_config(struct display *d)
3047{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003048 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003049}
3050
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003051struct wl_shell *
3052display_get_shell(struct display *display)
3053{
3054 return display->shell;
3055}
3056
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003057int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003058display_acquire_window_surface(struct display *display,
3059 struct window *window,
3060 EGLContext ctx)
3061{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003062#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003063 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003064 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003065
3066 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003067 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003068 device = cairo_surface_get_device(window->cairo_surface);
3069 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003070 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003071
Benjamin Franzke0c991632011-09-27 21:57:31 +02003072 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003073 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003074 ctx = display->argb_ctx;
3075 else
3076 assert(0);
3077 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003078
3079 data = cairo_surface_get_user_data(window->cairo_surface,
3080 &surface_data_key);
3081
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003082 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003083 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003084 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3085 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003086
3087 return 0;
3088#else
3089 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003090#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003091}
3092
3093void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003094display_release_window_surface(struct display *display,
3095 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003096{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003097#ifdef HAVE_CAIRO_EGL
3098 cairo_device_t *device;
3099
3100 device = cairo_surface_get_device(window->cairo_surface);
3101 if (!device)
3102 return;
3103
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003104 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003105 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003106 cairo_device_release(device);
3107#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003108}
3109
3110void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003111display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003112{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003113 wl_list_insert(&display->deferred_list, &task->link);
3114}
3115
3116void
3117display_watch_fd(struct display *display,
3118 int fd, uint32_t events, struct task *task)
3119{
3120 struct epoll_event ep;
3121
3122 ep.events = events;
3123 ep.data.ptr = task;
3124 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3125}
3126
3127void
3128display_run(struct display *display)
3129{
3130 struct task *task;
3131 struct epoll_event ep[16];
3132 int i, count;
3133
Pekka Paalanen826d7952011-12-15 10:14:07 +02003134 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003135 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003136 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003137
3138 while (!wl_list_empty(&display->deferred_list)) {
3139 task = container_of(display->deferred_list.next,
3140 struct task, link);
3141 wl_list_remove(&task->link);
3142 task->run(task, 0);
3143 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003144
3145 if (!display->running)
3146 break;
3147
3148 wl_display_flush(display->display);
3149
3150 count = epoll_wait(display->epoll_fd,
3151 ep, ARRAY_LENGTH(ep), -1);
3152 for (i = 0; i < count; i++) {
3153 task = ep[i].data.ptr;
3154 task->run(task, ep[i].events);
3155 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003156 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003157}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003158
3159void
3160display_exit(struct display *display)
3161{
3162 display->running = 0;
3163}