blob: 9eb62e6cc8bb2eff1d2a30f4ce34887760c831be [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070067struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030068
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050069struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050070 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050071 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040072 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040073 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040074 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040075 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050076 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020077 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020078 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040079 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040080
81 int display_fd;
82 uint32_t mask;
83 struct task display_task;
84
85 int epoll_fd;
86 struct wl_list deferred_list;
87
Pekka Paalanen826d7952011-12-15 10:14:07 +020088 int running;
89
Kristian Høgsberg478d9262010-06-08 20:34:11 -040090 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040091 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050092 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040093
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040094 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040095
96 struct {
97 struct xkb_rule_names names;
98 struct xkb_keymap *keymap;
99 struct xkb_state *state;
100 struct xkb_context *context;
101 xkb_mod_mask_t control_mask;
102 xkb_mod_mask_t alt_mask;
103 xkb_mod_mask_t shift_mask;
104 } xkb;
105
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300106 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300107 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400108
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500109 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
110 PFNEGLCREATEIMAGEKHRPROC create_image;
111 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200112
113 display_output_handler_t output_configure_handler;
114
115 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500116};
117
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400119 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400121 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500122 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500124 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400125 TYPE_CUSTOM
126};
Rob Bradford7507b572012-05-15 17:55:34 +0100127
128struct window_output {
129 struct output *output;
130 struct wl_list link;
131};
132
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133struct window {
134 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500135 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100136 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500137 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200138 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500139 struct wl_region *input_region;
140 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500141 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500142 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500143 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500144 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400145 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400146 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400147 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400148 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400149 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400150 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400151 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400152 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400153 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500154
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400155 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500156
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700157 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400158
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500159 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500160 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400161 window_data_handler_t data_handler;
162 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500163 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400164
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200165 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500166 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500167
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500168 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400169 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500170};
171
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500172struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500173 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300174 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500175 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400176 struct wl_list link;
177 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500178 widget_resize_handler_t resize_handler;
179 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500180 widget_enter_handler_t enter_handler;
181 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500182 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500183 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400184 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500185 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300186 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400187};
188
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400189struct input {
190 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100191 struct wl_seat *seat;
192 struct wl_pointer *pointer;
193 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400194 struct window *pointer_focus;
195 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300196 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400197 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400198 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400199 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400200 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400201
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500202 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500203 struct widget *grab;
204 uint32_t grab_button;
205
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400206 struct wl_data_device *data_device;
207 struct data_offer *drag_offer;
208 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400209};
210
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500211struct output {
212 struct display *display;
213 struct wl_output *output;
214 struct rectangle allocation;
215 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200216
217 display_output_handler_t destroy_handler;
218 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500219};
220
Martin Minarik1998b152012-05-10 02:04:35 +0200221enum frame_button_action {
222 FRAME_BUTTON_NULL = 0,
223 FRAME_BUTTON_ICON = 1,
224 FRAME_BUTTON_CLOSE = 2,
225 FRAME_BUTTON_MINIMIZE = 3,
226 FRAME_BUTTON_MAXIMIZE = 4,
227};
228
229enum frame_button_pointer {
230 FRAME_BUTTON_DEFAULT = 0,
231 FRAME_BUTTON_OVER = 1,
232 FRAME_BUTTON_ACTIVE = 2,
233};
234
235enum frame_button_align {
236 FRAME_BUTTON_RIGHT = 0,
237 FRAME_BUTTON_LEFT = 1,
238};
239
240enum frame_button_decoration {
241 FRAME_BUTTON_NONE = 0,
242 FRAME_BUTTON_FANCY = 1,
243};
244
245struct frame_button {
246 struct widget *widget;
247 struct frame *frame;
248 cairo_surface_t *icon;
249 enum frame_button_action type;
250 enum frame_button_pointer state;
251 struct wl_list link; /* buttons_list */
252 enum frame_button_align align;
253 enum frame_button_decoration decoration;
254};
255
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500256struct frame {
257 struct widget *widget;
258 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200259 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500260};
261
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500262struct menu {
263 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500264 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500265 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500266 const char **entries;
267 uint32_t time;
268 int current;
269 int count;
270 menu_func_t func;
271};
272
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300273struct tooltip {
274 struct widget *parent;
275 struct window *window;
276 struct widget *widget;
277 char *entry;
278 struct task tooltip_task;
279 int tooltip_fd;
280 float x, y;
281};
282
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700283struct shm_pool {
284 struct wl_shm_pool *pool;
285 size_t size;
286 size_t used;
287 void *data;
288};
289
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400290enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300291 CURSOR_DEFAULT = 100,
292 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400293};
294
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500295enum window_location {
296 WINDOW_INTERIOR = 0,
297 WINDOW_RESIZING_TOP = 1,
298 WINDOW_RESIZING_BOTTOM = 2,
299 WINDOW_RESIZING_LEFT = 4,
300 WINDOW_RESIZING_TOP_LEFT = 5,
301 WINDOW_RESIZING_BOTTOM_LEFT = 6,
302 WINDOW_RESIZING_RIGHT = 8,
303 WINDOW_RESIZING_TOP_RIGHT = 9,
304 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
305 WINDOW_RESIZING_MASK = 15,
306 WINDOW_EXTERIOR = 16,
307 WINDOW_TITLEBAR = 17,
308 WINDOW_CLIENT_AREA = 18,
309};
310
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400311const char *option_xkb_layout = "us";
312const char *option_xkb_variant = "";
313const char *option_xkb_options = "";
314
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400315static const struct weston_option xkb_options[] = {
316 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
317 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
318 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400319};
320
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400321static const cairo_user_data_key_t surface_data_key;
322struct surface_data {
323 struct wl_buffer *buffer;
324};
325
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500326#define MULT(_d,c,a,t) \
327 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
328
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500329#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100331struct egl_window_surface_data {
332 struct display *display;
333 struct wl_surface *surface;
334 struct wl_egl_window *window;
335 EGLSurface surf;
336};
337
338static void
339egl_window_surface_data_destroy(void *p)
340{
341 struct egl_window_surface_data *data = p;
342 struct display *d = data->display;
343
344 eglDestroySurface(d->dpy, data->surf);
345 wl_egl_window_destroy(data->window);
346 data->surface = NULL;
347
348 free(p);
349}
350
351static cairo_surface_t *
352display_create_egl_window_surface(struct display *display,
353 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400354 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100355 struct rectangle *rectangle)
356{
357 cairo_surface_t *cairo_surface;
358 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400359 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200360 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100361
362 data = malloc(sizeof *data);
363 if (data == NULL)
364 return NULL;
365
366 data->display = display;
367 data->surface = surface;
368
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500369 config = display->argb_config;
370 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400371
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400372 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100373 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100375
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500377 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378
Benjamin Franzke0c991632011-09-27 21:57:31 +0200379 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100380 data->surf,
381 rectangle->width,
382 rectangle->height);
383
384 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
385 data, egl_window_surface_data_destroy);
386
387 return cairo_surface;
388}
389
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390#endif
391
392struct wl_buffer *
393display_get_buffer_for_surface(struct display *display,
394 cairo_surface_t *surface)
395{
396 struct surface_data *data;
397
398 data = cairo_surface_get_user_data (surface, &surface_data_key);
399
400 return data->buffer;
401}
402
403struct shm_surface_data {
404 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700405 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406};
407
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500408static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700409shm_pool_destroy(struct shm_pool *pool);
410
411static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412shm_surface_data_destroy(void *p)
413{
414 struct shm_surface_data *data = p;
415
416 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700417 if (data->pool)
418 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419}
420
Kristian Høgsberg16626282012-04-03 11:21:27 -0400421static struct wl_shm_pool *
422make_shm_pool(struct display *display, int size, void **data)
423{
424 char filename[] = "/tmp/wayland-shm-XXXXXX";
425 struct wl_shm_pool *pool;
426 int fd;
427
428 fd = mkstemp(filename);
429 if (fd < 0) {
430 fprintf(stderr, "open %s failed: %m\n", filename);
431 return NULL;
432 }
433 if (ftruncate(fd, size) < 0) {
434 fprintf(stderr, "ftruncate failed: %m\n");
435 close(fd);
436 return NULL;
437 }
438
439 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
440 unlink(filename);
441
442 if (*data == MAP_FAILED) {
443 fprintf(stderr, "mmap failed: %m\n");
444 close(fd);
445 return NULL;
446 }
447
448 pool = wl_shm_create_pool(display->shm, fd, size);
449
450 close(fd);
451
452 return pool;
453}
454
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700455static struct shm_pool *
456shm_pool_create(struct display *display, size_t size)
457{
458 struct shm_pool *pool = malloc(sizeof *pool);
459
460 if (!pool)
461 return NULL;
462
463 pool->pool = make_shm_pool(display, size, &pool->data);
464 if (!pool->pool) {
465 free(pool);
466 return NULL;
467 }
468
469 pool->size = size;
470 pool->used = 0;
471
472 return pool;
473}
474
475static void *
476shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
477{
478 if (pool->used + size > pool->size)
479 return NULL;
480
481 *offset = pool->used;
482 pool->used += size;
483
484 return (char *) pool->data + *offset;
485}
486
487/* destroy the pool. this does not unmap the memory though */
488static void
489shm_pool_destroy(struct shm_pool *pool)
490{
491 munmap(pool->data, pool->size);
492 wl_shm_pool_destroy(pool->pool);
493 free(pool);
494}
495
496/* Start allocating from the beginning of the pool again */
497static void
498shm_pool_reset(struct shm_pool *pool)
499{
500 pool->used = 0;
501}
502
503static int
504data_length_for_shm_surface(struct rectangle *rect)
505{
506 int stride;
507
508 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
509 rect->width);
510 return stride * rect->height;
511}
512
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500513static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700514display_create_shm_surface_from_pool(struct display *display,
515 struct rectangle *rectangle,
516 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517{
518 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400519 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400520 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700521 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400522 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523
524 data = malloc(sizeof *data);
525 if (data == NULL)
526 return NULL;
527
528 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
529 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530 length = stride * rectangle->height;
531 data->pool = NULL;
532 map = shm_pool_allocate(pool, length, &offset);
533
534 if (!map) {
535 free(data);
536 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400537 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400539 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540 CAIRO_FORMAT_ARGB32,
541 rectangle->width,
542 rectangle->height,
543 stride);
544
545 cairo_surface_set_user_data (surface, &surface_data_key,
546 data, shm_surface_data_destroy);
547
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500549 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500551 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400552
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700553 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400554 rectangle->width,
555 rectangle->height,
556 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700558 return surface;
559}
560
561static cairo_surface_t *
562display_create_shm_surface(struct display *display,
563 struct rectangle *rectangle, uint32_t flags,
564 struct window *window)
565{
566 struct shm_surface_data *data;
567 struct shm_pool *pool;
568 cairo_surface_t *surface;
569
570 if (window && window->pool) {
571 shm_pool_reset(window->pool);
572 surface = display_create_shm_surface_from_pool(display,
573 rectangle,
574 flags,
575 window->pool);
576 if (surface)
577 return surface;
578 }
579
580 pool = shm_pool_create(display,
581 data_length_for_shm_surface(rectangle));
582 if (!pool)
583 return NULL;
584
585 surface =
586 display_create_shm_surface_from_pool(display, rectangle,
587 flags, pool);
588
589 if (!surface) {
590 shm_pool_destroy(pool);
591 return NULL;
592 }
593
594 /* make sure we destroy the pool when the surface is destroyed */
595 data = cairo_surface_get_user_data(surface, &surface_data_key);
596 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597
598 return surface;
599}
600
nobled7b87cb02011-02-01 18:51:47 +0000601static int
602check_size(struct rectangle *rect)
603{
604 if (rect->width && rect->height)
605 return 0;
606
607 fprintf(stderr, "tried to create surface of "
608 "width: %d, height: %d\n", rect->width, rect->height);
609 return -1;
610}
611
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612cairo_surface_t *
613display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100614 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400615 struct rectangle *rectangle,
616 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617{
nobled7b87cb02011-02-01 18:51:47 +0000618 if (check_size(rectangle) < 0)
619 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500620#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300621 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400622 return display_create_egl_window_surface(display,
623 surface,
624 flags,
625 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400627 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628}
629
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300630static const char *cursors[] = {
631 "bottom_left_corner",
632 "bottom_right_corner",
633 "bottom_side",
634 "grabbing",
635 "left_ptr",
636 "left_side",
637 "right_side",
638 "top_left_corner",
639 "top_right_corner",
640 "top_side",
641 "xterm",
642 "hand1",
643};
644
645static void
646create_cursors(struct display *display)
647{
648 unsigned int i;
649
650 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
651 display->cursors =
652 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
653
654 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
655 display->cursors[i] =
656 wl_cursor_theme_get_cursor(display->cursor_theme,
657 cursors[i]);
658}
659
660static void
661destroy_cursors(struct display *display)
662{
663 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800664 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300665}
666
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300667struct wl_cursor_image *
668display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400669{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300670 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300671 wl_cursor_theme_get_cursor(display->cursor_theme,
672 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300674 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400675}
676
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400677static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200678window_get_resize_dx_dy(struct window *window, int *x, int *y)
679{
680 if (window->resize_edges & WINDOW_RESIZING_LEFT)
681 *x = window->server_allocation.width - window->allocation.width;
682 else
683 *x = 0;
684
685 if (window->resize_edges & WINDOW_RESIZING_TOP)
686 *y = window->server_allocation.height -
687 window->allocation.height;
688 else
689 *y = 0;
690
691 window->resize_edges = 0;
692}
693
694static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500695window_attach_surface(struct window *window)
696{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400697 struct display *display = window->display;
698 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000699#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100700 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000701#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500702 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100703
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400704 if (window->type == TYPE_NONE) {
705 window->type = TYPE_TOPLEVEL;
706 if (display->shell)
707 wl_shell_surface_set_toplevel(window->shell_surface);
708 }
709
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100710 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000711#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100712 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
713 data = cairo_surface_get_user_data(window->cairo_surface,
714 &surface_data_key);
715
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100716 cairo_gl_surface_swapbuffers(window->cairo_surface);
717 wl_egl_window_get_attached_size(data->window,
718 &window->server_allocation.width,
719 &window->server_allocation.height);
720 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000721#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723 buffer =
724 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400725 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200727 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400729 wl_surface_damage(window->surface, 0, 0,
730 window->allocation.width,
731 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400733 cairo_surface_destroy(window->cairo_surface);
734 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000736 default:
737 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100738 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500739
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500740 if (window->input_region) {
741 wl_surface_set_input_region(window->surface,
742 window->input_region);
743 wl_region_destroy(window->input_region);
744 window->input_region = NULL;
745 }
746
747 if (window->opaque_region) {
748 wl_surface_set_opaque_region(window->surface,
749 window->opaque_region);
750 wl_region_destroy(window->opaque_region);
751 window->opaque_region = NULL;
752 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500753}
754
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500755void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400756window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500757{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400758 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100759 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500760}
761
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400762void
763window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400764{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500765 cairo_surface_reference(surface);
766
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400767 if (window->cairo_surface != NULL)
768 cairo_surface_destroy(window->cairo_surface);
769
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500770 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400771}
772
Benjamin Franzke22d54812011-07-16 19:50:32 +0000773#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100774static void
775window_resize_cairo_window_surface(struct window *window)
776{
777 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200778 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100779
780 data = cairo_surface_get_user_data(window->cairo_surface,
781 &surface_data_key);
782
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200783 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784 wl_egl_window_resize(data->window,
785 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200786 window->allocation.height,
787 x,y);
788
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 cairo_gl_surface_set_size(window->cairo_surface,
790 window->allocation.width,
791 window->allocation.height);
792}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000793#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400795struct display *
796window_get_display(struct window *window)
797{
798 return window->display;
799}
800
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400801void
802window_create_surface(struct window *window)
803{
804 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400805 uint32_t flags = 0;
806
807 if (!window->transparent)
808 flags = SURFACE_OPAQUE;
809
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400810 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500811#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
813 if (window->cairo_surface) {
814 window_resize_cairo_window_surface(window);
815 return;
816 }
817 surface = display_create_surface(window->display,
818 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400819 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821#endif
822 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400823 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400824 &window->allocation,
825 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800827 default:
828 surface = NULL;
829 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400831
832 window_set_surface(window, surface);
833 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834}
835
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200836static void frame_destroy(struct frame *frame);
837
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400838void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500839window_destroy(struct window *window)
840{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200841 struct display *display = window->display;
842 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100843 struct window_output *window_output;
844 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200845
846 if (window->redraw_scheduled)
847 wl_list_remove(&window->redraw_task.link);
848
849 wl_list_for_each(input, &display->input_list, link) {
850 if (input->pointer_focus == window)
851 input->pointer_focus = NULL;
852 if (input->keyboard_focus == window)
853 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500854 if (input->focus_widget &&
855 input->focus_widget->window == window)
856 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200857 }
858
Rob Bradford7507b572012-05-15 17:55:34 +0100859 wl_list_for_each_safe(window_output, window_output_tmp,
860 &window->window_output_list, link) {
861 free (window_output);
862 }
863
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500864 if (window->input_region)
865 wl_region_destroy(window->input_region);
866 if (window->opaque_region)
867 wl_region_destroy(window->opaque_region);
868
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200869 if (window->frame)
870 frame_destroy(window->frame);
871
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200872 if (window->shell_surface)
873 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500874 wl_surface_destroy(window->surface);
875 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200876
877 if (window->cairo_surface != NULL)
878 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200879
880 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500881 free(window);
882}
883
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500884static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500885widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400886{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500887 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400888
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500889 wl_list_for_each(child, &widget->child_list, link) {
890 target = widget_find_widget(child, x, y);
891 if (target)
892 return target;
893 }
894
895 if (widget->allocation.x <= x &&
896 x < widget->allocation.x + widget->allocation.width &&
897 widget->allocation.y <= y &&
898 y < widget->allocation.y + widget->allocation.height) {
899 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400900 }
901
902 return NULL;
903}
904
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500905static struct widget *
906widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400907{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500908 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400909
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500910 widget = malloc(sizeof *widget);
911 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500912 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500913 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500914 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500915 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500916 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300917 widget->tooltip = NULL;
918 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500919
920 return widget;
921}
922
923struct widget *
924window_add_widget(struct window *window, void *data)
925{
926 window->widget = widget_create(window, data);
927 wl_list_init(&window->widget->link);
928
929 return window->widget;
930}
931
932struct widget *
933widget_add_widget(struct widget *parent, void *data)
934{
935 struct widget *widget;
936
937 widget = widget_create(parent->window, data);
938 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400939
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500940 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400941}
942
943void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500944widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400945{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200946 struct display *display = widget->window->display;
947 struct input *input;
948
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300949 if (widget->tooltip) {
950 free(widget->tooltip);
951 widget->tooltip = NULL;
952 }
953
Pekka Paalanene156fb62012-01-19 13:51:38 +0200954 wl_list_for_each(input, &display->input_list, link) {
955 if (input->focus_widget == widget)
956 input->focus_widget = NULL;
957 }
958
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500959 wl_list_remove(&widget->link);
960 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400961}
962
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400963void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500964widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400965{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500966 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400967}
968
969void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500970widget_set_size(struct widget *widget, int32_t width, int32_t height)
971{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500972 widget->allocation.width = width;
973 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500974}
975
976void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500977widget_set_allocation(struct widget *widget,
978 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400979{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500980 widget->allocation.x = x;
981 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200982 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400983}
984
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500985void
986widget_set_transparent(struct widget *widget, int transparent)
987{
988 widget->opaque = !transparent;
989}
990
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400991void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500992widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400993{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500994 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995}
996
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500997void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500998widget_set_resize_handler(struct widget *widget,
999 widget_resize_handler_t handler)
1000{
1001 widget->resize_handler = handler;
1002}
1003
1004void
1005widget_set_redraw_handler(struct widget *widget,
1006 widget_redraw_handler_t handler)
1007{
1008 widget->redraw_handler = handler;
1009}
1010
1011void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001012widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001013{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001014 widget->enter_handler = handler;
1015}
1016
1017void
1018widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1019{
1020 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001021}
1022
1023void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001024widget_set_motion_handler(struct widget *widget,
1025 widget_motion_handler_t handler)
1026{
1027 widget->motion_handler = handler;
1028}
1029
1030void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001031widget_set_button_handler(struct widget *widget,
1032 widget_button_handler_t handler)
1033{
1034 widget->button_handler = handler;
1035}
1036
1037void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001038widget_schedule_redraw(struct widget *widget)
1039{
1040 window_schedule_redraw(widget->window);
1041}
1042
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001043cairo_surface_t *
1044window_get_surface(struct window *window)
1045{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001046 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001047}
1048
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001049struct wl_surface *
1050window_get_wl_surface(struct window *window)
1051{
1052 return window->surface;
1053}
1054
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001055struct wl_shell_surface *
1056window_get_wl_shell_surface(struct window *window)
1057{
1058 return window->shell_surface;
1059}
1060
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001061static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001062tooltip_redraw_handler(struct widget *widget, void *data)
1063{
1064 cairo_t *cr;
1065 const int32_t r = 3;
1066 struct tooltip *tooltip = data;
1067 int32_t width, height;
1068 struct window *window = widget->window;
1069
1070 cr = cairo_create(window->cairo_surface);
1071 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1072 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1073 cairo_paint(cr);
1074
1075 width = window->allocation.width;
1076 height = window->allocation.height;
1077 rounded_rect(cr, 0, 0, width, height, r);
1078
1079 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1080 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1081 cairo_fill(cr);
1082
1083 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1084 cairo_move_to(cr, 10, 16);
1085 cairo_show_text(cr, tooltip->entry);
1086 cairo_destroy(cr);
1087}
1088
1089static cairo_text_extents_t
1090get_text_extents(struct tooltip *tooltip)
1091{
1092 struct window *window;
1093 cairo_t *cr;
1094 cairo_text_extents_t extents;
1095
1096 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1097 * created yet */
1098 window = tooltip->widget->window->parent;
1099 cr = cairo_create(window->cairo_surface);
1100 cairo_text_extents(cr, tooltip->entry, &extents);
1101 cairo_destroy(cr);
1102
1103 return extents;
1104}
1105
1106static int
1107window_create_tooltip(struct tooltip *tooltip)
1108{
1109 struct widget *parent = tooltip->parent;
1110 struct display *display = parent->window->display;
1111 struct window *window;
1112 const int offset_y = 27;
1113 const int margin = 3;
1114 cairo_text_extents_t extents;
1115
1116 if (tooltip->widget)
1117 return 0;
1118
1119 window = window_create_transient(display, parent->window, tooltip->x,
1120 tooltip->y + offset_y,
1121 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1122 if (!window)
1123 return -1;
1124
1125 tooltip->window = window;
1126 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1127
1128 extents = get_text_extents(tooltip);
1129 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1130 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1131
1132 return 0;
1133}
1134
1135void
1136widget_destroy_tooltip(struct widget *parent)
1137{
1138 struct tooltip *tooltip = parent->tooltip;
1139
1140 parent->tooltip_count = 0;
1141 if (!tooltip)
1142 return;
1143
1144 if (tooltip->widget) {
1145 widget_destroy(tooltip->widget);
1146 window_destroy(tooltip->window);
1147 tooltip->widget = NULL;
1148 tooltip->window = NULL;
1149 }
1150
1151 close(tooltip->tooltip_fd);
1152 free(tooltip->entry);
1153 free(tooltip);
1154 parent->tooltip = NULL;
1155}
1156
1157static void
1158tooltip_func(struct task *task, uint32_t events)
1159{
1160 struct tooltip *tooltip =
1161 container_of(task, struct tooltip, tooltip_task);
1162 uint64_t exp;
1163
1164 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1165 window_create_tooltip(tooltip);
1166}
1167
1168#define TOOLTIP_TIMEOUT 500
1169static int
1170tooltip_timer_reset(struct tooltip *tooltip)
1171{
1172 struct itimerspec its;
1173
1174 its.it_interval.tv_sec = 0;
1175 its.it_interval.tv_nsec = 0;
1176 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1177 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1178 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1179 fprintf(stderr, "could not set timerfd\n: %m");
1180 return -1;
1181 }
1182
1183 return 0;
1184}
1185
1186int
1187widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1188{
1189 struct tooltip *tooltip = parent->tooltip;
1190
1191 parent->tooltip_count++;
1192 if (tooltip) {
1193 tooltip->x = x;
1194 tooltip->y = y;
1195 tooltip_timer_reset(tooltip);
1196 return 0;
1197 }
1198
1199 /* the handler might be triggered too fast via input device motion, so
1200 * we need this check here to make sure tooltip is fully initialized */
1201 if (parent->tooltip_count > 1)
1202 return 0;
1203
1204 tooltip = malloc(sizeof *tooltip);
1205 if (!tooltip)
1206 return -1;
1207
1208 parent->tooltip = tooltip;
1209 tooltip->parent = parent;
1210 tooltip->widget = NULL;
1211 tooltip->window = NULL;
1212 tooltip->x = x;
1213 tooltip->y = y;
1214 tooltip->entry = strdup(entry);
1215 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1216 if (tooltip->tooltip_fd < 0) {
1217 fprintf(stderr, "could not create timerfd\n: %m");
1218 return -1;
1219 }
1220
1221 tooltip->tooltip_task.run = tooltip_func;
1222 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1223 EPOLLIN, &tooltip->tooltip_task);
1224 tooltip_timer_reset(tooltip);
1225
1226 return 0;
1227}
1228
1229static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001230frame_resize_handler(struct widget *widget,
1231 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001233 struct frame *frame = data;
1234 struct widget *child = frame->child;
1235 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001236 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001237 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001238 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001239 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001240 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001241 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001243 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001244 decoration_width = (t->width + t->margin) * 2;
1245 decoration_height = t->width +
1246 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001247
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001248 allocation.x = t->width + t->margin;
1249 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001250 allocation.width = width - decoration_width;
1251 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001252
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001253 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001254
1255 wl_list_for_each(button, &frame->buttons_list, link)
1256 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001257 } else {
1258 decoration_width = 0;
1259 decoration_height = 0;
1260
1261 allocation.x = 0;
1262 allocation.y = 0;
1263 allocation.width = width;
1264 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001265 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001266
1267 wl_list_for_each(button, &frame->buttons_list, link)
1268 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001269 }
1270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001271 widget_set_allocation(child, allocation.x, allocation.y,
1272 allocation.width, allocation.height);
1273
1274 if (child->resize_handler)
1275 child->resize_handler(child,
1276 allocation.width,
1277 allocation.height,
1278 child->user_data);
1279
Scott Moreauf7e498c2012-05-14 11:39:29 -06001280 width = child->allocation.width + decoration_width;
1281 height = child->allocation.height + decoration_height;
1282
1283 widget->window->input_region =
1284 wl_compositor_create_region(display->compositor);
1285 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001286 t->margin, t->margin,
1287 width - 2 * t->margin,
1288 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001289
1290 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001291
1292 if (child->opaque) {
1293 widget->window->opaque_region =
1294 wl_compositor_create_region(display->compositor);
1295 wl_region_add(widget->window->opaque_region,
1296 opaque_margin, opaque_margin,
1297 widget->allocation.width - 2 * opaque_margin,
1298 widget->allocation.height - 2 * opaque_margin);
1299 }
Martin Minarik1998b152012-05-10 02:04:35 +02001300
1301 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001302 x_r = frame->widget->allocation.width - t->width - t->margin;
1303 x_l = t->width + t->margin;
1304 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001305 wl_list_for_each(button, &frame->buttons_list, link) {
1306 const int button_padding = 4;
1307 w = cairo_image_surface_get_width(button->icon);
1308 h = cairo_image_surface_get_height(button->icon);
1309
1310 if (button->decoration == FRAME_BUTTON_FANCY)
1311 w += 10;
1312
1313 if (button->align == FRAME_BUTTON_LEFT) {
1314 widget_set_allocation(button->widget,
1315 x_l, y , w + 1, h + 1);
1316 x_l += w;
1317 x_l += button_padding;
1318 } else {
1319 x_r -= w;
1320 widget_set_allocation(button->widget,
1321 x_r, y , w + 1, h + 1);
1322 x_r -= button_padding;
1323 }
1324 }
1325}
1326
1327static int
1328frame_button_enter_handler(struct widget *widget,
1329 struct input *input, float x, float y, void *data)
1330{
1331 struct frame_button *frame_button = data;
1332
1333 widget_schedule_redraw(frame_button->widget);
1334 frame_button->state = FRAME_BUTTON_OVER;
1335
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001336 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001337}
1338
1339static void
1340frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1341{
1342 struct frame_button *frame_button = data;
1343
1344 widget_schedule_redraw(frame_button->widget);
1345 frame_button->state = FRAME_BUTTON_DEFAULT;
1346}
1347
1348static void
1349frame_button_button_handler(struct widget *widget,
1350 struct input *input, uint32_t time,
1351 uint32_t button, uint32_t state, void *data)
1352{
1353 struct frame_button *frame_button = data;
1354 struct window *window = widget->window;
1355
1356 if (button != BTN_LEFT)
1357 return;
1358
1359 switch (state) {
1360 case 1:
1361 frame_button->state = FRAME_BUTTON_ACTIVE;
1362 widget_schedule_redraw(frame_button->widget);
1363
1364 if (frame_button->type == FRAME_BUTTON_ICON)
1365 window_show_frame_menu(window, input, time);
1366 return;
1367 case 0:
1368 frame_button->state = FRAME_BUTTON_DEFAULT;
1369 widget_schedule_redraw(frame_button->widget);
1370 break;
1371 }
1372
1373 switch (frame_button->type) {
1374 case FRAME_BUTTON_CLOSE:
1375 if (window->close_handler)
1376 window->close_handler(window->parent,
1377 window->user_data);
1378 else
1379 display_exit(window->display);
1380 break;
1381 case FRAME_BUTTON_MINIMIZE:
1382 fprintf(stderr,"Minimize stub\n");
1383 break;
1384 case FRAME_BUTTON_MAXIMIZE:
1385 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1386 break;
1387 default:
1388 /* Unknown operation */
1389 break;
1390 }
1391}
1392
1393static void
1394frame_button_redraw_handler(struct widget *widget, void *data)
1395{
1396 struct frame_button *frame_button = data;
1397 cairo_t *cr;
1398 int width, height, x, y;
1399 struct window *window = widget->window;
1400
1401 x = widget->allocation.x;
1402 y = widget->allocation.y;
1403 width = widget->allocation.width;
1404 height = widget->allocation.height;
1405
1406 if (!width)
1407 return;
1408 if (!height)
1409 return;
1410 if (widget->opaque)
1411 return;
1412
1413 cr = cairo_create(window->cairo_surface);
1414
1415 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1416 cairo_set_line_width(cr, 1);
1417
1418 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1419 cairo_rectangle (cr, x, y, 25, 16);
1420
1421 cairo_stroke_preserve(cr);
1422
1423 switch (frame_button->state) {
1424 case FRAME_BUTTON_DEFAULT:
1425 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1426 break;
1427 case FRAME_BUTTON_OVER:
1428 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1429 break;
1430 case FRAME_BUTTON_ACTIVE:
1431 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1432 break;
1433 }
1434
1435 cairo_fill (cr);
1436
1437 x += 4;
1438 }
1439
1440 cairo_set_source_surface(cr, frame_button->icon, x, y);
1441 cairo_paint(cr);
1442
1443 cairo_destroy(cr);
1444}
1445
1446static struct widget *
1447frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1448 enum frame_button_align align, enum frame_button_decoration style)
1449{
1450 struct frame_button *frame_button;
1451 const char *icon = data;
1452
1453 frame_button = malloc (sizeof *frame_button);
1454 memset(frame_button, 0, sizeof *frame_button);
1455
1456 frame_button->icon = cairo_image_surface_create_from_png(icon);
1457 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1458 frame_button->frame = frame;
1459 frame_button->type = type;
1460 frame_button->align = align;
1461 frame_button->decoration = style;
1462
1463 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1464
1465 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1466 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1467 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1468 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1469 return frame_button->widget;
1470}
1471
1472static void
1473frame_button_destroy(struct frame_button *frame_button)
1474{
1475 widget_destroy(frame_button->widget);
1476 wl_list_remove(&frame_button->link);
1477 cairo_surface_destroy(frame_button->icon);
1478 free(frame_button);
1479
1480 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001481}
1482
1483static void
1484frame_redraw_handler(struct widget *widget, void *data)
1485{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001486 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001487 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001488 struct theme *t = window->display->theme;
1489 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001490
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001491 if (window->type == TYPE_FULLSCREEN)
1492 return;
1493
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001494 cr = cairo_create(window->cairo_surface);
1495
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001496 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001497 flags |= THEME_FRAME_ACTIVE;
1498 theme_render_frame(t, cr, widget->allocation.width,
1499 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001500
1501 cairo_destroy(cr);
1502}
1503
1504static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001505frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001506{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001507 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001508 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001509
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001510 location = theme_get_location(t, input->sx, input->sy,
1511 frame->widget->allocation.width,
1512 frame->widget->allocation.height);
1513
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001514 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001515 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001516 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001517 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001518 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001519 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001520 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001521 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001522 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001523 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001524 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001525 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001526 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001527 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001528 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001529 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001530 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001531 case THEME_LOCATION_EXTERIOR:
1532 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001533 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001534 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001535 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001536}
1537
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001538static void
1539frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001540{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001541 switch (index) {
1542 case 0: /* close */
1543 if (window->close_handler)
1544 window->close_handler(window->parent,
1545 window->user_data);
1546 else
1547 display_exit(window->display);
1548 break;
1549 case 1: /* fullscreen */
1550 /* we don't have a way to get out of fullscreen for now */
1551 window_set_fullscreen(window, 1);
1552 break;
1553 case 2: /* rotate */
1554 case 3: /* scale */
1555 break;
1556 }
1557}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001558
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001559void
1560window_show_frame_menu(struct window *window,
1561 struct input *input, uint32_t time)
1562{
1563 int32_t x, y;
1564
1565 static const char *entries[] = {
1566 "Close", "Fullscreen", "Rotate", "Scale"
1567 };
1568
1569 input_get_position(input, &x, &y);
1570 window_show_menu(window->display, input, time, window,
1571 x - 10, y - 10, frame_menu_func, entries, 4);
1572}
1573
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001574static int
1575frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001576 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001577{
1578 return frame_get_pointer_image_for_location(data, input);
1579}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001580
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001581static int
1582frame_motion_handler(struct widget *widget,
1583 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001584 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001585{
1586 return frame_get_pointer_image_for_location(data, input);
1587}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001588
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001589static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001590frame_button_handler(struct widget *widget,
1591 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001592 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593
1594{
1595 struct frame *frame = data;
1596 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001597 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001598 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001599
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001600 location = theme_get_location(display->theme, input->sx, input->sy,
1601 frame->widget->allocation.width,
1602 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603
1604 if (window->display->shell && button == BTN_LEFT && state == 1) {
1605 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001606 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001607 if (!window->shell_surface)
1608 break;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001609 input_set_pointer_image(input, time, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001610 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001611 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001612 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001613 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001615 case THEME_LOCATION_RESIZING_TOP:
1616 case THEME_LOCATION_RESIZING_BOTTOM:
1617 case THEME_LOCATION_RESIZING_LEFT:
1618 case THEME_LOCATION_RESIZING_RIGHT:
1619 case THEME_LOCATION_RESIZING_TOP_LEFT:
1620 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1621 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1622 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623 if (!window->shell_surface)
1624 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001625 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001626
1627 if (!display->dpy) {
1628 /* If we're using shm, allocate a big
1629 pool to create buffers out of while
1630 we resize. We should probably base
1631 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001632 window->pool =
1633 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001634 }
1635
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001636 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001637 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001638 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001639 break;
1640 }
1641 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001642 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001643 }
1644}
1645
1646struct widget *
1647frame_create(struct window *window, void *data)
1648{
1649 struct frame *frame;
1650
1651 frame = malloc(sizeof *frame);
1652 memset(frame, 0, sizeof *frame);
1653
1654 frame->widget = window_add_widget(window, frame);
1655 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001656
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001657 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1658 widget_set_resize_handler(frame->widget, frame_resize_handler);
1659 widget_set_enter_handler(frame->widget, frame_enter_handler);
1660 widget_set_motion_handler(frame->widget, frame_motion_handler);
1661 widget_set_button_handler(frame->widget, frame_button_handler);
1662
Martin Minarik1998b152012-05-10 02:04:35 +02001663 /* Create empty list for frame buttons */
1664 wl_list_init(&frame->buttons_list);
1665
1666 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1667 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1668
1669 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1670 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1671
1672 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1673 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1674
1675 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1676 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1677
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001678 window->frame = frame;
1679
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001680 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001681}
1682
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001683static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001684frame_destroy(struct frame *frame)
1685{
Martin Minarik1998b152012-05-10 02:04:35 +02001686 struct frame_button *button, *tmp;
1687
1688 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1689 frame_button_destroy(button);
1690
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001691 /* frame->child must be destroyed by the application */
1692 widget_destroy(frame->widget);
1693 free(frame);
1694}
1695
1696static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001697input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001698 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001699{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001700 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001701 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001702
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001703 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001704 return;
1705
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001706 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001707 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001708 widget = old;
1709 if (input->grab)
1710 widget = input->grab;
1711 if (widget->leave_handler)
1712 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001713 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001714 }
1715
1716 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001717 widget = focus;
1718 if (input->grab)
1719 widget = input->grab;
1720 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001721 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001722 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001723 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001724
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001725 input_set_pointer_image(input, input->pointer_enter_serial,
1726 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001727 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001728}
1729
1730static void
Daniel Stone37816df2012-05-16 18:45:18 +01001731pointer_handle_motion(void *data, struct wl_pointer *pointer,
1732 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001733{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001734 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001735 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001736 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001737 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001738 float sx = wl_fixed_to_double(sx_w);
1739 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001740
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001741 input->sx = sx;
1742 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001743
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001744 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001745 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001746 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001747 }
1748
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001749 if (input->grab)
1750 widget = input->grab;
1751 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001752 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001753 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001754 cursor = widget->motion_handler(input->focus_widget,
1755 input, time, sx, sy,
1756 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001757
Daniel Stone37816df2012-05-16 18:45:18 +01001758 input_set_pointer_image(input, time, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001759}
1760
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001761void
1762input_grab(struct input *input, struct widget *widget, uint32_t button)
1763{
1764 input->grab = widget;
1765 input->grab_button = button;
1766}
1767
1768void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001769input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001770{
1771 struct widget *widget;
1772
1773 input->grab = NULL;
1774 if (input->pointer_focus) {
1775 widget = widget_find_widget(input->pointer_focus->widget,
1776 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001777 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001778 }
1779}
1780
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001781static void
Daniel Stone37816df2012-05-16 18:45:18 +01001782pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1783 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001784{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001785 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001786 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001787
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001788 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001789 if (input->focus_widget && input->grab == NULL && state)
1790 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001791
Neil Roberts6b28aad2012-01-23 19:11:18 +00001792 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001793 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001794 (*widget->button_handler)(widget,
1795 input, time,
1796 button, state,
1797 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001798
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001799 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001800 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001801}
1802
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001803static void
Daniel Stone37816df2012-05-16 18:45:18 +01001804pointer_handle_axis(void *data, struct wl_pointer *pointer,
Scott Moreau210d0792012-03-22 10:47:01 -06001805 uint32_t time, uint32_t axis, int32_t value)
1806{
1807}
1808
1809static void
Daniel Stone37816df2012-05-16 18:45:18 +01001810keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1811 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001812{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001813 struct input *input = data;
1814 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001815 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001816 uint32_t code, num_syms;
1817 const xkb_keysym_t *syms;
1818 xkb_keysym_t sym;
1819 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001820
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001821 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001822 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001823 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001824 return;
1825
Kristian Høgsberg70163132012-05-08 15:55:39 -04001826 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1827 xkb_state_update_key(d->xkb.state, code,
1828 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001829
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001830 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001831 XKB_STATE_DEPRESSED |
1832 XKB_STATE_LATCHED);
1833 input->modifiers = 0;
1834 if (mask & input->display->xkb.control_mask)
1835 input->modifiers |= MOD_CONTROL_MASK;
1836 if (mask & input->display->xkb.alt_mask)
1837 input->modifiers |= MOD_ALT_MASK;
1838 if (mask & input->display->xkb.shift_mask)
1839 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001840
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001841 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001842 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001843 if (state)
1844 window_set_maximized(window,
1845 window->type != TYPE_MAXIMIZED);
1846 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001847 if (num_syms == 1)
1848 sym = syms[0];
1849 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001850 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001851
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001852 (*window->key_handler)(window, input, time, key,
1853 sym, state, window->user_data);
1854 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001855}
1856
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001857static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001858input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001859{
1860 struct window *window = input->pointer_focus;
1861
1862 if (!window)
1863 return;
1864
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001865 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001866
Pekka Paalanene1207c72011-12-16 12:02:09 +02001867 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001868 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001869}
1870
1871static void
Daniel Stone37816df2012-05-16 18:45:18 +01001872pointer_handle_enter(void *data, struct wl_pointer *pointer,
1873 uint32_t serial, struct wl_surface *surface,
1874 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001875{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001876 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001877 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001878 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001879 float sx = wl_fixed_to_double(sx_w);
1880 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001881
Martin Minarik1998b152012-05-10 02:04:35 +02001882 if (!surface) {
1883 /* enter event for a window we've just destroyed */
1884 return;
1885 }
1886
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001887 input->display->serial = serial;
1888 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001889 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001890 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001891
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001892 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001893 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001894 window->pool = NULL;
1895 /* Schedule a redraw to free the pool */
1896 window_schedule_redraw(window);
1897 }
1898
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001899 input->sx = sx;
1900 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001901
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001902 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001903 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001904}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001905
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001906static void
Daniel Stone37816df2012-05-16 18:45:18 +01001907pointer_handle_leave(void *data, struct wl_pointer *pointer,
1908 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001909{
1910 struct input *input = data;
1911
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001912 input->display->serial = serial;
1913 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001914}
1915
1916static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001917input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001918{
1919 struct window *window = input->keyboard_focus;
1920
1921 if (!window)
1922 return;
1923
1924 window->keyboard_device = NULL;
1925 if (window->keyboard_focus_handler)
1926 (*window->keyboard_focus_handler)(window, NULL,
1927 window->user_data);
1928
1929 input->keyboard_focus = NULL;
1930}
1931
1932static void
Daniel Stone37816df2012-05-16 18:45:18 +01001933keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1934 uint32_t serial, struct wl_surface *surface,
1935 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001936{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001937 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001938 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001939
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001940 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001941 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001942
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001943 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001944 window->keyboard_device = input;
1945 if (window->keyboard_focus_handler)
1946 (*window->keyboard_focus_handler)(window,
1947 window->keyboard_device,
1948 window->user_data);
1949}
1950
1951static void
Daniel Stone37816df2012-05-16 18:45:18 +01001952keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1953 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001954{
1955 struct input *input = data;
1956
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001957 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001958 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001959}
1960
Daniel Stone37816df2012-05-16 18:45:18 +01001961static const struct wl_pointer_listener pointer_listener = {
1962 pointer_handle_enter,
1963 pointer_handle_leave,
1964 pointer_handle_motion,
1965 pointer_handle_button,
1966 pointer_handle_axis,
1967};
1968
1969static const struct wl_keyboard_listener keyboard_listener = {
1970 keyboard_handle_enter,
1971 keyboard_handle_leave,
1972 keyboard_handle_key,
1973};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001974
1975static void
Daniel Stone37816df2012-05-16 18:45:18 +01001976seat_handle_capabilities(void *data, struct wl_seat *seat,
1977 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001978{
Daniel Stone37816df2012-05-16 18:45:18 +01001979 struct input *input = data;
1980
1981 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1982 input->pointer = wl_seat_get_pointer(seat);
1983 wl_pointer_set_user_data(input->pointer, input);
1984 wl_pointer_add_listener(input->pointer, &pointer_listener,
1985 input);
1986 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1987 wl_pointer_destroy(input->pointer);
1988 input->pointer = NULL;
1989 }
1990
1991 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1992 input->keyboard = wl_seat_get_keyboard(seat);
1993 wl_keyboard_set_user_data(input->keyboard, input);
1994 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
1995 input);
1996 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
1997 wl_keyboard_destroy(input->keyboard);
1998 input->keyboard = NULL;
1999 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002000}
2001
Daniel Stone37816df2012-05-16 18:45:18 +01002002static const struct wl_seat_listener seat_listener = {
2003 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002004};
2005
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002006void
2007input_get_position(struct input *input, int32_t *x, int32_t *y)
2008{
2009 *x = input->sx;
2010 *y = input->sy;
2011}
2012
Daniel Stone37816df2012-05-16 18:45:18 +01002013struct wl_seat *
2014input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002015{
Daniel Stone37816df2012-05-16 18:45:18 +01002016 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002017}
2018
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002019uint32_t
2020input_get_modifiers(struct input *input)
2021{
2022 return input->modifiers;
2023}
2024
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002025struct widget *
2026input_get_focus_widget(struct input *input)
2027{
2028 return input->focus_widget;
2029}
2030
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002031struct data_offer {
2032 struct wl_data_offer *offer;
2033 struct input *input;
2034 struct wl_array types;
2035 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002036
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002037 struct task io_task;
2038 int fd;
2039 data_func_t func;
2040 int32_t x, y;
2041 void *user_data;
2042};
2043
2044static void
2045data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2046{
2047 struct data_offer *offer = data;
2048 char **p;
2049
2050 p = wl_array_add(&offer->types, sizeof *p);
2051 *p = strdup(type);
2052}
2053
2054static const struct wl_data_offer_listener data_offer_listener = {
2055 data_offer_offer,
2056};
2057
2058static void
2059data_offer_destroy(struct data_offer *offer)
2060{
2061 char **p;
2062
2063 offer->refcount--;
2064 if (offer->refcount == 0) {
2065 wl_data_offer_destroy(offer->offer);
2066 for (p = offer->types.data; *p; p++)
2067 free(*p);
2068 wl_array_release(&offer->types);
2069 free(offer);
2070 }
2071}
2072
2073static void
2074data_device_data_offer(void *data,
2075 struct wl_data_device *data_device, uint32_t id)
2076{
2077 struct data_offer *offer;
2078
2079 offer = malloc(sizeof *offer);
2080
2081 wl_array_init(&offer->types);
2082 offer->refcount = 1;
2083 offer->input = data;
2084
2085 /* FIXME: Generate typesafe wrappers for this */
2086 offer->offer = (struct wl_data_offer *)
2087 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2088 id, &wl_data_offer_interface);
2089
2090 wl_data_offer_add_listener(offer->offer,
2091 &data_offer_listener, offer);
2092}
2093
2094static void
2095data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002096 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002097 wl_fixed_t x_w, wl_fixed_t y_w,
2098 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002099{
2100 struct input *input = data;
2101 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002102 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002103 float x = wl_fixed_to_double(x_w);
2104 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002105 char **p;
2106
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002107 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002108 window = wl_surface_get_user_data(surface);
2109 input->pointer_focus = window;
2110
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002111 if (offer) {
2112 input->drag_offer = wl_data_offer_get_user_data(offer);
2113
2114 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2115 *p = NULL;
2116
2117 types_data = input->drag_offer->types.data;
2118 } else {
2119 input->drag_offer = NULL;
2120 types_data = NULL;
2121 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002122
2123 window = input->pointer_focus;
2124 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002125 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002126 window->user_data);
2127}
2128
2129static void
2130data_device_leave(void *data, struct wl_data_device *data_device)
2131{
2132 struct input *input = data;
2133
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002134 if (input->drag_offer) {
2135 data_offer_destroy(input->drag_offer);
2136 input->drag_offer = NULL;
2137 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002138}
2139
2140static void
2141data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002142 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002143{
2144 struct input *input = data;
2145 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002146 float x = wl_fixed_to_double(x_w);
2147 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002148 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002149
2150 input->sx = x;
2151 input->sy = y;
2152
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002153 if (input->drag_offer)
2154 types_data = input->drag_offer->types.data;
2155 else
2156 types_data = NULL;
2157
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002158 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002159 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002160 window->user_data);
2161}
2162
2163static void
2164data_device_drop(void *data, struct wl_data_device *data_device)
2165{
2166 struct input *input = data;
2167 struct window *window = input->pointer_focus;
2168
2169 if (window->drop_handler)
2170 window->drop_handler(window, input,
2171 input->sx, input->sy, window->user_data);
2172}
2173
2174static void
2175data_device_selection(void *data,
2176 struct wl_data_device *wl_data_device,
2177 struct wl_data_offer *offer)
2178{
2179 struct input *input = data;
2180 char **p;
2181
2182 if (input->selection_offer)
2183 data_offer_destroy(input->selection_offer);
2184
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002185 if (offer) {
2186 input->selection_offer = wl_data_offer_get_user_data(offer);
2187 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2188 *p = NULL;
2189 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002190}
2191
2192static const struct wl_data_device_listener data_device_listener = {
2193 data_device_data_offer,
2194 data_device_enter,
2195 data_device_leave,
2196 data_device_motion,
2197 data_device_drop,
2198 data_device_selection
2199};
2200
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002201void
2202input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2203{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002204 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002205 struct wl_cursor *cursor;
2206 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002207
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002208 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002209 return;
2210
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002211 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002212 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002213 return;
2214
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002215 image = cursor->images[0];
2216 buffer = wl_cursor_image_get_buffer(image);
2217 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002218 return;
2219
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002220 input->current_cursor = pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002221 wl_pointer_attach(input->pointer, time, buffer,
2222 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002223}
2224
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002225struct wl_data_device *
2226input_get_data_device(struct input *input)
2227{
2228 return input->data_device;
2229}
2230
2231void
2232input_set_selection(struct input *input,
2233 struct wl_data_source *source, uint32_t time)
2234{
2235 wl_data_device_set_selection(input->data_device, source, time);
2236}
2237
2238void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002239input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002240{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002241 wl_data_offer_accept(input->drag_offer->offer,
2242 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002243}
2244
2245static void
2246offer_io_func(struct task *task, uint32_t events)
2247{
2248 struct data_offer *offer =
2249 container_of(task, struct data_offer, io_task);
2250 unsigned int len;
2251 char buffer[4096];
2252
2253 len = read(offer->fd, buffer, sizeof buffer);
2254 offer->func(buffer, len,
2255 offer->x, offer->y, offer->user_data);
2256
2257 if (len == 0) {
2258 close(offer->fd);
2259 data_offer_destroy(offer);
2260 }
2261}
2262
2263static void
2264data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2265 data_func_t func, void *user_data)
2266{
2267 int p[2];
2268
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002269 if (pipe2(p, O_CLOEXEC) == -1)
2270 return;
2271
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002272 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2273 close(p[1]);
2274
2275 offer->io_task.run = offer_io_func;
2276 offer->fd = p[0];
2277 offer->func = func;
2278 offer->refcount++;
2279 offer->user_data = user_data;
2280
2281 display_watch_fd(offer->input->display,
2282 offer->fd, EPOLLIN, &offer->io_task);
2283}
2284
2285void
2286input_receive_drag_data(struct input *input, const char *mime_type,
2287 data_func_t func, void *data)
2288{
2289 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2290 input->drag_offer->x = input->sx;
2291 input->drag_offer->y = input->sy;
2292}
2293
2294int
2295input_receive_selection_data(struct input *input, const char *mime_type,
2296 data_func_t func, void *data)
2297{
2298 char **p;
2299
2300 if (input->selection_offer == NULL)
2301 return -1;
2302
2303 for (p = input->selection_offer->types.data; *p; p++)
2304 if (strcmp(mime_type, *p) == 0)
2305 break;
2306
2307 if (*p == NULL)
2308 return -1;
2309
2310 data_offer_receive_data(input->selection_offer,
2311 mime_type, func, data);
2312 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002313}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002314
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002315int
2316input_receive_selection_data_to_fd(struct input *input,
2317 const char *mime_type, int fd)
2318{
2319 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2320
2321 return 0;
2322}
2323
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002324void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002325window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002326{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002327 if (!window->shell_surface)
2328 return;
2329
Daniel Stone37816df2012-05-16 18:45:18 +01002330 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002331}
2332
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002333static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002334idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002335{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002336 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002337
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002338 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002339 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002340 widget_set_allocation(widget,
2341 window->pending_allocation.x,
2342 window->pending_allocation.y,
2343 window->pending_allocation.width,
2344 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002345
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002346 if (window->input_region) {
2347 wl_region_destroy(window->input_region);
2348 window->input_region = NULL;
2349 }
2350
2351 if (window->opaque_region) {
2352 wl_region_destroy(window->opaque_region);
2353 window->opaque_region = NULL;
2354 }
2355
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002356 if (widget->resize_handler)
2357 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002358 widget->allocation.width,
2359 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002360 widget->user_data);
2361
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002362 if (window->allocation.width != widget->allocation.width ||
2363 window->allocation.height != widget->allocation.height) {
2364 window->allocation = widget->allocation;
2365 window_schedule_redraw(window);
2366 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002367}
2368
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002369void
2370window_schedule_resize(struct window *window, int width, int height)
2371{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002372 window->pending_allocation.x = 0;
2373 window->pending_allocation.y = 0;
2374 window->pending_allocation.width = width;
2375 window->pending_allocation.height = height;
2376
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002377 window->resize_needed = 1;
2378 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002379}
2380
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002381void
2382widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2383{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002384 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002385}
2386
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002387static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002388handle_ping(void *data, struct wl_shell_surface *shell_surface,
2389 uint32_t serial)
2390{
2391 wl_shell_surface_pong(shell_surface, serial);
2392}
2393
2394static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002395handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002396 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002397{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002398 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002399
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002400 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002401 return;
2402
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002403 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002404 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002405}
2406
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002407static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002408menu_destroy(struct menu *menu)
2409{
2410 widget_destroy(menu->widget);
2411 window_destroy(menu->window);
2412 free(menu);
2413}
2414
2415static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002416handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2417{
2418 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002419 struct menu *menu = window->widget->user_data;
2420
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002421 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002422 * device. Or just use wl_callback. And this really needs to
2423 * be a window vfunc that the menu can set. And we need the
2424 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002425
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002426 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002427 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002428 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002429}
2430
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002431static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002432 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002433 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002434 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002435};
2436
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002437void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002438window_get_allocation(struct window *window,
2439 struct rectangle *allocation)
2440{
2441 *allocation = window->allocation;
2442}
2443
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002444static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002445widget_redraw(struct widget *widget)
2446{
2447 struct widget *child;
2448
2449 if (widget->redraw_handler)
2450 widget->redraw_handler(widget, widget->user_data);
2451 wl_list_for_each(child, &widget->child_list, link)
2452 widget_redraw(child);
2453}
2454
2455static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002456frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2457{
2458 struct window *window = data;
2459
2460 wl_callback_destroy(callback);
2461 window->redraw_scheduled = 0;
2462 if (window->redraw_needed)
2463 window_schedule_redraw(window);
2464}
2465
2466static const struct wl_callback_listener listener = {
2467 frame_callback
2468};
2469
2470static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002471idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002472{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002473 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002474 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002475
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002476 if (window->resize_needed)
2477 idle_resize(window);
2478
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002479 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002480 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002481 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002482 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002483 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002484
2485 callback = wl_surface_frame(window->surface);
2486 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002487}
2488
2489void
2490window_schedule_redraw(struct window *window)
2491{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002492 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002493 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002494 window->redraw_task.run = idle_redraw;
2495 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002496 window->redraw_scheduled = 1;
2497 }
2498}
2499
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002500void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002501window_set_custom(struct window *window)
2502{
2503 window->type = TYPE_CUSTOM;
2504}
2505
2506void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002507window_set_fullscreen(struct window *window, int fullscreen)
2508{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002509 if (!window->display->shell)
2510 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002511
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002512 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002513 return;
2514
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002515 if (fullscreen) {
2516 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002517 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002518 wl_shell_surface_set_fullscreen(window->shell_surface,
2519 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2520 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002521 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002522 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002523 wl_shell_surface_set_toplevel(window->shell_surface);
2524 window_schedule_resize(window,
2525 window->saved_allocation.width,
2526 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002527 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002528}
2529
2530void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002531window_set_maximized(struct window *window, int maximized)
2532{
2533 if (!window->display->shell)
2534 return;
2535
2536 if ((window->type == TYPE_MAXIMIZED) == maximized)
2537 return;
2538
2539 if (window->type == TYPE_TOPLEVEL) {
2540 window->saved_allocation = window->allocation;
2541 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2542 window->type = TYPE_MAXIMIZED;
2543 } else {
2544 wl_shell_surface_set_toplevel(window->shell_surface);
2545 window->type = TYPE_TOPLEVEL;
2546 window_schedule_resize(window,
2547 window->saved_allocation.width,
2548 window->saved_allocation.height);
2549 }
2550}
2551
2552void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002553window_set_user_data(struct window *window, void *data)
2554{
2555 window->user_data = data;
2556}
2557
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002558void *
2559window_get_user_data(struct window *window)
2560{
2561 return window->user_data;
2562}
2563
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002564void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002565window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002566 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002567{
2568 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002569}
2570
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002571void
2572window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002573 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002574{
2575 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002576}
2577
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002578void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002579window_set_data_handler(struct window *window, window_data_handler_t handler)
2580{
2581 window->data_handler = handler;
2582}
2583
2584void
2585window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2586{
2587 window->drop_handler = handler;
2588}
2589
2590void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002591window_set_close_handler(struct window *window,
2592 window_close_handler_t handler)
2593{
2594 window->close_handler = handler;
2595}
2596
2597void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002598window_set_title(struct window *window, const char *title)
2599{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002600 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002601 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002602 if (window->shell_surface)
2603 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002604}
2605
2606const char *
2607window_get_title(struct window *window)
2608{
2609 return window->title;
2610}
2611
2612void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002613window_damage(struct window *window, int32_t x, int32_t y,
2614 int32_t width, int32_t height)
2615{
2616 wl_surface_damage(window->surface, x, y, width, height);
2617}
2618
Casey Dahlin9074db52012-04-19 22:50:09 -04002619static void
2620surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002621 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002622{
Rob Bradford7507b572012-05-15 17:55:34 +01002623 struct window *window = data;
2624 struct output *output;
2625 struct output *output_found = NULL;
2626 struct window_output *window_output;
2627
2628 wl_list_for_each(output, &window->display->output_list, link) {
2629 if (output->output == wl_output) {
2630 output_found = output;
2631 break;
2632 }
2633 }
2634
2635 if (!output_found)
2636 return;
2637
2638 window_output = malloc (sizeof *window_output);
2639 window_output->output = output_found;
2640
2641 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002642}
2643
2644static void
2645surface_leave(void *data,
2646 struct wl_surface *wl_surface, struct wl_output *output)
2647{
Rob Bradford7507b572012-05-15 17:55:34 +01002648 struct window *window = data;
2649 struct window_output *window_output;
2650 struct window_output *window_output_found = NULL;
2651
2652 wl_list_for_each(window_output, &window->window_output_list, link) {
2653 if (window_output->output->output == output) {
2654 window_output_found = window_output;
2655 break;
2656 }
2657 }
2658
2659 if (window_output_found) {
2660 wl_list_remove(&window_output_found->link);
2661 free(window_output_found);
2662 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002663}
2664
2665static const struct wl_surface_listener surface_listener = {
2666 surface_enter,
2667 surface_leave
2668};
2669
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002670static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002671window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002672{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002673 struct window *window;
2674
2675 window = malloc(sizeof *window);
2676 if (window == NULL)
2677 return NULL;
2678
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002679 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002680 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002681 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002682 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002683 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002684 if (display->shell) {
2685 window->shell_surface =
2686 wl_shell_get_shell_surface(display->shell,
2687 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002688 if (window->title)
2689 wl_shell_surface_set_title(window->shell_surface,
2690 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002691 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002692 window->allocation.x = 0;
2693 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002694 window->allocation.width = 0;
2695 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002696 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002697 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002698 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002699 window->input_region = NULL;
2700 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002701
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002702 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002703#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002704 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002705#else
2706 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2707#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002708 else
2709 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002710
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002711 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002712 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002713 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002714
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002715 if (window->shell_surface) {
2716 wl_shell_surface_set_user_data(window->shell_surface, window);
2717 wl_shell_surface_add_listener(window->shell_surface,
2718 &shell_surface_listener, window);
2719 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002720
Rob Bradford7507b572012-05-15 17:55:34 +01002721 wl_list_init (&window->window_output_list);
2722
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002723 return window;
2724}
2725
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002726struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002727window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002728{
2729 struct window *window;
2730
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002731 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002732 if (!window)
2733 return NULL;
2734
2735 return window;
2736}
2737
2738struct window *
2739window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002740 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002741{
2742 struct window *window;
2743
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002744 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002745 if (!window)
2746 return NULL;
2747
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002748 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002749 window->x = x;
2750 window->y = y;
2751
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002752 if (display->shell)
2753 wl_shell_surface_set_transient(window->shell_surface,
2754 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002755 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002756
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002757 return window;
2758}
2759
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002760static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002761menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002762{
2763 int next;
2764
2765 next = (sy - 8) / 20;
2766 if (menu->current != next) {
2767 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002768 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002769 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002770}
2771
2772static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002773menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002774 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002775 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002776{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002777 struct menu *menu = data;
2778
2779 if (widget == menu->widget)
2780 menu_set_item(data, y);
2781
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002782 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002783}
2784
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002785static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002786menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002787 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002788{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002789 struct menu *menu = data;
2790
2791 if (widget == menu->widget)
2792 menu_set_item(data, y);
2793
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002794 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002795}
2796
2797static void
2798menu_leave_handler(struct widget *widget, struct input *input, void *data)
2799{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002800 struct menu *menu = data;
2801
2802 if (widget == menu->widget)
2803 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002804}
2805
2806static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002807menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002808 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002809 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002810
2811{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002812 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002813
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002814 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002815 /* Either relase after press-drag-release or
2816 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002817 menu->func(menu->window->parent,
2818 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002819 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002820 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002821 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002822}
2823
2824static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002825menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002826{
2827 cairo_t *cr;
2828 const int32_t r = 3, margin = 3;
2829 struct menu *menu = data;
2830 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002831 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002832
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002833 cr = cairo_create(window->cairo_surface);
2834 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2835 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2836 cairo_paint(cr);
2837
2838 width = window->allocation.width;
2839 height = window->allocation.height;
2840 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002841
2842 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002843 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2844 cairo_fill(cr);
2845
2846 for (i = 0; i < menu->count; i++) {
2847 if (i == menu->current) {
2848 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2849 cairo_rectangle(cr, margin, i * 20 + margin,
2850 width - 2 * margin, 20);
2851 cairo_fill(cr);
2852 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2853 cairo_move_to(cr, 10, i * 20 + 16);
2854 cairo_show_text(cr, menu->entries[i]);
2855 } else {
2856 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2857 cairo_move_to(cr, 10, i * 20 + 16);
2858 cairo_show_text(cr, menu->entries[i]);
2859 }
2860 }
2861
2862 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002863}
2864
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002865void
2866window_show_menu(struct display *display,
2867 struct input *input, uint32_t time, struct window *parent,
2868 int32_t x, int32_t y,
2869 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002870{
2871 struct window *window;
2872 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002873 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002874
2875 menu = malloc(sizeof *menu);
2876 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002877 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002878
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002879 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002880 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002881 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002882
2883 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002884 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002885 menu->entries = entries;
2886 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002887 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002888 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002889 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002890 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002891 window->type = TYPE_MENU;
2892 window->x = x;
2893 window->y = y;
2894
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002895 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002896 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002897 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002898 window->parent->shell_surface,
2899 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002900
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002901 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002902 widget_set_enter_handler(menu->widget, menu_enter_handler);
2903 widget_set_leave_handler(menu->widget, menu_leave_handler);
2904 widget_set_motion_handler(menu->widget, menu_motion_handler);
2905 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002906
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002907 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002908 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002909}
2910
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002911void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002912window_set_buffer_type(struct window *window, enum window_buffer_type type)
2913{
2914 window->buffer_type = type;
2915}
2916
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002917
2918static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002919display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002920 struct wl_output *wl_output,
2921 int x, int y,
2922 int physical_width,
2923 int physical_height,
2924 int subpixel,
2925 const char *make,
2926 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002927{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002928 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002929
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002930 output->allocation.x = x;
2931 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002932}
2933
2934static void
2935display_handle_mode(void *data,
2936 struct wl_output *wl_output,
2937 uint32_t flags,
2938 int width,
2939 int height,
2940 int refresh)
2941{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002942 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002943 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002944
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002945 if (flags & WL_OUTPUT_MODE_CURRENT) {
2946 output->allocation.width = width;
2947 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002948 if (display->output_configure_handler)
2949 (*display->output_configure_handler)(
2950 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002951 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002952}
2953
2954static const struct wl_output_listener output_listener = {
2955 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002956 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002957};
2958
2959static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002960display_add_output(struct display *d, uint32_t id)
2961{
2962 struct output *output;
2963
2964 output = malloc(sizeof *output);
2965 if (output == NULL)
2966 return;
2967
2968 memset(output, 0, sizeof *output);
2969 output->display = d;
2970 output->output =
2971 wl_display_bind(d->display, id, &wl_output_interface);
2972 wl_list_insert(d->output_list.prev, &output->link);
2973
2974 wl_output_add_listener(output->output, &output_listener, output);
2975}
2976
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002977static void
2978output_destroy(struct output *output)
2979{
2980 if (output->destroy_handler)
2981 (*output->destroy_handler)(output, output->user_data);
2982
2983 wl_output_destroy(output->output);
2984 wl_list_remove(&output->link);
2985 free(output);
2986}
2987
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002988void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002989display_set_output_configure_handler(struct display *display,
2990 display_output_handler_t handler)
2991{
2992 struct output *output;
2993
2994 display->output_configure_handler = handler;
2995 if (!handler)
2996 return;
2997
2998 wl_list_for_each(output, &display->output_list, link)
2999 (*display->output_configure_handler)(output,
3000 display->user_data);
3001}
3002
3003void
3004output_set_user_data(struct output *output, void *data)
3005{
3006 output->user_data = data;
3007}
3008
3009void *
3010output_get_user_data(struct output *output)
3011{
3012 return output->user_data;
3013}
3014
3015void
3016output_set_destroy_handler(struct output *output,
3017 display_output_handler_t handler)
3018{
3019 output->destroy_handler = handler;
3020 /* FIXME: implement this, once we have way to remove outputs */
3021}
3022
3023void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003024output_get_allocation(struct output *output, struct rectangle *allocation)
3025{
3026 *allocation = output->allocation;
3027}
3028
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003029struct wl_output *
3030output_get_wl_output(struct output *output)
3031{
3032 return output->output;
3033}
3034
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003035static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003036display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003037{
3038 struct input *input;
3039
3040 input = malloc(sizeof *input);
3041 if (input == NULL)
3042 return;
3043
3044 memset(input, 0, sizeof *input);
3045 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003046 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003047 input->pointer_focus = NULL;
3048 input->keyboard_focus = NULL;
3049 wl_list_insert(d->input_list.prev, &input->link);
3050
Daniel Stone37816df2012-05-16 18:45:18 +01003051 wl_seat_add_listener(input->seat, &seat_listener, input);
3052 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003053
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003054 input->data_device =
3055 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003056 input->seat);
3057 wl_data_device_add_listener(input->data_device, &data_device_listener,
3058 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003059}
3060
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003061static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003062input_destroy(struct input *input)
3063{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003064 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003065 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003066
3067 if (input->drag_offer)
3068 data_offer_destroy(input->drag_offer);
3069
3070 if (input->selection_offer)
3071 data_offer_destroy(input->selection_offer);
3072
3073 wl_data_device_destroy(input->data_device);
3074 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003075 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003076 free(input);
3077}
3078
3079static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003080display_handle_global(struct wl_display *display, uint32_t id,
3081 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003082{
3083 struct display *d = data;
3084
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003085 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003086 d->compositor =
3087 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003088 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003089 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003090 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003091 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003092 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003093 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003094 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003095 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003096 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3097 d->data_device_manager =
3098 wl_display_bind(display, id,
3099 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003100 }
3101}
3102
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003103static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003104init_xkb(struct display *d)
3105{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003106 d->xkb.names.rules = "evdev";
3107 d->xkb.names.model = "pc105";
3108 d->xkb.names.layout = (char *) option_xkb_layout;
3109 d->xkb.names.variant = (char *) option_xkb_variant;
3110 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003111
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003112 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003113 if (!d->xkb.context) {
3114 fprintf(stderr, "Failed to create XKB context\n");
3115 exit(1);
3116 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003117
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003118 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003119 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003120 fprintf(stderr, "Failed to compile keymap\n");
3121 exit(1);
3122 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003123
3124 d->xkb.state = xkb_state_new(d->xkb.keymap);
3125 if (!d->xkb.state) {
3126 fprintf(stderr, "Failed to create XKB state\n");
3127 exit(1);
3128 }
3129
3130 d->xkb.control_mask =
3131 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3132 d->xkb.alt_mask =
3133 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3134 d->xkb.shift_mask =
3135 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3136
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003137}
3138
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003139static void
3140fini_xkb(struct display *display)
3141{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003142 xkb_state_unref(display->xkb.state);
3143 xkb_map_unref(display->xkb.keymap);
3144 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003145}
3146
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003147#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003148static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003149init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003150{
3151 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003152 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003153
Rob Clark6396ed32012-03-11 19:48:41 -05003154#ifdef USE_CAIRO_GLESV2
3155# define GL_BIT EGL_OPENGL_ES2_BIT
3156#else
3157# define GL_BIT EGL_OPENGL_BIT
3158#endif
3159
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003160 static const EGLint argb_cfg_attribs[] = {
3161 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003162 EGL_RED_SIZE, 1,
3163 EGL_GREEN_SIZE, 1,
3164 EGL_BLUE_SIZE, 1,
3165 EGL_ALPHA_SIZE, 1,
3166 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003167 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003168 EGL_NONE
3169 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003170
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003171#ifdef USE_CAIRO_GLESV2
3172 static const EGLint context_attribs[] = {
3173 EGL_CONTEXT_CLIENT_VERSION, 2,
3174 EGL_NONE
3175 };
3176 EGLint api = EGL_OPENGL_ES_API;
3177#else
3178 EGLint *context_attribs = NULL;
3179 EGLint api = EGL_OPENGL_API;
3180#endif
3181
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003182 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003183 if (!eglInitialize(d->dpy, &major, &minor)) {
3184 fprintf(stderr, "failed to initialize display\n");
3185 return -1;
3186 }
3187
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003188 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003189 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3190 return -1;
3191 }
3192
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003193 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3194 &d->argb_config, 1, &n) || n != 1) {
3195 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003196 return -1;
3197 }
3198
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003199 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003200 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003201 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003202 fprintf(stderr, "failed to create context\n");
3203 return -1;
3204 }
3205
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003206 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003207 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003208 return -1;
3209 }
3210
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003211#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003212 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3213 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3214 fprintf(stderr, "failed to get cairo egl argb device\n");
3215 return -1;
3216 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003217#endif
3218
3219 return 0;
3220}
3221
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003222static void
3223fini_egl(struct display *display)
3224{
3225#ifdef HAVE_CAIRO_EGL
3226 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003227#endif
3228
3229 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3230 EGL_NO_CONTEXT);
3231
3232 eglTerminate(display->dpy);
3233 eglReleaseThread();
3234}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003235#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003236
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003237static int
3238event_mask_update(uint32_t mask, void *data)
3239{
3240 struct display *d = data;
3241
3242 d->mask = mask;
3243
3244 return 0;
3245}
3246
3247static void
3248handle_display_data(struct task *task, uint32_t events)
3249{
3250 struct display *display =
3251 container_of(task, struct display, display_task);
3252
3253 wl_display_iterate(display->display, display->mask);
3254}
3255
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003256struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003257display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003258{
3259 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003260
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003261 argc = parse_options(xkb_options,
3262 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003263
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003264 d = malloc(sizeof *d);
3265 if (d == NULL)
3266 return NULL;
3267
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003268 memset(d, 0, sizeof *d);
3269
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003270 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003271 if (d->display == NULL) {
3272 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003273 return NULL;
3274 }
3275
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003276 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3277 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3278 d->display_task.run = handle_display_data;
3279 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3280
3281 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003282 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003283 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003284
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003285 /* Set up listener so we'll catch all events. */
3286 wl_display_add_global_listener(d->display,
3287 display_handle_global, d);
3288
3289 /* Process connection events. */
3290 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003291#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003292 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003293 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003294#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003295
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003296 d->image_target_texture_2d =
3297 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3298 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3299 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3300
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003301 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003302
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003303 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003304
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003305 wl_list_init(&d->window_list);
3306
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003307 init_xkb(d);
3308
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003309 return d;
3310}
3311
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003312static void
3313display_destroy_outputs(struct display *display)
3314{
3315 struct output *tmp;
3316 struct output *output;
3317
3318 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3319 output_destroy(output);
3320}
3321
Pekka Paalanene1207c72011-12-16 12:02:09 +02003322static void
3323display_destroy_inputs(struct display *display)
3324{
3325 struct input *tmp;
3326 struct input *input;
3327
3328 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3329 input_destroy(input);
3330}
3331
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003332void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003333display_destroy(struct display *display)
3334{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003335 if (!wl_list_empty(&display->window_list))
3336 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3337
3338 if (!wl_list_empty(&display->deferred_list))
3339 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3340
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003341 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003342 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003343
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003344 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003345
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003346 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003347 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003348
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003349#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003350 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003351#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003352
Pekka Paalanenc2052982011-12-16 11:41:32 +02003353 if (display->shell)
3354 wl_shell_destroy(display->shell);
3355
3356 if (display->shm)
3357 wl_shm_destroy(display->shm);
3358
3359 if (display->data_device_manager)
3360 wl_data_device_manager_destroy(display->data_device_manager);
3361
3362 wl_compositor_destroy(display->compositor);
3363
3364 close(display->epoll_fd);
3365
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003366 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003367 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003368 free(display);
3369}
3370
3371void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003372display_set_user_data(struct display *display, void *data)
3373{
3374 display->user_data = data;
3375}
3376
3377void *
3378display_get_user_data(struct display *display)
3379{
3380 return display->user_data;
3381}
3382
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003383struct wl_display *
3384display_get_display(struct display *display)
3385{
3386 return display->display;
3387}
3388
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003389struct output *
3390display_get_output(struct display *display)
3391{
3392 return container_of(display->output_list.next, struct output, link);
3393}
3394
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003395struct wl_compositor *
3396display_get_compositor(struct display *display)
3397{
3398 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003399}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003400
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003401uint32_t
3402display_get_serial(struct display *display)
3403{
3404 return display->serial;
3405}
3406
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003407EGLDisplay
3408display_get_egl_display(struct display *d)
3409{
3410 return d->dpy;
3411}
3412
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003413struct wl_data_source *
3414display_create_data_source(struct display *display)
3415{
3416 return wl_data_device_manager_create_data_source(display->data_device_manager);
3417}
3418
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003419EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003420display_get_argb_egl_config(struct display *d)
3421{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003422 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003423}
3424
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003425struct wl_shell *
3426display_get_shell(struct display *display)
3427{
3428 return display->shell;
3429}
3430
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003431int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003432display_acquire_window_surface(struct display *display,
3433 struct window *window,
3434 EGLContext ctx)
3435{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003436#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003437 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003438 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003439
3440 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003441 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003442 device = cairo_surface_get_device(window->cairo_surface);
3443 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003444 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003445
Benjamin Franzke0c991632011-09-27 21:57:31 +02003446 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003447 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003448 ctx = display->argb_ctx;
3449 else
3450 assert(0);
3451 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003452
3453 data = cairo_surface_get_user_data(window->cairo_surface,
3454 &surface_data_key);
3455
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003456 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003457 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003458 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3459 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003460
3461 return 0;
3462#else
3463 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003464#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003465}
3466
3467void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003468display_release_window_surface(struct display *display,
3469 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003470{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003471#ifdef HAVE_CAIRO_EGL
3472 cairo_device_t *device;
3473
3474 device = cairo_surface_get_device(window->cairo_surface);
3475 if (!device)
3476 return;
3477
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003478 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003479 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003480 cairo_device_release(device);
3481#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003482}
3483
3484void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003485display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003486{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003487 wl_list_insert(&display->deferred_list, &task->link);
3488}
3489
3490void
3491display_watch_fd(struct display *display,
3492 int fd, uint32_t events, struct task *task)
3493{
3494 struct epoll_event ep;
3495
3496 ep.events = events;
3497 ep.data.ptr = task;
3498 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3499}
3500
3501void
3502display_run(struct display *display)
3503{
3504 struct task *task;
3505 struct epoll_event ep[16];
3506 int i, count;
3507
Pekka Paalanen826d7952011-12-15 10:14:07 +02003508 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003509 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003510 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003511
3512 while (!wl_list_empty(&display->deferred_list)) {
3513 task = container_of(display->deferred_list.next,
3514 struct task, link);
3515 wl_list_remove(&task->link);
3516 task->run(task, 0);
3517 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003518
3519 if (!display->running)
3520 break;
3521
3522 wl_display_flush(display->display);
3523
3524 count = epoll_wait(display->epoll_fd,
3525 ep, ARRAY_LENGTH(ep), -1);
3526 for (i = 0; i < count; i++) {
3527 task = ep[i].data.ptr;
3528 task->run(task, ep[i].events);
3529 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003530 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003531}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003532
3533void
3534display_exit(struct display *display)
3535{
3536 display->running = 0;
3537}