blob: 9e9b209920fc5a282f405c2bb8f814573f7ce150 [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"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070067struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030068
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050069struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050070 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050071 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040072 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040073 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040074 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040075 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050076 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020077 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020078 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040079 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040080
81 int display_fd;
82 uint32_t mask;
83 struct task display_task;
84
85 int epoll_fd;
86 struct wl_list deferred_list;
87
Pekka Paalanen826d7952011-12-15 10:14:07 +020088 int running;
89
Kristian Høgsberg478d9262010-06-08 20:34:11 -040090 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040091 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050092 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040093
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040094 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040095
96 struct {
97 struct xkb_rule_names names;
98 struct xkb_keymap *keymap;
99 struct xkb_state *state;
100 struct xkb_context *context;
101 xkb_mod_mask_t control_mask;
102 xkb_mod_mask_t alt_mask;
103 xkb_mod_mask_t shift_mask;
104 } xkb;
105
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300106 struct wl_cursor_theme *cursor_theme;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400107
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500108 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
109 PFNEGLCREATEIMAGEKHRPROC create_image;
110 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200111
112 display_output_handler_t output_configure_handler;
113
114 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500115};
116
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400117enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400118 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400119 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400120 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500121 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400122 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500123 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400124 TYPE_CUSTOM
125};
Rob Bradford7507b572012-05-15 17:55:34 +0100126
127struct window_output {
128 struct output *output;
129 struct wl_list link;
130};
131
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500132struct window {
133 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500134 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100135 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500136 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200137 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500138 struct wl_region *input_region;
139 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500140 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500141 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500142 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500143 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400144 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400145 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400146 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400147 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400148 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400149 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400150 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400151 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400152 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500153
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400154 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500155
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700156 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400157
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500158 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500159 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400160 window_data_handler_t data_handler;
161 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500162 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400163
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200164 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500165 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500166
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500167 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400168 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500169};
170
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500171struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500172 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300173 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500174 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400175 struct wl_list link;
176 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500177 widget_resize_handler_t resize_handler;
178 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500179 widget_enter_handler_t enter_handler;
180 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500181 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500182 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400183 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500184 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300185 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400186};
187
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400188struct input {
189 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100190 struct wl_seat *seat;
191 struct wl_pointer *pointer;
192 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 struct window *pointer_focus;
194 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300195 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400196 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400197 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400198 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400199 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400200
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500201 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500202 struct widget *grab;
203 uint32_t grab_button;
204
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400205 struct wl_data_device *data_device;
206 struct data_offer *drag_offer;
207 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400208};
209
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500210struct output {
211 struct display *display;
212 struct wl_output *output;
213 struct rectangle allocation;
214 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200215
216 display_output_handler_t destroy_handler;
217 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500218};
219
Martin Minarik1998b152012-05-10 02:04:35 +0200220enum frame_button_action {
221 FRAME_BUTTON_NULL = 0,
222 FRAME_BUTTON_ICON = 1,
223 FRAME_BUTTON_CLOSE = 2,
224 FRAME_BUTTON_MINIMIZE = 3,
225 FRAME_BUTTON_MAXIMIZE = 4,
226};
227
228enum frame_button_pointer {
229 FRAME_BUTTON_DEFAULT = 0,
230 FRAME_BUTTON_OVER = 1,
231 FRAME_BUTTON_ACTIVE = 2,
232};
233
234enum frame_button_align {
235 FRAME_BUTTON_RIGHT = 0,
236 FRAME_BUTTON_LEFT = 1,
237};
238
239enum frame_button_decoration {
240 FRAME_BUTTON_NONE = 0,
241 FRAME_BUTTON_FANCY = 1,
242};
243
244struct frame_button {
245 struct widget *widget;
246 struct frame *frame;
247 cairo_surface_t *icon;
248 enum frame_button_action type;
249 enum frame_button_pointer state;
250 struct wl_list link; /* buttons_list */
251 enum frame_button_align align;
252 enum frame_button_decoration decoration;
253};
254
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500255struct frame {
256 struct widget *widget;
257 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200258 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500259};
260
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500261struct menu {
262 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500263 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500264 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500265 const char **entries;
266 uint32_t time;
267 int current;
268 int count;
269 menu_func_t func;
270};
271
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300272struct tooltip {
273 struct widget *parent;
274 struct window *window;
275 struct widget *widget;
276 char *entry;
277 struct task tooltip_task;
278 int tooltip_fd;
279 float x, y;
280};
281
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700282struct shm_pool {
283 struct wl_shm_pool *pool;
284 size_t size;
285 size_t used;
286 void *data;
287};
288
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400289enum {
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300290 WL_CURSOR_DEFAULT = 100,
291 WL_CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400292};
293
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500294enum window_location {
295 WINDOW_INTERIOR = 0,
296 WINDOW_RESIZING_TOP = 1,
297 WINDOW_RESIZING_BOTTOM = 2,
298 WINDOW_RESIZING_LEFT = 4,
299 WINDOW_RESIZING_TOP_LEFT = 5,
300 WINDOW_RESIZING_BOTTOM_LEFT = 6,
301 WINDOW_RESIZING_RIGHT = 8,
302 WINDOW_RESIZING_TOP_RIGHT = 9,
303 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
304 WINDOW_RESIZING_MASK = 15,
305 WINDOW_EXTERIOR = 16,
306 WINDOW_TITLEBAR = 17,
307 WINDOW_CLIENT_AREA = 18,
308};
309
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400310const char *option_xkb_layout = "us";
311const char *option_xkb_variant = "";
312const char *option_xkb_options = "";
313
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400314static const struct weston_option xkb_options[] = {
315 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
316 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
317 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400318};
319
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320static const cairo_user_data_key_t surface_data_key;
321struct surface_data {
322 struct wl_buffer *buffer;
323};
324
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500325#define MULT(_d,c,a,t) \
326 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
327
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500328#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100330struct egl_window_surface_data {
331 struct display *display;
332 struct wl_surface *surface;
333 struct wl_egl_window *window;
334 EGLSurface surf;
335};
336
337static void
338egl_window_surface_data_destroy(void *p)
339{
340 struct egl_window_surface_data *data = p;
341 struct display *d = data->display;
342
343 eglDestroySurface(d->dpy, data->surf);
344 wl_egl_window_destroy(data->window);
345 data->surface = NULL;
346
347 free(p);
348}
349
350static cairo_surface_t *
351display_create_egl_window_surface(struct display *display,
352 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400353 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100354 struct rectangle *rectangle)
355{
356 cairo_surface_t *cairo_surface;
357 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400358 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200359 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100360
361 data = malloc(sizeof *data);
362 if (data == NULL)
363 return NULL;
364
365 data->display = display;
366 data->surface = surface;
367
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500368 config = display->argb_config;
369 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400370
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400371 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100372 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400373 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100374
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400375 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500376 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100377
Benjamin Franzke0c991632011-09-27 21:57:31 +0200378 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100379 data->surf,
380 rectangle->width,
381 rectangle->height);
382
383 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
384 data, egl_window_surface_data_destroy);
385
386 return cairo_surface;
387}
388
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400389#endif
390
391struct wl_buffer *
392display_get_buffer_for_surface(struct display *display,
393 cairo_surface_t *surface)
394{
395 struct surface_data *data;
396
397 data = cairo_surface_get_user_data (surface, &surface_data_key);
398
399 return data->buffer;
400}
401
402struct shm_surface_data {
403 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700404 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405};
406
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500407static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700408shm_pool_destroy(struct shm_pool *pool);
409
410static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411shm_surface_data_destroy(void *p)
412{
413 struct shm_surface_data *data = p;
414
415 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700416 if (data->pool)
417 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418}
419
Kristian Høgsberg16626282012-04-03 11:21:27 -0400420static struct wl_shm_pool *
421make_shm_pool(struct display *display, int size, void **data)
422{
423 char filename[] = "/tmp/wayland-shm-XXXXXX";
424 struct wl_shm_pool *pool;
425 int fd;
426
427 fd = mkstemp(filename);
428 if (fd < 0) {
429 fprintf(stderr, "open %s failed: %m\n", filename);
430 return NULL;
431 }
432 if (ftruncate(fd, size) < 0) {
433 fprintf(stderr, "ftruncate failed: %m\n");
434 close(fd);
435 return NULL;
436 }
437
438 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
439 unlink(filename);
440
441 if (*data == MAP_FAILED) {
442 fprintf(stderr, "mmap failed: %m\n");
443 close(fd);
444 return NULL;
445 }
446
447 pool = wl_shm_create_pool(display->shm, fd, size);
448
449 close(fd);
450
451 return pool;
452}
453
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700454static struct shm_pool *
455shm_pool_create(struct display *display, size_t size)
456{
457 struct shm_pool *pool = malloc(sizeof *pool);
458
459 if (!pool)
460 return NULL;
461
462 pool->pool = make_shm_pool(display, size, &pool->data);
463 if (!pool->pool) {
464 free(pool);
465 return NULL;
466 }
467
468 pool->size = size;
469 pool->used = 0;
470
471 return pool;
472}
473
474static void *
475shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
476{
477 if (pool->used + size > pool->size)
478 return NULL;
479
480 *offset = pool->used;
481 pool->used += size;
482
483 return (char *) pool->data + *offset;
484}
485
486/* destroy the pool. this does not unmap the memory though */
487static void
488shm_pool_destroy(struct shm_pool *pool)
489{
490 munmap(pool->data, pool->size);
491 wl_shm_pool_destroy(pool->pool);
492 free(pool);
493}
494
495/* Start allocating from the beginning of the pool again */
496static void
497shm_pool_reset(struct shm_pool *pool)
498{
499 pool->used = 0;
500}
501
502static int
503data_length_for_shm_surface(struct rectangle *rect)
504{
505 int stride;
506
507 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
508 rect->width);
509 return stride * rect->height;
510}
511
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500512static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700513display_create_shm_surface_from_pool(struct display *display,
514 struct rectangle *rectangle,
515 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516{
517 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400518 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400519 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700520 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400521 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400522
523 data = malloc(sizeof *data);
524 if (data == NULL)
525 return NULL;
526
527 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
528 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700529 length = stride * rectangle->height;
530 data->pool = NULL;
531 map = shm_pool_allocate(pool, length, &offset);
532
533 if (!map) {
534 free(data);
535 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400536 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400537
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400538 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539 CAIRO_FORMAT_ARGB32,
540 rectangle->width,
541 rectangle->height,
542 stride);
543
544 cairo_surface_set_user_data (surface, &surface_data_key,
545 data, shm_surface_data_destroy);
546
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500548 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500550 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700552 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400553 rectangle->width,
554 rectangle->height,
555 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700557 return surface;
558}
559
560static cairo_surface_t *
561display_create_shm_surface(struct display *display,
562 struct rectangle *rectangle, uint32_t flags,
563 struct window *window)
564{
565 struct shm_surface_data *data;
566 struct shm_pool *pool;
567 cairo_surface_t *surface;
568
569 if (window && window->pool) {
570 shm_pool_reset(window->pool);
571 surface = display_create_shm_surface_from_pool(display,
572 rectangle,
573 flags,
574 window->pool);
575 if (surface)
576 return surface;
577 }
578
579 pool = shm_pool_create(display,
580 data_length_for_shm_surface(rectangle));
581 if (!pool)
582 return NULL;
583
584 surface =
585 display_create_shm_surface_from_pool(display, rectangle,
586 flags, pool);
587
588 if (!surface) {
589 shm_pool_destroy(pool);
590 return NULL;
591 }
592
593 /* make sure we destroy the pool when the surface is destroyed */
594 data = cairo_surface_get_user_data(surface, &surface_data_key);
595 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596
597 return surface;
598}
599
nobled7b87cb02011-02-01 18:51:47 +0000600static int
601check_size(struct rectangle *rect)
602{
603 if (rect->width && rect->height)
604 return 0;
605
606 fprintf(stderr, "tried to create surface of "
607 "width: %d, height: %d\n", rect->width, rect->height);
608 return -1;
609}
610
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400611cairo_surface_t *
612display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100613 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400614 struct rectangle *rectangle,
615 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616{
nobled7b87cb02011-02-01 18:51:47 +0000617 if (check_size(rectangle) < 0)
618 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500619#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400620 if (display->dpy)
621 return display_create_egl_window_surface(display,
622 surface,
623 flags,
624 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400625#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400626 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627}
628
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300629struct wl_cursor_image *
630display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400631{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300632 struct wl_cursor *cursor =
633 wl_cursor_theme_get_cursor(display->cursor_theme, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400634
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300635 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400636}
637
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400638static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200639window_get_resize_dx_dy(struct window *window, int *x, int *y)
640{
641 if (window->resize_edges & WINDOW_RESIZING_LEFT)
642 *x = window->server_allocation.width - window->allocation.width;
643 else
644 *x = 0;
645
646 if (window->resize_edges & WINDOW_RESIZING_TOP)
647 *y = window->server_allocation.height -
648 window->allocation.height;
649 else
650 *y = 0;
651
652 window->resize_edges = 0;
653}
654
655static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500656window_attach_surface(struct window *window)
657{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400658 struct display *display = window->display;
659 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000660#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100661 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000662#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500663 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100664
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400665 if (window->type == TYPE_NONE) {
666 window->type = TYPE_TOPLEVEL;
667 if (display->shell)
668 wl_shell_surface_set_toplevel(window->shell_surface);
669 }
670
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100671 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000672#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100673 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
674 data = cairo_surface_get_user_data(window->cairo_surface,
675 &surface_data_key);
676
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100677 cairo_gl_surface_swapbuffers(window->cairo_surface);
678 wl_egl_window_get_attached_size(data->window,
679 &window->server_allocation.width,
680 &window->server_allocation.height);
681 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000682#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100683 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100684 buffer =
685 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400686 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100687
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200688 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100689 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400690 wl_surface_damage(window->surface, 0, 0,
691 window->allocation.width,
692 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100693 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400694 cairo_surface_destroy(window->cairo_surface);
695 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100696 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000697 default:
698 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100699 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500700
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500701 if (window->input_region) {
702 wl_surface_set_input_region(window->surface,
703 window->input_region);
704 wl_region_destroy(window->input_region);
705 window->input_region = NULL;
706 }
707
708 if (window->opaque_region) {
709 wl_surface_set_opaque_region(window->surface,
710 window->opaque_region);
711 wl_region_destroy(window->opaque_region);
712 window->opaque_region = NULL;
713 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500714}
715
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500716void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400717window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500718{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400719 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100720 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500721}
722
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400723void
724window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400725{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500726 cairo_surface_reference(surface);
727
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400728 if (window->cairo_surface != NULL)
729 cairo_surface_destroy(window->cairo_surface);
730
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500731 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400732}
733
Benjamin Franzke22d54812011-07-16 19:50:32 +0000734#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735static void
736window_resize_cairo_window_surface(struct window *window)
737{
738 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200739 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740
741 data = cairo_surface_get_user_data(window->cairo_surface,
742 &surface_data_key);
743
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200744 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100745 wl_egl_window_resize(data->window,
746 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200747 window->allocation.height,
748 x,y);
749
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100750 cairo_gl_surface_set_size(window->cairo_surface,
751 window->allocation.width,
752 window->allocation.height);
753}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000754#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400756struct display *
757window_get_display(struct window *window)
758{
759 return window->display;
760}
761
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400762void
763window_create_surface(struct window *window)
764{
765 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400766 uint32_t flags = 0;
767
768 if (!window->transparent)
769 flags = SURFACE_OPAQUE;
770
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400771 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500772#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100773 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
774 if (window->cairo_surface) {
775 window_resize_cairo_window_surface(window);
776 return;
777 }
778 surface = display_create_surface(window->display,
779 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400780 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100781 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400782#endif
783 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400784 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400785 &window->allocation,
786 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400787 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800788 default:
789 surface = NULL;
790 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400791 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400792
793 window_set_surface(window, surface);
794 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400795}
796
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200797static void frame_destroy(struct frame *frame);
798
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400799void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500800window_destroy(struct window *window)
801{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200802 struct display *display = window->display;
803 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100804 struct window_output *window_output;
805 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200806
807 if (window->redraw_scheduled)
808 wl_list_remove(&window->redraw_task.link);
809
810 wl_list_for_each(input, &display->input_list, link) {
811 if (input->pointer_focus == window)
812 input->pointer_focus = NULL;
813 if (input->keyboard_focus == window)
814 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500815 if (input->focus_widget &&
816 input->focus_widget->window == window)
817 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200818 }
819
Rob Bradford7507b572012-05-15 17:55:34 +0100820 wl_list_for_each_safe(window_output, window_output_tmp,
821 &window->window_output_list, link) {
822 free (window_output);
823 }
824
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500825 if (window->input_region)
826 wl_region_destroy(window->input_region);
827 if (window->opaque_region)
828 wl_region_destroy(window->opaque_region);
829
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200830 if (window->frame)
831 frame_destroy(window->frame);
832
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200833 if (window->shell_surface)
834 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500835 wl_surface_destroy(window->surface);
836 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200837
838 if (window->cairo_surface != NULL)
839 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200840
841 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500842 free(window);
843}
844
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500845static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500846widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400847{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500848 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400849
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500850 wl_list_for_each(child, &widget->child_list, link) {
851 target = widget_find_widget(child, x, y);
852 if (target)
853 return target;
854 }
855
856 if (widget->allocation.x <= x &&
857 x < widget->allocation.x + widget->allocation.width &&
858 widget->allocation.y <= y &&
859 y < widget->allocation.y + widget->allocation.height) {
860 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400861 }
862
863 return NULL;
864}
865
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500866static struct widget *
867widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400868{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500869 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400870
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500871 widget = malloc(sizeof *widget);
872 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500873 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500874 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500875 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500876 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500877 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300878 widget->tooltip = NULL;
879 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500880
881 return widget;
882}
883
884struct widget *
885window_add_widget(struct window *window, void *data)
886{
887 window->widget = widget_create(window, data);
888 wl_list_init(&window->widget->link);
889
890 return window->widget;
891}
892
893struct widget *
894widget_add_widget(struct widget *parent, void *data)
895{
896 struct widget *widget;
897
898 widget = widget_create(parent->window, data);
899 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400900
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500901 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400902}
903
904void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500905widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400906{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200907 struct display *display = widget->window->display;
908 struct input *input;
909
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300910 if (widget->tooltip) {
911 free(widget->tooltip);
912 widget->tooltip = NULL;
913 }
914
Pekka Paalanene156fb62012-01-19 13:51:38 +0200915 wl_list_for_each(input, &display->input_list, link) {
916 if (input->focus_widget == widget)
917 input->focus_widget = NULL;
918 }
919
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500920 wl_list_remove(&widget->link);
921 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400922}
923
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400924void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500925widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400926{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500927 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400928}
929
930void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500931widget_set_size(struct widget *widget, int32_t width, int32_t height)
932{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500933 widget->allocation.width = width;
934 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500935}
936
937void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500938widget_set_allocation(struct widget *widget,
939 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400940{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500941 widget->allocation.x = x;
942 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200943 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400944}
945
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500946void
947widget_set_transparent(struct widget *widget, int transparent)
948{
949 widget->opaque = !transparent;
950}
951
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400952void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500953widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400954{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500955 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400956}
957
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500958void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500959widget_set_resize_handler(struct widget *widget,
960 widget_resize_handler_t handler)
961{
962 widget->resize_handler = handler;
963}
964
965void
966widget_set_redraw_handler(struct widget *widget,
967 widget_redraw_handler_t handler)
968{
969 widget->redraw_handler = handler;
970}
971
972void
Kristian Høgsbergee143232012-01-09 08:42:24 -0500973widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500974{
Kristian Høgsbergee143232012-01-09 08:42:24 -0500975 widget->enter_handler = handler;
976}
977
978void
979widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
980{
981 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500982}
983
984void
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500985widget_set_motion_handler(struct widget *widget,
986 widget_motion_handler_t handler)
987{
988 widget->motion_handler = handler;
989}
990
991void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500992widget_set_button_handler(struct widget *widget,
993 widget_button_handler_t handler)
994{
995 widget->button_handler = handler;
996}
997
998void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500999widget_schedule_redraw(struct widget *widget)
1000{
1001 window_schedule_redraw(widget->window);
1002}
1003
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001004cairo_surface_t *
1005window_get_surface(struct window *window)
1006{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001007 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001008}
1009
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001010struct wl_surface *
1011window_get_wl_surface(struct window *window)
1012{
1013 return window->surface;
1014}
1015
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001016struct wl_shell_surface *
1017window_get_wl_shell_surface(struct window *window)
1018{
1019 return window->shell_surface;
1020}
1021
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001022static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001023tooltip_redraw_handler(struct widget *widget, void *data)
1024{
1025 cairo_t *cr;
1026 const int32_t r = 3;
1027 struct tooltip *tooltip = data;
1028 int32_t width, height;
1029 struct window *window = widget->window;
1030
1031 cr = cairo_create(window->cairo_surface);
1032 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1033 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1034 cairo_paint(cr);
1035
1036 width = window->allocation.width;
1037 height = window->allocation.height;
1038 rounded_rect(cr, 0, 0, width, height, r);
1039
1040 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1041 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1042 cairo_fill(cr);
1043
1044 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1045 cairo_move_to(cr, 10, 16);
1046 cairo_show_text(cr, tooltip->entry);
1047 cairo_destroy(cr);
1048}
1049
1050static cairo_text_extents_t
1051get_text_extents(struct tooltip *tooltip)
1052{
1053 struct window *window;
1054 cairo_t *cr;
1055 cairo_text_extents_t extents;
1056
1057 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1058 * created yet */
1059 window = tooltip->widget->window->parent;
1060 cr = cairo_create(window->cairo_surface);
1061 cairo_text_extents(cr, tooltip->entry, &extents);
1062 cairo_destroy(cr);
1063
1064 return extents;
1065}
1066
1067static int
1068window_create_tooltip(struct tooltip *tooltip)
1069{
1070 struct widget *parent = tooltip->parent;
1071 struct display *display = parent->window->display;
1072 struct window *window;
1073 const int offset_y = 27;
1074 const int margin = 3;
1075 cairo_text_extents_t extents;
1076
1077 if (tooltip->widget)
1078 return 0;
1079
1080 window = window_create_transient(display, parent->window, tooltip->x,
1081 tooltip->y + offset_y,
1082 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1083 if (!window)
1084 return -1;
1085
1086 tooltip->window = window;
1087 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1088
1089 extents = get_text_extents(tooltip);
1090 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1091 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1092
1093 return 0;
1094}
1095
1096void
1097widget_destroy_tooltip(struct widget *parent)
1098{
1099 struct tooltip *tooltip = parent->tooltip;
1100
1101 parent->tooltip_count = 0;
1102 if (!tooltip)
1103 return;
1104
1105 if (tooltip->widget) {
1106 widget_destroy(tooltip->widget);
1107 window_destroy(tooltip->window);
1108 tooltip->widget = NULL;
1109 tooltip->window = NULL;
1110 }
1111
1112 close(tooltip->tooltip_fd);
1113 free(tooltip->entry);
1114 free(tooltip);
1115 parent->tooltip = NULL;
1116}
1117
1118static void
1119tooltip_func(struct task *task, uint32_t events)
1120{
1121 struct tooltip *tooltip =
1122 container_of(task, struct tooltip, tooltip_task);
1123 uint64_t exp;
1124
1125 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1126 window_create_tooltip(tooltip);
1127}
1128
1129#define TOOLTIP_TIMEOUT 500
1130static int
1131tooltip_timer_reset(struct tooltip *tooltip)
1132{
1133 struct itimerspec its;
1134
1135 its.it_interval.tv_sec = 0;
1136 its.it_interval.tv_nsec = 0;
1137 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1138 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1139 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1140 fprintf(stderr, "could not set timerfd\n: %m");
1141 return -1;
1142 }
1143
1144 return 0;
1145}
1146
1147int
1148widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1149{
1150 struct tooltip *tooltip = parent->tooltip;
1151
1152 parent->tooltip_count++;
1153 if (tooltip) {
1154 tooltip->x = x;
1155 tooltip->y = y;
1156 tooltip_timer_reset(tooltip);
1157 return 0;
1158 }
1159
1160 /* the handler might be triggered too fast via input device motion, so
1161 * we need this check here to make sure tooltip is fully initialized */
1162 if (parent->tooltip_count > 1)
1163 return 0;
1164
1165 tooltip = malloc(sizeof *tooltip);
1166 if (!tooltip)
1167 return -1;
1168
1169 parent->tooltip = tooltip;
1170 tooltip->parent = parent;
1171 tooltip->widget = NULL;
1172 tooltip->window = NULL;
1173 tooltip->x = x;
1174 tooltip->y = y;
1175 tooltip->entry = strdup(entry);
1176 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1177 if (tooltip->tooltip_fd < 0) {
1178 fprintf(stderr, "could not create timerfd\n: %m");
1179 return -1;
1180 }
1181
1182 tooltip->tooltip_task.run = tooltip_func;
1183 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1184 EPOLLIN, &tooltip->tooltip_task);
1185 tooltip_timer_reset(tooltip);
1186
1187 return 0;
1188}
1189
1190static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001191frame_resize_handler(struct widget *widget,
1192 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001193{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001194 struct frame *frame = data;
1195 struct widget *child = frame->child;
1196 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001197 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001198 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001199 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001200 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001201 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001202 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001203
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001204 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001205 decoration_width = (t->width + t->margin) * 2;
1206 decoration_height = t->width +
1207 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001208
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001209 allocation.x = t->width + t->margin;
1210 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001211 allocation.width = width - decoration_width;
1212 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001213
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001214 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001215
1216 wl_list_for_each(button, &frame->buttons_list, link)
1217 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001218 } else {
1219 decoration_width = 0;
1220 decoration_height = 0;
1221
1222 allocation.x = 0;
1223 allocation.y = 0;
1224 allocation.width = width;
1225 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001226 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001227
1228 wl_list_for_each(button, &frame->buttons_list, link)
1229 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001230 }
1231
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001232 widget_set_allocation(child, allocation.x, allocation.y,
1233 allocation.width, allocation.height);
1234
1235 if (child->resize_handler)
1236 child->resize_handler(child,
1237 allocation.width,
1238 allocation.height,
1239 child->user_data);
1240
Scott Moreauf7e498c2012-05-14 11:39:29 -06001241 width = child->allocation.width + decoration_width;
1242 height = child->allocation.height + decoration_height;
1243
1244 widget->window->input_region =
1245 wl_compositor_create_region(display->compositor);
1246 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001247 t->margin, t->margin,
1248 width - 2 * t->margin,
1249 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001250
1251 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001252
1253 if (child->opaque) {
1254 widget->window->opaque_region =
1255 wl_compositor_create_region(display->compositor);
1256 wl_region_add(widget->window->opaque_region,
1257 opaque_margin, opaque_margin,
1258 widget->allocation.width - 2 * opaque_margin,
1259 widget->allocation.height - 2 * opaque_margin);
1260 }
Martin Minarik1998b152012-05-10 02:04:35 +02001261
1262 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001263 x_r = frame->widget->allocation.width - t->width - t->margin;
1264 x_l = t->width + t->margin;
1265 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001266 wl_list_for_each(button, &frame->buttons_list, link) {
1267 const int button_padding = 4;
1268 w = cairo_image_surface_get_width(button->icon);
1269 h = cairo_image_surface_get_height(button->icon);
1270
1271 if (button->decoration == FRAME_BUTTON_FANCY)
1272 w += 10;
1273
1274 if (button->align == FRAME_BUTTON_LEFT) {
1275 widget_set_allocation(button->widget,
1276 x_l, y , w + 1, h + 1);
1277 x_l += w;
1278 x_l += button_padding;
1279 } else {
1280 x_r -= w;
1281 widget_set_allocation(button->widget,
1282 x_r, y , w + 1, h + 1);
1283 x_r -= button_padding;
1284 }
1285 }
1286}
1287
1288static int
1289frame_button_enter_handler(struct widget *widget,
1290 struct input *input, float x, float y, void *data)
1291{
1292 struct frame_button *frame_button = data;
1293
1294 widget_schedule_redraw(frame_button->widget);
1295 frame_button->state = FRAME_BUTTON_OVER;
1296
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001297 return WL_CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001298}
1299
1300static void
1301frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1302{
1303 struct frame_button *frame_button = data;
1304
1305 widget_schedule_redraw(frame_button->widget);
1306 frame_button->state = FRAME_BUTTON_DEFAULT;
1307}
1308
1309static void
1310frame_button_button_handler(struct widget *widget,
1311 struct input *input, uint32_t time,
1312 uint32_t button, uint32_t state, void *data)
1313{
1314 struct frame_button *frame_button = data;
1315 struct window *window = widget->window;
1316
1317 if (button != BTN_LEFT)
1318 return;
1319
1320 switch (state) {
1321 case 1:
1322 frame_button->state = FRAME_BUTTON_ACTIVE;
1323 widget_schedule_redraw(frame_button->widget);
1324
1325 if (frame_button->type == FRAME_BUTTON_ICON)
1326 window_show_frame_menu(window, input, time);
1327 return;
1328 case 0:
1329 frame_button->state = FRAME_BUTTON_DEFAULT;
1330 widget_schedule_redraw(frame_button->widget);
1331 break;
1332 }
1333
1334 switch (frame_button->type) {
1335 case FRAME_BUTTON_CLOSE:
1336 if (window->close_handler)
1337 window->close_handler(window->parent,
1338 window->user_data);
1339 else
1340 display_exit(window->display);
1341 break;
1342 case FRAME_BUTTON_MINIMIZE:
1343 fprintf(stderr,"Minimize stub\n");
1344 break;
1345 case FRAME_BUTTON_MAXIMIZE:
1346 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1347 break;
1348 default:
1349 /* Unknown operation */
1350 break;
1351 }
1352}
1353
1354static void
1355frame_button_redraw_handler(struct widget *widget, void *data)
1356{
1357 struct frame_button *frame_button = data;
1358 cairo_t *cr;
1359 int width, height, x, y;
1360 struct window *window = widget->window;
1361
1362 x = widget->allocation.x;
1363 y = widget->allocation.y;
1364 width = widget->allocation.width;
1365 height = widget->allocation.height;
1366
1367 if (!width)
1368 return;
1369 if (!height)
1370 return;
1371 if (widget->opaque)
1372 return;
1373
1374 cr = cairo_create(window->cairo_surface);
1375
1376 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1377 cairo_set_line_width(cr, 1);
1378
1379 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1380 cairo_rectangle (cr, x, y, 25, 16);
1381
1382 cairo_stroke_preserve(cr);
1383
1384 switch (frame_button->state) {
1385 case FRAME_BUTTON_DEFAULT:
1386 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1387 break;
1388 case FRAME_BUTTON_OVER:
1389 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1390 break;
1391 case FRAME_BUTTON_ACTIVE:
1392 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1393 break;
1394 }
1395
1396 cairo_fill (cr);
1397
1398 x += 4;
1399 }
1400
1401 cairo_set_source_surface(cr, frame_button->icon, x, y);
1402 cairo_paint(cr);
1403
1404 cairo_destroy(cr);
1405}
1406
1407static struct widget *
1408frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1409 enum frame_button_align align, enum frame_button_decoration style)
1410{
1411 struct frame_button *frame_button;
1412 const char *icon = data;
1413
1414 frame_button = malloc (sizeof *frame_button);
1415 memset(frame_button, 0, sizeof *frame_button);
1416
1417 frame_button->icon = cairo_image_surface_create_from_png(icon);
1418 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1419 frame_button->frame = frame;
1420 frame_button->type = type;
1421 frame_button->align = align;
1422 frame_button->decoration = style;
1423
1424 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1425
1426 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1427 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1428 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1429 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1430 return frame_button->widget;
1431}
1432
1433static void
1434frame_button_destroy(struct frame_button *frame_button)
1435{
1436 widget_destroy(frame_button->widget);
1437 wl_list_remove(&frame_button->link);
1438 cairo_surface_destroy(frame_button->icon);
1439 free(frame_button);
1440
1441 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001442}
1443
1444static void
1445frame_redraw_handler(struct widget *widget, void *data)
1446{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001447 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001448 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001449 struct theme *t = window->display->theme;
1450 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001451
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001452 if (window->type == TYPE_FULLSCREEN)
1453 return;
1454
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001455 cr = cairo_create(window->cairo_surface);
1456
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001457 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001458 flags |= THEME_FRAME_ACTIVE;
1459 theme_render_frame(t, cr, widget->allocation.width,
1460 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001461
1462 cairo_destroy(cr);
1463}
1464
1465static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001466frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001467{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001468 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001469 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001470
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001471 location = theme_get_location(t, input->sx, input->sy,
1472 frame->widget->allocation.width,
1473 frame->widget->allocation.height);
1474
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001475 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001476 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001477 return WL_CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001478 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001479 return WL_CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001480 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001481 return WL_CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001482 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001483 return WL_CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001484 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001485 return WL_CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001486 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001487 return WL_CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001488 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001489 return WL_CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001490 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001491 return WL_CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001492 case THEME_LOCATION_EXTERIOR:
1493 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001494 default:
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001495 return WL_CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001496 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001497}
1498
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499static void
1500frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001501{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001502 switch (index) {
1503 case 0: /* close */
1504 if (window->close_handler)
1505 window->close_handler(window->parent,
1506 window->user_data);
1507 else
1508 display_exit(window->display);
1509 break;
1510 case 1: /* fullscreen */
1511 /* we don't have a way to get out of fullscreen for now */
1512 window_set_fullscreen(window, 1);
1513 break;
1514 case 2: /* rotate */
1515 case 3: /* scale */
1516 break;
1517 }
1518}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001519
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001520void
1521window_show_frame_menu(struct window *window,
1522 struct input *input, uint32_t time)
1523{
1524 int32_t x, y;
1525
1526 static const char *entries[] = {
1527 "Close", "Fullscreen", "Rotate", "Scale"
1528 };
1529
1530 input_get_position(input, &x, &y);
1531 window_show_menu(window->display, input, time, window,
1532 x - 10, y - 10, frame_menu_func, entries, 4);
1533}
1534
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001535static int
1536frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001537 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001538{
1539 return frame_get_pointer_image_for_location(data, input);
1540}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001541
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001542static int
1543frame_motion_handler(struct widget *widget,
1544 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001545 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001546{
1547 return frame_get_pointer_image_for_location(data, input);
1548}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001549
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001550static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001551frame_button_handler(struct widget *widget,
1552 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001553 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001554
1555{
1556 struct frame *frame = data;
1557 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001558 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001559 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001560
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001561 location = theme_get_location(display->theme, input->sx, input->sy,
1562 frame->widget->allocation.width,
1563 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001564
1565 if (window->display->shell && button == BTN_LEFT && state == 1) {
1566 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001567 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001568 if (!window->shell_surface)
1569 break;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001570 input_set_pointer_image(input, time, WL_CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001571 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001572 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001573 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001574 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001575 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001576 case THEME_LOCATION_RESIZING_TOP:
1577 case THEME_LOCATION_RESIZING_BOTTOM:
1578 case THEME_LOCATION_RESIZING_LEFT:
1579 case THEME_LOCATION_RESIZING_RIGHT:
1580 case THEME_LOCATION_RESIZING_TOP_LEFT:
1581 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1582 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1583 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584 if (!window->shell_surface)
1585 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001586 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001587
1588 if (!display->dpy) {
1589 /* If we're using shm, allocate a big
1590 pool to create buffers out of while
1591 we resize. We should probably base
1592 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001593 window->pool =
1594 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001595 }
1596
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001597 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001598 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001599 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001600 break;
1601 }
1602 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001603 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001604 }
1605}
1606
1607struct widget *
1608frame_create(struct window *window, void *data)
1609{
1610 struct frame *frame;
1611
1612 frame = malloc(sizeof *frame);
1613 memset(frame, 0, sizeof *frame);
1614
1615 frame->widget = window_add_widget(window, frame);
1616 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001617
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001618 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1619 widget_set_resize_handler(frame->widget, frame_resize_handler);
1620 widget_set_enter_handler(frame->widget, frame_enter_handler);
1621 widget_set_motion_handler(frame->widget, frame_motion_handler);
1622 widget_set_button_handler(frame->widget, frame_button_handler);
1623
Martin Minarik1998b152012-05-10 02:04:35 +02001624 /* Create empty list for frame buttons */
1625 wl_list_init(&frame->buttons_list);
1626
1627 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1628 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1629
1630 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1631 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1632
1633 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1634 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1635
1636 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1637 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1638
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001639 window->frame = frame;
1640
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001641 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001642}
1643
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001644static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001645frame_destroy(struct frame *frame)
1646{
Martin Minarik1998b152012-05-10 02:04:35 +02001647 struct frame_button *button, *tmp;
1648
1649 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1650 frame_button_destroy(button);
1651
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001652 /* frame->child must be destroyed by the application */
1653 widget_destroy(frame->widget);
1654 free(frame);
1655}
1656
1657static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001658input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001659 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001660{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001661 struct widget *old, *widget;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001662 int pointer = WL_CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001664 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001665 return;
1666
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001667 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001668 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001669 widget = old;
1670 if (input->grab)
1671 widget = input->grab;
1672 if (widget->leave_handler)
1673 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001674 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001675 }
1676
1677 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001678 widget = focus;
1679 if (input->grab)
1680 widget = input->grab;
1681 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001682 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001683 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001684 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001685
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001686 input_set_pointer_image(input, input->pointer_enter_serial,
1687 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001688 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001689}
1690
1691static void
Daniel Stone37816df2012-05-16 18:45:18 +01001692pointer_handle_motion(void *data, struct wl_pointer *pointer,
1693 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001694{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001695 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001696 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001697 struct widget *widget;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001698 int cursor = WL_CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001699 float sx = wl_fixed_to_double(sx_w);
1700 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001701
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001702 input->sx = sx;
1703 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001704
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001705 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001706 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001707 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708 }
1709
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001710 if (input->grab)
1711 widget = input->grab;
1712 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001713 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001714 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001715 cursor = widget->motion_handler(input->focus_widget,
1716 input, time, sx, sy,
1717 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001718
Daniel Stone37816df2012-05-16 18:45:18 +01001719 input_set_pointer_image(input, time, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001720}
1721
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001722void
1723input_grab(struct input *input, struct widget *widget, uint32_t button)
1724{
1725 input->grab = widget;
1726 input->grab_button = button;
1727}
1728
1729void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001730input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001731{
1732 struct widget *widget;
1733
1734 input->grab = NULL;
1735 if (input->pointer_focus) {
1736 widget = widget_find_widget(input->pointer_focus->widget,
1737 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001738 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001739 }
1740}
1741
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001742static void
Daniel Stone37816df2012-05-16 18:45:18 +01001743pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1744 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001745{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001746 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001747 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001748
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001749 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001750 if (input->focus_widget && input->grab == NULL && state)
1751 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001752
Neil Roberts6b28aad2012-01-23 19:11:18 +00001753 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001754 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001755 (*widget->button_handler)(widget,
1756 input, time,
1757 button, state,
1758 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001759
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001760 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001761 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001762}
1763
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001764static void
Daniel Stone37816df2012-05-16 18:45:18 +01001765pointer_handle_axis(void *data, struct wl_pointer *pointer,
Scott Moreau210d0792012-03-22 10:47:01 -06001766 uint32_t time, uint32_t axis, int32_t value)
1767{
1768}
1769
1770static void
Daniel Stone37816df2012-05-16 18:45:18 +01001771keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1772 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001773{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001774 struct input *input = data;
1775 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001776 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001777 uint32_t code, num_syms;
1778 const xkb_keysym_t *syms;
1779 xkb_keysym_t sym;
1780 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001781
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001782 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001783 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001784 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001785 return;
1786
Kristian Høgsberg70163132012-05-08 15:55:39 -04001787 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1788 xkb_state_update_key(d->xkb.state, code,
1789 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001790
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001791 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001792 XKB_STATE_DEPRESSED |
1793 XKB_STATE_LATCHED);
1794 input->modifiers = 0;
1795 if (mask & input->display->xkb.control_mask)
1796 input->modifiers |= MOD_CONTROL_MASK;
1797 if (mask & input->display->xkb.alt_mask)
1798 input->modifiers |= MOD_ALT_MASK;
1799 if (mask & input->display->xkb.shift_mask)
1800 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001801
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001802 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001803 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001804 if (state)
1805 window_set_maximized(window,
1806 window->type != TYPE_MAXIMIZED);
1807 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001808 if (num_syms == 1)
1809 sym = syms[0];
1810 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001811 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001812
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001813 (*window->key_handler)(window, input, time, key,
1814 sym, state, window->user_data);
1815 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001816}
1817
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001818static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001819input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001820{
1821 struct window *window = input->pointer_focus;
1822
1823 if (!window)
1824 return;
1825
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001826 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001827
Pekka Paalanene1207c72011-12-16 12:02:09 +02001828 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001829 input->current_cursor = WL_CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001830}
1831
1832static void
Daniel Stone37816df2012-05-16 18:45:18 +01001833pointer_handle_enter(void *data, struct wl_pointer *pointer,
1834 uint32_t serial, struct wl_surface *surface,
1835 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001836{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001837 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001838 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001839 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001840 float sx = wl_fixed_to_double(sx_w);
1841 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001842
Martin Minarik1998b152012-05-10 02:04:35 +02001843 if (!surface) {
1844 /* enter event for a window we've just destroyed */
1845 return;
1846 }
1847
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001848 input->display->serial = serial;
1849 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001850 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001851 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001852
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001853 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001854 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001855 window->pool = NULL;
1856 /* Schedule a redraw to free the pool */
1857 window_schedule_redraw(window);
1858 }
1859
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001860 input->sx = sx;
1861 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001862
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001863 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001864 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001865}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001866
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001867static void
Daniel Stone37816df2012-05-16 18:45:18 +01001868pointer_handle_leave(void *data, struct wl_pointer *pointer,
1869 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001870{
1871 struct input *input = data;
1872
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001873 input->display->serial = serial;
1874 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001875}
1876
1877static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001878input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001879{
1880 struct window *window = input->keyboard_focus;
1881
1882 if (!window)
1883 return;
1884
1885 window->keyboard_device = NULL;
1886 if (window->keyboard_focus_handler)
1887 (*window->keyboard_focus_handler)(window, NULL,
1888 window->user_data);
1889
1890 input->keyboard_focus = NULL;
1891}
1892
1893static void
Daniel Stone37816df2012-05-16 18:45:18 +01001894keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1895 uint32_t serial, struct wl_surface *surface,
1896 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001897{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001898 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001899 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001900
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001901 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001902 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001903
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001904 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001905 window->keyboard_device = input;
1906 if (window->keyboard_focus_handler)
1907 (*window->keyboard_focus_handler)(window,
1908 window->keyboard_device,
1909 window->user_data);
1910}
1911
1912static void
Daniel Stone37816df2012-05-16 18:45:18 +01001913keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1914 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001915{
1916 struct input *input = data;
1917
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001918 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001919 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001920}
1921
Daniel Stone37816df2012-05-16 18:45:18 +01001922static const struct wl_pointer_listener pointer_listener = {
1923 pointer_handle_enter,
1924 pointer_handle_leave,
1925 pointer_handle_motion,
1926 pointer_handle_button,
1927 pointer_handle_axis,
1928};
1929
1930static const struct wl_keyboard_listener keyboard_listener = {
1931 keyboard_handle_enter,
1932 keyboard_handle_leave,
1933 keyboard_handle_key,
1934};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001935
1936static void
Daniel Stone37816df2012-05-16 18:45:18 +01001937seat_handle_capabilities(void *data, struct wl_seat *seat,
1938 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001939{
Daniel Stone37816df2012-05-16 18:45:18 +01001940 struct input *input = data;
1941
1942 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1943 input->pointer = wl_seat_get_pointer(seat);
1944 wl_pointer_set_user_data(input->pointer, input);
1945 wl_pointer_add_listener(input->pointer, &pointer_listener,
1946 input);
1947 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1948 wl_pointer_destroy(input->pointer);
1949 input->pointer = NULL;
1950 }
1951
1952 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1953 input->keyboard = wl_seat_get_keyboard(seat);
1954 wl_keyboard_set_user_data(input->keyboard, input);
1955 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
1956 input);
1957 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
1958 wl_keyboard_destroy(input->keyboard);
1959 input->keyboard = NULL;
1960 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001961}
1962
Daniel Stone37816df2012-05-16 18:45:18 +01001963static const struct wl_seat_listener seat_listener = {
1964 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001965};
1966
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001967void
1968input_get_position(struct input *input, int32_t *x, int32_t *y)
1969{
1970 *x = input->sx;
1971 *y = input->sy;
1972}
1973
Daniel Stone37816df2012-05-16 18:45:18 +01001974struct wl_seat *
1975input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001976{
Daniel Stone37816df2012-05-16 18:45:18 +01001977 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001978}
1979
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001980uint32_t
1981input_get_modifiers(struct input *input)
1982{
1983 return input->modifiers;
1984}
1985
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001986struct widget *
1987input_get_focus_widget(struct input *input)
1988{
1989 return input->focus_widget;
1990}
1991
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001992struct data_offer {
1993 struct wl_data_offer *offer;
1994 struct input *input;
1995 struct wl_array types;
1996 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001997
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001998 struct task io_task;
1999 int fd;
2000 data_func_t func;
2001 int32_t x, y;
2002 void *user_data;
2003};
2004
2005static void
2006data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2007{
2008 struct data_offer *offer = data;
2009 char **p;
2010
2011 p = wl_array_add(&offer->types, sizeof *p);
2012 *p = strdup(type);
2013}
2014
2015static const struct wl_data_offer_listener data_offer_listener = {
2016 data_offer_offer,
2017};
2018
2019static void
2020data_offer_destroy(struct data_offer *offer)
2021{
2022 char **p;
2023
2024 offer->refcount--;
2025 if (offer->refcount == 0) {
2026 wl_data_offer_destroy(offer->offer);
2027 for (p = offer->types.data; *p; p++)
2028 free(*p);
2029 wl_array_release(&offer->types);
2030 free(offer);
2031 }
2032}
2033
2034static void
2035data_device_data_offer(void *data,
2036 struct wl_data_device *data_device, uint32_t id)
2037{
2038 struct data_offer *offer;
2039
2040 offer = malloc(sizeof *offer);
2041
2042 wl_array_init(&offer->types);
2043 offer->refcount = 1;
2044 offer->input = data;
2045
2046 /* FIXME: Generate typesafe wrappers for this */
2047 offer->offer = (struct wl_data_offer *)
2048 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2049 id, &wl_data_offer_interface);
2050
2051 wl_data_offer_add_listener(offer->offer,
2052 &data_offer_listener, offer);
2053}
2054
2055static void
2056data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002057 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002058 wl_fixed_t x_w, wl_fixed_t y_w,
2059 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002060{
2061 struct input *input = data;
2062 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002063 float x = wl_fixed_to_double(x_w);
2064 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002065 char **p;
2066
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002067 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002068 input->drag_offer = wl_data_offer_get_user_data(offer);
2069 window = wl_surface_get_user_data(surface);
2070 input->pointer_focus = window;
2071
2072 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2073 *p = NULL;
2074
2075 window = input->pointer_focus;
2076 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002077 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002078 input->drag_offer->types.data,
2079 window->user_data);
2080}
2081
2082static void
2083data_device_leave(void *data, struct wl_data_device *data_device)
2084{
2085 struct input *input = data;
2086
2087 data_offer_destroy(input->drag_offer);
2088 input->drag_offer = NULL;
2089}
2090
2091static void
2092data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002093 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002094{
2095 struct input *input = data;
2096 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002097 float x = wl_fixed_to_double(x_w);
2098 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002099
2100 input->sx = x;
2101 input->sy = y;
2102
2103 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002104 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002105 input->drag_offer->types.data,
2106 window->user_data);
2107}
2108
2109static void
2110data_device_drop(void *data, struct wl_data_device *data_device)
2111{
2112 struct input *input = data;
2113 struct window *window = input->pointer_focus;
2114
2115 if (window->drop_handler)
2116 window->drop_handler(window, input,
2117 input->sx, input->sy, window->user_data);
2118}
2119
2120static void
2121data_device_selection(void *data,
2122 struct wl_data_device *wl_data_device,
2123 struct wl_data_offer *offer)
2124{
2125 struct input *input = data;
2126 char **p;
2127
2128 if (input->selection_offer)
2129 data_offer_destroy(input->selection_offer);
2130
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002131 if (offer) {
2132 input->selection_offer = wl_data_offer_get_user_data(offer);
2133 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2134 *p = NULL;
2135 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002136}
2137
2138static const struct wl_data_device_listener data_device_listener = {
2139 data_device_data_offer,
2140 data_device_enter,
2141 data_device_leave,
2142 data_device_motion,
2143 data_device_drop,
2144 data_device_selection
2145};
2146
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002147void
2148input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2149{
2150 struct display *display = input->display;
2151 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002152 struct wl_cursor *cursor;
2153 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002154
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002155 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002156 return;
2157
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002158 cursor = wl_cursor_theme_get_cursor(display->cursor_theme, pointer);
2159 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002160 return;
2161
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002162 image = cursor->images[0];
2163 buffer = wl_cursor_image_get_buffer(image);
2164 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002165 return;
2166
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002167 input->current_cursor = pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002168 wl_pointer_attach(input->pointer, time, buffer,
2169 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002170}
2171
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002172struct wl_data_device *
2173input_get_data_device(struct input *input)
2174{
2175 return input->data_device;
2176}
2177
2178void
2179input_set_selection(struct input *input,
2180 struct wl_data_source *source, uint32_t time)
2181{
2182 wl_data_device_set_selection(input->data_device, source, time);
2183}
2184
2185void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002186input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002187{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002188 wl_data_offer_accept(input->drag_offer->offer,
2189 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002190}
2191
2192static void
2193offer_io_func(struct task *task, uint32_t events)
2194{
2195 struct data_offer *offer =
2196 container_of(task, struct data_offer, io_task);
2197 unsigned int len;
2198 char buffer[4096];
2199
2200 len = read(offer->fd, buffer, sizeof buffer);
2201 offer->func(buffer, len,
2202 offer->x, offer->y, offer->user_data);
2203
2204 if (len == 0) {
2205 close(offer->fd);
2206 data_offer_destroy(offer);
2207 }
2208}
2209
2210static void
2211data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2212 data_func_t func, void *user_data)
2213{
2214 int p[2];
2215
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002216 if (pipe2(p, O_CLOEXEC) == -1)
2217 return;
2218
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002219 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2220 close(p[1]);
2221
2222 offer->io_task.run = offer_io_func;
2223 offer->fd = p[0];
2224 offer->func = func;
2225 offer->refcount++;
2226 offer->user_data = user_data;
2227
2228 display_watch_fd(offer->input->display,
2229 offer->fd, EPOLLIN, &offer->io_task);
2230}
2231
2232void
2233input_receive_drag_data(struct input *input, const char *mime_type,
2234 data_func_t func, void *data)
2235{
2236 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2237 input->drag_offer->x = input->sx;
2238 input->drag_offer->y = input->sy;
2239}
2240
2241int
2242input_receive_selection_data(struct input *input, const char *mime_type,
2243 data_func_t func, void *data)
2244{
2245 char **p;
2246
2247 if (input->selection_offer == NULL)
2248 return -1;
2249
2250 for (p = input->selection_offer->types.data; *p; p++)
2251 if (strcmp(mime_type, *p) == 0)
2252 break;
2253
2254 if (*p == NULL)
2255 return -1;
2256
2257 data_offer_receive_data(input->selection_offer,
2258 mime_type, func, data);
2259 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002260}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002261
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002262int
2263input_receive_selection_data_to_fd(struct input *input,
2264 const char *mime_type, int fd)
2265{
2266 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2267
2268 return 0;
2269}
2270
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002271void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002272window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002273{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002274 if (!window->shell_surface)
2275 return;
2276
Daniel Stone37816df2012-05-16 18:45:18 +01002277 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002278}
2279
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002280static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002281idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002282{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002283 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002284
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002285 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002286 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002287 widget_set_allocation(widget,
2288 window->pending_allocation.x,
2289 window->pending_allocation.y,
2290 window->pending_allocation.width,
2291 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002292
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002293 if (window->input_region) {
2294 wl_region_destroy(window->input_region);
2295 window->input_region = NULL;
2296 }
2297
2298 if (window->opaque_region) {
2299 wl_region_destroy(window->opaque_region);
2300 window->opaque_region = NULL;
2301 }
2302
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002303 if (widget->resize_handler)
2304 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002305 widget->allocation.width,
2306 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002307 widget->user_data);
2308
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002309 if (window->allocation.width != widget->allocation.width ||
2310 window->allocation.height != widget->allocation.height) {
2311 window->allocation = widget->allocation;
2312 window_schedule_redraw(window);
2313 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002314}
2315
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002316void
2317window_schedule_resize(struct window *window, int width, int height)
2318{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002319 window->pending_allocation.x = 0;
2320 window->pending_allocation.y = 0;
2321 window->pending_allocation.width = width;
2322 window->pending_allocation.height = height;
2323
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002324 window->resize_needed = 1;
2325 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002326}
2327
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002328void
2329widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2330{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002331 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002332}
2333
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002334static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002335handle_ping(void *data, struct wl_shell_surface *shell_surface,
2336 uint32_t serial)
2337{
2338 wl_shell_surface_pong(shell_surface, serial);
2339}
2340
2341static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002342handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002343 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002344{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002345 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002346
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002347 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002348 return;
2349
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002350 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002351 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002352}
2353
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002354static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002355menu_destroy(struct menu *menu)
2356{
2357 widget_destroy(menu->widget);
2358 window_destroy(menu->window);
2359 free(menu);
2360}
2361
2362static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002363handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2364{
2365 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002366 struct menu *menu = window->widget->user_data;
2367
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002368 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002369 * device. Or just use wl_callback. And this really needs to
2370 * be a window vfunc that the menu can set. And we need the
2371 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002372
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002373 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002374 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002375 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002376}
2377
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002378static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002379 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002380 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002381 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002382};
2383
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002384void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002385window_get_allocation(struct window *window,
2386 struct rectangle *allocation)
2387{
2388 *allocation = window->allocation;
2389}
2390
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002391static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002392widget_redraw(struct widget *widget)
2393{
2394 struct widget *child;
2395
2396 if (widget->redraw_handler)
2397 widget->redraw_handler(widget, widget->user_data);
2398 wl_list_for_each(child, &widget->child_list, link)
2399 widget_redraw(child);
2400}
2401
2402static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002403frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2404{
2405 struct window *window = data;
2406
2407 wl_callback_destroy(callback);
2408 window->redraw_scheduled = 0;
2409 if (window->redraw_needed)
2410 window_schedule_redraw(window);
2411}
2412
2413static const struct wl_callback_listener listener = {
2414 frame_callback
2415};
2416
2417static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002418idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002419{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002420 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002421 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002422
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002423 if (window->resize_needed)
2424 idle_resize(window);
2425
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002426 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002427 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002428 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002429 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002430 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002431
2432 callback = wl_surface_frame(window->surface);
2433 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002434}
2435
2436void
2437window_schedule_redraw(struct window *window)
2438{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002439 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002440 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002441 window->redraw_task.run = idle_redraw;
2442 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002443 window->redraw_scheduled = 1;
2444 }
2445}
2446
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002447void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002448window_set_custom(struct window *window)
2449{
2450 window->type = TYPE_CUSTOM;
2451}
2452
2453void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002454window_set_fullscreen(struct window *window, int fullscreen)
2455{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002456 if (!window->display->shell)
2457 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002458
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002459 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002460 return;
2461
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002462 if (fullscreen) {
2463 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002464 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002465 wl_shell_surface_set_fullscreen(window->shell_surface,
2466 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2467 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002468 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002469 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002470 wl_shell_surface_set_toplevel(window->shell_surface);
2471 window_schedule_resize(window,
2472 window->saved_allocation.width,
2473 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002474 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002475}
2476
2477void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002478window_set_maximized(struct window *window, int maximized)
2479{
2480 if (!window->display->shell)
2481 return;
2482
2483 if ((window->type == TYPE_MAXIMIZED) == maximized)
2484 return;
2485
2486 if (window->type == TYPE_TOPLEVEL) {
2487 window->saved_allocation = window->allocation;
2488 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2489 window->type = TYPE_MAXIMIZED;
2490 } else {
2491 wl_shell_surface_set_toplevel(window->shell_surface);
2492 window->type = TYPE_TOPLEVEL;
2493 window_schedule_resize(window,
2494 window->saved_allocation.width,
2495 window->saved_allocation.height);
2496 }
2497}
2498
2499void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002500window_set_user_data(struct window *window, void *data)
2501{
2502 window->user_data = data;
2503}
2504
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002505void *
2506window_get_user_data(struct window *window)
2507{
2508 return window->user_data;
2509}
2510
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002511void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002512window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002513 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002514{
2515 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002516}
2517
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002518void
2519window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002520 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002521{
2522 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002523}
2524
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002525void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002526window_set_data_handler(struct window *window, window_data_handler_t handler)
2527{
2528 window->data_handler = handler;
2529}
2530
2531void
2532window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2533{
2534 window->drop_handler = handler;
2535}
2536
2537void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002538window_set_close_handler(struct window *window,
2539 window_close_handler_t handler)
2540{
2541 window->close_handler = handler;
2542}
2543
2544void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002545window_set_title(struct window *window, const char *title)
2546{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002547 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002548 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002549 if (window->shell_surface)
2550 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002551}
2552
2553const char *
2554window_get_title(struct window *window)
2555{
2556 return window->title;
2557}
2558
2559void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002560window_damage(struct window *window, int32_t x, int32_t y,
2561 int32_t width, int32_t height)
2562{
2563 wl_surface_damage(window->surface, x, y, width, height);
2564}
2565
Casey Dahlin9074db52012-04-19 22:50:09 -04002566static void
2567surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002568 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002569{
Rob Bradford7507b572012-05-15 17:55:34 +01002570 struct window *window = data;
2571 struct output *output;
2572 struct output *output_found = NULL;
2573 struct window_output *window_output;
2574
2575 wl_list_for_each(output, &window->display->output_list, link) {
2576 if (output->output == wl_output) {
2577 output_found = output;
2578 break;
2579 }
2580 }
2581
2582 if (!output_found)
2583 return;
2584
2585 window_output = malloc (sizeof *window_output);
2586 window_output->output = output_found;
2587
2588 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002589}
2590
2591static void
2592surface_leave(void *data,
2593 struct wl_surface *wl_surface, struct wl_output *output)
2594{
Rob Bradford7507b572012-05-15 17:55:34 +01002595 struct window *window = data;
2596 struct window_output *window_output;
2597 struct window_output *window_output_found = NULL;
2598
2599 wl_list_for_each(window_output, &window->window_output_list, link) {
2600 if (window_output->output->output == output) {
2601 window_output_found = window_output;
2602 break;
2603 }
2604 }
2605
2606 if (window_output_found) {
2607 wl_list_remove(&window_output_found->link);
2608 free(window_output_found);
2609 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002610}
2611
2612static const struct wl_surface_listener surface_listener = {
2613 surface_enter,
2614 surface_leave
2615};
2616
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002617static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002618window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002619{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002620 struct window *window;
2621
2622 window = malloc(sizeof *window);
2623 if (window == NULL)
2624 return NULL;
2625
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002626 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002627 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002628 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002629 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002630 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002631 if (display->shell) {
2632 window->shell_surface =
2633 wl_shell_get_shell_surface(display->shell,
2634 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002635 if (window->title)
2636 wl_shell_surface_set_title(window->shell_surface,
2637 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002638 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002639 window->allocation.x = 0;
2640 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002641 window->allocation.width = 0;
2642 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002643 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002644 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002645 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002646 window->input_region = NULL;
2647 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002648
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002649 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002650#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002651 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002652#else
2653 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2654#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002655 else
2656 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002657
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002658 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002659 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002660 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002661
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002662 if (window->shell_surface) {
2663 wl_shell_surface_set_user_data(window->shell_surface, window);
2664 wl_shell_surface_add_listener(window->shell_surface,
2665 &shell_surface_listener, window);
2666 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002667
Rob Bradford7507b572012-05-15 17:55:34 +01002668 wl_list_init (&window->window_output_list);
2669
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002670 return window;
2671}
2672
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002673struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002674window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002675{
2676 struct window *window;
2677
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002678 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002679 if (!window)
2680 return NULL;
2681
2682 return window;
2683}
2684
2685struct window *
2686window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002687 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002688{
2689 struct window *window;
2690
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002691 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002692 if (!window)
2693 return NULL;
2694
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002695 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002696 window->x = x;
2697 window->y = y;
2698
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002699 if (display->shell)
2700 wl_shell_surface_set_transient(window->shell_surface,
2701 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002702 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002703
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002704 return window;
2705}
2706
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002707static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002708menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002709{
2710 int next;
2711
2712 next = (sy - 8) / 20;
2713 if (menu->current != next) {
2714 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002715 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002716 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002717}
2718
2719static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002720menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002721 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002722 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002723{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002724 struct menu *menu = data;
2725
2726 if (widget == menu->widget)
2727 menu_set_item(data, y);
2728
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002729 return WL_CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002730}
2731
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002732static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002733menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002734 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002735{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002736 struct menu *menu = data;
2737
2738 if (widget == menu->widget)
2739 menu_set_item(data, y);
2740
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002741 return WL_CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002742}
2743
2744static void
2745menu_leave_handler(struct widget *widget, struct input *input, void *data)
2746{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002747 struct menu *menu = data;
2748
2749 if (widget == menu->widget)
2750 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002751}
2752
2753static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002754menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002755 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002756 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002757
2758{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002759 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002760
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002761 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002762 /* Either relase after press-drag-release or
2763 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002764 menu->func(menu->window->parent,
2765 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002766 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002767 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002768 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002769}
2770
2771static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002772menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002773{
2774 cairo_t *cr;
2775 const int32_t r = 3, margin = 3;
2776 struct menu *menu = data;
2777 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002778 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002779
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002780 cr = cairo_create(window->cairo_surface);
2781 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2782 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2783 cairo_paint(cr);
2784
2785 width = window->allocation.width;
2786 height = window->allocation.height;
2787 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002788
2789 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002790 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2791 cairo_fill(cr);
2792
2793 for (i = 0; i < menu->count; i++) {
2794 if (i == menu->current) {
2795 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2796 cairo_rectangle(cr, margin, i * 20 + margin,
2797 width - 2 * margin, 20);
2798 cairo_fill(cr);
2799 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2800 cairo_move_to(cr, 10, i * 20 + 16);
2801 cairo_show_text(cr, menu->entries[i]);
2802 } else {
2803 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2804 cairo_move_to(cr, 10, i * 20 + 16);
2805 cairo_show_text(cr, menu->entries[i]);
2806 }
2807 }
2808
2809 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002810}
2811
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002812void
2813window_show_menu(struct display *display,
2814 struct input *input, uint32_t time, struct window *parent,
2815 int32_t x, int32_t y,
2816 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002817{
2818 struct window *window;
2819 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002820 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002821
2822 menu = malloc(sizeof *menu);
2823 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002824 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002825
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002826 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002827 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002828 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002829
2830 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002831 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002832 menu->entries = entries;
2833 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002834 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002835 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002836 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002837 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002838 window->type = TYPE_MENU;
2839 window->x = x;
2840 window->y = y;
2841
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002842 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002843 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002844 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002845 window->parent->shell_surface,
2846 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002847
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002848 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002849 widget_set_enter_handler(menu->widget, menu_enter_handler);
2850 widget_set_leave_handler(menu->widget, menu_leave_handler);
2851 widget_set_motion_handler(menu->widget, menu_motion_handler);
2852 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002853
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002854 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002855 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002856}
2857
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002858void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002859window_set_buffer_type(struct window *window, enum window_buffer_type type)
2860{
2861 window->buffer_type = type;
2862}
2863
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002864
2865static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002866display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002867 struct wl_output *wl_output,
2868 int x, int y,
2869 int physical_width,
2870 int physical_height,
2871 int subpixel,
2872 const char *make,
2873 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002874{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002875 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002876
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002877 output->allocation.x = x;
2878 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002879}
2880
2881static void
2882display_handle_mode(void *data,
2883 struct wl_output *wl_output,
2884 uint32_t flags,
2885 int width,
2886 int height,
2887 int refresh)
2888{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002889 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002890 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002891
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002892 if (flags & WL_OUTPUT_MODE_CURRENT) {
2893 output->allocation.width = width;
2894 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002895 if (display->output_configure_handler)
2896 (*display->output_configure_handler)(
2897 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002898 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002899}
2900
2901static const struct wl_output_listener output_listener = {
2902 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002903 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002904};
2905
2906static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002907display_add_output(struct display *d, uint32_t id)
2908{
2909 struct output *output;
2910
2911 output = malloc(sizeof *output);
2912 if (output == NULL)
2913 return;
2914
2915 memset(output, 0, sizeof *output);
2916 output->display = d;
2917 output->output =
2918 wl_display_bind(d->display, id, &wl_output_interface);
2919 wl_list_insert(d->output_list.prev, &output->link);
2920
2921 wl_output_add_listener(output->output, &output_listener, output);
2922}
2923
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002924static void
2925output_destroy(struct output *output)
2926{
2927 if (output->destroy_handler)
2928 (*output->destroy_handler)(output, output->user_data);
2929
2930 wl_output_destroy(output->output);
2931 wl_list_remove(&output->link);
2932 free(output);
2933}
2934
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002935void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002936display_set_output_configure_handler(struct display *display,
2937 display_output_handler_t handler)
2938{
2939 struct output *output;
2940
2941 display->output_configure_handler = handler;
2942 if (!handler)
2943 return;
2944
2945 wl_list_for_each(output, &display->output_list, link)
2946 (*display->output_configure_handler)(output,
2947 display->user_data);
2948}
2949
2950void
2951output_set_user_data(struct output *output, void *data)
2952{
2953 output->user_data = data;
2954}
2955
2956void *
2957output_get_user_data(struct output *output)
2958{
2959 return output->user_data;
2960}
2961
2962void
2963output_set_destroy_handler(struct output *output,
2964 display_output_handler_t handler)
2965{
2966 output->destroy_handler = handler;
2967 /* FIXME: implement this, once we have way to remove outputs */
2968}
2969
2970void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002971output_get_allocation(struct output *output, struct rectangle *allocation)
2972{
2973 *allocation = output->allocation;
2974}
2975
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002976struct wl_output *
2977output_get_wl_output(struct output *output)
2978{
2979 return output->output;
2980}
2981
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002982static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002983display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002984{
2985 struct input *input;
2986
2987 input = malloc(sizeof *input);
2988 if (input == NULL)
2989 return;
2990
2991 memset(input, 0, sizeof *input);
2992 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01002993 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002994 input->pointer_focus = NULL;
2995 input->keyboard_focus = NULL;
2996 wl_list_insert(d->input_list.prev, &input->link);
2997
Daniel Stone37816df2012-05-16 18:45:18 +01002998 wl_seat_add_listener(input->seat, &seat_listener, input);
2999 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003000
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003001 input->data_device =
3002 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003003 input->seat);
3004 wl_data_device_add_listener(input->data_device, &data_device_listener,
3005 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003006}
3007
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003008static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003009input_destroy(struct input *input)
3010{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003011 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003012 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003013
3014 if (input->drag_offer)
3015 data_offer_destroy(input->drag_offer);
3016
3017 if (input->selection_offer)
3018 data_offer_destroy(input->selection_offer);
3019
3020 wl_data_device_destroy(input->data_device);
3021 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003022 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003023 free(input);
3024}
3025
3026static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003027display_handle_global(struct wl_display *display, uint32_t id,
3028 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003029{
3030 struct display *d = data;
3031
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003032 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003033 d->compositor =
3034 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003035 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003036 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003037 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003038 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003039 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003040 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003041 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003042 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003043 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3044 d->data_device_manager =
3045 wl_display_bind(display, id,
3046 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003047 }
3048}
3049
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003050static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003051init_xkb(struct display *d)
3052{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003053 d->xkb.names.rules = "evdev";
3054 d->xkb.names.model = "pc105";
3055 d->xkb.names.layout = (char *) option_xkb_layout;
3056 d->xkb.names.variant = (char *) option_xkb_variant;
3057 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003058
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003059 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003060 if (!d->xkb.context) {
3061 fprintf(stderr, "Failed to create XKB context\n");
3062 exit(1);
3063 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003064
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003065 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003066 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003067 fprintf(stderr, "Failed to compile keymap\n");
3068 exit(1);
3069 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003070
3071 d->xkb.state = xkb_state_new(d->xkb.keymap);
3072 if (!d->xkb.state) {
3073 fprintf(stderr, "Failed to create XKB state\n");
3074 exit(1);
3075 }
3076
3077 d->xkb.control_mask =
3078 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3079 d->xkb.alt_mask =
3080 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3081 d->xkb.shift_mask =
3082 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3083
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003084}
3085
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003086static void
3087fini_xkb(struct display *display)
3088{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003089 xkb_state_unref(display->xkb.state);
3090 xkb_map_unref(display->xkb.keymap);
3091 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003092}
3093
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003094#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003095static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003096init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003097{
3098 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003099 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003100
Rob Clark6396ed32012-03-11 19:48:41 -05003101#ifdef USE_CAIRO_GLESV2
3102# define GL_BIT EGL_OPENGL_ES2_BIT
3103#else
3104# define GL_BIT EGL_OPENGL_BIT
3105#endif
3106
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003107 static const EGLint argb_cfg_attribs[] = {
3108 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003109 EGL_RED_SIZE, 1,
3110 EGL_GREEN_SIZE, 1,
3111 EGL_BLUE_SIZE, 1,
3112 EGL_ALPHA_SIZE, 1,
3113 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003114 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003115 EGL_NONE
3116 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003117
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003118#ifdef USE_CAIRO_GLESV2
3119 static const EGLint context_attribs[] = {
3120 EGL_CONTEXT_CLIENT_VERSION, 2,
3121 EGL_NONE
3122 };
3123 EGLint api = EGL_OPENGL_ES_API;
3124#else
3125 EGLint *context_attribs = NULL;
3126 EGLint api = EGL_OPENGL_API;
3127#endif
3128
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003129 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003130 if (!eglInitialize(d->dpy, &major, &minor)) {
3131 fprintf(stderr, "failed to initialize display\n");
3132 return -1;
3133 }
3134
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003135 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003136 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3137 return -1;
3138 }
3139
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003140 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3141 &d->argb_config, 1, &n) || n != 1) {
3142 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003143 return -1;
3144 }
3145
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003146 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003147 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003148 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003149 fprintf(stderr, "failed to create context\n");
3150 return -1;
3151 }
3152
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003153 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003154 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003155 return -1;
3156 }
3157
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003158#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003159 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3160 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3161 fprintf(stderr, "failed to get cairo egl argb device\n");
3162 return -1;
3163 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003164#endif
3165
3166 return 0;
3167}
3168
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003169static void
3170fini_egl(struct display *display)
3171{
3172#ifdef HAVE_CAIRO_EGL
3173 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003174#endif
3175
3176 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3177 EGL_NO_CONTEXT);
3178
3179 eglTerminate(display->dpy);
3180 eglReleaseThread();
3181}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003182#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003183
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003184static int
3185event_mask_update(uint32_t mask, void *data)
3186{
3187 struct display *d = data;
3188
3189 d->mask = mask;
3190
3191 return 0;
3192}
3193
3194static void
3195handle_display_data(struct task *task, uint32_t events)
3196{
3197 struct display *display =
3198 container_of(task, struct display, display_task);
3199
3200 wl_display_iterate(display->display, display->mask);
3201}
3202
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003203struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003204display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003205{
3206 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003207
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003208 argc = parse_options(xkb_options,
3209 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003210
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003211 d = malloc(sizeof *d);
3212 if (d == NULL)
3213 return NULL;
3214
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003215 memset(d, 0, sizeof *d);
3216
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003217 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003218 if (d->display == NULL) {
3219 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003220 return NULL;
3221 }
3222
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003223 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3224 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3225 d->display_task.run = handle_display_data;
3226 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3227
3228 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003229 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003230 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003231
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003232 /* Set up listener so we'll catch all events. */
3233 wl_display_add_global_listener(d->display,
3234 display_handle_global, d);
3235
3236 /* Process connection events. */
3237 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003238#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003239 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003240 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003241#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003242
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003243 d->image_target_texture_2d =
3244 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3245 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3246 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3247
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003248 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003249
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003250 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003251
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003252 wl_list_init(&d->window_list);
3253
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003254 init_xkb(d);
3255
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003256 return d;
3257}
3258
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003259static void
3260display_destroy_outputs(struct display *display)
3261{
3262 struct output *tmp;
3263 struct output *output;
3264
3265 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3266 output_destroy(output);
3267}
3268
Pekka Paalanene1207c72011-12-16 12:02:09 +02003269static void
3270display_destroy_inputs(struct display *display)
3271{
3272 struct input *tmp;
3273 struct input *input;
3274
3275 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3276 input_destroy(input);
3277}
3278
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003279void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003280display_destroy(struct display *display)
3281{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003282 if (!wl_list_empty(&display->window_list))
3283 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3284
3285 if (!wl_list_empty(&display->deferred_list))
3286 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3287
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003288 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003289 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003290
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003291 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003292
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003293 theme_destroy(display->theme);
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003294 wl_cursor_theme_destroy(display->cursor_theme);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003295
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003296#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003297 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003298#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003299
Pekka Paalanenc2052982011-12-16 11:41:32 +02003300 if (display->shell)
3301 wl_shell_destroy(display->shell);
3302
3303 if (display->shm)
3304 wl_shm_destroy(display->shm);
3305
3306 if (display->data_device_manager)
3307 wl_data_device_manager_destroy(display->data_device_manager);
3308
3309 wl_compositor_destroy(display->compositor);
3310
3311 close(display->epoll_fd);
3312
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003313 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003314 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003315 free(display);
3316}
3317
3318void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003319display_set_user_data(struct display *display, void *data)
3320{
3321 display->user_data = data;
3322}
3323
3324void *
3325display_get_user_data(struct display *display)
3326{
3327 return display->user_data;
3328}
3329
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003330struct wl_display *
3331display_get_display(struct display *display)
3332{
3333 return display->display;
3334}
3335
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003336struct output *
3337display_get_output(struct display *display)
3338{
3339 return container_of(display->output_list.next, struct output, link);
3340}
3341
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003342struct wl_compositor *
3343display_get_compositor(struct display *display)
3344{
3345 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003346}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003347
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003348uint32_t
3349display_get_serial(struct display *display)
3350{
3351 return display->serial;
3352}
3353
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003354EGLDisplay
3355display_get_egl_display(struct display *d)
3356{
3357 return d->dpy;
3358}
3359
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003360struct wl_data_source *
3361display_create_data_source(struct display *display)
3362{
3363 return wl_data_device_manager_create_data_source(display->data_device_manager);
3364}
3365
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003366EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003367display_get_argb_egl_config(struct display *d)
3368{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003369 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003370}
3371
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003372struct wl_shell *
3373display_get_shell(struct display *display)
3374{
3375 return display->shell;
3376}
3377
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003378int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003379display_acquire_window_surface(struct display *display,
3380 struct window *window,
3381 EGLContext ctx)
3382{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003383#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003384 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003385 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003386
3387 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003388 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003389 device = cairo_surface_get_device(window->cairo_surface);
3390 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003391 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003392
Benjamin Franzke0c991632011-09-27 21:57:31 +02003393 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003394 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003395 ctx = display->argb_ctx;
3396 else
3397 assert(0);
3398 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003399
3400 data = cairo_surface_get_user_data(window->cairo_surface,
3401 &surface_data_key);
3402
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003403 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003404 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003405 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3406 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003407
3408 return 0;
3409#else
3410 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003411#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003412}
3413
3414void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003415display_release_window_surface(struct display *display,
3416 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003417{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003418#ifdef HAVE_CAIRO_EGL
3419 cairo_device_t *device;
3420
3421 device = cairo_surface_get_device(window->cairo_surface);
3422 if (!device)
3423 return;
3424
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003425 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003426 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003427 cairo_device_release(device);
3428#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003429}
3430
3431void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003432display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003433{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003434 wl_list_insert(&display->deferred_list, &task->link);
3435}
3436
3437void
3438display_watch_fd(struct display *display,
3439 int fd, uint32_t events, struct task *task)
3440{
3441 struct epoll_event ep;
3442
3443 ep.events = events;
3444 ep.data.ptr = task;
3445 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3446}
3447
3448void
3449display_run(struct display *display)
3450{
3451 struct task *task;
3452 struct epoll_event ep[16];
3453 int i, count;
3454
Pekka Paalanen826d7952011-12-15 10:14:07 +02003455 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003456 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003457 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003458
3459 while (!wl_list_empty(&display->deferred_list)) {
3460 task = container_of(display->deferred_list.next,
3461 struct task, link);
3462 wl_list_remove(&task->link);
3463 task->run(task, 0);
3464 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003465
3466 if (!display->running)
3467 break;
3468
3469 wl_display_flush(display->display);
3470
3471 count = epoll_wait(display->epoll_fd,
3472 ep, ARRAY_LENGTH(ep), -1);
3473 for (i = 0; i < count; i++) {
3474 task = ep[i].data.ptr;
3475 task->run(task, ep[i].events);
3476 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003477 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003478}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003479
3480void
3481display_exit(struct display *display)
3482{
3483 display->running = 0;
3484}