blob: e699fbc859f91ba4525b4ed8e0a5ee6ec78bb3e6 [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;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400873 cairo_surface_destroy(window->cairo_surface);
874 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100875 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000876 default:
877 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100878 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500879}
880
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400881int
882window_has_focus(struct window *window)
883{
884 return window->focus_count > 0;
885}
886
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500887void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400888window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500889{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400890 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100891 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500892}
893
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400894void
895window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400896{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500897 cairo_surface_reference(surface);
898
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400899 if (window->cairo_surface != NULL)
900 cairo_surface_destroy(window->cairo_surface);
901
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500902 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400903}
904
Benjamin Franzke22d54812011-07-16 19:50:32 +0000905#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906static void
907window_resize_cairo_window_surface(struct window *window)
908{
909 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200910 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911
912 data = cairo_surface_get_user_data(window->cairo_surface,
913 &surface_data_key);
914
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200915 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100916 wl_egl_window_resize(data->window,
917 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200918 window->allocation.height,
919 x,y);
920
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100921 cairo_gl_surface_set_size(window->cairo_surface,
922 window->allocation.width,
923 window->allocation.height);
924}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000925#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100926
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400927struct display *
928window_get_display(struct window *window)
929{
930 return window->display;
931}
932
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400933void
934window_create_surface(struct window *window)
935{
936 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400937 uint32_t flags = 0;
938
939 if (!window->transparent)
940 flags = SURFACE_OPAQUE;
941
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400942 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500943#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100944 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
945 if (window->cairo_surface) {
946 window_resize_cairo_window_surface(window);
947 return;
948 }
949 surface = display_create_surface(window->display,
950 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400951 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100952 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400953#endif
954 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400955 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400956 &window->allocation,
957 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400958 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800959 default:
960 surface = NULL;
961 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400962 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400963
964 window_set_surface(window, surface);
965 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400966}
967
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200968static void frame_destroy(struct frame *frame);
969
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400970void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500971window_destroy(struct window *window)
972{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200973 struct display *display = window->display;
974 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100975 struct window_output *window_output;
976 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200977
978 if (window->redraw_scheduled)
979 wl_list_remove(&window->redraw_task.link);
980
981 wl_list_for_each(input, &display->input_list, link) {
982 if (input->pointer_focus == window)
983 input->pointer_focus = NULL;
984 if (input->keyboard_focus == window)
985 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500986 if (input->focus_widget &&
987 input->focus_widget->window == window)
988 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200989 }
990
Rob Bradford7507b572012-05-15 17:55:34 +0100991 wl_list_for_each_safe(window_output, window_output_tmp,
992 &window->window_output_list, link) {
993 free (window_output);
994 }
995
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500996 if (window->input_region)
997 wl_region_destroy(window->input_region);
998 if (window->opaque_region)
999 wl_region_destroy(window->opaque_region);
1000
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001001 if (window->frame)
1002 frame_destroy(window->frame);
1003
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001004 if (window->shell_surface)
1005 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001006 wl_surface_destroy(window->surface);
1007 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001008
1009 if (window->cairo_surface != NULL)
1010 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001011
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001012 if (window->frame_cb)
1013 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001014 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001015 free(window);
1016}
1017
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001018static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001019widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001021 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001022
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001023 wl_list_for_each(child, &widget->child_list, link) {
1024 target = widget_find_widget(child, x, y);
1025 if (target)
1026 return target;
1027 }
1028
1029 if (widget->allocation.x <= x &&
1030 x < widget->allocation.x + widget->allocation.width &&
1031 widget->allocation.y <= y &&
1032 y < widget->allocation.y + widget->allocation.height) {
1033 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001034 }
1035
1036 return NULL;
1037}
1038
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001039static struct widget *
1040widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001042 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001043
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001044 widget = malloc(sizeof *widget);
1045 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001046 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001047 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001048 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001049 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001050 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001051 widget->tooltip = NULL;
1052 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001053
1054 return widget;
1055}
1056
1057struct widget *
1058window_add_widget(struct window *window, void *data)
1059{
1060 window->widget = widget_create(window, data);
1061 wl_list_init(&window->widget->link);
1062
1063 return window->widget;
1064}
1065
1066struct widget *
1067widget_add_widget(struct widget *parent, void *data)
1068{
1069 struct widget *widget;
1070
1071 widget = widget_create(parent->window, data);
1072 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001074 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001075}
1076
1077void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001078widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001079{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001080 struct display *display = widget->window->display;
1081 struct input *input;
1082
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001083 if (widget->tooltip) {
1084 free(widget->tooltip);
1085 widget->tooltip = NULL;
1086 }
1087
Pekka Paalanene156fb62012-01-19 13:51:38 +02001088 wl_list_for_each(input, &display->input_list, link) {
1089 if (input->focus_widget == widget)
1090 input->focus_widget = NULL;
1091 }
1092
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001093 wl_list_remove(&widget->link);
1094 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095}
1096
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001100 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001101}
1102
1103void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001104widget_set_size(struct widget *widget, int32_t width, int32_t height)
1105{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001106 widget->allocation.width = width;
1107 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001108}
1109
1110void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001111widget_set_allocation(struct widget *widget,
1112 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001113{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001114 widget->allocation.x = x;
1115 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001116 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001117}
1118
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001119void
1120widget_set_transparent(struct widget *widget, int transparent)
1121{
1122 widget->opaque = !transparent;
1123}
1124
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001125void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001126widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001127{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001128 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001129}
1130
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001131void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001132widget_set_resize_handler(struct widget *widget,
1133 widget_resize_handler_t handler)
1134{
1135 widget->resize_handler = handler;
1136}
1137
1138void
1139widget_set_redraw_handler(struct widget *widget,
1140 widget_redraw_handler_t handler)
1141{
1142 widget->redraw_handler = handler;
1143}
1144
1145void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001146widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001147{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001148 widget->enter_handler = handler;
1149}
1150
1151void
1152widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1153{
1154 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001155}
1156
1157void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001158widget_set_motion_handler(struct widget *widget,
1159 widget_motion_handler_t handler)
1160{
1161 widget->motion_handler = handler;
1162}
1163
1164void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001165widget_set_button_handler(struct widget *widget,
1166 widget_button_handler_t handler)
1167{
1168 widget->button_handler = handler;
1169}
1170
1171void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001172widget_set_axis_handler(struct widget *widget,
1173 widget_axis_handler_t handler)
1174{
1175 widget->axis_handler = handler;
1176}
1177
1178void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001179widget_schedule_redraw(struct widget *widget)
1180{
1181 window_schedule_redraw(widget->window);
1182}
1183
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001184cairo_surface_t *
1185window_get_surface(struct window *window)
1186{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001187 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001188}
1189
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001190struct wl_surface *
1191window_get_wl_surface(struct window *window)
1192{
1193 return window->surface;
1194}
1195
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001196struct wl_shell_surface *
1197window_get_wl_shell_surface(struct window *window)
1198{
1199 return window->shell_surface;
1200}
1201
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001202static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001203tooltip_redraw_handler(struct widget *widget, void *data)
1204{
1205 cairo_t *cr;
1206 const int32_t r = 3;
1207 struct tooltip *tooltip = data;
1208 int32_t width, height;
1209 struct window *window = widget->window;
1210
1211 cr = cairo_create(window->cairo_surface);
1212 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1213 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1214 cairo_paint(cr);
1215
1216 width = window->allocation.width;
1217 height = window->allocation.height;
1218 rounded_rect(cr, 0, 0, width, height, r);
1219
1220 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1221 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1222 cairo_fill(cr);
1223
1224 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1225 cairo_move_to(cr, 10, 16);
1226 cairo_show_text(cr, tooltip->entry);
1227 cairo_destroy(cr);
1228}
1229
1230static cairo_text_extents_t
1231get_text_extents(struct tooltip *tooltip)
1232{
1233 struct window *window;
1234 cairo_t *cr;
1235 cairo_text_extents_t extents;
1236
1237 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1238 * created yet */
1239 window = tooltip->widget->window->parent;
1240 cr = cairo_create(window->cairo_surface);
1241 cairo_text_extents(cr, tooltip->entry, &extents);
1242 cairo_destroy(cr);
1243
1244 return extents;
1245}
1246
1247static int
1248window_create_tooltip(struct tooltip *tooltip)
1249{
1250 struct widget *parent = tooltip->parent;
1251 struct display *display = parent->window->display;
1252 struct window *window;
1253 const int offset_y = 27;
1254 const int margin = 3;
1255 cairo_text_extents_t extents;
1256
1257 if (tooltip->widget)
1258 return 0;
1259
1260 window = window_create_transient(display, parent->window, tooltip->x,
1261 tooltip->y + offset_y,
1262 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1263 if (!window)
1264 return -1;
1265
1266 tooltip->window = window;
1267 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1268
1269 extents = get_text_extents(tooltip);
1270 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1271 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1272
1273 return 0;
1274}
1275
1276void
1277widget_destroy_tooltip(struct widget *parent)
1278{
1279 struct tooltip *tooltip = parent->tooltip;
1280
1281 parent->tooltip_count = 0;
1282 if (!tooltip)
1283 return;
1284
1285 if (tooltip->widget) {
1286 widget_destroy(tooltip->widget);
1287 window_destroy(tooltip->window);
1288 tooltip->widget = NULL;
1289 tooltip->window = NULL;
1290 }
1291
1292 close(tooltip->tooltip_fd);
1293 free(tooltip->entry);
1294 free(tooltip);
1295 parent->tooltip = NULL;
1296}
1297
1298static void
1299tooltip_func(struct task *task, uint32_t events)
1300{
1301 struct tooltip *tooltip =
1302 container_of(task, struct tooltip, tooltip_task);
1303 uint64_t exp;
1304
Martin Olsson8df662a2012-07-08 03:03:47 +02001305 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1306 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001307 window_create_tooltip(tooltip);
1308}
1309
1310#define TOOLTIP_TIMEOUT 500
1311static int
1312tooltip_timer_reset(struct tooltip *tooltip)
1313{
1314 struct itimerspec its;
1315
1316 its.it_interval.tv_sec = 0;
1317 its.it_interval.tv_nsec = 0;
1318 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1319 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1320 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1321 fprintf(stderr, "could not set timerfd\n: %m");
1322 return -1;
1323 }
1324
1325 return 0;
1326}
1327
1328int
1329widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1330{
1331 struct tooltip *tooltip = parent->tooltip;
1332
1333 parent->tooltip_count++;
1334 if (tooltip) {
1335 tooltip->x = x;
1336 tooltip->y = y;
1337 tooltip_timer_reset(tooltip);
1338 return 0;
1339 }
1340
1341 /* the handler might be triggered too fast via input device motion, so
1342 * we need this check here to make sure tooltip is fully initialized */
1343 if (parent->tooltip_count > 1)
1344 return 0;
1345
1346 tooltip = malloc(sizeof *tooltip);
1347 if (!tooltip)
1348 return -1;
1349
1350 parent->tooltip = tooltip;
1351 tooltip->parent = parent;
1352 tooltip->widget = NULL;
1353 tooltip->window = NULL;
1354 tooltip->x = x;
1355 tooltip->y = y;
1356 tooltip->entry = strdup(entry);
1357 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1358 if (tooltip->tooltip_fd < 0) {
1359 fprintf(stderr, "could not create timerfd\n: %m");
1360 return -1;
1361 }
1362
1363 tooltip->tooltip_task.run = tooltip_func;
1364 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1365 EPOLLIN, &tooltip->tooltip_task);
1366 tooltip_timer_reset(tooltip);
1367
1368 return 0;
1369}
1370
1371static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001372workspace_manager_state(void *data,
1373 struct workspace_manager *workspace_manager,
1374 uint32_t current,
1375 uint32_t count)
1376{
1377 struct display *display = data;
1378
1379 display->workspace = current;
1380 display->workspace_count = count;
1381}
1382
1383static const struct workspace_manager_listener workspace_manager_listener = {
1384 workspace_manager_state
1385};
1386
1387static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001388frame_resize_handler(struct widget *widget,
1389 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001390{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001391 struct frame *frame = data;
1392 struct widget *child = frame->child;
1393 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001394 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001395 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001396 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001397 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001398 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001399 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001400
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001401 switch (widget->window->type) {
1402 case TYPE_FULLSCREEN:
1403 decoration_width = 0;
1404 decoration_height = 0;
1405
1406 allocation.x = 0;
1407 allocation.y = 0;
1408 allocation.width = width;
1409 allocation.height = height;
1410 opaque_margin = 0;
1411
1412 wl_list_for_each(button, &frame->buttons_list, link)
1413 button->widget->opaque = 1;
1414 break;
1415 case TYPE_MAXIMIZED:
1416 decoration_width = t->width * 2;
1417 decoration_height = t->width + t->titlebar_height;
1418
1419 allocation.x = t->width;
1420 allocation.y = t->titlebar_height;
1421 allocation.width = width - decoration_width;
1422 allocation.height = height - decoration_height;
1423
1424 opaque_margin = 0;
1425
1426 wl_list_for_each(button, &frame->buttons_list, link)
1427 button->widget->opaque = 0;
1428 break;
1429 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001430 decoration_width = (t->width + t->margin) * 2;
1431 decoration_height = t->width +
1432 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001433
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001434 allocation.x = t->width + t->margin;
1435 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001436 allocation.width = width - decoration_width;
1437 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001438
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001439 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001440
1441 wl_list_for_each(button, &frame->buttons_list, link)
1442 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001443 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001444 }
1445
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001446 widget_set_allocation(child, allocation.x, allocation.y,
1447 allocation.width, allocation.height);
1448
1449 if (child->resize_handler)
1450 child->resize_handler(child,
1451 allocation.width,
1452 allocation.height,
1453 child->user_data);
1454
Scott Moreauf7e498c2012-05-14 11:39:29 -06001455 width = child->allocation.width + decoration_width;
1456 height = child->allocation.height + decoration_height;
1457
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001458 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1459
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001460 widget->window->input_region =
1461 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001462 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001463 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001464 shadow_margin, shadow_margin,
1465 width - 2 * shadow_margin,
1466 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001467 } else {
1468 wl_region_add(widget->window->input_region,
1469 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001470 }
1471
Scott Moreauf7e498c2012-05-14 11:39:29 -06001472 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001473
1474 if (child->opaque) {
1475 widget->window->opaque_region =
1476 wl_compositor_create_region(display->compositor);
1477 wl_region_add(widget->window->opaque_region,
1478 opaque_margin, opaque_margin,
1479 widget->allocation.width - 2 * opaque_margin,
1480 widget->allocation.height - 2 * opaque_margin);
1481 }
Martin Minarik1998b152012-05-10 02:04:35 +02001482
1483 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001484 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1485 x_l = t->width + shadow_margin;
1486 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001487 wl_list_for_each(button, &frame->buttons_list, link) {
1488 const int button_padding = 4;
1489 w = cairo_image_surface_get_width(button->icon);
1490 h = cairo_image_surface_get_height(button->icon);
1491
1492 if (button->decoration == FRAME_BUTTON_FANCY)
1493 w += 10;
1494
1495 if (button->align == FRAME_BUTTON_LEFT) {
1496 widget_set_allocation(button->widget,
1497 x_l, y , w + 1, h + 1);
1498 x_l += w;
1499 x_l += button_padding;
1500 } else {
1501 x_r -= w;
1502 widget_set_allocation(button->widget,
1503 x_r, y , w + 1, h + 1);
1504 x_r -= button_padding;
1505 }
1506 }
1507}
1508
1509static int
1510frame_button_enter_handler(struct widget *widget,
1511 struct input *input, float x, float y, void *data)
1512{
1513 struct frame_button *frame_button = data;
1514
1515 widget_schedule_redraw(frame_button->widget);
1516 frame_button->state = FRAME_BUTTON_OVER;
1517
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001518 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001519}
1520
1521static void
1522frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1523{
1524 struct frame_button *frame_button = data;
1525
1526 widget_schedule_redraw(frame_button->widget);
1527 frame_button->state = FRAME_BUTTON_DEFAULT;
1528}
1529
1530static void
1531frame_button_button_handler(struct widget *widget,
1532 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001533 uint32_t button,
1534 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001535{
1536 struct frame_button *frame_button = data;
1537 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001538 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001539
1540 if (button != BTN_LEFT)
1541 return;
1542
1543 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001544 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001545 frame_button->state = FRAME_BUTTON_ACTIVE;
1546 widget_schedule_redraw(frame_button->widget);
1547
1548 if (frame_button->type == FRAME_BUTTON_ICON)
1549 window_show_frame_menu(window, input, time);
1550 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001551 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001552 frame_button->state = FRAME_BUTTON_DEFAULT;
1553 widget_schedule_redraw(frame_button->widget);
1554 break;
1555 }
1556
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001557 if (!was_pressed)
1558 return;
1559
Martin Minarik1998b152012-05-10 02:04:35 +02001560 switch (frame_button->type) {
1561 case FRAME_BUTTON_CLOSE:
1562 if (window->close_handler)
1563 window->close_handler(window->parent,
1564 window->user_data);
1565 else
1566 display_exit(window->display);
1567 break;
1568 case FRAME_BUTTON_MINIMIZE:
1569 fprintf(stderr,"Minimize stub\n");
1570 break;
1571 case FRAME_BUTTON_MAXIMIZE:
1572 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1573 break;
1574 default:
1575 /* Unknown operation */
1576 break;
1577 }
1578}
1579
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001580static int
1581frame_button_motion_handler(struct widget *widget,
1582 struct input *input, uint32_t time,
1583 float x, float y, void *data)
1584{
1585 struct frame_button *frame_button = data;
1586 enum frame_button_pointer previous_button_state = frame_button->state;
1587
1588 /* only track state for a pressed button */
1589 if (input->grab != widget)
1590 return CURSOR_LEFT_PTR;
1591
1592 if (x > widget->allocation.x &&
1593 x < (widget->allocation.x + widget->allocation.width) &&
1594 y > widget->allocation.y &&
1595 y < (widget->allocation.y + widget->allocation.height)) {
1596 frame_button->state = FRAME_BUTTON_ACTIVE;
1597 } else {
1598 frame_button->state = FRAME_BUTTON_DEFAULT;
1599 }
1600
1601 if (frame_button->state != previous_button_state)
1602 widget_schedule_redraw(frame_button->widget);
1603
1604 return CURSOR_LEFT_PTR;
1605}
1606
Martin Minarik1998b152012-05-10 02:04:35 +02001607static void
1608frame_button_redraw_handler(struct widget *widget, void *data)
1609{
1610 struct frame_button *frame_button = data;
1611 cairo_t *cr;
1612 int width, height, x, y;
1613 struct window *window = widget->window;
1614
1615 x = widget->allocation.x;
1616 y = widget->allocation.y;
1617 width = widget->allocation.width;
1618 height = widget->allocation.height;
1619
1620 if (!width)
1621 return;
1622 if (!height)
1623 return;
1624 if (widget->opaque)
1625 return;
1626
1627 cr = cairo_create(window->cairo_surface);
1628
1629 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1630 cairo_set_line_width(cr, 1);
1631
1632 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1633 cairo_rectangle (cr, x, y, 25, 16);
1634
1635 cairo_stroke_preserve(cr);
1636
1637 switch (frame_button->state) {
1638 case FRAME_BUTTON_DEFAULT:
1639 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1640 break;
1641 case FRAME_BUTTON_OVER:
1642 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1643 break;
1644 case FRAME_BUTTON_ACTIVE:
1645 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1646 break;
1647 }
1648
1649 cairo_fill (cr);
1650
1651 x += 4;
1652 }
1653
1654 cairo_set_source_surface(cr, frame_button->icon, x, y);
1655 cairo_paint(cr);
1656
1657 cairo_destroy(cr);
1658}
1659
1660static struct widget *
1661frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1662 enum frame_button_align align, enum frame_button_decoration style)
1663{
1664 struct frame_button *frame_button;
1665 const char *icon = data;
1666
1667 frame_button = malloc (sizeof *frame_button);
1668 memset(frame_button, 0, sizeof *frame_button);
1669
1670 frame_button->icon = cairo_image_surface_create_from_png(icon);
1671 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1672 frame_button->frame = frame;
1673 frame_button->type = type;
1674 frame_button->align = align;
1675 frame_button->decoration = style;
1676
1677 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1678
1679 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1680 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1681 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1682 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001683 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001684 return frame_button->widget;
1685}
1686
1687static void
1688frame_button_destroy(struct frame_button *frame_button)
1689{
1690 widget_destroy(frame_button->widget);
1691 wl_list_remove(&frame_button->link);
1692 cairo_surface_destroy(frame_button->icon);
1693 free(frame_button);
1694
1695 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001696}
1697
1698static void
1699frame_redraw_handler(struct widget *widget, void *data)
1700{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001701 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001702 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001703 struct theme *t = window->display->theme;
1704 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001705
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001706 if (window->type == TYPE_FULLSCREEN)
1707 return;
1708
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001709 cr = cairo_create(window->cairo_surface);
1710
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001711 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001712 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001713 if (window->type == TYPE_MAXIMIZED)
1714 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001715 theme_render_frame(t, cr, widget->allocation.width,
1716 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001717
1718 cairo_destroy(cr);
1719}
1720
1721static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001722frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001723{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001724 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001725 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001726 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001727
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001728 location = theme_get_location(t, input->sx, input->sy,
1729 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001730 frame->widget->allocation.height,
1731 window->type == TYPE_MAXIMIZED ?
1732 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001733
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001734 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001735 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001736 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001737 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001738 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001739 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001740 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001741 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001742 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001743 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001744 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001745 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001746 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001747 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001748 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001749 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001750 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001751 case THEME_LOCATION_EXTERIOR:
1752 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001753 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001754 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001755 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001756}
1757
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001758static void
1759frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001760{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001761 struct display *display;
1762
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001763 switch (index) {
1764 case 0: /* close */
1765 if (window->close_handler)
1766 window->close_handler(window->parent,
1767 window->user_data);
1768 else
1769 display_exit(window->display);
1770 break;
1771 case 1: /* fullscreen */
1772 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001773 if (window->fullscreen_handler)
1774 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001775 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001776 case 2: /* move to workspace above */
1777 display = window->display;
1778 if (display->workspace > 0)
1779 workspace_manager_move_surface(display->workspace_manager,
1780 window->surface,
1781 display->workspace - 1);
1782 break;
1783 case 3: /* move to workspace below */
1784 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001785 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001786 workspace_manager_move_surface(display->workspace_manager,
1787 window->surface,
1788 display->workspace + 1);
1789 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001790 }
1791}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001792
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001793void
1794window_show_frame_menu(struct window *window,
1795 struct input *input, uint32_t time)
1796{
1797 int32_t x, y;
1798
1799 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001800 "Close", "Fullscreen",
1801 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001802 };
1803
1804 input_get_position(input, &x, &y);
1805 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001806 x - 10, y - 10, frame_menu_func, entries,
1807 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001808}
1809
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001810static int
1811frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001812 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001813{
1814 return frame_get_pointer_image_for_location(data, input);
1815}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001816
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001817static int
1818frame_motion_handler(struct widget *widget,
1819 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001820 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001821{
1822 return frame_get_pointer_image_for_location(data, input);
1823}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001824
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001825static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001826frame_button_handler(struct widget *widget,
1827 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001828 uint32_t button, enum wl_pointer_button_state state,
1829 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001830
1831{
1832 struct frame *frame = data;
1833 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001834 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001835 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001836
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001837 location = theme_get_location(display->theme, input->sx, input->sy,
1838 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001839 frame->widget->allocation.height,
1840 window->type == TYPE_MAXIMIZED ?
1841 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001842
Daniel Stone4dbadb12012-05-30 16:31:51 +01001843 if (window->display->shell && button == BTN_LEFT &&
1844 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001845 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001846 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001847 if (!window->shell_surface)
1848 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001849 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001850 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001851 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001852 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001853 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001854 case THEME_LOCATION_RESIZING_TOP:
1855 case THEME_LOCATION_RESIZING_BOTTOM:
1856 case THEME_LOCATION_RESIZING_LEFT:
1857 case THEME_LOCATION_RESIZING_RIGHT:
1858 case THEME_LOCATION_RESIZING_TOP_LEFT:
1859 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1860 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1861 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001862 if (!window->shell_surface)
1863 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001864 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001865
1866 if (!display->dpy) {
1867 /* If we're using shm, allocate a big
1868 pool to create buffers out of while
1869 we resize. We should probably base
1870 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001871 window->pool =
1872 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001873 }
1874
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001875 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001876 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001877 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001878 break;
1879 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001880 } else if (button == BTN_RIGHT &&
1881 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001882 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001883 }
1884}
1885
1886struct widget *
1887frame_create(struct window *window, void *data)
1888{
1889 struct frame *frame;
1890
1891 frame = malloc(sizeof *frame);
1892 memset(frame, 0, sizeof *frame);
1893
1894 frame->widget = window_add_widget(window, frame);
1895 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001896
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001897 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1898 widget_set_resize_handler(frame->widget, frame_resize_handler);
1899 widget_set_enter_handler(frame->widget, frame_enter_handler);
1900 widget_set_motion_handler(frame->widget, frame_motion_handler);
1901 widget_set_button_handler(frame->widget, frame_button_handler);
1902
Martin Minarik1998b152012-05-10 02:04:35 +02001903 /* Create empty list for frame buttons */
1904 wl_list_init(&frame->buttons_list);
1905
1906 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1907 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1908
1909 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1910 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1911
1912 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1913 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1914
1915 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1916 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1917
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001918 window->frame = frame;
1919
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001920 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001921}
1922
Kristian Høgsberga1627922012-06-20 17:30:03 -04001923void
1924frame_set_child_size(struct widget *widget, int child_width, int child_height)
1925{
1926 struct display *display = widget->window->display;
1927 struct theme *t = display->theme;
1928 int decoration_width, decoration_height;
1929 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001930 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001931
1932 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001933 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001934 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001935 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001936
1937 width = child_width + decoration_width;
1938 height = child_height + decoration_height;
1939 } else {
1940 width = child_width;
1941 height = child_height;
1942 }
1943
1944 window_schedule_resize(widget->window, width, height);
1945}
1946
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001947static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001948frame_destroy(struct frame *frame)
1949{
Martin Minarik1998b152012-05-10 02:04:35 +02001950 struct frame_button *button, *tmp;
1951
1952 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1953 frame_button_destroy(button);
1954
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001955 /* frame->child must be destroyed by the application */
1956 widget_destroy(frame->widget);
1957 free(frame);
1958}
1959
1960static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001961input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001962 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001963{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001964 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001965 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001966
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001967 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001968 return;
1969
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001970 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001971 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001972 widget = old;
1973 if (input->grab)
1974 widget = input->grab;
1975 if (widget->leave_handler)
1976 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001977 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001978 }
1979
1980 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001981 widget = focus;
1982 if (input->grab)
1983 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001984 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001985 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001986 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001987 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001988
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001989 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001990 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001991}
1992
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001993void
1994input_grab(struct input *input, struct widget *widget, uint32_t button)
1995{
1996 input->grab = widget;
1997 input->grab_button = button;
1998}
1999
2000void
2001input_ungrab(struct input *input)
2002{
2003 struct widget *widget;
2004
2005 input->grab = NULL;
2006 if (input->pointer_focus) {
2007 widget = widget_find_widget(input->pointer_focus->widget,
2008 input->sx, input->sy);
2009 input_set_focus_widget(input, widget, input->sx, input->sy);
2010 }
2011}
2012
2013static void
2014input_remove_pointer_focus(struct input *input)
2015{
2016 struct window *window = input->pointer_focus;
2017
2018 if (!window)
2019 return;
2020
2021 input_set_focus_widget(input, NULL, 0, 0);
2022
2023 input->pointer_focus = NULL;
2024 input->current_cursor = CURSOR_UNSET;
2025}
2026
2027static void
2028pointer_handle_enter(void *data, struct wl_pointer *pointer,
2029 uint32_t serial, struct wl_surface *surface,
2030 wl_fixed_t sx_w, wl_fixed_t sy_w)
2031{
2032 struct input *input = data;
2033 struct window *window;
2034 struct widget *widget;
2035 float sx = wl_fixed_to_double(sx_w);
2036 float sy = wl_fixed_to_double(sy_w);
2037
2038 if (!surface) {
2039 /* enter event for a window we've just destroyed */
2040 return;
2041 }
2042
2043 input->display->serial = serial;
2044 input->pointer_enter_serial = serial;
2045 input->pointer_focus = wl_surface_get_user_data(surface);
2046 window = input->pointer_focus;
2047
2048 if (window->pool) {
2049 shm_pool_destroy(window->pool);
2050 window->pool = NULL;
2051 /* Schedule a redraw to free the pool */
2052 window_schedule_redraw(window);
2053 }
2054
2055 input->sx = sx;
2056 input->sy = sy;
2057
2058 widget = widget_find_widget(window->widget, sx, sy);
2059 input_set_focus_widget(input, widget, sx, sy);
2060}
2061
2062static void
2063pointer_handle_leave(void *data, struct wl_pointer *pointer,
2064 uint32_t serial, struct wl_surface *surface)
2065{
2066 struct input *input = data;
2067
2068 input->display->serial = serial;
2069 input_remove_pointer_focus(input);
2070}
2071
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002072static void
Daniel Stone37816df2012-05-16 18:45:18 +01002073pointer_handle_motion(void *data, struct wl_pointer *pointer,
2074 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002075{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002076 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002077 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002078 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002079 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002080 float sx = wl_fixed_to_double(sx_w);
2081 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002082
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002083 input->sx = sx;
2084 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002085
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002086 if (!window)
2087 return;
2088
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002089 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002090 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002091 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002092 }
2093
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002094 if (input->grab)
2095 widget = input->grab;
2096 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002097 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002098 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002099 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002100 input, time, sx, sy,
2101 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002102
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002103 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002104}
2105
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002106static void
Daniel Stone37816df2012-05-16 18:45:18 +01002107pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002108 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002109{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002110 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002111 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002112 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002113
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002114 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002115 if (input->focus_widget && input->grab == NULL &&
2116 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002117 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002118
Neil Roberts6b28aad2012-01-23 19:11:18 +00002119 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002121 (*widget->button_handler)(widget,
2122 input, time,
2123 button, state,
2124 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002125
Daniel Stone4dbadb12012-05-30 16:31:51 +01002126 if (input->grab && input->grab_button == button &&
2127 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002128 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002129}
2130
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002131static void
Daniel Stone37816df2012-05-16 18:45:18 +01002132pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002133 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002134{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002135 struct input *input = data;
2136 struct widget *widget;
2137
2138 widget = input->focus_widget;
2139 if (input->grab)
2140 widget = input->grab;
2141 if (widget && widget->axis_handler)
2142 (*widget->axis_handler)(widget,
2143 input, time,
2144 axis, value,
2145 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002146}
2147
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002148static const struct wl_pointer_listener pointer_listener = {
2149 pointer_handle_enter,
2150 pointer_handle_leave,
2151 pointer_handle_motion,
2152 pointer_handle_button,
2153 pointer_handle_axis,
2154};
2155
2156static void
2157input_remove_keyboard_focus(struct input *input)
2158{
2159 struct window *window = input->keyboard_focus;
2160 struct itimerspec its;
2161
2162 its.it_interval.tv_sec = 0;
2163 its.it_interval.tv_nsec = 0;
2164 its.it_value.tv_sec = 0;
2165 its.it_value.tv_nsec = 0;
2166 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2167
2168 if (!window)
2169 return;
2170
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002171 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002172 if (window->keyboard_focus_handler)
2173 (*window->keyboard_focus_handler)(window, NULL,
2174 window->user_data);
2175
2176 input->keyboard_focus = NULL;
2177}
2178
2179static void
2180keyboard_repeat_func(struct task *task, uint32_t events)
2181{
2182 struct input *input =
2183 container_of(task, struct input, repeat_task);
2184 struct window *window = input->keyboard_focus;
2185 uint64_t exp;
2186
2187 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2188 /* If we change the timer between the fd becoming
2189 * readable and getting here, there'll be nothing to
2190 * read and we get EAGAIN. */
2191 return;
2192
2193 if (window && window->key_handler) {
2194 (*window->key_handler)(window, input, input->repeat_time,
2195 input->repeat_key, input->repeat_sym,
2196 WL_KEYBOARD_KEY_STATE_PRESSED,
2197 window->user_data);
2198 }
2199}
2200
2201static void
2202keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2203 uint32_t format, int fd, uint32_t size)
2204{
2205 struct input *input = data;
2206 char *map_str;
2207
2208 if (!data) {
2209 close(fd);
2210 return;
2211 }
2212
2213 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2214 close(fd);
2215 return;
2216 }
2217
2218 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2219 if (map_str == MAP_FAILED) {
2220 close(fd);
2221 return;
2222 }
2223
2224 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2225 map_str,
2226 XKB_KEYMAP_FORMAT_TEXT_V1,
2227 0);
2228 munmap(map_str, size);
2229 close(fd);
2230
2231 if (!input->xkb.keymap) {
2232 fprintf(stderr, "failed to compile keymap\n");
2233 return;
2234 }
2235
2236 input->xkb.state = xkb_state_new(input->xkb.keymap);
2237 if (!input->xkb.state) {
2238 fprintf(stderr, "failed to create XKB state\n");
2239 xkb_map_unref(input->xkb.keymap);
2240 input->xkb.keymap = NULL;
2241 return;
2242 }
2243
2244 input->xkb.control_mask =
2245 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2246 input->xkb.alt_mask =
2247 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2248 input->xkb.shift_mask =
2249 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2250}
2251
2252static void
2253keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2254 uint32_t serial, struct wl_surface *surface,
2255 struct wl_array *keys)
2256{
2257 struct input *input = data;
2258 struct window *window;
2259
2260 input->display->serial = serial;
2261 input->keyboard_focus = wl_surface_get_user_data(surface);
2262
2263 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002264 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002265 if (window->keyboard_focus_handler)
2266 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002267 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002268}
2269
2270static void
2271keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2272 uint32_t serial, struct wl_surface *surface)
2273{
2274 struct input *input = data;
2275
2276 input->display->serial = serial;
2277 input_remove_keyboard_focus(input);
2278}
2279
Scott Moreau210d0792012-03-22 10:47:01 -06002280static void
Daniel Stone37816df2012-05-16 18:45:18 +01002281keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002282 uint32_t serial, uint32_t time, uint32_t key,
2283 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002284{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002285 struct input *input = data;
2286 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002287 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002288 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002289 const xkb_keysym_t *syms;
2290 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002291 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002292
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002293 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002294 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002295 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002296 return;
2297
Daniel Stone97f68542012-05-30 16:32:01 +01002298 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002299
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002300 sym = XKB_KEY_NoSymbol;
2301 if (num_syms == 1)
2302 sym = syms[0];
2303
2304 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002305 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002306 window_set_maximized(window,
2307 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002308 } else if (sym == XKB_KEY_F11 &&
2309 window->fullscreen_handler &&
2310 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2311 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002312 } else if (sym == XKB_KEY_F4 &&
2313 input->modifiers == MOD_ALT_MASK &&
2314 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2315 if (window->close_handler)
2316 window->close_handler(window->parent,
2317 window->user_data);
2318 else
2319 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002320 } else if (window->key_handler) {
2321 (*window->key_handler)(window, input, time, key,
2322 sym, state, window->user_data);
2323 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002324
2325 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2326 key == input->repeat_key) {
2327 its.it_interval.tv_sec = 0;
2328 its.it_interval.tv_nsec = 0;
2329 its.it_value.tv_sec = 0;
2330 its.it_value.tv_nsec = 0;
2331 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2332 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2333 input->repeat_sym = sym;
2334 input->repeat_key = key;
2335 input->repeat_time = time;
2336 its.it_interval.tv_sec = 0;
2337 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2338 its.it_value.tv_sec = 0;
2339 its.it_value.tv_nsec = 400 * 1000 * 1000;
2340 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2341 }
2342}
2343
2344static void
Daniel Stone351eb612012-05-31 15:27:47 -04002345keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2346 uint32_t serial, uint32_t mods_depressed,
2347 uint32_t mods_latched, uint32_t mods_locked,
2348 uint32_t group)
2349{
2350 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002351 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002352
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002353 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2354 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002355 mask = xkb_state_serialize_mods(input->xkb.state,
2356 XKB_STATE_DEPRESSED |
2357 XKB_STATE_LATCHED);
2358 input->modifiers = 0;
2359 if (mask & input->xkb.control_mask)
2360 input->modifiers |= MOD_CONTROL_MASK;
2361 if (mask & input->xkb.alt_mask)
2362 input->modifiers |= MOD_ALT_MASK;
2363 if (mask & input->xkb.shift_mask)
2364 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002365}
2366
Daniel Stone37816df2012-05-16 18:45:18 +01002367static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002368 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002369 keyboard_handle_enter,
2370 keyboard_handle_leave,
2371 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002372 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002373};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002374
2375static void
Daniel Stone37816df2012-05-16 18:45:18 +01002376seat_handle_capabilities(void *data, struct wl_seat *seat,
2377 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002378{
Daniel Stone37816df2012-05-16 18:45:18 +01002379 struct input *input = data;
2380
2381 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2382 input->pointer = wl_seat_get_pointer(seat);
2383 wl_pointer_set_user_data(input->pointer, input);
2384 wl_pointer_add_listener(input->pointer, &pointer_listener,
2385 input);
2386 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2387 wl_pointer_destroy(input->pointer);
2388 input->pointer = NULL;
2389 }
2390
2391 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2392 input->keyboard = wl_seat_get_keyboard(seat);
2393 wl_keyboard_set_user_data(input->keyboard, input);
2394 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2395 input);
2396 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2397 wl_keyboard_destroy(input->keyboard);
2398 input->keyboard = NULL;
2399 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002400}
2401
Daniel Stone37816df2012-05-16 18:45:18 +01002402static const struct wl_seat_listener seat_listener = {
2403 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002404};
2405
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002406void
2407input_get_position(struct input *input, int32_t *x, int32_t *y)
2408{
2409 *x = input->sx;
2410 *y = input->sy;
2411}
2412
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002413struct display *
2414input_get_display(struct input *input)
2415{
2416 return input->display;
2417}
2418
Daniel Stone37816df2012-05-16 18:45:18 +01002419struct wl_seat *
2420input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002421{
Daniel Stone37816df2012-05-16 18:45:18 +01002422 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002423}
2424
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002425uint32_t
2426input_get_modifiers(struct input *input)
2427{
2428 return input->modifiers;
2429}
2430
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002431struct widget *
2432input_get_focus_widget(struct input *input)
2433{
2434 return input->focus_widget;
2435}
2436
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002437struct data_offer {
2438 struct wl_data_offer *offer;
2439 struct input *input;
2440 struct wl_array types;
2441 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002442
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002443 struct task io_task;
2444 int fd;
2445 data_func_t func;
2446 int32_t x, y;
2447 void *user_data;
2448};
2449
2450static void
2451data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2452{
2453 struct data_offer *offer = data;
2454 char **p;
2455
2456 p = wl_array_add(&offer->types, sizeof *p);
2457 *p = strdup(type);
2458}
2459
2460static const struct wl_data_offer_listener data_offer_listener = {
2461 data_offer_offer,
2462};
2463
2464static void
2465data_offer_destroy(struct data_offer *offer)
2466{
2467 char **p;
2468
2469 offer->refcount--;
2470 if (offer->refcount == 0) {
2471 wl_data_offer_destroy(offer->offer);
2472 for (p = offer->types.data; *p; p++)
2473 free(*p);
2474 wl_array_release(&offer->types);
2475 free(offer);
2476 }
2477}
2478
2479static void
2480data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002481 struct wl_data_device *data_device,
2482 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002483{
2484 struct data_offer *offer;
2485
2486 offer = malloc(sizeof *offer);
2487
2488 wl_array_init(&offer->types);
2489 offer->refcount = 1;
2490 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002491 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002492 wl_data_offer_add_listener(offer->offer,
2493 &data_offer_listener, offer);
2494}
2495
2496static void
2497data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002498 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002499 wl_fixed_t x_w, wl_fixed_t y_w,
2500 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002501{
2502 struct input *input = data;
2503 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002504 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002505 float x = wl_fixed_to_double(x_w);
2506 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002507 char **p;
2508
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002509 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002510 window = wl_surface_get_user_data(surface);
2511 input->pointer_focus = window;
2512
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002513 if (offer) {
2514 input->drag_offer = wl_data_offer_get_user_data(offer);
2515
2516 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2517 *p = NULL;
2518
2519 types_data = input->drag_offer->types.data;
2520 } else {
2521 input->drag_offer = NULL;
2522 types_data = NULL;
2523 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002524
2525 window = input->pointer_focus;
2526 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002527 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002528 window->user_data);
2529}
2530
2531static void
2532data_device_leave(void *data, struct wl_data_device *data_device)
2533{
2534 struct input *input = data;
2535
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002536 if (input->drag_offer) {
2537 data_offer_destroy(input->drag_offer);
2538 input->drag_offer = NULL;
2539 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002540}
2541
2542static void
2543data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002544 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002545{
2546 struct input *input = data;
2547 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002548 float x = wl_fixed_to_double(x_w);
2549 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002550 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002551
2552 input->sx = x;
2553 input->sy = y;
2554
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002555 if (input->drag_offer)
2556 types_data = input->drag_offer->types.data;
2557 else
2558 types_data = NULL;
2559
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002560 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002561 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002562 window->user_data);
2563}
2564
2565static void
2566data_device_drop(void *data, struct wl_data_device *data_device)
2567{
2568 struct input *input = data;
2569 struct window *window = input->pointer_focus;
2570
2571 if (window->drop_handler)
2572 window->drop_handler(window, input,
2573 input->sx, input->sy, window->user_data);
2574}
2575
2576static void
2577data_device_selection(void *data,
2578 struct wl_data_device *wl_data_device,
2579 struct wl_data_offer *offer)
2580{
2581 struct input *input = data;
2582 char **p;
2583
2584 if (input->selection_offer)
2585 data_offer_destroy(input->selection_offer);
2586
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002587 if (offer) {
2588 input->selection_offer = wl_data_offer_get_user_data(offer);
2589 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2590 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002591 } else {
2592 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002593 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002594}
2595
2596static const struct wl_data_device_listener data_device_listener = {
2597 data_device_data_offer,
2598 data_device_enter,
2599 data_device_leave,
2600 data_device_motion,
2601 data_device_drop,
2602 data_device_selection
2603};
2604
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002605static void
2606input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002607{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002608 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002609 struct wl_cursor *cursor;
2610 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002611
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002612 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002613 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002614 return;
2615
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002616 if (index >= (int) cursor->image_count) {
2617 fprintf(stderr, "cursor index out of range\n");
2618 return;
2619 }
2620
2621 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002622 buffer = wl_cursor_image_get_buffer(image);
2623 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002624 return;
2625
Kristian Høgsbergae277372012-08-01 09:41:08 -04002626 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002627 input->pointer_surface,
2628 image->hotspot_x, image->hotspot_y);
2629 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2630 wl_surface_damage(input->pointer_surface, 0, 0,
2631 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002632 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002633}
2634
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002635static const struct wl_callback_listener pointer_surface_listener;
2636
2637static void
2638pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2639 uint32_t time)
2640{
2641 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002642 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002643 int i;
2644
2645 if (callback) {
2646 assert(callback == input->cursor_frame_cb);
2647 wl_callback_destroy(callback);
2648 input->cursor_frame_cb = NULL;
2649 }
2650
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002651 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002652 wl_pointer_set_cursor(input->pointer,
2653 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002654 NULL, 0, 0);
2655 return;
2656 }
2657
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002658 if (input->current_cursor == CURSOR_UNSET)
2659 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002660 cursor = input->display->cursors[input->current_cursor];
2661 if (!cursor)
2662 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002663
2664 /* FIXME We don't have the current time on the first call so we set
2665 * the animation start to the time of the first frame callback. */
2666 if (time == 0)
2667 input->cursor_anim_start = 0;
2668 else if (input->cursor_anim_start == 0)
2669 input->cursor_anim_start = time;
2670
2671 if (time == 0 || input->cursor_anim_start == 0)
2672 i = 0;
2673 else
2674 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2675
Pekka Paalanenbc106382012-10-10 12:49:31 +03002676 if (cursor->image_count > 1) {
2677 input->cursor_frame_cb =
2678 wl_surface_frame(input->pointer_surface);
2679 wl_callback_add_listener(input->cursor_frame_cb,
2680 &pointer_surface_listener, input);
2681 }
2682
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002683 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002684}
2685
2686static const struct wl_callback_listener pointer_surface_listener = {
2687 pointer_surface_frame_callback
2688};
2689
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002690void
2691input_set_pointer_image(struct input *input, int pointer)
2692{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002693 int force = 0;
2694
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002695 if (!input->pointer)
2696 return;
2697
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002698 if (input->pointer_enter_serial > input->cursor_serial)
2699 force = 1;
2700
2701 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002702 return;
2703
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002704 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002705 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002706 if (!input->cursor_frame_cb)
2707 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002708 else if (force) {
2709 /* The current frame callback may be stuck if, for instance,
2710 * the set cursor request was processed by the server after
2711 * this client lost the focus. In this case the cursor surface
2712 * might not be mapped and the frame callback wouldn't ever
2713 * complete. Send a set_cursor and attach to try to map the
2714 * cursor surface again so that the callback will finish */
2715 input_set_pointer_image_index(input, 0);
2716 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002717}
2718
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002719struct wl_data_device *
2720input_get_data_device(struct input *input)
2721{
2722 return input->data_device;
2723}
2724
2725void
2726input_set_selection(struct input *input,
2727 struct wl_data_source *source, uint32_t time)
2728{
2729 wl_data_device_set_selection(input->data_device, source, time);
2730}
2731
2732void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002733input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002734{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002735 wl_data_offer_accept(input->drag_offer->offer,
2736 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002737}
2738
2739static void
2740offer_io_func(struct task *task, uint32_t events)
2741{
2742 struct data_offer *offer =
2743 container_of(task, struct data_offer, io_task);
2744 unsigned int len;
2745 char buffer[4096];
2746
2747 len = read(offer->fd, buffer, sizeof buffer);
2748 offer->func(buffer, len,
2749 offer->x, offer->y, offer->user_data);
2750
2751 if (len == 0) {
2752 close(offer->fd);
2753 data_offer_destroy(offer);
2754 }
2755}
2756
2757static void
2758data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2759 data_func_t func, void *user_data)
2760{
2761 int p[2];
2762
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002763 if (pipe2(p, O_CLOEXEC) == -1)
2764 return;
2765
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002766 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2767 close(p[1]);
2768
2769 offer->io_task.run = offer_io_func;
2770 offer->fd = p[0];
2771 offer->func = func;
2772 offer->refcount++;
2773 offer->user_data = user_data;
2774
2775 display_watch_fd(offer->input->display,
2776 offer->fd, EPOLLIN, &offer->io_task);
2777}
2778
2779void
2780input_receive_drag_data(struct input *input, const char *mime_type,
2781 data_func_t func, void *data)
2782{
2783 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2784 input->drag_offer->x = input->sx;
2785 input->drag_offer->y = input->sy;
2786}
2787
2788int
2789input_receive_selection_data(struct input *input, const char *mime_type,
2790 data_func_t func, void *data)
2791{
2792 char **p;
2793
2794 if (input->selection_offer == NULL)
2795 return -1;
2796
2797 for (p = input->selection_offer->types.data; *p; p++)
2798 if (strcmp(mime_type, *p) == 0)
2799 break;
2800
2801 if (*p == NULL)
2802 return -1;
2803
2804 data_offer_receive_data(input->selection_offer,
2805 mime_type, func, data);
2806 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002807}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002808
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002809int
2810input_receive_selection_data_to_fd(struct input *input,
2811 const char *mime_type, int fd)
2812{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002813 if (input->selection_offer)
2814 wl_data_offer_receive(input->selection_offer->offer,
2815 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002816
2817 return 0;
2818}
2819
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002820void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002821window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002822{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002823 if (!window->shell_surface)
2824 return;
2825
Daniel Stone37816df2012-05-16 18:45:18 +01002826 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002827}
2828
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002829static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002830idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002831{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002832 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002833
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002834 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002835 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002836 widget_set_allocation(widget,
2837 window->pending_allocation.x,
2838 window->pending_allocation.y,
2839 window->pending_allocation.width,
2840 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002841
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002842 if (window->input_region) {
2843 wl_region_destroy(window->input_region);
2844 window->input_region = NULL;
2845 }
2846
2847 if (window->opaque_region) {
2848 wl_region_destroy(window->opaque_region);
2849 window->opaque_region = NULL;
2850 }
2851
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002852 if (widget->resize_handler)
2853 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002854 widget->allocation.width,
2855 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002856 widget->user_data);
2857
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002858 if (window->allocation.width != widget->allocation.width ||
2859 window->allocation.height != widget->allocation.height) {
2860 window->allocation = widget->allocation;
2861 window_schedule_redraw(window);
2862 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002863}
2864
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002865void
2866window_schedule_resize(struct window *window, int width, int height)
2867{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002868 window->pending_allocation.x = 0;
2869 window->pending_allocation.y = 0;
2870 window->pending_allocation.width = width;
2871 window->pending_allocation.height = height;
2872
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002873 if (window->min_allocation.width == 0)
2874 window->min_allocation = window->pending_allocation;
2875 if (window->pending_allocation.width < window->min_allocation.width)
2876 window->pending_allocation.width = window->min_allocation.width;
2877 if (window->pending_allocation.height < window->min_allocation.height)
2878 window->pending_allocation.height = window->min_allocation.height;
2879
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002880 window->resize_needed = 1;
2881 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002882}
2883
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002884void
2885widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2886{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002887 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002888}
2889
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002890static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002891handle_ping(void *data, struct wl_shell_surface *shell_surface,
2892 uint32_t serial)
2893{
2894 wl_shell_surface_pong(shell_surface, serial);
2895}
2896
2897static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002898handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002899 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002900{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002901 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002902
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002903 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002904 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002905}
2906
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002907static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002908menu_destroy(struct menu *menu)
2909{
2910 widget_destroy(menu->widget);
2911 window_destroy(menu->window);
2912 free(menu);
2913}
2914
2915static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002916handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2917{
2918 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002919 struct menu *menu = window->widget->user_data;
2920
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002921 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002922 * device. Or just use wl_callback. And this really needs to
2923 * be a window vfunc that the menu can set. And we need the
2924 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002925
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002926 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002927 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002928 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002929}
2930
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002931static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002932 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002933 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002934 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002935};
2936
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002937void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002938window_get_allocation(struct window *window,
2939 struct rectangle *allocation)
2940{
2941 *allocation = window->allocation;
2942}
2943
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002944static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002945widget_redraw(struct widget *widget)
2946{
2947 struct widget *child;
2948
2949 if (widget->redraw_handler)
2950 widget->redraw_handler(widget, widget->user_data);
2951 wl_list_for_each(child, &widget->child_list, link)
2952 widget_redraw(child);
2953}
2954
2955static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002956frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2957{
2958 struct window *window = data;
2959
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002960 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002961 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002962 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002963 window->redraw_scheduled = 0;
2964 if (window->redraw_needed)
2965 window_schedule_redraw(window);
2966}
2967
2968static const struct wl_callback_listener listener = {
2969 frame_callback
2970};
2971
2972static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002973idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002974{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002975 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002976
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002977 if (window->resize_needed)
2978 idle_resize(window);
2979
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002980 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002981 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002982 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002983 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002984
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002985 window->frame_cb = wl_surface_frame(window->surface);
2986 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002987 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002988}
2989
2990void
2991window_schedule_redraw(struct window *window)
2992{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002993 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002994 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002995 window->redraw_task.run = idle_redraw;
2996 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002997 window->redraw_scheduled = 1;
2998 }
2999}
3000
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003001int
3002window_is_fullscreen(struct window *window)
3003{
3004 return window->type == TYPE_FULLSCREEN;
3005}
3006
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003007void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003008window_set_fullscreen(struct window *window, int fullscreen)
3009{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003010 if (!window->display->shell)
3011 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003012
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003013 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003014 return;
3015
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003016 if (fullscreen) {
3017 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003018 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003019 wl_shell_surface_set_fullscreen(window->shell_surface,
3020 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3021 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003022 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003023 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003024 wl_shell_surface_set_toplevel(window->shell_surface);
3025 window_schedule_resize(window,
3026 window->saved_allocation.width,
3027 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003028 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003029}
3030
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003031int
3032window_is_maximized(struct window *window)
3033{
3034 return window->type == TYPE_MAXIMIZED;
3035}
3036
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003037void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003038window_set_maximized(struct window *window, int maximized)
3039{
3040 if (!window->display->shell)
3041 return;
3042
3043 if ((window->type == TYPE_MAXIMIZED) == maximized)
3044 return;
3045
3046 if (window->type == TYPE_TOPLEVEL) {
3047 window->saved_allocation = window->allocation;
3048 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3049 window->type = TYPE_MAXIMIZED;
3050 } else {
3051 wl_shell_surface_set_toplevel(window->shell_surface);
3052 window->type = TYPE_TOPLEVEL;
3053 window_schedule_resize(window,
3054 window->saved_allocation.width,
3055 window->saved_allocation.height);
3056 }
3057}
3058
3059void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003060window_set_user_data(struct window *window, void *data)
3061{
3062 window->user_data = data;
3063}
3064
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003065void *
3066window_get_user_data(struct window *window)
3067{
3068 return window->user_data;
3069}
3070
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003071void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003072window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003073 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003074{
3075 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003076}
3077
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003078void
3079window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003080 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003081{
3082 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003083}
3084
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003085void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003086window_set_data_handler(struct window *window, window_data_handler_t handler)
3087{
3088 window->data_handler = handler;
3089}
3090
3091void
3092window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3093{
3094 window->drop_handler = handler;
3095}
3096
3097void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003098window_set_close_handler(struct window *window,
3099 window_close_handler_t handler)
3100{
3101 window->close_handler = handler;
3102}
3103
3104void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003105window_set_fullscreen_handler(struct window *window,
3106 window_fullscreen_handler_t handler)
3107{
3108 window->fullscreen_handler = handler;
3109}
3110
3111void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003112window_set_title(struct window *window, const char *title)
3113{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003114 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003115 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003116 if (window->shell_surface)
3117 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003118}
3119
3120const char *
3121window_get_title(struct window *window)
3122{
3123 return window->title;
3124}
3125
3126void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003127window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3128{
3129 struct text_cursor_position *text_cursor_position =
3130 window->display->text_cursor_position;
3131
Scott Moreau9295ce02012-06-01 12:46:10 -06003132 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003133 return;
3134
3135 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003136 window->surface,
3137 wl_fixed_from_int(x),
3138 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003139}
3140
3141void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003142window_damage(struct window *window, int32_t x, int32_t y,
3143 int32_t width, int32_t height)
3144{
3145 wl_surface_damage(window->surface, x, y, width, height);
3146}
3147
Casey Dahlin9074db52012-04-19 22:50:09 -04003148static void
3149surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003150 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003151{
Rob Bradford7507b572012-05-15 17:55:34 +01003152 struct window *window = data;
3153 struct output *output;
3154 struct output *output_found = NULL;
3155 struct window_output *window_output;
3156
3157 wl_list_for_each(output, &window->display->output_list, link) {
3158 if (output->output == wl_output) {
3159 output_found = output;
3160 break;
3161 }
3162 }
3163
3164 if (!output_found)
3165 return;
3166
3167 window_output = malloc (sizeof *window_output);
3168 window_output->output = output_found;
3169
3170 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003171}
3172
3173static void
3174surface_leave(void *data,
3175 struct wl_surface *wl_surface, struct wl_output *output)
3176{
Rob Bradford7507b572012-05-15 17:55:34 +01003177 struct window *window = data;
3178 struct window_output *window_output;
3179 struct window_output *window_output_found = NULL;
3180
3181 wl_list_for_each(window_output, &window->window_output_list, link) {
3182 if (window_output->output->output == output) {
3183 window_output_found = window_output;
3184 break;
3185 }
3186 }
3187
3188 if (window_output_found) {
3189 wl_list_remove(&window_output_found->link);
3190 free(window_output_found);
3191 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003192}
3193
3194static const struct wl_surface_listener surface_listener = {
3195 surface_enter,
3196 surface_leave
3197};
3198
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003199static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003200window_create_internal(struct display *display,
3201 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003202{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003203 struct window *window;
3204
3205 window = malloc(sizeof *window);
3206 if (window == NULL)
3207 return NULL;
3208
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003209 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003210 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003211 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003212 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003213 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003214 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003215 window->shell_surface =
3216 wl_shell_get_shell_surface(display->shell,
3217 window->surface);
3218 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003219 window->allocation.x = 0;
3220 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003221 window->allocation.width = 0;
3222 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003223 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003224 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003225 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003226 window->input_region = NULL;
3227 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003228
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003229 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003230#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003231 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003232#else
3233 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3234#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003235 else
3236 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003237
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003238 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003239 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003240 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003241
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003242 if (window->shell_surface) {
3243 wl_shell_surface_set_user_data(window->shell_surface, window);
3244 wl_shell_surface_add_listener(window->shell_surface,
3245 &shell_surface_listener, window);
3246 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003247
Rob Bradford7507b572012-05-15 17:55:34 +01003248 wl_list_init (&window->window_output_list);
3249
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003250 return window;
3251}
3252
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003253struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003254window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003255{
3256 struct window *window;
3257
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003258 window = window_create_internal(display, NULL, TYPE_NONE);
3259 if (!window)
3260 return NULL;
3261
3262 return window;
3263}
3264
3265struct window *
3266window_create_custom(struct display *display)
3267{
3268 struct window *window;
3269
3270 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003271 if (!window)
3272 return NULL;
3273
3274 return window;
3275}
3276
3277struct window *
3278window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003279 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003280{
3281 struct window *window;
3282
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003283 window = window_create_internal(parent->display,
3284 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003285 if (!window)
3286 return NULL;
3287
3288 window->x = x;
3289 window->y = y;
3290
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003291 if (display->shell)
3292 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003293 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003294 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003295
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003296 return window;
3297}
3298
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003299static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003300menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003301{
3302 int next;
3303
3304 next = (sy - 8) / 20;
3305 if (menu->current != next) {
3306 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003307 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003308 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003309}
3310
3311static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003312menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003313 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003314 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003315{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003316 struct menu *menu = data;
3317
3318 if (widget == menu->widget)
3319 menu_set_item(data, y);
3320
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003321 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003322}
3323
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003324static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003325menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003326 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003327{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003328 struct menu *menu = data;
3329
3330 if (widget == menu->widget)
3331 menu_set_item(data, y);
3332
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003333 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003334}
3335
3336static void
3337menu_leave_handler(struct widget *widget, struct input *input, void *data)
3338{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003339 struct menu *menu = data;
3340
3341 if (widget == menu->widget)
3342 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003343}
3344
3345static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003346menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003347 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003348 uint32_t button, enum wl_pointer_button_state state,
3349 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003350
3351{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003352 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003353
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003354 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3355 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003356 /* Either relase after press-drag-release or
3357 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003358 menu->func(menu->window->parent,
3359 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003360 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003361 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003362 }
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003363
3364 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
3365 menu->release_count++;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003366}
3367
3368static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003369menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003370{
3371 cairo_t *cr;
3372 const int32_t r = 3, margin = 3;
3373 struct menu *menu = data;
3374 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003375 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003376
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003377 cr = cairo_create(window->cairo_surface);
3378 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3379 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3380 cairo_paint(cr);
3381
3382 width = window->allocation.width;
3383 height = window->allocation.height;
3384 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003385
3386 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003387 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3388 cairo_fill(cr);
3389
3390 for (i = 0; i < menu->count; i++) {
3391 if (i == menu->current) {
3392 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3393 cairo_rectangle(cr, margin, i * 20 + margin,
3394 width - 2 * margin, 20);
3395 cairo_fill(cr);
3396 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3397 cairo_move_to(cr, 10, i * 20 + 16);
3398 cairo_show_text(cr, menu->entries[i]);
3399 } else {
3400 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3401 cairo_move_to(cr, 10, i * 20 + 16);
3402 cairo_show_text(cr, menu->entries[i]);
3403 }
3404 }
3405
3406 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003407}
3408
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003409void
3410window_show_menu(struct display *display,
3411 struct input *input, uint32_t time, struct window *parent,
3412 int32_t x, int32_t y,
3413 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003414{
3415 struct window *window;
3416 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003417 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003418
3419 menu = malloc(sizeof *menu);
3420 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003421 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003422
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003423 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003424 if (!window) {
3425 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003426 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003427 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003428
3429 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003430 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003431 menu->entries = entries;
3432 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003433 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003434 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003435 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003436 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003437 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003438 window->type = TYPE_MENU;
3439 window->x = x;
3440 window->y = y;
3441
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003442 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003443 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003444 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003445 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003446 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003447
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003448 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003449 widget_set_enter_handler(menu->widget, menu_enter_handler);
3450 widget_set_leave_handler(menu->widget, menu_leave_handler);
3451 widget_set_motion_handler(menu->widget, menu_motion_handler);
3452 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003453
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003454 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003455 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003456}
3457
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003458void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003459window_set_buffer_type(struct window *window, enum window_buffer_type type)
3460{
3461 window->buffer_type = type;
3462}
3463
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003464
3465static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003466display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003467 struct wl_output *wl_output,
3468 int x, int y,
3469 int physical_width,
3470 int physical_height,
3471 int subpixel,
3472 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003473 const char *model,
3474 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003475{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003476 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003477
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003478 output->allocation.x = x;
3479 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003480 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003481}
3482
3483static void
3484display_handle_mode(void *data,
3485 struct wl_output *wl_output,
3486 uint32_t flags,
3487 int width,
3488 int height,
3489 int refresh)
3490{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003491 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003492 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003493
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003494 if (flags & WL_OUTPUT_MODE_CURRENT) {
3495 output->allocation.width = width;
3496 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003497 if (display->output_configure_handler)
3498 (*display->output_configure_handler)(
3499 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003500 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003501}
3502
3503static const struct wl_output_listener output_listener = {
3504 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003505 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003506};
3507
3508static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003509display_add_output(struct display *d, uint32_t id)
3510{
3511 struct output *output;
3512
3513 output = malloc(sizeof *output);
3514 if (output == NULL)
3515 return;
3516
3517 memset(output, 0, sizeof *output);
3518 output->display = d;
3519 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003520 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003521 wl_list_insert(d->output_list.prev, &output->link);
3522
3523 wl_output_add_listener(output->output, &output_listener, output);
3524}
3525
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003526static void
3527output_destroy(struct output *output)
3528{
3529 if (output->destroy_handler)
3530 (*output->destroy_handler)(output, output->user_data);
3531
3532 wl_output_destroy(output->output);
3533 wl_list_remove(&output->link);
3534 free(output);
3535}
3536
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003537void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003538display_set_global_handler(struct display *display,
3539 display_global_handler_t handler)
3540{
3541 struct global *global;
3542
3543 display->global_handler = handler;
3544 if (!handler)
3545 return;
3546
3547 wl_list_for_each(global, &display->global_list, link)
3548 display->global_handler(display,
3549 global->name, global->interface,
3550 global->version, display->user_data);
3551}
3552
3553void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003554display_set_output_configure_handler(struct display *display,
3555 display_output_handler_t handler)
3556{
3557 struct output *output;
3558
3559 display->output_configure_handler = handler;
3560 if (!handler)
3561 return;
3562
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003563 wl_list_for_each(output, &display->output_list, link) {
3564 if (output->allocation.width == 0 &&
3565 output->allocation.height == 0)
3566 continue;
3567
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003568 (*display->output_configure_handler)(output,
3569 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003570 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003571}
3572
3573void
3574output_set_user_data(struct output *output, void *data)
3575{
3576 output->user_data = data;
3577}
3578
3579void *
3580output_get_user_data(struct output *output)
3581{
3582 return output->user_data;
3583}
3584
3585void
3586output_set_destroy_handler(struct output *output,
3587 display_output_handler_t handler)
3588{
3589 output->destroy_handler = handler;
3590 /* FIXME: implement this, once we have way to remove outputs */
3591}
3592
3593void
Scott Moreau4e072362012-09-29 02:03:11 -06003594output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003595{
Scott Moreau4e072362012-09-29 02:03:11 -06003596 struct rectangle allocation = output->allocation;
3597
3598 switch (output->transform) {
3599 case WL_OUTPUT_TRANSFORM_90:
3600 case WL_OUTPUT_TRANSFORM_270:
3601 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3602 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3603 /* Swap width and height */
3604 allocation.width = output->allocation.height;
3605 allocation.height = output->allocation.width;
3606 break;
3607 }
3608
3609 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003610}
3611
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003612struct wl_output *
3613output_get_wl_output(struct output *output)
3614{
3615 return output->output;
3616}
3617
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003618static void
Daniel Stone97f68542012-05-30 16:32:01 +01003619fini_xkb(struct input *input)
3620{
3621 xkb_state_unref(input->xkb.state);
3622 xkb_map_unref(input->xkb.keymap);
3623}
3624
3625static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003626display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003627{
3628 struct input *input;
3629
3630 input = malloc(sizeof *input);
3631 if (input == NULL)
3632 return;
3633
3634 memset(input, 0, sizeof *input);
3635 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003636 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003637 input->pointer_focus = NULL;
3638 input->keyboard_focus = NULL;
3639 wl_list_insert(d->input_list.prev, &input->link);
3640
Daniel Stone37816df2012-05-16 18:45:18 +01003641 wl_seat_add_listener(input->seat, &seat_listener, input);
3642 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003643
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003644 input->data_device =
3645 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003646 input->seat);
3647 wl_data_device_add_listener(input->data_device, &data_device_listener,
3648 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003649
3650 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003651
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003652 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3653 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003654 input->repeat_task.run = keyboard_repeat_func;
3655 display_watch_fd(d, input->repeat_timer_fd,
3656 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003657}
3658
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003659static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003660input_destroy(struct input *input)
3661{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003662 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003663 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003664
3665 if (input->drag_offer)
3666 data_offer_destroy(input->drag_offer);
3667
3668 if (input->selection_offer)
3669 data_offer_destroy(input->selection_offer);
3670
3671 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003672 fini_xkb(input);
3673
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003674 wl_surface_destroy(input->pointer_surface);
3675
Pekka Paalanene1207c72011-12-16 12:02:09 +02003676 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003677 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003678 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003679 free(input);
3680}
3681
3682static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003683init_workspace_manager(struct display *d, uint32_t id)
3684{
3685 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003686 wl_registry_bind(d->registry, id,
3687 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003688 if (d->workspace_manager != NULL)
3689 workspace_manager_add_listener(d->workspace_manager,
3690 &workspace_manager_listener,
3691 d);
3692}
3693
3694static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003695registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3696 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003697{
3698 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003699 struct global *global;
3700
3701 global = malloc(sizeof *global);
3702 global->name = id;
3703 global->interface = strdup(interface);
3704 global->version = version;
3705 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003706
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003707 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003708 d->compositor = wl_registry_bind(registry, id,
3709 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003710 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003711 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003712 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003713 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003714 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003715 d->shell = wl_registry_bind(registry,
3716 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003717 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003718 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003719 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3720 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003721 wl_registry_bind(registry, id,
3722 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003723 } else if (strcmp(interface, "text_cursor_position") == 0) {
3724 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003725 wl_registry_bind(registry, id,
3726 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003727 } else if (strcmp(interface, "workspace_manager") == 0) {
3728 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003729 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003730
3731 if (d->global_handler)
3732 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003733}
3734
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003735void *
3736display_bind(struct display *display, uint32_t name,
3737 const struct wl_interface *interface, uint32_t version)
3738{
3739 return wl_registry_bind(display->registry, name, interface, version);
3740}
3741
3742static const struct wl_registry_listener registry_listener = {
3743 registry_handle_global
3744};
3745
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003746#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003747static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003748init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003749{
3750 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003751 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003752
Rob Clark6396ed32012-03-11 19:48:41 -05003753#ifdef USE_CAIRO_GLESV2
3754# define GL_BIT EGL_OPENGL_ES2_BIT
3755#else
3756# define GL_BIT EGL_OPENGL_BIT
3757#endif
3758
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003759 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003760 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003761 EGL_RED_SIZE, 1,
3762 EGL_GREEN_SIZE, 1,
3763 EGL_BLUE_SIZE, 1,
3764 EGL_ALPHA_SIZE, 1,
3765 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003766 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003767 EGL_NONE
3768 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003769
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003770#ifdef USE_CAIRO_GLESV2
3771 static const EGLint context_attribs[] = {
3772 EGL_CONTEXT_CLIENT_VERSION, 2,
3773 EGL_NONE
3774 };
3775 EGLint api = EGL_OPENGL_ES_API;
3776#else
3777 EGLint *context_attribs = NULL;
3778 EGLint api = EGL_OPENGL_API;
3779#endif
3780
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003781 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003782 if (!eglInitialize(d->dpy, &major, &minor)) {
3783 fprintf(stderr, "failed to initialize display\n");
3784 return -1;
3785 }
3786
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003787 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003788 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3789 return -1;
3790 }
3791
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003792 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3793 &d->argb_config, 1, &n) || n != 1) {
3794 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003795 return -1;
3796 }
3797
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003798 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003799 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003800 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003801 fprintf(stderr, "failed to create context\n");
3802 return -1;
3803 }
3804
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003805 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003806 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003807 return -1;
3808 }
3809
Benjamin Franzke0c991632011-09-27 21:57:31 +02003810 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3811 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3812 fprintf(stderr, "failed to get cairo egl argb device\n");
3813 return -1;
3814 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003815
3816 return 0;
3817}
3818
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003819static void
3820fini_egl(struct display *display)
3821{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003822 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003823
3824 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3825 EGL_NO_CONTEXT);
3826
3827 eglTerminate(display->dpy);
3828 eglReleaseThread();
3829}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003830#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003831
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003832static void
3833handle_display_data(struct task *task, uint32_t events)
3834{
3835 struct display *display =
3836 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003837 struct epoll_event ep;
3838 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003839
3840 display->display_fd_events = events;
3841
3842 if (events & EPOLLERR || events & EPOLLHUP) {
3843 display_exit(display);
3844 return;
3845 }
3846
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04003847 if (events & EPOLLIN) {
3848 ret = wl_display_dispatch(display->display);
3849 if (ret == -1) {
3850 display_exit(display);
3851 return;
3852 }
3853 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003854
3855 if (events & EPOLLOUT) {
3856 ret = wl_display_flush(display->display);
3857 if (ret == 0) {
3858 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3859 ep.data.ptr = &display->display_task;
3860 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3861 display->display_fd, &ep);
3862 } else if (ret == -1 && errno != EAGAIN) {
3863 display_exit(display);
3864 return;
3865 }
3866 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003867}
3868
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003869struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003870display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003871{
3872 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003873
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003874 d = malloc(sizeof *d);
3875 if (d == NULL)
3876 return NULL;
3877
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003878 memset(d, 0, sizeof *d);
3879
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003880 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003881 if (d->display == NULL) {
3882 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003883 return NULL;
3884 }
3885
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003886 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003887 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003888 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003889 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3890 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003891
3892 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003893 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003894 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003895 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003896
Daniel Stone97f68542012-05-30 16:32:01 +01003897 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003898 if (d->xkb_context == NULL) {
3899 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003900 return NULL;
3901 }
3902
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003903 d->workspace = 0;
3904 d->workspace_count = 1;
3905
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003906 d->registry = wl_display_get_registry(d->display);
3907 wl_registry_add_listener(d->registry, &registry_listener, d);
3908 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003909#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003910 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003911 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003912#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003913
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003914 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003915
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003916 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003917
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003918 wl_list_init(&d->window_list);
3919
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003920 return d;
3921}
3922
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003923static void
3924display_destroy_outputs(struct display *display)
3925{
3926 struct output *tmp;
3927 struct output *output;
3928
3929 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3930 output_destroy(output);
3931}
3932
Pekka Paalanene1207c72011-12-16 12:02:09 +02003933static void
3934display_destroy_inputs(struct display *display)
3935{
3936 struct input *tmp;
3937 struct input *input;
3938
3939 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3940 input_destroy(input);
3941}
3942
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003943void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003944display_destroy(struct display *display)
3945{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003946 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003947 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3948 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003949
3950 if (!wl_list_empty(&display->deferred_list))
3951 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3952
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003953 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003954 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003955
Daniel Stone97f68542012-05-30 16:32:01 +01003956 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003957
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003958 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003959 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003960
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003961#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003962 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003963#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003964
Pekka Paalanenc2052982011-12-16 11:41:32 +02003965 if (display->shell)
3966 wl_shell_destroy(display->shell);
3967
3968 if (display->shm)
3969 wl_shm_destroy(display->shm);
3970
3971 if (display->data_device_manager)
3972 wl_data_device_manager_destroy(display->data_device_manager);
3973
3974 wl_compositor_destroy(display->compositor);
3975
3976 close(display->epoll_fd);
3977
U. Artie Eoff44874d92012-10-02 21:12:35 -07003978 if (!(display->display_fd_events & EPOLLERR) &&
3979 !(display->display_fd_events & EPOLLHUP))
3980 wl_display_flush(display->display);
3981
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003982 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003983 free(display);
3984}
3985
3986void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003987display_set_user_data(struct display *display, void *data)
3988{
3989 display->user_data = data;
3990}
3991
3992void *
3993display_get_user_data(struct display *display)
3994{
3995 return display->user_data;
3996}
3997
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003998struct wl_display *
3999display_get_display(struct display *display)
4000{
4001 return display->display;
4002}
4003
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004004struct output *
4005display_get_output(struct display *display)
4006{
4007 return container_of(display->output_list.next, struct output, link);
4008}
4009
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004010struct wl_compositor *
4011display_get_compositor(struct display *display)
4012{
4013 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004014}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004015
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004016uint32_t
4017display_get_serial(struct display *display)
4018{
4019 return display->serial;
4020}
4021
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004022EGLDisplay
4023display_get_egl_display(struct display *d)
4024{
4025 return d->dpy;
4026}
4027
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004028struct wl_data_source *
4029display_create_data_source(struct display *display)
4030{
4031 return wl_data_device_manager_create_data_source(display->data_device_manager);
4032}
4033
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004034EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004035display_get_argb_egl_config(struct display *d)
4036{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004037 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004038}
4039
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004040struct wl_shell *
4041display_get_shell(struct display *display)
4042{
4043 return display->shell;
4044}
4045
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004046int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004047display_acquire_window_surface(struct display *display,
4048 struct window *window,
4049 EGLContext ctx)
4050{
Benjamin Franzke22d54812011-07-16 19:50:32 +00004051#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004052 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004053 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004054
4055 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004056 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004057 device = cairo_surface_get_device(window->cairo_surface);
4058 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004059 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004060
Benjamin Franzke0c991632011-09-27 21:57:31 +02004061 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004062 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004063 ctx = display->argb_ctx;
4064 else
4065 assert(0);
4066 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004067
4068 data = cairo_surface_get_user_data(window->cairo_surface,
4069 &surface_data_key);
4070
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004071 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004072 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004073 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4074 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004075
4076 return 0;
4077#else
4078 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004079#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004080}
4081
4082void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004083display_release_window_surface(struct display *display,
4084 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004085{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004086#ifdef HAVE_CAIRO_EGL
4087 cairo_device_t *device;
4088
4089 device = cairo_surface_get_device(window->cairo_surface);
4090 if (!device)
4091 return;
4092
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004093 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004094 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004095 cairo_device_release(device);
4096#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004097}
4098
4099void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004100display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004101{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004102 wl_list_insert(&display->deferred_list, &task->link);
4103}
4104
4105void
4106display_watch_fd(struct display *display,
4107 int fd, uint32_t events, struct task *task)
4108{
4109 struct epoll_event ep;
4110
4111 ep.events = events;
4112 ep.data.ptr = task;
4113 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4114}
4115
4116void
4117display_run(struct display *display)
4118{
4119 struct task *task;
4120 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004121 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004122
Pekka Paalanen826d7952011-12-15 10:14:07 +02004123 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004124 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004125 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004126 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004127 struct task, link);
4128 wl_list_remove(&task->link);
4129 task->run(task, 0);
4130 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004131
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004132 wl_display_dispatch_pending(display->display);
4133
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004134 if (!display->running)
4135 break;
4136
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004137 ret = wl_display_flush(display->display);
4138 if (ret < 0 && errno == EAGAIN) {
4139 ep[0].events =
4140 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4141 ep[0].data.ptr = &display->display_task;
4142
4143 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4144 display->display_fd, &ep[0]);
4145 } else if (ret < 0) {
4146 break;
4147 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004148
4149 count = epoll_wait(display->epoll_fd,
4150 ep, ARRAY_LENGTH(ep), -1);
4151 for (i = 0; i < count; i++) {
4152 task = ep[i].data.ptr;
4153 task->run(task, ep[i].events);
4154 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004155 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004156}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004157
4158void
4159display_exit(struct display *display)
4160{
4161 display->running = 0;
4162}