blob: aaf20091a1c73f85f37701c30e335d62826aa3ec [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);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300425
426 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427}
428
Kristian Høgsberg16626282012-04-03 11:21:27 -0400429static struct wl_shm_pool *
430make_shm_pool(struct display *display, int size, void **data)
431{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400432 struct wl_shm_pool *pool;
433 int fd;
434
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300435 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400436 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300437 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
438 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400439 return NULL;
440 }
441
442 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400443 if (*data == MAP_FAILED) {
444 fprintf(stderr, "mmap failed: %m\n");
445 close(fd);
446 return NULL;
447 }
448
449 pool = wl_shm_create_pool(display->shm, fd, size);
450
451 close(fd);
452
453 return pool;
454}
455
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700456static struct shm_pool *
457shm_pool_create(struct display *display, size_t size)
458{
459 struct shm_pool *pool = malloc(sizeof *pool);
460
461 if (!pool)
462 return NULL;
463
464 pool->pool = make_shm_pool(display, size, &pool->data);
465 if (!pool->pool) {
466 free(pool);
467 return NULL;
468 }
469
470 pool->size = size;
471 pool->used = 0;
472
473 return pool;
474}
475
476static void *
477shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
478{
479 if (pool->used + size > pool->size)
480 return NULL;
481
482 *offset = pool->used;
483 pool->used += size;
484
485 return (char *) pool->data + *offset;
486}
487
488/* destroy the pool. this does not unmap the memory though */
489static void
490shm_pool_destroy(struct shm_pool *pool)
491{
492 munmap(pool->data, pool->size);
493 wl_shm_pool_destroy(pool->pool);
494 free(pool);
495}
496
497/* Start allocating from the beginning of the pool again */
498static void
499shm_pool_reset(struct shm_pool *pool)
500{
501 pool->used = 0;
502}
503
504static int
505data_length_for_shm_surface(struct rectangle *rect)
506{
507 int stride;
508
509 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
510 rect->width);
511 return stride * rect->height;
512}
513
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500514static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700515display_create_shm_surface_from_pool(struct display *display,
516 struct rectangle *rectangle,
517 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400518{
519 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400520 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400521 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700522 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400523 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524
525 data = malloc(sizeof *data);
526 if (data == NULL)
527 return NULL;
528
529 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
530 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700531 length = stride * rectangle->height;
532 data->pool = NULL;
533 map = shm_pool_allocate(pool, length, &offset);
534
535 if (!map) {
536 free(data);
537 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400538 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400540 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541 CAIRO_FORMAT_ARGB32,
542 rectangle->width,
543 rectangle->height,
544 stride);
545
546 cairo_surface_set_user_data (surface, &surface_data_key,
547 data, shm_surface_data_destroy);
548
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500550 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500552 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700554 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400555 rectangle->width,
556 rectangle->height,
557 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400558
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700559 return surface;
560}
561
562static cairo_surface_t *
563display_create_shm_surface(struct display *display,
564 struct rectangle *rectangle, uint32_t flags,
565 struct window *window)
566{
567 struct shm_surface_data *data;
568 struct shm_pool *pool;
569 cairo_surface_t *surface;
570
571 if (window && window->pool) {
572 shm_pool_reset(window->pool);
573 surface = display_create_shm_surface_from_pool(display,
574 rectangle,
575 flags,
576 window->pool);
577 if (surface)
578 return surface;
579 }
580
581 pool = shm_pool_create(display,
582 data_length_for_shm_surface(rectangle));
583 if (!pool)
584 return NULL;
585
586 surface =
587 display_create_shm_surface_from_pool(display, rectangle,
588 flags, pool);
589
590 if (!surface) {
591 shm_pool_destroy(pool);
592 return NULL;
593 }
594
595 /* make sure we destroy the pool when the surface is destroyed */
596 data = cairo_surface_get_user_data(surface, &surface_data_key);
597 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598
599 return surface;
600}
601
nobled7b87cb02011-02-01 18:51:47 +0000602static int
603check_size(struct rectangle *rect)
604{
605 if (rect->width && rect->height)
606 return 0;
607
608 fprintf(stderr, "tried to create surface of "
609 "width: %d, height: %d\n", rect->width, rect->height);
610 return -1;
611}
612
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400613cairo_surface_t *
614display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100615 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400616 struct rectangle *rectangle,
617 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618{
nobled7b87cb02011-02-01 18:51:47 +0000619 if (check_size(rectangle) < 0)
620 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500621#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300622 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400623 return display_create_egl_window_surface(display,
624 surface,
625 flags,
626 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400628 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629}
630
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300631static const char *cursors[] = {
632 "bottom_left_corner",
633 "bottom_right_corner",
634 "bottom_side",
635 "grabbing",
636 "left_ptr",
637 "left_side",
638 "right_side",
639 "top_left_corner",
640 "top_right_corner",
641 "top_side",
642 "xterm",
643 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400644 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300645};
646
647static void
648create_cursors(struct display *display)
649{
650 unsigned int i;
651
652 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
653 display->cursors =
654 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
655
656 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
657 display->cursors[i] =
658 wl_cursor_theme_get_cursor(display->cursor_theme,
659 cursors[i]);
660}
661
662static void
663destroy_cursors(struct display *display)
664{
665 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800666 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300667}
668
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300669struct wl_cursor_image *
670display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300672 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300673 wl_cursor_theme_get_cursor(display->cursor_theme,
674 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400675
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300676 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400677}
678
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400679static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200680window_get_resize_dx_dy(struct window *window, int *x, int *y)
681{
682 if (window->resize_edges & WINDOW_RESIZING_LEFT)
683 *x = window->server_allocation.width - window->allocation.width;
684 else
685 *x = 0;
686
687 if (window->resize_edges & WINDOW_RESIZING_TOP)
688 *y = window->server_allocation.height -
689 window->allocation.height;
690 else
691 *y = 0;
692
693 window->resize_edges = 0;
694}
695
696static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500697window_attach_surface(struct window *window)
698{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400699 struct display *display = window->display;
700 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000701#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100702 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000703#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500704 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100705
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400706 if (window->type == TYPE_NONE) {
707 window->type = TYPE_TOPLEVEL;
708 if (display->shell)
709 wl_shell_surface_set_toplevel(window->shell_surface);
710 }
711
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100712 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000713#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100714 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
715 data = cairo_surface_get_user_data(window->cairo_surface,
716 &surface_data_key);
717
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100718 cairo_gl_surface_swapbuffers(window->cairo_surface);
719 wl_egl_window_get_attached_size(data->window,
720 &window->server_allocation.width,
721 &window->server_allocation.height);
722 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000723#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100724 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725 buffer =
726 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400727 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200729 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100730 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400731 wl_surface_damage(window->surface, 0, 0,
732 window->allocation.width,
733 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100734 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400735 cairo_surface_destroy(window->cairo_surface);
736 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100737 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000738 default:
739 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500741
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500742 if (window->input_region) {
743 wl_surface_set_input_region(window->surface,
744 window->input_region);
745 wl_region_destroy(window->input_region);
746 window->input_region = NULL;
747 }
748
749 if (window->opaque_region) {
750 wl_surface_set_opaque_region(window->surface,
751 window->opaque_region);
752 wl_region_destroy(window->opaque_region);
753 window->opaque_region = NULL;
754 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500755}
756
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500757void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400758window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500759{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400760 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100761 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500762}
763
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400764void
765window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400766{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500767 cairo_surface_reference(surface);
768
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400769 if (window->cairo_surface != NULL)
770 cairo_surface_destroy(window->cairo_surface);
771
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500772 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400773}
774
Benjamin Franzke22d54812011-07-16 19:50:32 +0000775#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100776static void
777window_resize_cairo_window_surface(struct window *window)
778{
779 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200780 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100781
782 data = cairo_surface_get_user_data(window->cairo_surface,
783 &surface_data_key);
784
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200785 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100786 wl_egl_window_resize(data->window,
787 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200788 window->allocation.height,
789 x,y);
790
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100791 cairo_gl_surface_set_size(window->cairo_surface,
792 window->allocation.width,
793 window->allocation.height);
794}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000795#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100796
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400797struct display *
798window_get_display(struct window *window)
799{
800 return window->display;
801}
802
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400803void
804window_create_surface(struct window *window)
805{
806 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400807 uint32_t flags = 0;
808
809 if (!window->transparent)
810 flags = SURFACE_OPAQUE;
811
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400812 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500813#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
815 if (window->cairo_surface) {
816 window_resize_cairo_window_surface(window);
817 return;
818 }
819 surface = display_create_surface(window->display,
820 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400821 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823#endif
824 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400825 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400826 &window->allocation,
827 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400828 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800829 default:
830 surface = NULL;
831 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400832 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400833
834 window_set_surface(window, surface);
835 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400836}
837
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200838static void frame_destroy(struct frame *frame);
839
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400840void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500841window_destroy(struct window *window)
842{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200843 struct display *display = window->display;
844 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100845 struct window_output *window_output;
846 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200847
848 if (window->redraw_scheduled)
849 wl_list_remove(&window->redraw_task.link);
850
851 wl_list_for_each(input, &display->input_list, link) {
852 if (input->pointer_focus == window)
853 input->pointer_focus = NULL;
854 if (input->keyboard_focus == window)
855 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500856 if (input->focus_widget &&
857 input->focus_widget->window == window)
858 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200859 }
860
Rob Bradford7507b572012-05-15 17:55:34 +0100861 wl_list_for_each_safe(window_output, window_output_tmp,
862 &window->window_output_list, link) {
863 free (window_output);
864 }
865
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500866 if (window->input_region)
867 wl_region_destroy(window->input_region);
868 if (window->opaque_region)
869 wl_region_destroy(window->opaque_region);
870
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200871 if (window->frame)
872 frame_destroy(window->frame);
873
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200874 if (window->shell_surface)
875 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500876 wl_surface_destroy(window->surface);
877 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200878
879 if (window->cairo_surface != NULL)
880 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200881
882 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500883 free(window);
884}
885
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500886static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500887widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400888{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500889 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400890
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500891 wl_list_for_each(child, &widget->child_list, link) {
892 target = widget_find_widget(child, x, y);
893 if (target)
894 return target;
895 }
896
897 if (widget->allocation.x <= x &&
898 x < widget->allocation.x + widget->allocation.width &&
899 widget->allocation.y <= y &&
900 y < widget->allocation.y + widget->allocation.height) {
901 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400902 }
903
904 return NULL;
905}
906
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500907static struct widget *
908widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400909{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500910 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400911
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500912 widget = malloc(sizeof *widget);
913 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500914 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500915 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500916 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500917 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500918 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300919 widget->tooltip = NULL;
920 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500921
922 return widget;
923}
924
925struct widget *
926window_add_widget(struct window *window, void *data)
927{
928 window->widget = widget_create(window, data);
929 wl_list_init(&window->widget->link);
930
931 return window->widget;
932}
933
934struct widget *
935widget_add_widget(struct widget *parent, void *data)
936{
937 struct widget *widget;
938
939 widget = widget_create(parent->window, data);
940 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400941
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500942 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400943}
944
945void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500946widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400947{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200948 struct display *display = widget->window->display;
949 struct input *input;
950
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300951 if (widget->tooltip) {
952 free(widget->tooltip);
953 widget->tooltip = NULL;
954 }
955
Pekka Paalanene156fb62012-01-19 13:51:38 +0200956 wl_list_for_each(input, &display->input_list, link) {
957 if (input->focus_widget == widget)
958 input->focus_widget = NULL;
959 }
960
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500961 wl_list_remove(&widget->link);
962 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400963}
964
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400965void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500966widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400967{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500968 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400969}
970
971void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500972widget_set_size(struct widget *widget, int32_t width, int32_t height)
973{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500974 widget->allocation.width = width;
975 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500976}
977
978void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500979widget_set_allocation(struct widget *widget,
980 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400981{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500982 widget->allocation.x = x;
983 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200984 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400985}
986
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500987void
988widget_set_transparent(struct widget *widget, int transparent)
989{
990 widget->opaque = !transparent;
991}
992
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400993void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500994widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500996 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400997}
998
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500999void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001000widget_set_resize_handler(struct widget *widget,
1001 widget_resize_handler_t handler)
1002{
1003 widget->resize_handler = handler;
1004}
1005
1006void
1007widget_set_redraw_handler(struct widget *widget,
1008 widget_redraw_handler_t handler)
1009{
1010 widget->redraw_handler = handler;
1011}
1012
1013void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001014widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001015{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001016 widget->enter_handler = handler;
1017}
1018
1019void
1020widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1021{
1022 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001023}
1024
1025void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001026widget_set_motion_handler(struct widget *widget,
1027 widget_motion_handler_t handler)
1028{
1029 widget->motion_handler = handler;
1030}
1031
1032void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001033widget_set_button_handler(struct widget *widget,
1034 widget_button_handler_t handler)
1035{
1036 widget->button_handler = handler;
1037}
1038
1039void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001040widget_schedule_redraw(struct widget *widget)
1041{
1042 window_schedule_redraw(widget->window);
1043}
1044
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001045cairo_surface_t *
1046window_get_surface(struct window *window)
1047{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001048 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001049}
1050
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001051struct wl_surface *
1052window_get_wl_surface(struct window *window)
1053{
1054 return window->surface;
1055}
1056
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001057struct wl_shell_surface *
1058window_get_wl_shell_surface(struct window *window)
1059{
1060 return window->shell_surface;
1061}
1062
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001063static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001064tooltip_redraw_handler(struct widget *widget, void *data)
1065{
1066 cairo_t *cr;
1067 const int32_t r = 3;
1068 struct tooltip *tooltip = data;
1069 int32_t width, height;
1070 struct window *window = widget->window;
1071
1072 cr = cairo_create(window->cairo_surface);
1073 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1074 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1075 cairo_paint(cr);
1076
1077 width = window->allocation.width;
1078 height = window->allocation.height;
1079 rounded_rect(cr, 0, 0, width, height, r);
1080
1081 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1082 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1083 cairo_fill(cr);
1084
1085 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1086 cairo_move_to(cr, 10, 16);
1087 cairo_show_text(cr, tooltip->entry);
1088 cairo_destroy(cr);
1089}
1090
1091static cairo_text_extents_t
1092get_text_extents(struct tooltip *tooltip)
1093{
1094 struct window *window;
1095 cairo_t *cr;
1096 cairo_text_extents_t extents;
1097
1098 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1099 * created yet */
1100 window = tooltip->widget->window->parent;
1101 cr = cairo_create(window->cairo_surface);
1102 cairo_text_extents(cr, tooltip->entry, &extents);
1103 cairo_destroy(cr);
1104
1105 return extents;
1106}
1107
1108static int
1109window_create_tooltip(struct tooltip *tooltip)
1110{
1111 struct widget *parent = tooltip->parent;
1112 struct display *display = parent->window->display;
1113 struct window *window;
1114 const int offset_y = 27;
1115 const int margin = 3;
1116 cairo_text_extents_t extents;
1117
1118 if (tooltip->widget)
1119 return 0;
1120
1121 window = window_create_transient(display, parent->window, tooltip->x,
1122 tooltip->y + offset_y,
1123 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1124 if (!window)
1125 return -1;
1126
1127 tooltip->window = window;
1128 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1129
1130 extents = get_text_extents(tooltip);
1131 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1132 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1133
1134 return 0;
1135}
1136
1137void
1138widget_destroy_tooltip(struct widget *parent)
1139{
1140 struct tooltip *tooltip = parent->tooltip;
1141
1142 parent->tooltip_count = 0;
1143 if (!tooltip)
1144 return;
1145
1146 if (tooltip->widget) {
1147 widget_destroy(tooltip->widget);
1148 window_destroy(tooltip->window);
1149 tooltip->widget = NULL;
1150 tooltip->window = NULL;
1151 }
1152
1153 close(tooltip->tooltip_fd);
1154 free(tooltip->entry);
1155 free(tooltip);
1156 parent->tooltip = NULL;
1157}
1158
1159static void
1160tooltip_func(struct task *task, uint32_t events)
1161{
1162 struct tooltip *tooltip =
1163 container_of(task, struct tooltip, tooltip_task);
1164 uint64_t exp;
1165
1166 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1167 window_create_tooltip(tooltip);
1168}
1169
1170#define TOOLTIP_TIMEOUT 500
1171static int
1172tooltip_timer_reset(struct tooltip *tooltip)
1173{
1174 struct itimerspec its;
1175
1176 its.it_interval.tv_sec = 0;
1177 its.it_interval.tv_nsec = 0;
1178 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1179 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1180 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1181 fprintf(stderr, "could not set timerfd\n: %m");
1182 return -1;
1183 }
1184
1185 return 0;
1186}
1187
1188int
1189widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1190{
1191 struct tooltip *tooltip = parent->tooltip;
1192
1193 parent->tooltip_count++;
1194 if (tooltip) {
1195 tooltip->x = x;
1196 tooltip->y = y;
1197 tooltip_timer_reset(tooltip);
1198 return 0;
1199 }
1200
1201 /* the handler might be triggered too fast via input device motion, so
1202 * we need this check here to make sure tooltip is fully initialized */
1203 if (parent->tooltip_count > 1)
1204 return 0;
1205
1206 tooltip = malloc(sizeof *tooltip);
1207 if (!tooltip)
1208 return -1;
1209
1210 parent->tooltip = tooltip;
1211 tooltip->parent = parent;
1212 tooltip->widget = NULL;
1213 tooltip->window = NULL;
1214 tooltip->x = x;
1215 tooltip->y = y;
1216 tooltip->entry = strdup(entry);
1217 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1218 if (tooltip->tooltip_fd < 0) {
1219 fprintf(stderr, "could not create timerfd\n: %m");
1220 return -1;
1221 }
1222
1223 tooltip->tooltip_task.run = tooltip_func;
1224 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1225 EPOLLIN, &tooltip->tooltip_task);
1226 tooltip_timer_reset(tooltip);
1227
1228 return 0;
1229}
1230
1231static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001232frame_resize_handler(struct widget *widget,
1233 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001235 struct frame *frame = data;
1236 struct widget *child = frame->child;
1237 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001238 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001239 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001240 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001241 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001243 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001245 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001246 decoration_width = (t->width + t->margin) * 2;
1247 decoration_height = t->width +
1248 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001250 allocation.x = t->width + t->margin;
1251 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001252 allocation.width = width - decoration_width;
1253 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001254
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001255 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001256
1257 wl_list_for_each(button, &frame->buttons_list, link)
1258 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001259 } else {
1260 decoration_width = 0;
1261 decoration_height = 0;
1262
1263 allocation.x = 0;
1264 allocation.y = 0;
1265 allocation.width = width;
1266 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001267 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001268
1269 wl_list_for_each(button, &frame->buttons_list, link)
1270 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001271 }
1272
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001273 widget_set_allocation(child, allocation.x, allocation.y,
1274 allocation.width, allocation.height);
1275
1276 if (child->resize_handler)
1277 child->resize_handler(child,
1278 allocation.width,
1279 allocation.height,
1280 child->user_data);
1281
Scott Moreauf7e498c2012-05-14 11:39:29 -06001282 width = child->allocation.width + decoration_width;
1283 height = child->allocation.height + decoration_height;
1284
1285 widget->window->input_region =
1286 wl_compositor_create_region(display->compositor);
1287 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001288 t->margin, t->margin,
1289 width - 2 * t->margin,
1290 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001291
1292 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001293
1294 if (child->opaque) {
1295 widget->window->opaque_region =
1296 wl_compositor_create_region(display->compositor);
1297 wl_region_add(widget->window->opaque_region,
1298 opaque_margin, opaque_margin,
1299 widget->allocation.width - 2 * opaque_margin,
1300 widget->allocation.height - 2 * opaque_margin);
1301 }
Martin Minarik1998b152012-05-10 02:04:35 +02001302
1303 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001304 x_r = frame->widget->allocation.width - t->width - t->margin;
1305 x_l = t->width + t->margin;
1306 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001307 wl_list_for_each(button, &frame->buttons_list, link) {
1308 const int button_padding = 4;
1309 w = cairo_image_surface_get_width(button->icon);
1310 h = cairo_image_surface_get_height(button->icon);
1311
1312 if (button->decoration == FRAME_BUTTON_FANCY)
1313 w += 10;
1314
1315 if (button->align == FRAME_BUTTON_LEFT) {
1316 widget_set_allocation(button->widget,
1317 x_l, y , w + 1, h + 1);
1318 x_l += w;
1319 x_l += button_padding;
1320 } else {
1321 x_r -= w;
1322 widget_set_allocation(button->widget,
1323 x_r, y , w + 1, h + 1);
1324 x_r -= button_padding;
1325 }
1326 }
1327}
1328
1329static int
1330frame_button_enter_handler(struct widget *widget,
1331 struct input *input, float x, float y, void *data)
1332{
1333 struct frame_button *frame_button = data;
1334
1335 widget_schedule_redraw(frame_button->widget);
1336 frame_button->state = FRAME_BUTTON_OVER;
1337
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001338 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001339}
1340
1341static void
1342frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1343{
1344 struct frame_button *frame_button = data;
1345
1346 widget_schedule_redraw(frame_button->widget);
1347 frame_button->state = FRAME_BUTTON_DEFAULT;
1348}
1349
1350static void
1351frame_button_button_handler(struct widget *widget,
1352 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001353 uint32_t button,
1354 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001355{
1356 struct frame_button *frame_button = data;
1357 struct window *window = widget->window;
1358
1359 if (button != BTN_LEFT)
1360 return;
1361
1362 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001363 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001364 frame_button->state = FRAME_BUTTON_ACTIVE;
1365 widget_schedule_redraw(frame_button->widget);
1366
1367 if (frame_button->type == FRAME_BUTTON_ICON)
1368 window_show_frame_menu(window, input, time);
1369 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001370 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001371 frame_button->state = FRAME_BUTTON_DEFAULT;
1372 widget_schedule_redraw(frame_button->widget);
1373 break;
1374 }
1375
1376 switch (frame_button->type) {
1377 case FRAME_BUTTON_CLOSE:
1378 if (window->close_handler)
1379 window->close_handler(window->parent,
1380 window->user_data);
1381 else
1382 display_exit(window->display);
1383 break;
1384 case FRAME_BUTTON_MINIMIZE:
1385 fprintf(stderr,"Minimize stub\n");
1386 break;
1387 case FRAME_BUTTON_MAXIMIZE:
1388 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1389 break;
1390 default:
1391 /* Unknown operation */
1392 break;
1393 }
1394}
1395
1396static void
1397frame_button_redraw_handler(struct widget *widget, void *data)
1398{
1399 struct frame_button *frame_button = data;
1400 cairo_t *cr;
1401 int width, height, x, y;
1402 struct window *window = widget->window;
1403
1404 x = widget->allocation.x;
1405 y = widget->allocation.y;
1406 width = widget->allocation.width;
1407 height = widget->allocation.height;
1408
1409 if (!width)
1410 return;
1411 if (!height)
1412 return;
1413 if (widget->opaque)
1414 return;
1415
1416 cr = cairo_create(window->cairo_surface);
1417
1418 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1419 cairo_set_line_width(cr, 1);
1420
1421 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1422 cairo_rectangle (cr, x, y, 25, 16);
1423
1424 cairo_stroke_preserve(cr);
1425
1426 switch (frame_button->state) {
1427 case FRAME_BUTTON_DEFAULT:
1428 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1429 break;
1430 case FRAME_BUTTON_OVER:
1431 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1432 break;
1433 case FRAME_BUTTON_ACTIVE:
1434 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1435 break;
1436 }
1437
1438 cairo_fill (cr);
1439
1440 x += 4;
1441 }
1442
1443 cairo_set_source_surface(cr, frame_button->icon, x, y);
1444 cairo_paint(cr);
1445
1446 cairo_destroy(cr);
1447}
1448
1449static struct widget *
1450frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1451 enum frame_button_align align, enum frame_button_decoration style)
1452{
1453 struct frame_button *frame_button;
1454 const char *icon = data;
1455
1456 frame_button = malloc (sizeof *frame_button);
1457 memset(frame_button, 0, sizeof *frame_button);
1458
1459 frame_button->icon = cairo_image_surface_create_from_png(icon);
1460 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1461 frame_button->frame = frame;
1462 frame_button->type = type;
1463 frame_button->align = align;
1464 frame_button->decoration = style;
1465
1466 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1467
1468 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1469 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1470 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1471 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1472 return frame_button->widget;
1473}
1474
1475static void
1476frame_button_destroy(struct frame_button *frame_button)
1477{
1478 widget_destroy(frame_button->widget);
1479 wl_list_remove(&frame_button->link);
1480 cairo_surface_destroy(frame_button->icon);
1481 free(frame_button);
1482
1483 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001484}
1485
1486static void
1487frame_redraw_handler(struct widget *widget, void *data)
1488{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001489 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001490 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001491 struct theme *t = window->display->theme;
1492 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001493
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001494 if (window->type == TYPE_FULLSCREEN)
1495 return;
1496
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001497 cr = cairo_create(window->cairo_surface);
1498
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001500 flags |= THEME_FRAME_ACTIVE;
1501 theme_render_frame(t, cr, widget->allocation.width,
1502 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001503
1504 cairo_destroy(cr);
1505}
1506
1507static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001508frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001509{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001510 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001511 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001512
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001513 location = theme_get_location(t, input->sx, input->sy,
1514 frame->widget->allocation.width,
1515 frame->widget->allocation.height);
1516
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001517 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001518 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001519 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001521 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001522 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001523 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001533 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001534 case THEME_LOCATION_EXTERIOR:
1535 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001536 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001537 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001538 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001539}
1540
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001541static void
1542frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001543{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001544 switch (index) {
1545 case 0: /* close */
1546 if (window->close_handler)
1547 window->close_handler(window->parent,
1548 window->user_data);
1549 else
1550 display_exit(window->display);
1551 break;
1552 case 1: /* fullscreen */
1553 /* we don't have a way to get out of fullscreen for now */
1554 window_set_fullscreen(window, 1);
1555 break;
1556 case 2: /* rotate */
1557 case 3: /* scale */
1558 break;
1559 }
1560}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001561
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001562void
1563window_show_frame_menu(struct window *window,
1564 struct input *input, uint32_t time)
1565{
1566 int32_t x, y;
1567
1568 static const char *entries[] = {
1569 "Close", "Fullscreen", "Rotate", "Scale"
1570 };
1571
1572 input_get_position(input, &x, &y);
1573 window_show_menu(window->display, input, time, window,
1574 x - 10, y - 10, frame_menu_func, entries, 4);
1575}
1576
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001577static int
1578frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001579 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001580{
1581 return frame_get_pointer_image_for_location(data, input);
1582}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001583
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584static int
1585frame_motion_handler(struct widget *widget,
1586 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001587 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001588{
1589 return frame_get_pointer_image_for_location(data, input);
1590}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001591
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001592static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593frame_button_handler(struct widget *widget,
1594 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001595 uint32_t button, enum wl_pointer_button_state state,
1596 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001597
1598{
1599 struct frame *frame = data;
1600 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001601 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001602 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001603
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001604 location = theme_get_location(display->theme, input->sx, input->sy,
1605 frame->widget->allocation.width,
1606 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001607
Daniel Stone4dbadb12012-05-30 16:31:51 +01001608 if (window->display->shell && button == BTN_LEFT &&
1609 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001611 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612 if (!window->shell_surface)
1613 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001614 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001615 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001616 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001617 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001618 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001620 case THEME_LOCATION_RESIZING_TOP:
1621 case THEME_LOCATION_RESIZING_BOTTOM:
1622 case THEME_LOCATION_RESIZING_LEFT:
1623 case THEME_LOCATION_RESIZING_RIGHT:
1624 case THEME_LOCATION_RESIZING_TOP_LEFT:
1625 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1626 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1627 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001628 if (!window->shell_surface)
1629 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001630 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001631
1632 if (!display->dpy) {
1633 /* If we're using shm, allocate a big
1634 pool to create buffers out of while
1635 we resize. We should probably base
1636 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001637 window->pool =
1638 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001639 }
1640
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001641 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001642 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001643 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001644 break;
1645 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001646 } else if (button == BTN_RIGHT &&
1647 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001648 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001649 }
1650}
1651
1652struct widget *
1653frame_create(struct window *window, void *data)
1654{
1655 struct frame *frame;
1656
1657 frame = malloc(sizeof *frame);
1658 memset(frame, 0, sizeof *frame);
1659
1660 frame->widget = window_add_widget(window, frame);
1661 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001662
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001663 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1664 widget_set_resize_handler(frame->widget, frame_resize_handler);
1665 widget_set_enter_handler(frame->widget, frame_enter_handler);
1666 widget_set_motion_handler(frame->widget, frame_motion_handler);
1667 widget_set_button_handler(frame->widget, frame_button_handler);
1668
Martin Minarik1998b152012-05-10 02:04:35 +02001669 /* Create empty list for frame buttons */
1670 wl_list_init(&frame->buttons_list);
1671
1672 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1673 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1674
1675 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1676 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1677
1678 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1679 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1680
1681 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1682 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1683
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001684 window->frame = frame;
1685
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001686 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001687}
1688
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001689static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001690frame_destroy(struct frame *frame)
1691{
Martin Minarik1998b152012-05-10 02:04:35 +02001692 struct frame_button *button, *tmp;
1693
1694 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1695 frame_button_destroy(button);
1696
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001697 /* frame->child must be destroyed by the application */
1698 widget_destroy(frame->widget);
1699 free(frame);
1700}
1701
1702static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001703input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001704 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001705{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001706 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001707 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001709 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001710 return;
1711
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001712 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001713 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001714 widget = old;
1715 if (input->grab)
1716 widget = input->grab;
1717 if (widget->leave_handler)
1718 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001719 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001720 }
1721
1722 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001723 widget = focus;
1724 if (input->grab)
1725 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001726 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001727 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001728 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001729 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001730
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001731 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001732 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001733}
1734
1735static void
Daniel Stone37816df2012-05-16 18:45:18 +01001736pointer_handle_motion(void *data, struct wl_pointer *pointer,
1737 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001738{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001739 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001740 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001741 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001742 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001743 float sx = wl_fixed_to_double(sx_w);
1744 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001745
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001746 input->sx = sx;
1747 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001748
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001749 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001750 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001751 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001752 }
1753
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001754 if (input->grab)
1755 widget = input->grab;
1756 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001757 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001758 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001759 cursor = widget->motion_handler(input->focus_widget,
1760 input, time, sx, sy,
1761 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001762
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001763 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001764}
1765
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001766void
1767input_grab(struct input *input, struct widget *widget, uint32_t button)
1768{
1769 input->grab = widget;
1770 input->grab_button = button;
1771}
1772
1773void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001774input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001775{
1776 struct widget *widget;
1777
1778 input->grab = NULL;
1779 if (input->pointer_focus) {
1780 widget = widget_find_widget(input->pointer_focus->widget,
1781 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001782 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001783 }
1784}
1785
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001786static void
Daniel Stone37816df2012-05-16 18:45:18 +01001787pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001788 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001789{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001790 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001791 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001792 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001793
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001794 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001795 if (input->focus_widget && input->grab == NULL &&
1796 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001797 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001798
Neil Roberts6b28aad2012-01-23 19:11:18 +00001799 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001800 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001801 (*widget->button_handler)(widget,
1802 input, time,
1803 button, state,
1804 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001805
Daniel Stone4dbadb12012-05-30 16:31:51 +01001806 if (input->grab && input->grab_button == button &&
1807 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001808 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001809}
1810
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001811static void
Daniel Stone37816df2012-05-16 18:45:18 +01001812pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001813 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001814{
1815}
1816
1817static void
Daniel Stone37816df2012-05-16 18:45:18 +01001818keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001819 uint32_t serial, uint32_t time, uint32_t key,
1820 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001821{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001822 struct input *input = data;
1823 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001824 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001825 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001826 const xkb_keysym_t *syms;
1827 xkb_keysym_t sym;
1828 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001829
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001830 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001831 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001832 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001833 return;
1834
Daniel Stone97f68542012-05-30 16:32:01 +01001835 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001836
Daniel Stone97f68542012-05-30 16:32:01 +01001837 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001838 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001839 XKB_STATE_LATCHED);
1840 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001841 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001842 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001843 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001844 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001845 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001846 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001847
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001848 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001849 input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001850 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001851 window_set_maximized(window,
1852 window->type != TYPE_MAXIMIZED);
1853 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001854 if (num_syms == 1)
1855 sym = syms[0];
1856 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001857 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001858
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001859 (*window->key_handler)(window, input, time, key,
1860 sym, state, window->user_data);
1861 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001862}
1863
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001864static void
Daniel Stone351eb612012-05-31 15:27:47 -04001865keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1866 uint32_t serial, uint32_t mods_depressed,
1867 uint32_t mods_latched, uint32_t mods_locked,
1868 uint32_t group)
1869{
1870 struct input *input = data;
1871
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001872 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1873 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001874}
1875
1876static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001877input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001878{
1879 struct window *window = input->pointer_focus;
1880
1881 if (!window)
1882 return;
1883
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001884 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001885
Pekka Paalanene1207c72011-12-16 12:02:09 +02001886 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001887 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001888}
1889
1890static void
Daniel Stone37816df2012-05-16 18:45:18 +01001891pointer_handle_enter(void *data, struct wl_pointer *pointer,
1892 uint32_t serial, struct wl_surface *surface,
1893 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001894{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001895 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001896 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001897 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001898 float sx = wl_fixed_to_double(sx_w);
1899 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001900
Martin Minarik1998b152012-05-10 02:04:35 +02001901 if (!surface) {
1902 /* enter event for a window we've just destroyed */
1903 return;
1904 }
1905
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001906 input->display->serial = serial;
1907 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001908 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001909 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001910
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001911 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001912 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001913 window->pool = NULL;
1914 /* Schedule a redraw to free the pool */
1915 window_schedule_redraw(window);
1916 }
1917
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001918 input->sx = sx;
1919 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001920
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001921 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001922 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001923}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001924
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001925static void
Daniel Stone37816df2012-05-16 18:45:18 +01001926pointer_handle_leave(void *data, struct wl_pointer *pointer,
1927 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001928{
1929 struct input *input = data;
1930
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001931 input->display->serial = serial;
1932 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001933}
1934
1935static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001936input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001937{
1938 struct window *window = input->keyboard_focus;
1939
1940 if (!window)
1941 return;
1942
1943 window->keyboard_device = NULL;
1944 if (window->keyboard_focus_handler)
1945 (*window->keyboard_focus_handler)(window, NULL,
1946 window->user_data);
1947
1948 input->keyboard_focus = NULL;
1949}
1950
1951static void
Daniel Stone37816df2012-05-16 18:45:18 +01001952keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1953 uint32_t serial, struct wl_surface *surface,
1954 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001955{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001956 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001957 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001958
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001959 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001960 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001961
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001962 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001963 window->keyboard_device = input;
1964 if (window->keyboard_focus_handler)
1965 (*window->keyboard_focus_handler)(window,
1966 window->keyboard_device,
1967 window->user_data);
1968}
1969
1970static void
Daniel Stone37816df2012-05-16 18:45:18 +01001971keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1972 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001973{
1974 struct input *input = data;
1975
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001976 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001977 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001978}
1979
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001980static void
1981keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1982 uint32_t format, int fd, uint32_t size)
1983{
1984 struct input *input = data;
1985 char *map_str;
1986
1987 if (!data) {
1988 close(fd);
1989 return;
1990 }
1991
1992 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1993 close(fd);
1994 return;
1995 }
1996
1997 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1998 if (map_str == MAP_FAILED) {
1999 close(fd);
2000 return;
2001 }
2002
2003 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2004 map_str,
2005 XKB_KEYMAP_FORMAT_TEXT_V1,
2006 0);
2007 munmap(map_str, size);
2008 close(fd);
2009
2010 if (!input->xkb.keymap) {
2011 fprintf(stderr, "failed to compile keymap\n");
2012 return;
2013 }
2014
2015 input->xkb.state = xkb_state_new(input->xkb.keymap);
2016 if (!input->xkb.state) {
2017 fprintf(stderr, "failed to create XKB state\n");
2018 xkb_map_unref(input->xkb.keymap);
2019 input->xkb.keymap = NULL;
2020 return;
2021 }
2022
2023 input->xkb.control_mask =
2024 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2025 input->xkb.alt_mask =
2026 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2027 input->xkb.shift_mask =
2028 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2029}
2030
Daniel Stone37816df2012-05-16 18:45:18 +01002031static const struct wl_pointer_listener pointer_listener = {
2032 pointer_handle_enter,
2033 pointer_handle_leave,
2034 pointer_handle_motion,
2035 pointer_handle_button,
2036 pointer_handle_axis,
2037};
2038
2039static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002040 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002041 keyboard_handle_enter,
2042 keyboard_handle_leave,
2043 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002044 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002045};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002046
2047static void
Daniel Stone37816df2012-05-16 18:45:18 +01002048seat_handle_capabilities(void *data, struct wl_seat *seat,
2049 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002050{
Daniel Stone37816df2012-05-16 18:45:18 +01002051 struct input *input = data;
2052
2053 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2054 input->pointer = wl_seat_get_pointer(seat);
2055 wl_pointer_set_user_data(input->pointer, input);
2056 wl_pointer_add_listener(input->pointer, &pointer_listener,
2057 input);
2058 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2059 wl_pointer_destroy(input->pointer);
2060 input->pointer = NULL;
2061 }
2062
2063 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2064 input->keyboard = wl_seat_get_keyboard(seat);
2065 wl_keyboard_set_user_data(input->keyboard, input);
2066 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2067 input);
2068 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2069 wl_keyboard_destroy(input->keyboard);
2070 input->keyboard = NULL;
2071 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002072}
2073
Daniel Stone37816df2012-05-16 18:45:18 +01002074static const struct wl_seat_listener seat_listener = {
2075 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002076};
2077
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002078void
2079input_get_position(struct input *input, int32_t *x, int32_t *y)
2080{
2081 *x = input->sx;
2082 *y = input->sy;
2083}
2084
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002085struct display *
2086input_get_display(struct input *input)
2087{
2088 return input->display;
2089}
2090
Daniel Stone37816df2012-05-16 18:45:18 +01002091struct wl_seat *
2092input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002093{
Daniel Stone37816df2012-05-16 18:45:18 +01002094 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002095}
2096
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002097uint32_t
2098input_get_modifiers(struct input *input)
2099{
2100 return input->modifiers;
2101}
2102
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002103struct widget *
2104input_get_focus_widget(struct input *input)
2105{
2106 return input->focus_widget;
2107}
2108
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002109struct data_offer {
2110 struct wl_data_offer *offer;
2111 struct input *input;
2112 struct wl_array types;
2113 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002114
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002115 struct task io_task;
2116 int fd;
2117 data_func_t func;
2118 int32_t x, y;
2119 void *user_data;
2120};
2121
2122static void
2123data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2124{
2125 struct data_offer *offer = data;
2126 char **p;
2127
2128 p = wl_array_add(&offer->types, sizeof *p);
2129 *p = strdup(type);
2130}
2131
2132static const struct wl_data_offer_listener data_offer_listener = {
2133 data_offer_offer,
2134};
2135
2136static void
2137data_offer_destroy(struct data_offer *offer)
2138{
2139 char **p;
2140
2141 offer->refcount--;
2142 if (offer->refcount == 0) {
2143 wl_data_offer_destroy(offer->offer);
2144 for (p = offer->types.data; *p; p++)
2145 free(*p);
2146 wl_array_release(&offer->types);
2147 free(offer);
2148 }
2149}
2150
2151static void
2152data_device_data_offer(void *data,
2153 struct wl_data_device *data_device, uint32_t id)
2154{
2155 struct data_offer *offer;
2156
2157 offer = malloc(sizeof *offer);
2158
2159 wl_array_init(&offer->types);
2160 offer->refcount = 1;
2161 offer->input = data;
2162
2163 /* FIXME: Generate typesafe wrappers for this */
2164 offer->offer = (struct wl_data_offer *)
2165 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2166 id, &wl_data_offer_interface);
2167
2168 wl_data_offer_add_listener(offer->offer,
2169 &data_offer_listener, offer);
2170}
2171
2172static void
2173data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002174 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002175 wl_fixed_t x_w, wl_fixed_t y_w,
2176 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002177{
2178 struct input *input = data;
2179 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002180 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002181 float x = wl_fixed_to_double(x_w);
2182 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002183 char **p;
2184
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002185 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002186 window = wl_surface_get_user_data(surface);
2187 input->pointer_focus = window;
2188
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002189 if (offer) {
2190 input->drag_offer = wl_data_offer_get_user_data(offer);
2191
2192 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2193 *p = NULL;
2194
2195 types_data = input->drag_offer->types.data;
2196 } else {
2197 input->drag_offer = NULL;
2198 types_data = NULL;
2199 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002200
2201 window = input->pointer_focus;
2202 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002203 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002204 window->user_data);
2205}
2206
2207static void
2208data_device_leave(void *data, struct wl_data_device *data_device)
2209{
2210 struct input *input = data;
2211
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002212 if (input->drag_offer) {
2213 data_offer_destroy(input->drag_offer);
2214 input->drag_offer = NULL;
2215 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002216}
2217
2218static void
2219data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002220 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002221{
2222 struct input *input = data;
2223 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002224 float x = wl_fixed_to_double(x_w);
2225 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002226 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002227
2228 input->sx = x;
2229 input->sy = y;
2230
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002231 if (input->drag_offer)
2232 types_data = input->drag_offer->types.data;
2233 else
2234 types_data = NULL;
2235
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002236 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002237 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002238 window->user_data);
2239}
2240
2241static void
2242data_device_drop(void *data, struct wl_data_device *data_device)
2243{
2244 struct input *input = data;
2245 struct window *window = input->pointer_focus;
2246
2247 if (window->drop_handler)
2248 window->drop_handler(window, input,
2249 input->sx, input->sy, window->user_data);
2250}
2251
2252static void
2253data_device_selection(void *data,
2254 struct wl_data_device *wl_data_device,
2255 struct wl_data_offer *offer)
2256{
2257 struct input *input = data;
2258 char **p;
2259
2260 if (input->selection_offer)
2261 data_offer_destroy(input->selection_offer);
2262
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002263 if (offer) {
2264 input->selection_offer = wl_data_offer_get_user_data(offer);
2265 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2266 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002267 } else {
2268 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002269 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002270}
2271
2272static const struct wl_data_device_listener data_device_listener = {
2273 data_device_data_offer,
2274 data_device_enter,
2275 data_device_leave,
2276 data_device_motion,
2277 data_device_drop,
2278 data_device_selection
2279};
2280
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002281static void
2282input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002283{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002284 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002285 struct wl_cursor *cursor;
2286 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002287
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002288 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002289 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002290 return;
2291
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002292 if (index >= (int) cursor->image_count) {
2293 fprintf(stderr, "cursor index out of range\n");
2294 return;
2295 }
2296
2297 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002298 buffer = wl_cursor_image_get_buffer(image);
2299 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002300 return;
2301
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002302 wl_pointer_set_cursor(input->pointer, input->display->serial,
2303 input->pointer_surface,
2304 image->hotspot_x, image->hotspot_y);
2305 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2306 wl_surface_damage(input->pointer_surface, 0, 0,
2307 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002308}
2309
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002310static const struct wl_callback_listener pointer_surface_listener;
2311
2312static void
2313pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2314 uint32_t time)
2315{
2316 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002317 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002318 int i;
2319
2320 if (callback) {
2321 assert(callback == input->cursor_frame_cb);
2322 wl_callback_destroy(callback);
2323 input->cursor_frame_cb = NULL;
2324 }
2325
2326 if (input->current_cursor == CURSOR_UNSET)
2327 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002328 cursor = input->display->cursors[input->current_cursor];
2329 if (!cursor)
2330 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002331
2332 /* FIXME We don't have the current time on the first call so we set
2333 * the animation start to the time of the first frame callback. */
2334 if (time == 0)
2335 input->cursor_anim_start = 0;
2336 else if (input->cursor_anim_start == 0)
2337 input->cursor_anim_start = time;
2338
2339 if (time == 0 || input->cursor_anim_start == 0)
2340 i = 0;
2341 else
2342 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2343
2344 input_set_pointer_image_index(input, i);
2345
2346 if (cursor->image_count == 1)
2347 return;
2348
2349 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2350 wl_callback_add_listener(input->cursor_frame_cb,
2351 &pointer_surface_listener, input);
2352}
2353
2354static const struct wl_callback_listener pointer_surface_listener = {
2355 pointer_surface_frame_callback
2356};
2357
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002358void
2359input_set_pointer_image(struct input *input, int pointer)
2360{
2361 if (pointer == input->current_cursor)
2362 return;
2363
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002364 input->current_cursor = pointer;
2365 if (!input->cursor_frame_cb)
2366 pointer_surface_frame_callback(input, NULL, 0);
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002367}
2368
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002369struct wl_data_device *
2370input_get_data_device(struct input *input)
2371{
2372 return input->data_device;
2373}
2374
2375void
2376input_set_selection(struct input *input,
2377 struct wl_data_source *source, uint32_t time)
2378{
2379 wl_data_device_set_selection(input->data_device, source, time);
2380}
2381
2382void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002383input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002384{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002385 wl_data_offer_accept(input->drag_offer->offer,
2386 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002387}
2388
2389static void
2390offer_io_func(struct task *task, uint32_t events)
2391{
2392 struct data_offer *offer =
2393 container_of(task, struct data_offer, io_task);
2394 unsigned int len;
2395 char buffer[4096];
2396
2397 len = read(offer->fd, buffer, sizeof buffer);
2398 offer->func(buffer, len,
2399 offer->x, offer->y, offer->user_data);
2400
2401 if (len == 0) {
2402 close(offer->fd);
2403 data_offer_destroy(offer);
2404 }
2405}
2406
2407static void
2408data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2409 data_func_t func, void *user_data)
2410{
2411 int p[2];
2412
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002413 if (pipe2(p, O_CLOEXEC) == -1)
2414 return;
2415
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002416 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2417 close(p[1]);
2418
2419 offer->io_task.run = offer_io_func;
2420 offer->fd = p[0];
2421 offer->func = func;
2422 offer->refcount++;
2423 offer->user_data = user_data;
2424
2425 display_watch_fd(offer->input->display,
2426 offer->fd, EPOLLIN, &offer->io_task);
2427}
2428
2429void
2430input_receive_drag_data(struct input *input, const char *mime_type,
2431 data_func_t func, void *data)
2432{
2433 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2434 input->drag_offer->x = input->sx;
2435 input->drag_offer->y = input->sy;
2436}
2437
2438int
2439input_receive_selection_data(struct input *input, const char *mime_type,
2440 data_func_t func, void *data)
2441{
2442 char **p;
2443
2444 if (input->selection_offer == NULL)
2445 return -1;
2446
2447 for (p = input->selection_offer->types.data; *p; p++)
2448 if (strcmp(mime_type, *p) == 0)
2449 break;
2450
2451 if (*p == NULL)
2452 return -1;
2453
2454 data_offer_receive_data(input->selection_offer,
2455 mime_type, func, data);
2456 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002457}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002458
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002459int
2460input_receive_selection_data_to_fd(struct input *input,
2461 const char *mime_type, int fd)
2462{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002463 if (input->selection_offer)
2464 wl_data_offer_receive(input->selection_offer->offer,
2465 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002466
2467 return 0;
2468}
2469
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002470void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002471window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002472{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002473 if (!window->shell_surface)
2474 return;
2475
Daniel Stone37816df2012-05-16 18:45:18 +01002476 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002477}
2478
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002479static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002480idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002481{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002482 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002483
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002484 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002485 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002486 widget_set_allocation(widget,
2487 window->pending_allocation.x,
2488 window->pending_allocation.y,
2489 window->pending_allocation.width,
2490 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002491
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002492 if (window->input_region) {
2493 wl_region_destroy(window->input_region);
2494 window->input_region = NULL;
2495 }
2496
2497 if (window->opaque_region) {
2498 wl_region_destroy(window->opaque_region);
2499 window->opaque_region = NULL;
2500 }
2501
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002502 if (widget->resize_handler)
2503 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002504 widget->allocation.width,
2505 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002506 widget->user_data);
2507
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002508 if (window->allocation.width != widget->allocation.width ||
2509 window->allocation.height != widget->allocation.height) {
2510 window->allocation = widget->allocation;
2511 window_schedule_redraw(window);
2512 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002513}
2514
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002515void
2516window_schedule_resize(struct window *window, int width, int height)
2517{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002518 window->pending_allocation.x = 0;
2519 window->pending_allocation.y = 0;
2520 window->pending_allocation.width = width;
2521 window->pending_allocation.height = height;
2522
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002523 window->resize_needed = 1;
2524 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002525}
2526
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002527void
2528widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2529{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002530 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002531}
2532
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002533static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002534handle_ping(void *data, struct wl_shell_surface *shell_surface,
2535 uint32_t serial)
2536{
2537 wl_shell_surface_pong(shell_surface, serial);
2538}
2539
2540static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002541handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002542 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002543{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002544 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002545
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002546 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002547 return;
2548
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002549 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002550 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002551}
2552
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002553static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002554menu_destroy(struct menu *menu)
2555{
2556 widget_destroy(menu->widget);
2557 window_destroy(menu->window);
2558 free(menu);
2559}
2560
2561static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002562handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2563{
2564 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002565 struct menu *menu = window->widget->user_data;
2566
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002567 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002568 * device. Or just use wl_callback. And this really needs to
2569 * be a window vfunc that the menu can set. And we need the
2570 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002571
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002572 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002573 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002574 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002575}
2576
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002577static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002578 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002579 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002580 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002581};
2582
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002583void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002584window_get_allocation(struct window *window,
2585 struct rectangle *allocation)
2586{
2587 *allocation = window->allocation;
2588}
2589
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002590static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002591widget_redraw(struct widget *widget)
2592{
2593 struct widget *child;
2594
2595 if (widget->redraw_handler)
2596 widget->redraw_handler(widget, widget->user_data);
2597 wl_list_for_each(child, &widget->child_list, link)
2598 widget_redraw(child);
2599}
2600
2601static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002602frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2603{
2604 struct window *window = data;
2605
2606 wl_callback_destroy(callback);
2607 window->redraw_scheduled = 0;
2608 if (window->redraw_needed)
2609 window_schedule_redraw(window);
2610}
2611
2612static const struct wl_callback_listener listener = {
2613 frame_callback
2614};
2615
2616static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002617idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002618{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002619 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002620 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002621
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002622 if (window->resize_needed)
2623 idle_resize(window);
2624
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002625 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002626 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002627 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002628 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002629 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002630
2631 callback = wl_surface_frame(window->surface);
2632 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002633}
2634
2635void
2636window_schedule_redraw(struct window *window)
2637{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002638 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002639 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002640 window->redraw_task.run = idle_redraw;
2641 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002642 window->redraw_scheduled = 1;
2643 }
2644}
2645
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002646void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002647window_set_custom(struct window *window)
2648{
2649 window->type = TYPE_CUSTOM;
2650}
2651
2652void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002653window_set_fullscreen(struct window *window, int fullscreen)
2654{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002655 if (!window->display->shell)
2656 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002657
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002658 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002659 return;
2660
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002661 if (fullscreen) {
2662 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002663 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002664 wl_shell_surface_set_fullscreen(window->shell_surface,
2665 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2666 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002667 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002668 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002669 wl_shell_surface_set_toplevel(window->shell_surface);
2670 window_schedule_resize(window,
2671 window->saved_allocation.width,
2672 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002673 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002674}
2675
2676void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002677window_set_maximized(struct window *window, int maximized)
2678{
2679 if (!window->display->shell)
2680 return;
2681
2682 if ((window->type == TYPE_MAXIMIZED) == maximized)
2683 return;
2684
2685 if (window->type == TYPE_TOPLEVEL) {
2686 window->saved_allocation = window->allocation;
2687 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2688 window->type = TYPE_MAXIMIZED;
2689 } else {
2690 wl_shell_surface_set_toplevel(window->shell_surface);
2691 window->type = TYPE_TOPLEVEL;
2692 window_schedule_resize(window,
2693 window->saved_allocation.width,
2694 window->saved_allocation.height);
2695 }
2696}
2697
2698void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002699window_set_user_data(struct window *window, void *data)
2700{
2701 window->user_data = data;
2702}
2703
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002704void *
2705window_get_user_data(struct window *window)
2706{
2707 return window->user_data;
2708}
2709
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002710void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002711window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002712 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002713{
2714 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002715}
2716
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002717void
2718window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002719 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002720{
2721 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002722}
2723
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002724void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002725window_set_data_handler(struct window *window, window_data_handler_t handler)
2726{
2727 window->data_handler = handler;
2728}
2729
2730void
2731window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2732{
2733 window->drop_handler = handler;
2734}
2735
2736void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002737window_set_close_handler(struct window *window,
2738 window_close_handler_t handler)
2739{
2740 window->close_handler = handler;
2741}
2742
2743void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002744window_set_title(struct window *window, const char *title)
2745{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002746 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002747 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002748 if (window->shell_surface)
2749 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002750}
2751
2752const char *
2753window_get_title(struct window *window)
2754{
2755 return window->title;
2756}
2757
2758void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002759window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2760{
2761 struct text_cursor_position *text_cursor_position =
2762 window->display->text_cursor_position;
2763
Scott Moreau9295ce02012-06-01 12:46:10 -06002764 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002765 return;
2766
2767 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002768 window->surface,
2769 wl_fixed_from_int(x),
2770 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002771}
2772
2773void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002774window_damage(struct window *window, int32_t x, int32_t y,
2775 int32_t width, int32_t height)
2776{
2777 wl_surface_damage(window->surface, x, y, width, height);
2778}
2779
Casey Dahlin9074db52012-04-19 22:50:09 -04002780static void
2781surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002782 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002783{
Rob Bradford7507b572012-05-15 17:55:34 +01002784 struct window *window = data;
2785 struct output *output;
2786 struct output *output_found = NULL;
2787 struct window_output *window_output;
2788
2789 wl_list_for_each(output, &window->display->output_list, link) {
2790 if (output->output == wl_output) {
2791 output_found = output;
2792 break;
2793 }
2794 }
2795
2796 if (!output_found)
2797 return;
2798
2799 window_output = malloc (sizeof *window_output);
2800 window_output->output = output_found;
2801
2802 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002803}
2804
2805static void
2806surface_leave(void *data,
2807 struct wl_surface *wl_surface, struct wl_output *output)
2808{
Rob Bradford7507b572012-05-15 17:55:34 +01002809 struct window *window = data;
2810 struct window_output *window_output;
2811 struct window_output *window_output_found = NULL;
2812
2813 wl_list_for_each(window_output, &window->window_output_list, link) {
2814 if (window_output->output->output == output) {
2815 window_output_found = window_output;
2816 break;
2817 }
2818 }
2819
2820 if (window_output_found) {
2821 wl_list_remove(&window_output_found->link);
2822 free(window_output_found);
2823 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002824}
2825
2826static const struct wl_surface_listener surface_listener = {
2827 surface_enter,
2828 surface_leave
2829};
2830
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002831static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002832window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002833{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002834 struct window *window;
2835
2836 window = malloc(sizeof *window);
2837 if (window == NULL)
2838 return NULL;
2839
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002840 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002841 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002842 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002843 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002844 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002845 if (display->shell) {
2846 window->shell_surface =
2847 wl_shell_get_shell_surface(display->shell,
2848 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002849 if (window->title)
2850 wl_shell_surface_set_title(window->shell_surface,
2851 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002852 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002853 window->allocation.x = 0;
2854 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002855 window->allocation.width = 0;
2856 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002857 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002858 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002859 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002860 window->input_region = NULL;
2861 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002862
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002863 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002864#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002865 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002866#else
2867 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2868#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002869 else
2870 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002871
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002872 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002873 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002874 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002875
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002876 if (window->shell_surface) {
2877 wl_shell_surface_set_user_data(window->shell_surface, window);
2878 wl_shell_surface_add_listener(window->shell_surface,
2879 &shell_surface_listener, window);
2880 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002881
Rob Bradford7507b572012-05-15 17:55:34 +01002882 wl_list_init (&window->window_output_list);
2883
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002884 return window;
2885}
2886
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002887struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002888window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002889{
2890 struct window *window;
2891
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002892 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002893 if (!window)
2894 return NULL;
2895
2896 return window;
2897}
2898
2899struct window *
2900window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002901 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002902{
2903 struct window *window;
2904
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002905 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002906 if (!window)
2907 return NULL;
2908
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002909 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002910 window->x = x;
2911 window->y = y;
2912
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002913 if (display->shell)
2914 wl_shell_surface_set_transient(window->shell_surface,
2915 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002916 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002917
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002918 return window;
2919}
2920
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002921static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002922menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002923{
2924 int next;
2925
2926 next = (sy - 8) / 20;
2927 if (menu->current != next) {
2928 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002929 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002930 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002931}
2932
2933static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002934menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002935 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002936 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002937{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002938 struct menu *menu = data;
2939
2940 if (widget == menu->widget)
2941 menu_set_item(data, y);
2942
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002943 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002944}
2945
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002946static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002947menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002948 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002949{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002950 struct menu *menu = data;
2951
2952 if (widget == menu->widget)
2953 menu_set_item(data, y);
2954
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002955 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002956}
2957
2958static void
2959menu_leave_handler(struct widget *widget, struct input *input, void *data)
2960{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002961 struct menu *menu = data;
2962
2963 if (widget == menu->widget)
2964 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002965}
2966
2967static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002968menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002969 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002970 uint32_t button, enum wl_pointer_button_state state,
2971 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002972
2973{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002974 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002975
Daniel Stone4dbadb12012-05-30 16:31:51 +01002976 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2977 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002978 /* Either relase after press-drag-release or
2979 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002980 menu->func(menu->window->parent,
2981 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002982 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002983 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002984 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002985}
2986
2987static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002988menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002989{
2990 cairo_t *cr;
2991 const int32_t r = 3, margin = 3;
2992 struct menu *menu = data;
2993 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002994 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002995
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002996 cr = cairo_create(window->cairo_surface);
2997 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2998 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2999 cairo_paint(cr);
3000
3001 width = window->allocation.width;
3002 height = window->allocation.height;
3003 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003004
3005 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003006 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3007 cairo_fill(cr);
3008
3009 for (i = 0; i < menu->count; i++) {
3010 if (i == menu->current) {
3011 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3012 cairo_rectangle(cr, margin, i * 20 + margin,
3013 width - 2 * margin, 20);
3014 cairo_fill(cr);
3015 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3016 cairo_move_to(cr, 10, i * 20 + 16);
3017 cairo_show_text(cr, menu->entries[i]);
3018 } else {
3019 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3020 cairo_move_to(cr, 10, i * 20 + 16);
3021 cairo_show_text(cr, menu->entries[i]);
3022 }
3023 }
3024
3025 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003026}
3027
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003028void
3029window_show_menu(struct display *display,
3030 struct input *input, uint32_t time, struct window *parent,
3031 int32_t x, int32_t y,
3032 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003033{
3034 struct window *window;
3035 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003036 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003037
3038 menu = malloc(sizeof *menu);
3039 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003040 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003041
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003042 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003043 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003044 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003045
3046 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003047 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003048 menu->entries = entries;
3049 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003050 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003051 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003052 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003053 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003054 window->type = TYPE_MENU;
3055 window->x = x;
3056 window->y = y;
3057
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003058 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003059 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003060 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003061 window->parent->shell_surface,
3062 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003063
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003064 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003065 widget_set_enter_handler(menu->widget, menu_enter_handler);
3066 widget_set_leave_handler(menu->widget, menu_leave_handler);
3067 widget_set_motion_handler(menu->widget, menu_motion_handler);
3068 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003069
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003070 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003071 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003072}
3073
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003074void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003075window_set_buffer_type(struct window *window, enum window_buffer_type type)
3076{
3077 window->buffer_type = type;
3078}
3079
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003080
3081static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003082display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003083 struct wl_output *wl_output,
3084 int x, int y,
3085 int physical_width,
3086 int physical_height,
3087 int subpixel,
3088 const char *make,
3089 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003090{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003091 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003092
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003093 output->allocation.x = x;
3094 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003095}
3096
3097static void
3098display_handle_mode(void *data,
3099 struct wl_output *wl_output,
3100 uint32_t flags,
3101 int width,
3102 int height,
3103 int refresh)
3104{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003105 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003106 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003107
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003108 if (flags & WL_OUTPUT_MODE_CURRENT) {
3109 output->allocation.width = width;
3110 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003111 if (display->output_configure_handler)
3112 (*display->output_configure_handler)(
3113 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003114 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003115}
3116
3117static const struct wl_output_listener output_listener = {
3118 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003119 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003120};
3121
3122static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003123display_add_output(struct display *d, uint32_t id)
3124{
3125 struct output *output;
3126
3127 output = malloc(sizeof *output);
3128 if (output == NULL)
3129 return;
3130
3131 memset(output, 0, sizeof *output);
3132 output->display = d;
3133 output->output =
3134 wl_display_bind(d->display, id, &wl_output_interface);
3135 wl_list_insert(d->output_list.prev, &output->link);
3136
3137 wl_output_add_listener(output->output, &output_listener, output);
3138}
3139
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003140static void
3141output_destroy(struct output *output)
3142{
3143 if (output->destroy_handler)
3144 (*output->destroy_handler)(output, output->user_data);
3145
3146 wl_output_destroy(output->output);
3147 wl_list_remove(&output->link);
3148 free(output);
3149}
3150
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003151void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003152display_set_output_configure_handler(struct display *display,
3153 display_output_handler_t handler)
3154{
3155 struct output *output;
3156
3157 display->output_configure_handler = handler;
3158 if (!handler)
3159 return;
3160
3161 wl_list_for_each(output, &display->output_list, link)
3162 (*display->output_configure_handler)(output,
3163 display->user_data);
3164}
3165
3166void
3167output_set_user_data(struct output *output, void *data)
3168{
3169 output->user_data = data;
3170}
3171
3172void *
3173output_get_user_data(struct output *output)
3174{
3175 return output->user_data;
3176}
3177
3178void
3179output_set_destroy_handler(struct output *output,
3180 display_output_handler_t handler)
3181{
3182 output->destroy_handler = handler;
3183 /* FIXME: implement this, once we have way to remove outputs */
3184}
3185
3186void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003187output_get_allocation(struct output *output, struct rectangle *allocation)
3188{
3189 *allocation = output->allocation;
3190}
3191
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003192struct wl_output *
3193output_get_wl_output(struct output *output)
3194{
3195 return output->output;
3196}
3197
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003198static void
Daniel Stone97f68542012-05-30 16:32:01 +01003199fini_xkb(struct input *input)
3200{
3201 xkb_state_unref(input->xkb.state);
3202 xkb_map_unref(input->xkb.keymap);
3203}
3204
3205static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003206display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003207{
3208 struct input *input;
3209
3210 input = malloc(sizeof *input);
3211 if (input == NULL)
3212 return;
3213
3214 memset(input, 0, sizeof *input);
3215 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003216 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003217 input->pointer_focus = NULL;
3218 input->keyboard_focus = NULL;
3219 wl_list_insert(d->input_list.prev, &input->link);
3220
Daniel Stone37816df2012-05-16 18:45:18 +01003221 wl_seat_add_listener(input->seat, &seat_listener, input);
3222 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003223
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003224 input->data_device =
3225 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003226 input->seat);
3227 wl_data_device_add_listener(input->data_device, &data_device_listener,
3228 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003229
3230 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003231}
3232
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003233static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003234input_destroy(struct input *input)
3235{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003236 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003237 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003238
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003239 if (input->xkb.state)
3240 xkb_state_unref(input->xkb.state);
3241 if (input->xkb.keymap)
3242 xkb_map_unref(input->xkb.keymap);
3243
Pekka Paalanene1207c72011-12-16 12:02:09 +02003244 if (input->drag_offer)
3245 data_offer_destroy(input->drag_offer);
3246
3247 if (input->selection_offer)
3248 data_offer_destroy(input->selection_offer);
3249
3250 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003251 fini_xkb(input);
3252
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003253 wl_surface_destroy(input->pointer_surface);
3254
Pekka Paalanene1207c72011-12-16 12:02:09 +02003255 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003256 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003257 free(input);
3258}
3259
3260static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003261display_handle_global(struct wl_display *display, uint32_t id,
3262 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003263{
3264 struct display *d = data;
3265
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003266 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003267 d->compositor =
3268 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003269 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003270 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003271 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003272 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003273 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003274 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003275 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003276 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003277 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3278 d->data_device_manager =
3279 wl_display_bind(display, id,
3280 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003281 } else if (strcmp(interface, "text_cursor_position") == 0) {
3282 d->text_cursor_position =
3283 wl_display_bind(display, id,
3284 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003285 }
3286}
3287
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003288#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003289static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003290init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003291{
3292 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003293 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003294
Rob Clark6396ed32012-03-11 19:48:41 -05003295#ifdef USE_CAIRO_GLESV2
3296# define GL_BIT EGL_OPENGL_ES2_BIT
3297#else
3298# define GL_BIT EGL_OPENGL_BIT
3299#endif
3300
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003301 static const EGLint argb_cfg_attribs[] = {
3302 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003303 EGL_RED_SIZE, 1,
3304 EGL_GREEN_SIZE, 1,
3305 EGL_BLUE_SIZE, 1,
3306 EGL_ALPHA_SIZE, 1,
3307 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003308 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003309 EGL_NONE
3310 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003311
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003312#ifdef USE_CAIRO_GLESV2
3313 static const EGLint context_attribs[] = {
3314 EGL_CONTEXT_CLIENT_VERSION, 2,
3315 EGL_NONE
3316 };
3317 EGLint api = EGL_OPENGL_ES_API;
3318#else
3319 EGLint *context_attribs = NULL;
3320 EGLint api = EGL_OPENGL_API;
3321#endif
3322
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003323 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003324 if (!eglInitialize(d->dpy, &major, &minor)) {
3325 fprintf(stderr, "failed to initialize display\n");
3326 return -1;
3327 }
3328
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003329 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003330 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3331 return -1;
3332 }
3333
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003334 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3335 &d->argb_config, 1, &n) || n != 1) {
3336 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003337 return -1;
3338 }
3339
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003340 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003341 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003342 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003343 fprintf(stderr, "failed to create context\n");
3344 return -1;
3345 }
3346
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003347 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003348 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003349 return -1;
3350 }
3351
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003352#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003353 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3354 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3355 fprintf(stderr, "failed to get cairo egl argb device\n");
3356 return -1;
3357 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003358#endif
3359
3360 return 0;
3361}
3362
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003363static void
3364fini_egl(struct display *display)
3365{
3366#ifdef HAVE_CAIRO_EGL
3367 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003368#endif
3369
3370 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3371 EGL_NO_CONTEXT);
3372
3373 eglTerminate(display->dpy);
3374 eglReleaseThread();
3375}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003376#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003377
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003378static int
3379event_mask_update(uint32_t mask, void *data)
3380{
3381 struct display *d = data;
3382
3383 d->mask = mask;
3384
3385 return 0;
3386}
3387
3388static void
3389handle_display_data(struct task *task, uint32_t events)
3390{
3391 struct display *display =
3392 container_of(task, struct display, display_task);
3393
3394 wl_display_iterate(display->display, display->mask);
3395}
3396
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003397struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003398display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003399{
3400 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003401
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003402 argc = parse_options(xkb_options,
3403 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003404
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003405 d = malloc(sizeof *d);
3406 if (d == NULL)
3407 return NULL;
3408
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003409 memset(d, 0, sizeof *d);
3410
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003411 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003412 if (d->display == NULL) {
3413 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003414 return NULL;
3415 }
3416
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003417 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003418 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3419 d->display_task.run = handle_display_data;
3420 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3421
3422 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003423 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003424 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003425
Daniel Stone97f68542012-05-30 16:32:01 +01003426 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003427 if (d->xkb_context == NULL) {
3428 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003429 return NULL;
3430 }
3431
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003432 /* Set up listener so we'll catch all events. */
3433 wl_display_add_global_listener(d->display,
3434 display_handle_global, d);
3435
3436 /* Process connection events. */
3437 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003438#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003439 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003440 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003441#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003442
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003443 d->image_target_texture_2d =
3444 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3445 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3446 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3447
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003448 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003449
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003450 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003451
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003452 wl_list_init(&d->window_list);
3453
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003454 return d;
3455}
3456
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003457static void
3458display_destroy_outputs(struct display *display)
3459{
3460 struct output *tmp;
3461 struct output *output;
3462
3463 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3464 output_destroy(output);
3465}
3466
Pekka Paalanene1207c72011-12-16 12:02:09 +02003467static void
3468display_destroy_inputs(struct display *display)
3469{
3470 struct input *tmp;
3471 struct input *input;
3472
3473 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3474 input_destroy(input);
3475}
3476
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003477void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003478display_destroy(struct display *display)
3479{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003480 if (!wl_list_empty(&display->window_list))
3481 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3482
3483 if (!wl_list_empty(&display->deferred_list))
3484 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3485
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003486 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003487 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003488
Daniel Stone97f68542012-05-30 16:32:01 +01003489 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003490
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003491 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003492 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003493
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003494#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003495 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003496#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003497
Pekka Paalanenc2052982011-12-16 11:41:32 +02003498 if (display->shell)
3499 wl_shell_destroy(display->shell);
3500
3501 if (display->shm)
3502 wl_shm_destroy(display->shm);
3503
3504 if (display->data_device_manager)
3505 wl_data_device_manager_destroy(display->data_device_manager);
3506
3507 wl_compositor_destroy(display->compositor);
3508
3509 close(display->epoll_fd);
3510
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003511 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003512 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003513 free(display);
3514}
3515
3516void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003517display_set_user_data(struct display *display, void *data)
3518{
3519 display->user_data = data;
3520}
3521
3522void *
3523display_get_user_data(struct display *display)
3524{
3525 return display->user_data;
3526}
3527
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003528struct wl_display *
3529display_get_display(struct display *display)
3530{
3531 return display->display;
3532}
3533
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003534struct output *
3535display_get_output(struct display *display)
3536{
3537 return container_of(display->output_list.next, struct output, link);
3538}
3539
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003540struct wl_compositor *
3541display_get_compositor(struct display *display)
3542{
3543 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003544}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003545
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003546uint32_t
3547display_get_serial(struct display *display)
3548{
3549 return display->serial;
3550}
3551
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003552EGLDisplay
3553display_get_egl_display(struct display *d)
3554{
3555 return d->dpy;
3556}
3557
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003558struct wl_data_source *
3559display_create_data_source(struct display *display)
3560{
3561 return wl_data_device_manager_create_data_source(display->data_device_manager);
3562}
3563
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003564EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003565display_get_argb_egl_config(struct display *d)
3566{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003567 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003568}
3569
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003570struct wl_shell *
3571display_get_shell(struct display *display)
3572{
3573 return display->shell;
3574}
3575
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003576int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003577display_acquire_window_surface(struct display *display,
3578 struct window *window,
3579 EGLContext ctx)
3580{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003581#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003582 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003583 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003584
3585 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003586 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003587 device = cairo_surface_get_device(window->cairo_surface);
3588 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003589 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003590
Benjamin Franzke0c991632011-09-27 21:57:31 +02003591 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003592 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003593 ctx = display->argb_ctx;
3594 else
3595 assert(0);
3596 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003597
3598 data = cairo_surface_get_user_data(window->cairo_surface,
3599 &surface_data_key);
3600
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003601 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003602 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003603 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3604 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003605
3606 return 0;
3607#else
3608 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003609#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003610}
3611
3612void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003613display_release_window_surface(struct display *display,
3614 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003615{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003616#ifdef HAVE_CAIRO_EGL
3617 cairo_device_t *device;
3618
3619 device = cairo_surface_get_device(window->cairo_surface);
3620 if (!device)
3621 return;
3622
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003623 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003624 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003625 cairo_device_release(device);
3626#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003627}
3628
3629void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003630display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003631{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003632 wl_list_insert(&display->deferred_list, &task->link);
3633}
3634
3635void
3636display_watch_fd(struct display *display,
3637 int fd, uint32_t events, struct task *task)
3638{
3639 struct epoll_event ep;
3640
3641 ep.events = events;
3642 ep.data.ptr = task;
3643 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3644}
3645
3646void
3647display_run(struct display *display)
3648{
3649 struct task *task;
3650 struct epoll_event ep[16];
3651 int i, count;
3652
Pekka Paalanen826d7952011-12-15 10:14:07 +02003653 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003654 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003655 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003656
3657 while (!wl_list_empty(&display->deferred_list)) {
3658 task = container_of(display->deferred_list.next,
3659 struct task, link);
3660 wl_list_remove(&task->link);
3661 task->run(task, 0);
3662 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003663
3664 if (!display->running)
3665 break;
3666
3667 wl_display_flush(display->display);
3668
3669 count = epoll_wait(display->epoll_fd,
3670 ep, ARRAY_LENGTH(ep), -1);
3671 for (i = 0; i < count; i++) {
3672 task = ep[i].data.ptr;
3673 task->run(task, ep[i].events);
3674 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003675 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003676}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003677
3678void
3679display_exit(struct display *display)
3680{
3681 display->running = 0;
3682}