blob: c4e01e75c226b5bd7fb8df0469decebc67f4c86e [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 Oliveirad8f527c2012-05-25 09:30:02 +0300291 POINTER_DEFAULT = 100,
292 POINTER_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
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400621 if (display->dpy)
622 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);
664}
665
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300666struct wl_cursor_image *
667display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400668{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300669 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300670 wl_cursor_theme_get_cursor(display->cursor_theme,
671 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400672
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300673 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400674}
675
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400676static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200677window_get_resize_dx_dy(struct window *window, int *x, int *y)
678{
679 if (window->resize_edges & WINDOW_RESIZING_LEFT)
680 *x = window->server_allocation.width - window->allocation.width;
681 else
682 *x = 0;
683
684 if (window->resize_edges & WINDOW_RESIZING_TOP)
685 *y = window->server_allocation.height -
686 window->allocation.height;
687 else
688 *y = 0;
689
690 window->resize_edges = 0;
691}
692
693static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500694window_attach_surface(struct window *window)
695{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400696 struct display *display = window->display;
697 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000698#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100699 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000700#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500701 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100702
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400703 if (window->type == TYPE_NONE) {
704 window->type = TYPE_TOPLEVEL;
705 if (display->shell)
706 wl_shell_surface_set_toplevel(window->shell_surface);
707 }
708
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100709 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000710#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100711 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
712 data = cairo_surface_get_user_data(window->cairo_surface,
713 &surface_data_key);
714
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100715 cairo_gl_surface_swapbuffers(window->cairo_surface);
716 wl_egl_window_get_attached_size(data->window,
717 &window->server_allocation.width,
718 &window->server_allocation.height);
719 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000720#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100721 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722 buffer =
723 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400724 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200726 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400728 wl_surface_damage(window->surface, 0, 0,
729 window->allocation.width,
730 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400732 cairo_surface_destroy(window->cairo_surface);
733 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100734 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000735 default:
736 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100737 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500738
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500739 if (window->input_region) {
740 wl_surface_set_input_region(window->surface,
741 window->input_region);
742 wl_region_destroy(window->input_region);
743 window->input_region = NULL;
744 }
745
746 if (window->opaque_region) {
747 wl_surface_set_opaque_region(window->surface,
748 window->opaque_region);
749 wl_region_destroy(window->opaque_region);
750 window->opaque_region = NULL;
751 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500752}
753
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500754void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400755window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500756{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400757 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100758 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500759}
760
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400761void
762window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400763{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500764 cairo_surface_reference(surface);
765
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400766 if (window->cairo_surface != NULL)
767 cairo_surface_destroy(window->cairo_surface);
768
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500769 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400770}
771
Benjamin Franzke22d54812011-07-16 19:50:32 +0000772#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100773static void
774window_resize_cairo_window_surface(struct window *window)
775{
776 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200777 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778
779 data = cairo_surface_get_user_data(window->cairo_surface,
780 &surface_data_key);
781
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200782 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100783 wl_egl_window_resize(data->window,
784 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200785 window->allocation.height,
786 x,y);
787
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 cairo_gl_surface_set_size(window->cairo_surface,
789 window->allocation.width,
790 window->allocation.height);
791}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000792#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100793
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400794struct display *
795window_get_display(struct window *window)
796{
797 return window->display;
798}
799
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400800void
801window_create_surface(struct window *window)
802{
803 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400804 uint32_t flags = 0;
805
806 if (!window->transparent)
807 flags = SURFACE_OPAQUE;
808
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400809 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500810#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
812 if (window->cairo_surface) {
813 window_resize_cairo_window_surface(window);
814 return;
815 }
816 surface = display_create_surface(window->display,
817 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400818 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400820#endif
821 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400822 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400823 &window->allocation,
824 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400825 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800826 default:
827 surface = NULL;
828 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400829 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400830
831 window_set_surface(window, surface);
832 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833}
834
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200835static void frame_destroy(struct frame *frame);
836
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400837void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500838window_destroy(struct window *window)
839{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200840 struct display *display = window->display;
841 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100842 struct window_output *window_output;
843 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200844
845 if (window->redraw_scheduled)
846 wl_list_remove(&window->redraw_task.link);
847
848 wl_list_for_each(input, &display->input_list, link) {
849 if (input->pointer_focus == window)
850 input->pointer_focus = NULL;
851 if (input->keyboard_focus == window)
852 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500853 if (input->focus_widget &&
854 input->focus_widget->window == window)
855 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200856 }
857
Rob Bradford7507b572012-05-15 17:55:34 +0100858 wl_list_for_each_safe(window_output, window_output_tmp,
859 &window->window_output_list, link) {
860 free (window_output);
861 }
862
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500863 if (window->input_region)
864 wl_region_destroy(window->input_region);
865 if (window->opaque_region)
866 wl_region_destroy(window->opaque_region);
867
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200868 if (window->frame)
869 frame_destroy(window->frame);
870
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200871 if (window->shell_surface)
872 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500873 wl_surface_destroy(window->surface);
874 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200875
876 if (window->cairo_surface != NULL)
877 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200878
879 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500880 free(window);
881}
882
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500883static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500884widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400885{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500886 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400887
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500888 wl_list_for_each(child, &widget->child_list, link) {
889 target = widget_find_widget(child, x, y);
890 if (target)
891 return target;
892 }
893
894 if (widget->allocation.x <= x &&
895 x < widget->allocation.x + widget->allocation.width &&
896 widget->allocation.y <= y &&
897 y < widget->allocation.y + widget->allocation.height) {
898 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400899 }
900
901 return NULL;
902}
903
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500904static struct widget *
905widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400906{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500907 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400908
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500909 widget = malloc(sizeof *widget);
910 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500911 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500912 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500913 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500914 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500915 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300916 widget->tooltip = NULL;
917 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500918
919 return widget;
920}
921
922struct widget *
923window_add_widget(struct window *window, void *data)
924{
925 window->widget = widget_create(window, data);
926 wl_list_init(&window->widget->link);
927
928 return window->widget;
929}
930
931struct widget *
932widget_add_widget(struct widget *parent, void *data)
933{
934 struct widget *widget;
935
936 widget = widget_create(parent->window, data);
937 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400938
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500939 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400940}
941
942void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500943widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400944{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200945 struct display *display = widget->window->display;
946 struct input *input;
947
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300948 if (widget->tooltip) {
949 free(widget->tooltip);
950 widget->tooltip = NULL;
951 }
952
Pekka Paalanene156fb62012-01-19 13:51:38 +0200953 wl_list_for_each(input, &display->input_list, link) {
954 if (input->focus_widget == widget)
955 input->focus_widget = NULL;
956 }
957
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500958 wl_list_remove(&widget->link);
959 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400960}
961
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400962void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500963widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400964{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500965 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966}
967
968void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500969widget_set_size(struct widget *widget, int32_t width, int32_t height)
970{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500971 widget->allocation.width = width;
972 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500973}
974
975void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500976widget_set_allocation(struct widget *widget,
977 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400978{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500979 widget->allocation.x = x;
980 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200981 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400982}
983
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500984void
985widget_set_transparent(struct widget *widget, int transparent)
986{
987 widget->opaque = !transparent;
988}
989
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400990void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500991widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400992{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500993 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400994}
995
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500996void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500997widget_set_resize_handler(struct widget *widget,
998 widget_resize_handler_t handler)
999{
1000 widget->resize_handler = handler;
1001}
1002
1003void
1004widget_set_redraw_handler(struct widget *widget,
1005 widget_redraw_handler_t handler)
1006{
1007 widget->redraw_handler = handler;
1008}
1009
1010void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001011widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001012{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001013 widget->enter_handler = handler;
1014}
1015
1016void
1017widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1018{
1019 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001020}
1021
1022void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001023widget_set_motion_handler(struct widget *widget,
1024 widget_motion_handler_t handler)
1025{
1026 widget->motion_handler = handler;
1027}
1028
1029void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001030widget_set_button_handler(struct widget *widget,
1031 widget_button_handler_t handler)
1032{
1033 widget->button_handler = handler;
1034}
1035
1036void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001037widget_schedule_redraw(struct widget *widget)
1038{
1039 window_schedule_redraw(widget->window);
1040}
1041
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001042cairo_surface_t *
1043window_get_surface(struct window *window)
1044{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001045 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001046}
1047
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001048struct wl_surface *
1049window_get_wl_surface(struct window *window)
1050{
1051 return window->surface;
1052}
1053
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001054struct wl_shell_surface *
1055window_get_wl_shell_surface(struct window *window)
1056{
1057 return window->shell_surface;
1058}
1059
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001060static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001061tooltip_redraw_handler(struct widget *widget, void *data)
1062{
1063 cairo_t *cr;
1064 const int32_t r = 3;
1065 struct tooltip *tooltip = data;
1066 int32_t width, height;
1067 struct window *window = widget->window;
1068
1069 cr = cairo_create(window->cairo_surface);
1070 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1071 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1072 cairo_paint(cr);
1073
1074 width = window->allocation.width;
1075 height = window->allocation.height;
1076 rounded_rect(cr, 0, 0, width, height, r);
1077
1078 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1079 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1080 cairo_fill(cr);
1081
1082 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1083 cairo_move_to(cr, 10, 16);
1084 cairo_show_text(cr, tooltip->entry);
1085 cairo_destroy(cr);
1086}
1087
1088static cairo_text_extents_t
1089get_text_extents(struct tooltip *tooltip)
1090{
1091 struct window *window;
1092 cairo_t *cr;
1093 cairo_text_extents_t extents;
1094
1095 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1096 * created yet */
1097 window = tooltip->widget->window->parent;
1098 cr = cairo_create(window->cairo_surface);
1099 cairo_text_extents(cr, tooltip->entry, &extents);
1100 cairo_destroy(cr);
1101
1102 return extents;
1103}
1104
1105static int
1106window_create_tooltip(struct tooltip *tooltip)
1107{
1108 struct widget *parent = tooltip->parent;
1109 struct display *display = parent->window->display;
1110 struct window *window;
1111 const int offset_y = 27;
1112 const int margin = 3;
1113 cairo_text_extents_t extents;
1114
1115 if (tooltip->widget)
1116 return 0;
1117
1118 window = window_create_transient(display, parent->window, tooltip->x,
1119 tooltip->y + offset_y,
1120 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1121 if (!window)
1122 return -1;
1123
1124 tooltip->window = window;
1125 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1126
1127 extents = get_text_extents(tooltip);
1128 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1129 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1130
1131 return 0;
1132}
1133
1134void
1135widget_destroy_tooltip(struct widget *parent)
1136{
1137 struct tooltip *tooltip = parent->tooltip;
1138
1139 parent->tooltip_count = 0;
1140 if (!tooltip)
1141 return;
1142
1143 if (tooltip->widget) {
1144 widget_destroy(tooltip->widget);
1145 window_destroy(tooltip->window);
1146 tooltip->widget = NULL;
1147 tooltip->window = NULL;
1148 }
1149
1150 close(tooltip->tooltip_fd);
1151 free(tooltip->entry);
1152 free(tooltip);
1153 parent->tooltip = NULL;
1154}
1155
1156static void
1157tooltip_func(struct task *task, uint32_t events)
1158{
1159 struct tooltip *tooltip =
1160 container_of(task, struct tooltip, tooltip_task);
1161 uint64_t exp;
1162
1163 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1164 window_create_tooltip(tooltip);
1165}
1166
1167#define TOOLTIP_TIMEOUT 500
1168static int
1169tooltip_timer_reset(struct tooltip *tooltip)
1170{
1171 struct itimerspec its;
1172
1173 its.it_interval.tv_sec = 0;
1174 its.it_interval.tv_nsec = 0;
1175 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1176 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1177 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1178 fprintf(stderr, "could not set timerfd\n: %m");
1179 return -1;
1180 }
1181
1182 return 0;
1183}
1184
1185int
1186widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1187{
1188 struct tooltip *tooltip = parent->tooltip;
1189
1190 parent->tooltip_count++;
1191 if (tooltip) {
1192 tooltip->x = x;
1193 tooltip->y = y;
1194 tooltip_timer_reset(tooltip);
1195 return 0;
1196 }
1197
1198 /* the handler might be triggered too fast via input device motion, so
1199 * we need this check here to make sure tooltip is fully initialized */
1200 if (parent->tooltip_count > 1)
1201 return 0;
1202
1203 tooltip = malloc(sizeof *tooltip);
1204 if (!tooltip)
1205 return -1;
1206
1207 parent->tooltip = tooltip;
1208 tooltip->parent = parent;
1209 tooltip->widget = NULL;
1210 tooltip->window = NULL;
1211 tooltip->x = x;
1212 tooltip->y = y;
1213 tooltip->entry = strdup(entry);
1214 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1215 if (tooltip->tooltip_fd < 0) {
1216 fprintf(stderr, "could not create timerfd\n: %m");
1217 return -1;
1218 }
1219
1220 tooltip->tooltip_task.run = tooltip_func;
1221 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1222 EPOLLIN, &tooltip->tooltip_task);
1223 tooltip_timer_reset(tooltip);
1224
1225 return 0;
1226}
1227
1228static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001229frame_resize_handler(struct widget *widget,
1230 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001231{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001232 struct frame *frame = data;
1233 struct widget *child = frame->child;
1234 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001235 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001236 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001237 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001238 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001240 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001241
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001242 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001243 decoration_width = (t->width + t->margin) * 2;
1244 decoration_height = t->width +
1245 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001246
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001247 allocation.x = t->width + t->margin;
1248 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001249 allocation.width = width - decoration_width;
1250 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001251
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001252 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001253
1254 wl_list_for_each(button, &frame->buttons_list, link)
1255 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001256 } else {
1257 decoration_width = 0;
1258 decoration_height = 0;
1259
1260 allocation.x = 0;
1261 allocation.y = 0;
1262 allocation.width = width;
1263 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001264 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001265
1266 wl_list_for_each(button, &frame->buttons_list, link)
1267 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001268 }
1269
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001270 widget_set_allocation(child, allocation.x, allocation.y,
1271 allocation.width, allocation.height);
1272
1273 if (child->resize_handler)
1274 child->resize_handler(child,
1275 allocation.width,
1276 allocation.height,
1277 child->user_data);
1278
Scott Moreauf7e498c2012-05-14 11:39:29 -06001279 width = child->allocation.width + decoration_width;
1280 height = child->allocation.height + decoration_height;
1281
1282 widget->window->input_region =
1283 wl_compositor_create_region(display->compositor);
1284 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001285 t->margin, t->margin,
1286 width - 2 * t->margin,
1287 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001288
1289 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001290
1291 if (child->opaque) {
1292 widget->window->opaque_region =
1293 wl_compositor_create_region(display->compositor);
1294 wl_region_add(widget->window->opaque_region,
1295 opaque_margin, opaque_margin,
1296 widget->allocation.width - 2 * opaque_margin,
1297 widget->allocation.height - 2 * opaque_margin);
1298 }
Martin Minarik1998b152012-05-10 02:04:35 +02001299
1300 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001301 x_r = frame->widget->allocation.width - t->width - t->margin;
1302 x_l = t->width + t->margin;
1303 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001304 wl_list_for_each(button, &frame->buttons_list, link) {
1305 const int button_padding = 4;
1306 w = cairo_image_surface_get_width(button->icon);
1307 h = cairo_image_surface_get_height(button->icon);
1308
1309 if (button->decoration == FRAME_BUTTON_FANCY)
1310 w += 10;
1311
1312 if (button->align == FRAME_BUTTON_LEFT) {
1313 widget_set_allocation(button->widget,
1314 x_l, y , w + 1, h + 1);
1315 x_l += w;
1316 x_l += button_padding;
1317 } else {
1318 x_r -= w;
1319 widget_set_allocation(button->widget,
1320 x_r, y , w + 1, h + 1);
1321 x_r -= button_padding;
1322 }
1323 }
1324}
1325
1326static int
1327frame_button_enter_handler(struct widget *widget,
1328 struct input *input, float x, float y, void *data)
1329{
1330 struct frame_button *frame_button = data;
1331
1332 widget_schedule_redraw(frame_button->widget);
1333 frame_button->state = FRAME_BUTTON_OVER;
1334
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001335 return POINTER_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001336}
1337
1338static void
1339frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1340{
1341 struct frame_button *frame_button = data;
1342
1343 widget_schedule_redraw(frame_button->widget);
1344 frame_button->state = FRAME_BUTTON_DEFAULT;
1345}
1346
1347static void
1348frame_button_button_handler(struct widget *widget,
1349 struct input *input, uint32_t time,
1350 uint32_t button, uint32_t state, void *data)
1351{
1352 struct frame_button *frame_button = data;
1353 struct window *window = widget->window;
1354
1355 if (button != BTN_LEFT)
1356 return;
1357
1358 switch (state) {
1359 case 1:
1360 frame_button->state = FRAME_BUTTON_ACTIVE;
1361 widget_schedule_redraw(frame_button->widget);
1362
1363 if (frame_button->type == FRAME_BUTTON_ICON)
1364 window_show_frame_menu(window, input, time);
1365 return;
1366 case 0:
1367 frame_button->state = FRAME_BUTTON_DEFAULT;
1368 widget_schedule_redraw(frame_button->widget);
1369 break;
1370 }
1371
1372 switch (frame_button->type) {
1373 case FRAME_BUTTON_CLOSE:
1374 if (window->close_handler)
1375 window->close_handler(window->parent,
1376 window->user_data);
1377 else
1378 display_exit(window->display);
1379 break;
1380 case FRAME_BUTTON_MINIMIZE:
1381 fprintf(stderr,"Minimize stub\n");
1382 break;
1383 case FRAME_BUTTON_MAXIMIZE:
1384 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1385 break;
1386 default:
1387 /* Unknown operation */
1388 break;
1389 }
1390}
1391
1392static void
1393frame_button_redraw_handler(struct widget *widget, void *data)
1394{
1395 struct frame_button *frame_button = data;
1396 cairo_t *cr;
1397 int width, height, x, y;
1398 struct window *window = widget->window;
1399
1400 x = widget->allocation.x;
1401 y = widget->allocation.y;
1402 width = widget->allocation.width;
1403 height = widget->allocation.height;
1404
1405 if (!width)
1406 return;
1407 if (!height)
1408 return;
1409 if (widget->opaque)
1410 return;
1411
1412 cr = cairo_create(window->cairo_surface);
1413
1414 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1415 cairo_set_line_width(cr, 1);
1416
1417 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1418 cairo_rectangle (cr, x, y, 25, 16);
1419
1420 cairo_stroke_preserve(cr);
1421
1422 switch (frame_button->state) {
1423 case FRAME_BUTTON_DEFAULT:
1424 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1425 break;
1426 case FRAME_BUTTON_OVER:
1427 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1428 break;
1429 case FRAME_BUTTON_ACTIVE:
1430 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1431 break;
1432 }
1433
1434 cairo_fill (cr);
1435
1436 x += 4;
1437 }
1438
1439 cairo_set_source_surface(cr, frame_button->icon, x, y);
1440 cairo_paint(cr);
1441
1442 cairo_destroy(cr);
1443}
1444
1445static struct widget *
1446frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1447 enum frame_button_align align, enum frame_button_decoration style)
1448{
1449 struct frame_button *frame_button;
1450 const char *icon = data;
1451
1452 frame_button = malloc (sizeof *frame_button);
1453 memset(frame_button, 0, sizeof *frame_button);
1454
1455 frame_button->icon = cairo_image_surface_create_from_png(icon);
1456 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1457 frame_button->frame = frame;
1458 frame_button->type = type;
1459 frame_button->align = align;
1460 frame_button->decoration = style;
1461
1462 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1463
1464 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1465 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1466 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1467 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1468 return frame_button->widget;
1469}
1470
1471static void
1472frame_button_destroy(struct frame_button *frame_button)
1473{
1474 widget_destroy(frame_button->widget);
1475 wl_list_remove(&frame_button->link);
1476 cairo_surface_destroy(frame_button->icon);
1477 free(frame_button);
1478
1479 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001480}
1481
1482static void
1483frame_redraw_handler(struct widget *widget, void *data)
1484{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001485 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001486 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001487 struct theme *t = window->display->theme;
1488 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001489
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001490 if (window->type == TYPE_FULLSCREEN)
1491 return;
1492
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001493 cr = cairo_create(window->cairo_surface);
1494
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001496 flags |= THEME_FRAME_ACTIVE;
1497 theme_render_frame(t, cr, widget->allocation.width,
1498 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499
1500 cairo_destroy(cr);
1501}
1502
1503static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001504frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001505{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001506 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001507 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001508
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001509 location = theme_get_location(t, input->sx, input->sy,
1510 frame->widget->allocation.width,
1511 frame->widget->allocation.height);
1512
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001513 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001514 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001515 return POINTER_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001516 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001517 return POINTER_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001518 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001519 return POINTER_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001521 return POINTER_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001522 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001523 return POINTER_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001525 return POINTER_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001527 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001529 return POINTER_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_EXTERIOR:
1531 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001532 default:
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001533 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001534 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001535}
1536
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001537static void
1538frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001539{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001540 switch (index) {
1541 case 0: /* close */
1542 if (window->close_handler)
1543 window->close_handler(window->parent,
1544 window->user_data);
1545 else
1546 display_exit(window->display);
1547 break;
1548 case 1: /* fullscreen */
1549 /* we don't have a way to get out of fullscreen for now */
1550 window_set_fullscreen(window, 1);
1551 break;
1552 case 2: /* rotate */
1553 case 3: /* scale */
1554 break;
1555 }
1556}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001557
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001558void
1559window_show_frame_menu(struct window *window,
1560 struct input *input, uint32_t time)
1561{
1562 int32_t x, y;
1563
1564 static const char *entries[] = {
1565 "Close", "Fullscreen", "Rotate", "Scale"
1566 };
1567
1568 input_get_position(input, &x, &y);
1569 window_show_menu(window->display, input, time, window,
1570 x - 10, y - 10, frame_menu_func, entries, 4);
1571}
1572
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001573static int
1574frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001575 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001576{
1577 return frame_get_pointer_image_for_location(data, input);
1578}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001579
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001580static int
1581frame_motion_handler(struct widget *widget,
1582 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001583 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584{
1585 return frame_get_pointer_image_for_location(data, input);
1586}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001587
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001588static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001589frame_button_handler(struct widget *widget,
1590 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001591 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001592
1593{
1594 struct frame *frame = data;
1595 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001596 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001597 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001598
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001599 location = theme_get_location(display->theme, input->sx, input->sy,
1600 frame->widget->allocation.width,
1601 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001602
1603 if (window->display->shell && button == BTN_LEFT && state == 1) {
1604 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001605 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001606 if (!window->shell_surface)
1607 break;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001608 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001609 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001611 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001612 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001613 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001614 case THEME_LOCATION_RESIZING_TOP:
1615 case THEME_LOCATION_RESIZING_BOTTOM:
1616 case THEME_LOCATION_RESIZING_LEFT:
1617 case THEME_LOCATION_RESIZING_RIGHT:
1618 case THEME_LOCATION_RESIZING_TOP_LEFT:
1619 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1620 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1621 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001622 if (!window->shell_surface)
1623 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001624 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001625
1626 if (!display->dpy) {
1627 /* If we're using shm, allocate a big
1628 pool to create buffers out of while
1629 we resize. We should probably base
1630 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001631 window->pool =
1632 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001633 }
1634
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001635 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001636 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001637 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001638 break;
1639 }
1640 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001641 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 }
1643}
1644
1645struct widget *
1646frame_create(struct window *window, void *data)
1647{
1648 struct frame *frame;
1649
1650 frame = malloc(sizeof *frame);
1651 memset(frame, 0, sizeof *frame);
1652
1653 frame->widget = window_add_widget(window, frame);
1654 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001655
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001656 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1657 widget_set_resize_handler(frame->widget, frame_resize_handler);
1658 widget_set_enter_handler(frame->widget, frame_enter_handler);
1659 widget_set_motion_handler(frame->widget, frame_motion_handler);
1660 widget_set_button_handler(frame->widget, frame_button_handler);
1661
Martin Minarik1998b152012-05-10 02:04:35 +02001662 /* Create empty list for frame buttons */
1663 wl_list_init(&frame->buttons_list);
1664
1665 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1666 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1667
1668 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1669 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1670
1671 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1672 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1673
1674 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1675 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1676
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001677 window->frame = frame;
1678
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001679 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001680}
1681
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001682static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001683frame_destroy(struct frame *frame)
1684{
Martin Minarik1998b152012-05-10 02:04:35 +02001685 struct frame_button *button, *tmp;
1686
1687 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1688 frame_button_destroy(button);
1689
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001690 /* frame->child must be destroyed by the application */
1691 widget_destroy(frame->widget);
1692 free(frame);
1693}
1694
1695static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001696input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001697 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001698{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001699 struct widget *old, *widget;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001700 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001701
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001702 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703 return;
1704
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001705 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001706 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001707 widget = old;
1708 if (input->grab)
1709 widget = input->grab;
1710 if (widget->leave_handler)
1711 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001712 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001713 }
1714
1715 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001716 widget = focus;
1717 if (input->grab)
1718 widget = input->grab;
1719 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001720 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001721 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001722 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001723
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001724 input_set_pointer_image(input, input->pointer_enter_serial,
1725 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001726 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001727}
1728
1729static void
Daniel Stone37816df2012-05-16 18:45:18 +01001730pointer_handle_motion(void *data, struct wl_pointer *pointer,
1731 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001732{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001733 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001734 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001735 struct widget *widget;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001736 int cursor = POINTER_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001737 float sx = wl_fixed_to_double(sx_w);
1738 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001739
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001740 input->sx = sx;
1741 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001742
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001743 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001744 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001745 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001746 }
1747
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001748 if (input->grab)
1749 widget = input->grab;
1750 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001751 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001752 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001753 cursor = widget->motion_handler(input->focus_widget,
1754 input, time, sx, sy,
1755 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001756
Daniel Stone37816df2012-05-16 18:45:18 +01001757 input_set_pointer_image(input, time, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001758}
1759
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001760void
1761input_grab(struct input *input, struct widget *widget, uint32_t button)
1762{
1763 input->grab = widget;
1764 input->grab_button = button;
1765}
1766
1767void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001768input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001769{
1770 struct widget *widget;
1771
1772 input->grab = NULL;
1773 if (input->pointer_focus) {
1774 widget = widget_find_widget(input->pointer_focus->widget,
1775 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001776 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001777 }
1778}
1779
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001780static void
Daniel Stone37816df2012-05-16 18:45:18 +01001781pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1782 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001783{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001784 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001785 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001786
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001787 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001788 if (input->focus_widget && input->grab == NULL && state)
1789 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001790
Neil Roberts6b28aad2012-01-23 19:11:18 +00001791 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001792 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001793 (*widget->button_handler)(widget,
1794 input, time,
1795 button, state,
1796 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001797
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001798 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001799 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001800}
1801
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001802static void
Daniel Stone37816df2012-05-16 18:45:18 +01001803pointer_handle_axis(void *data, struct wl_pointer *pointer,
Scott Moreau210d0792012-03-22 10:47:01 -06001804 uint32_t time, uint32_t axis, int32_t value)
1805{
1806}
1807
1808static void
Daniel Stone37816df2012-05-16 18:45:18 +01001809keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1810 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001811{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001812 struct input *input = data;
1813 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001814 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001815 uint32_t code, num_syms;
1816 const xkb_keysym_t *syms;
1817 xkb_keysym_t sym;
1818 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001819
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001820 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001821 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001822 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001823 return;
1824
Kristian Høgsberg70163132012-05-08 15:55:39 -04001825 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1826 xkb_state_update_key(d->xkb.state, code,
1827 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001828
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001829 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001830 XKB_STATE_DEPRESSED |
1831 XKB_STATE_LATCHED);
1832 input->modifiers = 0;
1833 if (mask & input->display->xkb.control_mask)
1834 input->modifiers |= MOD_CONTROL_MASK;
1835 if (mask & input->display->xkb.alt_mask)
1836 input->modifiers |= MOD_ALT_MASK;
1837 if (mask & input->display->xkb.shift_mask)
1838 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001839
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001840 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001841 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001842 if (state)
1843 window_set_maximized(window,
1844 window->type != TYPE_MAXIMIZED);
1845 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001846 if (num_syms == 1)
1847 sym = syms[0];
1848 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001849 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001850
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001851 (*window->key_handler)(window, input, time, key,
1852 sym, state, window->user_data);
1853 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001854}
1855
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001856static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001857input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001858{
1859 struct window *window = input->pointer_focus;
1860
1861 if (!window)
1862 return;
1863
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001864 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001865
Pekka Paalanene1207c72011-12-16 12:02:09 +02001866 input->pointer_focus = NULL;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001867 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001868}
1869
1870static void
Daniel Stone37816df2012-05-16 18:45:18 +01001871pointer_handle_enter(void *data, struct wl_pointer *pointer,
1872 uint32_t serial, struct wl_surface *surface,
1873 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001874{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001875 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001876 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001877 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001878 float sx = wl_fixed_to_double(sx_w);
1879 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001880
Martin Minarik1998b152012-05-10 02:04:35 +02001881 if (!surface) {
1882 /* enter event for a window we've just destroyed */
1883 return;
1884 }
1885
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001886 input->display->serial = serial;
1887 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001888 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001889 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001890
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001891 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001892 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001893 window->pool = NULL;
1894 /* Schedule a redraw to free the pool */
1895 window_schedule_redraw(window);
1896 }
1897
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001898 input->sx = sx;
1899 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001900
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001901 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001902 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001903}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001904
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001905static void
Daniel Stone37816df2012-05-16 18:45:18 +01001906pointer_handle_leave(void *data, struct wl_pointer *pointer,
1907 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001908{
1909 struct input *input = data;
1910
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001911 input->display->serial = serial;
1912 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001913}
1914
1915static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001916input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001917{
1918 struct window *window = input->keyboard_focus;
1919
1920 if (!window)
1921 return;
1922
1923 window->keyboard_device = NULL;
1924 if (window->keyboard_focus_handler)
1925 (*window->keyboard_focus_handler)(window, NULL,
1926 window->user_data);
1927
1928 input->keyboard_focus = NULL;
1929}
1930
1931static void
Daniel Stone37816df2012-05-16 18:45:18 +01001932keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1933 uint32_t serial, struct wl_surface *surface,
1934 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001935{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001936 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001937 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001938
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001939 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001940 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001941
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001942 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001943 window->keyboard_device = input;
1944 if (window->keyboard_focus_handler)
1945 (*window->keyboard_focus_handler)(window,
1946 window->keyboard_device,
1947 window->user_data);
1948}
1949
1950static void
Daniel Stone37816df2012-05-16 18:45:18 +01001951keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1952 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001953{
1954 struct input *input = data;
1955
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001956 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001957 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001958}
1959
Daniel Stone37816df2012-05-16 18:45:18 +01001960static const struct wl_pointer_listener pointer_listener = {
1961 pointer_handle_enter,
1962 pointer_handle_leave,
1963 pointer_handle_motion,
1964 pointer_handle_button,
1965 pointer_handle_axis,
1966};
1967
1968static const struct wl_keyboard_listener keyboard_listener = {
1969 keyboard_handle_enter,
1970 keyboard_handle_leave,
1971 keyboard_handle_key,
1972};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001973
1974static void
Daniel Stone37816df2012-05-16 18:45:18 +01001975seat_handle_capabilities(void *data, struct wl_seat *seat,
1976 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001977{
Daniel Stone37816df2012-05-16 18:45:18 +01001978 struct input *input = data;
1979
1980 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1981 input->pointer = wl_seat_get_pointer(seat);
1982 wl_pointer_set_user_data(input->pointer, input);
1983 wl_pointer_add_listener(input->pointer, &pointer_listener,
1984 input);
1985 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1986 wl_pointer_destroy(input->pointer);
1987 input->pointer = NULL;
1988 }
1989
1990 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1991 input->keyboard = wl_seat_get_keyboard(seat);
1992 wl_keyboard_set_user_data(input->keyboard, input);
1993 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
1994 input);
1995 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
1996 wl_keyboard_destroy(input->keyboard);
1997 input->keyboard = NULL;
1998 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001999}
2000
Daniel Stone37816df2012-05-16 18:45:18 +01002001static const struct wl_seat_listener seat_listener = {
2002 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002003};
2004
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002005void
2006input_get_position(struct input *input, int32_t *x, int32_t *y)
2007{
2008 *x = input->sx;
2009 *y = input->sy;
2010}
2011
Daniel Stone37816df2012-05-16 18:45:18 +01002012struct wl_seat *
2013input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002014{
Daniel Stone37816df2012-05-16 18:45:18 +01002015 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002016}
2017
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002018uint32_t
2019input_get_modifiers(struct input *input)
2020{
2021 return input->modifiers;
2022}
2023
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002024struct widget *
2025input_get_focus_widget(struct input *input)
2026{
2027 return input->focus_widget;
2028}
2029
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002030struct data_offer {
2031 struct wl_data_offer *offer;
2032 struct input *input;
2033 struct wl_array types;
2034 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002035
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002036 struct task io_task;
2037 int fd;
2038 data_func_t func;
2039 int32_t x, y;
2040 void *user_data;
2041};
2042
2043static void
2044data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2045{
2046 struct data_offer *offer = data;
2047 char **p;
2048
2049 p = wl_array_add(&offer->types, sizeof *p);
2050 *p = strdup(type);
2051}
2052
2053static const struct wl_data_offer_listener data_offer_listener = {
2054 data_offer_offer,
2055};
2056
2057static void
2058data_offer_destroy(struct data_offer *offer)
2059{
2060 char **p;
2061
2062 offer->refcount--;
2063 if (offer->refcount == 0) {
2064 wl_data_offer_destroy(offer->offer);
2065 for (p = offer->types.data; *p; p++)
2066 free(*p);
2067 wl_array_release(&offer->types);
2068 free(offer);
2069 }
2070}
2071
2072static void
2073data_device_data_offer(void *data,
2074 struct wl_data_device *data_device, uint32_t id)
2075{
2076 struct data_offer *offer;
2077
2078 offer = malloc(sizeof *offer);
2079
2080 wl_array_init(&offer->types);
2081 offer->refcount = 1;
2082 offer->input = data;
2083
2084 /* FIXME: Generate typesafe wrappers for this */
2085 offer->offer = (struct wl_data_offer *)
2086 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2087 id, &wl_data_offer_interface);
2088
2089 wl_data_offer_add_listener(offer->offer,
2090 &data_offer_listener, offer);
2091}
2092
2093static void
2094data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002095 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002096 wl_fixed_t x_w, wl_fixed_t y_w,
2097 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002098{
2099 struct input *input = data;
2100 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002101 float x = wl_fixed_to_double(x_w);
2102 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002103 char **p;
2104
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002105 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002106 input->drag_offer = wl_data_offer_get_user_data(offer);
2107 window = wl_surface_get_user_data(surface);
2108 input->pointer_focus = window;
2109
2110 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2111 *p = NULL;
2112
2113 window = input->pointer_focus;
2114 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002115 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002116 input->drag_offer->types.data,
2117 window->user_data);
2118}
2119
2120static void
2121data_device_leave(void *data, struct wl_data_device *data_device)
2122{
2123 struct input *input = data;
2124
2125 data_offer_destroy(input->drag_offer);
2126 input->drag_offer = NULL;
2127}
2128
2129static void
2130data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002131 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002132{
2133 struct input *input = data;
2134 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002135 float x = wl_fixed_to_double(x_w);
2136 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002137
2138 input->sx = x;
2139 input->sy = y;
2140
2141 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002142 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002143 input->drag_offer->types.data,
2144 window->user_data);
2145}
2146
2147static void
2148data_device_drop(void *data, struct wl_data_device *data_device)
2149{
2150 struct input *input = data;
2151 struct window *window = input->pointer_focus;
2152
2153 if (window->drop_handler)
2154 window->drop_handler(window, input,
2155 input->sx, input->sy, window->user_data);
2156}
2157
2158static void
2159data_device_selection(void *data,
2160 struct wl_data_device *wl_data_device,
2161 struct wl_data_offer *offer)
2162{
2163 struct input *input = data;
2164 char **p;
2165
2166 if (input->selection_offer)
2167 data_offer_destroy(input->selection_offer);
2168
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002169 if (offer) {
2170 input->selection_offer = wl_data_offer_get_user_data(offer);
2171 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2172 *p = NULL;
2173 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002174}
2175
2176static const struct wl_data_device_listener data_device_listener = {
2177 data_device_data_offer,
2178 data_device_enter,
2179 data_device_leave,
2180 data_device_motion,
2181 data_device_drop,
2182 data_device_selection
2183};
2184
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002185void
2186input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2187{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002188 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002189 struct wl_cursor *cursor;
2190 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002192 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002193 return;
2194
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002195 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002196 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002197 return;
2198
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002199 image = cursor->images[0];
2200 buffer = wl_cursor_image_get_buffer(image);
2201 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002202 return;
2203
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002204 input->current_cursor = pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002205 wl_pointer_attach(input->pointer, time, buffer,
2206 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002207}
2208
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002209struct wl_data_device *
2210input_get_data_device(struct input *input)
2211{
2212 return input->data_device;
2213}
2214
2215void
2216input_set_selection(struct input *input,
2217 struct wl_data_source *source, uint32_t time)
2218{
2219 wl_data_device_set_selection(input->data_device, source, time);
2220}
2221
2222void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002223input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002224{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002225 wl_data_offer_accept(input->drag_offer->offer,
2226 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002227}
2228
2229static void
2230offer_io_func(struct task *task, uint32_t events)
2231{
2232 struct data_offer *offer =
2233 container_of(task, struct data_offer, io_task);
2234 unsigned int len;
2235 char buffer[4096];
2236
2237 len = read(offer->fd, buffer, sizeof buffer);
2238 offer->func(buffer, len,
2239 offer->x, offer->y, offer->user_data);
2240
2241 if (len == 0) {
2242 close(offer->fd);
2243 data_offer_destroy(offer);
2244 }
2245}
2246
2247static void
2248data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2249 data_func_t func, void *user_data)
2250{
2251 int p[2];
2252
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002253 if (pipe2(p, O_CLOEXEC) == -1)
2254 return;
2255
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002256 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2257 close(p[1]);
2258
2259 offer->io_task.run = offer_io_func;
2260 offer->fd = p[0];
2261 offer->func = func;
2262 offer->refcount++;
2263 offer->user_data = user_data;
2264
2265 display_watch_fd(offer->input->display,
2266 offer->fd, EPOLLIN, &offer->io_task);
2267}
2268
2269void
2270input_receive_drag_data(struct input *input, const char *mime_type,
2271 data_func_t func, void *data)
2272{
2273 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2274 input->drag_offer->x = input->sx;
2275 input->drag_offer->y = input->sy;
2276}
2277
2278int
2279input_receive_selection_data(struct input *input, const char *mime_type,
2280 data_func_t func, void *data)
2281{
2282 char **p;
2283
2284 if (input->selection_offer == NULL)
2285 return -1;
2286
2287 for (p = input->selection_offer->types.data; *p; p++)
2288 if (strcmp(mime_type, *p) == 0)
2289 break;
2290
2291 if (*p == NULL)
2292 return -1;
2293
2294 data_offer_receive_data(input->selection_offer,
2295 mime_type, func, data);
2296 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002297}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002298
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002299int
2300input_receive_selection_data_to_fd(struct input *input,
2301 const char *mime_type, int fd)
2302{
2303 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2304
2305 return 0;
2306}
2307
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002308void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002309window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002310{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002311 if (!window->shell_surface)
2312 return;
2313
Daniel Stone37816df2012-05-16 18:45:18 +01002314 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002315}
2316
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002317static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002318idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002319{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002320 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002321
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002322 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002323 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002324 widget_set_allocation(widget,
2325 window->pending_allocation.x,
2326 window->pending_allocation.y,
2327 window->pending_allocation.width,
2328 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002329
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002330 if (window->input_region) {
2331 wl_region_destroy(window->input_region);
2332 window->input_region = NULL;
2333 }
2334
2335 if (window->opaque_region) {
2336 wl_region_destroy(window->opaque_region);
2337 window->opaque_region = NULL;
2338 }
2339
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002340 if (widget->resize_handler)
2341 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002342 widget->allocation.width,
2343 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002344 widget->user_data);
2345
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002346 if (window->allocation.width != widget->allocation.width ||
2347 window->allocation.height != widget->allocation.height) {
2348 window->allocation = widget->allocation;
2349 window_schedule_redraw(window);
2350 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002351}
2352
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002353void
2354window_schedule_resize(struct window *window, int width, int height)
2355{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002356 window->pending_allocation.x = 0;
2357 window->pending_allocation.y = 0;
2358 window->pending_allocation.width = width;
2359 window->pending_allocation.height = height;
2360
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002361 window->resize_needed = 1;
2362 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002363}
2364
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002365void
2366widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2367{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002368 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002369}
2370
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002371static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002372handle_ping(void *data, struct wl_shell_surface *shell_surface,
2373 uint32_t serial)
2374{
2375 wl_shell_surface_pong(shell_surface, serial);
2376}
2377
2378static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002379handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002380 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002381{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002382 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002383
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002384 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002385 return;
2386
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002387 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002388 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002389}
2390
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002391static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002392menu_destroy(struct menu *menu)
2393{
2394 widget_destroy(menu->widget);
2395 window_destroy(menu->window);
2396 free(menu);
2397}
2398
2399static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002400handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2401{
2402 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002403 struct menu *menu = window->widget->user_data;
2404
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002405 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002406 * device. Or just use wl_callback. And this really needs to
2407 * be a window vfunc that the menu can set. And we need the
2408 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002409
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002410 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002411 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002412 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002413}
2414
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002415static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002416 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002417 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002418 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002419};
2420
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002421void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002422window_get_allocation(struct window *window,
2423 struct rectangle *allocation)
2424{
2425 *allocation = window->allocation;
2426}
2427
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002428static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002429widget_redraw(struct widget *widget)
2430{
2431 struct widget *child;
2432
2433 if (widget->redraw_handler)
2434 widget->redraw_handler(widget, widget->user_data);
2435 wl_list_for_each(child, &widget->child_list, link)
2436 widget_redraw(child);
2437}
2438
2439static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002440frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2441{
2442 struct window *window = data;
2443
2444 wl_callback_destroy(callback);
2445 window->redraw_scheduled = 0;
2446 if (window->redraw_needed)
2447 window_schedule_redraw(window);
2448}
2449
2450static const struct wl_callback_listener listener = {
2451 frame_callback
2452};
2453
2454static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002455idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002456{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002457 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002458 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002459
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002460 if (window->resize_needed)
2461 idle_resize(window);
2462
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002463 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002464 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002465 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002466 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002467 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002468
2469 callback = wl_surface_frame(window->surface);
2470 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002471}
2472
2473void
2474window_schedule_redraw(struct window *window)
2475{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002476 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002477 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002478 window->redraw_task.run = idle_redraw;
2479 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002480 window->redraw_scheduled = 1;
2481 }
2482}
2483
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002484void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002485window_set_custom(struct window *window)
2486{
2487 window->type = TYPE_CUSTOM;
2488}
2489
2490void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002491window_set_fullscreen(struct window *window, int fullscreen)
2492{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002493 if (!window->display->shell)
2494 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002495
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002496 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002497 return;
2498
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002499 if (fullscreen) {
2500 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002501 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002502 wl_shell_surface_set_fullscreen(window->shell_surface,
2503 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2504 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002505 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002506 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002507 wl_shell_surface_set_toplevel(window->shell_surface);
2508 window_schedule_resize(window,
2509 window->saved_allocation.width,
2510 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002511 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002512}
2513
2514void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002515window_set_maximized(struct window *window, int maximized)
2516{
2517 if (!window->display->shell)
2518 return;
2519
2520 if ((window->type == TYPE_MAXIMIZED) == maximized)
2521 return;
2522
2523 if (window->type == TYPE_TOPLEVEL) {
2524 window->saved_allocation = window->allocation;
2525 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2526 window->type = TYPE_MAXIMIZED;
2527 } else {
2528 wl_shell_surface_set_toplevel(window->shell_surface);
2529 window->type = TYPE_TOPLEVEL;
2530 window_schedule_resize(window,
2531 window->saved_allocation.width,
2532 window->saved_allocation.height);
2533 }
2534}
2535
2536void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002537window_set_user_data(struct window *window, void *data)
2538{
2539 window->user_data = data;
2540}
2541
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002542void *
2543window_get_user_data(struct window *window)
2544{
2545 return window->user_data;
2546}
2547
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002548void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002549window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002550 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002551{
2552 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002553}
2554
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002555void
2556window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002557 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002558{
2559 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002560}
2561
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002562void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002563window_set_data_handler(struct window *window, window_data_handler_t handler)
2564{
2565 window->data_handler = handler;
2566}
2567
2568void
2569window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2570{
2571 window->drop_handler = handler;
2572}
2573
2574void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002575window_set_close_handler(struct window *window,
2576 window_close_handler_t handler)
2577{
2578 window->close_handler = handler;
2579}
2580
2581void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002582window_set_title(struct window *window, const char *title)
2583{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002584 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002585 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002586 if (window->shell_surface)
2587 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002588}
2589
2590const char *
2591window_get_title(struct window *window)
2592{
2593 return window->title;
2594}
2595
2596void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002597window_damage(struct window *window, int32_t x, int32_t y,
2598 int32_t width, int32_t height)
2599{
2600 wl_surface_damage(window->surface, x, y, width, height);
2601}
2602
Casey Dahlin9074db52012-04-19 22:50:09 -04002603static void
2604surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002605 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002606{
Rob Bradford7507b572012-05-15 17:55:34 +01002607 struct window *window = data;
2608 struct output *output;
2609 struct output *output_found = NULL;
2610 struct window_output *window_output;
2611
2612 wl_list_for_each(output, &window->display->output_list, link) {
2613 if (output->output == wl_output) {
2614 output_found = output;
2615 break;
2616 }
2617 }
2618
2619 if (!output_found)
2620 return;
2621
2622 window_output = malloc (sizeof *window_output);
2623 window_output->output = output_found;
2624
2625 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002626}
2627
2628static void
2629surface_leave(void *data,
2630 struct wl_surface *wl_surface, struct wl_output *output)
2631{
Rob Bradford7507b572012-05-15 17:55:34 +01002632 struct window *window = data;
2633 struct window_output *window_output;
2634 struct window_output *window_output_found = NULL;
2635
2636 wl_list_for_each(window_output, &window->window_output_list, link) {
2637 if (window_output->output->output == output) {
2638 window_output_found = window_output;
2639 break;
2640 }
2641 }
2642
2643 if (window_output_found) {
2644 wl_list_remove(&window_output_found->link);
2645 free(window_output_found);
2646 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002647}
2648
2649static const struct wl_surface_listener surface_listener = {
2650 surface_enter,
2651 surface_leave
2652};
2653
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002654static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002655window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002656{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002657 struct window *window;
2658
2659 window = malloc(sizeof *window);
2660 if (window == NULL)
2661 return NULL;
2662
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002663 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002664 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002665 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002666 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002667 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002668 if (display->shell) {
2669 window->shell_surface =
2670 wl_shell_get_shell_surface(display->shell,
2671 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002672 if (window->title)
2673 wl_shell_surface_set_title(window->shell_surface,
2674 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002675 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002676 window->allocation.x = 0;
2677 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002678 window->allocation.width = 0;
2679 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002680 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002681 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002682 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002683 window->input_region = NULL;
2684 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002685
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002686 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002687#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002688 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002689#else
2690 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2691#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002692 else
2693 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002694
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002695 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002696 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002697 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002698
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002699 if (window->shell_surface) {
2700 wl_shell_surface_set_user_data(window->shell_surface, window);
2701 wl_shell_surface_add_listener(window->shell_surface,
2702 &shell_surface_listener, window);
2703 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002704
Rob Bradford7507b572012-05-15 17:55:34 +01002705 wl_list_init (&window->window_output_list);
2706
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002707 return window;
2708}
2709
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002710struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002711window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002712{
2713 struct window *window;
2714
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002715 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002716 if (!window)
2717 return NULL;
2718
2719 return window;
2720}
2721
2722struct window *
2723window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002724 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002725{
2726 struct window *window;
2727
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002728 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002729 if (!window)
2730 return NULL;
2731
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002732 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002733 window->x = x;
2734 window->y = y;
2735
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002736 if (display->shell)
2737 wl_shell_surface_set_transient(window->shell_surface,
2738 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002739 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002740
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002741 return window;
2742}
2743
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002744static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002745menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002746{
2747 int next;
2748
2749 next = (sy - 8) / 20;
2750 if (menu->current != next) {
2751 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002752 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002753 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002754}
2755
2756static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002757menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002758 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002759 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002760{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002761 struct menu *menu = data;
2762
2763 if (widget == menu->widget)
2764 menu_set_item(data, y);
2765
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002766 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002767}
2768
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002769static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002770menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002771 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002772{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002773 struct menu *menu = data;
2774
2775 if (widget == menu->widget)
2776 menu_set_item(data, y);
2777
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002778 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002779}
2780
2781static void
2782menu_leave_handler(struct widget *widget, struct input *input, void *data)
2783{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002784 struct menu *menu = data;
2785
2786 if (widget == menu->widget)
2787 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002788}
2789
2790static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002791menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002792 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002793 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002794
2795{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002796 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002797
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002798 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002799 /* Either relase after press-drag-release or
2800 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002801 menu->func(menu->window->parent,
2802 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002803 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002804 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002805 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002806}
2807
2808static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002809menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002810{
2811 cairo_t *cr;
2812 const int32_t r = 3, margin = 3;
2813 struct menu *menu = data;
2814 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002815 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002816
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002817 cr = cairo_create(window->cairo_surface);
2818 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2819 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2820 cairo_paint(cr);
2821
2822 width = window->allocation.width;
2823 height = window->allocation.height;
2824 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002825
2826 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002827 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2828 cairo_fill(cr);
2829
2830 for (i = 0; i < menu->count; i++) {
2831 if (i == menu->current) {
2832 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2833 cairo_rectangle(cr, margin, i * 20 + margin,
2834 width - 2 * margin, 20);
2835 cairo_fill(cr);
2836 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2837 cairo_move_to(cr, 10, i * 20 + 16);
2838 cairo_show_text(cr, menu->entries[i]);
2839 } else {
2840 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2841 cairo_move_to(cr, 10, i * 20 + 16);
2842 cairo_show_text(cr, menu->entries[i]);
2843 }
2844 }
2845
2846 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002847}
2848
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002849void
2850window_show_menu(struct display *display,
2851 struct input *input, uint32_t time, struct window *parent,
2852 int32_t x, int32_t y,
2853 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002854{
2855 struct window *window;
2856 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002857 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002858
2859 menu = malloc(sizeof *menu);
2860 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002861 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002862
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002863 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002864 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002865 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002866
2867 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002868 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002869 menu->entries = entries;
2870 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002871 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002872 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002873 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002874 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002875 window->type = TYPE_MENU;
2876 window->x = x;
2877 window->y = y;
2878
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002879 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002880 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002881 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002882 window->parent->shell_surface,
2883 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002884
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002885 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002886 widget_set_enter_handler(menu->widget, menu_enter_handler);
2887 widget_set_leave_handler(menu->widget, menu_leave_handler);
2888 widget_set_motion_handler(menu->widget, menu_motion_handler);
2889 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002890
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002891 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002892 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002893}
2894
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002895void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002896window_set_buffer_type(struct window *window, enum window_buffer_type type)
2897{
2898 window->buffer_type = type;
2899}
2900
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002901
2902static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002903display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002904 struct wl_output *wl_output,
2905 int x, int y,
2906 int physical_width,
2907 int physical_height,
2908 int subpixel,
2909 const char *make,
2910 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002911{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002912 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002913
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002914 output->allocation.x = x;
2915 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002916}
2917
2918static void
2919display_handle_mode(void *data,
2920 struct wl_output *wl_output,
2921 uint32_t flags,
2922 int width,
2923 int height,
2924 int refresh)
2925{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002926 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002927 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002928
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002929 if (flags & WL_OUTPUT_MODE_CURRENT) {
2930 output->allocation.width = width;
2931 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002932 if (display->output_configure_handler)
2933 (*display->output_configure_handler)(
2934 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002935 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002936}
2937
2938static const struct wl_output_listener output_listener = {
2939 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002940 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002941};
2942
2943static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002944display_add_output(struct display *d, uint32_t id)
2945{
2946 struct output *output;
2947
2948 output = malloc(sizeof *output);
2949 if (output == NULL)
2950 return;
2951
2952 memset(output, 0, sizeof *output);
2953 output->display = d;
2954 output->output =
2955 wl_display_bind(d->display, id, &wl_output_interface);
2956 wl_list_insert(d->output_list.prev, &output->link);
2957
2958 wl_output_add_listener(output->output, &output_listener, output);
2959}
2960
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002961static void
2962output_destroy(struct output *output)
2963{
2964 if (output->destroy_handler)
2965 (*output->destroy_handler)(output, output->user_data);
2966
2967 wl_output_destroy(output->output);
2968 wl_list_remove(&output->link);
2969 free(output);
2970}
2971
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002972void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002973display_set_output_configure_handler(struct display *display,
2974 display_output_handler_t handler)
2975{
2976 struct output *output;
2977
2978 display->output_configure_handler = handler;
2979 if (!handler)
2980 return;
2981
2982 wl_list_for_each(output, &display->output_list, link)
2983 (*display->output_configure_handler)(output,
2984 display->user_data);
2985}
2986
2987void
2988output_set_user_data(struct output *output, void *data)
2989{
2990 output->user_data = data;
2991}
2992
2993void *
2994output_get_user_data(struct output *output)
2995{
2996 return output->user_data;
2997}
2998
2999void
3000output_set_destroy_handler(struct output *output,
3001 display_output_handler_t handler)
3002{
3003 output->destroy_handler = handler;
3004 /* FIXME: implement this, once we have way to remove outputs */
3005}
3006
3007void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003008output_get_allocation(struct output *output, struct rectangle *allocation)
3009{
3010 *allocation = output->allocation;
3011}
3012
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003013struct wl_output *
3014output_get_wl_output(struct output *output)
3015{
3016 return output->output;
3017}
3018
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003019static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003020display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003021{
3022 struct input *input;
3023
3024 input = malloc(sizeof *input);
3025 if (input == NULL)
3026 return;
3027
3028 memset(input, 0, sizeof *input);
3029 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003030 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003031 input->pointer_focus = NULL;
3032 input->keyboard_focus = NULL;
3033 wl_list_insert(d->input_list.prev, &input->link);
3034
Daniel Stone37816df2012-05-16 18:45:18 +01003035 wl_seat_add_listener(input->seat, &seat_listener, input);
3036 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003037
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003038 input->data_device =
3039 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003040 input->seat);
3041 wl_data_device_add_listener(input->data_device, &data_device_listener,
3042 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003043}
3044
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003045static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003046input_destroy(struct input *input)
3047{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003048 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003049 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003050
3051 if (input->drag_offer)
3052 data_offer_destroy(input->drag_offer);
3053
3054 if (input->selection_offer)
3055 data_offer_destroy(input->selection_offer);
3056
3057 wl_data_device_destroy(input->data_device);
3058 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003059 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003060 free(input);
3061}
3062
3063static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003064display_handle_global(struct wl_display *display, uint32_t id,
3065 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003066{
3067 struct display *d = data;
3068
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003069 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003070 d->compositor =
3071 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003072 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003073 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003074 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003075 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003076 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003077 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003078 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003079 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003080 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3081 d->data_device_manager =
3082 wl_display_bind(display, id,
3083 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003084 }
3085}
3086
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003087static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003088init_xkb(struct display *d)
3089{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003090 d->xkb.names.rules = "evdev";
3091 d->xkb.names.model = "pc105";
3092 d->xkb.names.layout = (char *) option_xkb_layout;
3093 d->xkb.names.variant = (char *) option_xkb_variant;
3094 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003095
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003096 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003097 if (!d->xkb.context) {
3098 fprintf(stderr, "Failed to create XKB context\n");
3099 exit(1);
3100 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003101
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003102 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003103 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003104 fprintf(stderr, "Failed to compile keymap\n");
3105 exit(1);
3106 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003107
3108 d->xkb.state = xkb_state_new(d->xkb.keymap);
3109 if (!d->xkb.state) {
3110 fprintf(stderr, "Failed to create XKB state\n");
3111 exit(1);
3112 }
3113
3114 d->xkb.control_mask =
3115 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3116 d->xkb.alt_mask =
3117 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3118 d->xkb.shift_mask =
3119 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3120
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003121}
3122
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003123static void
3124fini_xkb(struct display *display)
3125{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003126 xkb_state_unref(display->xkb.state);
3127 xkb_map_unref(display->xkb.keymap);
3128 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003129}
3130
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003131#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003132static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003133init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003134{
3135 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003136 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003137
Rob Clark6396ed32012-03-11 19:48:41 -05003138#ifdef USE_CAIRO_GLESV2
3139# define GL_BIT EGL_OPENGL_ES2_BIT
3140#else
3141# define GL_BIT EGL_OPENGL_BIT
3142#endif
3143
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003144 static const EGLint argb_cfg_attribs[] = {
3145 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003146 EGL_RED_SIZE, 1,
3147 EGL_GREEN_SIZE, 1,
3148 EGL_BLUE_SIZE, 1,
3149 EGL_ALPHA_SIZE, 1,
3150 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003151 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003152 EGL_NONE
3153 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003154
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003155#ifdef USE_CAIRO_GLESV2
3156 static const EGLint context_attribs[] = {
3157 EGL_CONTEXT_CLIENT_VERSION, 2,
3158 EGL_NONE
3159 };
3160 EGLint api = EGL_OPENGL_ES_API;
3161#else
3162 EGLint *context_attribs = NULL;
3163 EGLint api = EGL_OPENGL_API;
3164#endif
3165
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003166 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003167 if (!eglInitialize(d->dpy, &major, &minor)) {
3168 fprintf(stderr, "failed to initialize display\n");
3169 return -1;
3170 }
3171
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003172 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003173 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3174 return -1;
3175 }
3176
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003177 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3178 &d->argb_config, 1, &n) || n != 1) {
3179 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003180 return -1;
3181 }
3182
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003183 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003184 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003185 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003186 fprintf(stderr, "failed to create context\n");
3187 return -1;
3188 }
3189
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003190 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003191 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003192 return -1;
3193 }
3194
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003195#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003196 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3197 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3198 fprintf(stderr, "failed to get cairo egl argb device\n");
3199 return -1;
3200 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003201#endif
3202
3203 return 0;
3204}
3205
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003206static void
3207fini_egl(struct display *display)
3208{
3209#ifdef HAVE_CAIRO_EGL
3210 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003211#endif
3212
3213 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3214 EGL_NO_CONTEXT);
3215
3216 eglTerminate(display->dpy);
3217 eglReleaseThread();
3218}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003219#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003220
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003221static int
3222event_mask_update(uint32_t mask, void *data)
3223{
3224 struct display *d = data;
3225
3226 d->mask = mask;
3227
3228 return 0;
3229}
3230
3231static void
3232handle_display_data(struct task *task, uint32_t events)
3233{
3234 struct display *display =
3235 container_of(task, struct display, display_task);
3236
3237 wl_display_iterate(display->display, display->mask);
3238}
3239
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003240struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003241display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003242{
3243 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003244
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003245 argc = parse_options(xkb_options,
3246 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003247
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003248 d = malloc(sizeof *d);
3249 if (d == NULL)
3250 return NULL;
3251
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003252 memset(d, 0, sizeof *d);
3253
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003254 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003255 if (d->display == NULL) {
3256 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003257 return NULL;
3258 }
3259
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003260 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3261 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3262 d->display_task.run = handle_display_data;
3263 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3264
3265 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003266 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003267 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003268
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003269 /* Set up listener so we'll catch all events. */
3270 wl_display_add_global_listener(d->display,
3271 display_handle_global, d);
3272
3273 /* Process connection events. */
3274 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003275#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003276 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003277 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003278#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003279
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003280 d->image_target_texture_2d =
3281 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3282 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3283 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3284
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003285 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003286
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003287 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003288
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003289 wl_list_init(&d->window_list);
3290
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003291 init_xkb(d);
3292
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003293 return d;
3294}
3295
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003296static void
3297display_destroy_outputs(struct display *display)
3298{
3299 struct output *tmp;
3300 struct output *output;
3301
3302 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3303 output_destroy(output);
3304}
3305
Pekka Paalanene1207c72011-12-16 12:02:09 +02003306static void
3307display_destroy_inputs(struct display *display)
3308{
3309 struct input *tmp;
3310 struct input *input;
3311
3312 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3313 input_destroy(input);
3314}
3315
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003316void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003317display_destroy(struct display *display)
3318{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003319 if (!wl_list_empty(&display->window_list))
3320 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3321
3322 if (!wl_list_empty(&display->deferred_list))
3323 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3324
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003325 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003326 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003327
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003328 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003329
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003330 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003331 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003332
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003333#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003334 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003335#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003336
Pekka Paalanenc2052982011-12-16 11:41:32 +02003337 if (display->shell)
3338 wl_shell_destroy(display->shell);
3339
3340 if (display->shm)
3341 wl_shm_destroy(display->shm);
3342
3343 if (display->data_device_manager)
3344 wl_data_device_manager_destroy(display->data_device_manager);
3345
3346 wl_compositor_destroy(display->compositor);
3347
3348 close(display->epoll_fd);
3349
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003350 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003351 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003352 free(display);
3353}
3354
3355void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003356display_set_user_data(struct display *display, void *data)
3357{
3358 display->user_data = data;
3359}
3360
3361void *
3362display_get_user_data(struct display *display)
3363{
3364 return display->user_data;
3365}
3366
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003367struct wl_display *
3368display_get_display(struct display *display)
3369{
3370 return display->display;
3371}
3372
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003373struct output *
3374display_get_output(struct display *display)
3375{
3376 return container_of(display->output_list.next, struct output, link);
3377}
3378
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003379struct wl_compositor *
3380display_get_compositor(struct display *display)
3381{
3382 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003383}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003384
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003385uint32_t
3386display_get_serial(struct display *display)
3387{
3388 return display->serial;
3389}
3390
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003391EGLDisplay
3392display_get_egl_display(struct display *d)
3393{
3394 return d->dpy;
3395}
3396
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003397struct wl_data_source *
3398display_create_data_source(struct display *display)
3399{
3400 return wl_data_device_manager_create_data_source(display->data_device_manager);
3401}
3402
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003403EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003404display_get_argb_egl_config(struct display *d)
3405{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003406 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003407}
3408
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003409struct wl_shell *
3410display_get_shell(struct display *display)
3411{
3412 return display->shell;
3413}
3414
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003415int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003416display_acquire_window_surface(struct display *display,
3417 struct window *window,
3418 EGLContext ctx)
3419{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003420#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003421 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003422 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003423
3424 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003425 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003426 device = cairo_surface_get_device(window->cairo_surface);
3427 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003428 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003429
Benjamin Franzke0c991632011-09-27 21:57:31 +02003430 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003431 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003432 ctx = display->argb_ctx;
3433 else
3434 assert(0);
3435 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003436
3437 data = cairo_surface_get_user_data(window->cairo_surface,
3438 &surface_data_key);
3439
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003440 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003441 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003442 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3443 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003444
3445 return 0;
3446#else
3447 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003448#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003449}
3450
3451void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003452display_release_window_surface(struct display *display,
3453 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003454{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003455#ifdef HAVE_CAIRO_EGL
3456 cairo_device_t *device;
3457
3458 device = cairo_surface_get_device(window->cairo_surface);
3459 if (!device)
3460 return;
3461
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003462 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003463 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003464 cairo_device_release(device);
3465#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003466}
3467
3468void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003469display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003470{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003471 wl_list_insert(&display->deferred_list, &task->link);
3472}
3473
3474void
3475display_watch_fd(struct display *display,
3476 int fd, uint32_t events, struct task *task)
3477{
3478 struct epoll_event ep;
3479
3480 ep.events = events;
3481 ep.data.ptr = task;
3482 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3483}
3484
3485void
3486display_run(struct display *display)
3487{
3488 struct task *task;
3489 struct epoll_event ep[16];
3490 int i, count;
3491
Pekka Paalanen826d7952011-12-15 10:14:07 +02003492 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003493 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003494 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003495
3496 while (!wl_list_empty(&display->deferred_list)) {
3497 task = container_of(display->deferred_list.next,
3498 struct task, link);
3499 wl_list_remove(&task->link);
3500 task->run(task, 0);
3501 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003502
3503 if (!display->running)
3504 break;
3505
3506 wl_display_flush(display->display);
3507
3508 count = epoll_wait(display->epoll_fd,
3509 ep, ARRAY_LENGTH(ep), -1);
3510 for (i = 0; i < count; i++) {
3511 task = ep[i].data.ptr;
3512 task->run(task, ep[i].events);
3513 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003514 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003515}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003516
3517void
3518display_exit(struct display *display)
3519{
3520 display->running = 0;
3521}