blob: 288a5265ea46a31a10423bb530fdf99a90a840ac [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>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040042#include <pixman.h>
43
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040069#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060070#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020071#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030072#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050073
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050074#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050075
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070076struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030077
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040078struct global {
79 uint32_t name;
80 char *interface;
81 uint32_t version;
82 struct wl_list link;
83};
84
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050085struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050086 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040087 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050088 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040089 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040090 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040091 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060092 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020093 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200121
122 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100123
124 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200125
126 uint32_t workspace;
127 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128};
129
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400130enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400131 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400132 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400133 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500134 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500136 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_CUSTOM
138};
Rob Bradford7507b572012-05-15 17:55:34 +0100139
140struct window_output {
141 struct output *output;
142 struct wl_list link;
143};
144
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500145struct window {
146 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500147 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100148 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500149 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200150 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500151 struct wl_region *input_region;
152 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500153 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500154 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400155 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500156 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500157 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400158 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400159 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400160 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400161 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400162 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400163 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400164 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400165 int focus_count;
166
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400167 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500168
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400169 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500170
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700171 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400172
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500173 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500174 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400175 window_data_handler_t data_handler;
176 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500177 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400178 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300180 struct wl_callback *frame_cb;
181
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200182 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500183 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500184
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500185 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400186 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500187};
188
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500189struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500190 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300191 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500192 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400193 struct wl_list link;
194 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500195 widget_resize_handler_t resize_handler;
196 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500197 widget_enter_handler_t enter_handler;
198 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500199 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500200 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200201 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400202 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500203 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300204 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400205};
206
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400207struct input {
208 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100209 struct wl_seat *seat;
210 struct wl_pointer *pointer;
211 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400212 struct window *pointer_focus;
213 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300214 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300215 uint32_t cursor_anim_start;
216 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300217 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400218 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400219 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400220 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400221 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400222 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400223
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500224 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500225 struct widget *grab;
226 uint32_t grab_button;
227
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400228 struct wl_data_device *data_device;
229 struct data_offer *drag_offer;
230 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100231
232 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100233 struct xkb_keymap *keymap;
234 struct xkb_state *state;
235 xkb_mod_mask_t control_mask;
236 xkb_mod_mask_t alt_mask;
237 xkb_mod_mask_t shift_mask;
238 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400239
240 struct task repeat_task;
241 int repeat_timer_fd;
242 uint32_t repeat_sym;
243 uint32_t repeat_key;
244 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400245};
246
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500247struct output {
248 struct display *display;
249 struct wl_output *output;
250 struct rectangle allocation;
251 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600252 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200253
254 display_output_handler_t destroy_handler;
255 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500256};
257
Martin Minarik1998b152012-05-10 02:04:35 +0200258enum frame_button_action {
259 FRAME_BUTTON_NULL = 0,
260 FRAME_BUTTON_ICON = 1,
261 FRAME_BUTTON_CLOSE = 2,
262 FRAME_BUTTON_MINIMIZE = 3,
263 FRAME_BUTTON_MAXIMIZE = 4,
264};
265
266enum frame_button_pointer {
267 FRAME_BUTTON_DEFAULT = 0,
268 FRAME_BUTTON_OVER = 1,
269 FRAME_BUTTON_ACTIVE = 2,
270};
271
272enum frame_button_align {
273 FRAME_BUTTON_RIGHT = 0,
274 FRAME_BUTTON_LEFT = 1,
275};
276
277enum frame_button_decoration {
278 FRAME_BUTTON_NONE = 0,
279 FRAME_BUTTON_FANCY = 1,
280};
281
282struct frame_button {
283 struct widget *widget;
284 struct frame *frame;
285 cairo_surface_t *icon;
286 enum frame_button_action type;
287 enum frame_button_pointer state;
288 struct wl_list link; /* buttons_list */
289 enum frame_button_align align;
290 enum frame_button_decoration decoration;
291};
292
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500293struct frame {
294 struct widget *widget;
295 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200296 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500297};
298
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500299struct menu {
300 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500301 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500302 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500303 const char **entries;
304 uint32_t time;
305 int current;
306 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400307 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500308 menu_func_t func;
309};
310
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300311struct tooltip {
312 struct widget *parent;
313 struct window *window;
314 struct widget *widget;
315 char *entry;
316 struct task tooltip_task;
317 int tooltip_fd;
318 float x, y;
319};
320
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700321struct shm_pool {
322 struct wl_shm_pool *pool;
323 size_t size;
324 size_t used;
325 void *data;
326};
327
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400328enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300329 CURSOR_DEFAULT = 100,
330 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400331};
332
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500333enum window_location {
334 WINDOW_INTERIOR = 0,
335 WINDOW_RESIZING_TOP = 1,
336 WINDOW_RESIZING_BOTTOM = 2,
337 WINDOW_RESIZING_LEFT = 4,
338 WINDOW_RESIZING_TOP_LEFT = 5,
339 WINDOW_RESIZING_BOTTOM_LEFT = 6,
340 WINDOW_RESIZING_RIGHT = 8,
341 WINDOW_RESIZING_TOP_RIGHT = 9,
342 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
343 WINDOW_RESIZING_MASK = 15,
344 WINDOW_EXTERIOR = 16,
345 WINDOW_TITLEBAR = 17,
346 WINDOW_CLIENT_AREA = 18,
347};
348
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400349static const cairo_user_data_key_t surface_data_key;
350struct surface_data {
351 struct wl_buffer *buffer;
352};
353
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500354#define MULT(_d,c,a,t) \
355 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
356
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500357#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100359struct egl_window_surface_data {
360 struct display *display;
361 struct wl_surface *surface;
362 struct wl_egl_window *window;
363 EGLSurface surf;
364};
365
366static void
367egl_window_surface_data_destroy(void *p)
368{
369 struct egl_window_surface_data *data = p;
370 struct display *d = data->display;
371
372 eglDestroySurface(d->dpy, data->surf);
373 wl_egl_window_destroy(data->window);
374 data->surface = NULL;
375
376 free(p);
377}
378
379static cairo_surface_t *
380display_create_egl_window_surface(struct display *display,
381 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400382 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383 struct rectangle *rectangle)
384{
385 cairo_surface_t *cairo_surface;
386 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400387 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200388 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100389
390 data = malloc(sizeof *data);
391 if (data == NULL)
392 return NULL;
393
394 data->display = display;
395 data->surface = surface;
396
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500397 config = display->argb_config;
398 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400399
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400400 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100401 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400402 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100403
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400404 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500405 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100406
Benjamin Franzke0c991632011-09-27 21:57:31 +0200407 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100408 data->surf,
409 rectangle->width,
410 rectangle->height);
411
412 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
413 data, egl_window_surface_data_destroy);
414
415 return cairo_surface;
416}
417
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418#endif
419
420struct wl_buffer *
421display_get_buffer_for_surface(struct display *display,
422 cairo_surface_t *surface)
423{
424 struct surface_data *data;
425
426 data = cairo_surface_get_user_data (surface, &surface_data_key);
427
428 return data->buffer;
429}
430
431struct shm_surface_data {
432 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700433 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400434};
435
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500436static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700437shm_pool_destroy(struct shm_pool *pool);
438
439static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400440shm_surface_data_destroy(void *p)
441{
442 struct shm_surface_data *data = p;
443
444 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700445 if (data->pool)
446 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300447
448 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400449}
450
Kristian Høgsberg16626282012-04-03 11:21:27 -0400451static struct wl_shm_pool *
452make_shm_pool(struct display *display, int size, void **data)
453{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400454 struct wl_shm_pool *pool;
455 int fd;
456
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300457 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400458 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300459 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
460 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400461 return NULL;
462 }
463
464 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400465 if (*data == MAP_FAILED) {
466 fprintf(stderr, "mmap failed: %m\n");
467 close(fd);
468 return NULL;
469 }
470
471 pool = wl_shm_create_pool(display->shm, fd, size);
472
473 close(fd);
474
475 return pool;
476}
477
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700478static struct shm_pool *
479shm_pool_create(struct display *display, size_t size)
480{
481 struct shm_pool *pool = malloc(sizeof *pool);
482
483 if (!pool)
484 return NULL;
485
486 pool->pool = make_shm_pool(display, size, &pool->data);
487 if (!pool->pool) {
488 free(pool);
489 return NULL;
490 }
491
492 pool->size = size;
493 pool->used = 0;
494
495 return pool;
496}
497
498static void *
499shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
500{
501 if (pool->used + size > pool->size)
502 return NULL;
503
504 *offset = pool->used;
505 pool->used += size;
506
507 return (char *) pool->data + *offset;
508}
509
510/* destroy the pool. this does not unmap the memory though */
511static void
512shm_pool_destroy(struct shm_pool *pool)
513{
514 munmap(pool->data, pool->size);
515 wl_shm_pool_destroy(pool->pool);
516 free(pool);
517}
518
519/* Start allocating from the beginning of the pool again */
520static void
521shm_pool_reset(struct shm_pool *pool)
522{
523 pool->used = 0;
524}
525
526static int
527data_length_for_shm_surface(struct rectangle *rect)
528{
529 int stride;
530
531 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
532 rect->width);
533 return stride * rect->height;
534}
535
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500536static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700537display_create_shm_surface_from_pool(struct display *display,
538 struct rectangle *rectangle,
539 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540{
541 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400542 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400543 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700544 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400545 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546
547 data = malloc(sizeof *data);
548 if (data == NULL)
549 return NULL;
550
551 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
552 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700553 length = stride * rectangle->height;
554 data->pool = NULL;
555 map = shm_pool_allocate(pool, length, &offset);
556
557 if (!map) {
558 free(data);
559 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400560 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400562 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563 CAIRO_FORMAT_ARGB32,
564 rectangle->width,
565 rectangle->height,
566 stride);
567
568 cairo_surface_set_user_data (surface, &surface_data_key,
569 data, shm_surface_data_destroy);
570
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400571 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500572 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400573 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500574 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400575
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700576 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400577 rectangle->width,
578 rectangle->height,
579 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400580
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700581 return surface;
582}
583
584static cairo_surface_t *
585display_create_shm_surface(struct display *display,
586 struct rectangle *rectangle, uint32_t flags,
587 struct window *window)
588{
589 struct shm_surface_data *data;
590 struct shm_pool *pool;
591 cairo_surface_t *surface;
592
593 if (window && window->pool) {
594 shm_pool_reset(window->pool);
595 surface = display_create_shm_surface_from_pool(display,
596 rectangle,
597 flags,
598 window->pool);
599 if (surface)
600 return surface;
601 }
602
603 pool = shm_pool_create(display,
604 data_length_for_shm_surface(rectangle));
605 if (!pool)
606 return NULL;
607
608 surface =
609 display_create_shm_surface_from_pool(display, rectangle,
610 flags, pool);
611
612 if (!surface) {
613 shm_pool_destroy(pool);
614 return NULL;
615 }
616
617 /* make sure we destroy the pool when the surface is destroyed */
618 data = cairo_surface_get_user_data(surface, &surface_data_key);
619 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400620
621 return surface;
622}
623
nobled7b87cb02011-02-01 18:51:47 +0000624static int
625check_size(struct rectangle *rect)
626{
627 if (rect->width && rect->height)
628 return 0;
629
630 fprintf(stderr, "tried to create surface of "
631 "width: %d, height: %d\n", rect->width, rect->height);
632 return -1;
633}
634
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635cairo_surface_t *
636display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100637 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400638 struct rectangle *rectangle,
639 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400640{
nobled7b87cb02011-02-01 18:51:47 +0000641 if (check_size(rectangle) < 0)
642 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500643#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300644 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400645 return display_create_egl_window_surface(display,
646 surface,
647 flags,
648 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400650 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400651}
652
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200653/*
654 * The following correspondences between file names and cursors was copied
655 * from: https://bugs.kde.org/attachment.cgi?id=67313
656 */
657
658static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300659 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200660 "sw-resize"
661};
662
663static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300664 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200665 "se-resize"
666};
667
668static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300669 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200670 "s-resize"
671};
672
673static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300674 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200675 "closedhand",
676 "208530c400c041818281048008011002"
677};
678
679static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300680 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200681 "default",
682 "top_left_arrow",
683 "left-arrow"
684};
685
686static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300687 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200688 "w-resize"
689};
690
691static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300692 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200693 "e-resize"
694};
695
696static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300697 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200698 "nw-resize"
699};
700
701static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300702 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200703 "ne-resize"
704};
705
706static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300707 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200708 "n-resize"
709};
710
711static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300712 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200713 "ibeam",
714 "text"
715};
716
717static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300718 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200719 "pointer",
720 "pointing_hand",
721 "e29285e634086352946a0e7090d73106"
722};
723
724static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400725 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200726 "wait",
727 "0426c94ea35c87780ff01dc239897213"
728};
729
730struct cursor_alternatives {
731 const char **names;
732 size_t count;
733};
734
735static const struct cursor_alternatives cursors[] = {
736 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
737 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
738 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
739 {grabbings, ARRAY_LENGTH(grabbings)},
740 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
741 {left_sides, ARRAY_LENGTH(left_sides)},
742 {right_sides, ARRAY_LENGTH(right_sides)},
743 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
744 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
745 {top_sides, ARRAY_LENGTH(top_sides)},
746 {xterms, ARRAY_LENGTH(xterms)},
747 {hand1s, ARRAY_LENGTH(hand1s)},
748 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300749};
750
751static void
752create_cursors(struct display *display)
753{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100754 char *config_file;
755 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200756 unsigned int i, j;
757 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100758 struct config_key shell_keys[] = {
759 { "cursor-theme", CONFIG_KEY_STRING, &theme },
760 };
761 struct config_section cs[] = {
762 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
763 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300764
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100765 config_file = config_file_path("weston.ini");
766 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
767 free(config_file);
768
769 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300770 display->cursors =
771 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
772
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300773 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200774 cursor = NULL;
775 for (j = 0; !cursor && j < cursors[i].count; ++j)
776 cursor = wl_cursor_theme_get_cursor(
777 display->cursor_theme, cursors[i].names[j]);
778
779 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300780 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200781 cursors[i].names[0]);
782
783 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300784 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300785}
786
787static void
788destroy_cursors(struct display *display)
789{
790 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800791 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300792}
793
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300794struct wl_cursor_image *
795display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400796{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200797 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400798
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300799 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400800}
801
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400802static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200803window_get_resize_dx_dy(struct window *window, int *x, int *y)
804{
805 if (window->resize_edges & WINDOW_RESIZING_LEFT)
806 *x = window->server_allocation.width - window->allocation.width;
807 else
808 *x = 0;
809
810 if (window->resize_edges & WINDOW_RESIZING_TOP)
811 *y = window->server_allocation.height -
812 window->allocation.height;
813 else
814 *y = 0;
815
816 window->resize_edges = 0;
817}
818
819static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500820window_attach_surface(struct window *window)
821{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400822 struct display *display = window->display;
823 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000824#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000826#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500827 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400829 if (window->type == TYPE_NONE) {
830 window->type = TYPE_TOPLEVEL;
831 if (display->shell)
832 wl_shell_surface_set_toplevel(window->shell_surface);
833 }
834
Pekka Paalanen512dde82012-10-10 12:49:27 +0300835 if (window->opaque_region) {
836 wl_surface_set_opaque_region(window->surface,
837 window->opaque_region);
838 wl_region_destroy(window->opaque_region);
839 window->opaque_region = NULL;
840 }
841
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300842 if (window->input_region) {
843 wl_surface_set_input_region(window->surface,
844 window->input_region);
845 wl_region_destroy(window->input_region);
846 window->input_region = NULL;
847 }
848
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000850#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
852 data = cairo_surface_get_user_data(window->cairo_surface,
853 &surface_data_key);
854
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100855 cairo_gl_surface_swapbuffers(window->cairo_surface);
856 wl_egl_window_get_attached_size(data->window,
857 &window->server_allocation.width,
858 &window->server_allocation.height);
859 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000860#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100862 buffer =
863 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400864 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100865
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200866 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100867 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400868 wl_surface_damage(window->surface, 0, 0,
869 window->allocation.width,
870 window->allocation.height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300871 wl_surface_commit(window->surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100872 window->server_allocation = window->allocation;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100873 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000874 default:
875 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100876 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500877}
878
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400879int
880window_has_focus(struct window *window)
881{
882 return window->focus_count > 0;
883}
884
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500885void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400886window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500887{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400888 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100889 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500890}
891
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400892void
893window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500895 cairo_surface_reference(surface);
896
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400897 if (window->cairo_surface != NULL)
898 cairo_surface_destroy(window->cairo_surface);
899
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500900 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400901}
902
Benjamin Franzke22d54812011-07-16 19:50:32 +0000903#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904static void
905window_resize_cairo_window_surface(struct window *window)
906{
907 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200908 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100909
910 data = cairo_surface_get_user_data(window->cairo_surface,
911 &surface_data_key);
912
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200913 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100914 wl_egl_window_resize(data->window,
915 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200916 window->allocation.height,
917 x,y);
918
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100919 cairo_gl_surface_set_size(window->cairo_surface,
920 window->allocation.width,
921 window->allocation.height);
922}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000923#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100924
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400925struct display *
926window_get_display(struct window *window)
927{
928 return window->display;
929}
930
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400931void
932window_create_surface(struct window *window)
933{
934 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400935 uint32_t flags = 0;
936
937 if (!window->transparent)
938 flags = SURFACE_OPAQUE;
939
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500941#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
943 if (window->cairo_surface) {
944 window_resize_cairo_window_surface(window);
945 return;
946 }
947 surface = display_create_surface(window->display,
948 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400949 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100950 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400951#endif
952 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400953 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400954 &window->allocation,
955 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800957 default:
958 surface = NULL;
959 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400960 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400961
962 window_set_surface(window, surface);
963 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400964}
965
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200966static void frame_destroy(struct frame *frame);
967
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400968void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500969window_destroy(struct window *window)
970{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200971 struct display *display = window->display;
972 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100973 struct window_output *window_output;
974 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200975
976 if (window->redraw_scheduled)
977 wl_list_remove(&window->redraw_task.link);
978
979 wl_list_for_each(input, &display->input_list, link) {
980 if (input->pointer_focus == window)
981 input->pointer_focus = NULL;
982 if (input->keyboard_focus == window)
983 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500984 if (input->focus_widget &&
985 input->focus_widget->window == window)
986 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200987 }
988
Rob Bradford7507b572012-05-15 17:55:34 +0100989 wl_list_for_each_safe(window_output, window_output_tmp,
990 &window->window_output_list, link) {
991 free (window_output);
992 }
993
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500994 if (window->input_region)
995 wl_region_destroy(window->input_region);
996 if (window->opaque_region)
997 wl_region_destroy(window->opaque_region);
998
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200999 if (window->frame)
1000 frame_destroy(window->frame);
1001
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001002 if (window->shell_surface)
1003 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001004 wl_surface_destroy(window->surface);
1005 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001006
1007 if (window->cairo_surface != NULL)
1008 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001009
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001010 if (window->frame_cb)
1011 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001012 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001013 free(window);
1014}
1015
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001016static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001017widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001018{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001019 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001021 wl_list_for_each(child, &widget->child_list, link) {
1022 target = widget_find_widget(child, x, y);
1023 if (target)
1024 return target;
1025 }
1026
1027 if (widget->allocation.x <= x &&
1028 x < widget->allocation.x + widget->allocation.width &&
1029 widget->allocation.y <= y &&
1030 y < widget->allocation.y + widget->allocation.height) {
1031 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001032 }
1033
1034 return NULL;
1035}
1036
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001037static struct widget *
1038widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001039{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001040 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001042 widget = malloc(sizeof *widget);
1043 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001044 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001045 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001046 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001047 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001048 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001049 widget->tooltip = NULL;
1050 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001051
1052 return widget;
1053}
1054
1055struct widget *
1056window_add_widget(struct window *window, void *data)
1057{
1058 window->widget = widget_create(window, data);
1059 wl_list_init(&window->widget->link);
1060
1061 return window->widget;
1062}
1063
1064struct widget *
1065widget_add_widget(struct widget *parent, void *data)
1066{
1067 struct widget *widget;
1068
1069 widget = widget_create(parent->window, data);
1070 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001072 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073}
1074
1075void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001076widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001078 struct display *display = widget->window->display;
1079 struct input *input;
1080
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001081 if (widget->tooltip) {
1082 free(widget->tooltip);
1083 widget->tooltip = NULL;
1084 }
1085
Pekka Paalanene156fb62012-01-19 13:51:38 +02001086 wl_list_for_each(input, &display->input_list, link) {
1087 if (input->focus_widget == widget)
1088 input->focus_widget = NULL;
1089 }
1090
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001091 wl_list_remove(&widget->link);
1092 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099}
1100
1101void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001102widget_set_size(struct widget *widget, int32_t width, int32_t height)
1103{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001104 widget->allocation.width = width;
1105 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001106}
1107
1108void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001109widget_set_allocation(struct widget *widget,
1110 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001111{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001112 widget->allocation.x = x;
1113 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001114 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001115}
1116
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001117void
1118widget_set_transparent(struct widget *widget, int transparent)
1119{
1120 widget->opaque = !transparent;
1121}
1122
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001123void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001124widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001125{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001126 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001127}
1128
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001129void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001130widget_set_resize_handler(struct widget *widget,
1131 widget_resize_handler_t handler)
1132{
1133 widget->resize_handler = handler;
1134}
1135
1136void
1137widget_set_redraw_handler(struct widget *widget,
1138 widget_redraw_handler_t handler)
1139{
1140 widget->redraw_handler = handler;
1141}
1142
1143void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001144widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001145{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001146 widget->enter_handler = handler;
1147}
1148
1149void
1150widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1151{
1152 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001153}
1154
1155void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001156widget_set_motion_handler(struct widget *widget,
1157 widget_motion_handler_t handler)
1158{
1159 widget->motion_handler = handler;
1160}
1161
1162void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001163widget_set_button_handler(struct widget *widget,
1164 widget_button_handler_t handler)
1165{
1166 widget->button_handler = handler;
1167}
1168
1169void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001170widget_set_axis_handler(struct widget *widget,
1171 widget_axis_handler_t handler)
1172{
1173 widget->axis_handler = handler;
1174}
1175
1176void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001177widget_schedule_redraw(struct widget *widget)
1178{
1179 window_schedule_redraw(widget->window);
1180}
1181
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001182cairo_surface_t *
1183window_get_surface(struct window *window)
1184{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001185 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001186}
1187
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001188struct wl_surface *
1189window_get_wl_surface(struct window *window)
1190{
1191 return window->surface;
1192}
1193
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001194struct wl_shell_surface *
1195window_get_wl_shell_surface(struct window *window)
1196{
1197 return window->shell_surface;
1198}
1199
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001200static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001201tooltip_redraw_handler(struct widget *widget, void *data)
1202{
1203 cairo_t *cr;
1204 const int32_t r = 3;
1205 struct tooltip *tooltip = data;
1206 int32_t width, height;
1207 struct window *window = widget->window;
1208
1209 cr = cairo_create(window->cairo_surface);
1210 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1211 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1212 cairo_paint(cr);
1213
1214 width = window->allocation.width;
1215 height = window->allocation.height;
1216 rounded_rect(cr, 0, 0, width, height, r);
1217
1218 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1219 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1220 cairo_fill(cr);
1221
1222 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1223 cairo_move_to(cr, 10, 16);
1224 cairo_show_text(cr, tooltip->entry);
1225 cairo_destroy(cr);
1226}
1227
1228static cairo_text_extents_t
1229get_text_extents(struct tooltip *tooltip)
1230{
1231 struct window *window;
1232 cairo_t *cr;
1233 cairo_text_extents_t extents;
1234
1235 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1236 * created yet */
1237 window = tooltip->widget->window->parent;
1238 cr = cairo_create(window->cairo_surface);
1239 cairo_text_extents(cr, tooltip->entry, &extents);
1240 cairo_destroy(cr);
1241
1242 return extents;
1243}
1244
1245static int
1246window_create_tooltip(struct tooltip *tooltip)
1247{
1248 struct widget *parent = tooltip->parent;
1249 struct display *display = parent->window->display;
1250 struct window *window;
1251 const int offset_y = 27;
1252 const int margin = 3;
1253 cairo_text_extents_t extents;
1254
1255 if (tooltip->widget)
1256 return 0;
1257
1258 window = window_create_transient(display, parent->window, tooltip->x,
1259 tooltip->y + offset_y,
1260 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1261 if (!window)
1262 return -1;
1263
1264 tooltip->window = window;
1265 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1266
1267 extents = get_text_extents(tooltip);
1268 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1269 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1270
1271 return 0;
1272}
1273
1274void
1275widget_destroy_tooltip(struct widget *parent)
1276{
1277 struct tooltip *tooltip = parent->tooltip;
1278
1279 parent->tooltip_count = 0;
1280 if (!tooltip)
1281 return;
1282
1283 if (tooltip->widget) {
1284 widget_destroy(tooltip->widget);
1285 window_destroy(tooltip->window);
1286 tooltip->widget = NULL;
1287 tooltip->window = NULL;
1288 }
1289
1290 close(tooltip->tooltip_fd);
1291 free(tooltip->entry);
1292 free(tooltip);
1293 parent->tooltip = NULL;
1294}
1295
1296static void
1297tooltip_func(struct task *task, uint32_t events)
1298{
1299 struct tooltip *tooltip =
1300 container_of(task, struct tooltip, tooltip_task);
1301 uint64_t exp;
1302
Martin Olsson8df662a2012-07-08 03:03:47 +02001303 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1304 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001305 window_create_tooltip(tooltip);
1306}
1307
1308#define TOOLTIP_TIMEOUT 500
1309static int
1310tooltip_timer_reset(struct tooltip *tooltip)
1311{
1312 struct itimerspec its;
1313
1314 its.it_interval.tv_sec = 0;
1315 its.it_interval.tv_nsec = 0;
1316 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1317 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1318 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1319 fprintf(stderr, "could not set timerfd\n: %m");
1320 return -1;
1321 }
1322
1323 return 0;
1324}
1325
1326int
1327widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1328{
1329 struct tooltip *tooltip = parent->tooltip;
1330
1331 parent->tooltip_count++;
1332 if (tooltip) {
1333 tooltip->x = x;
1334 tooltip->y = y;
1335 tooltip_timer_reset(tooltip);
1336 return 0;
1337 }
1338
1339 /* the handler might be triggered too fast via input device motion, so
1340 * we need this check here to make sure tooltip is fully initialized */
1341 if (parent->tooltip_count > 1)
1342 return 0;
1343
1344 tooltip = malloc(sizeof *tooltip);
1345 if (!tooltip)
1346 return -1;
1347
1348 parent->tooltip = tooltip;
1349 tooltip->parent = parent;
1350 tooltip->widget = NULL;
1351 tooltip->window = NULL;
1352 tooltip->x = x;
1353 tooltip->y = y;
1354 tooltip->entry = strdup(entry);
1355 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1356 if (tooltip->tooltip_fd < 0) {
1357 fprintf(stderr, "could not create timerfd\n: %m");
1358 return -1;
1359 }
1360
1361 tooltip->tooltip_task.run = tooltip_func;
1362 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1363 EPOLLIN, &tooltip->tooltip_task);
1364 tooltip_timer_reset(tooltip);
1365
1366 return 0;
1367}
1368
1369static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001370workspace_manager_state(void *data,
1371 struct workspace_manager *workspace_manager,
1372 uint32_t current,
1373 uint32_t count)
1374{
1375 struct display *display = data;
1376
1377 display->workspace = current;
1378 display->workspace_count = count;
1379}
1380
1381static const struct workspace_manager_listener workspace_manager_listener = {
1382 workspace_manager_state
1383};
1384
1385static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001386frame_resize_handler(struct widget *widget,
1387 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001388{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001389 struct frame *frame = data;
1390 struct widget *child = frame->child;
1391 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001392 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001393 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001394 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001395 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001396 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001397 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001398
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001399 switch (widget->window->type) {
1400 case TYPE_FULLSCREEN:
1401 decoration_width = 0;
1402 decoration_height = 0;
1403
1404 allocation.x = 0;
1405 allocation.y = 0;
1406 allocation.width = width;
1407 allocation.height = height;
1408 opaque_margin = 0;
1409
1410 wl_list_for_each(button, &frame->buttons_list, link)
1411 button->widget->opaque = 1;
1412 break;
1413 case TYPE_MAXIMIZED:
1414 decoration_width = t->width * 2;
1415 decoration_height = t->width + t->titlebar_height;
1416
1417 allocation.x = t->width;
1418 allocation.y = t->titlebar_height;
1419 allocation.width = width - decoration_width;
1420 allocation.height = height - decoration_height;
1421
1422 opaque_margin = 0;
1423
1424 wl_list_for_each(button, &frame->buttons_list, link)
1425 button->widget->opaque = 0;
1426 break;
1427 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001428 decoration_width = (t->width + t->margin) * 2;
1429 decoration_height = t->width +
1430 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001431
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001432 allocation.x = t->width + t->margin;
1433 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001434 allocation.width = width - decoration_width;
1435 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001436
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001437 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001438
1439 wl_list_for_each(button, &frame->buttons_list, link)
1440 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001441 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001442 }
1443
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001444 widget_set_allocation(child, allocation.x, allocation.y,
1445 allocation.width, allocation.height);
1446
1447 if (child->resize_handler)
1448 child->resize_handler(child,
1449 allocation.width,
1450 allocation.height,
1451 child->user_data);
1452
Scott Moreauf7e498c2012-05-14 11:39:29 -06001453 width = child->allocation.width + decoration_width;
1454 height = child->allocation.height + decoration_height;
1455
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001456 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1457
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001458 widget->window->input_region =
1459 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001460 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001461 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001462 shadow_margin, shadow_margin,
1463 width - 2 * shadow_margin,
1464 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001465 } else {
1466 wl_region_add(widget->window->input_region,
1467 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001468 }
1469
Scott Moreauf7e498c2012-05-14 11:39:29 -06001470 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001471
1472 if (child->opaque) {
1473 widget->window->opaque_region =
1474 wl_compositor_create_region(display->compositor);
1475 wl_region_add(widget->window->opaque_region,
1476 opaque_margin, opaque_margin,
1477 widget->allocation.width - 2 * opaque_margin,
1478 widget->allocation.height - 2 * opaque_margin);
1479 }
Martin Minarik1998b152012-05-10 02:04:35 +02001480
1481 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001482 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1483 x_l = t->width + shadow_margin;
1484 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001485 wl_list_for_each(button, &frame->buttons_list, link) {
1486 const int button_padding = 4;
1487 w = cairo_image_surface_get_width(button->icon);
1488 h = cairo_image_surface_get_height(button->icon);
1489
1490 if (button->decoration == FRAME_BUTTON_FANCY)
1491 w += 10;
1492
1493 if (button->align == FRAME_BUTTON_LEFT) {
1494 widget_set_allocation(button->widget,
1495 x_l, y , w + 1, h + 1);
1496 x_l += w;
1497 x_l += button_padding;
1498 } else {
1499 x_r -= w;
1500 widget_set_allocation(button->widget,
1501 x_r, y , w + 1, h + 1);
1502 x_r -= button_padding;
1503 }
1504 }
1505}
1506
1507static int
1508frame_button_enter_handler(struct widget *widget,
1509 struct input *input, float x, float y, void *data)
1510{
1511 struct frame_button *frame_button = data;
1512
1513 widget_schedule_redraw(frame_button->widget);
1514 frame_button->state = FRAME_BUTTON_OVER;
1515
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001516 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001517}
1518
1519static void
1520frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1521{
1522 struct frame_button *frame_button = data;
1523
1524 widget_schedule_redraw(frame_button->widget);
1525 frame_button->state = FRAME_BUTTON_DEFAULT;
1526}
1527
1528static void
1529frame_button_button_handler(struct widget *widget,
1530 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001531 uint32_t button,
1532 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001533{
1534 struct frame_button *frame_button = data;
1535 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001536 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001537
1538 if (button != BTN_LEFT)
1539 return;
1540
1541 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001542 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001543 frame_button->state = FRAME_BUTTON_ACTIVE;
1544 widget_schedule_redraw(frame_button->widget);
1545
1546 if (frame_button->type == FRAME_BUTTON_ICON)
1547 window_show_frame_menu(window, input, time);
1548 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001549 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001550 frame_button->state = FRAME_BUTTON_DEFAULT;
1551 widget_schedule_redraw(frame_button->widget);
1552 break;
1553 }
1554
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001555 if (!was_pressed)
1556 return;
1557
Martin Minarik1998b152012-05-10 02:04:35 +02001558 switch (frame_button->type) {
1559 case FRAME_BUTTON_CLOSE:
1560 if (window->close_handler)
1561 window->close_handler(window->parent,
1562 window->user_data);
1563 else
1564 display_exit(window->display);
1565 break;
1566 case FRAME_BUTTON_MINIMIZE:
1567 fprintf(stderr,"Minimize stub\n");
1568 break;
1569 case FRAME_BUTTON_MAXIMIZE:
1570 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1571 break;
1572 default:
1573 /* Unknown operation */
1574 break;
1575 }
1576}
1577
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001578static int
1579frame_button_motion_handler(struct widget *widget,
1580 struct input *input, uint32_t time,
1581 float x, float y, void *data)
1582{
1583 struct frame_button *frame_button = data;
1584 enum frame_button_pointer previous_button_state = frame_button->state;
1585
1586 /* only track state for a pressed button */
1587 if (input->grab != widget)
1588 return CURSOR_LEFT_PTR;
1589
1590 if (x > widget->allocation.x &&
1591 x < (widget->allocation.x + widget->allocation.width) &&
1592 y > widget->allocation.y &&
1593 y < (widget->allocation.y + widget->allocation.height)) {
1594 frame_button->state = FRAME_BUTTON_ACTIVE;
1595 } else {
1596 frame_button->state = FRAME_BUTTON_DEFAULT;
1597 }
1598
1599 if (frame_button->state != previous_button_state)
1600 widget_schedule_redraw(frame_button->widget);
1601
1602 return CURSOR_LEFT_PTR;
1603}
1604
Martin Minarik1998b152012-05-10 02:04:35 +02001605static void
1606frame_button_redraw_handler(struct widget *widget, void *data)
1607{
1608 struct frame_button *frame_button = data;
1609 cairo_t *cr;
1610 int width, height, x, y;
1611 struct window *window = widget->window;
1612
1613 x = widget->allocation.x;
1614 y = widget->allocation.y;
1615 width = widget->allocation.width;
1616 height = widget->allocation.height;
1617
1618 if (!width)
1619 return;
1620 if (!height)
1621 return;
1622 if (widget->opaque)
1623 return;
1624
1625 cr = cairo_create(window->cairo_surface);
1626
1627 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1628 cairo_set_line_width(cr, 1);
1629
1630 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1631 cairo_rectangle (cr, x, y, 25, 16);
1632
1633 cairo_stroke_preserve(cr);
1634
1635 switch (frame_button->state) {
1636 case FRAME_BUTTON_DEFAULT:
1637 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1638 break;
1639 case FRAME_BUTTON_OVER:
1640 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1641 break;
1642 case FRAME_BUTTON_ACTIVE:
1643 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1644 break;
1645 }
1646
1647 cairo_fill (cr);
1648
1649 x += 4;
1650 }
1651
1652 cairo_set_source_surface(cr, frame_button->icon, x, y);
1653 cairo_paint(cr);
1654
1655 cairo_destroy(cr);
1656}
1657
1658static struct widget *
1659frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1660 enum frame_button_align align, enum frame_button_decoration style)
1661{
1662 struct frame_button *frame_button;
1663 const char *icon = data;
1664
1665 frame_button = malloc (sizeof *frame_button);
1666 memset(frame_button, 0, sizeof *frame_button);
1667
1668 frame_button->icon = cairo_image_surface_create_from_png(icon);
1669 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1670 frame_button->frame = frame;
1671 frame_button->type = type;
1672 frame_button->align = align;
1673 frame_button->decoration = style;
1674
1675 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1676
1677 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1678 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1679 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1680 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001681 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001682 return frame_button->widget;
1683}
1684
1685static void
1686frame_button_destroy(struct frame_button *frame_button)
1687{
1688 widget_destroy(frame_button->widget);
1689 wl_list_remove(&frame_button->link);
1690 cairo_surface_destroy(frame_button->icon);
1691 free(frame_button);
1692
1693 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001694}
1695
1696static void
1697frame_redraw_handler(struct widget *widget, void *data)
1698{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001699 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001700 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001701 struct theme *t = window->display->theme;
1702 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001703
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001704 if (window->type == TYPE_FULLSCREEN)
1705 return;
1706
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001707 cr = cairo_create(window->cairo_surface);
1708
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001709 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001710 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001711 if (window->type == TYPE_MAXIMIZED)
1712 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001713 theme_render_frame(t, cr, widget->allocation.width,
1714 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001715
1716 cairo_destroy(cr);
1717}
1718
1719static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001720frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001721{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001722 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001723 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001724 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001725
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001726 location = theme_get_location(t, input->sx, input->sy,
1727 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001728 frame->widget->allocation.height,
1729 window->type == TYPE_MAXIMIZED ?
1730 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001731
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001732 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001733 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001734 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001735 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001736 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001737 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001738 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001739 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001740 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001741 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001742 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001743 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001744 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001745 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001746 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001747 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001748 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001749 case THEME_LOCATION_EXTERIOR:
1750 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001751 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001752 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001753 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001754}
1755
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001756static void
1757frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001758{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001759 struct display *display;
1760
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001761 switch (index) {
1762 case 0: /* close */
1763 if (window->close_handler)
1764 window->close_handler(window->parent,
1765 window->user_data);
1766 else
1767 display_exit(window->display);
1768 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001769 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001770 display = window->display;
1771 if (display->workspace > 0)
1772 workspace_manager_move_surface(display->workspace_manager,
1773 window->surface,
1774 display->workspace - 1);
1775 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001776 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001777 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001778 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001779 workspace_manager_move_surface(display->workspace_manager,
1780 window->surface,
1781 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05001782 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001783 case 3: /* fullscreen */
1784 /* we don't have a way to get out of fullscreen for now */
1785 if (window->fullscreen_handler)
1786 window->fullscreen_handler(window, window->user_data);
1787 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001788 }
1789}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001790
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001791void
1792window_show_frame_menu(struct window *window,
1793 struct input *input, uint32_t time)
1794{
1795 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001796 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001797
1798 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001799 "Close",
1800 "Move to workspace above", "Move to workspace below",
1801 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001802 };
1803
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001804 if (window->fullscreen_handler)
1805 count = ARRAY_LENGTH(entries);
1806 else
1807 count = ARRAY_LENGTH(entries) - 1;
1808
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001809 input_get_position(input, &x, &y);
1810 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001811 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001812}
1813
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001814static int
1815frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001816 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001817{
1818 return frame_get_pointer_image_for_location(data, input);
1819}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001820
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001821static int
1822frame_motion_handler(struct widget *widget,
1823 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001824 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001825{
1826 return frame_get_pointer_image_for_location(data, input);
1827}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001828
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001829static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001830frame_button_handler(struct widget *widget,
1831 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001832 uint32_t button, enum wl_pointer_button_state state,
1833 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001834
1835{
1836 struct frame *frame = data;
1837 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001838 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001839 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001840
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001841 location = theme_get_location(display->theme, input->sx, input->sy,
1842 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001843 frame->widget->allocation.height,
1844 window->type == TYPE_MAXIMIZED ?
1845 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001846
Daniel Stone4dbadb12012-05-30 16:31:51 +01001847 if (window->display->shell && button == BTN_LEFT &&
1848 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001849 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001850 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001851 if (!window->shell_surface)
1852 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001853 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001854 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001855 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001856 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001857 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001858 case THEME_LOCATION_RESIZING_TOP:
1859 case THEME_LOCATION_RESIZING_BOTTOM:
1860 case THEME_LOCATION_RESIZING_LEFT:
1861 case THEME_LOCATION_RESIZING_RIGHT:
1862 case THEME_LOCATION_RESIZING_TOP_LEFT:
1863 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1864 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1865 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001866 if (!window->shell_surface)
1867 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001868 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001869
1870 if (!display->dpy) {
1871 /* If we're using shm, allocate a big
1872 pool to create buffers out of while
1873 we resize. We should probably base
1874 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001875 window->pool =
1876 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001877 }
1878
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001879 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001880 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001881 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001882 break;
1883 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001884 } else if (button == BTN_RIGHT &&
1885 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001886 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001887 }
1888}
1889
1890struct widget *
1891frame_create(struct window *window, void *data)
1892{
1893 struct frame *frame;
1894
1895 frame = malloc(sizeof *frame);
1896 memset(frame, 0, sizeof *frame);
1897
1898 frame->widget = window_add_widget(window, frame);
1899 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001900
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001901 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1902 widget_set_resize_handler(frame->widget, frame_resize_handler);
1903 widget_set_enter_handler(frame->widget, frame_enter_handler);
1904 widget_set_motion_handler(frame->widget, frame_motion_handler);
1905 widget_set_button_handler(frame->widget, frame_button_handler);
1906
Martin Minarik1998b152012-05-10 02:04:35 +02001907 /* Create empty list for frame buttons */
1908 wl_list_init(&frame->buttons_list);
1909
1910 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1911 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1912
1913 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1914 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1915
1916 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1917 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1918
1919 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1920 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1921
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001922 window->frame = frame;
1923
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001924 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001925}
1926
Kristian Høgsberga1627922012-06-20 17:30:03 -04001927void
1928frame_set_child_size(struct widget *widget, int child_width, int child_height)
1929{
1930 struct display *display = widget->window->display;
1931 struct theme *t = display->theme;
1932 int decoration_width, decoration_height;
1933 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001934 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001935
1936 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001937 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001938 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001939 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001940
1941 width = child_width + decoration_width;
1942 height = child_height + decoration_height;
1943 } else {
1944 width = child_width;
1945 height = child_height;
1946 }
1947
1948 window_schedule_resize(widget->window, width, height);
1949}
1950
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001951static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001952frame_destroy(struct frame *frame)
1953{
Martin Minarik1998b152012-05-10 02:04:35 +02001954 struct frame_button *button, *tmp;
1955
1956 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1957 frame_button_destroy(button);
1958
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001959 /* frame->child must be destroyed by the application */
1960 widget_destroy(frame->widget);
1961 free(frame);
1962}
1963
1964static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001965input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001966 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001967{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001968 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001969 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001970
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001971 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001972 return;
1973
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001974 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001975 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001976 widget = old;
1977 if (input->grab)
1978 widget = input->grab;
1979 if (widget->leave_handler)
1980 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001981 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001982 }
1983
1984 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001985 widget = focus;
1986 if (input->grab)
1987 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001988 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001989 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001990 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001991 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001992
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001993 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001994 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001995}
1996
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001997void
1998input_grab(struct input *input, struct widget *widget, uint32_t button)
1999{
2000 input->grab = widget;
2001 input->grab_button = button;
2002}
2003
2004void
2005input_ungrab(struct input *input)
2006{
2007 struct widget *widget;
2008
2009 input->grab = NULL;
2010 if (input->pointer_focus) {
2011 widget = widget_find_widget(input->pointer_focus->widget,
2012 input->sx, input->sy);
2013 input_set_focus_widget(input, widget, input->sx, input->sy);
2014 }
2015}
2016
2017static void
2018input_remove_pointer_focus(struct input *input)
2019{
2020 struct window *window = input->pointer_focus;
2021
2022 if (!window)
2023 return;
2024
2025 input_set_focus_widget(input, NULL, 0, 0);
2026
2027 input->pointer_focus = NULL;
2028 input->current_cursor = CURSOR_UNSET;
2029}
2030
2031static void
2032pointer_handle_enter(void *data, struct wl_pointer *pointer,
2033 uint32_t serial, struct wl_surface *surface,
2034 wl_fixed_t sx_w, wl_fixed_t sy_w)
2035{
2036 struct input *input = data;
2037 struct window *window;
2038 struct widget *widget;
2039 float sx = wl_fixed_to_double(sx_w);
2040 float sy = wl_fixed_to_double(sy_w);
2041
2042 if (!surface) {
2043 /* enter event for a window we've just destroyed */
2044 return;
2045 }
2046
2047 input->display->serial = serial;
2048 input->pointer_enter_serial = serial;
2049 input->pointer_focus = wl_surface_get_user_data(surface);
2050 window = input->pointer_focus;
2051
2052 if (window->pool) {
2053 shm_pool_destroy(window->pool);
2054 window->pool = NULL;
2055 /* Schedule a redraw to free the pool */
2056 window_schedule_redraw(window);
2057 }
2058
2059 input->sx = sx;
2060 input->sy = sy;
2061
2062 widget = widget_find_widget(window->widget, sx, sy);
2063 input_set_focus_widget(input, widget, sx, sy);
2064}
2065
2066static void
2067pointer_handle_leave(void *data, struct wl_pointer *pointer,
2068 uint32_t serial, struct wl_surface *surface)
2069{
2070 struct input *input = data;
2071
2072 input->display->serial = serial;
2073 input_remove_pointer_focus(input);
2074}
2075
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002076static void
Daniel Stone37816df2012-05-16 18:45:18 +01002077pointer_handle_motion(void *data, struct wl_pointer *pointer,
2078 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002079{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002080 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002081 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002082 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002083 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002084 float sx = wl_fixed_to_double(sx_w);
2085 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002086
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002087 input->sx = sx;
2088 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002089
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002090 if (!window)
2091 return;
2092
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002093 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002094 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002095 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002096 }
2097
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002098 if (input->grab)
2099 widget = input->grab;
2100 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002101 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002102 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002103 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002104 input, time, sx, sy,
2105 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002106
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002107 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002108}
2109
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002110static void
Daniel Stone37816df2012-05-16 18:45:18 +01002111pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002112 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002113{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002114 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002115 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002116 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002117
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002118 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002119 if (input->focus_widget && input->grab == NULL &&
2120 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002121 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002122
Neil Roberts6b28aad2012-01-23 19:11:18 +00002123 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002124 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002125 (*widget->button_handler)(widget,
2126 input, time,
2127 button, state,
2128 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002129
Daniel Stone4dbadb12012-05-30 16:31:51 +01002130 if (input->grab && input->grab_button == button &&
2131 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002132 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002133}
2134
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002135static void
Daniel Stone37816df2012-05-16 18:45:18 +01002136pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002137 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002138{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002139 struct input *input = data;
2140 struct widget *widget;
2141
2142 widget = input->focus_widget;
2143 if (input->grab)
2144 widget = input->grab;
2145 if (widget && widget->axis_handler)
2146 (*widget->axis_handler)(widget,
2147 input, time,
2148 axis, value,
2149 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002150}
2151
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002152static const struct wl_pointer_listener pointer_listener = {
2153 pointer_handle_enter,
2154 pointer_handle_leave,
2155 pointer_handle_motion,
2156 pointer_handle_button,
2157 pointer_handle_axis,
2158};
2159
2160static void
2161input_remove_keyboard_focus(struct input *input)
2162{
2163 struct window *window = input->keyboard_focus;
2164 struct itimerspec its;
2165
2166 its.it_interval.tv_sec = 0;
2167 its.it_interval.tv_nsec = 0;
2168 its.it_value.tv_sec = 0;
2169 its.it_value.tv_nsec = 0;
2170 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2171
2172 if (!window)
2173 return;
2174
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002175 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002176 if (window->keyboard_focus_handler)
2177 (*window->keyboard_focus_handler)(window, NULL,
2178 window->user_data);
2179
2180 input->keyboard_focus = NULL;
2181}
2182
2183static void
2184keyboard_repeat_func(struct task *task, uint32_t events)
2185{
2186 struct input *input =
2187 container_of(task, struct input, repeat_task);
2188 struct window *window = input->keyboard_focus;
2189 uint64_t exp;
2190
2191 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2192 /* If we change the timer between the fd becoming
2193 * readable and getting here, there'll be nothing to
2194 * read and we get EAGAIN. */
2195 return;
2196
2197 if (window && window->key_handler) {
2198 (*window->key_handler)(window, input, input->repeat_time,
2199 input->repeat_key, input->repeat_sym,
2200 WL_KEYBOARD_KEY_STATE_PRESSED,
2201 window->user_data);
2202 }
2203}
2204
2205static void
2206keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2207 uint32_t format, int fd, uint32_t size)
2208{
2209 struct input *input = data;
2210 char *map_str;
2211
2212 if (!data) {
2213 close(fd);
2214 return;
2215 }
2216
2217 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2218 close(fd);
2219 return;
2220 }
2221
2222 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2223 if (map_str == MAP_FAILED) {
2224 close(fd);
2225 return;
2226 }
2227
2228 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2229 map_str,
2230 XKB_KEYMAP_FORMAT_TEXT_V1,
2231 0);
2232 munmap(map_str, size);
2233 close(fd);
2234
2235 if (!input->xkb.keymap) {
2236 fprintf(stderr, "failed to compile keymap\n");
2237 return;
2238 }
2239
2240 input->xkb.state = xkb_state_new(input->xkb.keymap);
2241 if (!input->xkb.state) {
2242 fprintf(stderr, "failed to create XKB state\n");
2243 xkb_map_unref(input->xkb.keymap);
2244 input->xkb.keymap = NULL;
2245 return;
2246 }
2247
2248 input->xkb.control_mask =
2249 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2250 input->xkb.alt_mask =
2251 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2252 input->xkb.shift_mask =
2253 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2254}
2255
2256static void
2257keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2258 uint32_t serial, struct wl_surface *surface,
2259 struct wl_array *keys)
2260{
2261 struct input *input = data;
2262 struct window *window;
2263
2264 input->display->serial = serial;
2265 input->keyboard_focus = wl_surface_get_user_data(surface);
2266
2267 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002268 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002269 if (window->keyboard_focus_handler)
2270 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002271 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002272}
2273
2274static void
2275keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2276 uint32_t serial, struct wl_surface *surface)
2277{
2278 struct input *input = data;
2279
2280 input->display->serial = serial;
2281 input_remove_keyboard_focus(input);
2282}
2283
Scott Moreau210d0792012-03-22 10:47:01 -06002284static void
Daniel Stone37816df2012-05-16 18:45:18 +01002285keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002286 uint32_t serial, uint32_t time, uint32_t key,
2287 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002288{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002289 struct input *input = data;
2290 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002291 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002292 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002293 const xkb_keysym_t *syms;
2294 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002295 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002296
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002297 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002298 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002299 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002300 return;
2301
Daniel Stone97f68542012-05-30 16:32:01 +01002302 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002303
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002304 sym = XKB_KEY_NoSymbol;
2305 if (num_syms == 1)
2306 sym = syms[0];
2307
2308 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002309 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002310 window_set_maximized(window,
2311 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002312 } else if (sym == XKB_KEY_F11 &&
2313 window->fullscreen_handler &&
2314 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2315 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002316 } else if (sym == XKB_KEY_F4 &&
2317 input->modifiers == MOD_ALT_MASK &&
2318 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2319 if (window->close_handler)
2320 window->close_handler(window->parent,
2321 window->user_data);
2322 else
2323 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002324 } else if (window->key_handler) {
2325 (*window->key_handler)(window, input, time, key,
2326 sym, state, window->user_data);
2327 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002328
2329 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2330 key == input->repeat_key) {
2331 its.it_interval.tv_sec = 0;
2332 its.it_interval.tv_nsec = 0;
2333 its.it_value.tv_sec = 0;
2334 its.it_value.tv_nsec = 0;
2335 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2336 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2337 input->repeat_sym = sym;
2338 input->repeat_key = key;
2339 input->repeat_time = time;
2340 its.it_interval.tv_sec = 0;
2341 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2342 its.it_value.tv_sec = 0;
2343 its.it_value.tv_nsec = 400 * 1000 * 1000;
2344 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2345 }
2346}
2347
2348static void
Daniel Stone351eb612012-05-31 15:27:47 -04002349keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2350 uint32_t serial, uint32_t mods_depressed,
2351 uint32_t mods_latched, uint32_t mods_locked,
2352 uint32_t group)
2353{
2354 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002355 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002356
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002357 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2358 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002359 mask = xkb_state_serialize_mods(input->xkb.state,
2360 XKB_STATE_DEPRESSED |
2361 XKB_STATE_LATCHED);
2362 input->modifiers = 0;
2363 if (mask & input->xkb.control_mask)
2364 input->modifiers |= MOD_CONTROL_MASK;
2365 if (mask & input->xkb.alt_mask)
2366 input->modifiers |= MOD_ALT_MASK;
2367 if (mask & input->xkb.shift_mask)
2368 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002369}
2370
Daniel Stone37816df2012-05-16 18:45:18 +01002371static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002372 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002373 keyboard_handle_enter,
2374 keyboard_handle_leave,
2375 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002376 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002377};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002378
2379static void
Daniel Stone37816df2012-05-16 18:45:18 +01002380seat_handle_capabilities(void *data, struct wl_seat *seat,
2381 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002382{
Daniel Stone37816df2012-05-16 18:45:18 +01002383 struct input *input = data;
2384
2385 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2386 input->pointer = wl_seat_get_pointer(seat);
2387 wl_pointer_set_user_data(input->pointer, input);
2388 wl_pointer_add_listener(input->pointer, &pointer_listener,
2389 input);
2390 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2391 wl_pointer_destroy(input->pointer);
2392 input->pointer = NULL;
2393 }
2394
2395 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2396 input->keyboard = wl_seat_get_keyboard(seat);
2397 wl_keyboard_set_user_data(input->keyboard, input);
2398 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2399 input);
2400 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2401 wl_keyboard_destroy(input->keyboard);
2402 input->keyboard = NULL;
2403 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002404}
2405
Daniel Stone37816df2012-05-16 18:45:18 +01002406static const struct wl_seat_listener seat_listener = {
2407 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002408};
2409
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002410void
2411input_get_position(struct input *input, int32_t *x, int32_t *y)
2412{
2413 *x = input->sx;
2414 *y = input->sy;
2415}
2416
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002417struct display *
2418input_get_display(struct input *input)
2419{
2420 return input->display;
2421}
2422
Daniel Stone37816df2012-05-16 18:45:18 +01002423struct wl_seat *
2424input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002425{
Daniel Stone37816df2012-05-16 18:45:18 +01002426 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002427}
2428
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002429uint32_t
2430input_get_modifiers(struct input *input)
2431{
2432 return input->modifiers;
2433}
2434
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002435struct widget *
2436input_get_focus_widget(struct input *input)
2437{
2438 return input->focus_widget;
2439}
2440
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002441struct data_offer {
2442 struct wl_data_offer *offer;
2443 struct input *input;
2444 struct wl_array types;
2445 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002446
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002447 struct task io_task;
2448 int fd;
2449 data_func_t func;
2450 int32_t x, y;
2451 void *user_data;
2452};
2453
2454static void
2455data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2456{
2457 struct data_offer *offer = data;
2458 char **p;
2459
2460 p = wl_array_add(&offer->types, sizeof *p);
2461 *p = strdup(type);
2462}
2463
2464static const struct wl_data_offer_listener data_offer_listener = {
2465 data_offer_offer,
2466};
2467
2468static void
2469data_offer_destroy(struct data_offer *offer)
2470{
2471 char **p;
2472
2473 offer->refcount--;
2474 if (offer->refcount == 0) {
2475 wl_data_offer_destroy(offer->offer);
2476 for (p = offer->types.data; *p; p++)
2477 free(*p);
2478 wl_array_release(&offer->types);
2479 free(offer);
2480 }
2481}
2482
2483static void
2484data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002485 struct wl_data_device *data_device,
2486 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002487{
2488 struct data_offer *offer;
2489
2490 offer = malloc(sizeof *offer);
2491
2492 wl_array_init(&offer->types);
2493 offer->refcount = 1;
2494 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002495 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002496 wl_data_offer_add_listener(offer->offer,
2497 &data_offer_listener, offer);
2498}
2499
2500static void
2501data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002502 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002503 wl_fixed_t x_w, wl_fixed_t y_w,
2504 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002505{
2506 struct input *input = data;
2507 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002508 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002509 float x = wl_fixed_to_double(x_w);
2510 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002511 char **p;
2512
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002513 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002514 window = wl_surface_get_user_data(surface);
2515 input->pointer_focus = window;
2516
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002517 if (offer) {
2518 input->drag_offer = wl_data_offer_get_user_data(offer);
2519
2520 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2521 *p = NULL;
2522
2523 types_data = input->drag_offer->types.data;
2524 } else {
2525 input->drag_offer = NULL;
2526 types_data = NULL;
2527 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002528
2529 window = input->pointer_focus;
2530 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002531 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002532 window->user_data);
2533}
2534
2535static void
2536data_device_leave(void *data, struct wl_data_device *data_device)
2537{
2538 struct input *input = data;
2539
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002540 if (input->drag_offer) {
2541 data_offer_destroy(input->drag_offer);
2542 input->drag_offer = NULL;
2543 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002544}
2545
2546static void
2547data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002548 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002549{
2550 struct input *input = data;
2551 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002552 float x = wl_fixed_to_double(x_w);
2553 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002554 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002555
2556 input->sx = x;
2557 input->sy = y;
2558
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002559 if (input->drag_offer)
2560 types_data = input->drag_offer->types.data;
2561 else
2562 types_data = NULL;
2563
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002564 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002565 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002566 window->user_data);
2567}
2568
2569static void
2570data_device_drop(void *data, struct wl_data_device *data_device)
2571{
2572 struct input *input = data;
2573 struct window *window = input->pointer_focus;
2574
2575 if (window->drop_handler)
2576 window->drop_handler(window, input,
2577 input->sx, input->sy, window->user_data);
2578}
2579
2580static void
2581data_device_selection(void *data,
2582 struct wl_data_device *wl_data_device,
2583 struct wl_data_offer *offer)
2584{
2585 struct input *input = data;
2586 char **p;
2587
2588 if (input->selection_offer)
2589 data_offer_destroy(input->selection_offer);
2590
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002591 if (offer) {
2592 input->selection_offer = wl_data_offer_get_user_data(offer);
2593 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2594 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002595 } else {
2596 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002597 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002598}
2599
2600static const struct wl_data_device_listener data_device_listener = {
2601 data_device_data_offer,
2602 data_device_enter,
2603 data_device_leave,
2604 data_device_motion,
2605 data_device_drop,
2606 data_device_selection
2607};
2608
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002609static void
2610input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002611{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002612 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002613 struct wl_cursor *cursor;
2614 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002615
Daniel Stone80972742012-11-07 17:51:39 +11002616 if (!input->pointer)
2617 return;
2618
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002619 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002620 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002621 return;
2622
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002623 if (index >= (int) cursor->image_count) {
2624 fprintf(stderr, "cursor index out of range\n");
2625 return;
2626 }
2627
2628 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002629 buffer = wl_cursor_image_get_buffer(image);
2630 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002631 return;
2632
Kristian Høgsbergae277372012-08-01 09:41:08 -04002633 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002634 input->pointer_surface,
2635 image->hotspot_x, image->hotspot_y);
2636 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2637 wl_surface_damage(input->pointer_surface, 0, 0,
2638 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002639 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002640}
2641
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002642static const struct wl_callback_listener pointer_surface_listener;
2643
2644static void
2645pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2646 uint32_t time)
2647{
2648 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002649 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002650 int i;
2651
2652 if (callback) {
2653 assert(callback == input->cursor_frame_cb);
2654 wl_callback_destroy(callback);
2655 input->cursor_frame_cb = NULL;
2656 }
2657
Daniel Stone80972742012-11-07 17:51:39 +11002658 if (!input->pointer)
2659 return;
2660
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002661 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002662 wl_pointer_set_cursor(input->pointer,
2663 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002664 NULL, 0, 0);
2665 return;
2666 }
2667
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002668 if (input->current_cursor == CURSOR_UNSET)
2669 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002670 cursor = input->display->cursors[input->current_cursor];
2671 if (!cursor)
2672 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002673
2674 /* FIXME We don't have the current time on the first call so we set
2675 * the animation start to the time of the first frame callback. */
2676 if (time == 0)
2677 input->cursor_anim_start = 0;
2678 else if (input->cursor_anim_start == 0)
2679 input->cursor_anim_start = time;
2680
2681 if (time == 0 || input->cursor_anim_start == 0)
2682 i = 0;
2683 else
2684 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2685
Pekka Paalanenbc106382012-10-10 12:49:31 +03002686 if (cursor->image_count > 1) {
2687 input->cursor_frame_cb =
2688 wl_surface_frame(input->pointer_surface);
2689 wl_callback_add_listener(input->cursor_frame_cb,
2690 &pointer_surface_listener, input);
2691 }
2692
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002693 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002694}
2695
2696static const struct wl_callback_listener pointer_surface_listener = {
2697 pointer_surface_frame_callback
2698};
2699
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002700void
2701input_set_pointer_image(struct input *input, int pointer)
2702{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002703 int force = 0;
2704
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002705 if (!input->pointer)
2706 return;
2707
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002708 if (input->pointer_enter_serial > input->cursor_serial)
2709 force = 1;
2710
2711 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002712 return;
2713
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002714 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002715 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002716 if (!input->cursor_frame_cb)
2717 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002718 else if (force) {
2719 /* The current frame callback may be stuck if, for instance,
2720 * the set cursor request was processed by the server after
2721 * this client lost the focus. In this case the cursor surface
2722 * might not be mapped and the frame callback wouldn't ever
2723 * complete. Send a set_cursor and attach to try to map the
2724 * cursor surface again so that the callback will finish */
2725 input_set_pointer_image_index(input, 0);
2726 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002727}
2728
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002729struct wl_data_device *
2730input_get_data_device(struct input *input)
2731{
2732 return input->data_device;
2733}
2734
2735void
2736input_set_selection(struct input *input,
2737 struct wl_data_source *source, uint32_t time)
2738{
2739 wl_data_device_set_selection(input->data_device, source, time);
2740}
2741
2742void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002743input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002744{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002745 wl_data_offer_accept(input->drag_offer->offer,
2746 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002747}
2748
2749static void
2750offer_io_func(struct task *task, uint32_t events)
2751{
2752 struct data_offer *offer =
2753 container_of(task, struct data_offer, io_task);
2754 unsigned int len;
2755 char buffer[4096];
2756
2757 len = read(offer->fd, buffer, sizeof buffer);
2758 offer->func(buffer, len,
2759 offer->x, offer->y, offer->user_data);
2760
2761 if (len == 0) {
2762 close(offer->fd);
2763 data_offer_destroy(offer);
2764 }
2765}
2766
2767static void
2768data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2769 data_func_t func, void *user_data)
2770{
2771 int p[2];
2772
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002773 if (pipe2(p, O_CLOEXEC) == -1)
2774 return;
2775
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002776 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2777 close(p[1]);
2778
2779 offer->io_task.run = offer_io_func;
2780 offer->fd = p[0];
2781 offer->func = func;
2782 offer->refcount++;
2783 offer->user_data = user_data;
2784
2785 display_watch_fd(offer->input->display,
2786 offer->fd, EPOLLIN, &offer->io_task);
2787}
2788
2789void
2790input_receive_drag_data(struct input *input, const char *mime_type,
2791 data_func_t func, void *data)
2792{
2793 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2794 input->drag_offer->x = input->sx;
2795 input->drag_offer->y = input->sy;
2796}
2797
2798int
2799input_receive_selection_data(struct input *input, const char *mime_type,
2800 data_func_t func, void *data)
2801{
2802 char **p;
2803
2804 if (input->selection_offer == NULL)
2805 return -1;
2806
2807 for (p = input->selection_offer->types.data; *p; p++)
2808 if (strcmp(mime_type, *p) == 0)
2809 break;
2810
2811 if (*p == NULL)
2812 return -1;
2813
2814 data_offer_receive_data(input->selection_offer,
2815 mime_type, func, data);
2816 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002817}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002818
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002819int
2820input_receive_selection_data_to_fd(struct input *input,
2821 const char *mime_type, int fd)
2822{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002823 if (input->selection_offer)
2824 wl_data_offer_receive(input->selection_offer->offer,
2825 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002826
2827 return 0;
2828}
2829
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002830void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002831window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002832{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002833 if (!window->shell_surface)
2834 return;
2835
Daniel Stone37816df2012-05-16 18:45:18 +01002836 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002837}
2838
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002839static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002840idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002841{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002842 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002843
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002844 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002845 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002846 widget_set_allocation(widget,
2847 window->pending_allocation.x,
2848 window->pending_allocation.y,
2849 window->pending_allocation.width,
2850 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002851
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002852 if (window->input_region) {
2853 wl_region_destroy(window->input_region);
2854 window->input_region = NULL;
2855 }
2856
2857 if (window->opaque_region) {
2858 wl_region_destroy(window->opaque_region);
2859 window->opaque_region = NULL;
2860 }
2861
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002862 if (widget->resize_handler)
2863 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002864 widget->allocation.width,
2865 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002866 widget->user_data);
2867
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002868 if (window->allocation.width != widget->allocation.width ||
2869 window->allocation.height != widget->allocation.height) {
2870 window->allocation = widget->allocation;
2871 window_schedule_redraw(window);
2872 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002873}
2874
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002875void
2876window_schedule_resize(struct window *window, int width, int height)
2877{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002878 window->pending_allocation.x = 0;
2879 window->pending_allocation.y = 0;
2880 window->pending_allocation.width = width;
2881 window->pending_allocation.height = height;
2882
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002883 if (window->min_allocation.width == 0)
2884 window->min_allocation = window->pending_allocation;
2885 if (window->pending_allocation.width < window->min_allocation.width)
2886 window->pending_allocation.width = window->min_allocation.width;
2887 if (window->pending_allocation.height < window->min_allocation.height)
2888 window->pending_allocation.height = window->min_allocation.height;
2889
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002890 window->resize_needed = 1;
2891 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002892}
2893
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002894void
2895widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2896{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002897 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002898}
2899
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002900static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002901handle_ping(void *data, struct wl_shell_surface *shell_surface,
2902 uint32_t serial)
2903{
2904 wl_shell_surface_pong(shell_surface, serial);
2905}
2906
2907static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002908handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002909 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002910{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002911 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002912
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002913 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002914 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002915}
2916
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002917static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002918menu_destroy(struct menu *menu)
2919{
2920 widget_destroy(menu->widget);
2921 window_destroy(menu->window);
2922 free(menu);
2923}
2924
2925static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002926handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2927{
2928 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002929 struct menu *menu = window->widget->user_data;
2930
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002931 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002932 * device. Or just use wl_callback. And this really needs to
2933 * be a window vfunc that the menu can set. And we need the
2934 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002935
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002936 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002937 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002938 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002939}
2940
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002941static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002942 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002943 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002944 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002945};
2946
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002947void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002948window_get_allocation(struct window *window,
2949 struct rectangle *allocation)
2950{
2951 *allocation = window->allocation;
2952}
2953
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002954static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002955widget_redraw(struct widget *widget)
2956{
2957 struct widget *child;
2958
2959 if (widget->redraw_handler)
2960 widget->redraw_handler(widget, widget->user_data);
2961 wl_list_for_each(child, &widget->child_list, link)
2962 widget_redraw(child);
2963}
2964
2965static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002966frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2967{
2968 struct window *window = data;
2969
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002970 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002971 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002972 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002973 window->redraw_scheduled = 0;
2974 if (window->redraw_needed)
2975 window_schedule_redraw(window);
2976}
2977
2978static const struct wl_callback_listener listener = {
2979 frame_callback
2980};
2981
2982static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002983idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002984{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002985 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002986
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002987 if (window->resize_needed)
2988 idle_resize(window);
2989
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002990 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002991 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002992 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002993 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002994
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002995 window->frame_cb = wl_surface_frame(window->surface);
2996 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002997 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002998}
2999
3000void
3001window_schedule_redraw(struct window *window)
3002{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003003 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003004 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003005 window->redraw_task.run = idle_redraw;
3006 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003007 window->redraw_scheduled = 1;
3008 }
3009}
3010
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003011int
3012window_is_fullscreen(struct window *window)
3013{
3014 return window->type == TYPE_FULLSCREEN;
3015}
3016
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003017void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003018window_set_fullscreen(struct window *window, int fullscreen)
3019{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003020 if (!window->display->shell)
3021 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003022
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003023 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003024 return;
3025
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003026 if (fullscreen) {
3027 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003028 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003029 wl_shell_surface_set_fullscreen(window->shell_surface,
3030 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3031 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003032 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003033 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003034 wl_shell_surface_set_toplevel(window->shell_surface);
3035 window_schedule_resize(window,
3036 window->saved_allocation.width,
3037 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003038 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003039}
3040
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003041int
3042window_is_maximized(struct window *window)
3043{
3044 return window->type == TYPE_MAXIMIZED;
3045}
3046
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003047void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003048window_set_maximized(struct window *window, int maximized)
3049{
3050 if (!window->display->shell)
3051 return;
3052
3053 if ((window->type == TYPE_MAXIMIZED) == maximized)
3054 return;
3055
3056 if (window->type == TYPE_TOPLEVEL) {
3057 window->saved_allocation = window->allocation;
3058 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3059 window->type = TYPE_MAXIMIZED;
3060 } else {
3061 wl_shell_surface_set_toplevel(window->shell_surface);
3062 window->type = TYPE_TOPLEVEL;
3063 window_schedule_resize(window,
3064 window->saved_allocation.width,
3065 window->saved_allocation.height);
3066 }
3067}
3068
3069void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003070window_set_user_data(struct window *window, void *data)
3071{
3072 window->user_data = data;
3073}
3074
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003075void *
3076window_get_user_data(struct window *window)
3077{
3078 return window->user_data;
3079}
3080
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003081void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003082window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003083 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003084{
3085 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003086}
3087
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003088void
3089window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003090 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003091{
3092 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003093}
3094
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003095void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003096window_set_data_handler(struct window *window, window_data_handler_t handler)
3097{
3098 window->data_handler = handler;
3099}
3100
3101void
3102window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3103{
3104 window->drop_handler = handler;
3105}
3106
3107void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003108window_set_close_handler(struct window *window,
3109 window_close_handler_t handler)
3110{
3111 window->close_handler = handler;
3112}
3113
3114void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003115window_set_fullscreen_handler(struct window *window,
3116 window_fullscreen_handler_t handler)
3117{
3118 window->fullscreen_handler = handler;
3119}
3120
3121void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003122window_set_title(struct window *window, const char *title)
3123{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003124 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003125 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003126 if (window->shell_surface)
3127 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003128}
3129
3130const char *
3131window_get_title(struct window *window)
3132{
3133 return window->title;
3134}
3135
3136void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003137window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3138{
3139 struct text_cursor_position *text_cursor_position =
3140 window->display->text_cursor_position;
3141
Scott Moreau9295ce02012-06-01 12:46:10 -06003142 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003143 return;
3144
3145 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003146 window->surface,
3147 wl_fixed_from_int(x),
3148 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003149}
3150
3151void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003152window_damage(struct window *window, int32_t x, int32_t y,
3153 int32_t width, int32_t height)
3154{
3155 wl_surface_damage(window->surface, x, y, width, height);
3156}
3157
Casey Dahlin9074db52012-04-19 22:50:09 -04003158static void
3159surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003160 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003161{
Rob Bradford7507b572012-05-15 17:55:34 +01003162 struct window *window = data;
3163 struct output *output;
3164 struct output *output_found = NULL;
3165 struct window_output *window_output;
3166
3167 wl_list_for_each(output, &window->display->output_list, link) {
3168 if (output->output == wl_output) {
3169 output_found = output;
3170 break;
3171 }
3172 }
3173
3174 if (!output_found)
3175 return;
3176
3177 window_output = malloc (sizeof *window_output);
3178 window_output->output = output_found;
3179
3180 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003181}
3182
3183static void
3184surface_leave(void *data,
3185 struct wl_surface *wl_surface, struct wl_output *output)
3186{
Rob Bradford7507b572012-05-15 17:55:34 +01003187 struct window *window = data;
3188 struct window_output *window_output;
3189 struct window_output *window_output_found = NULL;
3190
3191 wl_list_for_each(window_output, &window->window_output_list, link) {
3192 if (window_output->output->output == output) {
3193 window_output_found = window_output;
3194 break;
3195 }
3196 }
3197
3198 if (window_output_found) {
3199 wl_list_remove(&window_output_found->link);
3200 free(window_output_found);
3201 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003202}
3203
3204static const struct wl_surface_listener surface_listener = {
3205 surface_enter,
3206 surface_leave
3207};
3208
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003209static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003210window_create_internal(struct display *display,
3211 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003212{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003213 struct window *window;
3214
3215 window = malloc(sizeof *window);
3216 if (window == NULL)
3217 return NULL;
3218
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003219 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003220 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003221 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003222 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003223 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003224 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003225 window->shell_surface =
3226 wl_shell_get_shell_surface(display->shell,
3227 window->surface);
3228 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003229 window->allocation.x = 0;
3230 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003231 window->allocation.width = 0;
3232 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003233 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003234 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003235 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003236 window->input_region = NULL;
3237 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003238
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003239 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003240#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003241 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003242#else
3243 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3244#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003245 else
3246 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003247
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003248 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003249 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003250 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003251
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003252 if (window->shell_surface) {
3253 wl_shell_surface_set_user_data(window->shell_surface, window);
3254 wl_shell_surface_add_listener(window->shell_surface,
3255 &shell_surface_listener, window);
3256 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003257
Rob Bradford7507b572012-05-15 17:55:34 +01003258 wl_list_init (&window->window_output_list);
3259
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003260 return window;
3261}
3262
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003263struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003264window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003265{
3266 struct window *window;
3267
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003268 window = window_create_internal(display, NULL, TYPE_NONE);
3269 if (!window)
3270 return NULL;
3271
3272 return window;
3273}
3274
3275struct window *
3276window_create_custom(struct display *display)
3277{
3278 struct window *window;
3279
3280 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003281 if (!window)
3282 return NULL;
3283
3284 return window;
3285}
3286
3287struct window *
3288window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003289 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003290{
3291 struct window *window;
3292
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003293 window = window_create_internal(parent->display,
3294 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003295 if (!window)
3296 return NULL;
3297
3298 window->x = x;
3299 window->y = y;
3300
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003301 if (display->shell)
3302 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003303 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003304 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003305
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003306 return window;
3307}
3308
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003309static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003310menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003311{
3312 int next;
3313
3314 next = (sy - 8) / 20;
3315 if (menu->current != next) {
3316 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003317 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003318 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003319}
3320
3321static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003322menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003323 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003324 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003325{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003326 struct menu *menu = data;
3327
3328 if (widget == menu->widget)
3329 menu_set_item(data, y);
3330
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003331 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003332}
3333
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003334static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003335menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003336 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003337{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003338 struct menu *menu = data;
3339
3340 if (widget == menu->widget)
3341 menu_set_item(data, y);
3342
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003343 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003344}
3345
3346static void
3347menu_leave_handler(struct widget *widget, struct input *input, void *data)
3348{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003349 struct menu *menu = data;
3350
3351 if (widget == menu->widget)
3352 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003353}
3354
3355static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003356menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003357 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003358 uint32_t button, enum wl_pointer_button_state state,
3359 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003360
3361{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003362 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003363
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003364 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3365 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003366 /* Either relase after press-drag-release or
3367 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003368 menu->func(menu->window->parent,
3369 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003370 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003371 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003372 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003373 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003374 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003375}
3376
3377static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003378menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003379{
3380 cairo_t *cr;
3381 const int32_t r = 3, margin = 3;
3382 struct menu *menu = data;
3383 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003384 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003385
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003386 cr = cairo_create(window->cairo_surface);
3387 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3388 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3389 cairo_paint(cr);
3390
3391 width = window->allocation.width;
3392 height = window->allocation.height;
3393 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003394
3395 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003396 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3397 cairo_fill(cr);
3398
3399 for (i = 0; i < menu->count; i++) {
3400 if (i == menu->current) {
3401 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3402 cairo_rectangle(cr, margin, i * 20 + margin,
3403 width - 2 * margin, 20);
3404 cairo_fill(cr);
3405 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3406 cairo_move_to(cr, 10, i * 20 + 16);
3407 cairo_show_text(cr, menu->entries[i]);
3408 } else {
3409 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3410 cairo_move_to(cr, 10, i * 20 + 16);
3411 cairo_show_text(cr, menu->entries[i]);
3412 }
3413 }
3414
3415 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003416}
3417
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003418void
3419window_show_menu(struct display *display,
3420 struct input *input, uint32_t time, struct window *parent,
3421 int32_t x, int32_t y,
3422 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003423{
3424 struct window *window;
3425 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003426 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003427
3428 menu = malloc(sizeof *menu);
3429 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003430 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003431
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003432 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003433 if (!window) {
3434 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003435 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003436 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003437
3438 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003439 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003440 menu->entries = entries;
3441 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003442 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003443 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003444 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003445 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003446 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003447 window->type = TYPE_MENU;
3448 window->x = x;
3449 window->y = y;
3450
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003451 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003452 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003453 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003454 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003455 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003456
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003457 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003458 widget_set_enter_handler(menu->widget, menu_enter_handler);
3459 widget_set_leave_handler(menu->widget, menu_leave_handler);
3460 widget_set_motion_handler(menu->widget, menu_motion_handler);
3461 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003462
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003463 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003464 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003465}
3466
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003467void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003468window_set_buffer_type(struct window *window, enum window_buffer_type type)
3469{
3470 window->buffer_type = type;
3471}
3472
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003473
3474static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003475display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003476 struct wl_output *wl_output,
3477 int x, int y,
3478 int physical_width,
3479 int physical_height,
3480 int subpixel,
3481 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003482 const char *model,
3483 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003484{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003485 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003486
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003487 output->allocation.x = x;
3488 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003489 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003490}
3491
3492static void
3493display_handle_mode(void *data,
3494 struct wl_output *wl_output,
3495 uint32_t flags,
3496 int width,
3497 int height,
3498 int refresh)
3499{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003500 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003501 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003502
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003503 if (flags & WL_OUTPUT_MODE_CURRENT) {
3504 output->allocation.width = width;
3505 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003506 if (display->output_configure_handler)
3507 (*display->output_configure_handler)(
3508 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003509 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003510}
3511
3512static const struct wl_output_listener output_listener = {
3513 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003514 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003515};
3516
3517static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003518display_add_output(struct display *d, uint32_t id)
3519{
3520 struct output *output;
3521
3522 output = malloc(sizeof *output);
3523 if (output == NULL)
3524 return;
3525
3526 memset(output, 0, sizeof *output);
3527 output->display = d;
3528 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003529 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003530 wl_list_insert(d->output_list.prev, &output->link);
3531
3532 wl_output_add_listener(output->output, &output_listener, output);
3533}
3534
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003535static void
3536output_destroy(struct output *output)
3537{
3538 if (output->destroy_handler)
3539 (*output->destroy_handler)(output, output->user_data);
3540
3541 wl_output_destroy(output->output);
3542 wl_list_remove(&output->link);
3543 free(output);
3544}
3545
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003546void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003547display_set_global_handler(struct display *display,
3548 display_global_handler_t handler)
3549{
3550 struct global *global;
3551
3552 display->global_handler = handler;
3553 if (!handler)
3554 return;
3555
3556 wl_list_for_each(global, &display->global_list, link)
3557 display->global_handler(display,
3558 global->name, global->interface,
3559 global->version, display->user_data);
3560}
3561
3562void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003563display_set_output_configure_handler(struct display *display,
3564 display_output_handler_t handler)
3565{
3566 struct output *output;
3567
3568 display->output_configure_handler = handler;
3569 if (!handler)
3570 return;
3571
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003572 wl_list_for_each(output, &display->output_list, link) {
3573 if (output->allocation.width == 0 &&
3574 output->allocation.height == 0)
3575 continue;
3576
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003577 (*display->output_configure_handler)(output,
3578 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003579 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003580}
3581
3582void
3583output_set_user_data(struct output *output, void *data)
3584{
3585 output->user_data = data;
3586}
3587
3588void *
3589output_get_user_data(struct output *output)
3590{
3591 return output->user_data;
3592}
3593
3594void
3595output_set_destroy_handler(struct output *output,
3596 display_output_handler_t handler)
3597{
3598 output->destroy_handler = handler;
3599 /* FIXME: implement this, once we have way to remove outputs */
3600}
3601
3602void
Scott Moreau4e072362012-09-29 02:03:11 -06003603output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003604{
Scott Moreau4e072362012-09-29 02:03:11 -06003605 struct rectangle allocation = output->allocation;
3606
3607 switch (output->transform) {
3608 case WL_OUTPUT_TRANSFORM_90:
3609 case WL_OUTPUT_TRANSFORM_270:
3610 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3611 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3612 /* Swap width and height */
3613 allocation.width = output->allocation.height;
3614 allocation.height = output->allocation.width;
3615 break;
3616 }
3617
3618 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003619}
3620
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003621struct wl_output *
3622output_get_wl_output(struct output *output)
3623{
3624 return output->output;
3625}
3626
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003627static void
Daniel Stone97f68542012-05-30 16:32:01 +01003628fini_xkb(struct input *input)
3629{
3630 xkb_state_unref(input->xkb.state);
3631 xkb_map_unref(input->xkb.keymap);
3632}
3633
3634static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003635display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003636{
3637 struct input *input;
3638
3639 input = malloc(sizeof *input);
3640 if (input == NULL)
3641 return;
3642
3643 memset(input, 0, sizeof *input);
3644 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003645 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003646 input->pointer_focus = NULL;
3647 input->keyboard_focus = NULL;
3648 wl_list_insert(d->input_list.prev, &input->link);
3649
Daniel Stone37816df2012-05-16 18:45:18 +01003650 wl_seat_add_listener(input->seat, &seat_listener, input);
3651 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003652
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003653 input->data_device =
3654 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003655 input->seat);
3656 wl_data_device_add_listener(input->data_device, &data_device_listener,
3657 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003658
3659 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003660
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003661 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3662 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003663 input->repeat_task.run = keyboard_repeat_func;
3664 display_watch_fd(d, input->repeat_timer_fd,
3665 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003666}
3667
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003668static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003669input_destroy(struct input *input)
3670{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003671 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003672 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003673
3674 if (input->drag_offer)
3675 data_offer_destroy(input->drag_offer);
3676
3677 if (input->selection_offer)
3678 data_offer_destroy(input->selection_offer);
3679
3680 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003681 fini_xkb(input);
3682
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003683 wl_surface_destroy(input->pointer_surface);
3684
Pekka Paalanene1207c72011-12-16 12:02:09 +02003685 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003686 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003687 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003688 free(input);
3689}
3690
3691static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003692init_workspace_manager(struct display *d, uint32_t id)
3693{
3694 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003695 wl_registry_bind(d->registry, id,
3696 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003697 if (d->workspace_manager != NULL)
3698 workspace_manager_add_listener(d->workspace_manager,
3699 &workspace_manager_listener,
3700 d);
3701}
3702
3703static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003704registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3705 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003706{
3707 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003708 struct global *global;
3709
3710 global = malloc(sizeof *global);
3711 global->name = id;
3712 global->interface = strdup(interface);
3713 global->version = version;
3714 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003715
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003716 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003717 d->compositor = wl_registry_bind(registry, id,
3718 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003719 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003720 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003721 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003722 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003723 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003724 d->shell = wl_registry_bind(registry,
3725 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003726 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003727 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003728 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3729 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003730 wl_registry_bind(registry, id,
3731 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003732 } else if (strcmp(interface, "text_cursor_position") == 0) {
3733 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003734 wl_registry_bind(registry, id,
3735 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003736 } else if (strcmp(interface, "workspace_manager") == 0) {
3737 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003738 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003739
3740 if (d->global_handler)
3741 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003742}
3743
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003744void *
3745display_bind(struct display *display, uint32_t name,
3746 const struct wl_interface *interface, uint32_t version)
3747{
3748 return wl_registry_bind(display->registry, name, interface, version);
3749}
3750
3751static const struct wl_registry_listener registry_listener = {
3752 registry_handle_global
3753};
3754
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003755#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003756static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003757init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003758{
3759 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003760 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003761
Rob Clark6396ed32012-03-11 19:48:41 -05003762#ifdef USE_CAIRO_GLESV2
3763# define GL_BIT EGL_OPENGL_ES2_BIT
3764#else
3765# define GL_BIT EGL_OPENGL_BIT
3766#endif
3767
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003768 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003769 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003770 EGL_RED_SIZE, 1,
3771 EGL_GREEN_SIZE, 1,
3772 EGL_BLUE_SIZE, 1,
3773 EGL_ALPHA_SIZE, 1,
3774 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003775 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003776 EGL_NONE
3777 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003778
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003779#ifdef USE_CAIRO_GLESV2
3780 static const EGLint context_attribs[] = {
3781 EGL_CONTEXT_CLIENT_VERSION, 2,
3782 EGL_NONE
3783 };
3784 EGLint api = EGL_OPENGL_ES_API;
3785#else
3786 EGLint *context_attribs = NULL;
3787 EGLint api = EGL_OPENGL_API;
3788#endif
3789
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003790 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003791 if (!eglInitialize(d->dpy, &major, &minor)) {
3792 fprintf(stderr, "failed to initialize display\n");
3793 return -1;
3794 }
3795
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003796 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003797 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3798 return -1;
3799 }
3800
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003801 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3802 &d->argb_config, 1, &n) || n != 1) {
3803 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003804 return -1;
3805 }
3806
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003807 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003808 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003809 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003810 fprintf(stderr, "failed to create context\n");
3811 return -1;
3812 }
3813
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003814 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003815 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003816 return -1;
3817 }
3818
Benjamin Franzke0c991632011-09-27 21:57:31 +02003819 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3820 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3821 fprintf(stderr, "failed to get cairo egl argb device\n");
3822 return -1;
3823 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003824
3825 return 0;
3826}
3827
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003828static void
3829fini_egl(struct display *display)
3830{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003831 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003832
3833 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3834 EGL_NO_CONTEXT);
3835
3836 eglTerminate(display->dpy);
3837 eglReleaseThread();
3838}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003839#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003840
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003841static void
3842handle_display_data(struct task *task, uint32_t events)
3843{
3844 struct display *display =
3845 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003846 struct epoll_event ep;
3847 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003848
3849 display->display_fd_events = events;
3850
3851 if (events & EPOLLERR || events & EPOLLHUP) {
3852 display_exit(display);
3853 return;
3854 }
3855
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04003856 if (events & EPOLLIN) {
3857 ret = wl_display_dispatch(display->display);
3858 if (ret == -1) {
3859 display_exit(display);
3860 return;
3861 }
3862 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003863
3864 if (events & EPOLLOUT) {
3865 ret = wl_display_flush(display->display);
3866 if (ret == 0) {
3867 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3868 ep.data.ptr = &display->display_task;
3869 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3870 display->display_fd, &ep);
3871 } else if (ret == -1 && errno != EAGAIN) {
3872 display_exit(display);
3873 return;
3874 }
3875 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003876}
3877
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003878struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003879display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003880{
3881 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003882
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003883 d = malloc(sizeof *d);
3884 if (d == NULL)
3885 return NULL;
3886
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003887 memset(d, 0, sizeof *d);
3888
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003889 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003890 if (d->display == NULL) {
3891 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003892 return NULL;
3893 }
3894
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003895 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003896 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003897 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003898 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3899 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003900
3901 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003902 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003903 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003904 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003905
Daniel Stone97f68542012-05-30 16:32:01 +01003906 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003907 if (d->xkb_context == NULL) {
3908 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003909 return NULL;
3910 }
3911
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003912 d->workspace = 0;
3913 d->workspace_count = 1;
3914
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003915 d->registry = wl_display_get_registry(d->display);
3916 wl_registry_add_listener(d->registry, &registry_listener, d);
3917 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003918#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003919 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003920 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003921#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003922
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003923 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003924
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003925 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003926
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003927 wl_list_init(&d->window_list);
3928
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003929 return d;
3930}
3931
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003932static void
3933display_destroy_outputs(struct display *display)
3934{
3935 struct output *tmp;
3936 struct output *output;
3937
3938 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3939 output_destroy(output);
3940}
3941
Pekka Paalanene1207c72011-12-16 12:02:09 +02003942static void
3943display_destroy_inputs(struct display *display)
3944{
3945 struct input *tmp;
3946 struct input *input;
3947
3948 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3949 input_destroy(input);
3950}
3951
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003952void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003953display_destroy(struct display *display)
3954{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003955 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003956 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3957 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003958
3959 if (!wl_list_empty(&display->deferred_list))
3960 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3961
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003962 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003963 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003964
Daniel Stone97f68542012-05-30 16:32:01 +01003965 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003966
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003967 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003968 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003969
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003970#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003971 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003972#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003973
Pekka Paalanenc2052982011-12-16 11:41:32 +02003974 if (display->shell)
3975 wl_shell_destroy(display->shell);
3976
3977 if (display->shm)
3978 wl_shm_destroy(display->shm);
3979
3980 if (display->data_device_manager)
3981 wl_data_device_manager_destroy(display->data_device_manager);
3982
3983 wl_compositor_destroy(display->compositor);
3984
3985 close(display->epoll_fd);
3986
U. Artie Eoff44874d92012-10-02 21:12:35 -07003987 if (!(display->display_fd_events & EPOLLERR) &&
3988 !(display->display_fd_events & EPOLLHUP))
3989 wl_display_flush(display->display);
3990
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003991 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003992 free(display);
3993}
3994
3995void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003996display_set_user_data(struct display *display, void *data)
3997{
3998 display->user_data = data;
3999}
4000
4001void *
4002display_get_user_data(struct display *display)
4003{
4004 return display->user_data;
4005}
4006
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004007struct wl_display *
4008display_get_display(struct display *display)
4009{
4010 return display->display;
4011}
4012
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004013struct output *
4014display_get_output(struct display *display)
4015{
4016 return container_of(display->output_list.next, struct output, link);
4017}
4018
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004019struct wl_compositor *
4020display_get_compositor(struct display *display)
4021{
4022 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004023}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004024
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004025uint32_t
4026display_get_serial(struct display *display)
4027{
4028 return display->serial;
4029}
4030
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004031EGLDisplay
4032display_get_egl_display(struct display *d)
4033{
4034 return d->dpy;
4035}
4036
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004037struct wl_data_source *
4038display_create_data_source(struct display *display)
4039{
4040 return wl_data_device_manager_create_data_source(display->data_device_manager);
4041}
4042
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004043EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004044display_get_argb_egl_config(struct display *d)
4045{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004046 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004047}
4048
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004049struct wl_shell *
4050display_get_shell(struct display *display)
4051{
4052 return display->shell;
4053}
4054
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004055int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004056display_acquire_window_surface(struct display *display,
4057 struct window *window,
4058 EGLContext ctx)
4059{
Benjamin Franzke22d54812011-07-16 19:50:32 +00004060#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004061 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004062 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004063
4064 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004065 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004066 device = cairo_surface_get_device(window->cairo_surface);
4067 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004068 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004069
Benjamin Franzke0c991632011-09-27 21:57:31 +02004070 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004071 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004072 ctx = display->argb_ctx;
4073 else
4074 assert(0);
4075 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004076
4077 data = cairo_surface_get_user_data(window->cairo_surface,
4078 &surface_data_key);
4079
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004080 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004081 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004082 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4083 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004084
4085 return 0;
4086#else
4087 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004088#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004089}
4090
4091void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004092display_release_window_surface(struct display *display,
4093 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004094{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004095#ifdef HAVE_CAIRO_EGL
4096 cairo_device_t *device;
4097
4098 device = cairo_surface_get_device(window->cairo_surface);
4099 if (!device)
4100 return;
4101
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004102 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004103 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004104 cairo_device_release(device);
4105#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004106}
4107
4108void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004109display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004110{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004111 wl_list_insert(&display->deferred_list, &task->link);
4112}
4113
4114void
4115display_watch_fd(struct display *display,
4116 int fd, uint32_t events, struct task *task)
4117{
4118 struct epoll_event ep;
4119
4120 ep.events = events;
4121 ep.data.ptr = task;
4122 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4123}
4124
4125void
4126display_run(struct display *display)
4127{
4128 struct task *task;
4129 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004130 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004131
Pekka Paalanen826d7952011-12-15 10:14:07 +02004132 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004133 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004134 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004135 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004136 struct task, link);
4137 wl_list_remove(&task->link);
4138 task->run(task, 0);
4139 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004140
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004141 wl_display_dispatch_pending(display->display);
4142
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004143 if (!display->running)
4144 break;
4145
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004146 ret = wl_display_flush(display->display);
4147 if (ret < 0 && errno == EAGAIN) {
4148 ep[0].events =
4149 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4150 ep[0].data.ptr = &display->display_task;
4151
4152 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4153 display->display_fd, &ep[0]);
4154 } else if (ret < 0) {
4155 break;
4156 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004157
4158 count = epoll_wait(display->epoll_fd,
4159 ep, ARRAY_LENGTH(ep), -1);
4160 for (i = 0; i < count; i++) {
4161 task = ep[i].data.ptr;
4162 task->run(task, ep[i].events);
4163 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004164 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004165}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004166
4167void
4168display_exit(struct display *display)
4169{
4170 display->running = 0;
4171}