blob: 36caeb3eb3ef1791fb1b0bed61fa852264a289f3 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030065#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070069struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030070
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050073 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040074 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040075 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060077 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040097 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030099 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300100 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400101
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200105
106 display_output_handler_t output_configure_handler;
107
108 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100109
110 struct xkb_context *xkb_context;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500111};
112
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400114 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400116 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500117 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500119 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_CUSTOM
121};
Rob Bradford7507b572012-05-15 17:55:34 +0100122
123struct window_output {
124 struct output *output;
125 struct wl_list link;
126};
127
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128struct window {
129 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100131 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500132 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200133 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500134 struct wl_region *input_region;
135 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500136 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500138 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500139 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400140 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400141 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400142 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400143 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400144 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400145 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400146 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400147 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400148 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500149
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400150 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500151
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700152 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400153
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500154 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500155 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400156 window_data_handler_t data_handler;
157 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500158 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400159
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200160 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500161 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500162
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500163 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400164 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500165};
166
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500167struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500168 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300169 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500170 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400171 struct wl_list link;
172 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500173 widget_resize_handler_t resize_handler;
174 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500175 widget_enter_handler_t enter_handler;
176 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500177 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500178 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500180 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300181 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182};
183
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400184struct input {
185 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100186 struct wl_seat *seat;
187 struct wl_pointer *pointer;
188 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400189 struct window *pointer_focus;
190 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300191 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300192 uint32_t cursor_anim_start;
193 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300194 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400195 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400196 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400197 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400198 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400199
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500200 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500201 struct widget *grab;
202 uint32_t grab_button;
203
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400204 struct wl_data_device *data_device;
205 struct data_offer *drag_offer;
206 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100207
208 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100209 struct xkb_keymap *keymap;
210 struct xkb_state *state;
211 xkb_mod_mask_t control_mask;
212 xkb_mod_mask_t alt_mask;
213 xkb_mod_mask_t shift_mask;
214 } xkb;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400215};
216
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500217struct output {
218 struct display *display;
219 struct wl_output *output;
220 struct rectangle allocation;
221 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200222
223 display_output_handler_t destroy_handler;
224 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500225};
226
Martin Minarik1998b152012-05-10 02:04:35 +0200227enum frame_button_action {
228 FRAME_BUTTON_NULL = 0,
229 FRAME_BUTTON_ICON = 1,
230 FRAME_BUTTON_CLOSE = 2,
231 FRAME_BUTTON_MINIMIZE = 3,
232 FRAME_BUTTON_MAXIMIZE = 4,
233};
234
235enum frame_button_pointer {
236 FRAME_BUTTON_DEFAULT = 0,
237 FRAME_BUTTON_OVER = 1,
238 FRAME_BUTTON_ACTIVE = 2,
239};
240
241enum frame_button_align {
242 FRAME_BUTTON_RIGHT = 0,
243 FRAME_BUTTON_LEFT = 1,
244};
245
246enum frame_button_decoration {
247 FRAME_BUTTON_NONE = 0,
248 FRAME_BUTTON_FANCY = 1,
249};
250
251struct frame_button {
252 struct widget *widget;
253 struct frame *frame;
254 cairo_surface_t *icon;
255 enum frame_button_action type;
256 enum frame_button_pointer state;
257 struct wl_list link; /* buttons_list */
258 enum frame_button_align align;
259 enum frame_button_decoration decoration;
260};
261
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500262struct frame {
263 struct widget *widget;
264 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200265 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500266};
267
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500268struct menu {
269 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500270 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500271 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500272 const char **entries;
273 uint32_t time;
274 int current;
275 int count;
276 menu_func_t func;
277};
278
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300279struct tooltip {
280 struct widget *parent;
281 struct window *window;
282 struct widget *widget;
283 char *entry;
284 struct task tooltip_task;
285 int tooltip_fd;
286 float x, y;
287};
288
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700289struct shm_pool {
290 struct wl_shm_pool *pool;
291 size_t size;
292 size_t used;
293 void *data;
294};
295
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400296enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300297 CURSOR_DEFAULT = 100,
298 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400299};
300
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500301enum window_location {
302 WINDOW_INTERIOR = 0,
303 WINDOW_RESIZING_TOP = 1,
304 WINDOW_RESIZING_BOTTOM = 2,
305 WINDOW_RESIZING_LEFT = 4,
306 WINDOW_RESIZING_TOP_LEFT = 5,
307 WINDOW_RESIZING_BOTTOM_LEFT = 6,
308 WINDOW_RESIZING_RIGHT = 8,
309 WINDOW_RESIZING_TOP_RIGHT = 9,
310 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
311 WINDOW_RESIZING_MASK = 15,
312 WINDOW_EXTERIOR = 16,
313 WINDOW_TITLEBAR = 17,
314 WINDOW_CLIENT_AREA = 18,
315};
316
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400317const char *option_xkb_layout = "us";
318const char *option_xkb_variant = "";
319const char *option_xkb_options = "";
320
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400321static const struct weston_option xkb_options[] = {
322 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
323 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
324 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400325};
326
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327static const cairo_user_data_key_t surface_data_key;
328struct surface_data {
329 struct wl_buffer *buffer;
330};
331
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500332#define MULT(_d,c,a,t) \
333 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
334
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500335#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100337struct egl_window_surface_data {
338 struct display *display;
339 struct wl_surface *surface;
340 struct wl_egl_window *window;
341 EGLSurface surf;
342};
343
344static void
345egl_window_surface_data_destroy(void *p)
346{
347 struct egl_window_surface_data *data = p;
348 struct display *d = data->display;
349
350 eglDestroySurface(d->dpy, data->surf);
351 wl_egl_window_destroy(data->window);
352 data->surface = NULL;
353
354 free(p);
355}
356
357static cairo_surface_t *
358display_create_egl_window_surface(struct display *display,
359 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400360 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100361 struct rectangle *rectangle)
362{
363 cairo_surface_t *cairo_surface;
364 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400365 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200366 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100367
368 data = malloc(sizeof *data);
369 if (data == NULL)
370 return NULL;
371
372 data->display = display;
373 data->surface = surface;
374
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500375 config = display->argb_config;
376 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400378 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100379 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400380 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400382 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500383 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100384
Benjamin Franzke0c991632011-09-27 21:57:31 +0200385 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100386 data->surf,
387 rectangle->width,
388 rectangle->height);
389
390 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
391 data, egl_window_surface_data_destroy);
392
393 return cairo_surface;
394}
395
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396#endif
397
398struct wl_buffer *
399display_get_buffer_for_surface(struct display *display,
400 cairo_surface_t *surface)
401{
402 struct surface_data *data;
403
404 data = cairo_surface_get_user_data (surface, &surface_data_key);
405
406 return data->buffer;
407}
408
409struct shm_surface_data {
410 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700411 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412};
413
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500414static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700415shm_pool_destroy(struct shm_pool *pool);
416
417static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418shm_surface_data_destroy(void *p)
419{
420 struct shm_surface_data *data = p;
421
422 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700423 if (data->pool)
424 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400425}
426
Kristian Høgsberg16626282012-04-03 11:21:27 -0400427static struct wl_shm_pool *
428make_shm_pool(struct display *display, int size, void **data)
429{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400430 struct wl_shm_pool *pool;
431 int fd;
432
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300433 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400434 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300435 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
436 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400437 return NULL;
438 }
439
440 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400441 if (*data == MAP_FAILED) {
442 fprintf(stderr, "mmap failed: %m\n");
443 close(fd);
444 return NULL;
445 }
446
447 pool = wl_shm_create_pool(display->shm, fd, size);
448
449 close(fd);
450
451 return pool;
452}
453
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700454static struct shm_pool *
455shm_pool_create(struct display *display, size_t size)
456{
457 struct shm_pool *pool = malloc(sizeof *pool);
458
459 if (!pool)
460 return NULL;
461
462 pool->pool = make_shm_pool(display, size, &pool->data);
463 if (!pool->pool) {
464 free(pool);
465 return NULL;
466 }
467
468 pool->size = size;
469 pool->used = 0;
470
471 return pool;
472}
473
474static void *
475shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
476{
477 if (pool->used + size > pool->size)
478 return NULL;
479
480 *offset = pool->used;
481 pool->used += size;
482
483 return (char *) pool->data + *offset;
484}
485
486/* destroy the pool. this does not unmap the memory though */
487static void
488shm_pool_destroy(struct shm_pool *pool)
489{
490 munmap(pool->data, pool->size);
491 wl_shm_pool_destroy(pool->pool);
492 free(pool);
493}
494
495/* Start allocating from the beginning of the pool again */
496static void
497shm_pool_reset(struct shm_pool *pool)
498{
499 pool->used = 0;
500}
501
502static int
503data_length_for_shm_surface(struct rectangle *rect)
504{
505 int stride;
506
507 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
508 rect->width);
509 return stride * rect->height;
510}
511
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500512static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700513display_create_shm_surface_from_pool(struct display *display,
514 struct rectangle *rectangle,
515 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516{
517 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400518 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400519 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700520 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400521 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400522
523 data = malloc(sizeof *data);
524 if (data == NULL)
525 return NULL;
526
527 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
528 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700529 length = stride * rectangle->height;
530 data->pool = NULL;
531 map = shm_pool_allocate(pool, length, &offset);
532
533 if (!map) {
534 free(data);
535 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400536 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400537
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400538 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539 CAIRO_FORMAT_ARGB32,
540 rectangle->width,
541 rectangle->height,
542 stride);
543
544 cairo_surface_set_user_data (surface, &surface_data_key,
545 data, shm_surface_data_destroy);
546
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500548 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500550 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700552 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400553 rectangle->width,
554 rectangle->height,
555 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700557 return surface;
558}
559
560static cairo_surface_t *
561display_create_shm_surface(struct display *display,
562 struct rectangle *rectangle, uint32_t flags,
563 struct window *window)
564{
565 struct shm_surface_data *data;
566 struct shm_pool *pool;
567 cairo_surface_t *surface;
568
569 if (window && window->pool) {
570 shm_pool_reset(window->pool);
571 surface = display_create_shm_surface_from_pool(display,
572 rectangle,
573 flags,
574 window->pool);
575 if (surface)
576 return surface;
577 }
578
579 pool = shm_pool_create(display,
580 data_length_for_shm_surface(rectangle));
581 if (!pool)
582 return NULL;
583
584 surface =
585 display_create_shm_surface_from_pool(display, rectangle,
586 flags, pool);
587
588 if (!surface) {
589 shm_pool_destroy(pool);
590 return NULL;
591 }
592
593 /* make sure we destroy the pool when the surface is destroyed */
594 data = cairo_surface_get_user_data(surface, &surface_data_key);
595 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596
597 return surface;
598}
599
nobled7b87cb02011-02-01 18:51:47 +0000600static int
601check_size(struct rectangle *rect)
602{
603 if (rect->width && rect->height)
604 return 0;
605
606 fprintf(stderr, "tried to create surface of "
607 "width: %d, height: %d\n", rect->width, rect->height);
608 return -1;
609}
610
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400611cairo_surface_t *
612display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100613 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400614 struct rectangle *rectangle,
615 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616{
nobled7b87cb02011-02-01 18:51:47 +0000617 if (check_size(rectangle) < 0)
618 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500619#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300620 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400621 return display_create_egl_window_surface(display,
622 surface,
623 flags,
624 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400625#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400626 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627}
628
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300629static const char *cursors[] = {
630 "bottom_left_corner",
631 "bottom_right_corner",
632 "bottom_side",
633 "grabbing",
634 "left_ptr",
635 "left_side",
636 "right_side",
637 "top_left_corner",
638 "top_right_corner",
639 "top_side",
640 "xterm",
641 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400642 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300643};
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,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001351 uint32_t button,
1352 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001353{
1354 struct frame_button *frame_button = data;
1355 struct window *window = widget->window;
1356
1357 if (button != BTN_LEFT)
1358 return;
1359
1360 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001361 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001362 frame_button->state = FRAME_BUTTON_ACTIVE;
1363 widget_schedule_redraw(frame_button->widget);
1364
1365 if (frame_button->type == FRAME_BUTTON_ICON)
1366 window_show_frame_menu(window, input, time);
1367 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001368 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001369 frame_button->state = FRAME_BUTTON_DEFAULT;
1370 widget_schedule_redraw(frame_button->widget);
1371 break;
1372 }
1373
1374 switch (frame_button->type) {
1375 case FRAME_BUTTON_CLOSE:
1376 if (window->close_handler)
1377 window->close_handler(window->parent,
1378 window->user_data);
1379 else
1380 display_exit(window->display);
1381 break;
1382 case FRAME_BUTTON_MINIMIZE:
1383 fprintf(stderr,"Minimize stub\n");
1384 break;
1385 case FRAME_BUTTON_MAXIMIZE:
1386 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1387 break;
1388 default:
1389 /* Unknown operation */
1390 break;
1391 }
1392}
1393
1394static void
1395frame_button_redraw_handler(struct widget *widget, void *data)
1396{
1397 struct frame_button *frame_button = data;
1398 cairo_t *cr;
1399 int width, height, x, y;
1400 struct window *window = widget->window;
1401
1402 x = widget->allocation.x;
1403 y = widget->allocation.y;
1404 width = widget->allocation.width;
1405 height = widget->allocation.height;
1406
1407 if (!width)
1408 return;
1409 if (!height)
1410 return;
1411 if (widget->opaque)
1412 return;
1413
1414 cr = cairo_create(window->cairo_surface);
1415
1416 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1417 cairo_set_line_width(cr, 1);
1418
1419 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1420 cairo_rectangle (cr, x, y, 25, 16);
1421
1422 cairo_stroke_preserve(cr);
1423
1424 switch (frame_button->state) {
1425 case FRAME_BUTTON_DEFAULT:
1426 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1427 break;
1428 case FRAME_BUTTON_OVER:
1429 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1430 break;
1431 case FRAME_BUTTON_ACTIVE:
1432 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1433 break;
1434 }
1435
1436 cairo_fill (cr);
1437
1438 x += 4;
1439 }
1440
1441 cairo_set_source_surface(cr, frame_button->icon, x, y);
1442 cairo_paint(cr);
1443
1444 cairo_destroy(cr);
1445}
1446
1447static struct widget *
1448frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1449 enum frame_button_align align, enum frame_button_decoration style)
1450{
1451 struct frame_button *frame_button;
1452 const char *icon = data;
1453
1454 frame_button = malloc (sizeof *frame_button);
1455 memset(frame_button, 0, sizeof *frame_button);
1456
1457 frame_button->icon = cairo_image_surface_create_from_png(icon);
1458 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1459 frame_button->frame = frame;
1460 frame_button->type = type;
1461 frame_button->align = align;
1462 frame_button->decoration = style;
1463
1464 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1465
1466 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1467 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1468 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1469 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1470 return frame_button->widget;
1471}
1472
1473static void
1474frame_button_destroy(struct frame_button *frame_button)
1475{
1476 widget_destroy(frame_button->widget);
1477 wl_list_remove(&frame_button->link);
1478 cairo_surface_destroy(frame_button->icon);
1479 free(frame_button);
1480
1481 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001482}
1483
1484static void
1485frame_redraw_handler(struct widget *widget, void *data)
1486{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001487 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001488 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001489 struct theme *t = window->display->theme;
1490 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001492 if (window->type == TYPE_FULLSCREEN)
1493 return;
1494
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495 cr = cairo_create(window->cairo_surface);
1496
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001497 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001498 flags |= THEME_FRAME_ACTIVE;
1499 theme_render_frame(t, cr, widget->allocation.width,
1500 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001501
1502 cairo_destroy(cr);
1503}
1504
1505static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001506frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001507{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001508 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001509 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001510
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001511 location = theme_get_location(t, input->sx, input->sy,
1512 frame->widget->allocation.width,
1513 frame->widget->allocation.height);
1514
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001515 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001516 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001517 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001518 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001519 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001521 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001522 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001523 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_EXTERIOR:
1533 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001534 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001535 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001536 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001537}
1538
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001539static void
1540frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001541{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001542 switch (index) {
1543 case 0: /* close */
1544 if (window->close_handler)
1545 window->close_handler(window->parent,
1546 window->user_data);
1547 else
1548 display_exit(window->display);
1549 break;
1550 case 1: /* fullscreen */
1551 /* we don't have a way to get out of fullscreen for now */
1552 window_set_fullscreen(window, 1);
1553 break;
1554 case 2: /* rotate */
1555 case 3: /* scale */
1556 break;
1557 }
1558}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001559
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001560void
1561window_show_frame_menu(struct window *window,
1562 struct input *input, uint32_t time)
1563{
1564 int32_t x, y;
1565
1566 static const char *entries[] = {
1567 "Close", "Fullscreen", "Rotate", "Scale"
1568 };
1569
1570 input_get_position(input, &x, &y);
1571 window_show_menu(window->display, input, time, window,
1572 x - 10, y - 10, frame_menu_func, entries, 4);
1573}
1574
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001575static int
1576frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001577 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001578{
1579 return frame_get_pointer_image_for_location(data, input);
1580}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001581
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001582static int
1583frame_motion_handler(struct widget *widget,
1584 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001585 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001586{
1587 return frame_get_pointer_image_for_location(data, input);
1588}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001589
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001590static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591frame_button_handler(struct widget *widget,
1592 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001593 uint32_t button, enum wl_pointer_button_state state,
1594 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595
1596{
1597 struct frame *frame = data;
1598 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001599 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001600 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001601
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001602 location = theme_get_location(display->theme, input->sx, input->sy,
1603 frame->widget->allocation.width,
1604 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001605
Daniel Stone4dbadb12012-05-30 16:31:51 +01001606 if (window->display->shell && button == BTN_LEFT &&
1607 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001609 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610 if (!window->shell_surface)
1611 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001612 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001613 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001615 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001616 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001618 case THEME_LOCATION_RESIZING_TOP:
1619 case THEME_LOCATION_RESIZING_BOTTOM:
1620 case THEME_LOCATION_RESIZING_LEFT:
1621 case THEME_LOCATION_RESIZING_RIGHT:
1622 case THEME_LOCATION_RESIZING_TOP_LEFT:
1623 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1624 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1625 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626 if (!window->shell_surface)
1627 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001628 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001629
1630 if (!display->dpy) {
1631 /* If we're using shm, allocate a big
1632 pool to create buffers out of while
1633 we resize. We should probably base
1634 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001635 window->pool =
1636 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001637 }
1638
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001639 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001640 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001641 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 break;
1643 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001644 } else if (button == BTN_RIGHT &&
1645 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001646 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001647 }
1648}
1649
1650struct widget *
1651frame_create(struct window *window, void *data)
1652{
1653 struct frame *frame;
1654
1655 frame = malloc(sizeof *frame);
1656 memset(frame, 0, sizeof *frame);
1657
1658 frame->widget = window_add_widget(window, frame);
1659 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001660
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001661 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1662 widget_set_resize_handler(frame->widget, frame_resize_handler);
1663 widget_set_enter_handler(frame->widget, frame_enter_handler);
1664 widget_set_motion_handler(frame->widget, frame_motion_handler);
1665 widget_set_button_handler(frame->widget, frame_button_handler);
1666
Martin Minarik1998b152012-05-10 02:04:35 +02001667 /* Create empty list for frame buttons */
1668 wl_list_init(&frame->buttons_list);
1669
1670 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1671 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1672
1673 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1674 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1675
1676 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1677 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1678
1679 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1680 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1681
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001682 window->frame = frame;
1683
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001684 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001685}
1686
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001687static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001688frame_destroy(struct frame *frame)
1689{
Martin Minarik1998b152012-05-10 02:04:35 +02001690 struct frame_button *button, *tmp;
1691
1692 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1693 frame_button_destroy(button);
1694
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001695 /* frame->child must be destroyed by the application */
1696 widget_destroy(frame->widget);
1697 free(frame);
1698}
1699
1700static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001701input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001702 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001704 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001705 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001707 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708 return;
1709
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001710 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001711 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001712 widget = old;
1713 if (input->grab)
1714 widget = input->grab;
1715 if (widget->leave_handler)
1716 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001717 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001718 }
1719
1720 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001721 widget = focus;
1722 if (input->grab)
1723 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001724 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001725 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001726 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001727 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001728
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001729 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001730 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001731}
1732
1733static void
Daniel Stone37816df2012-05-16 18:45:18 +01001734pointer_handle_motion(void *data, struct wl_pointer *pointer,
1735 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001736{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001737 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001738 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001739 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001740 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001741 float sx = wl_fixed_to_double(sx_w);
1742 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001743
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001744 input->sx = sx;
1745 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001746
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001747 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001748 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001749 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001750 }
1751
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001752 if (input->grab)
1753 widget = input->grab;
1754 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001755 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001756 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001757 cursor = widget->motion_handler(input->focus_widget,
1758 input, time, sx, sy,
1759 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001760
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001761 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001762}
1763
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001764void
1765input_grab(struct input *input, struct widget *widget, uint32_t button)
1766{
1767 input->grab = widget;
1768 input->grab_button = button;
1769}
1770
1771void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001772input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001773{
1774 struct widget *widget;
1775
1776 input->grab = NULL;
1777 if (input->pointer_focus) {
1778 widget = widget_find_widget(input->pointer_focus->widget,
1779 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001780 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001781 }
1782}
1783
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001784static void
Daniel Stone37816df2012-05-16 18:45:18 +01001785pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001786 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001787{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001788 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001789 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001790 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001791
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001792 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001793 if (input->focus_widget && input->grab == NULL &&
1794 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001795 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001796
Neil Roberts6b28aad2012-01-23 19:11:18 +00001797 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001798 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001799 (*widget->button_handler)(widget,
1800 input, time,
1801 button, state,
1802 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001803
Daniel Stone4dbadb12012-05-30 16:31:51 +01001804 if (input->grab && input->grab_button == button &&
1805 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001806 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001807}
1808
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001809static void
Daniel Stone37816df2012-05-16 18:45:18 +01001810pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001811 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001812{
1813}
1814
1815static void
Daniel Stone37816df2012-05-16 18:45:18 +01001816keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001817 uint32_t serial, uint32_t time, uint32_t key,
1818 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001819{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001820 struct input *input = data;
1821 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001822 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001823 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001824 const xkb_keysym_t *syms;
1825 xkb_keysym_t sym;
1826 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001827
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001828 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001829 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001830 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001831 return;
1832
Daniel Stone97f68542012-05-30 16:32:01 +01001833 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001834
Daniel Stone97f68542012-05-30 16:32:01 +01001835 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001836 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001837 XKB_STATE_LATCHED);
1838 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001839 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001840 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001841 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001842 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001843 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001844 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001845
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001846 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001847 input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001848 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001849 window_set_maximized(window,
1850 window->type != TYPE_MAXIMIZED);
1851 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001852 if (num_syms == 1)
1853 sym = syms[0];
1854 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001855 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001856
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001857 (*window->key_handler)(window, input, time, key,
1858 sym, state, window->user_data);
1859 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001860}
1861
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001862static void
Daniel Stone351eb612012-05-31 15:27:47 -04001863keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1864 uint32_t serial, uint32_t mods_depressed,
1865 uint32_t mods_latched, uint32_t mods_locked,
1866 uint32_t group)
1867{
1868 struct input *input = data;
1869
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001870 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1871 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001872}
1873
1874static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001875input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001876{
1877 struct window *window = input->pointer_focus;
1878
1879 if (!window)
1880 return;
1881
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001882 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001883
Pekka Paalanene1207c72011-12-16 12:02:09 +02001884 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001885 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001886}
1887
1888static void
Daniel Stone37816df2012-05-16 18:45:18 +01001889pointer_handle_enter(void *data, struct wl_pointer *pointer,
1890 uint32_t serial, struct wl_surface *surface,
1891 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001892{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001893 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001894 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001895 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001896 float sx = wl_fixed_to_double(sx_w);
1897 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001898
Martin Minarik1998b152012-05-10 02:04:35 +02001899 if (!surface) {
1900 /* enter event for a window we've just destroyed */
1901 return;
1902 }
1903
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001904 input->display->serial = serial;
1905 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001906 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001907 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001908
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001909 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001910 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001911 window->pool = NULL;
1912 /* Schedule a redraw to free the pool */
1913 window_schedule_redraw(window);
1914 }
1915
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001916 input->sx = sx;
1917 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001918
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001919 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001920 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001921}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001922
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001923static void
Daniel Stone37816df2012-05-16 18:45:18 +01001924pointer_handle_leave(void *data, struct wl_pointer *pointer,
1925 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001926{
1927 struct input *input = data;
1928
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001929 input->display->serial = serial;
1930 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001931}
1932
1933static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001934input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001935{
1936 struct window *window = input->keyboard_focus;
1937
1938 if (!window)
1939 return;
1940
1941 window->keyboard_device = NULL;
1942 if (window->keyboard_focus_handler)
1943 (*window->keyboard_focus_handler)(window, NULL,
1944 window->user_data);
1945
1946 input->keyboard_focus = NULL;
1947}
1948
1949static void
Daniel Stone37816df2012-05-16 18:45:18 +01001950keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1951 uint32_t serial, struct wl_surface *surface,
1952 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001953{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001954 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001955 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001956
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001957 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001958 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001959
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001960 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001961 window->keyboard_device = input;
1962 if (window->keyboard_focus_handler)
1963 (*window->keyboard_focus_handler)(window,
1964 window->keyboard_device,
1965 window->user_data);
1966}
1967
1968static void
Daniel Stone37816df2012-05-16 18:45:18 +01001969keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1970 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001971{
1972 struct input *input = data;
1973
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001974 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001975 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001976}
1977
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001978static void
1979keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1980 uint32_t format, int fd, uint32_t size)
1981{
1982 struct input *input = data;
1983 char *map_str;
1984
1985 if (!data) {
1986 close(fd);
1987 return;
1988 }
1989
1990 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1991 close(fd);
1992 return;
1993 }
1994
1995 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1996 if (map_str == MAP_FAILED) {
1997 close(fd);
1998 return;
1999 }
2000
2001 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2002 map_str,
2003 XKB_KEYMAP_FORMAT_TEXT_V1,
2004 0);
2005 munmap(map_str, size);
2006 close(fd);
2007
2008 if (!input->xkb.keymap) {
2009 fprintf(stderr, "failed to compile keymap\n");
2010 return;
2011 }
2012
2013 input->xkb.state = xkb_state_new(input->xkb.keymap);
2014 if (!input->xkb.state) {
2015 fprintf(stderr, "failed to create XKB state\n");
2016 xkb_map_unref(input->xkb.keymap);
2017 input->xkb.keymap = NULL;
2018 return;
2019 }
2020
2021 input->xkb.control_mask =
2022 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2023 input->xkb.alt_mask =
2024 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2025 input->xkb.shift_mask =
2026 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2027}
2028
Daniel Stone37816df2012-05-16 18:45:18 +01002029static const struct wl_pointer_listener pointer_listener = {
2030 pointer_handle_enter,
2031 pointer_handle_leave,
2032 pointer_handle_motion,
2033 pointer_handle_button,
2034 pointer_handle_axis,
2035};
2036
2037static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002038 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002039 keyboard_handle_enter,
2040 keyboard_handle_leave,
2041 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002042 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002043};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002044
2045static void
Daniel Stone37816df2012-05-16 18:45:18 +01002046seat_handle_capabilities(void *data, struct wl_seat *seat,
2047 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002048{
Daniel Stone37816df2012-05-16 18:45:18 +01002049 struct input *input = data;
2050
2051 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2052 input->pointer = wl_seat_get_pointer(seat);
2053 wl_pointer_set_user_data(input->pointer, input);
2054 wl_pointer_add_listener(input->pointer, &pointer_listener,
2055 input);
2056 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2057 wl_pointer_destroy(input->pointer);
2058 input->pointer = NULL;
2059 }
2060
2061 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2062 input->keyboard = wl_seat_get_keyboard(seat);
2063 wl_keyboard_set_user_data(input->keyboard, input);
2064 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2065 input);
2066 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2067 wl_keyboard_destroy(input->keyboard);
2068 input->keyboard = NULL;
2069 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002070}
2071
Daniel Stone37816df2012-05-16 18:45:18 +01002072static const struct wl_seat_listener seat_listener = {
2073 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002074};
2075
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002076void
2077input_get_position(struct input *input, int32_t *x, int32_t *y)
2078{
2079 *x = input->sx;
2080 *y = input->sy;
2081}
2082
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002083struct display *
2084input_get_display(struct input *input)
2085{
2086 return input->display;
2087}
2088
Daniel Stone37816df2012-05-16 18:45:18 +01002089struct wl_seat *
2090input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002091{
Daniel Stone37816df2012-05-16 18:45:18 +01002092 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002093}
2094
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002095uint32_t
2096input_get_modifiers(struct input *input)
2097{
2098 return input->modifiers;
2099}
2100
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002101struct widget *
2102input_get_focus_widget(struct input *input)
2103{
2104 return input->focus_widget;
2105}
2106
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002107struct data_offer {
2108 struct wl_data_offer *offer;
2109 struct input *input;
2110 struct wl_array types;
2111 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002112
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002113 struct task io_task;
2114 int fd;
2115 data_func_t func;
2116 int32_t x, y;
2117 void *user_data;
2118};
2119
2120static void
2121data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2122{
2123 struct data_offer *offer = data;
2124 char **p;
2125
2126 p = wl_array_add(&offer->types, sizeof *p);
2127 *p = strdup(type);
2128}
2129
2130static const struct wl_data_offer_listener data_offer_listener = {
2131 data_offer_offer,
2132};
2133
2134static void
2135data_offer_destroy(struct data_offer *offer)
2136{
2137 char **p;
2138
2139 offer->refcount--;
2140 if (offer->refcount == 0) {
2141 wl_data_offer_destroy(offer->offer);
2142 for (p = offer->types.data; *p; p++)
2143 free(*p);
2144 wl_array_release(&offer->types);
2145 free(offer);
2146 }
2147}
2148
2149static void
2150data_device_data_offer(void *data,
2151 struct wl_data_device *data_device, uint32_t id)
2152{
2153 struct data_offer *offer;
2154
2155 offer = malloc(sizeof *offer);
2156
2157 wl_array_init(&offer->types);
2158 offer->refcount = 1;
2159 offer->input = data;
2160
2161 /* FIXME: Generate typesafe wrappers for this */
2162 offer->offer = (struct wl_data_offer *)
2163 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2164 id, &wl_data_offer_interface);
2165
2166 wl_data_offer_add_listener(offer->offer,
2167 &data_offer_listener, offer);
2168}
2169
2170static void
2171data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002172 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002173 wl_fixed_t x_w, wl_fixed_t y_w,
2174 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002175{
2176 struct input *input = data;
2177 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002178 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002179 float x = wl_fixed_to_double(x_w);
2180 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002181 char **p;
2182
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002183 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002184 window = wl_surface_get_user_data(surface);
2185 input->pointer_focus = window;
2186
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002187 if (offer) {
2188 input->drag_offer = wl_data_offer_get_user_data(offer);
2189
2190 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2191 *p = NULL;
2192
2193 types_data = input->drag_offer->types.data;
2194 } else {
2195 input->drag_offer = NULL;
2196 types_data = NULL;
2197 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002198
2199 window = input->pointer_focus;
2200 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002201 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002202 window->user_data);
2203}
2204
2205static void
2206data_device_leave(void *data, struct wl_data_device *data_device)
2207{
2208 struct input *input = data;
2209
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002210 if (input->drag_offer) {
2211 data_offer_destroy(input->drag_offer);
2212 input->drag_offer = NULL;
2213 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002214}
2215
2216static void
2217data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002218 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002219{
2220 struct input *input = data;
2221 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002222 float x = wl_fixed_to_double(x_w);
2223 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002224 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002225
2226 input->sx = x;
2227 input->sy = y;
2228
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002229 if (input->drag_offer)
2230 types_data = input->drag_offer->types.data;
2231 else
2232 types_data = NULL;
2233
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002234 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002235 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002236 window->user_data);
2237}
2238
2239static void
2240data_device_drop(void *data, struct wl_data_device *data_device)
2241{
2242 struct input *input = data;
2243 struct window *window = input->pointer_focus;
2244
2245 if (window->drop_handler)
2246 window->drop_handler(window, input,
2247 input->sx, input->sy, window->user_data);
2248}
2249
2250static void
2251data_device_selection(void *data,
2252 struct wl_data_device *wl_data_device,
2253 struct wl_data_offer *offer)
2254{
2255 struct input *input = data;
2256 char **p;
2257
2258 if (input->selection_offer)
2259 data_offer_destroy(input->selection_offer);
2260
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002261 if (offer) {
2262 input->selection_offer = wl_data_offer_get_user_data(offer);
2263 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2264 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002265 } else {
2266 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002267 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002268}
2269
2270static const struct wl_data_device_listener data_device_listener = {
2271 data_device_data_offer,
2272 data_device_enter,
2273 data_device_leave,
2274 data_device_motion,
2275 data_device_drop,
2276 data_device_selection
2277};
2278
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002279static void
2280input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002281{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002282 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002283 struct wl_cursor *cursor;
2284 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002285
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002286 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002287 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002288 return;
2289
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002290 if (index >= (int) cursor->image_count) {
2291 fprintf(stderr, "cursor index out of range\n");
2292 return;
2293 }
2294
2295 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002296 buffer = wl_cursor_image_get_buffer(image);
2297 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002298 return;
2299
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002300 wl_pointer_set_cursor(input->pointer, input->display->serial,
2301 input->pointer_surface,
2302 image->hotspot_x, image->hotspot_y);
2303 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2304 wl_surface_damage(input->pointer_surface, 0, 0,
2305 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002306}
2307
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002308static const struct wl_callback_listener pointer_surface_listener;
2309
2310static void
2311pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2312 uint32_t time)
2313{
2314 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002315 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002316 int i;
2317
2318 if (callback) {
2319 assert(callback == input->cursor_frame_cb);
2320 wl_callback_destroy(callback);
2321 input->cursor_frame_cb = NULL;
2322 }
2323
2324 if (input->current_cursor == CURSOR_UNSET)
2325 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002326 cursor = input->display->cursors[input->current_cursor];
2327 if (!cursor)
2328 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002329
2330 /* FIXME We don't have the current time on the first call so we set
2331 * the animation start to the time of the first frame callback. */
2332 if (time == 0)
2333 input->cursor_anim_start = 0;
2334 else if (input->cursor_anim_start == 0)
2335 input->cursor_anim_start = time;
2336
2337 if (time == 0 || input->cursor_anim_start == 0)
2338 i = 0;
2339 else
2340 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2341
2342 input_set_pointer_image_index(input, i);
2343
2344 if (cursor->image_count == 1)
2345 return;
2346
2347 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2348 wl_callback_add_listener(input->cursor_frame_cb,
2349 &pointer_surface_listener, input);
2350}
2351
2352static const struct wl_callback_listener pointer_surface_listener = {
2353 pointer_surface_frame_callback
2354};
2355
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002356void
2357input_set_pointer_image(struct input *input, int pointer)
2358{
2359 if (pointer == input->current_cursor)
2360 return;
2361
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002362 input->current_cursor = pointer;
2363 if (!input->cursor_frame_cb)
2364 pointer_surface_frame_callback(input, NULL, 0);
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002365}
2366
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002367struct wl_data_device *
2368input_get_data_device(struct input *input)
2369{
2370 return input->data_device;
2371}
2372
2373void
2374input_set_selection(struct input *input,
2375 struct wl_data_source *source, uint32_t time)
2376{
2377 wl_data_device_set_selection(input->data_device, source, time);
2378}
2379
2380void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002381input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002382{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002383 wl_data_offer_accept(input->drag_offer->offer,
2384 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002385}
2386
2387static void
2388offer_io_func(struct task *task, uint32_t events)
2389{
2390 struct data_offer *offer =
2391 container_of(task, struct data_offer, io_task);
2392 unsigned int len;
2393 char buffer[4096];
2394
2395 len = read(offer->fd, buffer, sizeof buffer);
2396 offer->func(buffer, len,
2397 offer->x, offer->y, offer->user_data);
2398
2399 if (len == 0) {
2400 close(offer->fd);
2401 data_offer_destroy(offer);
2402 }
2403}
2404
2405static void
2406data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2407 data_func_t func, void *user_data)
2408{
2409 int p[2];
2410
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002411 if (pipe2(p, O_CLOEXEC) == -1)
2412 return;
2413
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002414 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2415 close(p[1]);
2416
2417 offer->io_task.run = offer_io_func;
2418 offer->fd = p[0];
2419 offer->func = func;
2420 offer->refcount++;
2421 offer->user_data = user_data;
2422
2423 display_watch_fd(offer->input->display,
2424 offer->fd, EPOLLIN, &offer->io_task);
2425}
2426
2427void
2428input_receive_drag_data(struct input *input, const char *mime_type,
2429 data_func_t func, void *data)
2430{
2431 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2432 input->drag_offer->x = input->sx;
2433 input->drag_offer->y = input->sy;
2434}
2435
2436int
2437input_receive_selection_data(struct input *input, const char *mime_type,
2438 data_func_t func, void *data)
2439{
2440 char **p;
2441
2442 if (input->selection_offer == NULL)
2443 return -1;
2444
2445 for (p = input->selection_offer->types.data; *p; p++)
2446 if (strcmp(mime_type, *p) == 0)
2447 break;
2448
2449 if (*p == NULL)
2450 return -1;
2451
2452 data_offer_receive_data(input->selection_offer,
2453 mime_type, func, data);
2454 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002455}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002456
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002457int
2458input_receive_selection_data_to_fd(struct input *input,
2459 const char *mime_type, int fd)
2460{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002461 if (input->selection_offer)
2462 wl_data_offer_receive(input->selection_offer->offer,
2463 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002464
2465 return 0;
2466}
2467
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002468void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002469window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002470{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002471 if (!window->shell_surface)
2472 return;
2473
Daniel Stone37816df2012-05-16 18:45:18 +01002474 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002475}
2476
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002477static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002478idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002479{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002480 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002481
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002482 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002483 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002484 widget_set_allocation(widget,
2485 window->pending_allocation.x,
2486 window->pending_allocation.y,
2487 window->pending_allocation.width,
2488 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002489
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002490 if (window->input_region) {
2491 wl_region_destroy(window->input_region);
2492 window->input_region = NULL;
2493 }
2494
2495 if (window->opaque_region) {
2496 wl_region_destroy(window->opaque_region);
2497 window->opaque_region = NULL;
2498 }
2499
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002500 if (widget->resize_handler)
2501 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002502 widget->allocation.width,
2503 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002504 widget->user_data);
2505
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002506 if (window->allocation.width != widget->allocation.width ||
2507 window->allocation.height != widget->allocation.height) {
2508 window->allocation = widget->allocation;
2509 window_schedule_redraw(window);
2510 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002511}
2512
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002513void
2514window_schedule_resize(struct window *window, int width, int height)
2515{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002516 window->pending_allocation.x = 0;
2517 window->pending_allocation.y = 0;
2518 window->pending_allocation.width = width;
2519 window->pending_allocation.height = height;
2520
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002521 window->resize_needed = 1;
2522 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002523}
2524
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002525void
2526widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2527{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002528 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002529}
2530
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002531static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002532handle_ping(void *data, struct wl_shell_surface *shell_surface,
2533 uint32_t serial)
2534{
2535 wl_shell_surface_pong(shell_surface, serial);
2536}
2537
2538static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002539handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002540 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002541{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002542 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002543
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002544 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002545 return;
2546
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002547 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002548 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002549}
2550
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002551static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002552menu_destroy(struct menu *menu)
2553{
2554 widget_destroy(menu->widget);
2555 window_destroy(menu->window);
2556 free(menu);
2557}
2558
2559static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002560handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2561{
2562 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002563 struct menu *menu = window->widget->user_data;
2564
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002565 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002566 * device. Or just use wl_callback. And this really needs to
2567 * be a window vfunc that the menu can set. And we need the
2568 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002569
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002570 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002571 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002572 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002573}
2574
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002575static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002576 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002577 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002578 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002579};
2580
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002581void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002582window_get_allocation(struct window *window,
2583 struct rectangle *allocation)
2584{
2585 *allocation = window->allocation;
2586}
2587
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002588static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002589widget_redraw(struct widget *widget)
2590{
2591 struct widget *child;
2592
2593 if (widget->redraw_handler)
2594 widget->redraw_handler(widget, widget->user_data);
2595 wl_list_for_each(child, &widget->child_list, link)
2596 widget_redraw(child);
2597}
2598
2599static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002600frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2601{
2602 struct window *window = data;
2603
2604 wl_callback_destroy(callback);
2605 window->redraw_scheduled = 0;
2606 if (window->redraw_needed)
2607 window_schedule_redraw(window);
2608}
2609
2610static const struct wl_callback_listener listener = {
2611 frame_callback
2612};
2613
2614static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002615idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002616{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002617 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002618 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002619
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002620 if (window->resize_needed)
2621 idle_resize(window);
2622
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002623 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002624 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002625 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002626 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002627 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002628
2629 callback = wl_surface_frame(window->surface);
2630 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002631}
2632
2633void
2634window_schedule_redraw(struct window *window)
2635{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002636 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002637 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002638 window->redraw_task.run = idle_redraw;
2639 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002640 window->redraw_scheduled = 1;
2641 }
2642}
2643
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002644void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002645window_set_custom(struct window *window)
2646{
2647 window->type = TYPE_CUSTOM;
2648}
2649
2650void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002651window_set_fullscreen(struct window *window, int fullscreen)
2652{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002653 if (!window->display->shell)
2654 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002655
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002656 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002657 return;
2658
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002659 if (fullscreen) {
2660 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002661 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002662 wl_shell_surface_set_fullscreen(window->shell_surface,
2663 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2664 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002665 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002666 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002667 wl_shell_surface_set_toplevel(window->shell_surface);
2668 window_schedule_resize(window,
2669 window->saved_allocation.width,
2670 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002671 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002672}
2673
2674void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002675window_set_maximized(struct window *window, int maximized)
2676{
2677 if (!window->display->shell)
2678 return;
2679
2680 if ((window->type == TYPE_MAXIMIZED) == maximized)
2681 return;
2682
2683 if (window->type == TYPE_TOPLEVEL) {
2684 window->saved_allocation = window->allocation;
2685 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2686 window->type = TYPE_MAXIMIZED;
2687 } else {
2688 wl_shell_surface_set_toplevel(window->shell_surface);
2689 window->type = TYPE_TOPLEVEL;
2690 window_schedule_resize(window,
2691 window->saved_allocation.width,
2692 window->saved_allocation.height);
2693 }
2694}
2695
2696void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002697window_set_user_data(struct window *window, void *data)
2698{
2699 window->user_data = data;
2700}
2701
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002702void *
2703window_get_user_data(struct window *window)
2704{
2705 return window->user_data;
2706}
2707
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002708void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002709window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002710 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002711{
2712 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002713}
2714
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002715void
2716window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002717 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002718{
2719 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002720}
2721
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002722void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002723window_set_data_handler(struct window *window, window_data_handler_t handler)
2724{
2725 window->data_handler = handler;
2726}
2727
2728void
2729window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2730{
2731 window->drop_handler = handler;
2732}
2733
2734void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002735window_set_close_handler(struct window *window,
2736 window_close_handler_t handler)
2737{
2738 window->close_handler = handler;
2739}
2740
2741void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002742window_set_title(struct window *window, const char *title)
2743{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002744 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002745 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002746 if (window->shell_surface)
2747 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002748}
2749
2750const char *
2751window_get_title(struct window *window)
2752{
2753 return window->title;
2754}
2755
2756void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002757window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2758{
2759 struct text_cursor_position *text_cursor_position =
2760 window->display->text_cursor_position;
2761
Scott Moreau9295ce02012-06-01 12:46:10 -06002762 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002763 return;
2764
2765 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002766 window->surface,
2767 wl_fixed_from_int(x),
2768 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002769}
2770
2771void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002772window_damage(struct window *window, int32_t x, int32_t y,
2773 int32_t width, int32_t height)
2774{
2775 wl_surface_damage(window->surface, x, y, width, height);
2776}
2777
Casey Dahlin9074db52012-04-19 22:50:09 -04002778static void
2779surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002780 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002781{
Rob Bradford7507b572012-05-15 17:55:34 +01002782 struct window *window = data;
2783 struct output *output;
2784 struct output *output_found = NULL;
2785 struct window_output *window_output;
2786
2787 wl_list_for_each(output, &window->display->output_list, link) {
2788 if (output->output == wl_output) {
2789 output_found = output;
2790 break;
2791 }
2792 }
2793
2794 if (!output_found)
2795 return;
2796
2797 window_output = malloc (sizeof *window_output);
2798 window_output->output = output_found;
2799
2800 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002801}
2802
2803static void
2804surface_leave(void *data,
2805 struct wl_surface *wl_surface, struct wl_output *output)
2806{
Rob Bradford7507b572012-05-15 17:55:34 +01002807 struct window *window = data;
2808 struct window_output *window_output;
2809 struct window_output *window_output_found = NULL;
2810
2811 wl_list_for_each(window_output, &window->window_output_list, link) {
2812 if (window_output->output->output == output) {
2813 window_output_found = window_output;
2814 break;
2815 }
2816 }
2817
2818 if (window_output_found) {
2819 wl_list_remove(&window_output_found->link);
2820 free(window_output_found);
2821 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002822}
2823
2824static const struct wl_surface_listener surface_listener = {
2825 surface_enter,
2826 surface_leave
2827};
2828
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002829static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002830window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002831{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002832 struct window *window;
2833
2834 window = malloc(sizeof *window);
2835 if (window == NULL)
2836 return NULL;
2837
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002838 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002839 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002840 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002841 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002842 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002843 if (display->shell) {
2844 window->shell_surface =
2845 wl_shell_get_shell_surface(display->shell,
2846 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002847 if (window->title)
2848 wl_shell_surface_set_title(window->shell_surface,
2849 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002850 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002851 window->allocation.x = 0;
2852 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002853 window->allocation.width = 0;
2854 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002855 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002856 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002857 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002858 window->input_region = NULL;
2859 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002860
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002861 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002862#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002863 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002864#else
2865 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2866#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002867 else
2868 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002869
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002870 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002871 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002872 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002873
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002874 if (window->shell_surface) {
2875 wl_shell_surface_set_user_data(window->shell_surface, window);
2876 wl_shell_surface_add_listener(window->shell_surface,
2877 &shell_surface_listener, window);
2878 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002879
Rob Bradford7507b572012-05-15 17:55:34 +01002880 wl_list_init (&window->window_output_list);
2881
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002882 return window;
2883}
2884
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002885struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002886window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002887{
2888 struct window *window;
2889
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002890 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002891 if (!window)
2892 return NULL;
2893
2894 return window;
2895}
2896
2897struct window *
2898window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002899 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002900{
2901 struct window *window;
2902
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002903 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002904 if (!window)
2905 return NULL;
2906
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002907 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002908 window->x = x;
2909 window->y = y;
2910
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002911 if (display->shell)
2912 wl_shell_surface_set_transient(window->shell_surface,
2913 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002914 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002915
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002916 return window;
2917}
2918
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002919static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002920menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002921{
2922 int next;
2923
2924 next = (sy - 8) / 20;
2925 if (menu->current != next) {
2926 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002927 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002928 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002929}
2930
2931static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002932menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002933 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002934 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002935{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002936 struct menu *menu = data;
2937
2938 if (widget == menu->widget)
2939 menu_set_item(data, y);
2940
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002941 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002942}
2943
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002944static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002945menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002946 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002947{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002948 struct menu *menu = data;
2949
2950 if (widget == menu->widget)
2951 menu_set_item(data, y);
2952
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002953 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002954}
2955
2956static void
2957menu_leave_handler(struct widget *widget, struct input *input, void *data)
2958{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002959 struct menu *menu = data;
2960
2961 if (widget == menu->widget)
2962 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002963}
2964
2965static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002966menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002967 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002968 uint32_t button, enum wl_pointer_button_state state,
2969 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002970
2971{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002972 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002973
Daniel Stone4dbadb12012-05-30 16:31:51 +01002974 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2975 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002976 /* Either relase after press-drag-release or
2977 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002978 menu->func(menu->window->parent,
2979 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002980 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002981 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002982 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002983}
2984
2985static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002986menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002987{
2988 cairo_t *cr;
2989 const int32_t r = 3, margin = 3;
2990 struct menu *menu = data;
2991 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002992 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002993
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002994 cr = cairo_create(window->cairo_surface);
2995 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2996 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2997 cairo_paint(cr);
2998
2999 width = window->allocation.width;
3000 height = window->allocation.height;
3001 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003002
3003 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003004 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3005 cairo_fill(cr);
3006
3007 for (i = 0; i < menu->count; i++) {
3008 if (i == menu->current) {
3009 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3010 cairo_rectangle(cr, margin, i * 20 + margin,
3011 width - 2 * margin, 20);
3012 cairo_fill(cr);
3013 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3014 cairo_move_to(cr, 10, i * 20 + 16);
3015 cairo_show_text(cr, menu->entries[i]);
3016 } else {
3017 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3018 cairo_move_to(cr, 10, i * 20 + 16);
3019 cairo_show_text(cr, menu->entries[i]);
3020 }
3021 }
3022
3023 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003024}
3025
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003026void
3027window_show_menu(struct display *display,
3028 struct input *input, uint32_t time, struct window *parent,
3029 int32_t x, int32_t y,
3030 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003031{
3032 struct window *window;
3033 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003034 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003035
3036 menu = malloc(sizeof *menu);
3037 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003038 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003039
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003040 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003041 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003042 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003043
3044 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003045 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003046 menu->entries = entries;
3047 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003048 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003049 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003050 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003051 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003052 window->type = TYPE_MENU;
3053 window->x = x;
3054 window->y = y;
3055
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003056 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003057 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003058 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003059 window->parent->shell_surface,
3060 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003061
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003062 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003063 widget_set_enter_handler(menu->widget, menu_enter_handler);
3064 widget_set_leave_handler(menu->widget, menu_leave_handler);
3065 widget_set_motion_handler(menu->widget, menu_motion_handler);
3066 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003067
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003068 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003069 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003070}
3071
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003072void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003073window_set_buffer_type(struct window *window, enum window_buffer_type type)
3074{
3075 window->buffer_type = type;
3076}
3077
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003078
3079static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003080display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003081 struct wl_output *wl_output,
3082 int x, int y,
3083 int physical_width,
3084 int physical_height,
3085 int subpixel,
3086 const char *make,
3087 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003088{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003089 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003090
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003091 output->allocation.x = x;
3092 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003093}
3094
3095static void
3096display_handle_mode(void *data,
3097 struct wl_output *wl_output,
3098 uint32_t flags,
3099 int width,
3100 int height,
3101 int refresh)
3102{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003103 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003104 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003105
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003106 if (flags & WL_OUTPUT_MODE_CURRENT) {
3107 output->allocation.width = width;
3108 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003109 if (display->output_configure_handler)
3110 (*display->output_configure_handler)(
3111 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003112 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003113}
3114
3115static const struct wl_output_listener output_listener = {
3116 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003117 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003118};
3119
3120static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003121display_add_output(struct display *d, uint32_t id)
3122{
3123 struct output *output;
3124
3125 output = malloc(sizeof *output);
3126 if (output == NULL)
3127 return;
3128
3129 memset(output, 0, sizeof *output);
3130 output->display = d;
3131 output->output =
3132 wl_display_bind(d->display, id, &wl_output_interface);
3133 wl_list_insert(d->output_list.prev, &output->link);
3134
3135 wl_output_add_listener(output->output, &output_listener, output);
3136}
3137
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003138static void
3139output_destroy(struct output *output)
3140{
3141 if (output->destroy_handler)
3142 (*output->destroy_handler)(output, output->user_data);
3143
3144 wl_output_destroy(output->output);
3145 wl_list_remove(&output->link);
3146 free(output);
3147}
3148
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003149void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003150display_set_output_configure_handler(struct display *display,
3151 display_output_handler_t handler)
3152{
3153 struct output *output;
3154
3155 display->output_configure_handler = handler;
3156 if (!handler)
3157 return;
3158
3159 wl_list_for_each(output, &display->output_list, link)
3160 (*display->output_configure_handler)(output,
3161 display->user_data);
3162}
3163
3164void
3165output_set_user_data(struct output *output, void *data)
3166{
3167 output->user_data = data;
3168}
3169
3170void *
3171output_get_user_data(struct output *output)
3172{
3173 return output->user_data;
3174}
3175
3176void
3177output_set_destroy_handler(struct output *output,
3178 display_output_handler_t handler)
3179{
3180 output->destroy_handler = handler;
3181 /* FIXME: implement this, once we have way to remove outputs */
3182}
3183
3184void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003185output_get_allocation(struct output *output, struct rectangle *allocation)
3186{
3187 *allocation = output->allocation;
3188}
3189
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003190struct wl_output *
3191output_get_wl_output(struct output *output)
3192{
3193 return output->output;
3194}
3195
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003196static void
Daniel Stone97f68542012-05-30 16:32:01 +01003197fini_xkb(struct input *input)
3198{
3199 xkb_state_unref(input->xkb.state);
3200 xkb_map_unref(input->xkb.keymap);
3201}
3202
3203static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003204display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003205{
3206 struct input *input;
3207
3208 input = malloc(sizeof *input);
3209 if (input == NULL)
3210 return;
3211
3212 memset(input, 0, sizeof *input);
3213 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003214 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003215 input->pointer_focus = NULL;
3216 input->keyboard_focus = NULL;
3217 wl_list_insert(d->input_list.prev, &input->link);
3218
Daniel Stone37816df2012-05-16 18:45:18 +01003219 wl_seat_add_listener(input->seat, &seat_listener, input);
3220 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003221
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003222 input->data_device =
3223 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003224 input->seat);
3225 wl_data_device_add_listener(input->data_device, &data_device_listener,
3226 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003227
3228 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003229}
3230
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003231static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003232input_destroy(struct input *input)
3233{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003234 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003235 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003236
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003237 if (input->xkb.state)
3238 xkb_state_unref(input->xkb.state);
3239 if (input->xkb.keymap)
3240 xkb_map_unref(input->xkb.keymap);
3241
Pekka Paalanene1207c72011-12-16 12:02:09 +02003242 if (input->drag_offer)
3243 data_offer_destroy(input->drag_offer);
3244
3245 if (input->selection_offer)
3246 data_offer_destroy(input->selection_offer);
3247
3248 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003249 fini_xkb(input);
3250
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003251 wl_surface_destroy(input->pointer_surface);
3252
Pekka Paalanene1207c72011-12-16 12:02:09 +02003253 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003254 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003255 free(input);
3256}
3257
3258static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003259display_handle_global(struct wl_display *display, uint32_t id,
3260 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003261{
3262 struct display *d = data;
3263
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003264 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003265 d->compositor =
3266 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003267 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003268 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003269 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003270 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003271 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003272 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003273 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003274 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003275 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3276 d->data_device_manager =
3277 wl_display_bind(display, id,
3278 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003279 } else if (strcmp(interface, "text_cursor_position") == 0) {
3280 d->text_cursor_position =
3281 wl_display_bind(display, id,
3282 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003283 }
3284}
3285
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003286#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003287static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003288init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003289{
3290 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003291 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003292
Rob Clark6396ed32012-03-11 19:48:41 -05003293#ifdef USE_CAIRO_GLESV2
3294# define GL_BIT EGL_OPENGL_ES2_BIT
3295#else
3296# define GL_BIT EGL_OPENGL_BIT
3297#endif
3298
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003299 static const EGLint argb_cfg_attribs[] = {
3300 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003301 EGL_RED_SIZE, 1,
3302 EGL_GREEN_SIZE, 1,
3303 EGL_BLUE_SIZE, 1,
3304 EGL_ALPHA_SIZE, 1,
3305 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003306 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003307 EGL_NONE
3308 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003309
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003310#ifdef USE_CAIRO_GLESV2
3311 static const EGLint context_attribs[] = {
3312 EGL_CONTEXT_CLIENT_VERSION, 2,
3313 EGL_NONE
3314 };
3315 EGLint api = EGL_OPENGL_ES_API;
3316#else
3317 EGLint *context_attribs = NULL;
3318 EGLint api = EGL_OPENGL_API;
3319#endif
3320
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003321 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003322 if (!eglInitialize(d->dpy, &major, &minor)) {
3323 fprintf(stderr, "failed to initialize display\n");
3324 return -1;
3325 }
3326
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003327 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003328 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3329 return -1;
3330 }
3331
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003332 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3333 &d->argb_config, 1, &n) || n != 1) {
3334 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003335 return -1;
3336 }
3337
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003338 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003339 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003340 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003341 fprintf(stderr, "failed to create context\n");
3342 return -1;
3343 }
3344
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003345 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003346 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003347 return -1;
3348 }
3349
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003350#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003351 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3352 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3353 fprintf(stderr, "failed to get cairo egl argb device\n");
3354 return -1;
3355 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003356#endif
3357
3358 return 0;
3359}
3360
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003361static void
3362fini_egl(struct display *display)
3363{
3364#ifdef HAVE_CAIRO_EGL
3365 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003366#endif
3367
3368 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3369 EGL_NO_CONTEXT);
3370
3371 eglTerminate(display->dpy);
3372 eglReleaseThread();
3373}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003374#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003375
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003376static int
3377event_mask_update(uint32_t mask, void *data)
3378{
3379 struct display *d = data;
3380
3381 d->mask = mask;
3382
3383 return 0;
3384}
3385
3386static void
3387handle_display_data(struct task *task, uint32_t events)
3388{
3389 struct display *display =
3390 container_of(task, struct display, display_task);
3391
3392 wl_display_iterate(display->display, display->mask);
3393}
3394
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003395struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003396display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003397{
3398 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003399
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003400 argc = parse_options(xkb_options,
3401 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003402
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003403 d = malloc(sizeof *d);
3404 if (d == NULL)
3405 return NULL;
3406
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003407 memset(d, 0, sizeof *d);
3408
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003409 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003410 if (d->display == NULL) {
3411 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003412 return NULL;
3413 }
3414
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003415 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003416 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3417 d->display_task.run = handle_display_data;
3418 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3419
3420 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003421 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003422 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003423
Daniel Stone97f68542012-05-30 16:32:01 +01003424 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003425 if (d->xkb_context == NULL) {
3426 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003427 return NULL;
3428 }
3429
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003430 /* Set up listener so we'll catch all events. */
3431 wl_display_add_global_listener(d->display,
3432 display_handle_global, d);
3433
3434 /* Process connection events. */
3435 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003436#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003437 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003438 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003439#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003440
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003441 d->image_target_texture_2d =
3442 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3443 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3444 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3445
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003446 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003447
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003448 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003449
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003450 wl_list_init(&d->window_list);
3451
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003452 return d;
3453}
3454
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003455static void
3456display_destroy_outputs(struct display *display)
3457{
3458 struct output *tmp;
3459 struct output *output;
3460
3461 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3462 output_destroy(output);
3463}
3464
Pekka Paalanene1207c72011-12-16 12:02:09 +02003465static void
3466display_destroy_inputs(struct display *display)
3467{
3468 struct input *tmp;
3469 struct input *input;
3470
3471 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3472 input_destroy(input);
3473}
3474
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003475void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003476display_destroy(struct display *display)
3477{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003478 if (!wl_list_empty(&display->window_list))
3479 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3480
3481 if (!wl_list_empty(&display->deferred_list))
3482 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3483
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003484 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003485 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003486
Daniel Stone97f68542012-05-30 16:32:01 +01003487 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003488
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003489 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003490 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003491
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003492#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003493 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003494#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003495
Pekka Paalanenc2052982011-12-16 11:41:32 +02003496 if (display->shell)
3497 wl_shell_destroy(display->shell);
3498
3499 if (display->shm)
3500 wl_shm_destroy(display->shm);
3501
3502 if (display->data_device_manager)
3503 wl_data_device_manager_destroy(display->data_device_manager);
3504
3505 wl_compositor_destroy(display->compositor);
3506
3507 close(display->epoll_fd);
3508
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003509 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003510 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003511 free(display);
3512}
3513
3514void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003515display_set_user_data(struct display *display, void *data)
3516{
3517 display->user_data = data;
3518}
3519
3520void *
3521display_get_user_data(struct display *display)
3522{
3523 return display->user_data;
3524}
3525
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003526struct wl_display *
3527display_get_display(struct display *display)
3528{
3529 return display->display;
3530}
3531
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003532struct output *
3533display_get_output(struct display *display)
3534{
3535 return container_of(display->output_list.next, struct output, link);
3536}
3537
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003538struct wl_compositor *
3539display_get_compositor(struct display *display)
3540{
3541 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003542}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003543
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003544uint32_t
3545display_get_serial(struct display *display)
3546{
3547 return display->serial;
3548}
3549
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003550EGLDisplay
3551display_get_egl_display(struct display *d)
3552{
3553 return d->dpy;
3554}
3555
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003556struct wl_data_source *
3557display_create_data_source(struct display *display)
3558{
3559 return wl_data_device_manager_create_data_source(display->data_device_manager);
3560}
3561
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003562EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003563display_get_argb_egl_config(struct display *d)
3564{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003565 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003566}
3567
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003568struct wl_shell *
3569display_get_shell(struct display *display)
3570{
3571 return display->shell;
3572}
3573
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003574int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003575display_acquire_window_surface(struct display *display,
3576 struct window *window,
3577 EGLContext ctx)
3578{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003579#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003580 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003581 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003582
3583 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003584 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003585 device = cairo_surface_get_device(window->cairo_surface);
3586 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003587 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003588
Benjamin Franzke0c991632011-09-27 21:57:31 +02003589 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003590 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003591 ctx = display->argb_ctx;
3592 else
3593 assert(0);
3594 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003595
3596 data = cairo_surface_get_user_data(window->cairo_surface,
3597 &surface_data_key);
3598
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003599 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003600 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003601 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3602 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003603
3604 return 0;
3605#else
3606 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003607#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003608}
3609
3610void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003611display_release_window_surface(struct display *display,
3612 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003613{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003614#ifdef HAVE_CAIRO_EGL
3615 cairo_device_t *device;
3616
3617 device = cairo_surface_get_device(window->cairo_surface);
3618 if (!device)
3619 return;
3620
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003621 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003622 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003623 cairo_device_release(device);
3624#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003625}
3626
3627void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003628display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003629{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003630 wl_list_insert(&display->deferred_list, &task->link);
3631}
3632
3633void
3634display_watch_fd(struct display *display,
3635 int fd, uint32_t events, struct task *task)
3636{
3637 struct epoll_event ep;
3638
3639 ep.events = events;
3640 ep.data.ptr = task;
3641 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3642}
3643
3644void
3645display_run(struct display *display)
3646{
3647 struct task *task;
3648 struct epoll_event ep[16];
3649 int i, count;
3650
Pekka Paalanen826d7952011-12-15 10:14:07 +02003651 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003652 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003653 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003654
3655 while (!wl_list_empty(&display->deferred_list)) {
3656 task = container_of(display->deferred_list.next,
3657 struct task, link);
3658 wl_list_remove(&task->link);
3659 task->run(task, 0);
3660 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003661
3662 if (!display->running)
3663 break;
3664
3665 wl_display_flush(display->display);
3666
3667 count = epoll_wait(display->epoll_fd,
3668 ep, ARRAY_LENGTH(ep), -1);
3669 for (i = 0; i < count; i++) {
3670 task = ep[i].data.ptr;
3671 task->run(task, ep[i].events);
3672 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003673 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003674}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003675
3676void
3677display_exit(struct display *display)
3678{
3679 display->running = 0;
3680}