blob: 3ef648ef142e7a111271b125126e8c85d07cc129 [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"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050065
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050067
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070068struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030069
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050070struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050071 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050072 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040073 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040074 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040075 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060076 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040077 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050078 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020079 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040081 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040082
83 int display_fd;
84 uint32_t mask;
85 struct task display_task;
86
87 int epoll_fd;
88 struct wl_list deferred_list;
89
Pekka Paalanen826d7952011-12-15 10:14:07 +020090 int running;
91
Kristian Høgsberg478d9262010-06-08 20:34:11 -040092 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040093 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050094 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040095
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040096 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040097
98 struct {
99 struct xkb_rule_names names;
100 struct xkb_keymap *keymap;
101 struct xkb_state *state;
102 struct xkb_context *context;
103 xkb_mod_mask_t control_mask;
104 xkb_mod_mask_t alt_mask;
105 xkb_mod_mask_t shift_mask;
106 } xkb;
107
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300108 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300109 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400110
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500111 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
112 PFNEGLCREATEIMAGEKHRPROC create_image;
113 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200114
115 display_output_handler_t output_configure_handler;
116
117 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500118};
119
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400121 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400122 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400123 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500124 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400125 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500126 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400127 TYPE_CUSTOM
128};
Rob Bradford7507b572012-05-15 17:55:34 +0100129
130struct window_output {
131 struct output *output;
132 struct wl_list link;
133};
134
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500135struct window {
136 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500137 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100138 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500139 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200140 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500141 struct wl_region *input_region;
142 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500143 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500144 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500145 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500146 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400147 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400148 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400149 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400150 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400151 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400152 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400153 int transparent;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600154 int send_cursor_position;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400155 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400156 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500157
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400158 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500159
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700160 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400161
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500162 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500163 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400164 window_data_handler_t data_handler;
165 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500166 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400167
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200168 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500169 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500170
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500171 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400172 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500173};
174
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500175struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500176 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300177 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500178 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179 struct wl_list link;
180 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500181 widget_resize_handler_t resize_handler;
182 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500183 widget_enter_handler_t enter_handler;
184 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500185 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500186 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400187 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500188 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300189 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400190};
191
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400192struct input {
193 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100194 struct wl_seat *seat;
195 struct wl_pointer *pointer;
196 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400197 struct window *pointer_focus;
198 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300199 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400200 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400201 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400202 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400203 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400204
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500205 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500206 struct widget *grab;
207 uint32_t grab_button;
208
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400209 struct wl_data_device *data_device;
210 struct data_offer *drag_offer;
211 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400212};
213
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500214struct output {
215 struct display *display;
216 struct wl_output *output;
217 struct rectangle allocation;
218 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200219
220 display_output_handler_t destroy_handler;
221 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500222};
223
Martin Minarik1998b152012-05-10 02:04:35 +0200224enum frame_button_action {
225 FRAME_BUTTON_NULL = 0,
226 FRAME_BUTTON_ICON = 1,
227 FRAME_BUTTON_CLOSE = 2,
228 FRAME_BUTTON_MINIMIZE = 3,
229 FRAME_BUTTON_MAXIMIZE = 4,
230};
231
232enum frame_button_pointer {
233 FRAME_BUTTON_DEFAULT = 0,
234 FRAME_BUTTON_OVER = 1,
235 FRAME_BUTTON_ACTIVE = 2,
236};
237
238enum frame_button_align {
239 FRAME_BUTTON_RIGHT = 0,
240 FRAME_BUTTON_LEFT = 1,
241};
242
243enum frame_button_decoration {
244 FRAME_BUTTON_NONE = 0,
245 FRAME_BUTTON_FANCY = 1,
246};
247
248struct frame_button {
249 struct widget *widget;
250 struct frame *frame;
251 cairo_surface_t *icon;
252 enum frame_button_action type;
253 enum frame_button_pointer state;
254 struct wl_list link; /* buttons_list */
255 enum frame_button_align align;
256 enum frame_button_decoration decoration;
257};
258
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500259struct frame {
260 struct widget *widget;
261 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200262 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500263};
264
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500265struct menu {
266 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500267 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500268 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500269 const char **entries;
270 uint32_t time;
271 int current;
272 int count;
273 menu_func_t func;
274};
275
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300276struct tooltip {
277 struct widget *parent;
278 struct window *window;
279 struct widget *widget;
280 char *entry;
281 struct task tooltip_task;
282 int tooltip_fd;
283 float x, y;
284};
285
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700286struct shm_pool {
287 struct wl_shm_pool *pool;
288 size_t size;
289 size_t used;
290 void *data;
291};
292
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400293enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300294 CURSOR_DEFAULT = 100,
295 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400296};
297
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500298enum window_location {
299 WINDOW_INTERIOR = 0,
300 WINDOW_RESIZING_TOP = 1,
301 WINDOW_RESIZING_BOTTOM = 2,
302 WINDOW_RESIZING_LEFT = 4,
303 WINDOW_RESIZING_TOP_LEFT = 5,
304 WINDOW_RESIZING_BOTTOM_LEFT = 6,
305 WINDOW_RESIZING_RIGHT = 8,
306 WINDOW_RESIZING_TOP_RIGHT = 9,
307 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
308 WINDOW_RESIZING_MASK = 15,
309 WINDOW_EXTERIOR = 16,
310 WINDOW_TITLEBAR = 17,
311 WINDOW_CLIENT_AREA = 18,
312};
313
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400314const char *option_xkb_layout = "us";
315const char *option_xkb_variant = "";
316const char *option_xkb_options = "";
317
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400318static const struct weston_option xkb_options[] = {
319 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
320 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
321 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400322};
323
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324static const cairo_user_data_key_t surface_data_key;
325struct surface_data {
326 struct wl_buffer *buffer;
327};
328
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500329#define MULT(_d,c,a,t) \
330 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
331
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500332#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100334struct egl_window_surface_data {
335 struct display *display;
336 struct wl_surface *surface;
337 struct wl_egl_window *window;
338 EGLSurface surf;
339};
340
341static void
342egl_window_surface_data_destroy(void *p)
343{
344 struct egl_window_surface_data *data = p;
345 struct display *d = data->display;
346
347 eglDestroySurface(d->dpy, data->surf);
348 wl_egl_window_destroy(data->window);
349 data->surface = NULL;
350
351 free(p);
352}
353
354static cairo_surface_t *
355display_create_egl_window_surface(struct display *display,
356 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400357 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100358 struct rectangle *rectangle)
359{
360 cairo_surface_t *cairo_surface;
361 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400362 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200363 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100364
365 data = malloc(sizeof *data);
366 if (data == NULL)
367 return NULL;
368
369 data->display = display;
370 data->surface = surface;
371
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500372 config = display->argb_config;
373 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400375 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100376 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500380 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381
Benjamin Franzke0c991632011-09-27 21:57:31 +0200382 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383 data->surf,
384 rectangle->width,
385 rectangle->height);
386
387 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
388 data, egl_window_surface_data_destroy);
389
390 return cairo_surface;
391}
392
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393#endif
394
395struct wl_buffer *
396display_get_buffer_for_surface(struct display *display,
397 cairo_surface_t *surface)
398{
399 struct surface_data *data;
400
401 data = cairo_surface_get_user_data (surface, &surface_data_key);
402
403 return data->buffer;
404}
405
406struct shm_surface_data {
407 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700408 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409};
410
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500411static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700412shm_pool_destroy(struct shm_pool *pool);
413
414static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415shm_surface_data_destroy(void *p)
416{
417 struct shm_surface_data *data = p;
418
419 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700420 if (data->pool)
421 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422}
423
Kristian Høgsberg16626282012-04-03 11:21:27 -0400424static struct wl_shm_pool *
425make_shm_pool(struct display *display, int size, void **data)
426{
427 char filename[] = "/tmp/wayland-shm-XXXXXX";
428 struct wl_shm_pool *pool;
429 int fd;
430
431 fd = mkstemp(filename);
432 if (fd < 0) {
433 fprintf(stderr, "open %s failed: %m\n", filename);
434 return NULL;
435 }
436 if (ftruncate(fd, size) < 0) {
437 fprintf(stderr, "ftruncate failed: %m\n");
438 close(fd);
439 return NULL;
440 }
441
442 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
443 unlink(filename);
444
445 if (*data == MAP_FAILED) {
446 fprintf(stderr, "mmap failed: %m\n");
447 close(fd);
448 return NULL;
449 }
450
451 pool = wl_shm_create_pool(display->shm, fd, size);
452
453 close(fd);
454
455 return pool;
456}
457
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700458static struct shm_pool *
459shm_pool_create(struct display *display, size_t size)
460{
461 struct shm_pool *pool = malloc(sizeof *pool);
462
463 if (!pool)
464 return NULL;
465
466 pool->pool = make_shm_pool(display, size, &pool->data);
467 if (!pool->pool) {
468 free(pool);
469 return NULL;
470 }
471
472 pool->size = size;
473 pool->used = 0;
474
475 return pool;
476}
477
478static void *
479shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
480{
481 if (pool->used + size > pool->size)
482 return NULL;
483
484 *offset = pool->used;
485 pool->used += size;
486
487 return (char *) pool->data + *offset;
488}
489
490/* destroy the pool. this does not unmap the memory though */
491static void
492shm_pool_destroy(struct shm_pool *pool)
493{
494 munmap(pool->data, pool->size);
495 wl_shm_pool_destroy(pool->pool);
496 free(pool);
497}
498
499/* Start allocating from the beginning of the pool again */
500static void
501shm_pool_reset(struct shm_pool *pool)
502{
503 pool->used = 0;
504}
505
506static int
507data_length_for_shm_surface(struct rectangle *rect)
508{
509 int stride;
510
511 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
512 rect->width);
513 return stride * rect->height;
514}
515
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500516static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700517display_create_shm_surface_from_pool(struct display *display,
518 struct rectangle *rectangle,
519 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520{
521 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400522 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400523 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700524 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400525 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526
527 data = malloc(sizeof *data);
528 if (data == NULL)
529 return NULL;
530
531 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
532 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700533 length = stride * rectangle->height;
534 data->pool = NULL;
535 map = shm_pool_allocate(pool, length, &offset);
536
537 if (!map) {
538 free(data);
539 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400540 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400542 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400543 CAIRO_FORMAT_ARGB32,
544 rectangle->width,
545 rectangle->height,
546 stride);
547
548 cairo_surface_set_user_data (surface, &surface_data_key,
549 data, shm_surface_data_destroy);
550
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500552 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500554 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700556 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400557 rectangle->width,
558 rectangle->height,
559 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700561 return surface;
562}
563
564static cairo_surface_t *
565display_create_shm_surface(struct display *display,
566 struct rectangle *rectangle, uint32_t flags,
567 struct window *window)
568{
569 struct shm_surface_data *data;
570 struct shm_pool *pool;
571 cairo_surface_t *surface;
572
573 if (window && window->pool) {
574 shm_pool_reset(window->pool);
575 surface = display_create_shm_surface_from_pool(display,
576 rectangle,
577 flags,
578 window->pool);
579 if (surface)
580 return surface;
581 }
582
583 pool = shm_pool_create(display,
584 data_length_for_shm_surface(rectangle));
585 if (!pool)
586 return NULL;
587
588 surface =
589 display_create_shm_surface_from_pool(display, rectangle,
590 flags, pool);
591
592 if (!surface) {
593 shm_pool_destroy(pool);
594 return NULL;
595 }
596
597 /* make sure we destroy the pool when the surface is destroyed */
598 data = cairo_surface_get_user_data(surface, &surface_data_key);
599 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400600
601 return surface;
602}
603
nobled7b87cb02011-02-01 18:51:47 +0000604static int
605check_size(struct rectangle *rect)
606{
607 if (rect->width && rect->height)
608 return 0;
609
610 fprintf(stderr, "tried to create surface of "
611 "width: %d, height: %d\n", rect->width, rect->height);
612 return -1;
613}
614
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615cairo_surface_t *
616display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100617 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400618 struct rectangle *rectangle,
619 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400620{
nobled7b87cb02011-02-01 18:51:47 +0000621 if (check_size(rectangle) < 0)
622 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500623#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300624 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400625 return display_create_egl_window_surface(display,
626 surface,
627 flags,
628 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400630 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631}
632
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300633static const char *cursors[] = {
634 "bottom_left_corner",
635 "bottom_right_corner",
636 "bottom_side",
637 "grabbing",
638 "left_ptr",
639 "left_side",
640 "right_side",
641 "top_left_corner",
642 "top_right_corner",
643 "top_side",
644 "xterm",
645 "hand1",
646};
647
648static void
649create_cursors(struct display *display)
650{
651 unsigned int i;
652
653 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
654 display->cursors =
655 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
656
657 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
658 display->cursors[i] =
659 wl_cursor_theme_get_cursor(display->cursor_theme,
660 cursors[i]);
661}
662
663static void
664destroy_cursors(struct display *display)
665{
666 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800667 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300668}
669
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300670struct wl_cursor_image *
671display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400672{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300673 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300674 wl_cursor_theme_get_cursor(display->cursor_theme,
675 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400676
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300677 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400678}
679
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400680static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200681window_get_resize_dx_dy(struct window *window, int *x, int *y)
682{
683 if (window->resize_edges & WINDOW_RESIZING_LEFT)
684 *x = window->server_allocation.width - window->allocation.width;
685 else
686 *x = 0;
687
688 if (window->resize_edges & WINDOW_RESIZING_TOP)
689 *y = window->server_allocation.height -
690 window->allocation.height;
691 else
692 *y = 0;
693
694 window->resize_edges = 0;
695}
696
697static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500698window_attach_surface(struct window *window)
699{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400700 struct display *display = window->display;
701 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000702#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100703 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000704#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500705 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100706
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400707 if (window->type == TYPE_NONE) {
708 window->type = TYPE_TOPLEVEL;
709 if (display->shell)
710 wl_shell_surface_set_toplevel(window->shell_surface);
711 }
712
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100713 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000714#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100715 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
716 data = cairo_surface_get_user_data(window->cairo_surface,
717 &surface_data_key);
718
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100719 cairo_gl_surface_swapbuffers(window->cairo_surface);
720 wl_egl_window_get_attached_size(data->window,
721 &window->server_allocation.width,
722 &window->server_allocation.height);
723 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000724#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726 buffer =
727 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400728 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200730 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400732 wl_surface_damage(window->surface, 0, 0,
733 window->allocation.width,
734 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400736 cairo_surface_destroy(window->cairo_surface);
737 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100738 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000739 default:
740 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100741 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500742
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500743 if (window->input_region) {
744 wl_surface_set_input_region(window->surface,
745 window->input_region);
746 wl_region_destroy(window->input_region);
747 window->input_region = NULL;
748 }
749
750 if (window->opaque_region) {
751 wl_surface_set_opaque_region(window->surface,
752 window->opaque_region);
753 wl_region_destroy(window->opaque_region);
754 window->opaque_region = NULL;
755 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500756}
757
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500758void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400759window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500760{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400761 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100762 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500763}
764
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400765void
766window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400767{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500768 cairo_surface_reference(surface);
769
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400770 if (window->cairo_surface != NULL)
771 cairo_surface_destroy(window->cairo_surface);
772
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500773 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400774}
775
Benjamin Franzke22d54812011-07-16 19:50:32 +0000776#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100777static void
778window_resize_cairo_window_surface(struct window *window)
779{
780 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200781 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100782
783 data = cairo_surface_get_user_data(window->cairo_surface,
784 &surface_data_key);
785
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200786 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 wl_egl_window_resize(data->window,
788 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200789 window->allocation.height,
790 x,y);
791
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 cairo_gl_surface_set_size(window->cairo_surface,
793 window->allocation.width,
794 window->allocation.height);
795}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000796#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100797
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400798struct display *
799window_get_display(struct window *window)
800{
801 return window->display;
802}
803
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400804void
805window_create_surface(struct window *window)
806{
807 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400808 uint32_t flags = 0;
809
810 if (!window->transparent)
811 flags = SURFACE_OPAQUE;
812
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400813 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
816 if (window->cairo_surface) {
817 window_resize_cairo_window_surface(window);
818 return;
819 }
820 surface = display_create_surface(window->display,
821 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400822 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824#endif
825 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400826 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400827 &window->allocation,
828 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400829 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800830 default:
831 surface = NULL;
832 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400834
835 window_set_surface(window, surface);
836 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837}
838
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200839static void frame_destroy(struct frame *frame);
840
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400841void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500842window_destroy(struct window *window)
843{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200844 struct display *display = window->display;
845 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100846 struct window_output *window_output;
847 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200848
849 if (window->redraw_scheduled)
850 wl_list_remove(&window->redraw_task.link);
851
852 wl_list_for_each(input, &display->input_list, link) {
853 if (input->pointer_focus == window)
854 input->pointer_focus = NULL;
855 if (input->keyboard_focus == window)
856 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500857 if (input->focus_widget &&
858 input->focus_widget->window == window)
859 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200860 }
861
Rob Bradford7507b572012-05-15 17:55:34 +0100862 wl_list_for_each_safe(window_output, window_output_tmp,
863 &window->window_output_list, link) {
864 free (window_output);
865 }
866
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500867 if (window->input_region)
868 wl_region_destroy(window->input_region);
869 if (window->opaque_region)
870 wl_region_destroy(window->opaque_region);
871
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200872 if (window->frame)
873 frame_destroy(window->frame);
874
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200875 if (window->shell_surface)
876 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500877 wl_surface_destroy(window->surface);
878 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200879
880 if (window->cairo_surface != NULL)
881 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200882
883 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500884 free(window);
885}
886
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500887static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500888widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400889{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500890 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400891
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500892 wl_list_for_each(child, &widget->child_list, link) {
893 target = widget_find_widget(child, x, y);
894 if (target)
895 return target;
896 }
897
898 if (widget->allocation.x <= x &&
899 x < widget->allocation.x + widget->allocation.width &&
900 widget->allocation.y <= y &&
901 y < widget->allocation.y + widget->allocation.height) {
902 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400903 }
904
905 return NULL;
906}
907
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500908static struct widget *
909widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400910{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500911 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400912
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500913 widget = malloc(sizeof *widget);
914 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500915 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500916 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500917 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500918 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500919 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300920 widget->tooltip = NULL;
921 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500922
923 return widget;
924}
925
926struct widget *
927window_add_widget(struct window *window, void *data)
928{
929 window->widget = widget_create(window, data);
930 wl_list_init(&window->widget->link);
931
932 return window->widget;
933}
934
935struct widget *
936widget_add_widget(struct widget *parent, void *data)
937{
938 struct widget *widget;
939
940 widget = widget_create(parent->window, data);
941 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400942
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500943 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400944}
945
946void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500947widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400948{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200949 struct display *display = widget->window->display;
950 struct input *input;
951
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300952 if (widget->tooltip) {
953 free(widget->tooltip);
954 widget->tooltip = NULL;
955 }
956
Pekka Paalanene156fb62012-01-19 13:51:38 +0200957 wl_list_for_each(input, &display->input_list, link) {
958 if (input->focus_widget == widget)
959 input->focus_widget = NULL;
960 }
961
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500962 wl_list_remove(&widget->link);
963 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400964}
965
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500967widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400968{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500969 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400970}
971
972void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500973widget_set_size(struct widget *widget, int32_t width, int32_t height)
974{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500975 widget->allocation.width = width;
976 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500977}
978
979void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500980widget_set_allocation(struct widget *widget,
981 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400982{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500983 widget->allocation.x = x;
984 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200985 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400986}
987
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500988void
989widget_set_transparent(struct widget *widget, int transparent)
990{
991 widget->opaque = !transparent;
992}
993
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400994void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500995widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998}
999
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001000void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001001widget_set_resize_handler(struct widget *widget,
1002 widget_resize_handler_t handler)
1003{
1004 widget->resize_handler = handler;
1005}
1006
1007void
1008widget_set_redraw_handler(struct widget *widget,
1009 widget_redraw_handler_t handler)
1010{
1011 widget->redraw_handler = handler;
1012}
1013
1014void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001015widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001016{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001017 widget->enter_handler = handler;
1018}
1019
1020void
1021widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1022{
1023 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001024}
1025
1026void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001027widget_set_motion_handler(struct widget *widget,
1028 widget_motion_handler_t handler)
1029{
1030 widget->motion_handler = handler;
1031}
1032
1033void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001034widget_set_button_handler(struct widget *widget,
1035 widget_button_handler_t handler)
1036{
1037 widget->button_handler = handler;
1038}
1039
1040void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001041widget_schedule_redraw(struct widget *widget)
1042{
1043 window_schedule_redraw(widget->window);
1044}
1045
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001046cairo_surface_t *
1047window_get_surface(struct window *window)
1048{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001049 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001050}
1051
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001052struct wl_surface *
1053window_get_wl_surface(struct window *window)
1054{
1055 return window->surface;
1056}
1057
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001058struct wl_shell_surface *
1059window_get_wl_shell_surface(struct window *window)
1060{
1061 return window->shell_surface;
1062}
1063
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001064static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001065tooltip_redraw_handler(struct widget *widget, void *data)
1066{
1067 cairo_t *cr;
1068 const int32_t r = 3;
1069 struct tooltip *tooltip = data;
1070 int32_t width, height;
1071 struct window *window = widget->window;
1072
1073 cr = cairo_create(window->cairo_surface);
1074 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1075 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1076 cairo_paint(cr);
1077
1078 width = window->allocation.width;
1079 height = window->allocation.height;
1080 rounded_rect(cr, 0, 0, width, height, r);
1081
1082 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1083 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1084 cairo_fill(cr);
1085
1086 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1087 cairo_move_to(cr, 10, 16);
1088 cairo_show_text(cr, tooltip->entry);
1089 cairo_destroy(cr);
1090}
1091
1092static cairo_text_extents_t
1093get_text_extents(struct tooltip *tooltip)
1094{
1095 struct window *window;
1096 cairo_t *cr;
1097 cairo_text_extents_t extents;
1098
1099 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1100 * created yet */
1101 window = tooltip->widget->window->parent;
1102 cr = cairo_create(window->cairo_surface);
1103 cairo_text_extents(cr, tooltip->entry, &extents);
1104 cairo_destroy(cr);
1105
1106 return extents;
1107}
1108
1109static int
1110window_create_tooltip(struct tooltip *tooltip)
1111{
1112 struct widget *parent = tooltip->parent;
1113 struct display *display = parent->window->display;
1114 struct window *window;
1115 const int offset_y = 27;
1116 const int margin = 3;
1117 cairo_text_extents_t extents;
1118
1119 if (tooltip->widget)
1120 return 0;
1121
1122 window = window_create_transient(display, parent->window, tooltip->x,
1123 tooltip->y + offset_y,
1124 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1125 if (!window)
1126 return -1;
1127
1128 tooltip->window = window;
1129 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1130
1131 extents = get_text_extents(tooltip);
1132 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1133 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1134
1135 return 0;
1136}
1137
1138void
1139widget_destroy_tooltip(struct widget *parent)
1140{
1141 struct tooltip *tooltip = parent->tooltip;
1142
1143 parent->tooltip_count = 0;
1144 if (!tooltip)
1145 return;
1146
1147 if (tooltip->widget) {
1148 widget_destroy(tooltip->widget);
1149 window_destroy(tooltip->window);
1150 tooltip->widget = NULL;
1151 tooltip->window = NULL;
1152 }
1153
1154 close(tooltip->tooltip_fd);
1155 free(tooltip->entry);
1156 free(tooltip);
1157 parent->tooltip = NULL;
1158}
1159
1160static void
1161tooltip_func(struct task *task, uint32_t events)
1162{
1163 struct tooltip *tooltip =
1164 container_of(task, struct tooltip, tooltip_task);
1165 uint64_t exp;
1166
1167 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1168 window_create_tooltip(tooltip);
1169}
1170
1171#define TOOLTIP_TIMEOUT 500
1172static int
1173tooltip_timer_reset(struct tooltip *tooltip)
1174{
1175 struct itimerspec its;
1176
1177 its.it_interval.tv_sec = 0;
1178 its.it_interval.tv_nsec = 0;
1179 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1180 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1181 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1182 fprintf(stderr, "could not set timerfd\n: %m");
1183 return -1;
1184 }
1185
1186 return 0;
1187}
1188
1189int
1190widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1191{
1192 struct tooltip *tooltip = parent->tooltip;
1193
1194 parent->tooltip_count++;
1195 if (tooltip) {
1196 tooltip->x = x;
1197 tooltip->y = y;
1198 tooltip_timer_reset(tooltip);
1199 return 0;
1200 }
1201
1202 /* the handler might be triggered too fast via input device motion, so
1203 * we need this check here to make sure tooltip is fully initialized */
1204 if (parent->tooltip_count > 1)
1205 return 0;
1206
1207 tooltip = malloc(sizeof *tooltip);
1208 if (!tooltip)
1209 return -1;
1210
1211 parent->tooltip = tooltip;
1212 tooltip->parent = parent;
1213 tooltip->widget = NULL;
1214 tooltip->window = NULL;
1215 tooltip->x = x;
1216 tooltip->y = y;
1217 tooltip->entry = strdup(entry);
1218 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1219 if (tooltip->tooltip_fd < 0) {
1220 fprintf(stderr, "could not create timerfd\n: %m");
1221 return -1;
1222 }
1223
1224 tooltip->tooltip_task.run = tooltip_func;
1225 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1226 EPOLLIN, &tooltip->tooltip_task);
1227 tooltip_timer_reset(tooltip);
1228
1229 return 0;
1230}
1231
1232static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001233frame_resize_handler(struct widget *widget,
1234 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001235{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001236 struct frame *frame = data;
1237 struct widget *child = frame->child;
1238 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001239 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001240 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001241 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001242 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001243 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001244 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001246 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001247 decoration_width = (t->width + t->margin) * 2;
1248 decoration_height = t->width +
1249 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001250
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001251 allocation.x = t->width + t->margin;
1252 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001253 allocation.width = width - decoration_width;
1254 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001255
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001256 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001257
1258 wl_list_for_each(button, &frame->buttons_list, link)
1259 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001260 } else {
1261 decoration_width = 0;
1262 decoration_height = 0;
1263
1264 allocation.x = 0;
1265 allocation.y = 0;
1266 allocation.width = width;
1267 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001268 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001269
1270 wl_list_for_each(button, &frame->buttons_list, link)
1271 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001272 }
1273
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001274 widget_set_allocation(child, allocation.x, allocation.y,
1275 allocation.width, allocation.height);
1276
1277 if (child->resize_handler)
1278 child->resize_handler(child,
1279 allocation.width,
1280 allocation.height,
1281 child->user_data);
1282
Scott Moreauf7e498c2012-05-14 11:39:29 -06001283 width = child->allocation.width + decoration_width;
1284 height = child->allocation.height + decoration_height;
1285
1286 widget->window->input_region =
1287 wl_compositor_create_region(display->compositor);
1288 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001289 t->margin, t->margin,
1290 width - 2 * t->margin,
1291 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001292
1293 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001294
1295 if (child->opaque) {
1296 widget->window->opaque_region =
1297 wl_compositor_create_region(display->compositor);
1298 wl_region_add(widget->window->opaque_region,
1299 opaque_margin, opaque_margin,
1300 widget->allocation.width - 2 * opaque_margin,
1301 widget->allocation.height - 2 * opaque_margin);
1302 }
Martin Minarik1998b152012-05-10 02:04:35 +02001303
1304 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001305 x_r = frame->widget->allocation.width - t->width - t->margin;
1306 x_l = t->width + t->margin;
1307 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001308 wl_list_for_each(button, &frame->buttons_list, link) {
1309 const int button_padding = 4;
1310 w = cairo_image_surface_get_width(button->icon);
1311 h = cairo_image_surface_get_height(button->icon);
1312
1313 if (button->decoration == FRAME_BUTTON_FANCY)
1314 w += 10;
1315
1316 if (button->align == FRAME_BUTTON_LEFT) {
1317 widget_set_allocation(button->widget,
1318 x_l, y , w + 1, h + 1);
1319 x_l += w;
1320 x_l += button_padding;
1321 } else {
1322 x_r -= w;
1323 widget_set_allocation(button->widget,
1324 x_r, y , w + 1, h + 1);
1325 x_r -= button_padding;
1326 }
1327 }
1328}
1329
1330static int
1331frame_button_enter_handler(struct widget *widget,
1332 struct input *input, float x, float y, void *data)
1333{
1334 struct frame_button *frame_button = data;
1335
1336 widget_schedule_redraw(frame_button->widget);
1337 frame_button->state = FRAME_BUTTON_OVER;
1338
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001339 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001340}
1341
1342static void
1343frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1344{
1345 struct frame_button *frame_button = data;
1346
1347 widget_schedule_redraw(frame_button->widget);
1348 frame_button->state = FRAME_BUTTON_DEFAULT;
1349}
1350
1351static void
1352frame_button_button_handler(struct widget *widget,
1353 struct input *input, uint32_t time,
1354 uint32_t button, uint32_t state, void *data)
1355{
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) {
1363 case 1:
1364 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;
1370 case 0:
1371 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 Stone5d663712012-05-04 11:21:55 +01001595 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001596
1597{
1598 struct frame *frame = data;
1599 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001600 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001601 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001602
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001603 location = theme_get_location(display->theme, input->sx, input->sy,
1604 frame->widget->allocation.width,
1605 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001606
1607 if (window->display->shell && button == BTN_LEFT && state == 1) {
1608 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001609 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610 if (!window->shell_surface)
1611 break;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001612 input_set_pointer_image(input, time, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001613 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001615 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001616 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001618 case THEME_LOCATION_RESIZING_TOP:
1619 case THEME_LOCATION_RESIZING_BOTTOM:
1620 case THEME_LOCATION_RESIZING_LEFT:
1621 case THEME_LOCATION_RESIZING_RIGHT:
1622 case THEME_LOCATION_RESIZING_TOP_LEFT:
1623 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1624 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1625 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626 if (!window->shell_surface)
1627 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001628 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001629
1630 if (!display->dpy) {
1631 /* If we're using shm, allocate a big
1632 pool to create buffers out of while
1633 we resize. We should probably base
1634 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001635 window->pool =
1636 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001637 }
1638
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001639 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001640 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001641 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 break;
1643 }
1644 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001645 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001646 }
1647}
1648
1649struct widget *
1650frame_create(struct window *window, void *data)
1651{
1652 struct frame *frame;
1653
1654 frame = malloc(sizeof *frame);
1655 memset(frame, 0, sizeof *frame);
1656
1657 frame->widget = window_add_widget(window, frame);
1658 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001659
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001660 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1661 widget_set_resize_handler(frame->widget, frame_resize_handler);
1662 widget_set_enter_handler(frame->widget, frame_enter_handler);
1663 widget_set_motion_handler(frame->widget, frame_motion_handler);
1664 widget_set_button_handler(frame->widget, frame_button_handler);
1665
Martin Minarik1998b152012-05-10 02:04:35 +02001666 /* Create empty list for frame buttons */
1667 wl_list_init(&frame->buttons_list);
1668
1669 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1670 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1671
1672 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1673 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1674
1675 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1676 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1677
1678 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1679 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1680
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001681 window->frame = frame;
1682
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001683 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001684}
1685
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001686static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001687frame_destroy(struct frame *frame)
1688{
Martin Minarik1998b152012-05-10 02:04:35 +02001689 struct frame_button *button, *tmp;
1690
1691 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1692 frame_button_destroy(button);
1693
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001694 /* frame->child must be destroyed by the application */
1695 widget_destroy(frame->widget);
1696 free(frame);
1697}
1698
1699static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001700input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001701 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001702{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001703 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001704 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001705
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001706 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001707 return;
1708
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001709 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001710 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001711 widget = old;
1712 if (input->grab)
1713 widget = input->grab;
1714 if (widget->leave_handler)
1715 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001716 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001717 }
1718
1719 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001720 widget = focus;
1721 if (input->grab)
1722 widget = input->grab;
1723 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001724 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001725 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001726 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001727
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001728 input_set_pointer_image(input, input->pointer_enter_serial,
1729 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001730 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001731}
1732
1733static void
Daniel Stone37816df2012-05-16 18:45:18 +01001734pointer_handle_motion(void *data, struct wl_pointer *pointer,
1735 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001736{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001737 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001738 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001739 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001740 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001741 float sx = wl_fixed_to_double(sx_w);
1742 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001743
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001744 input->sx = sx;
1745 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001746
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001747 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001748 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001749 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001750 }
1751
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001752 if (input->grab)
1753 widget = input->grab;
1754 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001755 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001756 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001757 cursor = widget->motion_handler(input->focus_widget,
1758 input, time, sx, sy,
1759 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001760
Daniel Stone37816df2012-05-16 18:45:18 +01001761 input_set_pointer_image(input, time, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001762}
1763
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001764void
1765input_grab(struct input *input, struct widget *widget, uint32_t button)
1766{
1767 input->grab = widget;
1768 input->grab_button = button;
1769}
1770
1771void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001772input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001773{
1774 struct widget *widget;
1775
1776 input->grab = NULL;
1777 if (input->pointer_focus) {
1778 widget = widget_find_widget(input->pointer_focus->widget,
1779 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001780 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001781 }
1782}
1783
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001784static void
Daniel Stone37816df2012-05-16 18:45:18 +01001785pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1786 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001787{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001788 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001789 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001790
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001791 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001792 if (input->focus_widget && input->grab == NULL && state)
1793 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001794
Neil Roberts6b28aad2012-01-23 19:11:18 +00001795 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001796 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001797 (*widget->button_handler)(widget,
1798 input, time,
1799 button, state,
1800 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001801
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001802 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001803 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001804}
1805
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001806static void
Daniel Stone37816df2012-05-16 18:45:18 +01001807pointer_handle_axis(void *data, struct wl_pointer *pointer,
Scott Moreau210d0792012-03-22 10:47:01 -06001808 uint32_t time, uint32_t axis, int32_t value)
1809{
1810}
1811
1812static void
Daniel Stone37816df2012-05-16 18:45:18 +01001813keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1814 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001815{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001816 struct input *input = data;
1817 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001818 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001819 uint32_t code, num_syms;
1820 const xkb_keysym_t *syms;
1821 xkb_keysym_t sym;
1822 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001823
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001824 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001825 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001826 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001827 return;
1828
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001829 if (state)
1830 window->send_cursor_position = 1;
1831
Kristian Høgsberg70163132012-05-08 15:55:39 -04001832 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1833 xkb_state_update_key(d->xkb.state, code,
1834 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001835
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001836 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001837 XKB_STATE_DEPRESSED |
1838 XKB_STATE_LATCHED);
1839 input->modifiers = 0;
1840 if (mask & input->display->xkb.control_mask)
1841 input->modifiers |= MOD_CONTROL_MASK;
1842 if (mask & input->display->xkb.alt_mask)
1843 input->modifiers |= MOD_ALT_MASK;
1844 if (mask & input->display->xkb.shift_mask)
1845 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001846
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001847 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001848 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001849 if (state)
1850 window_set_maximized(window,
1851 window->type != TYPE_MAXIMIZED);
1852 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001853 if (num_syms == 1)
1854 sym = syms[0];
1855 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001856 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001857
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001858 (*window->key_handler)(window, input, time, key,
1859 sym, state, window->user_data);
1860 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001861}
1862
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001863static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001864input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001865{
1866 struct window *window = input->pointer_focus;
1867
1868 if (!window)
1869 return;
1870
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001871 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001872
Pekka Paalanene1207c72011-12-16 12:02:09 +02001873 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001874 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001875}
1876
1877static void
Daniel Stone37816df2012-05-16 18:45:18 +01001878pointer_handle_enter(void *data, struct wl_pointer *pointer,
1879 uint32_t serial, struct wl_surface *surface,
1880 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001881{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001882 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001883 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001884 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001885 float sx = wl_fixed_to_double(sx_w);
1886 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001887
Martin Minarik1998b152012-05-10 02:04:35 +02001888 if (!surface) {
1889 /* enter event for a window we've just destroyed */
1890 return;
1891 }
1892
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001893 input->display->serial = serial;
1894 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001895 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001896 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001897
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001898 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001899 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001900 window->pool = NULL;
1901 /* Schedule a redraw to free the pool */
1902 window_schedule_redraw(window);
1903 }
1904
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001905 input->sx = sx;
1906 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001907
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001908 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001909 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001910}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001911
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001912static void
Daniel Stone37816df2012-05-16 18:45:18 +01001913pointer_handle_leave(void *data, struct wl_pointer *pointer,
1914 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001915{
1916 struct input *input = data;
1917
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001918 input->display->serial = serial;
1919 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001920}
1921
1922static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001923input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001924{
1925 struct window *window = input->keyboard_focus;
1926
1927 if (!window)
1928 return;
1929
1930 window->keyboard_device = NULL;
1931 if (window->keyboard_focus_handler)
1932 (*window->keyboard_focus_handler)(window, NULL,
1933 window->user_data);
1934
1935 input->keyboard_focus = NULL;
1936}
1937
1938static void
Daniel Stone37816df2012-05-16 18:45:18 +01001939keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1940 uint32_t serial, struct wl_surface *surface,
1941 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001942{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001943 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001944 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001945
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001946 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001947 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001948
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001949 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001950 window->keyboard_device = input;
1951 if (window->keyboard_focus_handler)
1952 (*window->keyboard_focus_handler)(window,
1953 window->keyboard_device,
1954 window->user_data);
1955}
1956
1957static void
Daniel Stone37816df2012-05-16 18:45:18 +01001958keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1959 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001960{
1961 struct input *input = data;
1962
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001963 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001964 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001965}
1966
Daniel Stone37816df2012-05-16 18:45:18 +01001967static const struct wl_pointer_listener pointer_listener = {
1968 pointer_handle_enter,
1969 pointer_handle_leave,
1970 pointer_handle_motion,
1971 pointer_handle_button,
1972 pointer_handle_axis,
1973};
1974
1975static const struct wl_keyboard_listener keyboard_listener = {
1976 keyboard_handle_enter,
1977 keyboard_handle_leave,
1978 keyboard_handle_key,
1979};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001980
1981static void
Daniel Stone37816df2012-05-16 18:45:18 +01001982seat_handle_capabilities(void *data, struct wl_seat *seat,
1983 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001984{
Daniel Stone37816df2012-05-16 18:45:18 +01001985 struct input *input = data;
1986
1987 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1988 input->pointer = wl_seat_get_pointer(seat);
1989 wl_pointer_set_user_data(input->pointer, input);
1990 wl_pointer_add_listener(input->pointer, &pointer_listener,
1991 input);
1992 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1993 wl_pointer_destroy(input->pointer);
1994 input->pointer = NULL;
1995 }
1996
1997 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1998 input->keyboard = wl_seat_get_keyboard(seat);
1999 wl_keyboard_set_user_data(input->keyboard, input);
2000 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2001 input);
2002 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2003 wl_keyboard_destroy(input->keyboard);
2004 input->keyboard = NULL;
2005 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002006}
2007
Daniel Stone37816df2012-05-16 18:45:18 +01002008static const struct wl_seat_listener seat_listener = {
2009 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002010};
2011
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002012void
2013input_get_position(struct input *input, int32_t *x, int32_t *y)
2014{
2015 *x = input->sx;
2016 *y = input->sy;
2017}
2018
Daniel Stone37816df2012-05-16 18:45:18 +01002019struct wl_seat *
2020input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002021{
Daniel Stone37816df2012-05-16 18:45:18 +01002022 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002023}
2024
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002025uint32_t
2026input_get_modifiers(struct input *input)
2027{
2028 return input->modifiers;
2029}
2030
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002031struct widget *
2032input_get_focus_widget(struct input *input)
2033{
2034 return input->focus_widget;
2035}
2036
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002037struct data_offer {
2038 struct wl_data_offer *offer;
2039 struct input *input;
2040 struct wl_array types;
2041 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002042
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002043 struct task io_task;
2044 int fd;
2045 data_func_t func;
2046 int32_t x, y;
2047 void *user_data;
2048};
2049
2050static void
2051data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2052{
2053 struct data_offer *offer = data;
2054 char **p;
2055
2056 p = wl_array_add(&offer->types, sizeof *p);
2057 *p = strdup(type);
2058}
2059
2060static const struct wl_data_offer_listener data_offer_listener = {
2061 data_offer_offer,
2062};
2063
2064static void
2065data_offer_destroy(struct data_offer *offer)
2066{
2067 char **p;
2068
2069 offer->refcount--;
2070 if (offer->refcount == 0) {
2071 wl_data_offer_destroy(offer->offer);
2072 for (p = offer->types.data; *p; p++)
2073 free(*p);
2074 wl_array_release(&offer->types);
2075 free(offer);
2076 }
2077}
2078
2079static void
2080data_device_data_offer(void *data,
2081 struct wl_data_device *data_device, uint32_t id)
2082{
2083 struct data_offer *offer;
2084
2085 offer = malloc(sizeof *offer);
2086
2087 wl_array_init(&offer->types);
2088 offer->refcount = 1;
2089 offer->input = data;
2090
2091 /* FIXME: Generate typesafe wrappers for this */
2092 offer->offer = (struct wl_data_offer *)
2093 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2094 id, &wl_data_offer_interface);
2095
2096 wl_data_offer_add_listener(offer->offer,
2097 &data_offer_listener, offer);
2098}
2099
2100static void
2101data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002102 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002103 wl_fixed_t x_w, wl_fixed_t y_w,
2104 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002105{
2106 struct input *input = data;
2107 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002108 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002109 float x = wl_fixed_to_double(x_w);
2110 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002111 char **p;
2112
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002113 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002114 window = wl_surface_get_user_data(surface);
2115 input->pointer_focus = window;
2116
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002117 if (offer) {
2118 input->drag_offer = wl_data_offer_get_user_data(offer);
2119
2120 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2121 *p = NULL;
2122
2123 types_data = input->drag_offer->types.data;
2124 } else {
2125 input->drag_offer = NULL;
2126 types_data = NULL;
2127 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002128
2129 window = input->pointer_focus;
2130 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002131 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002132 window->user_data);
2133}
2134
2135static void
2136data_device_leave(void *data, struct wl_data_device *data_device)
2137{
2138 struct input *input = data;
2139
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002140 if (input->drag_offer) {
2141 data_offer_destroy(input->drag_offer);
2142 input->drag_offer = NULL;
2143 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002144}
2145
2146static void
2147data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002148 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002149{
2150 struct input *input = data;
2151 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002152 float x = wl_fixed_to_double(x_w);
2153 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002154 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002155
2156 input->sx = x;
2157 input->sy = y;
2158
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002159 if (input->drag_offer)
2160 types_data = input->drag_offer->types.data;
2161 else
2162 types_data = NULL;
2163
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002164 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002165 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002166 window->user_data);
2167}
2168
2169static void
2170data_device_drop(void *data, struct wl_data_device *data_device)
2171{
2172 struct input *input = data;
2173 struct window *window = input->pointer_focus;
2174
2175 if (window->drop_handler)
2176 window->drop_handler(window, input,
2177 input->sx, input->sy, window->user_data);
2178}
2179
2180static void
2181data_device_selection(void *data,
2182 struct wl_data_device *wl_data_device,
2183 struct wl_data_offer *offer)
2184{
2185 struct input *input = data;
2186 char **p;
2187
2188 if (input->selection_offer)
2189 data_offer_destroy(input->selection_offer);
2190
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002191 if (offer) {
2192 input->selection_offer = wl_data_offer_get_user_data(offer);
2193 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2194 *p = NULL;
2195 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002196}
2197
2198static const struct wl_data_device_listener data_device_listener = {
2199 data_device_data_offer,
2200 data_device_enter,
2201 data_device_leave,
2202 data_device_motion,
2203 data_device_drop,
2204 data_device_selection
2205};
2206
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002207void
2208input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2209{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002210 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002211 struct wl_cursor *cursor;
2212 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002213
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002214 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002215 return;
2216
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002217 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002218 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002219 return;
2220
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002221 image = cursor->images[0];
2222 buffer = wl_cursor_image_get_buffer(image);
2223 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 return;
2225
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002226 input->current_cursor = pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002227 wl_pointer_attach(input->pointer, time, buffer,
2228 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002229}
2230
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002231struct wl_data_device *
2232input_get_data_device(struct input *input)
2233{
2234 return input->data_device;
2235}
2236
2237void
2238input_set_selection(struct input *input,
2239 struct wl_data_source *source, uint32_t time)
2240{
2241 wl_data_device_set_selection(input->data_device, source, time);
2242}
2243
2244void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002245input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002246{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002247 wl_data_offer_accept(input->drag_offer->offer,
2248 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002249}
2250
2251static void
2252offer_io_func(struct task *task, uint32_t events)
2253{
2254 struct data_offer *offer =
2255 container_of(task, struct data_offer, io_task);
2256 unsigned int len;
2257 char buffer[4096];
2258
2259 len = read(offer->fd, buffer, sizeof buffer);
2260 offer->func(buffer, len,
2261 offer->x, offer->y, offer->user_data);
2262
2263 if (len == 0) {
2264 close(offer->fd);
2265 data_offer_destroy(offer);
2266 }
2267}
2268
2269static void
2270data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2271 data_func_t func, void *user_data)
2272{
2273 int p[2];
2274
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002275 if (pipe2(p, O_CLOEXEC) == -1)
2276 return;
2277
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002278 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2279 close(p[1]);
2280
2281 offer->io_task.run = offer_io_func;
2282 offer->fd = p[0];
2283 offer->func = func;
2284 offer->refcount++;
2285 offer->user_data = user_data;
2286
2287 display_watch_fd(offer->input->display,
2288 offer->fd, EPOLLIN, &offer->io_task);
2289}
2290
2291void
2292input_receive_drag_data(struct input *input, const char *mime_type,
2293 data_func_t func, void *data)
2294{
2295 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2296 input->drag_offer->x = input->sx;
2297 input->drag_offer->y = input->sy;
2298}
2299
2300int
2301input_receive_selection_data(struct input *input, const char *mime_type,
2302 data_func_t func, void *data)
2303{
2304 char **p;
2305
2306 if (input->selection_offer == NULL)
2307 return -1;
2308
2309 for (p = input->selection_offer->types.data; *p; p++)
2310 if (strcmp(mime_type, *p) == 0)
2311 break;
2312
2313 if (*p == NULL)
2314 return -1;
2315
2316 data_offer_receive_data(input->selection_offer,
2317 mime_type, func, data);
2318 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002319}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002320
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002321int
2322input_receive_selection_data_to_fd(struct input *input,
2323 const char *mime_type, int fd)
2324{
2325 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2326
2327 return 0;
2328}
2329
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002330void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002331window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002332{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002333 if (!window->shell_surface)
2334 return;
2335
Daniel Stone37816df2012-05-16 18:45:18 +01002336 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002337}
2338
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002339static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002340idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002341{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002342 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002343
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002344 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002345 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002346 widget_set_allocation(widget,
2347 window->pending_allocation.x,
2348 window->pending_allocation.y,
2349 window->pending_allocation.width,
2350 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002351
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002352 if (window->input_region) {
2353 wl_region_destroy(window->input_region);
2354 window->input_region = NULL;
2355 }
2356
2357 if (window->opaque_region) {
2358 wl_region_destroy(window->opaque_region);
2359 window->opaque_region = NULL;
2360 }
2361
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002362 if (widget->resize_handler)
2363 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002364 widget->allocation.width,
2365 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002366 widget->user_data);
2367
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002368 if (window->allocation.width != widget->allocation.width ||
2369 window->allocation.height != widget->allocation.height) {
2370 window->allocation = widget->allocation;
2371 window_schedule_redraw(window);
2372 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002373}
2374
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002375void
2376window_schedule_resize(struct window *window, int width, int height)
2377{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002378 window->pending_allocation.x = 0;
2379 window->pending_allocation.y = 0;
2380 window->pending_allocation.width = width;
2381 window->pending_allocation.height = height;
2382
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002383 window->resize_needed = 1;
2384 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002385}
2386
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002387void
2388widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2389{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002390 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002391}
2392
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002393static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002394handle_ping(void *data, struct wl_shell_surface *shell_surface,
2395 uint32_t serial)
2396{
2397 wl_shell_surface_pong(shell_surface, serial);
2398}
2399
2400static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002401handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002402 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002403{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002404 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002405
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002406 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002407 return;
2408
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002409 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002410 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002411}
2412
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002413static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002414menu_destroy(struct menu *menu)
2415{
2416 widget_destroy(menu->widget);
2417 window_destroy(menu->window);
2418 free(menu);
2419}
2420
2421static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002422handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2423{
2424 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002425 struct menu *menu = window->widget->user_data;
2426
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002427 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002428 * device. Or just use wl_callback. And this really needs to
2429 * be a window vfunc that the menu can set. And we need the
2430 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002431
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002432 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002433 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002434 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002435}
2436
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002437static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002438 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002439 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002440 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002441};
2442
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002443void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002444window_get_allocation(struct window *window,
2445 struct rectangle *allocation)
2446{
2447 *allocation = window->allocation;
2448}
2449
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002450static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002451widget_redraw(struct widget *widget)
2452{
2453 struct widget *child;
2454
2455 if (widget->redraw_handler)
2456 widget->redraw_handler(widget, widget->user_data);
2457 wl_list_for_each(child, &widget->child_list, link)
2458 widget_redraw(child);
2459}
2460
2461static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002462frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2463{
2464 struct window *window = data;
2465
2466 wl_callback_destroy(callback);
2467 window->redraw_scheduled = 0;
2468 if (window->redraw_needed)
2469 window_schedule_redraw(window);
2470}
2471
2472static const struct wl_callback_listener listener = {
2473 frame_callback
2474};
2475
2476static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002477idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002478{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002479 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002480 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002481
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002482 if (window->resize_needed)
2483 idle_resize(window);
2484
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002485 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002486 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002487 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002488 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002489 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002490
2491 callback = wl_surface_frame(window->surface);
2492 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002493}
2494
2495void
2496window_schedule_redraw(struct window *window)
2497{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002498 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002499 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002500 window->redraw_task.run = idle_redraw;
2501 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002502 window->redraw_scheduled = 1;
2503 }
2504}
2505
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002506void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002507window_set_custom(struct window *window)
2508{
2509 window->type = TYPE_CUSTOM;
2510}
2511
2512void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002513window_set_fullscreen(struct window *window, int fullscreen)
2514{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002515 if (!window->display->shell)
2516 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002517
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002518 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002519 return;
2520
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002521 if (fullscreen) {
2522 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002523 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002524 wl_shell_surface_set_fullscreen(window->shell_surface,
2525 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2526 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002527 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002528 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002529 wl_shell_surface_set_toplevel(window->shell_surface);
2530 window_schedule_resize(window,
2531 window->saved_allocation.width,
2532 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002533 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002534}
2535
2536void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002537window_set_maximized(struct window *window, int maximized)
2538{
2539 if (!window->display->shell)
2540 return;
2541
2542 if ((window->type == TYPE_MAXIMIZED) == maximized)
2543 return;
2544
2545 if (window->type == TYPE_TOPLEVEL) {
2546 window->saved_allocation = window->allocation;
2547 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2548 window->type = TYPE_MAXIMIZED;
2549 } else {
2550 wl_shell_surface_set_toplevel(window->shell_surface);
2551 window->type = TYPE_TOPLEVEL;
2552 window_schedule_resize(window,
2553 window->saved_allocation.width,
2554 window->saved_allocation.height);
2555 }
2556}
2557
2558void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002559window_set_user_data(struct window *window, void *data)
2560{
2561 window->user_data = data;
2562}
2563
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002564void *
2565window_get_user_data(struct window *window)
2566{
2567 return window->user_data;
2568}
2569
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002570void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002571window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002572 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002573{
2574 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002575}
2576
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002577void
2578window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002579 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002580{
2581 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002582}
2583
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002584void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002585window_set_data_handler(struct window *window, window_data_handler_t handler)
2586{
2587 window->data_handler = handler;
2588}
2589
2590void
2591window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2592{
2593 window->drop_handler = handler;
2594}
2595
2596void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002597window_set_close_handler(struct window *window,
2598 window_close_handler_t handler)
2599{
2600 window->close_handler = handler;
2601}
2602
2603void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002604window_set_title(struct window *window, const char *title)
2605{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002606 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002607 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002608 if (window->shell_surface)
2609 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002610}
2611
2612const char *
2613window_get_title(struct window *window)
2614{
2615 return window->title;
2616}
2617
2618void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002619window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2620{
2621 struct text_cursor_position *text_cursor_position =
2622 window->display->text_cursor_position;
2623
2624 if (!window->send_cursor_position || !text_cursor_position)
2625 return;
2626
2627 text_cursor_position_notify(text_cursor_position,
2628 window->surface, x, y);
2629
2630 window->send_cursor_position = 0;
2631}
2632
2633void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002634window_damage(struct window *window, int32_t x, int32_t y,
2635 int32_t width, int32_t height)
2636{
2637 wl_surface_damage(window->surface, x, y, width, height);
2638}
2639
Casey Dahlin9074db52012-04-19 22:50:09 -04002640static void
2641surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002642 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002643{
Rob Bradford7507b572012-05-15 17:55:34 +01002644 struct window *window = data;
2645 struct output *output;
2646 struct output *output_found = NULL;
2647 struct window_output *window_output;
2648
2649 wl_list_for_each(output, &window->display->output_list, link) {
2650 if (output->output == wl_output) {
2651 output_found = output;
2652 break;
2653 }
2654 }
2655
2656 if (!output_found)
2657 return;
2658
2659 window_output = malloc (sizeof *window_output);
2660 window_output->output = output_found;
2661
2662 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002663}
2664
2665static void
2666surface_leave(void *data,
2667 struct wl_surface *wl_surface, struct wl_output *output)
2668{
Rob Bradford7507b572012-05-15 17:55:34 +01002669 struct window *window = data;
2670 struct window_output *window_output;
2671 struct window_output *window_output_found = NULL;
2672
2673 wl_list_for_each(window_output, &window->window_output_list, link) {
2674 if (window_output->output->output == output) {
2675 window_output_found = window_output;
2676 break;
2677 }
2678 }
2679
2680 if (window_output_found) {
2681 wl_list_remove(&window_output_found->link);
2682 free(window_output_found);
2683 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002684}
2685
2686static const struct wl_surface_listener surface_listener = {
2687 surface_enter,
2688 surface_leave
2689};
2690
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002691static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002692window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002693{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002694 struct window *window;
2695
2696 window = malloc(sizeof *window);
2697 if (window == NULL)
2698 return NULL;
2699
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002700 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002701 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002702 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002703 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002704 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002705 if (display->shell) {
2706 window->shell_surface =
2707 wl_shell_get_shell_surface(display->shell,
2708 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002709 if (window->title)
2710 wl_shell_surface_set_title(window->shell_surface,
2711 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002712 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002713 window->allocation.x = 0;
2714 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002715 window->allocation.width = 0;
2716 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002717 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002718 window->transparent = 1;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002719 window->send_cursor_position = 0;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002720 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002721 window->input_region = NULL;
2722 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002723
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002724 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002725#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002726 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002727#else
2728 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2729#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002730 else
2731 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002732
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002733 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002734 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002735 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002736
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002737 if (window->shell_surface) {
2738 wl_shell_surface_set_user_data(window->shell_surface, window);
2739 wl_shell_surface_add_listener(window->shell_surface,
2740 &shell_surface_listener, window);
2741 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002742
Rob Bradford7507b572012-05-15 17:55:34 +01002743 wl_list_init (&window->window_output_list);
2744
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002745 return window;
2746}
2747
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002748struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002749window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002750{
2751 struct window *window;
2752
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002753 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002754 if (!window)
2755 return NULL;
2756
2757 return window;
2758}
2759
2760struct window *
2761window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002762 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002763{
2764 struct window *window;
2765
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002766 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002767 if (!window)
2768 return NULL;
2769
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002770 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002771 window->x = x;
2772 window->y = y;
2773
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002774 if (display->shell)
2775 wl_shell_surface_set_transient(window->shell_surface,
2776 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002777 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002778
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002779 return window;
2780}
2781
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002782static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002783menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002784{
2785 int next;
2786
2787 next = (sy - 8) / 20;
2788 if (menu->current != next) {
2789 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002790 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002791 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002792}
2793
2794static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002795menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002796 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002797 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002798{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002799 struct menu *menu = data;
2800
2801 if (widget == menu->widget)
2802 menu_set_item(data, y);
2803
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002804 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002805}
2806
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002807static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002808menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002809 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002810{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002811 struct menu *menu = data;
2812
2813 if (widget == menu->widget)
2814 menu_set_item(data, y);
2815
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002816 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002817}
2818
2819static void
2820menu_leave_handler(struct widget *widget, struct input *input, void *data)
2821{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002822 struct menu *menu = data;
2823
2824 if (widget == menu->widget)
2825 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002826}
2827
2828static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002829menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002830 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002831 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002832
2833{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002834 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002835
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002836 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002837 /* Either relase after press-drag-release or
2838 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002839 menu->func(menu->window->parent,
2840 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002841 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002842 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002843 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002844}
2845
2846static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002847menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002848{
2849 cairo_t *cr;
2850 const int32_t r = 3, margin = 3;
2851 struct menu *menu = data;
2852 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002853 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002854
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002855 cr = cairo_create(window->cairo_surface);
2856 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2857 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2858 cairo_paint(cr);
2859
2860 width = window->allocation.width;
2861 height = window->allocation.height;
2862 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002863
2864 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002865 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2866 cairo_fill(cr);
2867
2868 for (i = 0; i < menu->count; i++) {
2869 if (i == menu->current) {
2870 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2871 cairo_rectangle(cr, margin, i * 20 + margin,
2872 width - 2 * margin, 20);
2873 cairo_fill(cr);
2874 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2875 cairo_move_to(cr, 10, i * 20 + 16);
2876 cairo_show_text(cr, menu->entries[i]);
2877 } else {
2878 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2879 cairo_move_to(cr, 10, i * 20 + 16);
2880 cairo_show_text(cr, menu->entries[i]);
2881 }
2882 }
2883
2884 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002885}
2886
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002887void
2888window_show_menu(struct display *display,
2889 struct input *input, uint32_t time, struct window *parent,
2890 int32_t x, int32_t y,
2891 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002892{
2893 struct window *window;
2894 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002895 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002896
2897 menu = malloc(sizeof *menu);
2898 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002899 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002900
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002901 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002902 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002903 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002904
2905 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002906 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002907 menu->entries = entries;
2908 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002909 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002910 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002911 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002912 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002913 window->type = TYPE_MENU;
2914 window->x = x;
2915 window->y = y;
2916
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002917 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002918 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002919 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002920 window->parent->shell_surface,
2921 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002922
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002923 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002924 widget_set_enter_handler(menu->widget, menu_enter_handler);
2925 widget_set_leave_handler(menu->widget, menu_leave_handler);
2926 widget_set_motion_handler(menu->widget, menu_motion_handler);
2927 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002928
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002929 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002930 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002931}
2932
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002933void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002934window_set_buffer_type(struct window *window, enum window_buffer_type type)
2935{
2936 window->buffer_type = type;
2937}
2938
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002939
2940static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002941display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002942 struct wl_output *wl_output,
2943 int x, int y,
2944 int physical_width,
2945 int physical_height,
2946 int subpixel,
2947 const char *make,
2948 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002949{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002950 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002951
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002952 output->allocation.x = x;
2953 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002954}
2955
2956static void
2957display_handle_mode(void *data,
2958 struct wl_output *wl_output,
2959 uint32_t flags,
2960 int width,
2961 int height,
2962 int refresh)
2963{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002964 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002965 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002966
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002967 if (flags & WL_OUTPUT_MODE_CURRENT) {
2968 output->allocation.width = width;
2969 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002970 if (display->output_configure_handler)
2971 (*display->output_configure_handler)(
2972 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002973 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002974}
2975
2976static const struct wl_output_listener output_listener = {
2977 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002978 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002979};
2980
2981static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002982display_add_output(struct display *d, uint32_t id)
2983{
2984 struct output *output;
2985
2986 output = malloc(sizeof *output);
2987 if (output == NULL)
2988 return;
2989
2990 memset(output, 0, sizeof *output);
2991 output->display = d;
2992 output->output =
2993 wl_display_bind(d->display, id, &wl_output_interface);
2994 wl_list_insert(d->output_list.prev, &output->link);
2995
2996 wl_output_add_listener(output->output, &output_listener, output);
2997}
2998
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002999static void
3000output_destroy(struct output *output)
3001{
3002 if (output->destroy_handler)
3003 (*output->destroy_handler)(output, output->user_data);
3004
3005 wl_output_destroy(output->output);
3006 wl_list_remove(&output->link);
3007 free(output);
3008}
3009
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003010void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003011display_set_output_configure_handler(struct display *display,
3012 display_output_handler_t handler)
3013{
3014 struct output *output;
3015
3016 display->output_configure_handler = handler;
3017 if (!handler)
3018 return;
3019
3020 wl_list_for_each(output, &display->output_list, link)
3021 (*display->output_configure_handler)(output,
3022 display->user_data);
3023}
3024
3025void
3026output_set_user_data(struct output *output, void *data)
3027{
3028 output->user_data = data;
3029}
3030
3031void *
3032output_get_user_data(struct output *output)
3033{
3034 return output->user_data;
3035}
3036
3037void
3038output_set_destroy_handler(struct output *output,
3039 display_output_handler_t handler)
3040{
3041 output->destroy_handler = handler;
3042 /* FIXME: implement this, once we have way to remove outputs */
3043}
3044
3045void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003046output_get_allocation(struct output *output, struct rectangle *allocation)
3047{
3048 *allocation = output->allocation;
3049}
3050
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003051struct wl_output *
3052output_get_wl_output(struct output *output)
3053{
3054 return output->output;
3055}
3056
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003057static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003058display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003059{
3060 struct input *input;
3061
3062 input = malloc(sizeof *input);
3063 if (input == NULL)
3064 return;
3065
3066 memset(input, 0, sizeof *input);
3067 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003068 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003069 input->pointer_focus = NULL;
3070 input->keyboard_focus = NULL;
3071 wl_list_insert(d->input_list.prev, &input->link);
3072
Daniel Stone37816df2012-05-16 18:45:18 +01003073 wl_seat_add_listener(input->seat, &seat_listener, input);
3074 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003075
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003076 input->data_device =
3077 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003078 input->seat);
3079 wl_data_device_add_listener(input->data_device, &data_device_listener,
3080 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003081}
3082
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003083static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003084input_destroy(struct input *input)
3085{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003086 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003087 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003088
3089 if (input->drag_offer)
3090 data_offer_destroy(input->drag_offer);
3091
3092 if (input->selection_offer)
3093 data_offer_destroy(input->selection_offer);
3094
3095 wl_data_device_destroy(input->data_device);
3096 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003097 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003098 free(input);
3099}
3100
3101static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003102display_handle_global(struct wl_display *display, uint32_t id,
3103 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003104{
3105 struct display *d = data;
3106
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003107 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003108 d->compositor =
3109 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003110 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003111 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003112 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003113 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003114 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003115 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003116 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003117 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003118 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3119 d->data_device_manager =
3120 wl_display_bind(display, id,
3121 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003122 } else if (strcmp(interface, "text_cursor_position") == 0) {
3123 d->text_cursor_position =
3124 wl_display_bind(display, id,
3125 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003126 }
3127}
3128
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003129static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003130init_xkb(struct display *d)
3131{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003132 d->xkb.names.rules = "evdev";
3133 d->xkb.names.model = "pc105";
3134 d->xkb.names.layout = (char *) option_xkb_layout;
3135 d->xkb.names.variant = (char *) option_xkb_variant;
3136 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003137
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003138 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003139 if (!d->xkb.context) {
3140 fprintf(stderr, "Failed to create XKB context\n");
3141 exit(1);
3142 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003143
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003144 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003145 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003146 fprintf(stderr, "Failed to compile keymap\n");
3147 exit(1);
3148 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003149
3150 d->xkb.state = xkb_state_new(d->xkb.keymap);
3151 if (!d->xkb.state) {
3152 fprintf(stderr, "Failed to create XKB state\n");
3153 exit(1);
3154 }
3155
3156 d->xkb.control_mask =
3157 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3158 d->xkb.alt_mask =
3159 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3160 d->xkb.shift_mask =
3161 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3162
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003163}
3164
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003165static void
3166fini_xkb(struct display *display)
3167{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003168 xkb_state_unref(display->xkb.state);
3169 xkb_map_unref(display->xkb.keymap);
3170 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003171}
3172
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003173#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003174static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003175init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003176{
3177 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003178 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003179
Rob Clark6396ed32012-03-11 19:48:41 -05003180#ifdef USE_CAIRO_GLESV2
3181# define GL_BIT EGL_OPENGL_ES2_BIT
3182#else
3183# define GL_BIT EGL_OPENGL_BIT
3184#endif
3185
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003186 static const EGLint argb_cfg_attribs[] = {
3187 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003188 EGL_RED_SIZE, 1,
3189 EGL_GREEN_SIZE, 1,
3190 EGL_BLUE_SIZE, 1,
3191 EGL_ALPHA_SIZE, 1,
3192 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003193 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003194 EGL_NONE
3195 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003196
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003197#ifdef USE_CAIRO_GLESV2
3198 static const EGLint context_attribs[] = {
3199 EGL_CONTEXT_CLIENT_VERSION, 2,
3200 EGL_NONE
3201 };
3202 EGLint api = EGL_OPENGL_ES_API;
3203#else
3204 EGLint *context_attribs = NULL;
3205 EGLint api = EGL_OPENGL_API;
3206#endif
3207
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003208 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003209 if (!eglInitialize(d->dpy, &major, &minor)) {
3210 fprintf(stderr, "failed to initialize display\n");
3211 return -1;
3212 }
3213
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003214 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003215 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3216 return -1;
3217 }
3218
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003219 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3220 &d->argb_config, 1, &n) || n != 1) {
3221 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003222 return -1;
3223 }
3224
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003225 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003226 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003227 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003228 fprintf(stderr, "failed to create context\n");
3229 return -1;
3230 }
3231
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003232 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003233 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003234 return -1;
3235 }
3236
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003237#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003238 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3239 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3240 fprintf(stderr, "failed to get cairo egl argb device\n");
3241 return -1;
3242 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003243#endif
3244
3245 return 0;
3246}
3247
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003248static void
3249fini_egl(struct display *display)
3250{
3251#ifdef HAVE_CAIRO_EGL
3252 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003253#endif
3254
3255 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3256 EGL_NO_CONTEXT);
3257
3258 eglTerminate(display->dpy);
3259 eglReleaseThread();
3260}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003261#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003262
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003263static int
3264event_mask_update(uint32_t mask, void *data)
3265{
3266 struct display *d = data;
3267
3268 d->mask = mask;
3269
3270 return 0;
3271}
3272
3273static void
3274handle_display_data(struct task *task, uint32_t events)
3275{
3276 struct display *display =
3277 container_of(task, struct display, display_task);
3278
3279 wl_display_iterate(display->display, display->mask);
3280}
3281
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003282struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003283display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003284{
3285 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003286
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003287 argc = parse_options(xkb_options,
3288 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003289
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003290 d = malloc(sizeof *d);
3291 if (d == NULL)
3292 return NULL;
3293
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003294 memset(d, 0, sizeof *d);
3295
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003296 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003297 if (d->display == NULL) {
3298 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003299 return NULL;
3300 }
3301
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003302 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3303 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3304 d->display_task.run = handle_display_data;
3305 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3306
3307 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003308 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003309 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003310
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003311 /* Set up listener so we'll catch all events. */
3312 wl_display_add_global_listener(d->display,
3313 display_handle_global, d);
3314
3315 /* Process connection events. */
3316 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003317#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003318 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003319 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003320#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003321
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003322 d->image_target_texture_2d =
3323 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3324 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3325 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3326
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003327 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003328
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003329 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003330
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003331 wl_list_init(&d->window_list);
3332
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003333 init_xkb(d);
3334
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003335 return d;
3336}
3337
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003338static void
3339display_destroy_outputs(struct display *display)
3340{
3341 struct output *tmp;
3342 struct output *output;
3343
3344 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3345 output_destroy(output);
3346}
3347
Pekka Paalanene1207c72011-12-16 12:02:09 +02003348static void
3349display_destroy_inputs(struct display *display)
3350{
3351 struct input *tmp;
3352 struct input *input;
3353
3354 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3355 input_destroy(input);
3356}
3357
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003358void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003359display_destroy(struct display *display)
3360{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003361 if (!wl_list_empty(&display->window_list))
3362 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3363
3364 if (!wl_list_empty(&display->deferred_list))
3365 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3366
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003367 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003368 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003369
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003370 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003371
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003372 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003373 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003374
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003375#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003376 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003377#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003378
Pekka Paalanenc2052982011-12-16 11:41:32 +02003379 if (display->shell)
3380 wl_shell_destroy(display->shell);
3381
3382 if (display->shm)
3383 wl_shm_destroy(display->shm);
3384
3385 if (display->data_device_manager)
3386 wl_data_device_manager_destroy(display->data_device_manager);
3387
3388 wl_compositor_destroy(display->compositor);
3389
3390 close(display->epoll_fd);
3391
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003392 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003393 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003394 free(display);
3395}
3396
3397void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003398display_set_user_data(struct display *display, void *data)
3399{
3400 display->user_data = data;
3401}
3402
3403void *
3404display_get_user_data(struct display *display)
3405{
3406 return display->user_data;
3407}
3408
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003409struct wl_display *
3410display_get_display(struct display *display)
3411{
3412 return display->display;
3413}
3414
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003415struct output *
3416display_get_output(struct display *display)
3417{
3418 return container_of(display->output_list.next, struct output, link);
3419}
3420
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003421struct wl_compositor *
3422display_get_compositor(struct display *display)
3423{
3424 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003425}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003426
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003427uint32_t
3428display_get_serial(struct display *display)
3429{
3430 return display->serial;
3431}
3432
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003433EGLDisplay
3434display_get_egl_display(struct display *d)
3435{
3436 return d->dpy;
3437}
3438
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003439struct wl_data_source *
3440display_create_data_source(struct display *display)
3441{
3442 return wl_data_device_manager_create_data_source(display->data_device_manager);
3443}
3444
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003445EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003446display_get_argb_egl_config(struct display *d)
3447{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003448 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003449}
3450
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003451struct wl_shell *
3452display_get_shell(struct display *display)
3453{
3454 return display->shell;
3455}
3456
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003457int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003458display_acquire_window_surface(struct display *display,
3459 struct window *window,
3460 EGLContext ctx)
3461{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003462#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003463 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003464 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003465
3466 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003467 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003468 device = cairo_surface_get_device(window->cairo_surface);
3469 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003470 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003471
Benjamin Franzke0c991632011-09-27 21:57:31 +02003472 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003473 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003474 ctx = display->argb_ctx;
3475 else
3476 assert(0);
3477 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003478
3479 data = cairo_surface_get_user_data(window->cairo_surface,
3480 &surface_data_key);
3481
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003482 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003483 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003484 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3485 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003486
3487 return 0;
3488#else
3489 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003490#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003491}
3492
3493void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003494display_release_window_surface(struct display *display,
3495 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003496{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003497#ifdef HAVE_CAIRO_EGL
3498 cairo_device_t *device;
3499
3500 device = cairo_surface_get_device(window->cairo_surface);
3501 if (!device)
3502 return;
3503
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003504 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003505 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003506 cairo_device_release(device);
3507#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003508}
3509
3510void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003511display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003512{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003513 wl_list_insert(&display->deferred_list, &task->link);
3514}
3515
3516void
3517display_watch_fd(struct display *display,
3518 int fd, uint32_t events, struct task *task)
3519{
3520 struct epoll_event ep;
3521
3522 ep.events = events;
3523 ep.data.ptr = task;
3524 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3525}
3526
3527void
3528display_run(struct display *display)
3529{
3530 struct task *task;
3531 struct epoll_event ep[16];
3532 int i, count;
3533
Pekka Paalanen826d7952011-12-15 10:14:07 +02003534 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003535 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003536 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003537
3538 while (!wl_list_empty(&display->deferred_list)) {
3539 task = container_of(display->deferred_list.next,
3540 struct task, link);
3541 wl_list_remove(&task->link);
3542 task->run(task, 0);
3543 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003544
3545 if (!display->running)
3546 break;
3547
3548 wl_display_flush(display->display);
3549
3550 count = epoll_wait(display->epoll_fd,
3551 ep, ARRAY_LENGTH(ep), -1);
3552 for (i = 0; i < count; i++) {
3553 task = ep[i].data.ptr;
3554 task->run(task, ep[i].events);
3555 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003556 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003557}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003558
3559void
3560display_exit(struct display *display)
3561{
3562 display->running = 0;
3563}