blob: 0c6f5561c0ee3291a047b4d22cc7e570f3f6a602 [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;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300107 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400108
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500109 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
110 PFNEGLCREATEIMAGEKHRPROC create_image;
111 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200112
113 display_output_handler_t output_configure_handler;
114
115 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500116};
117
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400119 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400121 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500122 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500124 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400125 TYPE_CUSTOM
126};
Rob Bradford7507b572012-05-15 17:55:34 +0100127
128struct window_output {
129 struct output *output;
130 struct wl_list link;
131};
132
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133struct window {
134 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500135 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100136 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500137 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200138 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500139 struct wl_region *input_region;
140 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500141 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500142 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500143 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500144 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400145 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400146 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400147 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400148 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400149 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400150 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400151 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400152 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400153 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500154
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400155 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500156
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700157 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400158
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500159 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500160 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400161 window_data_handler_t data_handler;
162 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500163 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400164
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200165 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500166 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500167
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500168 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400169 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500170};
171
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500172struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500173 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300174 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500175 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400176 struct wl_list link;
177 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500178 widget_resize_handler_t resize_handler;
179 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500180 widget_enter_handler_t enter_handler;
181 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500182 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500183 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400184 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500185 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300186 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400187};
188
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400189struct input {
190 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100191 struct wl_seat *seat;
192 struct wl_pointer *pointer;
193 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400194 struct window *pointer_focus;
195 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300196 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400197 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400198 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400199 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400200 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400201
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500202 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500203 struct widget *grab;
204 uint32_t grab_button;
205
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400206 struct wl_data_device *data_device;
207 struct data_offer *drag_offer;
208 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400209};
210
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500211struct output {
212 struct display *display;
213 struct wl_output *output;
214 struct rectangle allocation;
215 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200216
217 display_output_handler_t destroy_handler;
218 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500219};
220
Martin Minarik1998b152012-05-10 02:04:35 +0200221enum frame_button_action {
222 FRAME_BUTTON_NULL = 0,
223 FRAME_BUTTON_ICON = 1,
224 FRAME_BUTTON_CLOSE = 2,
225 FRAME_BUTTON_MINIMIZE = 3,
226 FRAME_BUTTON_MAXIMIZE = 4,
227};
228
229enum frame_button_pointer {
230 FRAME_BUTTON_DEFAULT = 0,
231 FRAME_BUTTON_OVER = 1,
232 FRAME_BUTTON_ACTIVE = 2,
233};
234
235enum frame_button_align {
236 FRAME_BUTTON_RIGHT = 0,
237 FRAME_BUTTON_LEFT = 1,
238};
239
240enum frame_button_decoration {
241 FRAME_BUTTON_NONE = 0,
242 FRAME_BUTTON_FANCY = 1,
243};
244
245struct frame_button {
246 struct widget *widget;
247 struct frame *frame;
248 cairo_surface_t *icon;
249 enum frame_button_action type;
250 enum frame_button_pointer state;
251 struct wl_list link; /* buttons_list */
252 enum frame_button_align align;
253 enum frame_button_decoration decoration;
254};
255
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500256struct frame {
257 struct widget *widget;
258 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200259 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500260};
261
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500262struct menu {
263 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500264 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500265 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500266 const char **entries;
267 uint32_t time;
268 int current;
269 int count;
270 menu_func_t func;
271};
272
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300273struct tooltip {
274 struct widget *parent;
275 struct window *window;
276 struct widget *widget;
277 char *entry;
278 struct task tooltip_task;
279 int tooltip_fd;
280 float x, y;
281};
282
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700283struct shm_pool {
284 struct wl_shm_pool *pool;
285 size_t size;
286 size_t used;
287 void *data;
288};
289
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400290enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300291 CURSOR_DEFAULT = 100,
292 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400293};
294
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500295enum window_location {
296 WINDOW_INTERIOR = 0,
297 WINDOW_RESIZING_TOP = 1,
298 WINDOW_RESIZING_BOTTOM = 2,
299 WINDOW_RESIZING_LEFT = 4,
300 WINDOW_RESIZING_TOP_LEFT = 5,
301 WINDOW_RESIZING_BOTTOM_LEFT = 6,
302 WINDOW_RESIZING_RIGHT = 8,
303 WINDOW_RESIZING_TOP_RIGHT = 9,
304 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
305 WINDOW_RESIZING_MASK = 15,
306 WINDOW_EXTERIOR = 16,
307 WINDOW_TITLEBAR = 17,
308 WINDOW_CLIENT_AREA = 18,
309};
310
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400311const char *option_xkb_layout = "us";
312const char *option_xkb_variant = "";
313const char *option_xkb_options = "";
314
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400315static const struct weston_option xkb_options[] = {
316 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
317 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
318 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400319};
320
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400321static const cairo_user_data_key_t surface_data_key;
322struct surface_data {
323 struct wl_buffer *buffer;
324};
325
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500326#define MULT(_d,c,a,t) \
327 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
328
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500329#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100331struct egl_window_surface_data {
332 struct display *display;
333 struct wl_surface *surface;
334 struct wl_egl_window *window;
335 EGLSurface surf;
336};
337
338static void
339egl_window_surface_data_destroy(void *p)
340{
341 struct egl_window_surface_data *data = p;
342 struct display *d = data->display;
343
344 eglDestroySurface(d->dpy, data->surf);
345 wl_egl_window_destroy(data->window);
346 data->surface = NULL;
347
348 free(p);
349}
350
351static cairo_surface_t *
352display_create_egl_window_surface(struct display *display,
353 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400354 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100355 struct rectangle *rectangle)
356{
357 cairo_surface_t *cairo_surface;
358 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400359 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200360 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100361
362 data = malloc(sizeof *data);
363 if (data == NULL)
364 return NULL;
365
366 data->display = display;
367 data->surface = surface;
368
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500369 config = display->argb_config;
370 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400371
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400372 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100373 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100375
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500377 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378
Benjamin Franzke0c991632011-09-27 21:57:31 +0200379 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100380 data->surf,
381 rectangle->width,
382 rectangle->height);
383
384 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
385 data, egl_window_surface_data_destroy);
386
387 return cairo_surface;
388}
389
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390#endif
391
392struct wl_buffer *
393display_get_buffer_for_surface(struct display *display,
394 cairo_surface_t *surface)
395{
396 struct surface_data *data;
397
398 data = cairo_surface_get_user_data (surface, &surface_data_key);
399
400 return data->buffer;
401}
402
403struct shm_surface_data {
404 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700405 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406};
407
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500408static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700409shm_pool_destroy(struct shm_pool *pool);
410
411static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412shm_surface_data_destroy(void *p)
413{
414 struct shm_surface_data *data = p;
415
416 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700417 if (data->pool)
418 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419}
420
Kristian Høgsberg16626282012-04-03 11:21:27 -0400421static struct wl_shm_pool *
422make_shm_pool(struct display *display, int size, void **data)
423{
424 char filename[] = "/tmp/wayland-shm-XXXXXX";
425 struct wl_shm_pool *pool;
426 int fd;
427
428 fd = mkstemp(filename);
429 if (fd < 0) {
430 fprintf(stderr, "open %s failed: %m\n", filename);
431 return NULL;
432 }
433 if (ftruncate(fd, size) < 0) {
434 fprintf(stderr, "ftruncate failed: %m\n");
435 close(fd);
436 return NULL;
437 }
438
439 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
440 unlink(filename);
441
442 if (*data == MAP_FAILED) {
443 fprintf(stderr, "mmap failed: %m\n");
444 close(fd);
445 return NULL;
446 }
447
448 pool = wl_shm_create_pool(display->shm, fd, size);
449
450 close(fd);
451
452 return pool;
453}
454
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700455static struct shm_pool *
456shm_pool_create(struct display *display, size_t size)
457{
458 struct shm_pool *pool = malloc(sizeof *pool);
459
460 if (!pool)
461 return NULL;
462
463 pool->pool = make_shm_pool(display, size, &pool->data);
464 if (!pool->pool) {
465 free(pool);
466 return NULL;
467 }
468
469 pool->size = size;
470 pool->used = 0;
471
472 return pool;
473}
474
475static void *
476shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
477{
478 if (pool->used + size > pool->size)
479 return NULL;
480
481 *offset = pool->used;
482 pool->used += size;
483
484 return (char *) pool->data + *offset;
485}
486
487/* destroy the pool. this does not unmap the memory though */
488static void
489shm_pool_destroy(struct shm_pool *pool)
490{
491 munmap(pool->data, pool->size);
492 wl_shm_pool_destroy(pool->pool);
493 free(pool);
494}
495
496/* Start allocating from the beginning of the pool again */
497static void
498shm_pool_reset(struct shm_pool *pool)
499{
500 pool->used = 0;
501}
502
503static int
504data_length_for_shm_surface(struct rectangle *rect)
505{
506 int stride;
507
508 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
509 rect->width);
510 return stride * rect->height;
511}
512
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500513static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700514display_create_shm_surface_from_pool(struct display *display,
515 struct rectangle *rectangle,
516 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517{
518 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400519 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400520 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700521 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400522 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523
524 data = malloc(sizeof *data);
525 if (data == NULL)
526 return NULL;
527
528 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
529 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530 length = stride * rectangle->height;
531 data->pool = NULL;
532 map = shm_pool_allocate(pool, length, &offset);
533
534 if (!map) {
535 free(data);
536 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400537 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400539 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540 CAIRO_FORMAT_ARGB32,
541 rectangle->width,
542 rectangle->height,
543 stride);
544
545 cairo_surface_set_user_data (surface, &surface_data_key,
546 data, shm_surface_data_destroy);
547
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500549 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500551 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400552
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700553 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400554 rectangle->width,
555 rectangle->height,
556 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700558 return surface;
559}
560
561static cairo_surface_t *
562display_create_shm_surface(struct display *display,
563 struct rectangle *rectangle, uint32_t flags,
564 struct window *window)
565{
566 struct shm_surface_data *data;
567 struct shm_pool *pool;
568 cairo_surface_t *surface;
569
570 if (window && window->pool) {
571 shm_pool_reset(window->pool);
572 surface = display_create_shm_surface_from_pool(display,
573 rectangle,
574 flags,
575 window->pool);
576 if (surface)
577 return surface;
578 }
579
580 pool = shm_pool_create(display,
581 data_length_for_shm_surface(rectangle));
582 if (!pool)
583 return NULL;
584
585 surface =
586 display_create_shm_surface_from_pool(display, rectangle,
587 flags, pool);
588
589 if (!surface) {
590 shm_pool_destroy(pool);
591 return NULL;
592 }
593
594 /* make sure we destroy the pool when the surface is destroyed */
595 data = cairo_surface_get_user_data(surface, &surface_data_key);
596 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597
598 return surface;
599}
600
nobled7b87cb02011-02-01 18:51:47 +0000601static int
602check_size(struct rectangle *rect)
603{
604 if (rect->width && rect->height)
605 return 0;
606
607 fprintf(stderr, "tried to create surface of "
608 "width: %d, height: %d\n", rect->width, rect->height);
609 return -1;
610}
611
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612cairo_surface_t *
613display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100614 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400615 struct rectangle *rectangle,
616 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617{
nobled7b87cb02011-02-01 18:51:47 +0000618 if (check_size(rectangle) < 0)
619 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500620#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300621 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400622 return display_create_egl_window_surface(display,
623 surface,
624 flags,
625 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400627 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628}
629
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300630static const char *cursors[] = {
631 "bottom_left_corner",
632 "bottom_right_corner",
633 "bottom_side",
634 "grabbing",
635 "left_ptr",
636 "left_side",
637 "right_side",
638 "top_left_corner",
639 "top_right_corner",
640 "top_side",
641 "xterm",
642 "hand1",
643};
644
645static void
646create_cursors(struct display *display)
647{
648 unsigned int i;
649
650 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
651 display->cursors =
652 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
653
654 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
655 display->cursors[i] =
656 wl_cursor_theme_get_cursor(display->cursor_theme,
657 cursors[i]);
658}
659
660static void
661destroy_cursors(struct display *display)
662{
663 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800664 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300665}
666
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300667struct wl_cursor_image *
668display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400669{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300670 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300671 wl_cursor_theme_get_cursor(display->cursor_theme,
672 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300674 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400675}
676
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400677static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200678window_get_resize_dx_dy(struct window *window, int *x, int *y)
679{
680 if (window->resize_edges & WINDOW_RESIZING_LEFT)
681 *x = window->server_allocation.width - window->allocation.width;
682 else
683 *x = 0;
684
685 if (window->resize_edges & WINDOW_RESIZING_TOP)
686 *y = window->server_allocation.height -
687 window->allocation.height;
688 else
689 *y = 0;
690
691 window->resize_edges = 0;
692}
693
694static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500695window_attach_surface(struct window *window)
696{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400697 struct display *display = window->display;
698 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000699#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100700 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000701#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500702 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100703
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400704 if (window->type == TYPE_NONE) {
705 window->type = TYPE_TOPLEVEL;
706 if (display->shell)
707 wl_shell_surface_set_toplevel(window->shell_surface);
708 }
709
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100710 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000711#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100712 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
713 data = cairo_surface_get_user_data(window->cairo_surface,
714 &surface_data_key);
715
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100716 cairo_gl_surface_swapbuffers(window->cairo_surface);
717 wl_egl_window_get_attached_size(data->window,
718 &window->server_allocation.width,
719 &window->server_allocation.height);
720 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000721#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723 buffer =
724 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400725 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200727 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400729 wl_surface_damage(window->surface, 0, 0,
730 window->allocation.width,
731 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400733 cairo_surface_destroy(window->cairo_surface);
734 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000736 default:
737 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100738 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500739
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500740 if (window->input_region) {
741 wl_surface_set_input_region(window->surface,
742 window->input_region);
743 wl_region_destroy(window->input_region);
744 window->input_region = NULL;
745 }
746
747 if (window->opaque_region) {
748 wl_surface_set_opaque_region(window->surface,
749 window->opaque_region);
750 wl_region_destroy(window->opaque_region);
751 window->opaque_region = NULL;
752 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500753}
754
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500755void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400756window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500757{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400758 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100759 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500760}
761
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400762void
763window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400764{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500765 cairo_surface_reference(surface);
766
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400767 if (window->cairo_surface != NULL)
768 cairo_surface_destroy(window->cairo_surface);
769
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500770 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400771}
772
Benjamin Franzke22d54812011-07-16 19:50:32 +0000773#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100774static void
775window_resize_cairo_window_surface(struct window *window)
776{
777 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200778 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100779
780 data = cairo_surface_get_user_data(window->cairo_surface,
781 &surface_data_key);
782
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200783 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784 wl_egl_window_resize(data->window,
785 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200786 window->allocation.height,
787 x,y);
788
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 cairo_gl_surface_set_size(window->cairo_surface,
790 window->allocation.width,
791 window->allocation.height);
792}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000793#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400795struct display *
796window_get_display(struct window *window)
797{
798 return window->display;
799}
800
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400801void
802window_create_surface(struct window *window)
803{
804 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400805 uint32_t flags = 0;
806
807 if (!window->transparent)
808 flags = SURFACE_OPAQUE;
809
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400810 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500811#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
813 if (window->cairo_surface) {
814 window_resize_cairo_window_surface(window);
815 return;
816 }
817 surface = display_create_surface(window->display,
818 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400819 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821#endif
822 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400823 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400824 &window->allocation,
825 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800827 default:
828 surface = NULL;
829 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400831
832 window_set_surface(window, surface);
833 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834}
835
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200836static void frame_destroy(struct frame *frame);
837
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400838void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500839window_destroy(struct window *window)
840{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200841 struct display *display = window->display;
842 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100843 struct window_output *window_output;
844 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200845
846 if (window->redraw_scheduled)
847 wl_list_remove(&window->redraw_task.link);
848
849 wl_list_for_each(input, &display->input_list, link) {
850 if (input->pointer_focus == window)
851 input->pointer_focus = NULL;
852 if (input->keyboard_focus == window)
853 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500854 if (input->focus_widget &&
855 input->focus_widget->window == window)
856 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200857 }
858
Rob Bradford7507b572012-05-15 17:55:34 +0100859 wl_list_for_each_safe(window_output, window_output_tmp,
860 &window->window_output_list, link) {
861 free (window_output);
862 }
863
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500864 if (window->input_region)
865 wl_region_destroy(window->input_region);
866 if (window->opaque_region)
867 wl_region_destroy(window->opaque_region);
868
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200869 if (window->frame)
870 frame_destroy(window->frame);
871
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200872 if (window->shell_surface)
873 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500874 wl_surface_destroy(window->surface);
875 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200876
877 if (window->cairo_surface != NULL)
878 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200879
880 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500881 free(window);
882}
883
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500884static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500885widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400886{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500887 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400888
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500889 wl_list_for_each(child, &widget->child_list, link) {
890 target = widget_find_widget(child, x, y);
891 if (target)
892 return target;
893 }
894
895 if (widget->allocation.x <= x &&
896 x < widget->allocation.x + widget->allocation.width &&
897 widget->allocation.y <= y &&
898 y < widget->allocation.y + widget->allocation.height) {
899 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400900 }
901
902 return NULL;
903}
904
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500905static struct widget *
906widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400907{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500908 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400909
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500910 widget = malloc(sizeof *widget);
911 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500912 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500913 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500914 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500915 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500916 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300917 widget->tooltip = NULL;
918 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500919
920 return widget;
921}
922
923struct widget *
924window_add_widget(struct window *window, void *data)
925{
926 window->widget = widget_create(window, data);
927 wl_list_init(&window->widget->link);
928
929 return window->widget;
930}
931
932struct widget *
933widget_add_widget(struct widget *parent, void *data)
934{
935 struct widget *widget;
936
937 widget = widget_create(parent->window, data);
938 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400939
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500940 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400941}
942
943void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500944widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400945{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200946 struct display *display = widget->window->display;
947 struct input *input;
948
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300949 if (widget->tooltip) {
950 free(widget->tooltip);
951 widget->tooltip = NULL;
952 }
953
Pekka Paalanene156fb62012-01-19 13:51:38 +0200954 wl_list_for_each(input, &display->input_list, link) {
955 if (input->focus_widget == widget)
956 input->focus_widget = NULL;
957 }
958
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500959 wl_list_remove(&widget->link);
960 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400961}
962
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400963void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500964widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400965{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500966 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400967}
968
969void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500970widget_set_size(struct widget *widget, int32_t width, int32_t height)
971{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500972 widget->allocation.width = width;
973 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500974}
975
976void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500977widget_set_allocation(struct widget *widget,
978 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400979{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500980 widget->allocation.x = x;
981 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200982 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400983}
984
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500985void
986widget_set_transparent(struct widget *widget, int transparent)
987{
988 widget->opaque = !transparent;
989}
990
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400991void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500992widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400993{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500994 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995}
996
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500997void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500998widget_set_resize_handler(struct widget *widget,
999 widget_resize_handler_t handler)
1000{
1001 widget->resize_handler = handler;
1002}
1003
1004void
1005widget_set_redraw_handler(struct widget *widget,
1006 widget_redraw_handler_t handler)
1007{
1008 widget->redraw_handler = handler;
1009}
1010
1011void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001012widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001013{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001014 widget->enter_handler = handler;
1015}
1016
1017void
1018widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1019{
1020 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001021}
1022
1023void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001024widget_set_motion_handler(struct widget *widget,
1025 widget_motion_handler_t handler)
1026{
1027 widget->motion_handler = handler;
1028}
1029
1030void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001031widget_set_button_handler(struct widget *widget,
1032 widget_button_handler_t handler)
1033{
1034 widget->button_handler = handler;
1035}
1036
1037void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001038widget_schedule_redraw(struct widget *widget)
1039{
1040 window_schedule_redraw(widget->window);
1041}
1042
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001043cairo_surface_t *
1044window_get_surface(struct window *window)
1045{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001046 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001047}
1048
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001049struct wl_surface *
1050window_get_wl_surface(struct window *window)
1051{
1052 return window->surface;
1053}
1054
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001055struct wl_shell_surface *
1056window_get_wl_shell_surface(struct window *window)
1057{
1058 return window->shell_surface;
1059}
1060
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001061static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001062tooltip_redraw_handler(struct widget *widget, void *data)
1063{
1064 cairo_t *cr;
1065 const int32_t r = 3;
1066 struct tooltip *tooltip = data;
1067 int32_t width, height;
1068 struct window *window = widget->window;
1069
1070 cr = cairo_create(window->cairo_surface);
1071 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1072 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1073 cairo_paint(cr);
1074
1075 width = window->allocation.width;
1076 height = window->allocation.height;
1077 rounded_rect(cr, 0, 0, width, height, r);
1078
1079 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1080 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1081 cairo_fill(cr);
1082
1083 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1084 cairo_move_to(cr, 10, 16);
1085 cairo_show_text(cr, tooltip->entry);
1086 cairo_destroy(cr);
1087}
1088
1089static cairo_text_extents_t
1090get_text_extents(struct tooltip *tooltip)
1091{
1092 struct window *window;
1093 cairo_t *cr;
1094 cairo_text_extents_t extents;
1095
1096 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1097 * created yet */
1098 window = tooltip->widget->window->parent;
1099 cr = cairo_create(window->cairo_surface);
1100 cairo_text_extents(cr, tooltip->entry, &extents);
1101 cairo_destroy(cr);
1102
1103 return extents;
1104}
1105
1106static int
1107window_create_tooltip(struct tooltip *tooltip)
1108{
1109 struct widget *parent = tooltip->parent;
1110 struct display *display = parent->window->display;
1111 struct window *window;
1112 const int offset_y = 27;
1113 const int margin = 3;
1114 cairo_text_extents_t extents;
1115
1116 if (tooltip->widget)
1117 return 0;
1118
1119 window = window_create_transient(display, parent->window, tooltip->x,
1120 tooltip->y + offset_y,
1121 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1122 if (!window)
1123 return -1;
1124
1125 tooltip->window = window;
1126 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1127
1128 extents = get_text_extents(tooltip);
1129 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1130 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1131
1132 return 0;
1133}
1134
1135void
1136widget_destroy_tooltip(struct widget *parent)
1137{
1138 struct tooltip *tooltip = parent->tooltip;
1139
1140 parent->tooltip_count = 0;
1141 if (!tooltip)
1142 return;
1143
1144 if (tooltip->widget) {
1145 widget_destroy(tooltip->widget);
1146 window_destroy(tooltip->window);
1147 tooltip->widget = NULL;
1148 tooltip->window = NULL;
1149 }
1150
1151 close(tooltip->tooltip_fd);
1152 free(tooltip->entry);
1153 free(tooltip);
1154 parent->tooltip = NULL;
1155}
1156
1157static void
1158tooltip_func(struct task *task, uint32_t events)
1159{
1160 struct tooltip *tooltip =
1161 container_of(task, struct tooltip, tooltip_task);
1162 uint64_t exp;
1163
1164 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1165 window_create_tooltip(tooltip);
1166}
1167
1168#define TOOLTIP_TIMEOUT 500
1169static int
1170tooltip_timer_reset(struct tooltip *tooltip)
1171{
1172 struct itimerspec its;
1173
1174 its.it_interval.tv_sec = 0;
1175 its.it_interval.tv_nsec = 0;
1176 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1177 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1178 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1179 fprintf(stderr, "could not set timerfd\n: %m");
1180 return -1;
1181 }
1182
1183 return 0;
1184}
1185
1186int
1187widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1188{
1189 struct tooltip *tooltip = parent->tooltip;
1190
1191 parent->tooltip_count++;
1192 if (tooltip) {
1193 tooltip->x = x;
1194 tooltip->y = y;
1195 tooltip_timer_reset(tooltip);
1196 return 0;
1197 }
1198
1199 /* the handler might be triggered too fast via input device motion, so
1200 * we need this check here to make sure tooltip is fully initialized */
1201 if (parent->tooltip_count > 1)
1202 return 0;
1203
1204 tooltip = malloc(sizeof *tooltip);
1205 if (!tooltip)
1206 return -1;
1207
1208 parent->tooltip = tooltip;
1209 tooltip->parent = parent;
1210 tooltip->widget = NULL;
1211 tooltip->window = NULL;
1212 tooltip->x = x;
1213 tooltip->y = y;
1214 tooltip->entry = strdup(entry);
1215 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1216 if (tooltip->tooltip_fd < 0) {
1217 fprintf(stderr, "could not create timerfd\n: %m");
1218 return -1;
1219 }
1220
1221 tooltip->tooltip_task.run = tooltip_func;
1222 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1223 EPOLLIN, &tooltip->tooltip_task);
1224 tooltip_timer_reset(tooltip);
1225
1226 return 0;
1227}
1228
1229static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001230frame_resize_handler(struct widget *widget,
1231 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001233 struct frame *frame = data;
1234 struct widget *child = frame->child;
1235 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001236 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001237 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001238 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001239 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001240 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001241 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001243 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001244 decoration_width = (t->width + t->margin) * 2;
1245 decoration_height = t->width +
1246 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001247
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001248 allocation.x = t->width + t->margin;
1249 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001250 allocation.width = width - decoration_width;
1251 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001252
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001253 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001254
1255 wl_list_for_each(button, &frame->buttons_list, link)
1256 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001257 } else {
1258 decoration_width = 0;
1259 decoration_height = 0;
1260
1261 allocation.x = 0;
1262 allocation.y = 0;
1263 allocation.width = width;
1264 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001265 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001266
1267 wl_list_for_each(button, &frame->buttons_list, link)
1268 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001269 }
1270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001271 widget_set_allocation(child, allocation.x, allocation.y,
1272 allocation.width, allocation.height);
1273
1274 if (child->resize_handler)
1275 child->resize_handler(child,
1276 allocation.width,
1277 allocation.height,
1278 child->user_data);
1279
Scott Moreauf7e498c2012-05-14 11:39:29 -06001280 width = child->allocation.width + decoration_width;
1281 height = child->allocation.height + decoration_height;
1282
1283 widget->window->input_region =
1284 wl_compositor_create_region(display->compositor);
1285 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001286 t->margin, t->margin,
1287 width - 2 * t->margin,
1288 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001289
1290 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001291
1292 if (child->opaque) {
1293 widget->window->opaque_region =
1294 wl_compositor_create_region(display->compositor);
1295 wl_region_add(widget->window->opaque_region,
1296 opaque_margin, opaque_margin,
1297 widget->allocation.width - 2 * opaque_margin,
1298 widget->allocation.height - 2 * opaque_margin);
1299 }
Martin Minarik1998b152012-05-10 02:04:35 +02001300
1301 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001302 x_r = frame->widget->allocation.width - t->width - t->margin;
1303 x_l = t->width + t->margin;
1304 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001305 wl_list_for_each(button, &frame->buttons_list, link) {
1306 const int button_padding = 4;
1307 w = cairo_image_surface_get_width(button->icon);
1308 h = cairo_image_surface_get_height(button->icon);
1309
1310 if (button->decoration == FRAME_BUTTON_FANCY)
1311 w += 10;
1312
1313 if (button->align == FRAME_BUTTON_LEFT) {
1314 widget_set_allocation(button->widget,
1315 x_l, y , w + 1, h + 1);
1316 x_l += w;
1317 x_l += button_padding;
1318 } else {
1319 x_r -= w;
1320 widget_set_allocation(button->widget,
1321 x_r, y , w + 1, h + 1);
1322 x_r -= button_padding;
1323 }
1324 }
1325}
1326
1327static int
1328frame_button_enter_handler(struct widget *widget,
1329 struct input *input, float x, float y, void *data)
1330{
1331 struct frame_button *frame_button = data;
1332
1333 widget_schedule_redraw(frame_button->widget);
1334 frame_button->state = FRAME_BUTTON_OVER;
1335
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001336 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001337}
1338
1339static void
1340frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1341{
1342 struct frame_button *frame_button = data;
1343
1344 widget_schedule_redraw(frame_button->widget);
1345 frame_button->state = FRAME_BUTTON_DEFAULT;
1346}
1347
1348static void
1349frame_button_button_handler(struct widget *widget,
1350 struct input *input, uint32_t time,
1351 uint32_t button, uint32_t state, void *data)
1352{
1353 struct frame_button *frame_button = data;
1354 struct window *window = widget->window;
1355
1356 if (button != BTN_LEFT)
1357 return;
1358
1359 switch (state) {
1360 case 1:
1361 frame_button->state = FRAME_BUTTON_ACTIVE;
1362 widget_schedule_redraw(frame_button->widget);
1363
1364 if (frame_button->type == FRAME_BUTTON_ICON)
1365 window_show_frame_menu(window, input, time);
1366 return;
1367 case 0:
1368 frame_button->state = FRAME_BUTTON_DEFAULT;
1369 widget_schedule_redraw(frame_button->widget);
1370 break;
1371 }
1372
1373 switch (frame_button->type) {
1374 case FRAME_BUTTON_CLOSE:
1375 if (window->close_handler)
1376 window->close_handler(window->parent,
1377 window->user_data);
1378 else
1379 display_exit(window->display);
1380 break;
1381 case FRAME_BUTTON_MINIMIZE:
1382 fprintf(stderr,"Minimize stub\n");
1383 break;
1384 case FRAME_BUTTON_MAXIMIZE:
1385 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1386 break;
1387 default:
1388 /* Unknown operation */
1389 break;
1390 }
1391}
1392
1393static void
1394frame_button_redraw_handler(struct widget *widget, void *data)
1395{
1396 struct frame_button *frame_button = data;
1397 cairo_t *cr;
1398 int width, height, x, y;
1399 struct window *window = widget->window;
1400
1401 x = widget->allocation.x;
1402 y = widget->allocation.y;
1403 width = widget->allocation.width;
1404 height = widget->allocation.height;
1405
1406 if (!width)
1407 return;
1408 if (!height)
1409 return;
1410 if (widget->opaque)
1411 return;
1412
1413 cr = cairo_create(window->cairo_surface);
1414
1415 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1416 cairo_set_line_width(cr, 1);
1417
1418 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1419 cairo_rectangle (cr, x, y, 25, 16);
1420
1421 cairo_stroke_preserve(cr);
1422
1423 switch (frame_button->state) {
1424 case FRAME_BUTTON_DEFAULT:
1425 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1426 break;
1427 case FRAME_BUTTON_OVER:
1428 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1429 break;
1430 case FRAME_BUTTON_ACTIVE:
1431 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1432 break;
1433 }
1434
1435 cairo_fill (cr);
1436
1437 x += 4;
1438 }
1439
1440 cairo_set_source_surface(cr, frame_button->icon, x, y);
1441 cairo_paint(cr);
1442
1443 cairo_destroy(cr);
1444}
1445
1446static struct widget *
1447frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1448 enum frame_button_align align, enum frame_button_decoration style)
1449{
1450 struct frame_button *frame_button;
1451 const char *icon = data;
1452
1453 frame_button = malloc (sizeof *frame_button);
1454 memset(frame_button, 0, sizeof *frame_button);
1455
1456 frame_button->icon = cairo_image_surface_create_from_png(icon);
1457 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1458 frame_button->frame = frame;
1459 frame_button->type = type;
1460 frame_button->align = align;
1461 frame_button->decoration = style;
1462
1463 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1464
1465 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1466 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1467 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1468 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1469 return frame_button->widget;
1470}
1471
1472static void
1473frame_button_destroy(struct frame_button *frame_button)
1474{
1475 widget_destroy(frame_button->widget);
1476 wl_list_remove(&frame_button->link);
1477 cairo_surface_destroy(frame_button->icon);
1478 free(frame_button);
1479
1480 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001481}
1482
1483static void
1484frame_redraw_handler(struct widget *widget, void *data)
1485{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001486 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001487 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001488 struct theme *t = window->display->theme;
1489 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001490
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001491 if (window->type == TYPE_FULLSCREEN)
1492 return;
1493
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001494 cr = cairo_create(window->cairo_surface);
1495
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001496 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001497 flags |= THEME_FRAME_ACTIVE;
1498 theme_render_frame(t, cr, widget->allocation.width,
1499 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001500
1501 cairo_destroy(cr);
1502}
1503
1504static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001505frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001506{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001507 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001508 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001509
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001510 location = theme_get_location(t, input->sx, input->sy,
1511 frame->widget->allocation.width,
1512 frame->widget->allocation.height);
1513
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001514 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001515 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001516 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001517 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001518 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001519 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001520 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001521 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001522 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001523 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001524 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001525 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001526 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001527 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001528 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001529 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001530 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001531 case THEME_LOCATION_EXTERIOR:
1532 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001533 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001534 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001535 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001536}
1537
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001538static void
1539frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001540{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001541 switch (index) {
1542 case 0: /* close */
1543 if (window->close_handler)
1544 window->close_handler(window->parent,
1545 window->user_data);
1546 else
1547 display_exit(window->display);
1548 break;
1549 case 1: /* fullscreen */
1550 /* we don't have a way to get out of fullscreen for now */
1551 window_set_fullscreen(window, 1);
1552 break;
1553 case 2: /* rotate */
1554 case 3: /* scale */
1555 break;
1556 }
1557}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001558
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001559void
1560window_show_frame_menu(struct window *window,
1561 struct input *input, uint32_t time)
1562{
1563 int32_t x, y;
1564
1565 static const char *entries[] = {
1566 "Close", "Fullscreen", "Rotate", "Scale"
1567 };
1568
1569 input_get_position(input, &x, &y);
1570 window_show_menu(window->display, input, time, window,
1571 x - 10, y - 10, frame_menu_func, entries, 4);
1572}
1573
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001574static int
1575frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001576 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001577{
1578 return frame_get_pointer_image_for_location(data, input);
1579}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001580
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001581static int
1582frame_motion_handler(struct widget *widget,
1583 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001584 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001585{
1586 return frame_get_pointer_image_for_location(data, input);
1587}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001588
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001589static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001590frame_button_handler(struct widget *widget,
1591 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001592 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593
1594{
1595 struct frame *frame = data;
1596 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001597 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001598 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001599
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001600 location = theme_get_location(display->theme, input->sx, input->sy,
1601 frame->widget->allocation.width,
1602 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603
1604 if (window->display->shell && button == BTN_LEFT && state == 1) {
1605 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001606 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001607 if (!window->shell_surface)
1608 break;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001609 input_set_pointer_image(input, time, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001610 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001611 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001612 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001613 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001615 case THEME_LOCATION_RESIZING_TOP:
1616 case THEME_LOCATION_RESIZING_BOTTOM:
1617 case THEME_LOCATION_RESIZING_LEFT:
1618 case THEME_LOCATION_RESIZING_RIGHT:
1619 case THEME_LOCATION_RESIZING_TOP_LEFT:
1620 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1621 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1622 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623 if (!window->shell_surface)
1624 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001625 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001626
1627 if (!display->dpy) {
1628 /* If we're using shm, allocate a big
1629 pool to create buffers out of while
1630 we resize. We should probably base
1631 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001632 window->pool =
1633 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001634 }
1635
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001636 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001637 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001638 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001639 break;
1640 }
1641 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001642 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001643 }
1644}
1645
1646struct widget *
1647frame_create(struct window *window, void *data)
1648{
1649 struct frame *frame;
1650
1651 frame = malloc(sizeof *frame);
1652 memset(frame, 0, sizeof *frame);
1653
1654 frame->widget = window_add_widget(window, frame);
1655 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001656
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001657 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1658 widget_set_resize_handler(frame->widget, frame_resize_handler);
1659 widget_set_enter_handler(frame->widget, frame_enter_handler);
1660 widget_set_motion_handler(frame->widget, frame_motion_handler);
1661 widget_set_button_handler(frame->widget, frame_button_handler);
1662
Martin Minarik1998b152012-05-10 02:04:35 +02001663 /* Create empty list for frame buttons */
1664 wl_list_init(&frame->buttons_list);
1665
1666 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1667 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1668
1669 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1670 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1671
1672 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1673 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1674
1675 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1676 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1677
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001678 window->frame = frame;
1679
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001680 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001681}
1682
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001683static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001684frame_destroy(struct frame *frame)
1685{
Martin Minarik1998b152012-05-10 02:04:35 +02001686 struct frame_button *button, *tmp;
1687
1688 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1689 frame_button_destroy(button);
1690
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001691 /* frame->child must be destroyed by the application */
1692 widget_destroy(frame->widget);
1693 free(frame);
1694}
1695
1696static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001697input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001698 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001699{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001700 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001701 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001702
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001703 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001704 return;
1705
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001706 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001707 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001708 widget = old;
1709 if (input->grab)
1710 widget = input->grab;
1711 if (widget->leave_handler)
1712 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001713 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001714 }
1715
1716 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001717 widget = focus;
1718 if (input->grab)
1719 widget = input->grab;
1720 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001721 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001722 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001723 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001724
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001725 input_set_pointer_image(input, input->pointer_enter_serial,
1726 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001727 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001728}
1729
1730static void
Daniel Stone37816df2012-05-16 18:45:18 +01001731pointer_handle_motion(void *data, struct wl_pointer *pointer,
1732 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001733{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001734 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001735 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001736 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001737 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001738 float sx = wl_fixed_to_double(sx_w);
1739 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001740
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001741 input->sx = sx;
1742 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001743
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001744 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001745 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001746 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001747 }
1748
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001749 if (input->grab)
1750 widget = input->grab;
1751 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001752 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001753 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001754 cursor = widget->motion_handler(input->focus_widget,
1755 input, time, sx, sy,
1756 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001757
Daniel Stone37816df2012-05-16 18:45:18 +01001758 input_set_pointer_image(input, time, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001759}
1760
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001761void
1762input_grab(struct input *input, struct widget *widget, uint32_t button)
1763{
1764 input->grab = widget;
1765 input->grab_button = button;
1766}
1767
1768void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001769input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001770{
1771 struct widget *widget;
1772
1773 input->grab = NULL;
1774 if (input->pointer_focus) {
1775 widget = widget_find_widget(input->pointer_focus->widget,
1776 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001777 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001778 }
1779}
1780
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001781static void
Daniel Stone37816df2012-05-16 18:45:18 +01001782pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1783 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001784{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001785 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001786 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001787
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001788 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001789 if (input->focus_widget && input->grab == NULL && state)
1790 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001791
Neil Roberts6b28aad2012-01-23 19:11:18 +00001792 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001793 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001794 (*widget->button_handler)(widget,
1795 input, time,
1796 button, state,
1797 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001798
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001799 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001800 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001801}
1802
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001803static void
Daniel Stone37816df2012-05-16 18:45:18 +01001804pointer_handle_axis(void *data, struct wl_pointer *pointer,
Scott Moreau210d0792012-03-22 10:47:01 -06001805 uint32_t time, uint32_t axis, int32_t value)
1806{
1807}
1808
1809static void
Daniel Stone37816df2012-05-16 18:45:18 +01001810keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1811 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001812{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001813 struct input *input = data;
1814 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001815 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001816 uint32_t code, num_syms;
1817 const xkb_keysym_t *syms;
1818 xkb_keysym_t sym;
1819 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001820
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001821 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001822 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001823 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001824 return;
1825
Kristian Høgsberg70163132012-05-08 15:55:39 -04001826 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1827 xkb_state_update_key(d->xkb.state, code,
1828 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001829
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001830 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001831 XKB_STATE_DEPRESSED |
1832 XKB_STATE_LATCHED);
1833 input->modifiers = 0;
1834 if (mask & input->display->xkb.control_mask)
1835 input->modifiers |= MOD_CONTROL_MASK;
1836 if (mask & input->display->xkb.alt_mask)
1837 input->modifiers |= MOD_ALT_MASK;
1838 if (mask & input->display->xkb.shift_mask)
1839 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001840
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001841 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001842 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001843 if (state)
1844 window_set_maximized(window,
1845 window->type != TYPE_MAXIMIZED);
1846 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001847 if (num_syms == 1)
1848 sym = syms[0];
1849 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001850 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001851
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001852 (*window->key_handler)(window, input, time, key,
1853 sym, state, window->user_data);
1854 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001855}
1856
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001857static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001858input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001859{
1860 struct window *window = input->pointer_focus;
1861
1862 if (!window)
1863 return;
1864
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001865 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001866
Pekka Paalanene1207c72011-12-16 12:02:09 +02001867 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001868 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001869}
1870
1871static void
Daniel Stone37816df2012-05-16 18:45:18 +01001872pointer_handle_enter(void *data, struct wl_pointer *pointer,
1873 uint32_t serial, struct wl_surface *surface,
1874 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001875{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001876 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001877 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001878 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001879 float sx = wl_fixed_to_double(sx_w);
1880 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001881
Martin Minarik1998b152012-05-10 02:04:35 +02001882 if (!surface) {
1883 /* enter event for a window we've just destroyed */
1884 return;
1885 }
1886
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001887 input->display->serial = serial;
1888 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001889 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001890 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001891
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001892 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001893 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001894 window->pool = NULL;
1895 /* Schedule a redraw to free the pool */
1896 window_schedule_redraw(window);
1897 }
1898
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001899 input->sx = sx;
1900 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001901
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001902 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001903 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001904}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001905
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001906static void
Daniel Stone37816df2012-05-16 18:45:18 +01001907pointer_handle_leave(void *data, struct wl_pointer *pointer,
1908 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001909{
1910 struct input *input = data;
1911
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001912 input->display->serial = serial;
1913 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001914}
1915
1916static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001917input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001918{
1919 struct window *window = input->keyboard_focus;
1920
1921 if (!window)
1922 return;
1923
1924 window->keyboard_device = NULL;
1925 if (window->keyboard_focus_handler)
1926 (*window->keyboard_focus_handler)(window, NULL,
1927 window->user_data);
1928
1929 input->keyboard_focus = NULL;
1930}
1931
1932static void
Daniel Stone37816df2012-05-16 18:45:18 +01001933keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1934 uint32_t serial, struct wl_surface *surface,
1935 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001936{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001937 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001938 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001939
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001940 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001941 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001942
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001943 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001944 window->keyboard_device = input;
1945 if (window->keyboard_focus_handler)
1946 (*window->keyboard_focus_handler)(window,
1947 window->keyboard_device,
1948 window->user_data);
1949}
1950
1951static void
Daniel Stone37816df2012-05-16 18:45:18 +01001952keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1953 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001954{
1955 struct input *input = data;
1956
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001957 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001958 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001959}
1960
Daniel Stone37816df2012-05-16 18:45:18 +01001961static const struct wl_pointer_listener pointer_listener = {
1962 pointer_handle_enter,
1963 pointer_handle_leave,
1964 pointer_handle_motion,
1965 pointer_handle_button,
1966 pointer_handle_axis,
1967};
1968
1969static const struct wl_keyboard_listener keyboard_listener = {
1970 keyboard_handle_enter,
1971 keyboard_handle_leave,
1972 keyboard_handle_key,
1973};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001974
1975static void
Daniel Stone37816df2012-05-16 18:45:18 +01001976seat_handle_capabilities(void *data, struct wl_seat *seat,
1977 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001978{
Daniel Stone37816df2012-05-16 18:45:18 +01001979 struct input *input = data;
1980
1981 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1982 input->pointer = wl_seat_get_pointer(seat);
1983 wl_pointer_set_user_data(input->pointer, input);
1984 wl_pointer_add_listener(input->pointer, &pointer_listener,
1985 input);
1986 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1987 wl_pointer_destroy(input->pointer);
1988 input->pointer = NULL;
1989 }
1990
1991 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1992 input->keyboard = wl_seat_get_keyboard(seat);
1993 wl_keyboard_set_user_data(input->keyboard, input);
1994 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
1995 input);
1996 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
1997 wl_keyboard_destroy(input->keyboard);
1998 input->keyboard = NULL;
1999 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002000}
2001
Daniel Stone37816df2012-05-16 18:45:18 +01002002static const struct wl_seat_listener seat_listener = {
2003 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002004};
2005
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002006void
2007input_get_position(struct input *input, int32_t *x, int32_t *y)
2008{
2009 *x = input->sx;
2010 *y = input->sy;
2011}
2012
Daniel Stone37816df2012-05-16 18:45:18 +01002013struct wl_seat *
2014input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002015{
Daniel Stone37816df2012-05-16 18:45:18 +01002016 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002017}
2018
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002019uint32_t
2020input_get_modifiers(struct input *input)
2021{
2022 return input->modifiers;
2023}
2024
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002025struct widget *
2026input_get_focus_widget(struct input *input)
2027{
2028 return input->focus_widget;
2029}
2030
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002031struct data_offer {
2032 struct wl_data_offer *offer;
2033 struct input *input;
2034 struct wl_array types;
2035 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002036
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002037 struct task io_task;
2038 int fd;
2039 data_func_t func;
2040 int32_t x, y;
2041 void *user_data;
2042};
2043
2044static void
2045data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2046{
2047 struct data_offer *offer = data;
2048 char **p;
2049
2050 p = wl_array_add(&offer->types, sizeof *p);
2051 *p = strdup(type);
2052}
2053
2054static const struct wl_data_offer_listener data_offer_listener = {
2055 data_offer_offer,
2056};
2057
2058static void
2059data_offer_destroy(struct data_offer *offer)
2060{
2061 char **p;
2062
2063 offer->refcount--;
2064 if (offer->refcount == 0) {
2065 wl_data_offer_destroy(offer->offer);
2066 for (p = offer->types.data; *p; p++)
2067 free(*p);
2068 wl_array_release(&offer->types);
2069 free(offer);
2070 }
2071}
2072
2073static void
2074data_device_data_offer(void *data,
2075 struct wl_data_device *data_device, uint32_t id)
2076{
2077 struct data_offer *offer;
2078
2079 offer = malloc(sizeof *offer);
2080
2081 wl_array_init(&offer->types);
2082 offer->refcount = 1;
2083 offer->input = data;
2084
2085 /* FIXME: Generate typesafe wrappers for this */
2086 offer->offer = (struct wl_data_offer *)
2087 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2088 id, &wl_data_offer_interface);
2089
2090 wl_data_offer_add_listener(offer->offer,
2091 &data_offer_listener, offer);
2092}
2093
2094static void
2095data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002096 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002097 wl_fixed_t x_w, wl_fixed_t y_w,
2098 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002099{
2100 struct input *input = data;
2101 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002102 float x = wl_fixed_to_double(x_w);
2103 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002104 char **p;
2105
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002106 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002107 input->drag_offer = wl_data_offer_get_user_data(offer);
2108 window = wl_surface_get_user_data(surface);
2109 input->pointer_focus = window;
2110
2111 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2112 *p = NULL;
2113
2114 window = input->pointer_focus;
2115 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002116 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002117 input->drag_offer->types.data,
2118 window->user_data);
2119}
2120
2121static void
2122data_device_leave(void *data, struct wl_data_device *data_device)
2123{
2124 struct input *input = data;
2125
2126 data_offer_destroy(input->drag_offer);
2127 input->drag_offer = NULL;
2128}
2129
2130static void
2131data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002132 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002133{
2134 struct input *input = data;
2135 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002136 float x = wl_fixed_to_double(x_w);
2137 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002138
2139 input->sx = x;
2140 input->sy = y;
2141
2142 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002143 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002144 input->drag_offer->types.data,
2145 window->user_data);
2146}
2147
2148static void
2149data_device_drop(void *data, struct wl_data_device *data_device)
2150{
2151 struct input *input = data;
2152 struct window *window = input->pointer_focus;
2153
2154 if (window->drop_handler)
2155 window->drop_handler(window, input,
2156 input->sx, input->sy, window->user_data);
2157}
2158
2159static void
2160data_device_selection(void *data,
2161 struct wl_data_device *wl_data_device,
2162 struct wl_data_offer *offer)
2163{
2164 struct input *input = data;
2165 char **p;
2166
2167 if (input->selection_offer)
2168 data_offer_destroy(input->selection_offer);
2169
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002170 if (offer) {
2171 input->selection_offer = wl_data_offer_get_user_data(offer);
2172 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2173 *p = NULL;
2174 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002175}
2176
2177static const struct wl_data_device_listener data_device_listener = {
2178 data_device_data_offer,
2179 data_device_enter,
2180 data_device_leave,
2181 data_device_motion,
2182 data_device_drop,
2183 data_device_selection
2184};
2185
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002186void
2187input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2188{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002189 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002190 struct wl_cursor *cursor;
2191 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002193 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194 return;
2195
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002196 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002197 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002198 return;
2199
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002200 image = cursor->images[0];
2201 buffer = wl_cursor_image_get_buffer(image);
2202 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002203 return;
2204
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002205 input->current_cursor = pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002206 wl_pointer_attach(input->pointer, time, buffer,
2207 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002208}
2209
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002210struct wl_data_device *
2211input_get_data_device(struct input *input)
2212{
2213 return input->data_device;
2214}
2215
2216void
2217input_set_selection(struct input *input,
2218 struct wl_data_source *source, uint32_t time)
2219{
2220 wl_data_device_set_selection(input->data_device, source, time);
2221}
2222
2223void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002224input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002225{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002226 wl_data_offer_accept(input->drag_offer->offer,
2227 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002228}
2229
2230static void
2231offer_io_func(struct task *task, uint32_t events)
2232{
2233 struct data_offer *offer =
2234 container_of(task, struct data_offer, io_task);
2235 unsigned int len;
2236 char buffer[4096];
2237
2238 len = read(offer->fd, buffer, sizeof buffer);
2239 offer->func(buffer, len,
2240 offer->x, offer->y, offer->user_data);
2241
2242 if (len == 0) {
2243 close(offer->fd);
2244 data_offer_destroy(offer);
2245 }
2246}
2247
2248static void
2249data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2250 data_func_t func, void *user_data)
2251{
2252 int p[2];
2253
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002254 if (pipe2(p, O_CLOEXEC) == -1)
2255 return;
2256
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002257 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2258 close(p[1]);
2259
2260 offer->io_task.run = offer_io_func;
2261 offer->fd = p[0];
2262 offer->func = func;
2263 offer->refcount++;
2264 offer->user_data = user_data;
2265
2266 display_watch_fd(offer->input->display,
2267 offer->fd, EPOLLIN, &offer->io_task);
2268}
2269
2270void
2271input_receive_drag_data(struct input *input, const char *mime_type,
2272 data_func_t func, void *data)
2273{
2274 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2275 input->drag_offer->x = input->sx;
2276 input->drag_offer->y = input->sy;
2277}
2278
2279int
2280input_receive_selection_data(struct input *input, const char *mime_type,
2281 data_func_t func, void *data)
2282{
2283 char **p;
2284
2285 if (input->selection_offer == NULL)
2286 return -1;
2287
2288 for (p = input->selection_offer->types.data; *p; p++)
2289 if (strcmp(mime_type, *p) == 0)
2290 break;
2291
2292 if (*p == NULL)
2293 return -1;
2294
2295 data_offer_receive_data(input->selection_offer,
2296 mime_type, func, data);
2297 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002298}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002299
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002300int
2301input_receive_selection_data_to_fd(struct input *input,
2302 const char *mime_type, int fd)
2303{
2304 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2305
2306 return 0;
2307}
2308
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002309void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002310window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002311{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002312 if (!window->shell_surface)
2313 return;
2314
Daniel Stone37816df2012-05-16 18:45:18 +01002315 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002316}
2317
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002318static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002319idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002320{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002321 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002322
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002323 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002324 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002325 widget_set_allocation(widget,
2326 window->pending_allocation.x,
2327 window->pending_allocation.y,
2328 window->pending_allocation.width,
2329 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002330
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002331 if (window->input_region) {
2332 wl_region_destroy(window->input_region);
2333 window->input_region = NULL;
2334 }
2335
2336 if (window->opaque_region) {
2337 wl_region_destroy(window->opaque_region);
2338 window->opaque_region = NULL;
2339 }
2340
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002341 if (widget->resize_handler)
2342 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002343 widget->allocation.width,
2344 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002345 widget->user_data);
2346
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002347 if (window->allocation.width != widget->allocation.width ||
2348 window->allocation.height != widget->allocation.height) {
2349 window->allocation = widget->allocation;
2350 window_schedule_redraw(window);
2351 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002352}
2353
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002354void
2355window_schedule_resize(struct window *window, int width, int height)
2356{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002357 window->pending_allocation.x = 0;
2358 window->pending_allocation.y = 0;
2359 window->pending_allocation.width = width;
2360 window->pending_allocation.height = height;
2361
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002362 window->resize_needed = 1;
2363 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002364}
2365
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002366void
2367widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2368{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002369 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002370}
2371
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002372static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002373handle_ping(void *data, struct wl_shell_surface *shell_surface,
2374 uint32_t serial)
2375{
2376 wl_shell_surface_pong(shell_surface, serial);
2377}
2378
2379static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002380handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002381 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002382{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002383 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002384
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002385 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002386 return;
2387
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002388 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002389 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002390}
2391
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002392static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002393menu_destroy(struct menu *menu)
2394{
2395 widget_destroy(menu->widget);
2396 window_destroy(menu->window);
2397 free(menu);
2398}
2399
2400static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002401handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2402{
2403 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002404 struct menu *menu = window->widget->user_data;
2405
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002406 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002407 * device. Or just use wl_callback. And this really needs to
2408 * be a window vfunc that the menu can set. And we need the
2409 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002410
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002411 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002412 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002413 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002414}
2415
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002416static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002417 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002418 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002419 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002420};
2421
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002422void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002423window_get_allocation(struct window *window,
2424 struct rectangle *allocation)
2425{
2426 *allocation = window->allocation;
2427}
2428
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002429static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002430widget_redraw(struct widget *widget)
2431{
2432 struct widget *child;
2433
2434 if (widget->redraw_handler)
2435 widget->redraw_handler(widget, widget->user_data);
2436 wl_list_for_each(child, &widget->child_list, link)
2437 widget_redraw(child);
2438}
2439
2440static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002441frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2442{
2443 struct window *window = data;
2444
2445 wl_callback_destroy(callback);
2446 window->redraw_scheduled = 0;
2447 if (window->redraw_needed)
2448 window_schedule_redraw(window);
2449}
2450
2451static const struct wl_callback_listener listener = {
2452 frame_callback
2453};
2454
2455static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002456idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002457{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002458 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002459 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002460
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002461 if (window->resize_needed)
2462 idle_resize(window);
2463
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002464 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002465 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002466 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002467 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002468 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002469
2470 callback = wl_surface_frame(window->surface);
2471 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002472}
2473
2474void
2475window_schedule_redraw(struct window *window)
2476{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002477 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002478 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002479 window->redraw_task.run = idle_redraw;
2480 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002481 window->redraw_scheduled = 1;
2482 }
2483}
2484
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002485void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002486window_set_custom(struct window *window)
2487{
2488 window->type = TYPE_CUSTOM;
2489}
2490
2491void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002492window_set_fullscreen(struct window *window, int fullscreen)
2493{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002494 if (!window->display->shell)
2495 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002496
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002497 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002498 return;
2499
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002500 if (fullscreen) {
2501 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002502 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002503 wl_shell_surface_set_fullscreen(window->shell_surface,
2504 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2505 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002506 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002507 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002508 wl_shell_surface_set_toplevel(window->shell_surface);
2509 window_schedule_resize(window,
2510 window->saved_allocation.width,
2511 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002512 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002513}
2514
2515void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002516window_set_maximized(struct window *window, int maximized)
2517{
2518 if (!window->display->shell)
2519 return;
2520
2521 if ((window->type == TYPE_MAXIMIZED) == maximized)
2522 return;
2523
2524 if (window->type == TYPE_TOPLEVEL) {
2525 window->saved_allocation = window->allocation;
2526 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2527 window->type = TYPE_MAXIMIZED;
2528 } else {
2529 wl_shell_surface_set_toplevel(window->shell_surface);
2530 window->type = TYPE_TOPLEVEL;
2531 window_schedule_resize(window,
2532 window->saved_allocation.width,
2533 window->saved_allocation.height);
2534 }
2535}
2536
2537void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002538window_set_user_data(struct window *window, void *data)
2539{
2540 window->user_data = data;
2541}
2542
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002543void *
2544window_get_user_data(struct window *window)
2545{
2546 return window->user_data;
2547}
2548
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002549void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002550window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002551 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002552{
2553 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002554}
2555
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002556void
2557window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002558 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002559{
2560 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002561}
2562
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002563void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002564window_set_data_handler(struct window *window, window_data_handler_t handler)
2565{
2566 window->data_handler = handler;
2567}
2568
2569void
2570window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2571{
2572 window->drop_handler = handler;
2573}
2574
2575void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002576window_set_close_handler(struct window *window,
2577 window_close_handler_t handler)
2578{
2579 window->close_handler = handler;
2580}
2581
2582void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002583window_set_title(struct window *window, const char *title)
2584{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002585 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002586 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002587 if (window->shell_surface)
2588 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002589}
2590
2591const char *
2592window_get_title(struct window *window)
2593{
2594 return window->title;
2595}
2596
2597void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002598window_damage(struct window *window, int32_t x, int32_t y,
2599 int32_t width, int32_t height)
2600{
2601 wl_surface_damage(window->surface, x, y, width, height);
2602}
2603
Casey Dahlin9074db52012-04-19 22:50:09 -04002604static void
2605surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002606 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002607{
Rob Bradford7507b572012-05-15 17:55:34 +01002608 struct window *window = data;
2609 struct output *output;
2610 struct output *output_found = NULL;
2611 struct window_output *window_output;
2612
2613 wl_list_for_each(output, &window->display->output_list, link) {
2614 if (output->output == wl_output) {
2615 output_found = output;
2616 break;
2617 }
2618 }
2619
2620 if (!output_found)
2621 return;
2622
2623 window_output = malloc (sizeof *window_output);
2624 window_output->output = output_found;
2625
2626 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002627}
2628
2629static void
2630surface_leave(void *data,
2631 struct wl_surface *wl_surface, struct wl_output *output)
2632{
Rob Bradford7507b572012-05-15 17:55:34 +01002633 struct window *window = data;
2634 struct window_output *window_output;
2635 struct window_output *window_output_found = NULL;
2636
2637 wl_list_for_each(window_output, &window->window_output_list, link) {
2638 if (window_output->output->output == output) {
2639 window_output_found = window_output;
2640 break;
2641 }
2642 }
2643
2644 if (window_output_found) {
2645 wl_list_remove(&window_output_found->link);
2646 free(window_output_found);
2647 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002648}
2649
2650static const struct wl_surface_listener surface_listener = {
2651 surface_enter,
2652 surface_leave
2653};
2654
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002655static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002656window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002657{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002658 struct window *window;
2659
2660 window = malloc(sizeof *window);
2661 if (window == NULL)
2662 return NULL;
2663
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002664 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002665 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002666 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002667 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002668 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002669 if (display->shell) {
2670 window->shell_surface =
2671 wl_shell_get_shell_surface(display->shell,
2672 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002673 if (window->title)
2674 wl_shell_surface_set_title(window->shell_surface,
2675 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002676 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002677 window->allocation.x = 0;
2678 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002679 window->allocation.width = 0;
2680 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002681 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002682 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002683 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002684 window->input_region = NULL;
2685 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002686
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002687 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002688#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002689 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002690#else
2691 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2692#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002693 else
2694 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002695
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002696 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002697 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002698 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002699
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002700 if (window->shell_surface) {
2701 wl_shell_surface_set_user_data(window->shell_surface, window);
2702 wl_shell_surface_add_listener(window->shell_surface,
2703 &shell_surface_listener, window);
2704 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002705
Rob Bradford7507b572012-05-15 17:55:34 +01002706 wl_list_init (&window->window_output_list);
2707
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002708 return window;
2709}
2710
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002711struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002712window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002713{
2714 struct window *window;
2715
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002716 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002717 if (!window)
2718 return NULL;
2719
2720 return window;
2721}
2722
2723struct window *
2724window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002725 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002726{
2727 struct window *window;
2728
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002729 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002730 if (!window)
2731 return NULL;
2732
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002733 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002734 window->x = x;
2735 window->y = y;
2736
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002737 if (display->shell)
2738 wl_shell_surface_set_transient(window->shell_surface,
2739 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002740 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002741
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002742 return window;
2743}
2744
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002745static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002746menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002747{
2748 int next;
2749
2750 next = (sy - 8) / 20;
2751 if (menu->current != next) {
2752 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002753 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002754 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002755}
2756
2757static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002758menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002759 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002760 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002761{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002762 struct menu *menu = data;
2763
2764 if (widget == menu->widget)
2765 menu_set_item(data, y);
2766
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002767 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002768}
2769
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002770static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002771menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002772 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002773{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002774 struct menu *menu = data;
2775
2776 if (widget == menu->widget)
2777 menu_set_item(data, y);
2778
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002779 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002780}
2781
2782static void
2783menu_leave_handler(struct widget *widget, struct input *input, void *data)
2784{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002785 struct menu *menu = data;
2786
2787 if (widget == menu->widget)
2788 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002789}
2790
2791static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002792menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002793 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002794 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002795
2796{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002797 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002798
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002799 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002800 /* Either relase after press-drag-release or
2801 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002802 menu->func(menu->window->parent,
2803 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002804 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002805 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002806 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002807}
2808
2809static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002810menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002811{
2812 cairo_t *cr;
2813 const int32_t r = 3, margin = 3;
2814 struct menu *menu = data;
2815 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002816 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002817
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002818 cr = cairo_create(window->cairo_surface);
2819 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2820 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2821 cairo_paint(cr);
2822
2823 width = window->allocation.width;
2824 height = window->allocation.height;
2825 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002826
2827 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002828 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2829 cairo_fill(cr);
2830
2831 for (i = 0; i < menu->count; i++) {
2832 if (i == menu->current) {
2833 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2834 cairo_rectangle(cr, margin, i * 20 + margin,
2835 width - 2 * margin, 20);
2836 cairo_fill(cr);
2837 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2838 cairo_move_to(cr, 10, i * 20 + 16);
2839 cairo_show_text(cr, menu->entries[i]);
2840 } else {
2841 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2842 cairo_move_to(cr, 10, i * 20 + 16);
2843 cairo_show_text(cr, menu->entries[i]);
2844 }
2845 }
2846
2847 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002848}
2849
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002850void
2851window_show_menu(struct display *display,
2852 struct input *input, uint32_t time, struct window *parent,
2853 int32_t x, int32_t y,
2854 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002855{
2856 struct window *window;
2857 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002858 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002859
2860 menu = malloc(sizeof *menu);
2861 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002862 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002863
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002864 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002865 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002866 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002867
2868 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002869 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002870 menu->entries = entries;
2871 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002872 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002873 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002874 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002875 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002876 window->type = TYPE_MENU;
2877 window->x = x;
2878 window->y = y;
2879
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002880 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002881 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002882 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002883 window->parent->shell_surface,
2884 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002885
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002886 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002887 widget_set_enter_handler(menu->widget, menu_enter_handler);
2888 widget_set_leave_handler(menu->widget, menu_leave_handler);
2889 widget_set_motion_handler(menu->widget, menu_motion_handler);
2890 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002891
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002892 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002893 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002894}
2895
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002896void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002897window_set_buffer_type(struct window *window, enum window_buffer_type type)
2898{
2899 window->buffer_type = type;
2900}
2901
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002902
2903static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002904display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002905 struct wl_output *wl_output,
2906 int x, int y,
2907 int physical_width,
2908 int physical_height,
2909 int subpixel,
2910 const char *make,
2911 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002912{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002913 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002914
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002915 output->allocation.x = x;
2916 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002917}
2918
2919static void
2920display_handle_mode(void *data,
2921 struct wl_output *wl_output,
2922 uint32_t flags,
2923 int width,
2924 int height,
2925 int refresh)
2926{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002927 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002928 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002929
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002930 if (flags & WL_OUTPUT_MODE_CURRENT) {
2931 output->allocation.width = width;
2932 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002933 if (display->output_configure_handler)
2934 (*display->output_configure_handler)(
2935 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002936 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002937}
2938
2939static const struct wl_output_listener output_listener = {
2940 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002941 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002942};
2943
2944static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002945display_add_output(struct display *d, uint32_t id)
2946{
2947 struct output *output;
2948
2949 output = malloc(sizeof *output);
2950 if (output == NULL)
2951 return;
2952
2953 memset(output, 0, sizeof *output);
2954 output->display = d;
2955 output->output =
2956 wl_display_bind(d->display, id, &wl_output_interface);
2957 wl_list_insert(d->output_list.prev, &output->link);
2958
2959 wl_output_add_listener(output->output, &output_listener, output);
2960}
2961
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002962static void
2963output_destroy(struct output *output)
2964{
2965 if (output->destroy_handler)
2966 (*output->destroy_handler)(output, output->user_data);
2967
2968 wl_output_destroy(output->output);
2969 wl_list_remove(&output->link);
2970 free(output);
2971}
2972
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002973void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002974display_set_output_configure_handler(struct display *display,
2975 display_output_handler_t handler)
2976{
2977 struct output *output;
2978
2979 display->output_configure_handler = handler;
2980 if (!handler)
2981 return;
2982
2983 wl_list_for_each(output, &display->output_list, link)
2984 (*display->output_configure_handler)(output,
2985 display->user_data);
2986}
2987
2988void
2989output_set_user_data(struct output *output, void *data)
2990{
2991 output->user_data = data;
2992}
2993
2994void *
2995output_get_user_data(struct output *output)
2996{
2997 return output->user_data;
2998}
2999
3000void
3001output_set_destroy_handler(struct output *output,
3002 display_output_handler_t handler)
3003{
3004 output->destroy_handler = handler;
3005 /* FIXME: implement this, once we have way to remove outputs */
3006}
3007
3008void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003009output_get_allocation(struct output *output, struct rectangle *allocation)
3010{
3011 *allocation = output->allocation;
3012}
3013
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003014struct wl_output *
3015output_get_wl_output(struct output *output)
3016{
3017 return output->output;
3018}
3019
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003020static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003021display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003022{
3023 struct input *input;
3024
3025 input = malloc(sizeof *input);
3026 if (input == NULL)
3027 return;
3028
3029 memset(input, 0, sizeof *input);
3030 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003031 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003032 input->pointer_focus = NULL;
3033 input->keyboard_focus = NULL;
3034 wl_list_insert(d->input_list.prev, &input->link);
3035
Daniel Stone37816df2012-05-16 18:45:18 +01003036 wl_seat_add_listener(input->seat, &seat_listener, input);
3037 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003038
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003039 input->data_device =
3040 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003041 input->seat);
3042 wl_data_device_add_listener(input->data_device, &data_device_listener,
3043 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003044}
3045
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003046static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003047input_destroy(struct input *input)
3048{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003049 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003050 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003051
3052 if (input->drag_offer)
3053 data_offer_destroy(input->drag_offer);
3054
3055 if (input->selection_offer)
3056 data_offer_destroy(input->selection_offer);
3057
3058 wl_data_device_destroy(input->data_device);
3059 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003060 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003061 free(input);
3062}
3063
3064static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003065display_handle_global(struct wl_display *display, uint32_t id,
3066 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003067{
3068 struct display *d = data;
3069
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003070 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003071 d->compositor =
3072 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003073 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003074 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003075 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003076 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003077 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003078 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003079 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003080 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003081 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3082 d->data_device_manager =
3083 wl_display_bind(display, id,
3084 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003085 }
3086}
3087
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003088static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003089init_xkb(struct display *d)
3090{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003091 d->xkb.names.rules = "evdev";
3092 d->xkb.names.model = "pc105";
3093 d->xkb.names.layout = (char *) option_xkb_layout;
3094 d->xkb.names.variant = (char *) option_xkb_variant;
3095 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003096
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003097 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003098 if (!d->xkb.context) {
3099 fprintf(stderr, "Failed to create XKB context\n");
3100 exit(1);
3101 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003102
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003103 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003104 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003105 fprintf(stderr, "Failed to compile keymap\n");
3106 exit(1);
3107 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003108
3109 d->xkb.state = xkb_state_new(d->xkb.keymap);
3110 if (!d->xkb.state) {
3111 fprintf(stderr, "Failed to create XKB state\n");
3112 exit(1);
3113 }
3114
3115 d->xkb.control_mask =
3116 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3117 d->xkb.alt_mask =
3118 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3119 d->xkb.shift_mask =
3120 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3121
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003122}
3123
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003124static void
3125fini_xkb(struct display *display)
3126{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003127 xkb_state_unref(display->xkb.state);
3128 xkb_map_unref(display->xkb.keymap);
3129 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003130}
3131
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003132#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003133static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003134init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003135{
3136 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003137 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003138
Rob Clark6396ed32012-03-11 19:48:41 -05003139#ifdef USE_CAIRO_GLESV2
3140# define GL_BIT EGL_OPENGL_ES2_BIT
3141#else
3142# define GL_BIT EGL_OPENGL_BIT
3143#endif
3144
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003145 static const EGLint argb_cfg_attribs[] = {
3146 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003147 EGL_RED_SIZE, 1,
3148 EGL_GREEN_SIZE, 1,
3149 EGL_BLUE_SIZE, 1,
3150 EGL_ALPHA_SIZE, 1,
3151 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003152 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003153 EGL_NONE
3154 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003155
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003156#ifdef USE_CAIRO_GLESV2
3157 static const EGLint context_attribs[] = {
3158 EGL_CONTEXT_CLIENT_VERSION, 2,
3159 EGL_NONE
3160 };
3161 EGLint api = EGL_OPENGL_ES_API;
3162#else
3163 EGLint *context_attribs = NULL;
3164 EGLint api = EGL_OPENGL_API;
3165#endif
3166
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003167 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003168 if (!eglInitialize(d->dpy, &major, &minor)) {
3169 fprintf(stderr, "failed to initialize display\n");
3170 return -1;
3171 }
3172
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003173 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003174 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3175 return -1;
3176 }
3177
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003178 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3179 &d->argb_config, 1, &n) || n != 1) {
3180 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003181 return -1;
3182 }
3183
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003184 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003185 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003186 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003187 fprintf(stderr, "failed to create context\n");
3188 return -1;
3189 }
3190
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003191 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003192 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003193 return -1;
3194 }
3195
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003196#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003197 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3198 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3199 fprintf(stderr, "failed to get cairo egl argb device\n");
3200 return -1;
3201 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003202#endif
3203
3204 return 0;
3205}
3206
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003207static void
3208fini_egl(struct display *display)
3209{
3210#ifdef HAVE_CAIRO_EGL
3211 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003212#endif
3213
3214 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3215 EGL_NO_CONTEXT);
3216
3217 eglTerminate(display->dpy);
3218 eglReleaseThread();
3219}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003220#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003221
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003222static int
3223event_mask_update(uint32_t mask, void *data)
3224{
3225 struct display *d = data;
3226
3227 d->mask = mask;
3228
3229 return 0;
3230}
3231
3232static void
3233handle_display_data(struct task *task, uint32_t events)
3234{
3235 struct display *display =
3236 container_of(task, struct display, display_task);
3237
3238 wl_display_iterate(display->display, display->mask);
3239}
3240
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003241struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003242display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003243{
3244 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003245
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003246 argc = parse_options(xkb_options,
3247 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003248
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003249 d = malloc(sizeof *d);
3250 if (d == NULL)
3251 return NULL;
3252
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003253 memset(d, 0, sizeof *d);
3254
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003255 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003256 if (d->display == NULL) {
3257 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003258 return NULL;
3259 }
3260
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003261 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3262 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3263 d->display_task.run = handle_display_data;
3264 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3265
3266 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003267 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003268 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003269
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003270 /* Set up listener so we'll catch all events. */
3271 wl_display_add_global_listener(d->display,
3272 display_handle_global, d);
3273
3274 /* Process connection events. */
3275 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003276#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003277 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003278 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003279#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003280
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003281 d->image_target_texture_2d =
3282 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3283 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3284 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3285
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003286 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003287
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003288 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003289
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003290 wl_list_init(&d->window_list);
3291
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003292 init_xkb(d);
3293
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003294 return d;
3295}
3296
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003297static void
3298display_destroy_outputs(struct display *display)
3299{
3300 struct output *tmp;
3301 struct output *output;
3302
3303 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3304 output_destroy(output);
3305}
3306
Pekka Paalanene1207c72011-12-16 12:02:09 +02003307static void
3308display_destroy_inputs(struct display *display)
3309{
3310 struct input *tmp;
3311 struct input *input;
3312
3313 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3314 input_destroy(input);
3315}
3316
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003317void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003318display_destroy(struct display *display)
3319{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003320 if (!wl_list_empty(&display->window_list))
3321 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3322
3323 if (!wl_list_empty(&display->deferred_list))
3324 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3325
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003326 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003327 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003328
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003329 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003330
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003331 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003332 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003333
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003334#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003335 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003336#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003337
Pekka Paalanenc2052982011-12-16 11:41:32 +02003338 if (display->shell)
3339 wl_shell_destroy(display->shell);
3340
3341 if (display->shm)
3342 wl_shm_destroy(display->shm);
3343
3344 if (display->data_device_manager)
3345 wl_data_device_manager_destroy(display->data_device_manager);
3346
3347 wl_compositor_destroy(display->compositor);
3348
3349 close(display->epoll_fd);
3350
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003351 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003352 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003353 free(display);
3354}
3355
3356void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003357display_set_user_data(struct display *display, void *data)
3358{
3359 display->user_data = data;
3360}
3361
3362void *
3363display_get_user_data(struct display *display)
3364{
3365 return display->user_data;
3366}
3367
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003368struct wl_display *
3369display_get_display(struct display *display)
3370{
3371 return display->display;
3372}
3373
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003374struct output *
3375display_get_output(struct display *display)
3376{
3377 return container_of(display->output_list.next, struct output, link);
3378}
3379
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003380struct wl_compositor *
3381display_get_compositor(struct display *display)
3382{
3383 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003384}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003385
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003386uint32_t
3387display_get_serial(struct display *display)
3388{
3389 return display->serial;
3390}
3391
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003392EGLDisplay
3393display_get_egl_display(struct display *d)
3394{
3395 return d->dpy;
3396}
3397
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003398struct wl_data_source *
3399display_create_data_source(struct display *display)
3400{
3401 return wl_data_device_manager_create_data_source(display->data_device_manager);
3402}
3403
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003404EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003405display_get_argb_egl_config(struct display *d)
3406{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003407 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003408}
3409
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003410struct wl_shell *
3411display_get_shell(struct display *display)
3412{
3413 return display->shell;
3414}
3415
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003416int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003417display_acquire_window_surface(struct display *display,
3418 struct window *window,
3419 EGLContext ctx)
3420{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003421#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003422 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003423 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003424
3425 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003426 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003427 device = cairo_surface_get_device(window->cairo_surface);
3428 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003429 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003430
Benjamin Franzke0c991632011-09-27 21:57:31 +02003431 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003432 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003433 ctx = display->argb_ctx;
3434 else
3435 assert(0);
3436 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003437
3438 data = cairo_surface_get_user_data(window->cairo_surface,
3439 &surface_data_key);
3440
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003441 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003442 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003443 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3444 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003445
3446 return 0;
3447#else
3448 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003449#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003450}
3451
3452void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003453display_release_window_surface(struct display *display,
3454 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003455{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003456#ifdef HAVE_CAIRO_EGL
3457 cairo_device_t *device;
3458
3459 device = cairo_surface_get_device(window->cairo_surface);
3460 if (!device)
3461 return;
3462
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003463 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003464 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003465 cairo_device_release(device);
3466#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003467}
3468
3469void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003470display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003471{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003472 wl_list_insert(&display->deferred_list, &task->link);
3473}
3474
3475void
3476display_watch_fd(struct display *display,
3477 int fd, uint32_t events, struct task *task)
3478{
3479 struct epoll_event ep;
3480
3481 ep.events = events;
3482 ep.data.ptr = task;
3483 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3484}
3485
3486void
3487display_run(struct display *display)
3488{
3489 struct task *task;
3490 struct epoll_event ep[16];
3491 int i, count;
3492
Pekka Paalanen826d7952011-12-15 10:14:07 +02003493 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003494 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003495 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003496
3497 while (!wl_list_empty(&display->deferred_list)) {
3498 task = container_of(display->deferred_list.next,
3499 struct task, link);
3500 wl_list_remove(&task->link);
3501 task->run(task, 0);
3502 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003503
3504 if (!display->running)
3505 break;
3506
3507 wl_display_flush(display->display);
3508
3509 count = epoll_wait(display->epoll_fd,
3510 ep, ARRAY_LENGTH(ep), -1);
3511 for (i = 0; i < count; i++) {
3512 task = ep[i].data.ptr;
3513 task->run(task, ep[i].events);
3514 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003515 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003516}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003517
3518void
3519display_exit(struct display *display)
3520{
3521 display->running = 0;
3522}