blob: 18bacfc1263130cbf1c8dad0efe572ce44bca834 [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;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500351#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100353struct egl_window_surface_data {
354 struct display *display;
355 struct wl_surface *surface;
356 struct wl_egl_window *window;
357 EGLSurface surf;
358};
359
360static void
361egl_window_surface_data_destroy(void *p)
362{
363 struct egl_window_surface_data *data = p;
364 struct display *d = data->display;
365
366 eglDestroySurface(d->dpy, data->surf);
367 wl_egl_window_destroy(data->window);
368 data->surface = NULL;
369
370 free(p);
371}
372
373static cairo_surface_t *
374display_create_egl_window_surface(struct display *display,
375 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100377 struct rectangle *rectangle)
378{
379 cairo_surface_t *cairo_surface;
380 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400381 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200382 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383
384 data = malloc(sizeof *data);
385 if (data == NULL)
386 return NULL;
387
388 data->display = display;
389 data->surface = surface;
390
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500391 config = display->argb_config;
392 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400393
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400394 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100395 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400396 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100397
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400398 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500399 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100400
Benjamin Franzke0c991632011-09-27 21:57:31 +0200401 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100402 data->surf,
403 rectangle->width,
404 rectangle->height);
405
406 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
407 data, egl_window_surface_data_destroy);
408
409 return cairo_surface;
410}
411
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412#endif
413
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200414struct shm_surface_data {
415 struct wl_buffer *buffer;
416 struct shm_pool *pool;
417};
418
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419struct wl_buffer *
420display_get_buffer_for_surface(struct display *display,
421 cairo_surface_t *surface)
422{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200423 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424
425 data = cairo_surface_get_user_data (surface, &surface_data_key);
426
427 return data->buffer;
428}
429
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500430static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700431shm_pool_destroy(struct shm_pool *pool);
432
433static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400434shm_surface_data_destroy(void *p)
435{
436 struct shm_surface_data *data = p;
437
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200438 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700439 if (data->pool)
440 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300441
442 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400443}
444
Kristian Høgsberg16626282012-04-03 11:21:27 -0400445static struct wl_shm_pool *
446make_shm_pool(struct display *display, int size, void **data)
447{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400448 struct wl_shm_pool *pool;
449 int fd;
450
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300451 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400452 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300453 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
454 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400455 return NULL;
456 }
457
458 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400459 if (*data == MAP_FAILED) {
460 fprintf(stderr, "mmap failed: %m\n");
461 close(fd);
462 return NULL;
463 }
464
465 pool = wl_shm_create_pool(display->shm, fd, size);
466
467 close(fd);
468
469 return pool;
470}
471
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700472static struct shm_pool *
473shm_pool_create(struct display *display, size_t size)
474{
475 struct shm_pool *pool = malloc(sizeof *pool);
476
477 if (!pool)
478 return NULL;
479
480 pool->pool = make_shm_pool(display, size, &pool->data);
481 if (!pool->pool) {
482 free(pool);
483 return NULL;
484 }
485
486 pool->size = size;
487 pool->used = 0;
488
489 return pool;
490}
491
492static void *
493shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
494{
495 if (pool->used + size > pool->size)
496 return NULL;
497
498 *offset = pool->used;
499 pool->used += size;
500
501 return (char *) pool->data + *offset;
502}
503
504/* destroy the pool. this does not unmap the memory though */
505static void
506shm_pool_destroy(struct shm_pool *pool)
507{
508 munmap(pool->data, pool->size);
509 wl_shm_pool_destroy(pool->pool);
510 free(pool);
511}
512
513/* Start allocating from the beginning of the pool again */
514static void
515shm_pool_reset(struct shm_pool *pool)
516{
517 pool->used = 0;
518}
519
520static int
521data_length_for_shm_surface(struct rectangle *rect)
522{
523 int stride;
524
525 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
526 rect->width);
527 return stride * rect->height;
528}
529
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500530static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700531display_create_shm_surface_from_pool(struct display *display,
532 struct rectangle *rectangle,
533 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534{
535 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400536 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400537 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700538 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400539 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540
541 data = malloc(sizeof *data);
542 if (data == NULL)
543 return NULL;
544
545 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
546 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700547 length = stride * rectangle->height;
548 data->pool = NULL;
549 map = shm_pool_allocate(pool, length, &offset);
550
551 if (!map) {
552 free(data);
553 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400554 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400555
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400556 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557 CAIRO_FORMAT_ARGB32,
558 rectangle->width,
559 rectangle->height,
560 stride);
561
562 cairo_surface_set_user_data (surface, &surface_data_key,
563 data, shm_surface_data_destroy);
564
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400565 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500566 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400567 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500568 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400569
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200570 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
571 rectangle->width,
572 rectangle->height,
573 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400574
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700575 return surface;
576}
577
578static cairo_surface_t *
579display_create_shm_surface(struct display *display,
580 struct rectangle *rectangle, uint32_t flags,
581 struct window *window)
582{
583 struct shm_surface_data *data;
584 struct shm_pool *pool;
585 cairo_surface_t *surface;
586
587 if (window && window->pool) {
588 shm_pool_reset(window->pool);
589 surface = display_create_shm_surface_from_pool(display,
590 rectangle,
591 flags,
592 window->pool);
593 if (surface)
594 return surface;
595 }
596
597 pool = shm_pool_create(display,
598 data_length_for_shm_surface(rectangle));
599 if (!pool)
600 return NULL;
601
602 surface =
603 display_create_shm_surface_from_pool(display, rectangle,
604 flags, pool);
605
606 if (!surface) {
607 shm_pool_destroy(pool);
608 return NULL;
609 }
610
611 /* make sure we destroy the pool when the surface is destroyed */
612 data = cairo_surface_get_user_data(surface, &surface_data_key);
613 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614
615 return surface;
616}
617
nobled7b87cb02011-02-01 18:51:47 +0000618static int
619check_size(struct rectangle *rect)
620{
621 if (rect->width && rect->height)
622 return 0;
623
624 fprintf(stderr, "tried to create surface of "
625 "width: %d, height: %d\n", rect->width, rect->height);
626 return -1;
627}
628
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629cairo_surface_t *
630display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400632 struct rectangle *rectangle,
633 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634{
nobled7b87cb02011-02-01 18:51:47 +0000635 if (check_size(rectangle) < 0)
636 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500637#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300638 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400639 return display_create_egl_window_surface(display,
640 surface,
641 flags,
642 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400643#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400644 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400645}
646
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200647/*
648 * The following correspondences between file names and cursors was copied
649 * from: https://bugs.kde.org/attachment.cgi?id=67313
650 */
651
652static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300653 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200654 "sw-resize"
655};
656
657static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300658 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200659 "se-resize"
660};
661
662static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300663 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200664 "s-resize"
665};
666
667static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300668 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200669 "closedhand",
670 "208530c400c041818281048008011002"
671};
672
673static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300674 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200675 "default",
676 "top_left_arrow",
677 "left-arrow"
678};
679
680static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300681 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200682 "w-resize"
683};
684
685static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300686 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200687 "e-resize"
688};
689
690static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300691 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200692 "nw-resize"
693};
694
695static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300696 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200697 "ne-resize"
698};
699
700static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300701 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200702 "n-resize"
703};
704
705static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300706 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200707 "ibeam",
708 "text"
709};
710
711static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300712 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200713 "pointer",
714 "pointing_hand",
715 "e29285e634086352946a0e7090d73106"
716};
717
718static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400719 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200720 "wait",
721 "0426c94ea35c87780ff01dc239897213"
722};
723
724struct cursor_alternatives {
725 const char **names;
726 size_t count;
727};
728
729static const struct cursor_alternatives cursors[] = {
730 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
731 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
732 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
733 {grabbings, ARRAY_LENGTH(grabbings)},
734 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
735 {left_sides, ARRAY_LENGTH(left_sides)},
736 {right_sides, ARRAY_LENGTH(right_sides)},
737 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
738 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
739 {top_sides, ARRAY_LENGTH(top_sides)},
740 {xterms, ARRAY_LENGTH(xterms)},
741 {hand1s, ARRAY_LENGTH(hand1s)},
742 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300743};
744
745static void
746create_cursors(struct display *display)
747{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100748 char *config_file;
749 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200750 unsigned int i, j;
751 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100752 struct config_key shell_keys[] = {
753 { "cursor-theme", CONFIG_KEY_STRING, &theme },
754 };
755 struct config_section cs[] = {
756 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
757 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300758
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100759 config_file = config_file_path("weston.ini");
760 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
761 free(config_file);
762
763 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300764 display->cursors =
765 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
766
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300767 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200768 cursor = NULL;
769 for (j = 0; !cursor && j < cursors[i].count; ++j)
770 cursor = wl_cursor_theme_get_cursor(
771 display->cursor_theme, cursors[i].names[j]);
772
773 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300774 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200775 cursors[i].names[0]);
776
777 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300778 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300779}
780
781static void
782destroy_cursors(struct display *display)
783{
784 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800785 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300786}
787
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300788struct wl_cursor_image *
789display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400790{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200791 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400792
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300793 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400794}
795
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400796static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200797window_get_resize_dx_dy(struct window *window, int *x, int *y)
798{
799 if (window->resize_edges & WINDOW_RESIZING_LEFT)
800 *x = window->server_allocation.width - window->allocation.width;
801 else
802 *x = 0;
803
804 if (window->resize_edges & WINDOW_RESIZING_TOP)
805 *y = window->server_allocation.height -
806 window->allocation.height;
807 else
808 *y = 0;
809
810 window->resize_edges = 0;
811}
812
813static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500814window_attach_surface(struct window *window)
815{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400816 struct display *display = window->display;
817 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000818#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000820#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500821 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400823 if (window->type == TYPE_NONE) {
824 window->type = TYPE_TOPLEVEL;
825 if (display->shell)
826 wl_shell_surface_set_toplevel(window->shell_surface);
827 }
828
Pekka Paalanen512dde82012-10-10 12:49:27 +0300829 if (window->opaque_region) {
830 wl_surface_set_opaque_region(window->surface,
831 window->opaque_region);
832 wl_region_destroy(window->opaque_region);
833 window->opaque_region = NULL;
834 }
835
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300836 if (window->input_region) {
837 wl_surface_set_input_region(window->surface,
838 window->input_region);
839 wl_region_destroy(window->input_region);
840 window->input_region = NULL;
841 }
842
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000844#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
846 data = cairo_surface_get_user_data(window->cairo_surface,
847 &surface_data_key);
848
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 cairo_gl_surface_swapbuffers(window->cairo_surface);
850 wl_egl_window_get_attached_size(data->window,
851 &window->server_allocation.width,
852 &window->server_allocation.height);
853 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000854#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100855 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100856 buffer =
857 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400858 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200860 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400862 wl_surface_damage(window->surface, 0, 0,
863 window->allocation.width,
864 window->allocation.height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300865 wl_surface_commit(window->surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100866 window->server_allocation = window->allocation;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100867 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000868 default:
869 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100870 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500871}
872
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400873int
874window_has_focus(struct window *window)
875{
876 return window->focus_count > 0;
877}
878
Pekka Paalanena8d4c842012-11-19 15:32:48 +0200879static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400880window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500881{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400882 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100883 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500884}
885
Pekka Paalanence36f6d2012-11-19 15:32:47 +0200886static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400887window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400888{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500889 cairo_surface_reference(surface);
890
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400891 if (window->cairo_surface != NULL)
892 cairo_surface_destroy(window->cairo_surface);
893
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500894 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400895}
896
Benjamin Franzke22d54812011-07-16 19:50:32 +0000897#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898static void
899window_resize_cairo_window_surface(struct window *window)
900{
901 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200902 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903
904 data = cairo_surface_get_user_data(window->cairo_surface,
905 &surface_data_key);
906
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200907 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908 wl_egl_window_resize(data->window,
909 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200910 window->allocation.height,
911 x,y);
912
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100913 cairo_gl_surface_set_size(window->cairo_surface,
914 window->allocation.width,
915 window->allocation.height);
916}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000917#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100918
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400919struct display *
920window_get_display(struct window *window)
921{
922 return window->display;
923}
924
Pekka Paalanence36f6d2012-11-19 15:32:47 +0200925static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400926window_create_surface(struct window *window)
927{
928 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400929 uint32_t flags = 0;
930
931 if (!window->transparent)
932 flags = SURFACE_OPAQUE;
933
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400934 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500935#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100936 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
937 if (window->cairo_surface) {
938 window_resize_cairo_window_surface(window);
939 return;
940 }
941 surface = display_create_surface(window->display,
942 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400943 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100944 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945#endif
946 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400947 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400948 &window->allocation,
949 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400950 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800951 default:
952 surface = NULL;
953 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400954 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400955
956 window_set_surface(window, surface);
957 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400958}
959
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200960static void frame_destroy(struct frame *frame);
961
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400962void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500963window_destroy(struct window *window)
964{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200965 struct display *display = window->display;
966 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100967 struct window_output *window_output;
968 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200969
970 if (window->redraw_scheduled)
971 wl_list_remove(&window->redraw_task.link);
972
973 wl_list_for_each(input, &display->input_list, link) {
974 if (input->pointer_focus == window)
975 input->pointer_focus = NULL;
976 if (input->keyboard_focus == window)
977 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500978 if (input->focus_widget &&
979 input->focus_widget->window == window)
980 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200981 }
982
Rob Bradford7507b572012-05-15 17:55:34 +0100983 wl_list_for_each_safe(window_output, window_output_tmp,
984 &window->window_output_list, link) {
985 free (window_output);
986 }
987
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500988 if (window->input_region)
989 wl_region_destroy(window->input_region);
990 if (window->opaque_region)
991 wl_region_destroy(window->opaque_region);
992
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200993 if (window->frame)
994 frame_destroy(window->frame);
995
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200996 if (window->shell_surface)
997 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500998 wl_surface_destroy(window->surface);
999 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001000
1001 if (window->cairo_surface != NULL)
1002 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001003
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001004 if (window->frame_cb)
1005 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001006 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001007 free(window);
1008}
1009
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001010static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001011widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001012{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001013 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001014
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001015 wl_list_for_each(child, &widget->child_list, link) {
1016 target = widget_find_widget(child, x, y);
1017 if (target)
1018 return target;
1019 }
1020
1021 if (widget->allocation.x <= x &&
1022 x < widget->allocation.x + widget->allocation.width &&
1023 widget->allocation.y <= y &&
1024 y < widget->allocation.y + widget->allocation.height) {
1025 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001026 }
1027
1028 return NULL;
1029}
1030
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001031static struct widget *
1032widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001033{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001034 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001035
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001036 widget = malloc(sizeof *widget);
1037 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001038 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001039 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001040 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001041 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001042 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001043 widget->tooltip = NULL;
1044 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001045
1046 return widget;
1047}
1048
1049struct widget *
1050window_add_widget(struct window *window, void *data)
1051{
1052 window->widget = widget_create(window, data);
1053 wl_list_init(&window->widget->link);
1054
1055 return window->widget;
1056}
1057
1058struct widget *
1059widget_add_widget(struct widget *parent, void *data)
1060{
1061 struct widget *widget;
1062
1063 widget = widget_create(parent->window, data);
1064 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001065
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001066 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067}
1068
1069void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001070widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001072 struct display *display = widget->window->display;
1073 struct input *input;
1074
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001075 if (widget->tooltip) {
1076 free(widget->tooltip);
1077 widget->tooltip = NULL;
1078 }
1079
Pekka Paalanene156fb62012-01-19 13:51:38 +02001080 wl_list_for_each(input, &display->input_list, link) {
1081 if (input->focus_widget == widget)
1082 input->focus_widget = NULL;
1083 }
1084
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001085 wl_list_remove(&widget->link);
1086 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087}
1088
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001090widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
1095void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001096widget_set_size(struct widget *widget, int32_t width, int32_t height)
1097{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001098 widget->allocation.width = width;
1099 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001100}
1101
1102void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001103widget_set_allocation(struct widget *widget,
1104 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001105{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001106 widget->allocation.x = x;
1107 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001108 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001109}
1110
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001111void
1112widget_set_transparent(struct widget *widget, int transparent)
1113{
1114 widget->opaque = !transparent;
1115}
1116
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001117void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001118widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001119{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001120 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001121}
1122
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001123void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001124widget_set_resize_handler(struct widget *widget,
1125 widget_resize_handler_t handler)
1126{
1127 widget->resize_handler = handler;
1128}
1129
1130void
1131widget_set_redraw_handler(struct widget *widget,
1132 widget_redraw_handler_t handler)
1133{
1134 widget->redraw_handler = handler;
1135}
1136
1137void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001138widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001139{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001140 widget->enter_handler = handler;
1141}
1142
1143void
1144widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1145{
1146 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001147}
1148
1149void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001150widget_set_motion_handler(struct widget *widget,
1151 widget_motion_handler_t handler)
1152{
1153 widget->motion_handler = handler;
1154}
1155
1156void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001157widget_set_button_handler(struct widget *widget,
1158 widget_button_handler_t handler)
1159{
1160 widget->button_handler = handler;
1161}
1162
1163void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001164widget_set_axis_handler(struct widget *widget,
1165 widget_axis_handler_t handler)
1166{
1167 widget->axis_handler = handler;
1168}
1169
1170void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001171widget_schedule_redraw(struct widget *widget)
1172{
1173 window_schedule_redraw(widget->window);
1174}
1175
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001176cairo_surface_t *
1177window_get_surface(struct window *window)
1178{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001179 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001180}
1181
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001182struct wl_surface *
1183window_get_wl_surface(struct window *window)
1184{
1185 return window->surface;
1186}
1187
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001188struct wl_shell_surface *
1189window_get_wl_shell_surface(struct window *window)
1190{
1191 return window->shell_surface;
1192}
1193
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001194static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001195tooltip_redraw_handler(struct widget *widget, void *data)
1196{
1197 cairo_t *cr;
1198 const int32_t r = 3;
1199 struct tooltip *tooltip = data;
1200 int32_t width, height;
1201 struct window *window = widget->window;
1202
1203 cr = cairo_create(window->cairo_surface);
1204 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1205 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1206 cairo_paint(cr);
1207
1208 width = window->allocation.width;
1209 height = window->allocation.height;
1210 rounded_rect(cr, 0, 0, width, height, r);
1211
1212 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1213 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1214 cairo_fill(cr);
1215
1216 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1217 cairo_move_to(cr, 10, 16);
1218 cairo_show_text(cr, tooltip->entry);
1219 cairo_destroy(cr);
1220}
1221
1222static cairo_text_extents_t
1223get_text_extents(struct tooltip *tooltip)
1224{
1225 struct window *window;
1226 cairo_t *cr;
1227 cairo_text_extents_t extents;
1228
1229 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1230 * created yet */
1231 window = tooltip->widget->window->parent;
1232 cr = cairo_create(window->cairo_surface);
1233 cairo_text_extents(cr, tooltip->entry, &extents);
1234 cairo_destroy(cr);
1235
1236 return extents;
1237}
1238
1239static int
1240window_create_tooltip(struct tooltip *tooltip)
1241{
1242 struct widget *parent = tooltip->parent;
1243 struct display *display = parent->window->display;
1244 struct window *window;
1245 const int offset_y = 27;
1246 const int margin = 3;
1247 cairo_text_extents_t extents;
1248
1249 if (tooltip->widget)
1250 return 0;
1251
1252 window = window_create_transient(display, parent->window, tooltip->x,
1253 tooltip->y + offset_y,
1254 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1255 if (!window)
1256 return -1;
1257
1258 tooltip->window = window;
1259 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1260
1261 extents = get_text_extents(tooltip);
1262 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1263 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1264
1265 return 0;
1266}
1267
1268void
1269widget_destroy_tooltip(struct widget *parent)
1270{
1271 struct tooltip *tooltip = parent->tooltip;
1272
1273 parent->tooltip_count = 0;
1274 if (!tooltip)
1275 return;
1276
1277 if (tooltip->widget) {
1278 widget_destroy(tooltip->widget);
1279 window_destroy(tooltip->window);
1280 tooltip->widget = NULL;
1281 tooltip->window = NULL;
1282 }
1283
1284 close(tooltip->tooltip_fd);
1285 free(tooltip->entry);
1286 free(tooltip);
1287 parent->tooltip = NULL;
1288}
1289
1290static void
1291tooltip_func(struct task *task, uint32_t events)
1292{
1293 struct tooltip *tooltip =
1294 container_of(task, struct tooltip, tooltip_task);
1295 uint64_t exp;
1296
Martin Olsson8df662a2012-07-08 03:03:47 +02001297 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1298 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001299 window_create_tooltip(tooltip);
1300}
1301
1302#define TOOLTIP_TIMEOUT 500
1303static int
1304tooltip_timer_reset(struct tooltip *tooltip)
1305{
1306 struct itimerspec its;
1307
1308 its.it_interval.tv_sec = 0;
1309 its.it_interval.tv_nsec = 0;
1310 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1311 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1312 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1313 fprintf(stderr, "could not set timerfd\n: %m");
1314 return -1;
1315 }
1316
1317 return 0;
1318}
1319
1320int
1321widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1322{
1323 struct tooltip *tooltip = parent->tooltip;
1324
1325 parent->tooltip_count++;
1326 if (tooltip) {
1327 tooltip->x = x;
1328 tooltip->y = y;
1329 tooltip_timer_reset(tooltip);
1330 return 0;
1331 }
1332
1333 /* the handler might be triggered too fast via input device motion, so
1334 * we need this check here to make sure tooltip is fully initialized */
1335 if (parent->tooltip_count > 1)
1336 return 0;
1337
1338 tooltip = malloc(sizeof *tooltip);
1339 if (!tooltip)
1340 return -1;
1341
1342 parent->tooltip = tooltip;
1343 tooltip->parent = parent;
1344 tooltip->widget = NULL;
1345 tooltip->window = NULL;
1346 tooltip->x = x;
1347 tooltip->y = y;
1348 tooltip->entry = strdup(entry);
1349 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1350 if (tooltip->tooltip_fd < 0) {
1351 fprintf(stderr, "could not create timerfd\n: %m");
1352 return -1;
1353 }
1354
1355 tooltip->tooltip_task.run = tooltip_func;
1356 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1357 EPOLLIN, &tooltip->tooltip_task);
1358 tooltip_timer_reset(tooltip);
1359
1360 return 0;
1361}
1362
1363static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001364workspace_manager_state(void *data,
1365 struct workspace_manager *workspace_manager,
1366 uint32_t current,
1367 uint32_t count)
1368{
1369 struct display *display = data;
1370
1371 display->workspace = current;
1372 display->workspace_count = count;
1373}
1374
1375static const struct workspace_manager_listener workspace_manager_listener = {
1376 workspace_manager_state
1377};
1378
1379static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001380frame_resize_handler(struct widget *widget,
1381 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001382{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001383 struct frame *frame = data;
1384 struct widget *child = frame->child;
1385 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001386 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001387 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001388 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001389 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001390 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001391 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001392
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001393 switch (widget->window->type) {
1394 case TYPE_FULLSCREEN:
1395 decoration_width = 0;
1396 decoration_height = 0;
1397
1398 allocation.x = 0;
1399 allocation.y = 0;
1400 allocation.width = width;
1401 allocation.height = height;
1402 opaque_margin = 0;
1403
1404 wl_list_for_each(button, &frame->buttons_list, link)
1405 button->widget->opaque = 1;
1406 break;
1407 case TYPE_MAXIMIZED:
1408 decoration_width = t->width * 2;
1409 decoration_height = t->width + t->titlebar_height;
1410
1411 allocation.x = t->width;
1412 allocation.y = t->titlebar_height;
1413 allocation.width = width - decoration_width;
1414 allocation.height = height - decoration_height;
1415
1416 opaque_margin = 0;
1417
1418 wl_list_for_each(button, &frame->buttons_list, link)
1419 button->widget->opaque = 0;
1420 break;
1421 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001422 decoration_width = (t->width + t->margin) * 2;
1423 decoration_height = t->width +
1424 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001425
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001426 allocation.x = t->width + t->margin;
1427 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001428 allocation.width = width - decoration_width;
1429 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001430
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001431 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001432
1433 wl_list_for_each(button, &frame->buttons_list, link)
1434 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001435 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001436 }
1437
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001438 widget_set_allocation(child, allocation.x, allocation.y,
1439 allocation.width, allocation.height);
1440
1441 if (child->resize_handler)
1442 child->resize_handler(child,
1443 allocation.width,
1444 allocation.height,
1445 child->user_data);
1446
Scott Moreauf7e498c2012-05-14 11:39:29 -06001447 width = child->allocation.width + decoration_width;
1448 height = child->allocation.height + decoration_height;
1449
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001450 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1451
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001452 widget->window->input_region =
1453 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001454 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001455 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001456 shadow_margin, shadow_margin,
1457 width - 2 * shadow_margin,
1458 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001459 } else {
1460 wl_region_add(widget->window->input_region,
1461 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001462 }
1463
Scott Moreauf7e498c2012-05-14 11:39:29 -06001464 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001465
1466 if (child->opaque) {
1467 widget->window->opaque_region =
1468 wl_compositor_create_region(display->compositor);
1469 wl_region_add(widget->window->opaque_region,
1470 opaque_margin, opaque_margin,
1471 widget->allocation.width - 2 * opaque_margin,
1472 widget->allocation.height - 2 * opaque_margin);
1473 }
Martin Minarik1998b152012-05-10 02:04:35 +02001474
1475 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001476 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1477 x_l = t->width + shadow_margin;
1478 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001479 wl_list_for_each(button, &frame->buttons_list, link) {
1480 const int button_padding = 4;
1481 w = cairo_image_surface_get_width(button->icon);
1482 h = cairo_image_surface_get_height(button->icon);
1483
1484 if (button->decoration == FRAME_BUTTON_FANCY)
1485 w += 10;
1486
1487 if (button->align == FRAME_BUTTON_LEFT) {
1488 widget_set_allocation(button->widget,
1489 x_l, y , w + 1, h + 1);
1490 x_l += w;
1491 x_l += button_padding;
1492 } else {
1493 x_r -= w;
1494 widget_set_allocation(button->widget,
1495 x_r, y , w + 1, h + 1);
1496 x_r -= button_padding;
1497 }
1498 }
1499}
1500
1501static int
1502frame_button_enter_handler(struct widget *widget,
1503 struct input *input, float x, float y, void *data)
1504{
1505 struct frame_button *frame_button = data;
1506
1507 widget_schedule_redraw(frame_button->widget);
1508 frame_button->state = FRAME_BUTTON_OVER;
1509
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001510 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001511}
1512
1513static void
1514frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1515{
1516 struct frame_button *frame_button = data;
1517
1518 widget_schedule_redraw(frame_button->widget);
1519 frame_button->state = FRAME_BUTTON_DEFAULT;
1520}
1521
1522static void
1523frame_button_button_handler(struct widget *widget,
1524 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001525 uint32_t button,
1526 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001527{
1528 struct frame_button *frame_button = data;
1529 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001530 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001531
1532 if (button != BTN_LEFT)
1533 return;
1534
1535 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001536 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001537 frame_button->state = FRAME_BUTTON_ACTIVE;
1538 widget_schedule_redraw(frame_button->widget);
1539
1540 if (frame_button->type == FRAME_BUTTON_ICON)
1541 window_show_frame_menu(window, input, time);
1542 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001543 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001544 frame_button->state = FRAME_BUTTON_DEFAULT;
1545 widget_schedule_redraw(frame_button->widget);
1546 break;
1547 }
1548
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001549 if (!was_pressed)
1550 return;
1551
Martin Minarik1998b152012-05-10 02:04:35 +02001552 switch (frame_button->type) {
1553 case FRAME_BUTTON_CLOSE:
1554 if (window->close_handler)
1555 window->close_handler(window->parent,
1556 window->user_data);
1557 else
1558 display_exit(window->display);
1559 break;
1560 case FRAME_BUTTON_MINIMIZE:
1561 fprintf(stderr,"Minimize stub\n");
1562 break;
1563 case FRAME_BUTTON_MAXIMIZE:
1564 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1565 break;
1566 default:
1567 /* Unknown operation */
1568 break;
1569 }
1570}
1571
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001572static int
1573frame_button_motion_handler(struct widget *widget,
1574 struct input *input, uint32_t time,
1575 float x, float y, void *data)
1576{
1577 struct frame_button *frame_button = data;
1578 enum frame_button_pointer previous_button_state = frame_button->state;
1579
1580 /* only track state for a pressed button */
1581 if (input->grab != widget)
1582 return CURSOR_LEFT_PTR;
1583
1584 if (x > widget->allocation.x &&
1585 x < (widget->allocation.x + widget->allocation.width) &&
1586 y > widget->allocation.y &&
1587 y < (widget->allocation.y + widget->allocation.height)) {
1588 frame_button->state = FRAME_BUTTON_ACTIVE;
1589 } else {
1590 frame_button->state = FRAME_BUTTON_DEFAULT;
1591 }
1592
1593 if (frame_button->state != previous_button_state)
1594 widget_schedule_redraw(frame_button->widget);
1595
1596 return CURSOR_LEFT_PTR;
1597}
1598
Martin Minarik1998b152012-05-10 02:04:35 +02001599static void
1600frame_button_redraw_handler(struct widget *widget, void *data)
1601{
1602 struct frame_button *frame_button = data;
1603 cairo_t *cr;
1604 int width, height, x, y;
1605 struct window *window = widget->window;
1606
1607 x = widget->allocation.x;
1608 y = widget->allocation.y;
1609 width = widget->allocation.width;
1610 height = widget->allocation.height;
1611
1612 if (!width)
1613 return;
1614 if (!height)
1615 return;
1616 if (widget->opaque)
1617 return;
1618
1619 cr = cairo_create(window->cairo_surface);
1620
1621 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1622 cairo_set_line_width(cr, 1);
1623
1624 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1625 cairo_rectangle (cr, x, y, 25, 16);
1626
1627 cairo_stroke_preserve(cr);
1628
1629 switch (frame_button->state) {
1630 case FRAME_BUTTON_DEFAULT:
1631 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1632 break;
1633 case FRAME_BUTTON_OVER:
1634 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1635 break;
1636 case FRAME_BUTTON_ACTIVE:
1637 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1638 break;
1639 }
1640
1641 cairo_fill (cr);
1642
1643 x += 4;
1644 }
1645
1646 cairo_set_source_surface(cr, frame_button->icon, x, y);
1647 cairo_paint(cr);
1648
1649 cairo_destroy(cr);
1650}
1651
1652static struct widget *
1653frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1654 enum frame_button_align align, enum frame_button_decoration style)
1655{
1656 struct frame_button *frame_button;
1657 const char *icon = data;
1658
1659 frame_button = malloc (sizeof *frame_button);
1660 memset(frame_button, 0, sizeof *frame_button);
1661
1662 frame_button->icon = cairo_image_surface_create_from_png(icon);
1663 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1664 frame_button->frame = frame;
1665 frame_button->type = type;
1666 frame_button->align = align;
1667 frame_button->decoration = style;
1668
1669 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1670
1671 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1672 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1673 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1674 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001675 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001676 return frame_button->widget;
1677}
1678
1679static void
1680frame_button_destroy(struct frame_button *frame_button)
1681{
1682 widget_destroy(frame_button->widget);
1683 wl_list_remove(&frame_button->link);
1684 cairo_surface_destroy(frame_button->icon);
1685 free(frame_button);
1686
1687 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001688}
1689
1690static void
1691frame_redraw_handler(struct widget *widget, void *data)
1692{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001693 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001694 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001695 struct theme *t = window->display->theme;
1696 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001697
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001698 if (window->type == TYPE_FULLSCREEN)
1699 return;
1700
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001701 cr = cairo_create(window->cairo_surface);
1702
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001703 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001704 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001705 if (window->type == TYPE_MAXIMIZED)
1706 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001707 theme_render_frame(t, cr, widget->allocation.width,
1708 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001709
1710 cairo_destroy(cr);
1711}
1712
1713static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001714frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001715{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001716 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001717 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001718 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001719
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001720 location = theme_get_location(t, input->sx, input->sy,
1721 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001722 frame->widget->allocation.height,
1723 window->type == TYPE_MAXIMIZED ?
1724 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001725
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001726 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001727 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001728 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001729 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001730 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001731 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001732 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001733 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001734 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001735 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001736 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001737 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001738 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001739 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001740 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001741 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001742 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001743 case THEME_LOCATION_EXTERIOR:
1744 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001745 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001746 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001747 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001748}
1749
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001750static void
1751frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001752{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001753 struct display *display;
1754
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001755 switch (index) {
1756 case 0: /* close */
1757 if (window->close_handler)
1758 window->close_handler(window->parent,
1759 window->user_data);
1760 else
1761 display_exit(window->display);
1762 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001763 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001764 display = window->display;
1765 if (display->workspace > 0)
1766 workspace_manager_move_surface(display->workspace_manager,
1767 window->surface,
1768 display->workspace - 1);
1769 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001770 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001771 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001772 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001773 workspace_manager_move_surface(display->workspace_manager,
1774 window->surface,
1775 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05001776 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001777 case 3: /* fullscreen */
1778 /* we don't have a way to get out of fullscreen for now */
1779 if (window->fullscreen_handler)
1780 window->fullscreen_handler(window, window->user_data);
1781 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001782 }
1783}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001784
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001785void
1786window_show_frame_menu(struct window *window,
1787 struct input *input, uint32_t time)
1788{
1789 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001790 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001791
1792 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001793 "Close",
1794 "Move to workspace above", "Move to workspace below",
1795 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001796 };
1797
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001798 if (window->fullscreen_handler)
1799 count = ARRAY_LENGTH(entries);
1800 else
1801 count = ARRAY_LENGTH(entries) - 1;
1802
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001803 input_get_position(input, &x, &y);
1804 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04001805 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001806}
1807
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001808static int
1809frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001810 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001811{
1812 return frame_get_pointer_image_for_location(data, input);
1813}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001814
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001815static int
1816frame_motion_handler(struct widget *widget,
1817 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001818 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001819{
1820 return frame_get_pointer_image_for_location(data, input);
1821}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001822
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001823static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001824frame_button_handler(struct widget *widget,
1825 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001826 uint32_t button, enum wl_pointer_button_state state,
1827 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001828
1829{
1830 struct frame *frame = data;
1831 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001832 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001833 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001834
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001835 location = theme_get_location(display->theme, input->sx, input->sy,
1836 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001837 frame->widget->allocation.height,
1838 window->type == TYPE_MAXIMIZED ?
1839 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001840
Daniel Stone4dbadb12012-05-30 16:31:51 +01001841 if (window->display->shell && button == BTN_LEFT &&
1842 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001843 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001844 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001845 if (!window->shell_surface)
1846 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001847 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001848 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001849 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001850 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001851 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001852 case THEME_LOCATION_RESIZING_TOP:
1853 case THEME_LOCATION_RESIZING_BOTTOM:
1854 case THEME_LOCATION_RESIZING_LEFT:
1855 case THEME_LOCATION_RESIZING_RIGHT:
1856 case THEME_LOCATION_RESIZING_TOP_LEFT:
1857 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1858 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1859 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001860 if (!window->shell_surface)
1861 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001862 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001863
1864 if (!display->dpy) {
1865 /* If we're using shm, allocate a big
1866 pool to create buffers out of while
1867 we resize. We should probably base
1868 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001869 window->pool =
1870 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001871 }
1872
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001873 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001874 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001875 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001876 break;
1877 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001878 } else if (button == BTN_RIGHT &&
1879 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001880 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001881 }
1882}
1883
1884struct widget *
1885frame_create(struct window *window, void *data)
1886{
1887 struct frame *frame;
1888
1889 frame = malloc(sizeof *frame);
1890 memset(frame, 0, sizeof *frame);
1891
1892 frame->widget = window_add_widget(window, frame);
1893 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001894
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001895 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1896 widget_set_resize_handler(frame->widget, frame_resize_handler);
1897 widget_set_enter_handler(frame->widget, frame_enter_handler);
1898 widget_set_motion_handler(frame->widget, frame_motion_handler);
1899 widget_set_button_handler(frame->widget, frame_button_handler);
1900
Martin Minarik1998b152012-05-10 02:04:35 +02001901 /* Create empty list for frame buttons */
1902 wl_list_init(&frame->buttons_list);
1903
1904 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1905 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1906
1907 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1908 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1909
1910 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1911 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1912
1913 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1914 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1915
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001916 window->frame = frame;
1917
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001918 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001919}
1920
Kristian Høgsberga1627922012-06-20 17:30:03 -04001921void
1922frame_set_child_size(struct widget *widget, int child_width, int child_height)
1923{
1924 struct display *display = widget->window->display;
1925 struct theme *t = display->theme;
1926 int decoration_width, decoration_height;
1927 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001928 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001929
1930 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001931 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001932 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001933 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001934
1935 width = child_width + decoration_width;
1936 height = child_height + decoration_height;
1937 } else {
1938 width = child_width;
1939 height = child_height;
1940 }
1941
1942 window_schedule_resize(widget->window, width, height);
1943}
1944
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001945static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001946frame_destroy(struct frame *frame)
1947{
Martin Minarik1998b152012-05-10 02:04:35 +02001948 struct frame_button *button, *tmp;
1949
1950 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1951 frame_button_destroy(button);
1952
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001953 /* frame->child must be destroyed by the application */
1954 widget_destroy(frame->widget);
1955 free(frame);
1956}
1957
1958static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001959input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001960 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001961{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001962 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001963 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001964
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001965 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001966 return;
1967
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001968 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001969 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001970 widget = old;
1971 if (input->grab)
1972 widget = input->grab;
1973 if (widget->leave_handler)
1974 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001975 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001976 }
1977
1978 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001979 widget = focus;
1980 if (input->grab)
1981 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001982 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001983 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001984 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001985 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001986
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001987 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001988 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001989}
1990
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001991void
1992input_grab(struct input *input, struct widget *widget, uint32_t button)
1993{
1994 input->grab = widget;
1995 input->grab_button = button;
1996}
1997
1998void
1999input_ungrab(struct input *input)
2000{
2001 struct widget *widget;
2002
2003 input->grab = NULL;
2004 if (input->pointer_focus) {
2005 widget = widget_find_widget(input->pointer_focus->widget,
2006 input->sx, input->sy);
2007 input_set_focus_widget(input, widget, input->sx, input->sy);
2008 }
2009}
2010
2011static void
2012input_remove_pointer_focus(struct input *input)
2013{
2014 struct window *window = input->pointer_focus;
2015
2016 if (!window)
2017 return;
2018
2019 input_set_focus_widget(input, NULL, 0, 0);
2020
2021 input->pointer_focus = NULL;
2022 input->current_cursor = CURSOR_UNSET;
2023}
2024
2025static void
2026pointer_handle_enter(void *data, struct wl_pointer *pointer,
2027 uint32_t serial, struct wl_surface *surface,
2028 wl_fixed_t sx_w, wl_fixed_t sy_w)
2029{
2030 struct input *input = data;
2031 struct window *window;
2032 struct widget *widget;
2033 float sx = wl_fixed_to_double(sx_w);
2034 float sy = wl_fixed_to_double(sy_w);
2035
2036 if (!surface) {
2037 /* enter event for a window we've just destroyed */
2038 return;
2039 }
2040
2041 input->display->serial = serial;
2042 input->pointer_enter_serial = serial;
2043 input->pointer_focus = wl_surface_get_user_data(surface);
2044 window = input->pointer_focus;
2045
2046 if (window->pool) {
2047 shm_pool_destroy(window->pool);
2048 window->pool = NULL;
2049 /* Schedule a redraw to free the pool */
2050 window_schedule_redraw(window);
2051 }
2052
2053 input->sx = sx;
2054 input->sy = sy;
2055
2056 widget = widget_find_widget(window->widget, sx, sy);
2057 input_set_focus_widget(input, widget, sx, sy);
2058}
2059
2060static void
2061pointer_handle_leave(void *data, struct wl_pointer *pointer,
2062 uint32_t serial, struct wl_surface *surface)
2063{
2064 struct input *input = data;
2065
2066 input->display->serial = serial;
2067 input_remove_pointer_focus(input);
2068}
2069
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002070static void
Daniel Stone37816df2012-05-16 18:45:18 +01002071pointer_handle_motion(void *data, struct wl_pointer *pointer,
2072 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002073{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002074 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002075 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002076 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002077 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002078 float sx = wl_fixed_to_double(sx_w);
2079 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002080
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002081 input->sx = sx;
2082 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002083
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002084 if (!window)
2085 return;
2086
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002087 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002088 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002089 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002090 }
2091
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002092 if (input->grab)
2093 widget = input->grab;
2094 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002095 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002096 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002097 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002098 input, time, sx, sy,
2099 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002100
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002101 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002102}
2103
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002104static void
Daniel Stone37816df2012-05-16 18:45:18 +01002105pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002106 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002107{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002108 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002109 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002110 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002111
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002112 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002113 if (input->focus_widget && input->grab == NULL &&
2114 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002115 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002116
Neil Roberts6b28aad2012-01-23 19:11:18 +00002117 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002118 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002119 (*widget->button_handler)(widget,
2120 input, time,
2121 button, state,
2122 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002123
Daniel Stone4dbadb12012-05-30 16:31:51 +01002124 if (input->grab && input->grab_button == button &&
2125 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002126 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002127}
2128
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002129static void
Daniel Stone37816df2012-05-16 18:45:18 +01002130pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002131 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002132{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002133 struct input *input = data;
2134 struct widget *widget;
2135
2136 widget = input->focus_widget;
2137 if (input->grab)
2138 widget = input->grab;
2139 if (widget && widget->axis_handler)
2140 (*widget->axis_handler)(widget,
2141 input, time,
2142 axis, value,
2143 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002144}
2145
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002146static const struct wl_pointer_listener pointer_listener = {
2147 pointer_handle_enter,
2148 pointer_handle_leave,
2149 pointer_handle_motion,
2150 pointer_handle_button,
2151 pointer_handle_axis,
2152};
2153
2154static void
2155input_remove_keyboard_focus(struct input *input)
2156{
2157 struct window *window = input->keyboard_focus;
2158 struct itimerspec its;
2159
2160 its.it_interval.tv_sec = 0;
2161 its.it_interval.tv_nsec = 0;
2162 its.it_value.tv_sec = 0;
2163 its.it_value.tv_nsec = 0;
2164 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2165
2166 if (!window)
2167 return;
2168
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002169 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002170 if (window->keyboard_focus_handler)
2171 (*window->keyboard_focus_handler)(window, NULL,
2172 window->user_data);
2173
2174 input->keyboard_focus = NULL;
2175}
2176
2177static void
2178keyboard_repeat_func(struct task *task, uint32_t events)
2179{
2180 struct input *input =
2181 container_of(task, struct input, repeat_task);
2182 struct window *window = input->keyboard_focus;
2183 uint64_t exp;
2184
2185 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2186 /* If we change the timer between the fd becoming
2187 * readable and getting here, there'll be nothing to
2188 * read and we get EAGAIN. */
2189 return;
2190
2191 if (window && window->key_handler) {
2192 (*window->key_handler)(window, input, input->repeat_time,
2193 input->repeat_key, input->repeat_sym,
2194 WL_KEYBOARD_KEY_STATE_PRESSED,
2195 window->user_data);
2196 }
2197}
2198
2199static void
2200keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2201 uint32_t format, int fd, uint32_t size)
2202{
2203 struct input *input = data;
2204 char *map_str;
2205
2206 if (!data) {
2207 close(fd);
2208 return;
2209 }
2210
2211 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2212 close(fd);
2213 return;
2214 }
2215
2216 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2217 if (map_str == MAP_FAILED) {
2218 close(fd);
2219 return;
2220 }
2221
2222 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2223 map_str,
2224 XKB_KEYMAP_FORMAT_TEXT_V1,
2225 0);
2226 munmap(map_str, size);
2227 close(fd);
2228
2229 if (!input->xkb.keymap) {
2230 fprintf(stderr, "failed to compile keymap\n");
2231 return;
2232 }
2233
2234 input->xkb.state = xkb_state_new(input->xkb.keymap);
2235 if (!input->xkb.state) {
2236 fprintf(stderr, "failed to create XKB state\n");
2237 xkb_map_unref(input->xkb.keymap);
2238 input->xkb.keymap = NULL;
2239 return;
2240 }
2241
2242 input->xkb.control_mask =
2243 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2244 input->xkb.alt_mask =
2245 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2246 input->xkb.shift_mask =
2247 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2248}
2249
2250static void
2251keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2252 uint32_t serial, struct wl_surface *surface,
2253 struct wl_array *keys)
2254{
2255 struct input *input = data;
2256 struct window *window;
2257
2258 input->display->serial = serial;
2259 input->keyboard_focus = wl_surface_get_user_data(surface);
2260
2261 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002262 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002263 if (window->keyboard_focus_handler)
2264 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002265 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002266}
2267
2268static void
2269keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2270 uint32_t serial, struct wl_surface *surface)
2271{
2272 struct input *input = data;
2273
2274 input->display->serial = serial;
2275 input_remove_keyboard_focus(input);
2276}
2277
Scott Moreau210d0792012-03-22 10:47:01 -06002278static void
Daniel Stone37816df2012-05-16 18:45:18 +01002279keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002280 uint32_t serial, uint32_t time, uint32_t key,
2281 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002282{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002283 struct input *input = data;
2284 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002285 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002286 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002287 const xkb_keysym_t *syms;
2288 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002289 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002290
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002291 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002292 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002293 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002294 return;
2295
Daniel Stone97f68542012-05-30 16:32:01 +01002296 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002297
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002298 sym = XKB_KEY_NoSymbol;
2299 if (num_syms == 1)
2300 sym = syms[0];
2301
2302 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002303 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002304 window_set_maximized(window,
2305 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002306 } else if (sym == XKB_KEY_F11 &&
2307 window->fullscreen_handler &&
2308 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2309 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002310 } else if (sym == XKB_KEY_F4 &&
2311 input->modifiers == MOD_ALT_MASK &&
2312 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2313 if (window->close_handler)
2314 window->close_handler(window->parent,
2315 window->user_data);
2316 else
2317 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002318 } else if (window->key_handler) {
2319 (*window->key_handler)(window, input, time, key,
2320 sym, state, window->user_data);
2321 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002322
2323 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2324 key == input->repeat_key) {
2325 its.it_interval.tv_sec = 0;
2326 its.it_interval.tv_nsec = 0;
2327 its.it_value.tv_sec = 0;
2328 its.it_value.tv_nsec = 0;
2329 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2330 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2331 input->repeat_sym = sym;
2332 input->repeat_key = key;
2333 input->repeat_time = time;
2334 its.it_interval.tv_sec = 0;
2335 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2336 its.it_value.tv_sec = 0;
2337 its.it_value.tv_nsec = 400 * 1000 * 1000;
2338 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2339 }
2340}
2341
2342static void
Daniel Stone351eb612012-05-31 15:27:47 -04002343keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2344 uint32_t serial, uint32_t mods_depressed,
2345 uint32_t mods_latched, uint32_t mods_locked,
2346 uint32_t group)
2347{
2348 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002349 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002350
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002351 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2352 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002353 mask = xkb_state_serialize_mods(input->xkb.state,
2354 XKB_STATE_DEPRESSED |
2355 XKB_STATE_LATCHED);
2356 input->modifiers = 0;
2357 if (mask & input->xkb.control_mask)
2358 input->modifiers |= MOD_CONTROL_MASK;
2359 if (mask & input->xkb.alt_mask)
2360 input->modifiers |= MOD_ALT_MASK;
2361 if (mask & input->xkb.shift_mask)
2362 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002363}
2364
Daniel Stone37816df2012-05-16 18:45:18 +01002365static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002366 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002367 keyboard_handle_enter,
2368 keyboard_handle_leave,
2369 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002370 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002371};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002372
2373static void
Daniel Stone37816df2012-05-16 18:45:18 +01002374seat_handle_capabilities(void *data, struct wl_seat *seat,
2375 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002376{
Daniel Stone37816df2012-05-16 18:45:18 +01002377 struct input *input = data;
2378
2379 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2380 input->pointer = wl_seat_get_pointer(seat);
2381 wl_pointer_set_user_data(input->pointer, input);
2382 wl_pointer_add_listener(input->pointer, &pointer_listener,
2383 input);
2384 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2385 wl_pointer_destroy(input->pointer);
2386 input->pointer = NULL;
2387 }
2388
2389 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2390 input->keyboard = wl_seat_get_keyboard(seat);
2391 wl_keyboard_set_user_data(input->keyboard, input);
2392 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2393 input);
2394 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2395 wl_keyboard_destroy(input->keyboard);
2396 input->keyboard = NULL;
2397 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002398}
2399
Daniel Stone37816df2012-05-16 18:45:18 +01002400static const struct wl_seat_listener seat_listener = {
2401 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002402};
2403
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002404void
2405input_get_position(struct input *input, int32_t *x, int32_t *y)
2406{
2407 *x = input->sx;
2408 *y = input->sy;
2409}
2410
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002411struct display *
2412input_get_display(struct input *input)
2413{
2414 return input->display;
2415}
2416
Daniel Stone37816df2012-05-16 18:45:18 +01002417struct wl_seat *
2418input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002419{
Daniel Stone37816df2012-05-16 18:45:18 +01002420 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002421}
2422
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002423uint32_t
2424input_get_modifiers(struct input *input)
2425{
2426 return input->modifiers;
2427}
2428
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002429struct widget *
2430input_get_focus_widget(struct input *input)
2431{
2432 return input->focus_widget;
2433}
2434
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002435struct data_offer {
2436 struct wl_data_offer *offer;
2437 struct input *input;
2438 struct wl_array types;
2439 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002440
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002441 struct task io_task;
2442 int fd;
2443 data_func_t func;
2444 int32_t x, y;
2445 void *user_data;
2446};
2447
2448static void
2449data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2450{
2451 struct data_offer *offer = data;
2452 char **p;
2453
2454 p = wl_array_add(&offer->types, sizeof *p);
2455 *p = strdup(type);
2456}
2457
2458static const struct wl_data_offer_listener data_offer_listener = {
2459 data_offer_offer,
2460};
2461
2462static void
2463data_offer_destroy(struct data_offer *offer)
2464{
2465 char **p;
2466
2467 offer->refcount--;
2468 if (offer->refcount == 0) {
2469 wl_data_offer_destroy(offer->offer);
2470 for (p = offer->types.data; *p; p++)
2471 free(*p);
2472 wl_array_release(&offer->types);
2473 free(offer);
2474 }
2475}
2476
2477static void
2478data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002479 struct wl_data_device *data_device,
2480 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002481{
2482 struct data_offer *offer;
2483
2484 offer = malloc(sizeof *offer);
2485
2486 wl_array_init(&offer->types);
2487 offer->refcount = 1;
2488 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002489 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002490 wl_data_offer_add_listener(offer->offer,
2491 &data_offer_listener, offer);
2492}
2493
2494static void
2495data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002496 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002497 wl_fixed_t x_w, wl_fixed_t y_w,
2498 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002499{
2500 struct input *input = data;
2501 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002502 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002503 float x = wl_fixed_to_double(x_w);
2504 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002505 char **p;
2506
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002507 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002508 window = wl_surface_get_user_data(surface);
2509 input->pointer_focus = window;
2510
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002511 if (offer) {
2512 input->drag_offer = wl_data_offer_get_user_data(offer);
2513
2514 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2515 *p = NULL;
2516
2517 types_data = input->drag_offer->types.data;
2518 } else {
2519 input->drag_offer = NULL;
2520 types_data = NULL;
2521 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002522
2523 window = input->pointer_focus;
2524 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002525 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002526 window->user_data);
2527}
2528
2529static void
2530data_device_leave(void *data, struct wl_data_device *data_device)
2531{
2532 struct input *input = data;
2533
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002534 if (input->drag_offer) {
2535 data_offer_destroy(input->drag_offer);
2536 input->drag_offer = NULL;
2537 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002538}
2539
2540static void
2541data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002542 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002543{
2544 struct input *input = data;
2545 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002546 float x = wl_fixed_to_double(x_w);
2547 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002548 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002549
2550 input->sx = x;
2551 input->sy = y;
2552
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002553 if (input->drag_offer)
2554 types_data = input->drag_offer->types.data;
2555 else
2556 types_data = NULL;
2557
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002558 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002559 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002560 window->user_data);
2561}
2562
2563static void
2564data_device_drop(void *data, struct wl_data_device *data_device)
2565{
2566 struct input *input = data;
2567 struct window *window = input->pointer_focus;
2568
2569 if (window->drop_handler)
2570 window->drop_handler(window, input,
2571 input->sx, input->sy, window->user_data);
2572}
2573
2574static void
2575data_device_selection(void *data,
2576 struct wl_data_device *wl_data_device,
2577 struct wl_data_offer *offer)
2578{
2579 struct input *input = data;
2580 char **p;
2581
2582 if (input->selection_offer)
2583 data_offer_destroy(input->selection_offer);
2584
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002585 if (offer) {
2586 input->selection_offer = wl_data_offer_get_user_data(offer);
2587 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2588 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002589 } else {
2590 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002591 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002592}
2593
2594static const struct wl_data_device_listener data_device_listener = {
2595 data_device_data_offer,
2596 data_device_enter,
2597 data_device_leave,
2598 data_device_motion,
2599 data_device_drop,
2600 data_device_selection
2601};
2602
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002603static void
2604input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002605{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002606 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002607 struct wl_cursor *cursor;
2608 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002609
Daniel Stone80972742012-11-07 17:51:39 +11002610 if (!input->pointer)
2611 return;
2612
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002613 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002614 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002615 return;
2616
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002617 if (index >= (int) cursor->image_count) {
2618 fprintf(stderr, "cursor index out of range\n");
2619 return;
2620 }
2621
2622 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002623 buffer = wl_cursor_image_get_buffer(image);
2624 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002625 return;
2626
Kristian Høgsbergae277372012-08-01 09:41:08 -04002627 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002628 input->pointer_surface,
2629 image->hotspot_x, image->hotspot_y);
2630 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2631 wl_surface_damage(input->pointer_surface, 0, 0,
2632 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002633 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002634}
2635
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002636static const struct wl_callback_listener pointer_surface_listener;
2637
2638static void
2639pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2640 uint32_t time)
2641{
2642 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002643 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002644 int i;
2645
2646 if (callback) {
2647 assert(callback == input->cursor_frame_cb);
2648 wl_callback_destroy(callback);
2649 input->cursor_frame_cb = NULL;
2650 }
2651
Daniel Stone80972742012-11-07 17:51:39 +11002652 if (!input->pointer)
2653 return;
2654
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002655 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002656 wl_pointer_set_cursor(input->pointer,
2657 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002658 NULL, 0, 0);
2659 return;
2660 }
2661
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002662 if (input->current_cursor == CURSOR_UNSET)
2663 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002664 cursor = input->display->cursors[input->current_cursor];
2665 if (!cursor)
2666 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002667
2668 /* FIXME We don't have the current time on the first call so we set
2669 * the animation start to the time of the first frame callback. */
2670 if (time == 0)
2671 input->cursor_anim_start = 0;
2672 else if (input->cursor_anim_start == 0)
2673 input->cursor_anim_start = time;
2674
2675 if (time == 0 || input->cursor_anim_start == 0)
2676 i = 0;
2677 else
2678 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2679
Pekka Paalanenbc106382012-10-10 12:49:31 +03002680 if (cursor->image_count > 1) {
2681 input->cursor_frame_cb =
2682 wl_surface_frame(input->pointer_surface);
2683 wl_callback_add_listener(input->cursor_frame_cb,
2684 &pointer_surface_listener, input);
2685 }
2686
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002687 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002688}
2689
2690static const struct wl_callback_listener pointer_surface_listener = {
2691 pointer_surface_frame_callback
2692};
2693
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002694void
2695input_set_pointer_image(struct input *input, int pointer)
2696{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002697 int force = 0;
2698
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002699 if (!input->pointer)
2700 return;
2701
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002702 if (input->pointer_enter_serial > input->cursor_serial)
2703 force = 1;
2704
2705 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002706 return;
2707
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002708 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002709 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002710 if (!input->cursor_frame_cb)
2711 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002712 else if (force) {
2713 /* The current frame callback may be stuck if, for instance,
2714 * the set cursor request was processed by the server after
2715 * this client lost the focus. In this case the cursor surface
2716 * might not be mapped and the frame callback wouldn't ever
2717 * complete. Send a set_cursor and attach to try to map the
2718 * cursor surface again so that the callback will finish */
2719 input_set_pointer_image_index(input, 0);
2720 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002721}
2722
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002723struct wl_data_device *
2724input_get_data_device(struct input *input)
2725{
2726 return input->data_device;
2727}
2728
2729void
2730input_set_selection(struct input *input,
2731 struct wl_data_source *source, uint32_t time)
2732{
2733 wl_data_device_set_selection(input->data_device, source, time);
2734}
2735
2736void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002737input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002738{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002739 wl_data_offer_accept(input->drag_offer->offer,
2740 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002741}
2742
2743static void
2744offer_io_func(struct task *task, uint32_t events)
2745{
2746 struct data_offer *offer =
2747 container_of(task, struct data_offer, io_task);
2748 unsigned int len;
2749 char buffer[4096];
2750
2751 len = read(offer->fd, buffer, sizeof buffer);
2752 offer->func(buffer, len,
2753 offer->x, offer->y, offer->user_data);
2754
2755 if (len == 0) {
2756 close(offer->fd);
2757 data_offer_destroy(offer);
2758 }
2759}
2760
2761static void
2762data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2763 data_func_t func, void *user_data)
2764{
2765 int p[2];
2766
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002767 if (pipe2(p, O_CLOEXEC) == -1)
2768 return;
2769
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002770 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2771 close(p[1]);
2772
2773 offer->io_task.run = offer_io_func;
2774 offer->fd = p[0];
2775 offer->func = func;
2776 offer->refcount++;
2777 offer->user_data = user_data;
2778
2779 display_watch_fd(offer->input->display,
2780 offer->fd, EPOLLIN, &offer->io_task);
2781}
2782
2783void
2784input_receive_drag_data(struct input *input, const char *mime_type,
2785 data_func_t func, void *data)
2786{
2787 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2788 input->drag_offer->x = input->sx;
2789 input->drag_offer->y = input->sy;
2790}
2791
2792int
2793input_receive_selection_data(struct input *input, const char *mime_type,
2794 data_func_t func, void *data)
2795{
2796 char **p;
2797
2798 if (input->selection_offer == NULL)
2799 return -1;
2800
2801 for (p = input->selection_offer->types.data; *p; p++)
2802 if (strcmp(mime_type, *p) == 0)
2803 break;
2804
2805 if (*p == NULL)
2806 return -1;
2807
2808 data_offer_receive_data(input->selection_offer,
2809 mime_type, func, data);
2810 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002811}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002812
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002813int
2814input_receive_selection_data_to_fd(struct input *input,
2815 const char *mime_type, int fd)
2816{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002817 if (input->selection_offer)
2818 wl_data_offer_receive(input->selection_offer->offer,
2819 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002820
2821 return 0;
2822}
2823
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002824void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002825window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002826{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002827 if (!window->shell_surface)
2828 return;
2829
Daniel Stone37816df2012-05-16 18:45:18 +01002830 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002831}
2832
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002833static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002834idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002835{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002836 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002837
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002838 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002839 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002840 widget_set_allocation(widget,
2841 window->pending_allocation.x,
2842 window->pending_allocation.y,
2843 window->pending_allocation.width,
2844 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002845
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002846 if (window->input_region) {
2847 wl_region_destroy(window->input_region);
2848 window->input_region = NULL;
2849 }
2850
2851 if (window->opaque_region) {
2852 wl_region_destroy(window->opaque_region);
2853 window->opaque_region = NULL;
2854 }
2855
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002856 if (widget->resize_handler)
2857 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002858 widget->allocation.width,
2859 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002860 widget->user_data);
2861
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002862 if (window->allocation.width != widget->allocation.width ||
2863 window->allocation.height != widget->allocation.height) {
2864 window->allocation = widget->allocation;
2865 window_schedule_redraw(window);
2866 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002867}
2868
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002869void
2870window_schedule_resize(struct window *window, int width, int height)
2871{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002872 window->pending_allocation.x = 0;
2873 window->pending_allocation.y = 0;
2874 window->pending_allocation.width = width;
2875 window->pending_allocation.height = height;
2876
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002877 if (window->min_allocation.width == 0)
2878 window->min_allocation = window->pending_allocation;
2879 if (window->pending_allocation.width < window->min_allocation.width)
2880 window->pending_allocation.width = window->min_allocation.width;
2881 if (window->pending_allocation.height < window->min_allocation.height)
2882 window->pending_allocation.height = window->min_allocation.height;
2883
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002884 window->resize_needed = 1;
2885 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002886}
2887
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002888void
2889widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2890{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002891 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002892}
2893
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002894static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002895handle_ping(void *data, struct wl_shell_surface *shell_surface,
2896 uint32_t serial)
2897{
2898 wl_shell_surface_pong(shell_surface, serial);
2899}
2900
2901static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002902handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002903 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002904{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002905 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002906
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002907 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002908 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002909}
2910
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002911static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002912menu_destroy(struct menu *menu)
2913{
2914 widget_destroy(menu->widget);
2915 window_destroy(menu->window);
2916 free(menu);
2917}
2918
2919static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002920handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2921{
2922 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002923 struct menu *menu = window->widget->user_data;
2924
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002925 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002926 * device. Or just use wl_callback. And this really needs to
2927 * be a window vfunc that the menu can set. And we need the
2928 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002929
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002930 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002931 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002932 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002933}
2934
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002935static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002936 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002937 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002938 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002939};
2940
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002941void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002942window_get_allocation(struct window *window,
2943 struct rectangle *allocation)
2944{
2945 *allocation = window->allocation;
2946}
2947
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002948static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002949widget_redraw(struct widget *widget)
2950{
2951 struct widget *child;
2952
2953 if (widget->redraw_handler)
2954 widget->redraw_handler(widget, widget->user_data);
2955 wl_list_for_each(child, &widget->child_list, link)
2956 widget_redraw(child);
2957}
2958
2959static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002960frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2961{
2962 struct window *window = data;
2963
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002964 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002965 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002966 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002967 window->redraw_scheduled = 0;
2968 if (window->redraw_needed)
2969 window_schedule_redraw(window);
2970}
2971
2972static const struct wl_callback_listener listener = {
2973 frame_callback
2974};
2975
2976static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002977idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002978{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002979 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002980
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002981 if (window->resize_needed)
2982 idle_resize(window);
2983
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002984 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002985 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002986 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002987 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002988
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002989 window->frame_cb = wl_surface_frame(window->surface);
2990 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002991 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002992}
2993
2994void
2995window_schedule_redraw(struct window *window)
2996{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002997 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002998 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002999 window->redraw_task.run = idle_redraw;
3000 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003001 window->redraw_scheduled = 1;
3002 }
3003}
3004
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003005int
3006window_is_fullscreen(struct window *window)
3007{
3008 return window->type == TYPE_FULLSCREEN;
3009}
3010
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003011void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003012window_set_fullscreen(struct window *window, int fullscreen)
3013{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003014 if (!window->display->shell)
3015 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003016
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003017 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003018 return;
3019
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003020 if (fullscreen) {
3021 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003022 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003023 wl_shell_surface_set_fullscreen(window->shell_surface,
3024 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3025 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003026 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003027 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003028 wl_shell_surface_set_toplevel(window->shell_surface);
3029 window_schedule_resize(window,
3030 window->saved_allocation.width,
3031 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003032 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003033}
3034
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003035int
3036window_is_maximized(struct window *window)
3037{
3038 return window->type == TYPE_MAXIMIZED;
3039}
3040
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003041void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003042window_set_maximized(struct window *window, int maximized)
3043{
3044 if (!window->display->shell)
3045 return;
3046
3047 if ((window->type == TYPE_MAXIMIZED) == maximized)
3048 return;
3049
3050 if (window->type == TYPE_TOPLEVEL) {
3051 window->saved_allocation = window->allocation;
3052 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3053 window->type = TYPE_MAXIMIZED;
3054 } else {
3055 wl_shell_surface_set_toplevel(window->shell_surface);
3056 window->type = TYPE_TOPLEVEL;
3057 window_schedule_resize(window,
3058 window->saved_allocation.width,
3059 window->saved_allocation.height);
3060 }
3061}
3062
3063void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003064window_set_user_data(struct window *window, void *data)
3065{
3066 window->user_data = data;
3067}
3068
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003069void *
3070window_get_user_data(struct window *window)
3071{
3072 return window->user_data;
3073}
3074
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003075void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003076window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003077 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003078{
3079 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003080}
3081
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003082void
3083window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003084 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003085{
3086 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003087}
3088
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003089void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003090window_set_data_handler(struct window *window, window_data_handler_t handler)
3091{
3092 window->data_handler = handler;
3093}
3094
3095void
3096window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3097{
3098 window->drop_handler = handler;
3099}
3100
3101void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003102window_set_close_handler(struct window *window,
3103 window_close_handler_t handler)
3104{
3105 window->close_handler = handler;
3106}
3107
3108void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003109window_set_fullscreen_handler(struct window *window,
3110 window_fullscreen_handler_t handler)
3111{
3112 window->fullscreen_handler = handler;
3113}
3114
3115void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003116window_set_title(struct window *window, const char *title)
3117{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003118 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003119 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003120 if (window->shell_surface)
3121 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003122}
3123
3124const char *
3125window_get_title(struct window *window)
3126{
3127 return window->title;
3128}
3129
3130void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003131window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3132{
3133 struct text_cursor_position *text_cursor_position =
3134 window->display->text_cursor_position;
3135
Scott Moreau9295ce02012-06-01 12:46:10 -06003136 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003137 return;
3138
3139 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003140 window->surface,
3141 wl_fixed_from_int(x),
3142 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003143}
3144
3145void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003146window_damage(struct window *window, int32_t x, int32_t y,
3147 int32_t width, int32_t height)
3148{
3149 wl_surface_damage(window->surface, x, y, width, height);
3150}
3151
Casey Dahlin9074db52012-04-19 22:50:09 -04003152static void
3153surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003154 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003155{
Rob Bradford7507b572012-05-15 17:55:34 +01003156 struct window *window = data;
3157 struct output *output;
3158 struct output *output_found = NULL;
3159 struct window_output *window_output;
3160
3161 wl_list_for_each(output, &window->display->output_list, link) {
3162 if (output->output == wl_output) {
3163 output_found = output;
3164 break;
3165 }
3166 }
3167
3168 if (!output_found)
3169 return;
3170
3171 window_output = malloc (sizeof *window_output);
3172 window_output->output = output_found;
3173
3174 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003175}
3176
3177static void
3178surface_leave(void *data,
3179 struct wl_surface *wl_surface, struct wl_output *output)
3180{
Rob Bradford7507b572012-05-15 17:55:34 +01003181 struct window *window = data;
3182 struct window_output *window_output;
3183 struct window_output *window_output_found = NULL;
3184
3185 wl_list_for_each(window_output, &window->window_output_list, link) {
3186 if (window_output->output->output == output) {
3187 window_output_found = window_output;
3188 break;
3189 }
3190 }
3191
3192 if (window_output_found) {
3193 wl_list_remove(&window_output_found->link);
3194 free(window_output_found);
3195 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003196}
3197
3198static const struct wl_surface_listener surface_listener = {
3199 surface_enter,
3200 surface_leave
3201};
3202
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003203static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003204window_create_internal(struct display *display,
3205 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003206{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003207 struct window *window;
3208
3209 window = malloc(sizeof *window);
3210 if (window == NULL)
3211 return NULL;
3212
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003213 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003214 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003215 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003216 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003217 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003218 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003219 window->shell_surface =
3220 wl_shell_get_shell_surface(display->shell,
3221 window->surface);
3222 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003223 window->allocation.x = 0;
3224 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003225 window->allocation.width = 0;
3226 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003227 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003228 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003229 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003230 window->input_region = NULL;
3231 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003232
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003233 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003234#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003235 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003236#else
3237 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3238#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003239 else
3240 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003241
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003242 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003243 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003244 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003245
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003246 if (window->shell_surface) {
3247 wl_shell_surface_set_user_data(window->shell_surface, window);
3248 wl_shell_surface_add_listener(window->shell_surface,
3249 &shell_surface_listener, window);
3250 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003251
Rob Bradford7507b572012-05-15 17:55:34 +01003252 wl_list_init (&window->window_output_list);
3253
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003254 return window;
3255}
3256
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003257struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003258window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003259{
3260 struct window *window;
3261
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003262 window = window_create_internal(display, NULL, TYPE_NONE);
3263 if (!window)
3264 return NULL;
3265
3266 return window;
3267}
3268
3269struct window *
3270window_create_custom(struct display *display)
3271{
3272 struct window *window;
3273
3274 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003275 if (!window)
3276 return NULL;
3277
3278 return window;
3279}
3280
3281struct window *
3282window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003283 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003284{
3285 struct window *window;
3286
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003287 window = window_create_internal(parent->display,
3288 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003289 if (!window)
3290 return NULL;
3291
3292 window->x = x;
3293 window->y = y;
3294
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003295 if (display->shell)
3296 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003297 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003298 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003299
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003300 return window;
3301}
3302
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003303static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003304menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003305{
3306 int next;
3307
3308 next = (sy - 8) / 20;
3309 if (menu->current != next) {
3310 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003311 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003312 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003313}
3314
3315static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003316menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003317 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003318 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003319{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003320 struct menu *menu = data;
3321
3322 if (widget == menu->widget)
3323 menu_set_item(data, y);
3324
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003325 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003326}
3327
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003328static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003329menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003330 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003331{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003332 struct menu *menu = data;
3333
3334 if (widget == menu->widget)
3335 menu_set_item(data, y);
3336
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003337 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003338}
3339
3340static void
3341menu_leave_handler(struct widget *widget, struct input *input, void *data)
3342{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003343 struct menu *menu = data;
3344
3345 if (widget == menu->widget)
3346 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003347}
3348
3349static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003350menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003351 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003352 uint32_t button, enum wl_pointer_button_state state,
3353 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003354
3355{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003356 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003357
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003358 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3359 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003360 /* Either relase after press-drag-release or
3361 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003362 menu->func(menu->window->parent,
3363 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003364 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003365 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003366 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003367 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003368 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003369}
3370
3371static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003372menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003373{
3374 cairo_t *cr;
3375 const int32_t r = 3, margin = 3;
3376 struct menu *menu = data;
3377 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003378 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003379
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003380 cr = cairo_create(window->cairo_surface);
3381 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3382 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3383 cairo_paint(cr);
3384
3385 width = window->allocation.width;
3386 height = window->allocation.height;
3387 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003388
3389 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003390 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3391 cairo_fill(cr);
3392
3393 for (i = 0; i < menu->count; i++) {
3394 if (i == menu->current) {
3395 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3396 cairo_rectangle(cr, margin, i * 20 + margin,
3397 width - 2 * margin, 20);
3398 cairo_fill(cr);
3399 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3400 cairo_move_to(cr, 10, i * 20 + 16);
3401 cairo_show_text(cr, menu->entries[i]);
3402 } else {
3403 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3404 cairo_move_to(cr, 10, i * 20 + 16);
3405 cairo_show_text(cr, menu->entries[i]);
3406 }
3407 }
3408
3409 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003410}
3411
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003412void
3413window_show_menu(struct display *display,
3414 struct input *input, uint32_t time, struct window *parent,
3415 int32_t x, int32_t y,
3416 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003417{
3418 struct window *window;
3419 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003420 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003421
3422 menu = malloc(sizeof *menu);
3423 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003424 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003425
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003426 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003427 if (!window) {
3428 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003429 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003430 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003431
3432 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003433 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003434 menu->entries = entries;
3435 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003436 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003437 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003438 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003439 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003440 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003441 window->type = TYPE_MENU;
3442 window->x = x;
3443 window->y = y;
3444
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003445 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003446 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003447 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003448 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003449 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003450
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003451 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003452 widget_set_enter_handler(menu->widget, menu_enter_handler);
3453 widget_set_leave_handler(menu->widget, menu_leave_handler);
3454 widget_set_motion_handler(menu->widget, menu_motion_handler);
3455 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003456
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003457 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003458 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003459}
3460
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003461void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003462window_set_buffer_type(struct window *window, enum window_buffer_type type)
3463{
3464 window->buffer_type = type;
3465}
3466
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003467
3468static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003469display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003470 struct wl_output *wl_output,
3471 int x, int y,
3472 int physical_width,
3473 int physical_height,
3474 int subpixel,
3475 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003476 const char *model,
3477 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003478{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003479 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003480
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003481 output->allocation.x = x;
3482 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003483 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003484}
3485
3486static void
3487display_handle_mode(void *data,
3488 struct wl_output *wl_output,
3489 uint32_t flags,
3490 int width,
3491 int height,
3492 int refresh)
3493{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003494 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003495 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003496
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003497 if (flags & WL_OUTPUT_MODE_CURRENT) {
3498 output->allocation.width = width;
3499 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003500 if (display->output_configure_handler)
3501 (*display->output_configure_handler)(
3502 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003503 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003504}
3505
3506static const struct wl_output_listener output_listener = {
3507 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003508 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003509};
3510
3511static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003512display_add_output(struct display *d, uint32_t id)
3513{
3514 struct output *output;
3515
3516 output = malloc(sizeof *output);
3517 if (output == NULL)
3518 return;
3519
3520 memset(output, 0, sizeof *output);
3521 output->display = d;
3522 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003523 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003524 wl_list_insert(d->output_list.prev, &output->link);
3525
3526 wl_output_add_listener(output->output, &output_listener, output);
3527}
3528
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003529static void
3530output_destroy(struct output *output)
3531{
3532 if (output->destroy_handler)
3533 (*output->destroy_handler)(output, output->user_data);
3534
3535 wl_output_destroy(output->output);
3536 wl_list_remove(&output->link);
3537 free(output);
3538}
3539
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003540void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003541display_set_global_handler(struct display *display,
3542 display_global_handler_t handler)
3543{
3544 struct global *global;
3545
3546 display->global_handler = handler;
3547 if (!handler)
3548 return;
3549
3550 wl_list_for_each(global, &display->global_list, link)
3551 display->global_handler(display,
3552 global->name, global->interface,
3553 global->version, display->user_data);
3554}
3555
3556void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003557display_set_output_configure_handler(struct display *display,
3558 display_output_handler_t handler)
3559{
3560 struct output *output;
3561
3562 display->output_configure_handler = handler;
3563 if (!handler)
3564 return;
3565
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003566 wl_list_for_each(output, &display->output_list, link) {
3567 if (output->allocation.width == 0 &&
3568 output->allocation.height == 0)
3569 continue;
3570
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003571 (*display->output_configure_handler)(output,
3572 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003573 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003574}
3575
3576void
3577output_set_user_data(struct output *output, void *data)
3578{
3579 output->user_data = data;
3580}
3581
3582void *
3583output_get_user_data(struct output *output)
3584{
3585 return output->user_data;
3586}
3587
3588void
3589output_set_destroy_handler(struct output *output,
3590 display_output_handler_t handler)
3591{
3592 output->destroy_handler = handler;
3593 /* FIXME: implement this, once we have way to remove outputs */
3594}
3595
3596void
Scott Moreau4e072362012-09-29 02:03:11 -06003597output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003598{
Scott Moreau4e072362012-09-29 02:03:11 -06003599 struct rectangle allocation = output->allocation;
3600
3601 switch (output->transform) {
3602 case WL_OUTPUT_TRANSFORM_90:
3603 case WL_OUTPUT_TRANSFORM_270:
3604 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3605 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3606 /* Swap width and height */
3607 allocation.width = output->allocation.height;
3608 allocation.height = output->allocation.width;
3609 break;
3610 }
3611
3612 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003613}
3614
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003615struct wl_output *
3616output_get_wl_output(struct output *output)
3617{
3618 return output->output;
3619}
3620
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003621static void
Daniel Stone97f68542012-05-30 16:32:01 +01003622fini_xkb(struct input *input)
3623{
3624 xkb_state_unref(input->xkb.state);
3625 xkb_map_unref(input->xkb.keymap);
3626}
3627
3628static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003629display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003630{
3631 struct input *input;
3632
3633 input = malloc(sizeof *input);
3634 if (input == NULL)
3635 return;
3636
3637 memset(input, 0, sizeof *input);
3638 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003639 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003640 input->pointer_focus = NULL;
3641 input->keyboard_focus = NULL;
3642 wl_list_insert(d->input_list.prev, &input->link);
3643
Daniel Stone37816df2012-05-16 18:45:18 +01003644 wl_seat_add_listener(input->seat, &seat_listener, input);
3645 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003646
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003647 input->data_device =
3648 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003649 input->seat);
3650 wl_data_device_add_listener(input->data_device, &data_device_listener,
3651 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003652
3653 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003654
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003655 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3656 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003657 input->repeat_task.run = keyboard_repeat_func;
3658 display_watch_fd(d, input->repeat_timer_fd,
3659 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003660}
3661
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003662static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003663input_destroy(struct input *input)
3664{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003665 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003666 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003667
3668 if (input->drag_offer)
3669 data_offer_destroy(input->drag_offer);
3670
3671 if (input->selection_offer)
3672 data_offer_destroy(input->selection_offer);
3673
3674 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003675 fini_xkb(input);
3676
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003677 wl_surface_destroy(input->pointer_surface);
3678
Pekka Paalanene1207c72011-12-16 12:02:09 +02003679 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003680 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003681 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003682 free(input);
3683}
3684
3685static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003686init_workspace_manager(struct display *d, uint32_t id)
3687{
3688 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003689 wl_registry_bind(d->registry, id,
3690 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003691 if (d->workspace_manager != NULL)
3692 workspace_manager_add_listener(d->workspace_manager,
3693 &workspace_manager_listener,
3694 d);
3695}
3696
3697static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003698registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3699 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003700{
3701 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003702 struct global *global;
3703
3704 global = malloc(sizeof *global);
3705 global->name = id;
3706 global->interface = strdup(interface);
3707 global->version = version;
3708 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003709
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003710 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003711 d->compositor = wl_registry_bind(registry, id,
3712 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003713 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003714 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003715 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003716 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003717 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003718 d->shell = wl_registry_bind(registry,
3719 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003720 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003721 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003722 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3723 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003724 wl_registry_bind(registry, id,
3725 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003726 } else if (strcmp(interface, "text_cursor_position") == 0) {
3727 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003728 wl_registry_bind(registry, id,
3729 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003730 } else if (strcmp(interface, "workspace_manager") == 0) {
3731 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003732 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003733
3734 if (d->global_handler)
3735 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003736}
3737
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003738void *
3739display_bind(struct display *display, uint32_t name,
3740 const struct wl_interface *interface, uint32_t version)
3741{
3742 return wl_registry_bind(display->registry, name, interface, version);
3743}
3744
3745static const struct wl_registry_listener registry_listener = {
3746 registry_handle_global
3747};
3748
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003749#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003750static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003751init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003752{
3753 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003754 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003755
Rob Clark6396ed32012-03-11 19:48:41 -05003756#ifdef USE_CAIRO_GLESV2
3757# define GL_BIT EGL_OPENGL_ES2_BIT
3758#else
3759# define GL_BIT EGL_OPENGL_BIT
3760#endif
3761
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003762 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003763 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003764 EGL_RED_SIZE, 1,
3765 EGL_GREEN_SIZE, 1,
3766 EGL_BLUE_SIZE, 1,
3767 EGL_ALPHA_SIZE, 1,
3768 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003769 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003770 EGL_NONE
3771 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003772
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003773#ifdef USE_CAIRO_GLESV2
3774 static const EGLint context_attribs[] = {
3775 EGL_CONTEXT_CLIENT_VERSION, 2,
3776 EGL_NONE
3777 };
3778 EGLint api = EGL_OPENGL_ES_API;
3779#else
3780 EGLint *context_attribs = NULL;
3781 EGLint api = EGL_OPENGL_API;
3782#endif
3783
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003784 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003785 if (!eglInitialize(d->dpy, &major, &minor)) {
3786 fprintf(stderr, "failed to initialize display\n");
3787 return -1;
3788 }
3789
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003790 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003791 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3792 return -1;
3793 }
3794
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003795 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3796 &d->argb_config, 1, &n) || n != 1) {
3797 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003798 return -1;
3799 }
3800
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003801 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003802 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003803 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003804 fprintf(stderr, "failed to create context\n");
3805 return -1;
3806 }
3807
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003808 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003809 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003810 return -1;
3811 }
3812
Benjamin Franzke0c991632011-09-27 21:57:31 +02003813 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3814 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3815 fprintf(stderr, "failed to get cairo egl argb device\n");
3816 return -1;
3817 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003818
3819 return 0;
3820}
3821
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003822static void
3823fini_egl(struct display *display)
3824{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003825 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003826
3827 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3828 EGL_NO_CONTEXT);
3829
3830 eglTerminate(display->dpy);
3831 eglReleaseThread();
3832}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003833#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003834
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003835static void
3836handle_display_data(struct task *task, uint32_t events)
3837{
3838 struct display *display =
3839 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003840 struct epoll_event ep;
3841 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003842
3843 display->display_fd_events = events;
3844
3845 if (events & EPOLLERR || events & EPOLLHUP) {
3846 display_exit(display);
3847 return;
3848 }
3849
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04003850 if (events & EPOLLIN) {
3851 ret = wl_display_dispatch(display->display);
3852 if (ret == -1) {
3853 display_exit(display);
3854 return;
3855 }
3856 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003857
3858 if (events & EPOLLOUT) {
3859 ret = wl_display_flush(display->display);
3860 if (ret == 0) {
3861 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3862 ep.data.ptr = &display->display_task;
3863 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3864 display->display_fd, &ep);
3865 } else if (ret == -1 && errno != EAGAIN) {
3866 display_exit(display);
3867 return;
3868 }
3869 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003870}
3871
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003872struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003873display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003874{
3875 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003876
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003877 d = malloc(sizeof *d);
3878 if (d == NULL)
3879 return NULL;
3880
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003881 memset(d, 0, sizeof *d);
3882
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003883 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003884 if (d->display == NULL) {
3885 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003886 return NULL;
3887 }
3888
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003889 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003890 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003891 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003892 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3893 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003894
3895 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003896 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003897 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003898 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003899
Daniel Stone97f68542012-05-30 16:32:01 +01003900 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003901 if (d->xkb_context == NULL) {
3902 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003903 return NULL;
3904 }
3905
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003906 d->workspace = 0;
3907 d->workspace_count = 1;
3908
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003909 d->registry = wl_display_get_registry(d->display);
3910 wl_registry_add_listener(d->registry, &registry_listener, d);
3911 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003912#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003913 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003914 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003915#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003916
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003917 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003918
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003919 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003920
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003921 wl_list_init(&d->window_list);
3922
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003923 return d;
3924}
3925
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003926static void
3927display_destroy_outputs(struct display *display)
3928{
3929 struct output *tmp;
3930 struct output *output;
3931
3932 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3933 output_destroy(output);
3934}
3935
Pekka Paalanene1207c72011-12-16 12:02:09 +02003936static void
3937display_destroy_inputs(struct display *display)
3938{
3939 struct input *tmp;
3940 struct input *input;
3941
3942 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3943 input_destroy(input);
3944}
3945
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003946void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003947display_destroy(struct display *display)
3948{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003949 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003950 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3951 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003952
3953 if (!wl_list_empty(&display->deferred_list))
3954 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3955
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003956 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003957 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003958
Daniel Stone97f68542012-05-30 16:32:01 +01003959 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003960
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003961 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003962 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003963
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003964#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003965 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003966#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003967
Pekka Paalanenc2052982011-12-16 11:41:32 +02003968 if (display->shell)
3969 wl_shell_destroy(display->shell);
3970
3971 if (display->shm)
3972 wl_shm_destroy(display->shm);
3973
3974 if (display->data_device_manager)
3975 wl_data_device_manager_destroy(display->data_device_manager);
3976
3977 wl_compositor_destroy(display->compositor);
3978
3979 close(display->epoll_fd);
3980
U. Artie Eoff44874d92012-10-02 21:12:35 -07003981 if (!(display->display_fd_events & EPOLLERR) &&
3982 !(display->display_fd_events & EPOLLHUP))
3983 wl_display_flush(display->display);
3984
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003985 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003986 free(display);
3987}
3988
3989void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003990display_set_user_data(struct display *display, void *data)
3991{
3992 display->user_data = data;
3993}
3994
3995void *
3996display_get_user_data(struct display *display)
3997{
3998 return display->user_data;
3999}
4000
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004001struct wl_display *
4002display_get_display(struct display *display)
4003{
4004 return display->display;
4005}
4006
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004007struct output *
4008display_get_output(struct display *display)
4009{
4010 return container_of(display->output_list.next, struct output, link);
4011}
4012
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004013struct wl_compositor *
4014display_get_compositor(struct display *display)
4015{
4016 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004017}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004018
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004019uint32_t
4020display_get_serial(struct display *display)
4021{
4022 return display->serial;
4023}
4024
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004025EGLDisplay
4026display_get_egl_display(struct display *d)
4027{
4028 return d->dpy;
4029}
4030
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004031struct wl_data_source *
4032display_create_data_source(struct display *display)
4033{
4034 return wl_data_device_manager_create_data_source(display->data_device_manager);
4035}
4036
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004037EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004038display_get_argb_egl_config(struct display *d)
4039{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004040 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004041}
4042
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004043struct wl_shell *
4044display_get_shell(struct display *display)
4045{
4046 return display->shell;
4047}
4048
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004049int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004050display_acquire_window_surface(struct display *display,
4051 struct window *window,
4052 EGLContext ctx)
4053{
Benjamin Franzke22d54812011-07-16 19:50:32 +00004054#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004055 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004056 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004057
4058 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004059 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004060 device = cairo_surface_get_device(window->cairo_surface);
4061 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004062 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004063
Benjamin Franzke0c991632011-09-27 21:57:31 +02004064 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004065 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004066 ctx = display->argb_ctx;
4067 else
4068 assert(0);
4069 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004070
4071 data = cairo_surface_get_user_data(window->cairo_surface,
4072 &surface_data_key);
4073
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004074 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004075 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004076 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4077 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004078
4079 return 0;
4080#else
4081 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004082#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004083}
4084
4085void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004086display_release_window_surface(struct display *display,
4087 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004088{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004089#ifdef HAVE_CAIRO_EGL
4090 cairo_device_t *device;
4091
4092 device = cairo_surface_get_device(window->cairo_surface);
4093 if (!device)
4094 return;
4095
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004096 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004097 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004098 cairo_device_release(device);
4099#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004100}
4101
4102void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004103display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004104{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004105 wl_list_insert(&display->deferred_list, &task->link);
4106}
4107
4108void
4109display_watch_fd(struct display *display,
4110 int fd, uint32_t events, struct task *task)
4111{
4112 struct epoll_event ep;
4113
4114 ep.events = events;
4115 ep.data.ptr = task;
4116 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4117}
4118
4119void
4120display_run(struct display *display)
4121{
4122 struct task *task;
4123 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004124 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004125
Pekka Paalanen826d7952011-12-15 10:14:07 +02004126 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004127 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004128 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004129 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004130 struct task, link);
4131 wl_list_remove(&task->link);
4132 task->run(task, 0);
4133 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004134
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004135 wl_display_dispatch_pending(display->display);
4136
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004137 if (!display->running)
4138 break;
4139
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004140 ret = wl_display_flush(display->display);
4141 if (ret < 0 && errno == EAGAIN) {
4142 ep[0].events =
4143 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4144 ep[0].data.ptr = &display->display_task;
4145
4146 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4147 display->display_fd, &ep[0]);
4148 } else if (ret < 0) {
4149 break;
4150 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004151
4152 count = epoll_wait(display->epoll_fd,
4153 ep, ARRAY_LENGTH(ep), -1);
4154 for (i = 0; i < count; i++) {
4155 task = ep[i].data.ptr;
4156 task->run(task, ep[i].events);
4157 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004158 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004159}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004160
4161void
4162display_exit(struct display *display)
4163{
4164 display->running = 0;
4165}