blob: 186eed91b13d1b9e41706ea2305510e96ce35f7e [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øgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.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>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.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øgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030065#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070069struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030070
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050073 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040074 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040075 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060077 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040097 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030099 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300100 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400101
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200105
106 display_output_handler_t output_configure_handler;
107
108 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100109
110 struct xkb_context *xkb_context;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500111};
112
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400114 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400116 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500117 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500119 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_CUSTOM
121};
Rob Bradford7507b572012-05-15 17:55:34 +0100122
123struct window_output {
124 struct output *output;
125 struct wl_list link;
126};
127
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128struct window {
129 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100131 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500132 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200133 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500134 struct wl_region *input_region;
135 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500136 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400138 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500139 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500140 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400141 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400142 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400143 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400144 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400145 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400146 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400147 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400148 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400149 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500150
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400151 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500152
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700153 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400154
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500155 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500156 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400157 window_data_handler_t data_handler;
158 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500159 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400160 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200162 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500163 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500164
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500165 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400166 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500167};
168
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500169struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500170 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300171 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500172 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400173 struct wl_list link;
174 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500175 widget_resize_handler_t resize_handler;
176 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500177 widget_enter_handler_t enter_handler;
178 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500179 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500180 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400181 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500182 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300183 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400184};
185
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400186struct input {
187 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100188 struct wl_seat *seat;
189 struct wl_pointer *pointer;
190 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400191 struct window *pointer_focus;
192 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300193 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300194 uint32_t cursor_anim_start;
195 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300196 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400197 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400198 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400199 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400200 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400201 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500203 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500204 struct widget *grab;
205 uint32_t grab_button;
206
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400207 struct wl_data_device *data_device;
208 struct data_offer *drag_offer;
209 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100210
211 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100212 struct xkb_keymap *keymap;
213 struct xkb_state *state;
214 xkb_mod_mask_t control_mask;
215 xkb_mod_mask_t alt_mask;
216 xkb_mod_mask_t shift_mask;
217 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400218
219 struct task repeat_task;
220 int repeat_timer_fd;
221 uint32_t repeat_sym;
222 uint32_t repeat_key;
223 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400224};
225
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500226struct output {
227 struct display *display;
228 struct wl_output *output;
229 struct rectangle allocation;
230 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200231
232 display_output_handler_t destroy_handler;
233 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500234};
235
Martin Minarik1998b152012-05-10 02:04:35 +0200236enum frame_button_action {
237 FRAME_BUTTON_NULL = 0,
238 FRAME_BUTTON_ICON = 1,
239 FRAME_BUTTON_CLOSE = 2,
240 FRAME_BUTTON_MINIMIZE = 3,
241 FRAME_BUTTON_MAXIMIZE = 4,
242};
243
244enum frame_button_pointer {
245 FRAME_BUTTON_DEFAULT = 0,
246 FRAME_BUTTON_OVER = 1,
247 FRAME_BUTTON_ACTIVE = 2,
248};
249
250enum frame_button_align {
251 FRAME_BUTTON_RIGHT = 0,
252 FRAME_BUTTON_LEFT = 1,
253};
254
255enum frame_button_decoration {
256 FRAME_BUTTON_NONE = 0,
257 FRAME_BUTTON_FANCY = 1,
258};
259
260struct frame_button {
261 struct widget *widget;
262 struct frame *frame;
263 cairo_surface_t *icon;
264 enum frame_button_action type;
265 enum frame_button_pointer state;
266 struct wl_list link; /* buttons_list */
267 enum frame_button_align align;
268 enum frame_button_decoration decoration;
269};
270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500271struct frame {
272 struct widget *widget;
273 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200274 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500275};
276
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500277struct menu {
278 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500279 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500280 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500281 const char **entries;
282 uint32_t time;
283 int current;
284 int count;
285 menu_func_t func;
286};
287
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300288struct tooltip {
289 struct widget *parent;
290 struct window *window;
291 struct widget *widget;
292 char *entry;
293 struct task tooltip_task;
294 int tooltip_fd;
295 float x, y;
296};
297
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700298struct shm_pool {
299 struct wl_shm_pool *pool;
300 size_t size;
301 size_t used;
302 void *data;
303};
304
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400305enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300306 CURSOR_DEFAULT = 100,
307 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400308};
309
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500310enum window_location {
311 WINDOW_INTERIOR = 0,
312 WINDOW_RESIZING_TOP = 1,
313 WINDOW_RESIZING_BOTTOM = 2,
314 WINDOW_RESIZING_LEFT = 4,
315 WINDOW_RESIZING_TOP_LEFT = 5,
316 WINDOW_RESIZING_BOTTOM_LEFT = 6,
317 WINDOW_RESIZING_RIGHT = 8,
318 WINDOW_RESIZING_TOP_RIGHT = 9,
319 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
320 WINDOW_RESIZING_MASK = 15,
321 WINDOW_EXTERIOR = 16,
322 WINDOW_TITLEBAR = 17,
323 WINDOW_CLIENT_AREA = 18,
324};
325
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400326const char *option_xkb_layout = "us";
327const char *option_xkb_variant = "";
328const char *option_xkb_options = "";
329
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400330static const struct weston_option xkb_options[] = {
331 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
332 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
333 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400334};
335
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336static const cairo_user_data_key_t surface_data_key;
337struct surface_data {
338 struct wl_buffer *buffer;
339};
340
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500341#define MULT(_d,c,a,t) \
342 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
343
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500344#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400345
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100346struct egl_window_surface_data {
347 struct display *display;
348 struct wl_surface *surface;
349 struct wl_egl_window *window;
350 EGLSurface surf;
351};
352
353static void
354egl_window_surface_data_destroy(void *p)
355{
356 struct egl_window_surface_data *data = p;
357 struct display *d = data->display;
358
359 eglDestroySurface(d->dpy, data->surf);
360 wl_egl_window_destroy(data->window);
361 data->surface = NULL;
362
363 free(p);
364}
365
366static cairo_surface_t *
367display_create_egl_window_surface(struct display *display,
368 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400369 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100370 struct rectangle *rectangle)
371{
372 cairo_surface_t *cairo_surface;
373 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200375 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100376
377 data = malloc(sizeof *data);
378 if (data == NULL)
379 return NULL;
380
381 data->display = display;
382 data->surface = surface;
383
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500384 config = display->argb_config;
385 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400386
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400387 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100388 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400389 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100390
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400391 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500392 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100393
Benjamin Franzke0c991632011-09-27 21:57:31 +0200394 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100395 data->surf,
396 rectangle->width,
397 rectangle->height);
398
399 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
400 data, egl_window_surface_data_destroy);
401
402 return cairo_surface;
403}
404
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405#endif
406
407struct wl_buffer *
408display_get_buffer_for_surface(struct display *display,
409 cairo_surface_t *surface)
410{
411 struct surface_data *data;
412
413 data = cairo_surface_get_user_data (surface, &surface_data_key);
414
415 return data->buffer;
416}
417
418struct shm_surface_data {
419 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700420 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421};
422
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500423static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700424shm_pool_destroy(struct shm_pool *pool);
425
426static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427shm_surface_data_destroy(void *p)
428{
429 struct shm_surface_data *data = p;
430
431 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700432 if (data->pool)
433 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300434
435 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436}
437
Kristian Høgsberg16626282012-04-03 11:21:27 -0400438static struct wl_shm_pool *
439make_shm_pool(struct display *display, int size, void **data)
440{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400441 struct wl_shm_pool *pool;
442 int fd;
443
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300444 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400445 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300446 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
447 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400448 return NULL;
449 }
450
451 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400452 if (*data == MAP_FAILED) {
453 fprintf(stderr, "mmap failed: %m\n");
454 close(fd);
455 return NULL;
456 }
457
458 pool = wl_shm_create_pool(display->shm, fd, size);
459
460 close(fd);
461
462 return pool;
463}
464
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700465static struct shm_pool *
466shm_pool_create(struct display *display, size_t size)
467{
468 struct shm_pool *pool = malloc(sizeof *pool);
469
470 if (!pool)
471 return NULL;
472
473 pool->pool = make_shm_pool(display, size, &pool->data);
474 if (!pool->pool) {
475 free(pool);
476 return NULL;
477 }
478
479 pool->size = size;
480 pool->used = 0;
481
482 return pool;
483}
484
485static void *
486shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
487{
488 if (pool->used + size > pool->size)
489 return NULL;
490
491 *offset = pool->used;
492 pool->used += size;
493
494 return (char *) pool->data + *offset;
495}
496
497/* destroy the pool. this does not unmap the memory though */
498static void
499shm_pool_destroy(struct shm_pool *pool)
500{
501 munmap(pool->data, pool->size);
502 wl_shm_pool_destroy(pool->pool);
503 free(pool);
504}
505
506/* Start allocating from the beginning of the pool again */
507static void
508shm_pool_reset(struct shm_pool *pool)
509{
510 pool->used = 0;
511}
512
513static int
514data_length_for_shm_surface(struct rectangle *rect)
515{
516 int stride;
517
518 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
519 rect->width);
520 return stride * rect->height;
521}
522
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500523static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700524display_create_shm_surface_from_pool(struct display *display,
525 struct rectangle *rectangle,
526 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527{
528 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400529 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400530 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700531 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400532 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400533
534 data = malloc(sizeof *data);
535 if (data == NULL)
536 return NULL;
537
538 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
539 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700540 length = stride * rectangle->height;
541 data->pool = NULL;
542 map = shm_pool_allocate(pool, length, &offset);
543
544 if (!map) {
545 free(data);
546 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400547 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400549 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550 CAIRO_FORMAT_ARGB32,
551 rectangle->width,
552 rectangle->height,
553 stride);
554
555 cairo_surface_set_user_data (surface, &surface_data_key,
556 data, shm_surface_data_destroy);
557
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400558 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500559 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400560 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500561 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400562
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700563 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400564 rectangle->width,
565 rectangle->height,
566 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400567
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700568 return surface;
569}
570
571static cairo_surface_t *
572display_create_shm_surface(struct display *display,
573 struct rectangle *rectangle, uint32_t flags,
574 struct window *window)
575{
576 struct shm_surface_data *data;
577 struct shm_pool *pool;
578 cairo_surface_t *surface;
579
580 if (window && window->pool) {
581 shm_pool_reset(window->pool);
582 surface = display_create_shm_surface_from_pool(display,
583 rectangle,
584 flags,
585 window->pool);
586 if (surface)
587 return surface;
588 }
589
590 pool = shm_pool_create(display,
591 data_length_for_shm_surface(rectangle));
592 if (!pool)
593 return NULL;
594
595 surface =
596 display_create_shm_surface_from_pool(display, rectangle,
597 flags, pool);
598
599 if (!surface) {
600 shm_pool_destroy(pool);
601 return NULL;
602 }
603
604 /* make sure we destroy the pool when the surface is destroyed */
605 data = cairo_surface_get_user_data(surface, &surface_data_key);
606 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400607
608 return surface;
609}
610
nobled7b87cb02011-02-01 18:51:47 +0000611static int
612check_size(struct rectangle *rect)
613{
614 if (rect->width && rect->height)
615 return 0;
616
617 fprintf(stderr, "tried to create surface of "
618 "width: %d, height: %d\n", rect->width, rect->height);
619 return -1;
620}
621
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622cairo_surface_t *
623display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100624 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400625 struct rectangle *rectangle,
626 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627{
nobled7b87cb02011-02-01 18:51:47 +0000628 if (check_size(rectangle) < 0)
629 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500630#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300631 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400632 return display_create_egl_window_surface(display,
633 surface,
634 flags,
635 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400636#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400637 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638}
639
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300640static const char *cursors[] = {
641 "bottom_left_corner",
642 "bottom_right_corner",
643 "bottom_side",
644 "grabbing",
645 "left_ptr",
646 "left_side",
647 "right_side",
648 "top_left_corner",
649 "top_right_corner",
650 "top_side",
651 "xterm",
652 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400653 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300654};
655
656static void
657create_cursors(struct display *display)
658{
659 unsigned int i;
660
661 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
662 display->cursors =
663 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
664
665 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
666 display->cursors[i] =
667 wl_cursor_theme_get_cursor(display->cursor_theme,
668 cursors[i]);
669}
670
671static void
672destroy_cursors(struct display *display)
673{
674 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800675 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300676}
677
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300678struct wl_cursor_image *
679display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400680{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300681 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300682 wl_cursor_theme_get_cursor(display->cursor_theme,
683 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400684
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300685 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400686}
687
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400688static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200689window_get_resize_dx_dy(struct window *window, int *x, int *y)
690{
691 if (window->resize_edges & WINDOW_RESIZING_LEFT)
692 *x = window->server_allocation.width - window->allocation.width;
693 else
694 *x = 0;
695
696 if (window->resize_edges & WINDOW_RESIZING_TOP)
697 *y = window->server_allocation.height -
698 window->allocation.height;
699 else
700 *y = 0;
701
702 window->resize_edges = 0;
703}
704
705static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500706window_attach_surface(struct window *window)
707{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400708 struct display *display = window->display;
709 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000710#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100711 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000712#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500713 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100714
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400715 if (window->type == TYPE_NONE) {
716 window->type = TYPE_TOPLEVEL;
717 if (display->shell)
718 wl_shell_surface_set_toplevel(window->shell_surface);
719 }
720
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100721 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000722#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
724 data = cairo_surface_get_user_data(window->cairo_surface,
725 &surface_data_key);
726
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727 cairo_gl_surface_swapbuffers(window->cairo_surface);
728 wl_egl_window_get_attached_size(data->window,
729 &window->server_allocation.width,
730 &window->server_allocation.height);
731 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000732#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100734 buffer =
735 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400736 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100737
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200738 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400740 wl_surface_damage(window->surface, 0, 0,
741 window->allocation.width,
742 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100743 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400744 cairo_surface_destroy(window->cairo_surface);
745 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100746 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000747 default:
748 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100749 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500750
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500751 if (window->input_region) {
752 wl_surface_set_input_region(window->surface,
753 window->input_region);
754 wl_region_destroy(window->input_region);
755 window->input_region = NULL;
756 }
757
758 if (window->opaque_region) {
759 wl_surface_set_opaque_region(window->surface,
760 window->opaque_region);
761 wl_region_destroy(window->opaque_region);
762 window->opaque_region = NULL;
763 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500764}
765
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500766void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400767window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500768{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400769 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100770 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500771}
772
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400773void
774window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400775{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500776 cairo_surface_reference(surface);
777
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400778 if (window->cairo_surface != NULL)
779 cairo_surface_destroy(window->cairo_surface);
780
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500781 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400782}
783
Benjamin Franzke22d54812011-07-16 19:50:32 +0000784#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100785static void
786window_resize_cairo_window_surface(struct window *window)
787{
788 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200789 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100790
791 data = cairo_surface_get_user_data(window->cairo_surface,
792 &surface_data_key);
793
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200794 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100795 wl_egl_window_resize(data->window,
796 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200797 window->allocation.height,
798 x,y);
799
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100800 cairo_gl_surface_set_size(window->cairo_surface,
801 window->allocation.width,
802 window->allocation.height);
803}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000804#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100805
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400806struct display *
807window_get_display(struct window *window)
808{
809 return window->display;
810}
811
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400812void
813window_create_surface(struct window *window)
814{
815 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400816 uint32_t flags = 0;
817
818 if (!window->transparent)
819 flags = SURFACE_OPAQUE;
820
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
824 if (window->cairo_surface) {
825 window_resize_cairo_window_surface(window);
826 return;
827 }
828 surface = display_create_surface(window->display,
829 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400830 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400832#endif
833 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400834 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400835 &window->allocation,
836 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800838 default:
839 surface = NULL;
840 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400841 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400842
843 window_set_surface(window, surface);
844 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400845}
846
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200847static void frame_destroy(struct frame *frame);
848
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400849void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500850window_destroy(struct window *window)
851{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200852 struct display *display = window->display;
853 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100854 struct window_output *window_output;
855 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200856
857 if (window->redraw_scheduled)
858 wl_list_remove(&window->redraw_task.link);
859
860 wl_list_for_each(input, &display->input_list, link) {
861 if (input->pointer_focus == window)
862 input->pointer_focus = NULL;
863 if (input->keyboard_focus == window)
864 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500865 if (input->focus_widget &&
866 input->focus_widget->window == window)
867 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200868 }
869
Rob Bradford7507b572012-05-15 17:55:34 +0100870 wl_list_for_each_safe(window_output, window_output_tmp,
871 &window->window_output_list, link) {
872 free (window_output);
873 }
874
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500875 if (window->input_region)
876 wl_region_destroy(window->input_region);
877 if (window->opaque_region)
878 wl_region_destroy(window->opaque_region);
879
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200880 if (window->frame)
881 frame_destroy(window->frame);
882
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200883 if (window->shell_surface)
884 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500885 wl_surface_destroy(window->surface);
886 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200887
888 if (window->cairo_surface != NULL)
889 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200890
891 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500892 free(window);
893}
894
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500895static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500896widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400897{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500898 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400899
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500900 wl_list_for_each(child, &widget->child_list, link) {
901 target = widget_find_widget(child, x, y);
902 if (target)
903 return target;
904 }
905
906 if (widget->allocation.x <= x &&
907 x < widget->allocation.x + widget->allocation.width &&
908 widget->allocation.y <= y &&
909 y < widget->allocation.y + widget->allocation.height) {
910 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400911 }
912
913 return NULL;
914}
915
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500916static struct widget *
917widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400918{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500919 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400920
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500921 widget = malloc(sizeof *widget);
922 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500923 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500924 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500925 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500926 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500927 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300928 widget->tooltip = NULL;
929 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500930
931 return widget;
932}
933
934struct widget *
935window_add_widget(struct window *window, void *data)
936{
937 window->widget = widget_create(window, data);
938 wl_list_init(&window->widget->link);
939
940 return window->widget;
941}
942
943struct widget *
944widget_add_widget(struct widget *parent, void *data)
945{
946 struct widget *widget;
947
948 widget = widget_create(parent->window, data);
949 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400950
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500951 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400952}
953
954void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500955widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400956{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200957 struct display *display = widget->window->display;
958 struct input *input;
959
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300960 if (widget->tooltip) {
961 free(widget->tooltip);
962 widget->tooltip = NULL;
963 }
964
Pekka Paalanene156fb62012-01-19 13:51:38 +0200965 wl_list_for_each(input, &display->input_list, link) {
966 if (input->focus_widget == widget)
967 input->focus_widget = NULL;
968 }
969
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500970 wl_list_remove(&widget->link);
971 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400972}
973
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400974void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500975widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400976{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500977 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400978}
979
980void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500981widget_set_size(struct widget *widget, int32_t width, int32_t height)
982{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500983 widget->allocation.width = width;
984 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500985}
986
987void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500988widget_set_allocation(struct widget *widget,
989 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400990{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500991 widget->allocation.x = x;
992 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200993 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400994}
995
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500996void
997widget_set_transparent(struct widget *widget, int transparent)
998{
999 widget->opaque = !transparent;
1000}
1001
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001002void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001003widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001004{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001005 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001006}
1007
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001008void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001009widget_set_resize_handler(struct widget *widget,
1010 widget_resize_handler_t handler)
1011{
1012 widget->resize_handler = handler;
1013}
1014
1015void
1016widget_set_redraw_handler(struct widget *widget,
1017 widget_redraw_handler_t handler)
1018{
1019 widget->redraw_handler = handler;
1020}
1021
1022void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001023widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001024{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001025 widget->enter_handler = handler;
1026}
1027
1028void
1029widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1030{
1031 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001032}
1033
1034void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001035widget_set_motion_handler(struct widget *widget,
1036 widget_motion_handler_t handler)
1037{
1038 widget->motion_handler = handler;
1039}
1040
1041void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001042widget_set_button_handler(struct widget *widget,
1043 widget_button_handler_t handler)
1044{
1045 widget->button_handler = handler;
1046}
1047
1048void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001049widget_schedule_redraw(struct widget *widget)
1050{
1051 window_schedule_redraw(widget->window);
1052}
1053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001054cairo_surface_t *
1055window_get_surface(struct window *window)
1056{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001057 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001058}
1059
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001060struct wl_surface *
1061window_get_wl_surface(struct window *window)
1062{
1063 return window->surface;
1064}
1065
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001066struct wl_shell_surface *
1067window_get_wl_shell_surface(struct window *window)
1068{
1069 return window->shell_surface;
1070}
1071
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001072static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001073tooltip_redraw_handler(struct widget *widget, void *data)
1074{
1075 cairo_t *cr;
1076 const int32_t r = 3;
1077 struct tooltip *tooltip = data;
1078 int32_t width, height;
1079 struct window *window = widget->window;
1080
1081 cr = cairo_create(window->cairo_surface);
1082 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1083 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1084 cairo_paint(cr);
1085
1086 width = window->allocation.width;
1087 height = window->allocation.height;
1088 rounded_rect(cr, 0, 0, width, height, r);
1089
1090 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1091 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1092 cairo_fill(cr);
1093
1094 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1095 cairo_move_to(cr, 10, 16);
1096 cairo_show_text(cr, tooltip->entry);
1097 cairo_destroy(cr);
1098}
1099
1100static cairo_text_extents_t
1101get_text_extents(struct tooltip *tooltip)
1102{
1103 struct window *window;
1104 cairo_t *cr;
1105 cairo_text_extents_t extents;
1106
1107 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1108 * created yet */
1109 window = tooltip->widget->window->parent;
1110 cr = cairo_create(window->cairo_surface);
1111 cairo_text_extents(cr, tooltip->entry, &extents);
1112 cairo_destroy(cr);
1113
1114 return extents;
1115}
1116
1117static int
1118window_create_tooltip(struct tooltip *tooltip)
1119{
1120 struct widget *parent = tooltip->parent;
1121 struct display *display = parent->window->display;
1122 struct window *window;
1123 const int offset_y = 27;
1124 const int margin = 3;
1125 cairo_text_extents_t extents;
1126
1127 if (tooltip->widget)
1128 return 0;
1129
1130 window = window_create_transient(display, parent->window, tooltip->x,
1131 tooltip->y + offset_y,
1132 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1133 if (!window)
1134 return -1;
1135
1136 tooltip->window = window;
1137 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1138
1139 extents = get_text_extents(tooltip);
1140 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1141 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1142
1143 return 0;
1144}
1145
1146void
1147widget_destroy_tooltip(struct widget *parent)
1148{
1149 struct tooltip *tooltip = parent->tooltip;
1150
1151 parent->tooltip_count = 0;
1152 if (!tooltip)
1153 return;
1154
1155 if (tooltip->widget) {
1156 widget_destroy(tooltip->widget);
1157 window_destroy(tooltip->window);
1158 tooltip->widget = NULL;
1159 tooltip->window = NULL;
1160 }
1161
1162 close(tooltip->tooltip_fd);
1163 free(tooltip->entry);
1164 free(tooltip);
1165 parent->tooltip = NULL;
1166}
1167
1168static void
1169tooltip_func(struct task *task, uint32_t events)
1170{
1171 struct tooltip *tooltip =
1172 container_of(task, struct tooltip, tooltip_task);
1173 uint64_t exp;
1174
Martin Olsson8df662a2012-07-08 03:03:47 +02001175 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1176 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001177 window_create_tooltip(tooltip);
1178}
1179
1180#define TOOLTIP_TIMEOUT 500
1181static int
1182tooltip_timer_reset(struct tooltip *tooltip)
1183{
1184 struct itimerspec its;
1185
1186 its.it_interval.tv_sec = 0;
1187 its.it_interval.tv_nsec = 0;
1188 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1189 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1190 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1191 fprintf(stderr, "could not set timerfd\n: %m");
1192 return -1;
1193 }
1194
1195 return 0;
1196}
1197
1198int
1199widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1200{
1201 struct tooltip *tooltip = parent->tooltip;
1202
1203 parent->tooltip_count++;
1204 if (tooltip) {
1205 tooltip->x = x;
1206 tooltip->y = y;
1207 tooltip_timer_reset(tooltip);
1208 return 0;
1209 }
1210
1211 /* the handler might be triggered too fast via input device motion, so
1212 * we need this check here to make sure tooltip is fully initialized */
1213 if (parent->tooltip_count > 1)
1214 return 0;
1215
1216 tooltip = malloc(sizeof *tooltip);
1217 if (!tooltip)
1218 return -1;
1219
1220 parent->tooltip = tooltip;
1221 tooltip->parent = parent;
1222 tooltip->widget = NULL;
1223 tooltip->window = NULL;
1224 tooltip->x = x;
1225 tooltip->y = y;
1226 tooltip->entry = strdup(entry);
1227 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1228 if (tooltip->tooltip_fd < 0) {
1229 fprintf(stderr, "could not create timerfd\n: %m");
1230 return -1;
1231 }
1232
1233 tooltip->tooltip_task.run = tooltip_func;
1234 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1235 EPOLLIN, &tooltip->tooltip_task);
1236 tooltip_timer_reset(tooltip);
1237
1238 return 0;
1239}
1240
1241static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242frame_resize_handler(struct widget *widget,
1243 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001244{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245 struct frame *frame = data;
1246 struct widget *child = frame->child;
1247 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001248 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001249 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001250 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001251 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001252 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001253 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001254
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001255 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001256 decoration_width = (t->width + t->margin) * 2;
1257 decoration_height = t->width +
1258 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001259
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001260 allocation.x = t->width + t->margin;
1261 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001262 allocation.width = width - decoration_width;
1263 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001264
Kristian Høgsbergdd263e52012-07-09 22:22:37 -04001265 widget->window->input_region =
1266 wl_compositor_create_region(display->compositor);
1267 wl_region_add(widget->window->input_region,
1268 t->margin, t->margin,
1269 width - 2 * t->margin,
1270 height - 2 * t->margin);
1271
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001272 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001273
1274 wl_list_for_each(button, &frame->buttons_list, link)
1275 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001276 } else {
1277 decoration_width = 0;
1278 decoration_height = 0;
1279
1280 allocation.x = 0;
1281 allocation.y = 0;
1282 allocation.width = width;
1283 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001284 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001285
1286 wl_list_for_each(button, &frame->buttons_list, link)
1287 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001288 }
1289
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001290 widget_set_allocation(child, allocation.x, allocation.y,
1291 allocation.width, allocation.height);
1292
1293 if (child->resize_handler)
1294 child->resize_handler(child,
1295 allocation.width,
1296 allocation.height,
1297 child->user_data);
1298
Scott Moreauf7e498c2012-05-14 11:39:29 -06001299 width = child->allocation.width + decoration_width;
1300 height = child->allocation.height + decoration_height;
1301
Scott Moreauf7e498c2012-05-14 11:39:29 -06001302 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001303
1304 if (child->opaque) {
1305 widget->window->opaque_region =
1306 wl_compositor_create_region(display->compositor);
1307 wl_region_add(widget->window->opaque_region,
1308 opaque_margin, opaque_margin,
1309 widget->allocation.width - 2 * opaque_margin,
1310 widget->allocation.height - 2 * opaque_margin);
1311 }
Martin Minarik1998b152012-05-10 02:04:35 +02001312
1313 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001314 x_r = frame->widget->allocation.width - t->width - t->margin;
1315 x_l = t->width + t->margin;
1316 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001317 wl_list_for_each(button, &frame->buttons_list, link) {
1318 const int button_padding = 4;
1319 w = cairo_image_surface_get_width(button->icon);
1320 h = cairo_image_surface_get_height(button->icon);
1321
1322 if (button->decoration == FRAME_BUTTON_FANCY)
1323 w += 10;
1324
1325 if (button->align == FRAME_BUTTON_LEFT) {
1326 widget_set_allocation(button->widget,
1327 x_l, y , w + 1, h + 1);
1328 x_l += w;
1329 x_l += button_padding;
1330 } else {
1331 x_r -= w;
1332 widget_set_allocation(button->widget,
1333 x_r, y , w + 1, h + 1);
1334 x_r -= button_padding;
1335 }
1336 }
1337}
1338
1339static int
1340frame_button_enter_handler(struct widget *widget,
1341 struct input *input, float x, float y, void *data)
1342{
1343 struct frame_button *frame_button = data;
1344
1345 widget_schedule_redraw(frame_button->widget);
1346 frame_button->state = FRAME_BUTTON_OVER;
1347
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001348 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001349}
1350
1351static void
1352frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1353{
1354 struct frame_button *frame_button = data;
1355
1356 widget_schedule_redraw(frame_button->widget);
1357 frame_button->state = FRAME_BUTTON_DEFAULT;
1358}
1359
1360static void
1361frame_button_button_handler(struct widget *widget,
1362 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001363 uint32_t button,
1364 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001365{
1366 struct frame_button *frame_button = data;
1367 struct window *window = widget->window;
1368
1369 if (button != BTN_LEFT)
1370 return;
1371
1372 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001373 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001374 frame_button->state = FRAME_BUTTON_ACTIVE;
1375 widget_schedule_redraw(frame_button->widget);
1376
1377 if (frame_button->type == FRAME_BUTTON_ICON)
1378 window_show_frame_menu(window, input, time);
1379 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001380 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001381 frame_button->state = FRAME_BUTTON_DEFAULT;
1382 widget_schedule_redraw(frame_button->widget);
1383 break;
1384 }
1385
1386 switch (frame_button->type) {
1387 case FRAME_BUTTON_CLOSE:
1388 if (window->close_handler)
1389 window->close_handler(window->parent,
1390 window->user_data);
1391 else
1392 display_exit(window->display);
1393 break;
1394 case FRAME_BUTTON_MINIMIZE:
1395 fprintf(stderr,"Minimize stub\n");
1396 break;
1397 case FRAME_BUTTON_MAXIMIZE:
1398 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1399 break;
1400 default:
1401 /* Unknown operation */
1402 break;
1403 }
1404}
1405
1406static void
1407frame_button_redraw_handler(struct widget *widget, void *data)
1408{
1409 struct frame_button *frame_button = data;
1410 cairo_t *cr;
1411 int width, height, x, y;
1412 struct window *window = widget->window;
1413
1414 x = widget->allocation.x;
1415 y = widget->allocation.y;
1416 width = widget->allocation.width;
1417 height = widget->allocation.height;
1418
1419 if (!width)
1420 return;
1421 if (!height)
1422 return;
1423 if (widget->opaque)
1424 return;
1425
1426 cr = cairo_create(window->cairo_surface);
1427
1428 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1429 cairo_set_line_width(cr, 1);
1430
1431 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1432 cairo_rectangle (cr, x, y, 25, 16);
1433
1434 cairo_stroke_preserve(cr);
1435
1436 switch (frame_button->state) {
1437 case FRAME_BUTTON_DEFAULT:
1438 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1439 break;
1440 case FRAME_BUTTON_OVER:
1441 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1442 break;
1443 case FRAME_BUTTON_ACTIVE:
1444 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1445 break;
1446 }
1447
1448 cairo_fill (cr);
1449
1450 x += 4;
1451 }
1452
1453 cairo_set_source_surface(cr, frame_button->icon, x, y);
1454 cairo_paint(cr);
1455
1456 cairo_destroy(cr);
1457}
1458
1459static struct widget *
1460frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1461 enum frame_button_align align, enum frame_button_decoration style)
1462{
1463 struct frame_button *frame_button;
1464 const char *icon = data;
1465
1466 frame_button = malloc (sizeof *frame_button);
1467 memset(frame_button, 0, sizeof *frame_button);
1468
1469 frame_button->icon = cairo_image_surface_create_from_png(icon);
1470 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1471 frame_button->frame = frame;
1472 frame_button->type = type;
1473 frame_button->align = align;
1474 frame_button->decoration = style;
1475
1476 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1477
1478 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1479 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1480 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1481 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1482 return frame_button->widget;
1483}
1484
1485static void
1486frame_button_destroy(struct frame_button *frame_button)
1487{
1488 widget_destroy(frame_button->widget);
1489 wl_list_remove(&frame_button->link);
1490 cairo_surface_destroy(frame_button->icon);
1491 free(frame_button);
1492
1493 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001494}
1495
1496static void
1497frame_redraw_handler(struct widget *widget, void *data)
1498{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001500 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001501 struct theme *t = window->display->theme;
1502 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001503
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001504 if (window->type == TYPE_FULLSCREEN)
1505 return;
1506
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001507 cr = cairo_create(window->cairo_surface);
1508
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001509 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001510 flags |= THEME_FRAME_ACTIVE;
1511 theme_render_frame(t, cr, widget->allocation.width,
1512 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001513
1514 cairo_destroy(cr);
1515}
1516
1517static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001518frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001519{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001521 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001522
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001523 location = theme_get_location(t, input->sx, input->sy,
1524 frame->widget->allocation.width,
1525 frame->widget->allocation.height);
1526
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001527 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001533 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001534 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001535 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001536 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001537 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001538 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001539 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001540 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001541 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001542 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001543 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001544 case THEME_LOCATION_EXTERIOR:
1545 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001546 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001547 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001548 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001549}
1550
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001551static void
1552frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001553{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001554 switch (index) {
1555 case 0: /* close */
1556 if (window->close_handler)
1557 window->close_handler(window->parent,
1558 window->user_data);
1559 else
1560 display_exit(window->display);
1561 break;
1562 case 1: /* fullscreen */
1563 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001564 if (window->fullscreen_handler)
1565 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001566 break;
1567 case 2: /* rotate */
1568 case 3: /* scale */
1569 break;
1570 }
1571}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001572
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001573void
1574window_show_frame_menu(struct window *window,
1575 struct input *input, uint32_t time)
1576{
1577 int32_t x, y;
1578
1579 static const char *entries[] = {
1580 "Close", "Fullscreen", "Rotate", "Scale"
1581 };
1582
1583 input_get_position(input, &x, &y);
1584 window_show_menu(window->display, input, time, window,
1585 x - 10, y - 10, frame_menu_func, entries, 4);
1586}
1587
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001588static int
1589frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001590 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591{
1592 return frame_get_pointer_image_for_location(data, input);
1593}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001594
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595static int
1596frame_motion_handler(struct widget *widget,
1597 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001598 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599{
1600 return frame_get_pointer_image_for_location(data, input);
1601}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001602
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001604frame_button_handler(struct widget *widget,
1605 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001606 uint32_t button, enum wl_pointer_button_state state,
1607 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608
1609{
1610 struct frame *frame = data;
1611 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001612 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001613 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001614
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001615 location = theme_get_location(display->theme, input->sx, input->sy,
1616 frame->widget->allocation.width,
1617 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001618
Daniel Stone4dbadb12012-05-30 16:31:51 +01001619 if (window->display->shell && button == BTN_LEFT &&
1620 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001621 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001622 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623 if (!window->shell_surface)
1624 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001625 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001626 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001627 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001628 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001629 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001630 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001631 case THEME_LOCATION_RESIZING_TOP:
1632 case THEME_LOCATION_RESIZING_BOTTOM:
1633 case THEME_LOCATION_RESIZING_LEFT:
1634 case THEME_LOCATION_RESIZING_RIGHT:
1635 case THEME_LOCATION_RESIZING_TOP_LEFT:
1636 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1637 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1638 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001639 if (!window->shell_surface)
1640 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001641 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001642
1643 if (!display->dpy) {
1644 /* If we're using shm, allocate a big
1645 pool to create buffers out of while
1646 we resize. We should probably base
1647 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001648 window->pool =
1649 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001650 }
1651
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001652 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001653 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001654 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001655 break;
1656 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001657 } else if (button == BTN_RIGHT &&
1658 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001659 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001660 }
1661}
1662
1663struct widget *
1664frame_create(struct window *window, void *data)
1665{
1666 struct frame *frame;
1667
1668 frame = malloc(sizeof *frame);
1669 memset(frame, 0, sizeof *frame);
1670
1671 frame->widget = window_add_widget(window, frame);
1672 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001673
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001674 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1675 widget_set_resize_handler(frame->widget, frame_resize_handler);
1676 widget_set_enter_handler(frame->widget, frame_enter_handler);
1677 widget_set_motion_handler(frame->widget, frame_motion_handler);
1678 widget_set_button_handler(frame->widget, frame_button_handler);
1679
Martin Minarik1998b152012-05-10 02:04:35 +02001680 /* Create empty list for frame buttons */
1681 wl_list_init(&frame->buttons_list);
1682
1683 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1684 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1685
1686 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1687 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1688
1689 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1690 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1691
1692 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1693 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1694
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001695 window->frame = frame;
1696
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001697 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001698}
1699
Kristian Høgsberga1627922012-06-20 17:30:03 -04001700void
1701frame_set_child_size(struct widget *widget, int child_width, int child_height)
1702{
1703 struct display *display = widget->window->display;
1704 struct theme *t = display->theme;
1705 int decoration_width, decoration_height;
1706 int width, height;
1707
1708 if (widget->window->type != TYPE_FULLSCREEN) {
1709 decoration_width = (t->width + t->margin) * 2;
1710 decoration_height = t->width +
1711 t->titlebar_height + t->margin * 2;
1712
1713 width = child_width + decoration_width;
1714 height = child_height + decoration_height;
1715 } else {
1716 width = child_width;
1717 height = child_height;
1718 }
1719
1720 window_schedule_resize(widget->window, width, height);
1721}
1722
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001723static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001724frame_destroy(struct frame *frame)
1725{
Martin Minarik1998b152012-05-10 02:04:35 +02001726 struct frame_button *button, *tmp;
1727
1728 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1729 frame_button_destroy(button);
1730
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001731 /* frame->child must be destroyed by the application */
1732 widget_destroy(frame->widget);
1733 free(frame);
1734}
1735
1736static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001737input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001738 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001739{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001740 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001741 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001742
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001743 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001744 return;
1745
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001746 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001747 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001748 widget = old;
1749 if (input->grab)
1750 widget = input->grab;
1751 if (widget->leave_handler)
1752 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001753 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001754 }
1755
1756 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001757 widget = focus;
1758 if (input->grab)
1759 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001760 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001761 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001762 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001763 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001764
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001765 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001766 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001767}
1768
1769static void
Daniel Stone37816df2012-05-16 18:45:18 +01001770pointer_handle_motion(void *data, struct wl_pointer *pointer,
1771 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001772{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001773 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001774 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001775 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001776 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001777 float sx = wl_fixed_to_double(sx_w);
1778 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001779
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001780 input->sx = sx;
1781 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001782
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001783 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001784 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001785 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001786 }
1787
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001788 if (input->grab)
1789 widget = input->grab;
1790 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001791 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001792 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001793 cursor = widget->motion_handler(input->focus_widget,
1794 input, time, sx, sy,
1795 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001796
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001797 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001798}
1799
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001800void
1801input_grab(struct input *input, struct widget *widget, uint32_t button)
1802{
1803 input->grab = widget;
1804 input->grab_button = button;
1805}
1806
1807void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001808input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001809{
1810 struct widget *widget;
1811
1812 input->grab = NULL;
1813 if (input->pointer_focus) {
1814 widget = widget_find_widget(input->pointer_focus->widget,
1815 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001816 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001817 }
1818}
1819
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001820static void
Daniel Stone37816df2012-05-16 18:45:18 +01001821pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001822 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001823{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001824 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001825 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001826 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001827
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001828 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001829 if (input->focus_widget && input->grab == NULL &&
1830 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001831 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001832
Neil Roberts6b28aad2012-01-23 19:11:18 +00001833 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001834 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001835 (*widget->button_handler)(widget,
1836 input, time,
1837 button, state,
1838 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001839
Daniel Stone4dbadb12012-05-30 16:31:51 +01001840 if (input->grab && input->grab_button == button &&
1841 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001842 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001843}
1844
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001845static void
Daniel Stone37816df2012-05-16 18:45:18 +01001846pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001847 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001848{
1849}
1850
1851static void
Daniel Stone37816df2012-05-16 18:45:18 +01001852keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001853 uint32_t serial, uint32_t time, uint32_t key,
1854 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001855{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001856 struct input *input = data;
1857 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001858 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001859 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001860 const xkb_keysym_t *syms;
1861 xkb_keysym_t sym;
1862 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001863 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001864
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001865 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001866 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001867 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001868 return;
1869
Daniel Stone97f68542012-05-30 16:32:01 +01001870 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001871
Daniel Stone97f68542012-05-30 16:32:01 +01001872 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001873 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001874 XKB_STATE_LATCHED);
1875 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001876 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001877 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001878 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001879 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001880 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001881 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001882
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001883 sym = XKB_KEY_NoSymbol;
1884 if (num_syms == 1)
1885 sym = syms[0];
1886
1887 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001888 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001889 window_set_maximized(window,
1890 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001891 } else if (sym == XKB_KEY_F11 &&
1892 window->fullscreen_handler &&
1893 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1894 window->fullscreen_handler(window, window->user_data);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001895 } else if (window->key_handler) {
1896 (*window->key_handler)(window, input, time, key,
1897 sym, state, window->user_data);
1898 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001899
1900 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
1901 key == input->repeat_key) {
1902 its.it_interval.tv_sec = 0;
1903 its.it_interval.tv_nsec = 0;
1904 its.it_value.tv_sec = 0;
1905 its.it_value.tv_nsec = 0;
1906 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1907 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1908 input->repeat_sym = sym;
1909 input->repeat_key = key;
1910 input->repeat_time = time;
1911 its.it_interval.tv_sec = 0;
1912 its.it_interval.tv_nsec = 25 * 1000 * 1000;
1913 its.it_value.tv_sec = 0;
1914 its.it_value.tv_nsec = 400 * 1000 * 1000;
1915 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1916 }
1917}
1918
1919static void
1920keyboard_repeat_func(struct task *task, uint32_t events)
1921{
1922 struct input *input =
1923 container_of(task, struct input, repeat_task);
1924 struct window *window = input->keyboard_focus;
1925 uint64_t exp;
1926
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04001927 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1928 /* If we change the timer between the fd becoming
1929 * readable and getting here, there'll be nothing to
1930 * read and we get EAGAIN. */
1931 return;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001932
Kristian Høgsbergbd0cd762012-06-20 15:17:18 -04001933 if (window && window->key_handler) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001934 (*window->key_handler)(window, input, input->repeat_time,
1935 input->repeat_key, input->repeat_sym,
1936 WL_KEYBOARD_KEY_STATE_PRESSED,
1937 window->user_data);
1938 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001939}
1940
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001941static void
Daniel Stone351eb612012-05-31 15:27:47 -04001942keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1943 uint32_t serial, uint32_t mods_depressed,
1944 uint32_t mods_latched, uint32_t mods_locked,
1945 uint32_t group)
1946{
1947 struct input *input = data;
1948
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001949 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1950 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001951}
1952
1953static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001954input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001955{
1956 struct window *window = input->pointer_focus;
1957
1958 if (!window)
1959 return;
1960
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001961 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001962
Pekka Paalanene1207c72011-12-16 12:02:09 +02001963 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001964 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001965}
1966
1967static void
Daniel Stone37816df2012-05-16 18:45:18 +01001968pointer_handle_enter(void *data, struct wl_pointer *pointer,
1969 uint32_t serial, struct wl_surface *surface,
1970 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001971{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001972 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001973 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001974 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001975 float sx = wl_fixed_to_double(sx_w);
1976 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001977
Martin Minarik1998b152012-05-10 02:04:35 +02001978 if (!surface) {
1979 /* enter event for a window we've just destroyed */
1980 return;
1981 }
1982
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001983 input->display->serial = serial;
1984 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001985 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001986 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001987
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001988 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001989 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001990 window->pool = NULL;
1991 /* Schedule a redraw to free the pool */
1992 window_schedule_redraw(window);
1993 }
1994
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001995 input->sx = sx;
1996 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001997
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001998 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001999 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002000}
Kristian Høgsberg59826582011-01-20 11:56:57 -05002001
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002002static void
Daniel Stone37816df2012-05-16 18:45:18 +01002003pointer_handle_leave(void *data, struct wl_pointer *pointer,
2004 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002005{
2006 struct input *input = data;
2007
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002008 input->display->serial = serial;
2009 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002010}
2011
2012static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002013input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02002014{
2015 struct window *window = input->keyboard_focus;
Kristian Høgsbergd3c69c22012-06-20 22:41:59 -04002016 struct itimerspec its;
2017
2018 its.it_interval.tv_sec = 0;
2019 its.it_interval.tv_nsec = 0;
2020 its.it_value.tv_sec = 0;
2021 its.it_value.tv_nsec = 0;
2022 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002023
2024 if (!window)
2025 return;
2026
2027 window->keyboard_device = NULL;
2028 if (window->keyboard_focus_handler)
2029 (*window->keyboard_focus_handler)(window, NULL,
2030 window->user_data);
2031
2032 input->keyboard_focus = NULL;
2033}
2034
2035static void
Daniel Stone37816df2012-05-16 18:45:18 +01002036keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2037 uint32_t serial, struct wl_surface *surface,
2038 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002039{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002040 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02002041 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002042
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002043 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002044 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002045
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002046 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002047 window->keyboard_device = input;
2048 if (window->keyboard_focus_handler)
2049 (*window->keyboard_focus_handler)(window,
2050 window->keyboard_device,
2051 window->user_data);
2052}
2053
2054static void
Daniel Stone37816df2012-05-16 18:45:18 +01002055keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2056 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002057{
2058 struct input *input = data;
2059
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002060 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002061 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002062}
2063
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002064static void
2065keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2066 uint32_t format, int fd, uint32_t size)
2067{
2068 struct input *input = data;
2069 char *map_str;
2070
2071 if (!data) {
2072 close(fd);
2073 return;
2074 }
2075
2076 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2077 close(fd);
2078 return;
2079 }
2080
2081 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2082 if (map_str == MAP_FAILED) {
2083 close(fd);
2084 return;
2085 }
2086
2087 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2088 map_str,
2089 XKB_KEYMAP_FORMAT_TEXT_V1,
2090 0);
2091 munmap(map_str, size);
2092 close(fd);
2093
2094 if (!input->xkb.keymap) {
2095 fprintf(stderr, "failed to compile keymap\n");
2096 return;
2097 }
2098
2099 input->xkb.state = xkb_state_new(input->xkb.keymap);
2100 if (!input->xkb.state) {
2101 fprintf(stderr, "failed to create XKB state\n");
2102 xkb_map_unref(input->xkb.keymap);
2103 input->xkb.keymap = NULL;
2104 return;
2105 }
2106
2107 input->xkb.control_mask =
2108 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2109 input->xkb.alt_mask =
2110 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2111 input->xkb.shift_mask =
2112 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2113}
2114
Daniel Stone37816df2012-05-16 18:45:18 +01002115static const struct wl_pointer_listener pointer_listener = {
2116 pointer_handle_enter,
2117 pointer_handle_leave,
2118 pointer_handle_motion,
2119 pointer_handle_button,
2120 pointer_handle_axis,
2121};
2122
2123static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002124 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002125 keyboard_handle_enter,
2126 keyboard_handle_leave,
2127 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002128 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002129};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002130
2131static void
Daniel Stone37816df2012-05-16 18:45:18 +01002132seat_handle_capabilities(void *data, struct wl_seat *seat,
2133 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002134{
Daniel Stone37816df2012-05-16 18:45:18 +01002135 struct input *input = data;
2136
2137 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2138 input->pointer = wl_seat_get_pointer(seat);
2139 wl_pointer_set_user_data(input->pointer, input);
2140 wl_pointer_add_listener(input->pointer, &pointer_listener,
2141 input);
2142 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2143 wl_pointer_destroy(input->pointer);
2144 input->pointer = NULL;
2145 }
2146
2147 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2148 input->keyboard = wl_seat_get_keyboard(seat);
2149 wl_keyboard_set_user_data(input->keyboard, input);
2150 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2151 input);
2152 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2153 wl_keyboard_destroy(input->keyboard);
2154 input->keyboard = NULL;
2155 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002156}
2157
Daniel Stone37816df2012-05-16 18:45:18 +01002158static const struct wl_seat_listener seat_listener = {
2159 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002160};
2161
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002162void
2163input_get_position(struct input *input, int32_t *x, int32_t *y)
2164{
2165 *x = input->sx;
2166 *y = input->sy;
2167}
2168
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002169struct display *
2170input_get_display(struct input *input)
2171{
2172 return input->display;
2173}
2174
Daniel Stone37816df2012-05-16 18:45:18 +01002175struct wl_seat *
2176input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002177{
Daniel Stone37816df2012-05-16 18:45:18 +01002178 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002179}
2180
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002181uint32_t
2182input_get_modifiers(struct input *input)
2183{
2184 return input->modifiers;
2185}
2186
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002187struct widget *
2188input_get_focus_widget(struct input *input)
2189{
2190 return input->focus_widget;
2191}
2192
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002193struct data_offer {
2194 struct wl_data_offer *offer;
2195 struct input *input;
2196 struct wl_array types;
2197 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002198
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002199 struct task io_task;
2200 int fd;
2201 data_func_t func;
2202 int32_t x, y;
2203 void *user_data;
2204};
2205
2206static void
2207data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2208{
2209 struct data_offer *offer = data;
2210 char **p;
2211
2212 p = wl_array_add(&offer->types, sizeof *p);
2213 *p = strdup(type);
2214}
2215
2216static const struct wl_data_offer_listener data_offer_listener = {
2217 data_offer_offer,
2218};
2219
2220static void
2221data_offer_destroy(struct data_offer *offer)
2222{
2223 char **p;
2224
2225 offer->refcount--;
2226 if (offer->refcount == 0) {
2227 wl_data_offer_destroy(offer->offer);
2228 for (p = offer->types.data; *p; p++)
2229 free(*p);
2230 wl_array_release(&offer->types);
2231 free(offer);
2232 }
2233}
2234
2235static void
2236data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002237 struct wl_data_device *data_device,
2238 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002239{
2240 struct data_offer *offer;
2241
2242 offer = malloc(sizeof *offer);
2243
2244 wl_array_init(&offer->types);
2245 offer->refcount = 1;
2246 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002247 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002248 wl_data_offer_add_listener(offer->offer,
2249 &data_offer_listener, offer);
2250}
2251
2252static void
2253data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002254 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002255 wl_fixed_t x_w, wl_fixed_t y_w,
2256 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002257{
2258 struct input *input = data;
2259 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002260 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002261 float x = wl_fixed_to_double(x_w);
2262 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002263 char **p;
2264
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002265 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002266 window = wl_surface_get_user_data(surface);
2267 input->pointer_focus = window;
2268
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002269 if (offer) {
2270 input->drag_offer = wl_data_offer_get_user_data(offer);
2271
2272 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2273 *p = NULL;
2274
2275 types_data = input->drag_offer->types.data;
2276 } else {
2277 input->drag_offer = NULL;
2278 types_data = NULL;
2279 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002280
2281 window = input->pointer_focus;
2282 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002283 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002284 window->user_data);
2285}
2286
2287static void
2288data_device_leave(void *data, struct wl_data_device *data_device)
2289{
2290 struct input *input = data;
2291
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002292 if (input->drag_offer) {
2293 data_offer_destroy(input->drag_offer);
2294 input->drag_offer = NULL;
2295 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002296}
2297
2298static void
2299data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002300 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002301{
2302 struct input *input = data;
2303 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002304 float x = wl_fixed_to_double(x_w);
2305 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002306 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002307
2308 input->sx = x;
2309 input->sy = y;
2310
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002311 if (input->drag_offer)
2312 types_data = input->drag_offer->types.data;
2313 else
2314 types_data = NULL;
2315
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002316 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002317 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002318 window->user_data);
2319}
2320
2321static void
2322data_device_drop(void *data, struct wl_data_device *data_device)
2323{
2324 struct input *input = data;
2325 struct window *window = input->pointer_focus;
2326
2327 if (window->drop_handler)
2328 window->drop_handler(window, input,
2329 input->sx, input->sy, window->user_data);
2330}
2331
2332static void
2333data_device_selection(void *data,
2334 struct wl_data_device *wl_data_device,
2335 struct wl_data_offer *offer)
2336{
2337 struct input *input = data;
2338 char **p;
2339
2340 if (input->selection_offer)
2341 data_offer_destroy(input->selection_offer);
2342
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002343 if (offer) {
2344 input->selection_offer = wl_data_offer_get_user_data(offer);
2345 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2346 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002347 } else {
2348 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002349 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002350}
2351
2352static const struct wl_data_device_listener data_device_listener = {
2353 data_device_data_offer,
2354 data_device_enter,
2355 data_device_leave,
2356 data_device_motion,
2357 data_device_drop,
2358 data_device_selection
2359};
2360
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002361static void
2362input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002363{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002364 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002365 struct wl_cursor *cursor;
2366 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002367
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002368 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002369 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002370 return;
2371
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002372 if (index >= (int) cursor->image_count) {
2373 fprintf(stderr, "cursor index out of range\n");
2374 return;
2375 }
2376
2377 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002378 buffer = wl_cursor_image_get_buffer(image);
2379 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002380 return;
2381
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002382 wl_pointer_set_cursor(input->pointer, input->display->serial,
2383 input->pointer_surface,
2384 image->hotspot_x, image->hotspot_y);
2385 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2386 wl_surface_damage(input->pointer_surface, 0, 0,
2387 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002388}
2389
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002390static const struct wl_callback_listener pointer_surface_listener;
2391
2392static void
2393pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2394 uint32_t time)
2395{
2396 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002397 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002398 int i;
2399
2400 if (callback) {
2401 assert(callback == input->cursor_frame_cb);
2402 wl_callback_destroy(callback);
2403 input->cursor_frame_cb = NULL;
2404 }
2405
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002406 if (input->current_cursor == CURSOR_BLANK) {
2407 wl_pointer_set_cursor(input->pointer, input->display->serial,
2408 NULL, 0, 0);
2409 return;
2410 }
2411
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002412 if (input->current_cursor == CURSOR_UNSET)
2413 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002414 cursor = input->display->cursors[input->current_cursor];
2415 if (!cursor)
2416 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002417
2418 /* FIXME We don't have the current time on the first call so we set
2419 * the animation start to the time of the first frame callback. */
2420 if (time == 0)
2421 input->cursor_anim_start = 0;
2422 else if (input->cursor_anim_start == 0)
2423 input->cursor_anim_start = time;
2424
2425 if (time == 0 || input->cursor_anim_start == 0)
2426 i = 0;
2427 else
2428 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2429
2430 input_set_pointer_image_index(input, i);
2431
2432 if (cursor->image_count == 1)
2433 return;
2434
2435 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2436 wl_callback_add_listener(input->cursor_frame_cb,
2437 &pointer_surface_listener, input);
2438}
2439
2440static const struct wl_callback_listener pointer_surface_listener = {
2441 pointer_surface_frame_callback
2442};
2443
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002444void
2445input_set_pointer_image(struct input *input, int pointer)
2446{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002447 int force = 0;
2448
2449 if (input->pointer_enter_serial > input->cursor_serial)
2450 force = 1;
2451
2452 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002453 return;
2454
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002455 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002456 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002457 if (!input->cursor_frame_cb)
2458 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002459 else if (force) {
2460 /* The current frame callback may be stuck if, for instance,
2461 * the set cursor request was processed by the server after
2462 * this client lost the focus. In this case the cursor surface
2463 * might not be mapped and the frame callback wouldn't ever
2464 * complete. Send a set_cursor and attach to try to map the
2465 * cursor surface again so that the callback will finish */
2466 input_set_pointer_image_index(input, 0);
2467 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002468}
2469
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002470struct wl_data_device *
2471input_get_data_device(struct input *input)
2472{
2473 return input->data_device;
2474}
2475
2476void
2477input_set_selection(struct input *input,
2478 struct wl_data_source *source, uint32_t time)
2479{
2480 wl_data_device_set_selection(input->data_device, source, time);
2481}
2482
2483void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002484input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002485{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002486 wl_data_offer_accept(input->drag_offer->offer,
2487 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002488}
2489
2490static void
2491offer_io_func(struct task *task, uint32_t events)
2492{
2493 struct data_offer *offer =
2494 container_of(task, struct data_offer, io_task);
2495 unsigned int len;
2496 char buffer[4096];
2497
2498 len = read(offer->fd, buffer, sizeof buffer);
2499 offer->func(buffer, len,
2500 offer->x, offer->y, offer->user_data);
2501
2502 if (len == 0) {
2503 close(offer->fd);
2504 data_offer_destroy(offer);
2505 }
2506}
2507
2508static void
2509data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2510 data_func_t func, void *user_data)
2511{
2512 int p[2];
2513
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002514 if (pipe2(p, O_CLOEXEC) == -1)
2515 return;
2516
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002517 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2518 close(p[1]);
2519
2520 offer->io_task.run = offer_io_func;
2521 offer->fd = p[0];
2522 offer->func = func;
2523 offer->refcount++;
2524 offer->user_data = user_data;
2525
2526 display_watch_fd(offer->input->display,
2527 offer->fd, EPOLLIN, &offer->io_task);
2528}
2529
2530void
2531input_receive_drag_data(struct input *input, const char *mime_type,
2532 data_func_t func, void *data)
2533{
2534 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2535 input->drag_offer->x = input->sx;
2536 input->drag_offer->y = input->sy;
2537}
2538
2539int
2540input_receive_selection_data(struct input *input, const char *mime_type,
2541 data_func_t func, void *data)
2542{
2543 char **p;
2544
2545 if (input->selection_offer == NULL)
2546 return -1;
2547
2548 for (p = input->selection_offer->types.data; *p; p++)
2549 if (strcmp(mime_type, *p) == 0)
2550 break;
2551
2552 if (*p == NULL)
2553 return -1;
2554
2555 data_offer_receive_data(input->selection_offer,
2556 mime_type, func, data);
2557 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002558}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002559
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002560int
2561input_receive_selection_data_to_fd(struct input *input,
2562 const char *mime_type, int fd)
2563{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002564 if (input->selection_offer)
2565 wl_data_offer_receive(input->selection_offer->offer,
2566 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002567
2568 return 0;
2569}
2570
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002571void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002572window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002573{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002574 if (!window->shell_surface)
2575 return;
2576
Daniel Stone37816df2012-05-16 18:45:18 +01002577 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002578}
2579
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002580static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002581idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002582{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002583 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002584
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002585 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002586 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002587 widget_set_allocation(widget,
2588 window->pending_allocation.x,
2589 window->pending_allocation.y,
2590 window->pending_allocation.width,
2591 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002592
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002593 if (window->input_region) {
2594 wl_region_destroy(window->input_region);
2595 window->input_region = NULL;
2596 }
2597
2598 if (window->opaque_region) {
2599 wl_region_destroy(window->opaque_region);
2600 window->opaque_region = NULL;
2601 }
2602
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002603 if (widget->resize_handler)
2604 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002605 widget->allocation.width,
2606 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002607 widget->user_data);
2608
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002609 if (window->allocation.width != widget->allocation.width ||
2610 window->allocation.height != widget->allocation.height) {
2611 window->allocation = widget->allocation;
2612 window_schedule_redraw(window);
2613 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002614}
2615
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002616void
2617window_schedule_resize(struct window *window, int width, int height)
2618{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002619 window->pending_allocation.x = 0;
2620 window->pending_allocation.y = 0;
2621 window->pending_allocation.width = width;
2622 window->pending_allocation.height = height;
2623
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002624 if (window->min_allocation.width == 0)
2625 window->min_allocation = window->pending_allocation;
2626 if (window->pending_allocation.width < window->min_allocation.width)
2627 window->pending_allocation.width = window->min_allocation.width;
2628 if (window->pending_allocation.height < window->min_allocation.height)
2629 window->pending_allocation.height = window->min_allocation.height;
2630
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002631 window->resize_needed = 1;
2632 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002633}
2634
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002635void
2636widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2637{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002638 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002639}
2640
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002641static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002642handle_ping(void *data, struct wl_shell_surface *shell_surface,
2643 uint32_t serial)
2644{
2645 wl_shell_surface_pong(shell_surface, serial);
2646}
2647
2648static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002649handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002650 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002651{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002652 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002653
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002654 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002655 return;
2656
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002657 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002658 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002659}
2660
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002661static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002662menu_destroy(struct menu *menu)
2663{
2664 widget_destroy(menu->widget);
2665 window_destroy(menu->window);
2666 free(menu);
2667}
2668
2669static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002670handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2671{
2672 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002673 struct menu *menu = window->widget->user_data;
2674
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002675 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002676 * device. Or just use wl_callback. And this really needs to
2677 * be a window vfunc that the menu can set. And we need the
2678 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002679
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002680 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002681 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002682 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002683}
2684
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002685static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002686 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002687 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002688 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002689};
2690
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002691void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002692window_get_allocation(struct window *window,
2693 struct rectangle *allocation)
2694{
2695 *allocation = window->allocation;
2696}
2697
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002698static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002699widget_redraw(struct widget *widget)
2700{
2701 struct widget *child;
2702
2703 if (widget->redraw_handler)
2704 widget->redraw_handler(widget, widget->user_data);
2705 wl_list_for_each(child, &widget->child_list, link)
2706 widget_redraw(child);
2707}
2708
2709static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002710frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2711{
2712 struct window *window = data;
2713
2714 wl_callback_destroy(callback);
2715 window->redraw_scheduled = 0;
2716 if (window->redraw_needed)
2717 window_schedule_redraw(window);
2718}
2719
2720static const struct wl_callback_listener listener = {
2721 frame_callback
2722};
2723
2724static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002725idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002726{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002727 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002728 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002729
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002730 if (window->resize_needed)
2731 idle_resize(window);
2732
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002733 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002734 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002735 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002736 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002737 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002738
2739 callback = wl_surface_frame(window->surface);
2740 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002741}
2742
2743void
2744window_schedule_redraw(struct window *window)
2745{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002746 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002747 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002748 window->redraw_task.run = idle_redraw;
2749 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002750 window->redraw_scheduled = 1;
2751 }
2752}
2753
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002754void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002755window_set_fullscreen(struct window *window, int fullscreen)
2756{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002757 if (!window->display->shell)
2758 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002759
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002760 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002761 return;
2762
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002763 if (fullscreen) {
2764 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002765 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002766 wl_shell_surface_set_fullscreen(window->shell_surface,
2767 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2768 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002769 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002770 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002771 wl_shell_surface_set_toplevel(window->shell_surface);
2772 window_schedule_resize(window,
2773 window->saved_allocation.width,
2774 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002775 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002776}
2777
2778void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002779window_set_maximized(struct window *window, int maximized)
2780{
2781 if (!window->display->shell)
2782 return;
2783
2784 if ((window->type == TYPE_MAXIMIZED) == maximized)
2785 return;
2786
2787 if (window->type == TYPE_TOPLEVEL) {
2788 window->saved_allocation = window->allocation;
2789 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2790 window->type = TYPE_MAXIMIZED;
2791 } else {
2792 wl_shell_surface_set_toplevel(window->shell_surface);
2793 window->type = TYPE_TOPLEVEL;
2794 window_schedule_resize(window,
2795 window->saved_allocation.width,
2796 window->saved_allocation.height);
2797 }
2798}
2799
2800void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002801window_set_user_data(struct window *window, void *data)
2802{
2803 window->user_data = data;
2804}
2805
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002806void *
2807window_get_user_data(struct window *window)
2808{
2809 return window->user_data;
2810}
2811
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002812void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002813window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002814 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002815{
2816 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002817}
2818
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002819void
2820window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002821 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002822{
2823 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002824}
2825
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002826void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002827window_set_data_handler(struct window *window, window_data_handler_t handler)
2828{
2829 window->data_handler = handler;
2830}
2831
2832void
2833window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2834{
2835 window->drop_handler = handler;
2836}
2837
2838void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002839window_set_close_handler(struct window *window,
2840 window_close_handler_t handler)
2841{
2842 window->close_handler = handler;
2843}
2844
2845void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002846window_set_fullscreen_handler(struct window *window,
2847 window_fullscreen_handler_t handler)
2848{
2849 window->fullscreen_handler = handler;
2850}
2851
2852void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002853window_set_title(struct window *window, const char *title)
2854{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002855 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002856 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002857 if (window->shell_surface)
2858 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002859}
2860
2861const char *
2862window_get_title(struct window *window)
2863{
2864 return window->title;
2865}
2866
2867void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002868window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2869{
2870 struct text_cursor_position *text_cursor_position =
2871 window->display->text_cursor_position;
2872
Scott Moreau9295ce02012-06-01 12:46:10 -06002873 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002874 return;
2875
2876 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002877 window->surface,
2878 wl_fixed_from_int(x),
2879 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002880}
2881
2882void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002883window_damage(struct window *window, int32_t x, int32_t y,
2884 int32_t width, int32_t height)
2885{
2886 wl_surface_damage(window->surface, x, y, width, height);
2887}
2888
Casey Dahlin9074db52012-04-19 22:50:09 -04002889static void
2890surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002891 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002892{
Rob Bradford7507b572012-05-15 17:55:34 +01002893 struct window *window = data;
2894 struct output *output;
2895 struct output *output_found = NULL;
2896 struct window_output *window_output;
2897
2898 wl_list_for_each(output, &window->display->output_list, link) {
2899 if (output->output == wl_output) {
2900 output_found = output;
2901 break;
2902 }
2903 }
2904
2905 if (!output_found)
2906 return;
2907
2908 window_output = malloc (sizeof *window_output);
2909 window_output->output = output_found;
2910
2911 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002912}
2913
2914static void
2915surface_leave(void *data,
2916 struct wl_surface *wl_surface, struct wl_output *output)
2917{
Rob Bradford7507b572012-05-15 17:55:34 +01002918 struct window *window = data;
2919 struct window_output *window_output;
2920 struct window_output *window_output_found = NULL;
2921
2922 wl_list_for_each(window_output, &window->window_output_list, link) {
2923 if (window_output->output->output == output) {
2924 window_output_found = window_output;
2925 break;
2926 }
2927 }
2928
2929 if (window_output_found) {
2930 wl_list_remove(&window_output_found->link);
2931 free(window_output_found);
2932 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002933}
2934
2935static const struct wl_surface_listener surface_listener = {
2936 surface_enter,
2937 surface_leave
2938};
2939
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002940static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002941window_create_internal(struct display *display,
2942 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002943{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002944 struct window *window;
2945
2946 window = malloc(sizeof *window);
2947 if (window == NULL)
2948 return NULL;
2949
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002950 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002951 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002952 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002953 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002954 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002955 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002956 window->shell_surface =
2957 wl_shell_get_shell_surface(display->shell,
2958 window->surface);
2959 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002960 window->allocation.x = 0;
2961 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002962 window->allocation.width = 0;
2963 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002964 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002965 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002966 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002967 window->input_region = NULL;
2968 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002969
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002970 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002971#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002972 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002973#else
2974 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2975#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002976 else
2977 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002978
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002979 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002980 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002981 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002982
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002983 if (window->shell_surface) {
2984 wl_shell_surface_set_user_data(window->shell_surface, window);
2985 wl_shell_surface_add_listener(window->shell_surface,
2986 &shell_surface_listener, window);
2987 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002988
Rob Bradford7507b572012-05-15 17:55:34 +01002989 wl_list_init (&window->window_output_list);
2990
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002991 return window;
2992}
2993
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002994struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002995window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002996{
2997 struct window *window;
2998
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002999 window = window_create_internal(display, NULL, TYPE_NONE);
3000 if (!window)
3001 return NULL;
3002
3003 return window;
3004}
3005
3006struct window *
3007window_create_custom(struct display *display)
3008{
3009 struct window *window;
3010
3011 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003012 if (!window)
3013 return NULL;
3014
3015 return window;
3016}
3017
3018struct window *
3019window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003020 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003021{
3022 struct window *window;
3023
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003024 window = window_create_internal(parent->display,
3025 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003026 if (!window)
3027 return NULL;
3028
3029 window->x = x;
3030 window->y = y;
3031
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003032 if (display->shell)
3033 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003034 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003035 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003036
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003037 return window;
3038}
3039
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003040static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003041menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003042{
3043 int next;
3044
3045 next = (sy - 8) / 20;
3046 if (menu->current != next) {
3047 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003048 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003049 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003050}
3051
3052static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003053menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003054 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003055 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003056{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003057 struct menu *menu = data;
3058
3059 if (widget == menu->widget)
3060 menu_set_item(data, y);
3061
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003062 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003063}
3064
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003065static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003066menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003067 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003068{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003069 struct menu *menu = data;
3070
3071 if (widget == menu->widget)
3072 menu_set_item(data, y);
3073
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003074 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003075}
3076
3077static void
3078menu_leave_handler(struct widget *widget, struct input *input, void *data)
3079{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003080 struct menu *menu = data;
3081
3082 if (widget == menu->widget)
3083 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003084}
3085
3086static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003087menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003088 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003089 uint32_t button, enum wl_pointer_button_state state,
3090 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003091
3092{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003093 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003094
Daniel Stone4dbadb12012-05-30 16:31:51 +01003095 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3096 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003097 /* Either relase after press-drag-release or
3098 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003099 menu->func(menu->window->parent,
3100 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003101 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003102 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003103 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003104}
3105
3106static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003107menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003108{
3109 cairo_t *cr;
3110 const int32_t r = 3, margin = 3;
3111 struct menu *menu = data;
3112 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003113 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003114
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003115 cr = cairo_create(window->cairo_surface);
3116 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3117 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3118 cairo_paint(cr);
3119
3120 width = window->allocation.width;
3121 height = window->allocation.height;
3122 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003123
3124 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003125 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3126 cairo_fill(cr);
3127
3128 for (i = 0; i < menu->count; i++) {
3129 if (i == menu->current) {
3130 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3131 cairo_rectangle(cr, margin, i * 20 + margin,
3132 width - 2 * margin, 20);
3133 cairo_fill(cr);
3134 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3135 cairo_move_to(cr, 10, i * 20 + 16);
3136 cairo_show_text(cr, menu->entries[i]);
3137 } else {
3138 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3139 cairo_move_to(cr, 10, i * 20 + 16);
3140 cairo_show_text(cr, menu->entries[i]);
3141 }
3142 }
3143
3144 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003145}
3146
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003147void
3148window_show_menu(struct display *display,
3149 struct input *input, uint32_t time, struct window *parent,
3150 int32_t x, int32_t y,
3151 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003152{
3153 struct window *window;
3154 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003155 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003156
3157 menu = malloc(sizeof *menu);
3158 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003159 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003160
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003161 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003162 if (!window) {
3163 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003164 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003165 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003166
3167 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003168 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003169 menu->entries = entries;
3170 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003171 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003172 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003173 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003174 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003175 window->type = TYPE_MENU;
3176 window->x = x;
3177 window->y = y;
3178
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003179 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003180 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003181 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003182 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003183 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003184
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003185 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003186 widget_set_enter_handler(menu->widget, menu_enter_handler);
3187 widget_set_leave_handler(menu->widget, menu_leave_handler);
3188 widget_set_motion_handler(menu->widget, menu_motion_handler);
3189 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003190
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003191 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003192 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003193}
3194
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003195void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003196window_set_buffer_type(struct window *window, enum window_buffer_type type)
3197{
3198 window->buffer_type = type;
3199}
3200
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003201
3202static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003203display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003204 struct wl_output *wl_output,
3205 int x, int y,
3206 int physical_width,
3207 int physical_height,
3208 int subpixel,
3209 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003210 const char *model,
3211 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003212{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003213 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003214
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003215 output->allocation.x = x;
3216 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003217}
3218
3219static void
3220display_handle_mode(void *data,
3221 struct wl_output *wl_output,
3222 uint32_t flags,
3223 int width,
3224 int height,
3225 int refresh)
3226{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003227 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003228 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003229
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003230 if (flags & WL_OUTPUT_MODE_CURRENT) {
3231 output->allocation.width = width;
3232 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003233 if (display->output_configure_handler)
3234 (*display->output_configure_handler)(
3235 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003236 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003237}
3238
3239static const struct wl_output_listener output_listener = {
3240 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003241 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003242};
3243
3244static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003245display_add_output(struct display *d, uint32_t id)
3246{
3247 struct output *output;
3248
3249 output = malloc(sizeof *output);
3250 if (output == NULL)
3251 return;
3252
3253 memset(output, 0, sizeof *output);
3254 output->display = d;
3255 output->output =
3256 wl_display_bind(d->display, id, &wl_output_interface);
3257 wl_list_insert(d->output_list.prev, &output->link);
3258
3259 wl_output_add_listener(output->output, &output_listener, output);
3260}
3261
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003262static void
3263output_destroy(struct output *output)
3264{
3265 if (output->destroy_handler)
3266 (*output->destroy_handler)(output, output->user_data);
3267
3268 wl_output_destroy(output->output);
3269 wl_list_remove(&output->link);
3270 free(output);
3271}
3272
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003273void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003274display_set_output_configure_handler(struct display *display,
3275 display_output_handler_t handler)
3276{
3277 struct output *output;
3278
3279 display->output_configure_handler = handler;
3280 if (!handler)
3281 return;
3282
3283 wl_list_for_each(output, &display->output_list, link)
3284 (*display->output_configure_handler)(output,
3285 display->user_data);
3286}
3287
3288void
3289output_set_user_data(struct output *output, void *data)
3290{
3291 output->user_data = data;
3292}
3293
3294void *
3295output_get_user_data(struct output *output)
3296{
3297 return output->user_data;
3298}
3299
3300void
3301output_set_destroy_handler(struct output *output,
3302 display_output_handler_t handler)
3303{
3304 output->destroy_handler = handler;
3305 /* FIXME: implement this, once we have way to remove outputs */
3306}
3307
3308void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003309output_get_allocation(struct output *output, struct rectangle *allocation)
3310{
3311 *allocation = output->allocation;
3312}
3313
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003314struct wl_output *
3315output_get_wl_output(struct output *output)
3316{
3317 return output->output;
3318}
3319
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003320static void
Daniel Stone97f68542012-05-30 16:32:01 +01003321fini_xkb(struct input *input)
3322{
3323 xkb_state_unref(input->xkb.state);
3324 xkb_map_unref(input->xkb.keymap);
3325}
3326
3327static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003328display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003329{
3330 struct input *input;
3331
3332 input = malloc(sizeof *input);
3333 if (input == NULL)
3334 return;
3335
3336 memset(input, 0, sizeof *input);
3337 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003338 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003339 input->pointer_focus = NULL;
3340 input->keyboard_focus = NULL;
3341 wl_list_insert(d->input_list.prev, &input->link);
3342
Daniel Stone37816df2012-05-16 18:45:18 +01003343 wl_seat_add_listener(input->seat, &seat_listener, input);
3344 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003345
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003346 input->data_device =
3347 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003348 input->seat);
3349 wl_data_device_add_listener(input->data_device, &data_device_listener,
3350 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003351
3352 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003353
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003354 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3355 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003356 input->repeat_task.run = keyboard_repeat_func;
3357 display_watch_fd(d, input->repeat_timer_fd,
3358 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003359}
3360
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003361static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003362input_destroy(struct input *input)
3363{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003364 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003365 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003366
3367 if (input->drag_offer)
3368 data_offer_destroy(input->drag_offer);
3369
3370 if (input->selection_offer)
3371 data_offer_destroy(input->selection_offer);
3372
3373 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003374 fini_xkb(input);
3375
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003376 wl_surface_destroy(input->pointer_surface);
3377
Pekka Paalanene1207c72011-12-16 12:02:09 +02003378 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003379 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003380 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003381 free(input);
3382}
3383
3384static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003385display_handle_global(struct wl_display *display, uint32_t id,
3386 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003387{
3388 struct display *d = data;
3389
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003390 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003391 d->compositor =
3392 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003393 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003394 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003395 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003396 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003397 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003398 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003399 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003400 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003401 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3402 d->data_device_manager =
3403 wl_display_bind(display, id,
3404 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003405 } else if (strcmp(interface, "text_cursor_position") == 0) {
3406 d->text_cursor_position =
3407 wl_display_bind(display, id,
3408 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003409 }
3410}
3411
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003412#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003413static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003414init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003415{
3416 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003417 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003418
Rob Clark6396ed32012-03-11 19:48:41 -05003419#ifdef USE_CAIRO_GLESV2
3420# define GL_BIT EGL_OPENGL_ES2_BIT
3421#else
3422# define GL_BIT EGL_OPENGL_BIT
3423#endif
3424
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003425 static const EGLint argb_cfg_attribs[] = {
3426 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003427 EGL_RED_SIZE, 1,
3428 EGL_GREEN_SIZE, 1,
3429 EGL_BLUE_SIZE, 1,
3430 EGL_ALPHA_SIZE, 1,
3431 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003432 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003433 EGL_NONE
3434 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003435
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003436#ifdef USE_CAIRO_GLESV2
3437 static const EGLint context_attribs[] = {
3438 EGL_CONTEXT_CLIENT_VERSION, 2,
3439 EGL_NONE
3440 };
3441 EGLint api = EGL_OPENGL_ES_API;
3442#else
3443 EGLint *context_attribs = NULL;
3444 EGLint api = EGL_OPENGL_API;
3445#endif
3446
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003447 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003448 if (!eglInitialize(d->dpy, &major, &minor)) {
3449 fprintf(stderr, "failed to initialize display\n");
3450 return -1;
3451 }
3452
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003453 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003454 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3455 return -1;
3456 }
3457
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003458 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3459 &d->argb_config, 1, &n) || n != 1) {
3460 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003461 return -1;
3462 }
3463
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003464 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003465 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003466 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003467 fprintf(stderr, "failed to create context\n");
3468 return -1;
3469 }
3470
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003471 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003472 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003473 return -1;
3474 }
3475
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003476#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003477 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3478 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3479 fprintf(stderr, "failed to get cairo egl argb device\n");
3480 return -1;
3481 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003482#endif
3483
3484 return 0;
3485}
3486
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003487static void
3488fini_egl(struct display *display)
3489{
3490#ifdef HAVE_CAIRO_EGL
3491 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003492#endif
3493
3494 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3495 EGL_NO_CONTEXT);
3496
3497 eglTerminate(display->dpy);
3498 eglReleaseThread();
3499}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003500#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003501
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003502static int
3503event_mask_update(uint32_t mask, void *data)
3504{
3505 struct display *d = data;
3506
3507 d->mask = mask;
3508
3509 return 0;
3510}
3511
3512static void
3513handle_display_data(struct task *task, uint32_t events)
3514{
3515 struct display *display =
3516 container_of(task, struct display, display_task);
3517
3518 wl_display_iterate(display->display, display->mask);
3519}
3520
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003521struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003522display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003523{
3524 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003525
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003526 argc = parse_options(xkb_options,
3527 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003528
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003529 d = malloc(sizeof *d);
3530 if (d == NULL)
3531 return NULL;
3532
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003533 memset(d, 0, sizeof *d);
3534
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003535 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003536 if (d->display == NULL) {
3537 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003538 return NULL;
3539 }
3540
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003541 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003542 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3543 d->display_task.run = handle_display_data;
3544 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3545
3546 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003547 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003548 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003549
Daniel Stone97f68542012-05-30 16:32:01 +01003550 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003551 if (d->xkb_context == NULL) {
3552 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003553 return NULL;
3554 }
3555
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003556 /* Set up listener so we'll catch all events. */
3557 wl_display_add_global_listener(d->display,
3558 display_handle_global, d);
3559
3560 /* Process connection events. */
3561 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003562#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003563 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003564 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003565#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003566
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003567 d->image_target_texture_2d =
3568 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3569 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3570 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3571
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003572 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003573
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003574 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003575
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003576 wl_list_init(&d->window_list);
3577
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003578 return d;
3579}
3580
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003581static void
3582display_destroy_outputs(struct display *display)
3583{
3584 struct output *tmp;
3585 struct output *output;
3586
3587 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3588 output_destroy(output);
3589}
3590
Pekka Paalanene1207c72011-12-16 12:02:09 +02003591static void
3592display_destroy_inputs(struct display *display)
3593{
3594 struct input *tmp;
3595 struct input *input;
3596
3597 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3598 input_destroy(input);
3599}
3600
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003601void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003602display_destroy(struct display *display)
3603{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003604 if (!wl_list_empty(&display->window_list))
3605 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3606
3607 if (!wl_list_empty(&display->deferred_list))
3608 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3609
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003610 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003611 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003612
Daniel Stone97f68542012-05-30 16:32:01 +01003613 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003614
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003615 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003616 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003617
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003618#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003619 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003620#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003621
Pekka Paalanenc2052982011-12-16 11:41:32 +02003622 if (display->shell)
3623 wl_shell_destroy(display->shell);
3624
3625 if (display->shm)
3626 wl_shm_destroy(display->shm);
3627
3628 if (display->data_device_manager)
3629 wl_data_device_manager_destroy(display->data_device_manager);
3630
3631 wl_compositor_destroy(display->compositor);
3632
3633 close(display->epoll_fd);
3634
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003635 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003636 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003637 free(display);
3638}
3639
3640void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003641display_set_user_data(struct display *display, void *data)
3642{
3643 display->user_data = data;
3644}
3645
3646void *
3647display_get_user_data(struct display *display)
3648{
3649 return display->user_data;
3650}
3651
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003652struct wl_display *
3653display_get_display(struct display *display)
3654{
3655 return display->display;
3656}
3657
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003658struct output *
3659display_get_output(struct display *display)
3660{
3661 return container_of(display->output_list.next, struct output, link);
3662}
3663
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003664struct wl_compositor *
3665display_get_compositor(struct display *display)
3666{
3667 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003668}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003669
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003670uint32_t
3671display_get_serial(struct display *display)
3672{
3673 return display->serial;
3674}
3675
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003676EGLDisplay
3677display_get_egl_display(struct display *d)
3678{
3679 return d->dpy;
3680}
3681
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003682struct wl_data_source *
3683display_create_data_source(struct display *display)
3684{
3685 return wl_data_device_manager_create_data_source(display->data_device_manager);
3686}
3687
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003688EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003689display_get_argb_egl_config(struct display *d)
3690{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003691 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003692}
3693
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003694struct wl_shell *
3695display_get_shell(struct display *display)
3696{
3697 return display->shell;
3698}
3699
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003700int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003701display_acquire_window_surface(struct display *display,
3702 struct window *window,
3703 EGLContext ctx)
3704{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003705#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003706 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003707 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003708
3709 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003710 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003711 device = cairo_surface_get_device(window->cairo_surface);
3712 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003713 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003714
Benjamin Franzke0c991632011-09-27 21:57:31 +02003715 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003716 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003717 ctx = display->argb_ctx;
3718 else
3719 assert(0);
3720 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003721
3722 data = cairo_surface_get_user_data(window->cairo_surface,
3723 &surface_data_key);
3724
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003725 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003726 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003727 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3728 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003729
3730 return 0;
3731#else
3732 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003733#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003734}
3735
3736void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003737display_release_window_surface(struct display *display,
3738 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003739{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003740#ifdef HAVE_CAIRO_EGL
3741 cairo_device_t *device;
3742
3743 device = cairo_surface_get_device(window->cairo_surface);
3744 if (!device)
3745 return;
3746
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003747 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003748 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003749 cairo_device_release(device);
3750#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003751}
3752
3753void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003754display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003755{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003756 wl_list_insert(&display->deferred_list, &task->link);
3757}
3758
3759void
3760display_watch_fd(struct display *display,
3761 int fd, uint32_t events, struct task *task)
3762{
3763 struct epoll_event ep;
3764
3765 ep.events = events;
3766 ep.data.ptr = task;
3767 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3768}
3769
3770void
3771display_run(struct display *display)
3772{
3773 struct task *task;
3774 struct epoll_event ep[16];
3775 int i, count;
3776
Pekka Paalanen826d7952011-12-15 10:14:07 +02003777 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003778 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003779 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003780
3781 while (!wl_list_empty(&display->deferred_list)) {
3782 task = container_of(display->deferred_list.next,
3783 struct task, link);
3784 wl_list_remove(&task->link);
3785 task->run(task, 0);
3786 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003787
3788 if (!display->running)
3789 break;
3790
3791 wl_display_flush(display->display);
3792
3793 count = epoll_wait(display->epoll_fd,
3794 ep, ARRAY_LENGTH(ep), -1);
3795 for (i = 0; i < count; i++) {
3796 task = ep[i].data.ptr;
3797 task->run(task, ep[i].events);
3798 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003799 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003800}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003801
3802void
3803display_exit(struct display *display)
3804{
3805 display->running = 0;
3806}