blob: 4bbc6142c07c3255e775bf9a8fe1dcb09caca846 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040024#define _GNU_SOURCE
25
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026#include "../config.h"
27
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030031#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033#include <fcntl.h>
34#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040035#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020037#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050038#include <time.h>
39#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040040#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040041#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030042#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040043
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;
Pekka Paalanen35e82632013-04-25 13:57:48 +030089 struct wl_subcompositor *subcompositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040090 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040091 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040092 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060093 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020094 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040095 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050096 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020098 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040099 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400100
101 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700102 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400103 struct task display_task;
104
105 int epoll_fd;
106 struct wl_list deferred_list;
107
Pekka Paalanen826d7952011-12-15 10:14:07 +0200108 int running;
109
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400110 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400111 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500113 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400114
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400115 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400116
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300117 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300118 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400119
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400121 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133};
134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400136 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400138 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500139 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500141 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_CUSTOM
143};
Rob Bradford7507b572012-05-15 17:55:34 +0100144
145struct window_output {
146 struct output *output;
147 struct wl_list link;
148};
149
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150struct toysurface {
151 /*
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
155 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200156 * If flags has SURFACE_HINT_RESIZE set, the user is
157 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200158 * Returns the Cairo surface to draw to.
159 */
160 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200161 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200162
163 /*
164 * Post the surface to the server, returning the server allocation
165 * rectangle. The Cairo surface from prepare() must be destroyed
166 * after calling this.
167 */
168 void (*swap)(struct toysurface *base,
169 struct rectangle *server_allocation);
170
171 /*
172 * Make the toysurface current with the given EGL context.
173 * Returns 0 on success, and negative of failure.
174 */
175 int (*acquire)(struct toysurface *base, EGLContext ctx);
176
177 /*
178 * Release the toysurface from the EGL context, returning control
179 * to Cairo.
180 */
181 void (*release)(struct toysurface *base);
182
183 /*
184 * Destroy the toysurface, including the Cairo surface, any
185 * backing storage, and the Wayland protocol objects.
186 */
187 void (*destroy)(struct toysurface *base);
188};
189
Pekka Paalanen4e373742013-02-13 16:17:13 +0200190struct surface {
191 struct window *window;
192
193 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300194 struct wl_subsurface *subsurface;
195 int synchronized;
196 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200198 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300199 int redraw_needed;
200 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300201 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200202
203 struct rectangle allocation;
204 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200205
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200206 struct wl_region *input_region;
207 struct wl_region *opaque_region;
208
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209 enum window_buffer_type buffer_type;
210 enum wl_output_transform buffer_transform;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200211
212 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300213
214 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200215};
216
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500217struct window {
218 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500219 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100220 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500221 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200222 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400223 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500224 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500225 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400226 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400227 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300228 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400229 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400230 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100231 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400232 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400233 int focus_count;
234
Pekka Paalanen99436862012-11-19 17:15:59 +0200235 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200236 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000237 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400238
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500239 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500240 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400241 window_data_handler_t data_handler;
242 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500243 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400244 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200245 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400246
Pekka Paalanen4e373742013-02-13 16:17:13 +0200247 struct surface *main_surface;
248 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300249
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200250 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500251
Pekka Paalanen35e82632013-04-25 13:57:48 +0300252 /* struct surface::link, contains also main_surface */
253 struct wl_list subsurface_list;
254
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500255 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400256 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500257};
258
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500259struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500260 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200261 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300262 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500263 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400264 struct wl_list link;
265 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500266 widget_resize_handler_t resize_handler;
267 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500268 widget_enter_handler_t enter_handler;
269 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500270 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500271 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200272 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400273 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500274 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300275 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500276 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400277};
278
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400279struct input {
280 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100281 struct wl_seat *seat;
282 struct wl_pointer *pointer;
283 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400284 struct window *pointer_focus;
285 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300286 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300287 uint32_t cursor_anim_start;
288 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300289 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400290 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400291 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400292 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400293 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400294 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400295
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500296 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500297 struct widget *grab;
298 uint32_t grab_button;
299
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400300 struct wl_data_device *data_device;
301 struct data_offer *drag_offer;
302 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100303
304 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100305 struct xkb_keymap *keymap;
306 struct xkb_state *state;
307 xkb_mod_mask_t control_mask;
308 xkb_mod_mask_t alt_mask;
309 xkb_mod_mask_t shift_mask;
310 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400311
312 struct task repeat_task;
313 int repeat_timer_fd;
314 uint32_t repeat_sym;
315 uint32_t repeat_key;
316 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400317};
318
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500319struct output {
320 struct display *display;
321 struct wl_output *output;
322 struct rectangle allocation;
323 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600324 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200325 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200326
327 display_output_handler_t destroy_handler;
328 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500329};
330
Martin Minarik1998b152012-05-10 02:04:35 +0200331enum frame_button_action {
332 FRAME_BUTTON_NULL = 0,
333 FRAME_BUTTON_ICON = 1,
334 FRAME_BUTTON_CLOSE = 2,
335 FRAME_BUTTON_MINIMIZE = 3,
336 FRAME_BUTTON_MAXIMIZE = 4,
337};
338
339enum frame_button_pointer {
340 FRAME_BUTTON_DEFAULT = 0,
341 FRAME_BUTTON_OVER = 1,
342 FRAME_BUTTON_ACTIVE = 2,
343};
344
345enum frame_button_align {
346 FRAME_BUTTON_RIGHT = 0,
347 FRAME_BUTTON_LEFT = 1,
348};
349
350enum frame_button_decoration {
351 FRAME_BUTTON_NONE = 0,
352 FRAME_BUTTON_FANCY = 1,
353};
354
355struct frame_button {
356 struct widget *widget;
357 struct frame *frame;
358 cairo_surface_t *icon;
359 enum frame_button_action type;
360 enum frame_button_pointer state;
361 struct wl_list link; /* buttons_list */
362 enum frame_button_align align;
363 enum frame_button_decoration decoration;
364};
365
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500366struct frame {
367 struct widget *widget;
368 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200369 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500370};
371
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500372struct menu {
373 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500374 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500375 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500376 const char **entries;
377 uint32_t time;
378 int current;
379 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400380 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500381 menu_func_t func;
382};
383
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300384struct tooltip {
385 struct widget *parent;
386 struct window *window;
387 struct widget *widget;
388 char *entry;
389 struct task tooltip_task;
390 int tooltip_fd;
391 float x, y;
392};
393
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700394struct shm_pool {
395 struct wl_shm_pool *pool;
396 size_t size;
397 size_t used;
398 void *data;
399};
400
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400401enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300402 CURSOR_DEFAULT = 100,
403 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400404};
405
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500406enum window_location {
407 WINDOW_INTERIOR = 0,
408 WINDOW_RESIZING_TOP = 1,
409 WINDOW_RESIZING_BOTTOM = 2,
410 WINDOW_RESIZING_LEFT = 4,
411 WINDOW_RESIZING_TOP_LEFT = 5,
412 WINDOW_RESIZING_BOTTOM_LEFT = 6,
413 WINDOW_RESIZING_RIGHT = 8,
414 WINDOW_RESIZING_TOP_RIGHT = 9,
415 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
416 WINDOW_RESIZING_MASK = 15,
417 WINDOW_EXTERIOR = 16,
418 WINDOW_TITLEBAR = 17,
419 WINDOW_CLIENT_AREA = 18,
420};
421
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200422static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423
Pekka Paalanen97777442013-05-22 10:20:05 +0300424/* #define DEBUG */
425
426#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300427
428static void
429debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
430__attribute__ ((format (printf, 4, 5)));
431
432static void
433debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
434{
435 va_list ap;
436 struct timeval tv;
437
438 gettimeofday(&tv, NULL);
439 fprintf(stderr, "%8ld.%03ld ",
440 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
441
442 if (proxy)
443 fprintf(stderr, "%s@%d ",
444 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
445
446 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
447 fprintf(stderr, "%s ", func);
448
449 va_start(ap, fmt);
450 vfprintf(stderr, fmt, ap);
451 va_end(ap);
452}
453
454#define DBG(fmt, ...) \
455 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
456
457#define DBG_OBJ(obj, fmt, ...) \
458 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
459
460#else
461
462#define DBG(...) do {} while (0)
463#define DBG_OBJ(...) do {} while (0)
464
465#endif
466
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500467#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400468
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200469struct egl_window_surface {
470 struct toysurface base;
471 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100472 struct display *display;
473 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200474 struct wl_egl_window *egl_window;
475 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100476};
477
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200478static struct egl_window_surface *
479to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100480{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200481 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100482}
483
484static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200485egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200486 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100487{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200488 struct egl_window_surface *surface = to_egl_window_surface(base);
489
490 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
491 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
492
493 return cairo_surface_reference(surface->cairo_surface);
494}
495
496static void
497egl_window_surface_swap(struct toysurface *base,
498 struct rectangle *server_allocation)
499{
500 struct egl_window_surface *surface = to_egl_window_surface(base);
501
502 cairo_gl_surface_swapbuffers(surface->cairo_surface);
503 wl_egl_window_get_attached_size(surface->egl_window,
504 &server_allocation->width,
505 &server_allocation->height);
506}
507
508static int
509egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
510{
511 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200512 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100513
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200514 device = cairo_surface_get_device(surface->cairo_surface);
515 if (!device)
516 return -1;
517
518 if (!ctx) {
519 if (device == surface->display->argb_device)
520 ctx = surface->display->argb_ctx;
521 else
522 assert(0);
523 }
524
525 cairo_device_flush(device);
526 cairo_device_acquire(device);
527 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
528 surface->egl_surface, ctx))
529 fprintf(stderr, "failed to make surface current\n");
530
531 return 0;
532}
533
534static void
535egl_window_surface_release(struct toysurface *base)
536{
537 struct egl_window_surface *surface = to_egl_window_surface(base);
538 cairo_device_t *device;
539
540 device = cairo_surface_get_device(surface->cairo_surface);
541 if (!device)
542 return;
543
544 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
545 surface->display->argb_ctx))
546 fprintf(stderr, "failed to make context current\n");
547
548 cairo_device_release(device);
549}
550
551static void
552egl_window_surface_destroy(struct toysurface *base)
553{
554 struct egl_window_surface *surface = to_egl_window_surface(base);
555 struct display *d = surface->display;
556
557 cairo_surface_destroy(surface->cairo_surface);
558 eglDestroySurface(d->dpy, surface->egl_surface);
559 wl_egl_window_destroy(surface->egl_window);
560 surface->surface = NULL;
561
562 free(surface);
563}
564
565static struct toysurface *
566egl_window_surface_create(struct display *display,
567 struct wl_surface *wl_surface,
568 uint32_t flags,
569 struct rectangle *rectangle)
570{
571 struct egl_window_surface *surface;
572
Pekka Paalanenb3627362012-11-19 17:16:00 +0200573 if (display->dpy == EGL_NO_DISPLAY)
574 return NULL;
575
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200576 surface = calloc(1, sizeof *surface);
577 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100578 return NULL;
579
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200580 surface->base.prepare = egl_window_surface_prepare;
581 surface->base.swap = egl_window_surface_swap;
582 surface->base.acquire = egl_window_surface_acquire;
583 surface->base.release = egl_window_surface_release;
584 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100585
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200586 surface->display = display;
587 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400588
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200589 surface->egl_window = wl_egl_window_create(surface->surface,
590 rectangle->width,
591 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100592
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200593 surface->egl_surface = eglCreateWindowSurface(display->dpy,
594 display->argb_config,
595 surface->egl_window,
596 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100597
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200598 surface->cairo_surface =
599 cairo_gl_surface_create_for_egl(display->argb_device,
600 surface->egl_surface,
601 rectangle->width,
602 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100603
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200604 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100605}
606
Pekka Paalanenb3627362012-11-19 17:16:00 +0200607#else
608
609static struct toysurface *
610egl_window_surface_create(struct display *display,
611 struct wl_surface *wl_surface,
612 uint32_t flags,
613 struct rectangle *rectangle)
614{
615 return NULL;
616}
617
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618#endif
619
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200620struct shm_surface_data {
621 struct wl_buffer *buffer;
622 struct shm_pool *pool;
623};
624
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400625struct wl_buffer *
626display_get_buffer_for_surface(struct display *display,
627 cairo_surface_t *surface)
628{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200629 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400630
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200631 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632
633 return data->buffer;
634}
635
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500636static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700637shm_pool_destroy(struct shm_pool *pool);
638
639static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400640shm_surface_data_destroy(void *p)
641{
642 struct shm_surface_data *data = p;
643
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200644 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700645 if (data->pool)
646 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300647
648 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649}
650
Kristian Høgsberg16626282012-04-03 11:21:27 -0400651static struct wl_shm_pool *
652make_shm_pool(struct display *display, int size, void **data)
653{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400654 struct wl_shm_pool *pool;
655 int fd;
656
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300657 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400658 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300659 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
660 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400661 return NULL;
662 }
663
664 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400665 if (*data == MAP_FAILED) {
666 fprintf(stderr, "mmap failed: %m\n");
667 close(fd);
668 return NULL;
669 }
670
671 pool = wl_shm_create_pool(display->shm, fd, size);
672
673 close(fd);
674
675 return pool;
676}
677
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700678static struct shm_pool *
679shm_pool_create(struct display *display, size_t size)
680{
681 struct shm_pool *pool = malloc(sizeof *pool);
682
683 if (!pool)
684 return NULL;
685
686 pool->pool = make_shm_pool(display, size, &pool->data);
687 if (!pool->pool) {
688 free(pool);
689 return NULL;
690 }
691
692 pool->size = size;
693 pool->used = 0;
694
695 return pool;
696}
697
698static void *
699shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
700{
701 if (pool->used + size > pool->size)
702 return NULL;
703
704 *offset = pool->used;
705 pool->used += size;
706
707 return (char *) pool->data + *offset;
708}
709
710/* destroy the pool. this does not unmap the memory though */
711static void
712shm_pool_destroy(struct shm_pool *pool)
713{
714 munmap(pool->data, pool->size);
715 wl_shm_pool_destroy(pool->pool);
716 free(pool);
717}
718
719/* Start allocating from the beginning of the pool again */
720static void
721shm_pool_reset(struct shm_pool *pool)
722{
723 pool->used = 0;
724}
725
726static int
727data_length_for_shm_surface(struct rectangle *rect)
728{
729 int stride;
730
731 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
732 rect->width);
733 return stride * rect->height;
734}
735
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500736static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700737display_create_shm_surface_from_pool(struct display *display,
738 struct rectangle *rectangle,
739 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400740{
741 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400742 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400743 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700744 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400745 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400746
747 data = malloc(sizeof *data);
748 if (data == NULL)
749 return NULL;
750
751 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
752 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700753 length = stride * rectangle->height;
754 data->pool = NULL;
755 map = shm_pool_allocate(pool, length, &offset);
756
757 if (!map) {
758 free(data);
759 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400760 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400761
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400762 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400763 CAIRO_FORMAT_ARGB32,
764 rectangle->width,
765 rectangle->height,
766 stride);
767
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200768 cairo_surface_set_user_data(surface, &shm_surface_data_key,
769 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400770
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400771 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500772 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400773 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500774 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400775
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200776 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
777 rectangle->width,
778 rectangle->height,
779 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400780
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700781 return surface;
782}
783
784static cairo_surface_t *
785display_create_shm_surface(struct display *display,
786 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200787 struct shm_pool *alternate_pool,
788 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700789{
790 struct shm_surface_data *data;
791 struct shm_pool *pool;
792 cairo_surface_t *surface;
793
Pekka Paalanen99436862012-11-19 17:15:59 +0200794 if (alternate_pool) {
795 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700796 surface = display_create_shm_surface_from_pool(display,
797 rectangle,
798 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200799 alternate_pool);
800 if (surface) {
801 data = cairo_surface_get_user_data(surface,
802 &shm_surface_data_key);
803 goto out;
804 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700805 }
806
807 pool = shm_pool_create(display,
808 data_length_for_shm_surface(rectangle));
809 if (!pool)
810 return NULL;
811
812 surface =
813 display_create_shm_surface_from_pool(display, rectangle,
814 flags, pool);
815
816 if (!surface) {
817 shm_pool_destroy(pool);
818 return NULL;
819 }
820
821 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200822 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700823 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824
Pekka Paalanen99436862012-11-19 17:15:59 +0200825out:
826 if (data_ret)
827 *data_ret = data;
828
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400829 return surface;
830}
831
nobled7b87cb02011-02-01 18:51:47 +0000832static int
833check_size(struct rectangle *rect)
834{
835 if (rect->width && rect->height)
836 return 0;
837
838 fprintf(stderr, "tried to create surface of "
839 "width: %d, height: %d\n", rect->width, rect->height);
840 return -1;
841}
842
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400843cairo_surface_t *
844display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400846 struct rectangle *rectangle,
847 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400848{
nobled7b87cb02011-02-01 18:51:47 +0000849 if (check_size(rectangle) < 0)
850 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200851
852 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200853 return display_create_shm_surface(display, rectangle, flags,
854 NULL, NULL);
855}
856
Pekka Paalanena4eda732012-11-19 17:16:02 +0200857struct shm_surface_leaf {
858 cairo_surface_t *cairo_surface;
859 /* 'data' is automatically destroyed, when 'cairo_surface' is */
860 struct shm_surface_data *data;
861
862 struct shm_pool *resize_pool;
863 int busy;
864};
865
866static void
867shm_surface_leaf_release(struct shm_surface_leaf *leaf)
868{
869 if (leaf->cairo_surface)
870 cairo_surface_destroy(leaf->cairo_surface);
871 /* leaf->data already destroyed via cairo private */
872
873 if (leaf->resize_pool)
874 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200875
876 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200877}
878
Pekka Paalanenaef02542013-04-25 13:57:47 +0300879#define MAX_LEAVES 3
880
Pekka Paalanen99436862012-11-19 17:15:59 +0200881struct shm_surface {
882 struct toysurface base;
883 struct display *display;
884 struct wl_surface *surface;
885 uint32_t flags;
886 int dx, dy;
887
Pekka Paalanenaef02542013-04-25 13:57:47 +0300888 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200889 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200890};
891
892static struct shm_surface *
893to_shm_surface(struct toysurface *base)
894{
895 return container_of(base, struct shm_surface, base);
896}
897
Pekka Paalanena4eda732012-11-19 17:16:02 +0200898static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300899shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
900{
901#ifdef DEBUG
902 struct shm_surface_leaf *leaf;
903 char bufs[MAX_LEAVES + 1];
904 int i;
905
906 for (i = 0; i < MAX_LEAVES; i++) {
907 leaf = &surface->leaf[i];
908
909 if (leaf->busy)
910 bufs[i] = 'b';
911 else if (leaf->cairo_surface)
912 bufs[i] = 'a';
913 else
914 bufs[i] = ' ';
915 }
916
917 bufs[MAX_LEAVES] = '\0';
918 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
919#endif
920}
921
922static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200923shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
924{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200925 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300926 struct shm_surface_leaf *leaf;
927 int i;
928 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300929
930 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200931
Pekka Paalanenaef02542013-04-25 13:57:47 +0300932 for (i = 0; i < MAX_LEAVES; i++) {
933 leaf = &surface->leaf[i];
934 if (leaf->data && leaf->data->buffer == buffer) {
935 leaf->busy = 0;
936 break;
937 }
938 }
939 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200940
Pekka Paalanenaef02542013-04-25 13:57:47 +0300941 /* Leave one free leaf with storage, release others */
942 free_found = 0;
943 for (i = 0; i < MAX_LEAVES; i++) {
944 leaf = &surface->leaf[i];
945
946 if (!leaf->cairo_surface || leaf->busy)
947 continue;
948
949 if (!free_found)
950 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300951 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300952 shm_surface_leaf_release(leaf);
953 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300954
Pekka Paalanen97777442013-05-22 10:20:05 +0300955 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200956}
957
958static const struct wl_buffer_listener shm_surface_buffer_listener = {
959 shm_surface_buffer_release
960};
961
Pekka Paalanen99436862012-11-19 17:15:59 +0200962static cairo_surface_t *
963shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200964 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200965{
Pekka Paalanenec076692012-11-30 13:37:27 +0200966 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200967 struct shm_surface *surface = to_shm_surface(base);
968 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanenaef02542013-04-25 13:57:47 +0300969 struct shm_surface_leaf *leaf = NULL;
970 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200971
972 surface->dx = dx;
973 surface->dy = dy;
974
Pekka Paalanenaef02542013-04-25 13:57:47 +0300975 /* pick a free buffer, preferrably one that already has storage */
976 for (i = 0; i < MAX_LEAVES; i++) {
977 if (surface->leaf[i].busy)
978 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200979
Pekka Paalanenaef02542013-04-25 13:57:47 +0300980 if (!leaf || surface->leaf[i].cairo_surface)
981 leaf = &surface->leaf[i];
982 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300983 DBG_OBJ(surface->surface, "pick leaf %d\n",
984 (int)(leaf - &surface->leaf[0]));
985
Pekka Paalanenaef02542013-04-25 13:57:47 +0300986 if (!leaf) {
987 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +0200988 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +0300989 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200990 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200991 }
992
Pekka Paalanena4eda732012-11-19 17:16:02 +0200993 if (!resize_hint && leaf->resize_pool) {
994 cairo_surface_destroy(leaf->cairo_surface);
995 leaf->cairo_surface = NULL;
996 shm_pool_destroy(leaf->resize_pool);
997 leaf->resize_pool = NULL;
998 }
999
1000 if (leaf->cairo_surface &&
1001 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1002 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001003 goto out;
1004
Pekka Paalanena4eda732012-11-19 17:16:02 +02001005 if (leaf->cairo_surface)
1006 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001007
Pekka Paalanena4eda732012-11-19 17:16:02 +02001008 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001009 /* Create a big pool to allocate from, while continuously
1010 * resizing. Mmapping a new pool in the server
1011 * is relatively expensive, so reusing a pool performs
1012 * better, but may temporarily reserve unneeded memory.
1013 */
1014 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001015 leaf->resize_pool = shm_pool_create(surface->display,
1016 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001017 }
1018
Pekka Paalanena4eda732012-11-19 17:16:02 +02001019 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001020 display_create_shm_surface(surface->display, &rect,
1021 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001022 leaf->resize_pool,
1023 &leaf->data);
1024 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001025 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001026
1027out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001028 surface->current = leaf;
1029
1030 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001031}
1032
1033static void
1034shm_surface_swap(struct toysurface *base,
1035 struct rectangle *server_allocation)
1036{
1037 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001038 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001039
1040 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001041 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001042 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001043 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001044
Pekka Paalanena4eda732012-11-19 17:16:02 +02001045 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001046 surface->dx, surface->dy);
1047 wl_surface_damage(surface->surface, 0, 0,
1048 server_allocation->width, server_allocation->height);
1049 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001050
Pekka Paalanen71233882013-04-25 13:57:53 +03001051 DBG_OBJ(surface->surface, "leaf %d busy\n",
1052 (int)(leaf - &surface->leaf[0]));
1053
Pekka Paalanena4eda732012-11-19 17:16:02 +02001054 leaf->busy = 1;
1055 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001056}
1057
1058static int
1059shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1060{
1061 return -1;
1062}
1063
1064static void
1065shm_surface_release(struct toysurface *base)
1066{
1067}
1068
1069static void
1070shm_surface_destroy(struct toysurface *base)
1071{
1072 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001073 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001074
Pekka Paalanenaef02542013-04-25 13:57:47 +03001075 for (i = 0; i < MAX_LEAVES; i++)
1076 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001077
1078 free(surface);
1079}
1080
1081static struct toysurface *
1082shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1083 uint32_t flags, struct rectangle *rectangle)
1084{
1085 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001086 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001087
1088 surface = calloc(1, sizeof *surface);
1089 if (!surface)
1090 return NULL;
1091
1092 surface->base.prepare = shm_surface_prepare;
1093 surface->base.swap = shm_surface_swap;
1094 surface->base.acquire = shm_surface_acquire;
1095 surface->base.release = shm_surface_release;
1096 surface->base.destroy = shm_surface_destroy;
1097
1098 surface->display = display;
1099 surface->surface = wl_surface;
1100 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001101
1102 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001103}
1104
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001105/*
1106 * The following correspondences between file names and cursors was copied
1107 * from: https://bugs.kde.org/attachment.cgi?id=67313
1108 */
1109
1110static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001111 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001112 "sw-resize",
1113 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001114};
1115
1116static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001117 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001118 "se-resize",
1119 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001120};
1121
1122static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001123 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001124 "s-resize",
1125 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001126};
1127
1128static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001129 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001130 "closedhand",
1131 "208530c400c041818281048008011002"
1132};
1133
1134static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001135 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001136 "default",
1137 "top_left_arrow",
1138 "left-arrow"
1139};
1140
1141static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001142 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001143 "w-resize",
1144 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001145};
1146
1147static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001148 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001149 "e-resize",
1150 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001151};
1152
1153static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001154 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001155 "nw-resize",
1156 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001157};
1158
1159static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001160 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001161 "ne-resize",
1162 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001163};
1164
1165static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001166 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001167 "n-resize",
1168 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001169};
1170
1171static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001172 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001173 "ibeam",
1174 "text"
1175};
1176
1177static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001178 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001179 "pointer",
1180 "pointing_hand",
1181 "e29285e634086352946a0e7090d73106"
1182};
1183
1184static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001185 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001186 "wait",
1187 "0426c94ea35c87780ff01dc239897213"
1188};
1189
1190struct cursor_alternatives {
1191 const char **names;
1192 size_t count;
1193};
1194
1195static const struct cursor_alternatives cursors[] = {
1196 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1197 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1198 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1199 {grabbings, ARRAY_LENGTH(grabbings)},
1200 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1201 {left_sides, ARRAY_LENGTH(left_sides)},
1202 {right_sides, ARRAY_LENGTH(right_sides)},
1203 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1204 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1205 {top_sides, ARRAY_LENGTH(top_sides)},
1206 {xterms, ARRAY_LENGTH(xterms)},
1207 {hand1s, ARRAY_LENGTH(hand1s)},
1208 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001209};
1210
1211static void
1212create_cursors(struct display *display)
1213{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001214 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001215 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001216 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001217 unsigned int i, j;
1218 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001219 struct config_key shell_keys[] = {
1220 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001221 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001222 };
1223 struct config_section cs[] = {
1224 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1225 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001226
Ossama Othmana50e6e42013-05-14 09:48:26 -07001227 config_fd = open_config_file("weston.ini");
1228 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1229 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001230
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001231 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232 display->cursors =
1233 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1234
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001235 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236 cursor = NULL;
1237 for (j = 0; !cursor && j < cursors[i].count; ++j)
1238 cursor = wl_cursor_theme_get_cursor(
1239 display->cursor_theme, cursors[i].names[j]);
1240
1241 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001242 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001243 cursors[i].names[0]);
1244
1245 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001246 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001247}
1248
1249static void
1250destroy_cursors(struct display *display)
1251{
1252 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001253 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001254}
1255
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001256struct wl_cursor_image *
1257display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001258{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001259 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001261 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001262}
1263
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001264static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001265surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001266{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001267 if (!surface->cairo_surface)
1268 return;
1269
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001270 if (surface->opaque_region) {
1271 wl_surface_set_opaque_region(surface->surface,
1272 surface->opaque_region);
1273 wl_region_destroy(surface->opaque_region);
1274 surface->opaque_region = NULL;
1275 }
1276
1277 if (surface->input_region) {
1278 wl_surface_set_input_region(surface->surface,
1279 surface->input_region);
1280 wl_region_destroy(surface->input_region);
1281 surface->input_region = NULL;
1282 }
1283
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001284 surface->toysurface->swap(surface->toysurface,
1285 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001286
Pekka Paalanen89dee002013-02-13 16:17:20 +02001287 cairo_surface_destroy(surface->cairo_surface);
1288 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001289}
1290
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001291int
1292window_has_focus(struct window *window)
1293{
1294 return window->focus_count > 0;
1295}
1296
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001297static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001298window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001299{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001300 struct surface *surface;
1301
Pekka Paalanen89dee002013-02-13 16:17:20 +02001302 if (window->type == TYPE_NONE) {
1303 window->type = TYPE_TOPLEVEL;
1304 if (window->shell_surface)
1305 wl_shell_surface_set_toplevel(window->shell_surface);
1306 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001307
Pekka Paalanen35e82632013-04-25 13:57:48 +03001308 wl_list_for_each(surface, &window->subsurface_list, link) {
1309 if (surface == window->main_surface)
1310 continue;
1311
1312 surface_flush(surface);
1313 }
1314
Pekka Paalanen89dee002013-02-13 16:17:20 +02001315 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001316}
1317
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001318struct display *
1319window_get_display(struct window *window)
1320{
1321 return window->display;
1322}
1323
Pekka Paalanen89dee002013-02-13 16:17:20 +02001324static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001325surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001326{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001327 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001328 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001329
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001330 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001331 case WL_OUTPUT_TRANSFORM_90:
1332 case WL_OUTPUT_TRANSFORM_270:
1333 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1334 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001335 allocation.width = surface->allocation.height;
1336 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001337 break;
1338 default:
1339 break;
1340 }
1341
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001342 if (!surface->toysurface && display->dpy &&
1343 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001344 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001345 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001346 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001347 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001348 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001349 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001350
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001351 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001352 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001353 surface->surface,
1354 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001355
Pekka Paalanen89dee002013-02-13 16:17:20 +02001356 surface->cairo_surface = surface->toysurface->prepare(
1357 surface->toysurface, dx, dy,
1358 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001359}
1360
1361static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001362window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001363{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001364 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001365 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001366 int dx = 0;
1367 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001368
Pekka Paalanenec076692012-11-30 13:37:27 +02001369 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001370 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001371
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001372 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1373 dx = surface->server_allocation.width -
1374 surface->allocation.width;
1375
1376 if (window->resize_edges & WINDOW_RESIZING_TOP)
1377 dy = surface->server_allocation.height -
1378 surface->allocation.height;
1379
1380 window->resize_edges = 0;
1381
Pekka Paalanen89dee002013-02-13 16:17:20 +02001382 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001383}
1384
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001385int
1386window_get_buffer_transform(struct window *window)
1387{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001388 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001389}
1390
1391void
1392window_set_buffer_transform(struct window *window,
1393 enum wl_output_transform transform)
1394{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001395 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001396 wl_surface_set_buffer_transform(window->main_surface->surface,
1397 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001398}
1399
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001400static void frame_destroy(struct frame *frame);
1401
Pekka Paalanen4e373742013-02-13 16:17:13 +02001402static void
1403surface_destroy(struct surface *surface)
1404{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001405 if (surface->frame_cb)
1406 wl_callback_destroy(surface->frame_cb);
1407
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001408 if (surface->input_region)
1409 wl_region_destroy(surface->input_region);
1410
1411 if (surface->opaque_region)
1412 wl_region_destroy(surface->opaque_region);
1413
Pekka Paalanen35e82632013-04-25 13:57:48 +03001414 if (surface->subsurface)
1415 wl_subsurface_destroy(surface->subsurface);
1416
Pekka Paalanen4e373742013-02-13 16:17:13 +02001417 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418
1419 if (surface->toysurface)
1420 surface->toysurface->destroy(surface->toysurface);
1421
Pekka Paalanen35e82632013-04-25 13:57:48 +03001422 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001423 free(surface);
1424}
1425
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001426void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001427window_destroy(struct window *window)
1428{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001429 struct display *display = window->display;
1430 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001431 struct window_output *window_output;
1432 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001433
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001434 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001435
1436 wl_list_for_each(input, &display->input_list, link) {
1437 if (input->pointer_focus == window)
1438 input->pointer_focus = NULL;
1439 if (input->keyboard_focus == window)
1440 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001441 if (input->focus_widget &&
1442 input->focus_widget->window == window)
1443 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001444 }
1445
Rob Bradford7507b572012-05-15 17:55:34 +01001446 wl_list_for_each_safe(window_output, window_output_tmp,
1447 &window->window_output_list, link) {
1448 free (window_output);
1449 }
1450
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001451 if (window->frame)
1452 frame_destroy(window->frame);
1453
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001454 if (window->shell_surface)
1455 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001456
1457 surface_destroy(window->main_surface);
1458
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001459 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001460
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001461 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001462 free(window);
1463}
1464
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001465static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001466widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001467{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001468 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001469
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001470 wl_list_for_each(child, &widget->child_list, link) {
1471 target = widget_find_widget(child, x, y);
1472 if (target)
1473 return target;
1474 }
1475
1476 if (widget->allocation.x <= x &&
1477 x < widget->allocation.x + widget->allocation.width &&
1478 widget->allocation.y <= y &&
1479 y < widget->allocation.y + widget->allocation.height) {
1480 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001481 }
1482
1483 return NULL;
1484}
1485
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001486static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001487window_find_widget(struct window *window, int32_t x, int32_t y)
1488{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001489 struct surface *surface;
1490 struct widget *widget;
1491
1492 wl_list_for_each(surface, &window->subsurface_list, link) {
1493 widget = widget_find_widget(surface->widget, x, y);
1494 if (widget)
1495 return widget;
1496 }
1497
1498 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001499}
1500
1501static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001502widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001503{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001504 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001505
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001506 widget = malloc(sizeof *widget);
1507 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001508 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001509 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001510 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001511 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001512 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001513 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001514 widget->tooltip = NULL;
1515 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001516 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001517
1518 return widget;
1519}
1520
1521struct widget *
1522window_add_widget(struct window *window, void *data)
1523{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001524 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001525
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001526 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001527 wl_list_init(&widget->link);
1528 window->main_surface->widget = widget;
1529
1530 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001531}
1532
1533struct widget *
1534widget_add_widget(struct widget *parent, void *data)
1535{
1536 struct widget *widget;
1537
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001538 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001539 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001540
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001541 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001542}
1543
1544void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001545widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001546{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001547 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001548 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001549 struct input *input;
1550
Pekka Paalanen35e82632013-04-25 13:57:48 +03001551 /* Destroy the sub-surface along with the root widget */
1552 if (surface->widget == widget && surface->subsurface)
1553 surface_destroy(widget->surface);
1554
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001555 if (widget->tooltip) {
1556 free(widget->tooltip);
1557 widget->tooltip = NULL;
1558 }
1559
Pekka Paalanene156fb62012-01-19 13:51:38 +02001560 wl_list_for_each(input, &display->input_list, link) {
1561 if (input->focus_widget == widget)
1562 input->focus_widget = NULL;
1563 }
1564
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001565 wl_list_remove(&widget->link);
1566 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001567}
1568
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001569void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001570widget_set_default_cursor(struct widget *widget, int cursor)
1571{
1572 widget->default_cursor = cursor;
1573}
1574
1575void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001576widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001577{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001578 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001579}
1580
1581void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001582widget_set_size(struct widget *widget, int32_t width, int32_t height)
1583{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001584 widget->allocation.width = width;
1585 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001586}
1587
1588void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001589widget_set_allocation(struct widget *widget,
1590 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001591{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001592 widget->allocation.x = x;
1593 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001594 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001595}
1596
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001597void
1598widget_set_transparent(struct widget *widget, int transparent)
1599{
1600 widget->opaque = !transparent;
1601}
1602
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001603void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001604widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001605{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001606 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001607}
1608
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001609static cairo_surface_t *
1610widget_get_cairo_surface(struct widget *widget)
1611{
1612 struct surface *surface = widget->surface;
1613 struct window *window = widget->window;
1614
1615 if (!surface->cairo_surface) {
1616 if (surface == window->main_surface)
1617 window_create_main_surface(window);
1618 else
1619 surface_create_surface(surface, 0, 0, 0);
1620 }
1621
1622 return surface->cairo_surface;
1623}
1624
Alexander Larsson15901f02013-05-22 14:41:25 +02001625static void
1626widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1627{
1628 struct surface *surface = widget->surface;
1629 double angle;
1630 cairo_matrix_t m;
1631 enum wl_output_transform transform;
1632 int surface_width, surface_height;
1633 int translate_x, translate_y;
1634
1635 surface_width = surface->allocation.width;
1636 surface_height = surface->allocation.height;
1637
1638 transform = window_get_buffer_transform(widget->window);
1639 switch (transform) {
1640 case WL_OUTPUT_TRANSFORM_FLIPPED:
1641 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1642 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1643 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1644 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1645 break;
1646 default:
1647 cairo_matrix_init_identity(&m);
1648 break;
1649 }
1650
1651 switch (transform) {
1652 case WL_OUTPUT_TRANSFORM_NORMAL:
1653 default:
1654 angle = 0;
1655 translate_x = 0;
1656 translate_y = 0;
1657 break;
1658 case WL_OUTPUT_TRANSFORM_FLIPPED:
1659 angle = 0;
1660 translate_x = surface_width;
1661 translate_y = 0;
1662 break;
1663 case WL_OUTPUT_TRANSFORM_90:
1664 angle = M_PI_2;
1665 translate_x = surface_height;
1666 translate_y = 0;
1667 break;
1668 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1669 angle = M_PI_2;
1670 translate_x = surface_height;
1671 translate_y = surface_width;
1672 break;
1673 case WL_OUTPUT_TRANSFORM_180:
1674 angle = M_PI;
1675 translate_x = surface_width;
1676 translate_y = surface_height;
1677 break;
1678 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1679 angle = M_PI;
1680 translate_x = 0;
1681 translate_y = surface_height;
1682 break;
1683 case WL_OUTPUT_TRANSFORM_270:
1684 angle = M_PI + M_PI_2;
1685 translate_x = 0;
1686 translate_y = surface_width;
1687 break;
1688 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1689 angle = M_PI + M_PI_2;
1690 translate_x = 0;
1691 translate_y = 0;
1692 break;
1693 }
1694
1695 cairo_translate(cr, translate_x, translate_y);
1696 cairo_rotate(cr, angle);
1697 cairo_transform(cr, &m);
1698}
1699
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001700cairo_t *
1701widget_cairo_create(struct widget *widget)
1702{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001703 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001704 cairo_surface_t *cairo_surface;
1705 cairo_t *cr;
1706
1707 cairo_surface = widget_get_cairo_surface(widget);
1708 cr = cairo_create(cairo_surface);
1709
Alexander Larsson15901f02013-05-22 14:41:25 +02001710 widget_cairo_update_transform(widget, cr);
1711
Pekka Paalanen35e82632013-04-25 13:57:48 +03001712 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1713
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001714 return cr;
1715}
1716
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001717struct wl_surface *
1718widget_get_wl_surface(struct widget *widget)
1719{
1720 return widget->surface->surface;
1721}
1722
1723uint32_t
1724widget_get_last_time(struct widget *widget)
1725{
1726 return widget->surface->last_time;
1727}
1728
1729void
1730widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1731{
1732 struct wl_compositor *comp = widget->window->display->compositor;
1733 struct surface *surface = widget->surface;
1734
1735 if (!surface->input_region)
1736 surface->input_region = wl_compositor_create_region(comp);
1737
1738 if (rect) {
1739 wl_region_add(surface->input_region,
1740 rect->x, rect->y, rect->width, rect->height);
1741 }
1742}
1743
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001744void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001745widget_set_resize_handler(struct widget *widget,
1746 widget_resize_handler_t handler)
1747{
1748 widget->resize_handler = handler;
1749}
1750
1751void
1752widget_set_redraw_handler(struct widget *widget,
1753 widget_redraw_handler_t handler)
1754{
1755 widget->redraw_handler = handler;
1756}
1757
1758void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001759widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001760{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001761 widget->enter_handler = handler;
1762}
1763
1764void
1765widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1766{
1767 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001768}
1769
1770void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001771widget_set_motion_handler(struct widget *widget,
1772 widget_motion_handler_t handler)
1773{
1774 widget->motion_handler = handler;
1775}
1776
1777void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001778widget_set_button_handler(struct widget *widget,
1779 widget_button_handler_t handler)
1780{
1781 widget->button_handler = handler;
1782}
1783
1784void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001785widget_set_axis_handler(struct widget *widget,
1786 widget_axis_handler_t handler)
1787{
1788 widget->axis_handler = handler;
1789}
1790
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001791static void
1792window_schedule_redraw_task(struct window *window);
1793
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001794void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001795widget_schedule_redraw(struct widget *widget)
1796{
Pekka Paalanen71233882013-04-25 13:57:53 +03001797 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001798 widget->surface->redraw_needed = 1;
1799 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001800}
1801
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001802cairo_surface_t *
1803window_get_surface(struct window *window)
1804{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001805 cairo_surface_t *cairo_surface;
1806
1807 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1808
1809 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001810}
1811
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001812struct wl_surface *
1813window_get_wl_surface(struct window *window)
1814{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001815 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001816}
1817
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001818struct wl_shell_surface *
1819window_get_wl_shell_surface(struct window *window)
1820{
1821 return window->shell_surface;
1822}
1823
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001824static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001825tooltip_redraw_handler(struct widget *widget, void *data)
1826{
1827 cairo_t *cr;
1828 const int32_t r = 3;
1829 struct tooltip *tooltip = data;
1830 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001831
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001832 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001833 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1834 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1835 cairo_paint(cr);
1836
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001837 width = widget->allocation.width;
1838 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001839 rounded_rect(cr, 0, 0, width, height, r);
1840
1841 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1842 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1843 cairo_fill(cr);
1844
1845 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1846 cairo_move_to(cr, 10, 16);
1847 cairo_show_text(cr, tooltip->entry);
1848 cairo_destroy(cr);
1849}
1850
1851static cairo_text_extents_t
1852get_text_extents(struct tooltip *tooltip)
1853{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001854 cairo_t *cr;
1855 cairo_text_extents_t extents;
1856
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001857 /* Use the dummy_surface because tooltip's surface was not
1858 * created yet, and parent does not have a valid surface
1859 * outside repaint, either.
1860 */
1861 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001862 cairo_text_extents(cr, tooltip->entry, &extents);
1863 cairo_destroy(cr);
1864
1865 return extents;
1866}
1867
1868static int
1869window_create_tooltip(struct tooltip *tooltip)
1870{
1871 struct widget *parent = tooltip->parent;
1872 struct display *display = parent->window->display;
1873 struct window *window;
1874 const int offset_y = 27;
1875 const int margin = 3;
1876 cairo_text_extents_t extents;
1877
1878 if (tooltip->widget)
1879 return 0;
1880
1881 window = window_create_transient(display, parent->window, tooltip->x,
1882 tooltip->y + offset_y,
1883 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1884 if (!window)
1885 return -1;
1886
1887 tooltip->window = window;
1888 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1889
1890 extents = get_text_extents(tooltip);
1891 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1892 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1893
1894 return 0;
1895}
1896
1897void
1898widget_destroy_tooltip(struct widget *parent)
1899{
1900 struct tooltip *tooltip = parent->tooltip;
1901
1902 parent->tooltip_count = 0;
1903 if (!tooltip)
1904 return;
1905
1906 if (tooltip->widget) {
1907 widget_destroy(tooltip->widget);
1908 window_destroy(tooltip->window);
1909 tooltip->widget = NULL;
1910 tooltip->window = NULL;
1911 }
1912
1913 close(tooltip->tooltip_fd);
1914 free(tooltip->entry);
1915 free(tooltip);
1916 parent->tooltip = NULL;
1917}
1918
1919static void
1920tooltip_func(struct task *task, uint32_t events)
1921{
1922 struct tooltip *tooltip =
1923 container_of(task, struct tooltip, tooltip_task);
1924 uint64_t exp;
1925
Martin Olsson8df662a2012-07-08 03:03:47 +02001926 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1927 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001928 window_create_tooltip(tooltip);
1929}
1930
1931#define TOOLTIP_TIMEOUT 500
1932static int
1933tooltip_timer_reset(struct tooltip *tooltip)
1934{
1935 struct itimerspec its;
1936
1937 its.it_interval.tv_sec = 0;
1938 its.it_interval.tv_nsec = 0;
1939 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1940 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1941 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1942 fprintf(stderr, "could not set timerfd\n: %m");
1943 return -1;
1944 }
1945
1946 return 0;
1947}
1948
1949int
1950widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1951{
1952 struct tooltip *tooltip = parent->tooltip;
1953
1954 parent->tooltip_count++;
1955 if (tooltip) {
1956 tooltip->x = x;
1957 tooltip->y = y;
1958 tooltip_timer_reset(tooltip);
1959 return 0;
1960 }
1961
1962 /* the handler might be triggered too fast via input device motion, so
1963 * we need this check here to make sure tooltip is fully initialized */
1964 if (parent->tooltip_count > 1)
1965 return 0;
1966
1967 tooltip = malloc(sizeof *tooltip);
1968 if (!tooltip)
1969 return -1;
1970
1971 parent->tooltip = tooltip;
1972 tooltip->parent = parent;
1973 tooltip->widget = NULL;
1974 tooltip->window = NULL;
1975 tooltip->x = x;
1976 tooltip->y = y;
1977 tooltip->entry = strdup(entry);
1978 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1979 if (tooltip->tooltip_fd < 0) {
1980 fprintf(stderr, "could not create timerfd\n: %m");
1981 return -1;
1982 }
1983
1984 tooltip->tooltip_task.run = tooltip_func;
1985 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1986 EPOLLIN, &tooltip->tooltip_task);
1987 tooltip_timer_reset(tooltip);
1988
1989 return 0;
1990}
1991
1992static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001993workspace_manager_state(void *data,
1994 struct workspace_manager *workspace_manager,
1995 uint32_t current,
1996 uint32_t count)
1997{
1998 struct display *display = data;
1999
2000 display->workspace = current;
2001 display->workspace_count = count;
2002}
2003
2004static const struct workspace_manager_listener workspace_manager_listener = {
2005 workspace_manager_state
2006};
2007
2008static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002009frame_resize_handler(struct widget *widget,
2010 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002011{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002012 struct frame *frame = data;
2013 struct widget *child = frame->child;
2014 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002015 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002016 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002017 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002018 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002019 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002020 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002021 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002022
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002023 switch (widget->window->type) {
2024 case TYPE_FULLSCREEN:
2025 decoration_width = 0;
2026 decoration_height = 0;
2027
2028 allocation.x = 0;
2029 allocation.y = 0;
2030 allocation.width = width;
2031 allocation.height = height;
2032 opaque_margin = 0;
2033
2034 wl_list_for_each(button, &frame->buttons_list, link)
2035 button->widget->opaque = 1;
2036 break;
2037 case TYPE_MAXIMIZED:
2038 decoration_width = t->width * 2;
2039 decoration_height = t->width + t->titlebar_height;
2040
2041 allocation.x = t->width;
2042 allocation.y = t->titlebar_height;
2043 allocation.width = width - decoration_width;
2044 allocation.height = height - decoration_height;
2045
2046 opaque_margin = 0;
2047
2048 wl_list_for_each(button, &frame->buttons_list, link)
2049 button->widget->opaque = 0;
2050 break;
2051 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002052 decoration_width = (t->width + t->margin) * 2;
2053 decoration_height = t->width +
2054 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002055
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002056 allocation.x = t->width + t->margin;
2057 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002058 allocation.width = width - decoration_width;
2059 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002060
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002061 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002062
2063 wl_list_for_each(button, &frame->buttons_list, link)
2064 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002065 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002066 }
2067
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002068 widget_set_allocation(child, allocation.x, allocation.y,
2069 allocation.width, allocation.height);
2070
2071 if (child->resize_handler)
2072 child->resize_handler(child,
2073 allocation.width,
2074 allocation.height,
2075 child->user_data);
2076
Scott Moreauf7e498c2012-05-14 11:39:29 -06002077 width = child->allocation.width + decoration_width;
2078 height = child->allocation.height + decoration_height;
2079
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002080 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2081
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002082 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002083 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002084 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002085 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002086 shadow_margin, shadow_margin,
2087 width - 2 * shadow_margin,
2088 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002089 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002090 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002091 }
2092
Scott Moreauf7e498c2012-05-14 11:39:29 -06002093 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002094
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002095 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002096 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002097 opaque_margin, opaque_margin,
2098 widget->allocation.width - 2 * opaque_margin,
2099 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002100
2101 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002102 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2103 x_l = t->width + shadow_margin;
2104 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002105 wl_list_for_each(button, &frame->buttons_list, link) {
2106 const int button_padding = 4;
2107 w = cairo_image_surface_get_width(button->icon);
2108 h = cairo_image_surface_get_height(button->icon);
2109
2110 if (button->decoration == FRAME_BUTTON_FANCY)
2111 w += 10;
2112
2113 if (button->align == FRAME_BUTTON_LEFT) {
2114 widget_set_allocation(button->widget,
2115 x_l, y , w + 1, h + 1);
2116 x_l += w;
2117 x_l += button_padding;
2118 } else {
2119 x_r -= w;
2120 widget_set_allocation(button->widget,
2121 x_r, y , w + 1, h + 1);
2122 x_r -= button_padding;
2123 }
2124 }
2125}
2126
2127static int
2128frame_button_enter_handler(struct widget *widget,
2129 struct input *input, float x, float y, void *data)
2130{
2131 struct frame_button *frame_button = data;
2132
2133 widget_schedule_redraw(frame_button->widget);
2134 frame_button->state = FRAME_BUTTON_OVER;
2135
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002136 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002137}
2138
2139static void
2140frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2141{
2142 struct frame_button *frame_button = data;
2143
2144 widget_schedule_redraw(frame_button->widget);
2145 frame_button->state = FRAME_BUTTON_DEFAULT;
2146}
2147
2148static void
2149frame_button_button_handler(struct widget *widget,
2150 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002151 uint32_t button,
2152 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002153{
2154 struct frame_button *frame_button = data;
2155 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002156 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002157
2158 if (button != BTN_LEFT)
2159 return;
2160
2161 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002162 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002163 frame_button->state = FRAME_BUTTON_ACTIVE;
2164 widget_schedule_redraw(frame_button->widget);
2165
2166 if (frame_button->type == FRAME_BUTTON_ICON)
2167 window_show_frame_menu(window, input, time);
2168 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002169 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002170 frame_button->state = FRAME_BUTTON_DEFAULT;
2171 widget_schedule_redraw(frame_button->widget);
2172 break;
2173 }
2174
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002175 if (!was_pressed)
2176 return;
2177
Martin Minarik1998b152012-05-10 02:04:35 +02002178 switch (frame_button->type) {
2179 case FRAME_BUTTON_CLOSE:
2180 if (window->close_handler)
2181 window->close_handler(window->parent,
2182 window->user_data);
2183 else
2184 display_exit(window->display);
2185 break;
2186 case FRAME_BUTTON_MINIMIZE:
2187 fprintf(stderr,"Minimize stub\n");
2188 break;
2189 case FRAME_BUTTON_MAXIMIZE:
2190 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2191 break;
2192 default:
2193 /* Unknown operation */
2194 break;
2195 }
2196}
2197
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002198static int
2199frame_button_motion_handler(struct widget *widget,
2200 struct input *input, uint32_t time,
2201 float x, float y, void *data)
2202{
2203 struct frame_button *frame_button = data;
2204 enum frame_button_pointer previous_button_state = frame_button->state;
2205
2206 /* only track state for a pressed button */
2207 if (input->grab != widget)
2208 return CURSOR_LEFT_PTR;
2209
2210 if (x > widget->allocation.x &&
2211 x < (widget->allocation.x + widget->allocation.width) &&
2212 y > widget->allocation.y &&
2213 y < (widget->allocation.y + widget->allocation.height)) {
2214 frame_button->state = FRAME_BUTTON_ACTIVE;
2215 } else {
2216 frame_button->state = FRAME_BUTTON_DEFAULT;
2217 }
2218
2219 if (frame_button->state != previous_button_state)
2220 widget_schedule_redraw(frame_button->widget);
2221
2222 return CURSOR_LEFT_PTR;
2223}
2224
Martin Minarik1998b152012-05-10 02:04:35 +02002225static void
2226frame_button_redraw_handler(struct widget *widget, void *data)
2227{
2228 struct frame_button *frame_button = data;
2229 cairo_t *cr;
2230 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002231
2232 x = widget->allocation.x;
2233 y = widget->allocation.y;
2234 width = widget->allocation.width;
2235 height = widget->allocation.height;
2236
2237 if (!width)
2238 return;
2239 if (!height)
2240 return;
2241 if (widget->opaque)
2242 return;
2243
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002244 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002245
2246 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2247 cairo_set_line_width(cr, 1);
2248
2249 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2250 cairo_rectangle (cr, x, y, 25, 16);
2251
2252 cairo_stroke_preserve(cr);
2253
2254 switch (frame_button->state) {
2255 case FRAME_BUTTON_DEFAULT:
2256 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2257 break;
2258 case FRAME_BUTTON_OVER:
2259 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2260 break;
2261 case FRAME_BUTTON_ACTIVE:
2262 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2263 break;
2264 }
2265
2266 cairo_fill (cr);
2267
2268 x += 4;
2269 }
2270
2271 cairo_set_source_surface(cr, frame_button->icon, x, y);
2272 cairo_paint(cr);
2273
2274 cairo_destroy(cr);
2275}
2276
2277static struct widget *
2278frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2279 enum frame_button_align align, enum frame_button_decoration style)
2280{
2281 struct frame_button *frame_button;
2282 const char *icon = data;
2283
2284 frame_button = malloc (sizeof *frame_button);
2285 memset(frame_button, 0, sizeof *frame_button);
2286
2287 frame_button->icon = cairo_image_surface_create_from_png(icon);
2288 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2289 frame_button->frame = frame;
2290 frame_button->type = type;
2291 frame_button->align = align;
2292 frame_button->decoration = style;
2293
2294 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2295
2296 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2297 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2298 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2299 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002300 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002301 return frame_button->widget;
2302}
2303
2304static void
2305frame_button_destroy(struct frame_button *frame_button)
2306{
2307 widget_destroy(frame_button->widget);
2308 wl_list_remove(&frame_button->link);
2309 cairo_surface_destroy(frame_button->icon);
2310 free(frame_button);
2311
2312 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002313}
2314
2315static void
2316frame_redraw_handler(struct widget *widget, void *data)
2317{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002318 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002319 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002320 struct theme *t = window->display->theme;
2321 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002322
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002323 if (window->type == TYPE_FULLSCREEN)
2324 return;
2325
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002326 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002327
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002328 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002329 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002330 if (window->type == TYPE_MAXIMIZED)
2331 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002332 theme_render_frame(t, cr, widget->allocation.width,
2333 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002334
2335 cairo_destroy(cr);
2336}
2337
2338static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002339frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002340{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002341 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002342 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002343 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002344
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002345 if (window->type != TYPE_TOPLEVEL)
2346 return CURSOR_LEFT_PTR;
2347
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002348 location = theme_get_location(t, input->sx, input->sy,
2349 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002350 frame->widget->allocation.height,
2351 window->type == TYPE_MAXIMIZED ?
2352 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002353
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002354 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002355 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002356 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002357 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002358 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002359 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002360 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002361 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002362 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002363 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002364 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002365 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002366 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002367 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002368 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002369 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002370 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002371 case THEME_LOCATION_EXTERIOR:
2372 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002373 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002374 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002375 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002376}
2377
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002378static void
2379frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002380{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002381 struct display *display;
2382
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002383 switch (index) {
2384 case 0: /* close */
2385 if (window->close_handler)
2386 window->close_handler(window->parent,
2387 window->user_data);
2388 else
2389 display_exit(window->display);
2390 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002391 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002392 display = window->display;
2393 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002394 workspace_manager_move_surface(
2395 display->workspace_manager,
2396 window->main_surface->surface,
2397 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002398 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002399 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002400 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002401 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002402 workspace_manager_move_surface(
2403 display->workspace_manager,
2404 window->main_surface->surface,
2405 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002406 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002407 case 3: /* fullscreen */
2408 /* we don't have a way to get out of fullscreen for now */
2409 if (window->fullscreen_handler)
2410 window->fullscreen_handler(window, window->user_data);
2411 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002412 }
2413}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002414
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002415void
2416window_show_frame_menu(struct window *window,
2417 struct input *input, uint32_t time)
2418{
2419 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002420 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002421
2422 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002423 "Close",
2424 "Move to workspace above", "Move to workspace below",
2425 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002426 };
2427
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002428 if (window->fullscreen_handler)
2429 count = ARRAY_LENGTH(entries);
2430 else
2431 count = ARRAY_LENGTH(entries) - 1;
2432
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002433 input_get_position(input, &x, &y);
2434 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002435 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002436}
2437
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002438static int
2439frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002440 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002441{
2442 return frame_get_pointer_image_for_location(data, input);
2443}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002444
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002445static int
2446frame_motion_handler(struct widget *widget,
2447 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002448 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002449{
2450 return frame_get_pointer_image_for_location(data, input);
2451}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002452
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002453static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002454frame_button_handler(struct widget *widget,
2455 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002456 uint32_t button, enum wl_pointer_button_state state,
2457 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002458
2459{
2460 struct frame *frame = data;
2461 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002462 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002463 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002464
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002465 if (window->type != TYPE_TOPLEVEL)
2466 return;
2467
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002468 location = theme_get_location(display->theme, input->sx, input->sy,
2469 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002470 frame->widget->allocation.height,
2471 window->type == TYPE_MAXIMIZED ?
2472 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002473
Daniel Stone4dbadb12012-05-30 16:31:51 +01002474 if (window->display->shell && button == BTN_LEFT &&
2475 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002476 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002477 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002478 if (!window->shell_surface)
2479 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002480 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002481 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002482 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002483 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002484 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002485 case THEME_LOCATION_RESIZING_TOP:
2486 case THEME_LOCATION_RESIZING_BOTTOM:
2487 case THEME_LOCATION_RESIZING_LEFT:
2488 case THEME_LOCATION_RESIZING_RIGHT:
2489 case THEME_LOCATION_RESIZING_TOP_LEFT:
2490 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2491 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2492 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002493 if (!window->shell_surface)
2494 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002495 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002496
Pekka Paalanen99436862012-11-19 17:15:59 +02002497 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002499 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002500 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002501 break;
2502 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002503 } else if (button == BTN_RIGHT &&
2504 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002505 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002506 }
2507}
2508
2509struct widget *
2510frame_create(struct window *window, void *data)
2511{
2512 struct frame *frame;
2513
2514 frame = malloc(sizeof *frame);
2515 memset(frame, 0, sizeof *frame);
2516
2517 frame->widget = window_add_widget(window, frame);
2518 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002519
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002520 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2521 widget_set_resize_handler(frame->widget, frame_resize_handler);
2522 widget_set_enter_handler(frame->widget, frame_enter_handler);
2523 widget_set_motion_handler(frame->widget, frame_motion_handler);
2524 widget_set_button_handler(frame->widget, frame_button_handler);
2525
Martin Minarik1998b152012-05-10 02:04:35 +02002526 /* Create empty list for frame buttons */
2527 wl_list_init(&frame->buttons_list);
2528
2529 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2530 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2531
2532 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2533 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2534
2535 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2536 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2537
2538 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2539 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2540
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002541 window->frame = frame;
2542
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002543 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002544}
2545
Kristian Høgsberga1627922012-06-20 17:30:03 -04002546void
2547frame_set_child_size(struct widget *widget, int child_width, int child_height)
2548{
2549 struct display *display = widget->window->display;
2550 struct theme *t = display->theme;
2551 int decoration_width, decoration_height;
2552 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002553 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002554
2555 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002556 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002557 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002558 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002559
2560 width = child_width + decoration_width;
2561 height = child_height + decoration_height;
2562 } else {
2563 width = child_width;
2564 height = child_height;
2565 }
2566
2567 window_schedule_resize(widget->window, width, height);
2568}
2569
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002570static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002571frame_destroy(struct frame *frame)
2572{
Martin Minarik1998b152012-05-10 02:04:35 +02002573 struct frame_button *button, *tmp;
2574
2575 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2576 frame_button_destroy(button);
2577
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002578 /* frame->child must be destroyed by the application */
2579 widget_destroy(frame->widget);
2580 free(frame);
2581}
2582
2583static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002584input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002585 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002586{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002587 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002588 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002589
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002590 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002591 return;
2592
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002593 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002594 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002595 widget = old;
2596 if (input->grab)
2597 widget = input->grab;
2598 if (widget->leave_handler)
2599 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002600 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002601 }
2602
2603 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002604 widget = focus;
2605 if (input->grab)
2606 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002607 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002608 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002609 cursor = widget->enter_handler(focus, input, x, y,
2610 widget->user_data);
2611 else
2612 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002613
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002614 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002615 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002616}
2617
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002618void
2619input_grab(struct input *input, struct widget *widget, uint32_t button)
2620{
2621 input->grab = widget;
2622 input->grab_button = button;
2623}
2624
2625void
2626input_ungrab(struct input *input)
2627{
2628 struct widget *widget;
2629
2630 input->grab = NULL;
2631 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002632 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002633 input->sx, input->sy);
2634 input_set_focus_widget(input, widget, input->sx, input->sy);
2635 }
2636}
2637
2638static void
2639input_remove_pointer_focus(struct input *input)
2640{
2641 struct window *window = input->pointer_focus;
2642
2643 if (!window)
2644 return;
2645
2646 input_set_focus_widget(input, NULL, 0, 0);
2647
2648 input->pointer_focus = NULL;
2649 input->current_cursor = CURSOR_UNSET;
2650}
2651
2652static void
2653pointer_handle_enter(void *data, struct wl_pointer *pointer,
2654 uint32_t serial, struct wl_surface *surface,
2655 wl_fixed_t sx_w, wl_fixed_t sy_w)
2656{
2657 struct input *input = data;
2658 struct window *window;
2659 struct widget *widget;
2660 float sx = wl_fixed_to_double(sx_w);
2661 float sy = wl_fixed_to_double(sy_w);
2662
2663 if (!surface) {
2664 /* enter event for a window we've just destroyed */
2665 return;
2666 }
2667
2668 input->display->serial = serial;
2669 input->pointer_enter_serial = serial;
2670 input->pointer_focus = wl_surface_get_user_data(surface);
2671 window = input->pointer_focus;
2672
Pekka Paalanen99436862012-11-19 17:15:59 +02002673 if (window->resizing) {
2674 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002675 /* Schedule a redraw to free the pool */
2676 window_schedule_redraw(window);
2677 }
2678
2679 input->sx = sx;
2680 input->sy = sy;
2681
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002682 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002683 input_set_focus_widget(input, widget, sx, sy);
2684}
2685
2686static void
2687pointer_handle_leave(void *data, struct wl_pointer *pointer,
2688 uint32_t serial, struct wl_surface *surface)
2689{
2690 struct input *input = data;
2691
2692 input->display->serial = serial;
2693 input_remove_pointer_focus(input);
2694}
2695
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002696static void
Daniel Stone37816df2012-05-16 18:45:18 +01002697pointer_handle_motion(void *data, struct wl_pointer *pointer,
2698 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002699{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002700 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002701 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002702 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002703 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002704 float sx = wl_fixed_to_double(sx_w);
2705 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002706
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002707 input->sx = sx;
2708 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002709
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002710 if (!window)
2711 return;
2712
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002713 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002714 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002715 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002716 }
2717
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002718 if (input->grab)
2719 widget = input->grab;
2720 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002721 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002722 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002723 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002724 input, time, sx, sy,
2725 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002726 else
2727 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002728
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002729 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002730}
2731
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002732static void
Daniel Stone37816df2012-05-16 18:45:18 +01002733pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002734 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002735{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002736 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002737 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002738 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002739
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002740 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002741 if (input->focus_widget && input->grab == NULL &&
2742 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002743 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002744
Neil Roberts6b28aad2012-01-23 19:11:18 +00002745 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002746 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002747 (*widget->button_handler)(widget,
2748 input, time,
2749 button, state,
2750 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002751
Daniel Stone4dbadb12012-05-30 16:31:51 +01002752 if (input->grab && input->grab_button == button &&
2753 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002754 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002755}
2756
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002757static void
Daniel Stone37816df2012-05-16 18:45:18 +01002758pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002759 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002760{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002761 struct input *input = data;
2762 struct widget *widget;
2763
2764 widget = input->focus_widget;
2765 if (input->grab)
2766 widget = input->grab;
2767 if (widget && widget->axis_handler)
2768 (*widget->axis_handler)(widget,
2769 input, time,
2770 axis, value,
2771 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002772}
2773
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002774static const struct wl_pointer_listener pointer_listener = {
2775 pointer_handle_enter,
2776 pointer_handle_leave,
2777 pointer_handle_motion,
2778 pointer_handle_button,
2779 pointer_handle_axis,
2780};
2781
2782static void
2783input_remove_keyboard_focus(struct input *input)
2784{
2785 struct window *window = input->keyboard_focus;
2786 struct itimerspec its;
2787
2788 its.it_interval.tv_sec = 0;
2789 its.it_interval.tv_nsec = 0;
2790 its.it_value.tv_sec = 0;
2791 its.it_value.tv_nsec = 0;
2792 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2793
2794 if (!window)
2795 return;
2796
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002797 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002798 if (window->keyboard_focus_handler)
2799 (*window->keyboard_focus_handler)(window, NULL,
2800 window->user_data);
2801
2802 input->keyboard_focus = NULL;
2803}
2804
2805static void
2806keyboard_repeat_func(struct task *task, uint32_t events)
2807{
2808 struct input *input =
2809 container_of(task, struct input, repeat_task);
2810 struct window *window = input->keyboard_focus;
2811 uint64_t exp;
2812
2813 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2814 /* If we change the timer between the fd becoming
2815 * readable and getting here, there'll be nothing to
2816 * read and we get EAGAIN. */
2817 return;
2818
2819 if (window && window->key_handler) {
2820 (*window->key_handler)(window, input, input->repeat_time,
2821 input->repeat_key, input->repeat_sym,
2822 WL_KEYBOARD_KEY_STATE_PRESSED,
2823 window->user_data);
2824 }
2825}
2826
2827static void
2828keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2829 uint32_t format, int fd, uint32_t size)
2830{
2831 struct input *input = data;
2832 char *map_str;
2833
2834 if (!data) {
2835 close(fd);
2836 return;
2837 }
2838
2839 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2840 close(fd);
2841 return;
2842 }
2843
2844 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2845 if (map_str == MAP_FAILED) {
2846 close(fd);
2847 return;
2848 }
2849
2850 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2851 map_str,
2852 XKB_KEYMAP_FORMAT_TEXT_V1,
2853 0);
2854 munmap(map_str, size);
2855 close(fd);
2856
2857 if (!input->xkb.keymap) {
2858 fprintf(stderr, "failed to compile keymap\n");
2859 return;
2860 }
2861
2862 input->xkb.state = xkb_state_new(input->xkb.keymap);
2863 if (!input->xkb.state) {
2864 fprintf(stderr, "failed to create XKB state\n");
2865 xkb_map_unref(input->xkb.keymap);
2866 input->xkb.keymap = NULL;
2867 return;
2868 }
2869
2870 input->xkb.control_mask =
2871 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2872 input->xkb.alt_mask =
2873 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2874 input->xkb.shift_mask =
2875 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2876}
2877
2878static void
2879keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2880 uint32_t serial, struct wl_surface *surface,
2881 struct wl_array *keys)
2882{
2883 struct input *input = data;
2884 struct window *window;
2885
2886 input->display->serial = serial;
2887 input->keyboard_focus = wl_surface_get_user_data(surface);
2888
2889 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002890 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002891 if (window->keyboard_focus_handler)
2892 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002893 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002894}
2895
2896static void
2897keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2898 uint32_t serial, struct wl_surface *surface)
2899{
2900 struct input *input = data;
2901
2902 input->display->serial = serial;
2903 input_remove_keyboard_focus(input);
2904}
2905
Scott Moreau210d0792012-03-22 10:47:01 -06002906static void
Daniel Stone37816df2012-05-16 18:45:18 +01002907keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002908 uint32_t serial, uint32_t time, uint32_t key,
2909 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002910{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002911 struct input *input = data;
2912 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002913 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002914 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002915 const xkb_keysym_t *syms;
2916 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002917 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002918
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002919 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002920 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002921 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002922 return;
2923
Daniel Stone97f68542012-05-30 16:32:01 +01002924 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002925
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002926 sym = XKB_KEY_NoSymbol;
2927 if (num_syms == 1)
2928 sym = syms[0];
2929
2930 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002931 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002932 window_set_maximized(window,
2933 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002934 } else if (sym == XKB_KEY_F11 &&
2935 window->fullscreen_handler &&
2936 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2937 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002938 } else if (sym == XKB_KEY_F4 &&
2939 input->modifiers == MOD_ALT_MASK &&
2940 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2941 if (window->close_handler)
2942 window->close_handler(window->parent,
2943 window->user_data);
2944 else
2945 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002946 } else if (window->key_handler) {
2947 (*window->key_handler)(window, input, time, key,
2948 sym, state, window->user_data);
2949 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002950
2951 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2952 key == input->repeat_key) {
2953 its.it_interval.tv_sec = 0;
2954 its.it_interval.tv_nsec = 0;
2955 its.it_value.tv_sec = 0;
2956 its.it_value.tv_nsec = 0;
2957 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2958 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2959 input->repeat_sym = sym;
2960 input->repeat_key = key;
2961 input->repeat_time = time;
2962 its.it_interval.tv_sec = 0;
2963 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2964 its.it_value.tv_sec = 0;
2965 its.it_value.tv_nsec = 400 * 1000 * 1000;
2966 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2967 }
2968}
2969
2970static void
Daniel Stone351eb612012-05-31 15:27:47 -04002971keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2972 uint32_t serial, uint32_t mods_depressed,
2973 uint32_t mods_latched, uint32_t mods_locked,
2974 uint32_t group)
2975{
2976 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002977 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002978
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002979 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2980 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002981 mask = xkb_state_serialize_mods(input->xkb.state,
2982 XKB_STATE_DEPRESSED |
2983 XKB_STATE_LATCHED);
2984 input->modifiers = 0;
2985 if (mask & input->xkb.control_mask)
2986 input->modifiers |= MOD_CONTROL_MASK;
2987 if (mask & input->xkb.alt_mask)
2988 input->modifiers |= MOD_ALT_MASK;
2989 if (mask & input->xkb.shift_mask)
2990 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002991}
2992
Daniel Stone37816df2012-05-16 18:45:18 +01002993static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002994 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002995 keyboard_handle_enter,
2996 keyboard_handle_leave,
2997 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002998 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002999};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003000
3001static void
Daniel Stone37816df2012-05-16 18:45:18 +01003002seat_handle_capabilities(void *data, struct wl_seat *seat,
3003 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003004{
Daniel Stone37816df2012-05-16 18:45:18 +01003005 struct input *input = data;
3006
3007 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3008 input->pointer = wl_seat_get_pointer(seat);
3009 wl_pointer_set_user_data(input->pointer, input);
3010 wl_pointer_add_listener(input->pointer, &pointer_listener,
3011 input);
3012 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3013 wl_pointer_destroy(input->pointer);
3014 input->pointer = NULL;
3015 }
3016
3017 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3018 input->keyboard = wl_seat_get_keyboard(seat);
3019 wl_keyboard_set_user_data(input->keyboard, input);
3020 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3021 input);
3022 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3023 wl_keyboard_destroy(input->keyboard);
3024 input->keyboard = NULL;
3025 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003026}
3027
Daniel Stone37816df2012-05-16 18:45:18 +01003028static const struct wl_seat_listener seat_listener = {
3029 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003030};
3031
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003032void
3033input_get_position(struct input *input, int32_t *x, int32_t *y)
3034{
3035 *x = input->sx;
3036 *y = input->sy;
3037}
3038
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003039struct display *
3040input_get_display(struct input *input)
3041{
3042 return input->display;
3043}
3044
Daniel Stone37816df2012-05-16 18:45:18 +01003045struct wl_seat *
3046input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003047{
Daniel Stone37816df2012-05-16 18:45:18 +01003048 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003049}
3050
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003051uint32_t
3052input_get_modifiers(struct input *input)
3053{
3054 return input->modifiers;
3055}
3056
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003057struct widget *
3058input_get_focus_widget(struct input *input)
3059{
3060 return input->focus_widget;
3061}
3062
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003063struct data_offer {
3064 struct wl_data_offer *offer;
3065 struct input *input;
3066 struct wl_array types;
3067 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003068
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003069 struct task io_task;
3070 int fd;
3071 data_func_t func;
3072 int32_t x, y;
3073 void *user_data;
3074};
3075
3076static void
3077data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3078{
3079 struct data_offer *offer = data;
3080 char **p;
3081
3082 p = wl_array_add(&offer->types, sizeof *p);
3083 *p = strdup(type);
3084}
3085
3086static const struct wl_data_offer_listener data_offer_listener = {
3087 data_offer_offer,
3088};
3089
3090static void
3091data_offer_destroy(struct data_offer *offer)
3092{
3093 char **p;
3094
3095 offer->refcount--;
3096 if (offer->refcount == 0) {
3097 wl_data_offer_destroy(offer->offer);
3098 for (p = offer->types.data; *p; p++)
3099 free(*p);
3100 wl_array_release(&offer->types);
3101 free(offer);
3102 }
3103}
3104
3105static void
3106data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003107 struct wl_data_device *data_device,
3108 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003109{
3110 struct data_offer *offer;
3111
3112 offer = malloc(sizeof *offer);
3113
3114 wl_array_init(&offer->types);
3115 offer->refcount = 1;
3116 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003117 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003118 wl_data_offer_add_listener(offer->offer,
3119 &data_offer_listener, offer);
3120}
3121
3122static void
3123data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003124 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003125 wl_fixed_t x_w, wl_fixed_t y_w,
3126 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003127{
3128 struct input *input = data;
3129 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003130 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003131 float x = wl_fixed_to_double(x_w);
3132 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003133 char **p;
3134
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003135 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003136 window = wl_surface_get_user_data(surface);
3137 input->pointer_focus = window;
3138
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003139 if (offer) {
3140 input->drag_offer = wl_data_offer_get_user_data(offer);
3141
3142 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3143 *p = NULL;
3144
3145 types_data = input->drag_offer->types.data;
3146 } else {
3147 input->drag_offer = NULL;
3148 types_data = NULL;
3149 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003150
3151 window = input->pointer_focus;
3152 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003153 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003154 window->user_data);
3155}
3156
3157static void
3158data_device_leave(void *data, struct wl_data_device *data_device)
3159{
3160 struct input *input = data;
3161
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003162 if (input->drag_offer) {
3163 data_offer_destroy(input->drag_offer);
3164 input->drag_offer = NULL;
3165 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003166}
3167
3168static void
3169data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003170 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003171{
3172 struct input *input = data;
3173 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003174 float x = wl_fixed_to_double(x_w);
3175 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003176 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003177
3178 input->sx = x;
3179 input->sy = y;
3180
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003181 if (input->drag_offer)
3182 types_data = input->drag_offer->types.data;
3183 else
3184 types_data = NULL;
3185
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003186 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003187 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003188 window->user_data);
3189}
3190
3191static void
3192data_device_drop(void *data, struct wl_data_device *data_device)
3193{
3194 struct input *input = data;
3195 struct window *window = input->pointer_focus;
3196
3197 if (window->drop_handler)
3198 window->drop_handler(window, input,
3199 input->sx, input->sy, window->user_data);
3200}
3201
3202static void
3203data_device_selection(void *data,
3204 struct wl_data_device *wl_data_device,
3205 struct wl_data_offer *offer)
3206{
3207 struct input *input = data;
3208 char **p;
3209
3210 if (input->selection_offer)
3211 data_offer_destroy(input->selection_offer);
3212
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003213 if (offer) {
3214 input->selection_offer = wl_data_offer_get_user_data(offer);
3215 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3216 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003217 } else {
3218 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003219 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003220}
3221
3222static const struct wl_data_device_listener data_device_listener = {
3223 data_device_data_offer,
3224 data_device_enter,
3225 data_device_leave,
3226 data_device_motion,
3227 data_device_drop,
3228 data_device_selection
3229};
3230
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003231static void
3232input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003233{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003234 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003235 struct wl_cursor *cursor;
3236 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003237
Daniel Stone80972742012-11-07 17:51:39 +11003238 if (!input->pointer)
3239 return;
3240
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003241 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003242 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003243 return;
3244
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003245 if (index >= (int) cursor->image_count) {
3246 fprintf(stderr, "cursor index out of range\n");
3247 return;
3248 }
3249
3250 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003251 buffer = wl_cursor_image_get_buffer(image);
3252 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003253 return;
3254
Kristian Høgsbergae277372012-08-01 09:41:08 -04003255 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003256 input->pointer_surface,
3257 image->hotspot_x, image->hotspot_y);
3258 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3259 wl_surface_damage(input->pointer_surface, 0, 0,
3260 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003261 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003262}
3263
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003264static const struct wl_callback_listener pointer_surface_listener;
3265
3266static void
3267pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3268 uint32_t time)
3269{
3270 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003271 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003272 int i;
3273
3274 if (callback) {
3275 assert(callback == input->cursor_frame_cb);
3276 wl_callback_destroy(callback);
3277 input->cursor_frame_cb = NULL;
3278 }
3279
Daniel Stone80972742012-11-07 17:51:39 +11003280 if (!input->pointer)
3281 return;
3282
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003283 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003284 wl_pointer_set_cursor(input->pointer,
3285 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003286 NULL, 0, 0);
3287 return;
3288 }
3289
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003290 if (input->current_cursor == CURSOR_UNSET)
3291 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003292 cursor = input->display->cursors[input->current_cursor];
3293 if (!cursor)
3294 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003295
3296 /* FIXME We don't have the current time on the first call so we set
3297 * the animation start to the time of the first frame callback. */
3298 if (time == 0)
3299 input->cursor_anim_start = 0;
3300 else if (input->cursor_anim_start == 0)
3301 input->cursor_anim_start = time;
3302
3303 if (time == 0 || input->cursor_anim_start == 0)
3304 i = 0;
3305 else
3306 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3307
Pekka Paalanenbc106382012-10-10 12:49:31 +03003308 if (cursor->image_count > 1) {
3309 input->cursor_frame_cb =
3310 wl_surface_frame(input->pointer_surface);
3311 wl_callback_add_listener(input->cursor_frame_cb,
3312 &pointer_surface_listener, input);
3313 }
3314
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003315 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003316}
3317
3318static const struct wl_callback_listener pointer_surface_listener = {
3319 pointer_surface_frame_callback
3320};
3321
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003322void
3323input_set_pointer_image(struct input *input, int pointer)
3324{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003325 int force = 0;
3326
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003327 if (!input->pointer)
3328 return;
3329
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003330 if (input->pointer_enter_serial > input->cursor_serial)
3331 force = 1;
3332
3333 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003334 return;
3335
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003336 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003337 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003338 if (!input->cursor_frame_cb)
3339 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003340 else if (force) {
3341 /* The current frame callback may be stuck if, for instance,
3342 * the set cursor request was processed by the server after
3343 * this client lost the focus. In this case the cursor surface
3344 * might not be mapped and the frame callback wouldn't ever
3345 * complete. Send a set_cursor and attach to try to map the
3346 * cursor surface again so that the callback will finish */
3347 input_set_pointer_image_index(input, 0);
3348 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003349}
3350
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003351struct wl_data_device *
3352input_get_data_device(struct input *input)
3353{
3354 return input->data_device;
3355}
3356
3357void
3358input_set_selection(struct input *input,
3359 struct wl_data_source *source, uint32_t time)
3360{
3361 wl_data_device_set_selection(input->data_device, source, time);
3362}
3363
3364void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003365input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003366{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003367 wl_data_offer_accept(input->drag_offer->offer,
3368 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003369}
3370
3371static void
3372offer_io_func(struct task *task, uint32_t events)
3373{
3374 struct data_offer *offer =
3375 container_of(task, struct data_offer, io_task);
3376 unsigned int len;
3377 char buffer[4096];
3378
3379 len = read(offer->fd, buffer, sizeof buffer);
3380 offer->func(buffer, len,
3381 offer->x, offer->y, offer->user_data);
3382
3383 if (len == 0) {
3384 close(offer->fd);
3385 data_offer_destroy(offer);
3386 }
3387}
3388
3389static void
3390data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3391 data_func_t func, void *user_data)
3392{
3393 int p[2];
3394
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003395 if (pipe2(p, O_CLOEXEC) == -1)
3396 return;
3397
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003398 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3399 close(p[1]);
3400
3401 offer->io_task.run = offer_io_func;
3402 offer->fd = p[0];
3403 offer->func = func;
3404 offer->refcount++;
3405 offer->user_data = user_data;
3406
3407 display_watch_fd(offer->input->display,
3408 offer->fd, EPOLLIN, &offer->io_task);
3409}
3410
3411void
3412input_receive_drag_data(struct input *input, const char *mime_type,
3413 data_func_t func, void *data)
3414{
3415 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3416 input->drag_offer->x = input->sx;
3417 input->drag_offer->y = input->sy;
3418}
3419
3420int
3421input_receive_selection_data(struct input *input, const char *mime_type,
3422 data_func_t func, void *data)
3423{
3424 char **p;
3425
3426 if (input->selection_offer == NULL)
3427 return -1;
3428
3429 for (p = input->selection_offer->types.data; *p; p++)
3430 if (strcmp(mime_type, *p) == 0)
3431 break;
3432
3433 if (*p == NULL)
3434 return -1;
3435
3436 data_offer_receive_data(input->selection_offer,
3437 mime_type, func, data);
3438 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003439}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003440
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003441int
3442input_receive_selection_data_to_fd(struct input *input,
3443 const char *mime_type, int fd)
3444{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003445 if (input->selection_offer)
3446 wl_data_offer_receive(input->selection_offer->offer,
3447 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003448
3449 return 0;
3450}
3451
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003452void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003453window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003454{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003455 if (!window->shell_surface)
3456 return;
3457
Daniel Stone37816df2012-05-16 18:45:18 +01003458 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003459}
3460
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003461static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003462surface_set_synchronized(struct surface *surface)
3463{
3464 if (!surface->subsurface)
3465 return;
3466
3467 if (surface->synchronized)
3468 return;
3469
3470 wl_subsurface_set_sync(surface->subsurface);
3471 surface->synchronized = 1;
3472}
3473
3474static void
3475surface_set_synchronized_default(struct surface *surface)
3476{
3477 if (!surface->subsurface)
3478 return;
3479
3480 if (surface->synchronized == surface->synchronized_default)
3481 return;
3482
3483 if (surface->synchronized_default)
3484 wl_subsurface_set_sync(surface->subsurface);
3485 else
3486 wl_subsurface_set_desync(surface->subsurface);
3487
3488 surface->synchronized = surface->synchronized_default;
3489}
3490
3491static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003492surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003493{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003494 struct widget *widget = surface->widget;
3495 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003496
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003497 if (surface->input_region) {
3498 wl_region_destroy(surface->input_region);
3499 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003500 }
3501
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003502 if (surface->opaque_region)
3503 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003504
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003505 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003506
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003507 if (widget->resize_handler)
3508 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003509 widget->allocation.width,
3510 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003511 widget->user_data);
3512
Pekka Paalanen35e82632013-04-25 13:57:48 +03003513 if (surface->subsurface &&
3514 (surface->allocation.x != widget->allocation.x ||
3515 surface->allocation.y != widget->allocation.y)) {
3516 wl_subsurface_set_position(surface->subsurface,
3517 widget->allocation.x,
3518 widget->allocation.y);
3519 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003520 if (surface->allocation.width != widget->allocation.width ||
3521 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003522 window_schedule_redraw(widget->window);
3523 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003524 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003525
3526 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003527 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003528 widget->allocation.width,
3529 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003530}
3531
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003532static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003533hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3534{
3535 /*
3536 * This hack should be removed, when EGL respects
3537 * eglSwapInterval(0).
3538 *
3539 * If this window has sub-surfaces, especially a free-running
3540 * EGL-widget, we need to post the parent surface once with
3541 * all the old state to guarantee, that the EGL-widget will
3542 * receive its frame callback soon. Otherwise, a forced call
3543 * to eglSwapBuffers may end up blocking, waiting for a frame
3544 * event that will never come, because we will commit the parent
3545 * surface with all new state only after eglSwapBuffers returns.
3546 *
3547 * This assumes, that:
3548 * 1. When the EGL widget's resize hook is called, it pauses.
3549 * 2. When the EGL widget's redraw hook is called, it forces a
3550 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3551 * In a single threaded application condition 1 is a no-op.
3552 *
3553 * XXX: This should actually be after the surface_resize() calls,
3554 * but cannot, because then it would commit the incomplete state
3555 * accumulated from the widget resize hooks.
3556 */
3557 if (window->subsurface_list.next != &window->main_surface->link ||
3558 window->subsurface_list.prev != &window->main_surface->link)
3559 wl_surface_commit(window->main_surface->surface);
3560}
3561
3562static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003563idle_resize(struct window *window)
3564{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003565 struct surface *surface;
3566
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003567 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003568 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003569
Pekka Paalanen71233882013-04-25 13:57:53 +03003570 DBG("from %dx%d to %dx%d\n",
3571 window->main_surface->server_allocation.width,
3572 window->main_surface->server_allocation.height,
3573 window->pending_allocation.width,
3574 window->pending_allocation.height);
3575
Pekka Paalanene9297f82013-04-25 13:57:51 +03003576 hack_prevent_EGL_sub_surface_deadlock(window);
3577
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003578 widget_set_allocation(window->main_surface->widget,
3579 window->pending_allocation.x,
3580 window->pending_allocation.y,
3581 window->pending_allocation.width,
3582 window->pending_allocation.height);
3583
3584 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003585
3586 /* The main surface is in the list, too. Main surface's
3587 * resize_handler is responsible for calling widget_set_allocation()
3588 * on all sub-surface root widgets, so they will be resized
3589 * properly.
3590 */
3591 wl_list_for_each(surface, &window->subsurface_list, link) {
3592 if (surface == window->main_surface)
3593 continue;
3594
3595 surface_set_synchronized(surface);
3596 surface_resize(surface);
3597 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003598}
3599
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003600void
3601window_schedule_resize(struct window *window, int width, int height)
3602{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003603 window->pending_allocation.x = 0;
3604 window->pending_allocation.y = 0;
3605 window->pending_allocation.width = width;
3606 window->pending_allocation.height = height;
3607
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003608 if (window->min_allocation.width == 0)
3609 window->min_allocation = window->pending_allocation;
3610 if (window->pending_allocation.width < window->min_allocation.width)
3611 window->pending_allocation.width = window->min_allocation.width;
3612 if (window->pending_allocation.height < window->min_allocation.height)
3613 window->pending_allocation.height = window->min_allocation.height;
3614
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003615 window->resize_needed = 1;
3616 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003617}
3618
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003619void
3620widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3621{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003622 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003623}
3624
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003625static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003626handle_ping(void *data, struct wl_shell_surface *shell_surface,
3627 uint32_t serial)
3628{
3629 wl_shell_surface_pong(shell_surface, serial);
3630}
3631
3632static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003633handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003634 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003635{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003636 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003637
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003638 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003639 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003640}
3641
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003642static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003643menu_destroy(struct menu *menu)
3644{
3645 widget_destroy(menu->widget);
3646 window_destroy(menu->window);
3647 free(menu);
3648}
3649
3650static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003651handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3652{
3653 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003654 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003655
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003656 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003657 * device. Or just use wl_callback. And this really needs to
3658 * be a window vfunc that the menu can set. And we need the
3659 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003660
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003661 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003662 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003663}
3664
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003665static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003666 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003667 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003668 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003669};
3670
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003671void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003672window_get_allocation(struct window *window,
3673 struct rectangle *allocation)
3674{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003675 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003676}
3677
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003678static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003679widget_redraw(struct widget *widget)
3680{
3681 struct widget *child;
3682
3683 if (widget->redraw_handler)
3684 widget->redraw_handler(widget, widget->user_data);
3685 wl_list_for_each(child, &widget->child_list, link)
3686 widget_redraw(child);
3687}
3688
3689static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003690frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3691{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003692 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003693
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003694 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003695 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003696 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003697 surface->frame_cb = NULL;
3698
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003699 surface->last_time = time;
3700
Pekka Paalanen71233882013-04-25 13:57:53 +03003701 if (surface->redraw_needed || surface->window->redraw_needed) {
3702 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003703 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003704 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003705}
3706
3707static const struct wl_callback_listener listener = {
3708 frame_callback
3709};
3710
3711static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003712surface_redraw(struct surface *surface)
3713{
Pekka Paalanen71233882013-04-25 13:57:53 +03003714 DBG_OBJ(surface->surface, "begin\n");
3715
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003716 if (!surface->window->redraw_needed && !surface->redraw_needed)
3717 return;
3718
3719 /* Whole-window redraw forces a redraw even if the previous has
3720 * not yet hit the screen.
3721 */
3722 if (surface->frame_cb) {
3723 if (!surface->window->redraw_needed)
3724 return;
3725
Pekka Paalanen71233882013-04-25 13:57:53 +03003726 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003727 wl_callback_destroy(surface->frame_cb);
3728 }
3729
3730 surface->frame_cb = wl_surface_frame(surface->surface);
3731 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003732 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003733
3734 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003735 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003736 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003737 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003738}
3739
3740static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003741idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003742{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003743 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003744 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003745
Pekka Paalanen71233882013-04-25 13:57:53 +03003746 DBG(" --------- \n");
3747
Pekka Paalaneneebff542013-04-25 13:57:52 +03003748 wl_list_init(&window->redraw_task.link);
3749 window->redraw_task_scheduled = 0;
3750
3751 if (window->resize_needed) {
3752 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003753 if (window->main_surface->frame_cb) {
3754 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003755 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003756 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003757
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003758 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003759 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003760
Pekka Paalanen35e82632013-04-25 13:57:48 +03003761 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003762 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003763
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003764 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003765 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003766
3767 wl_list_for_each(surface, &window->subsurface_list, link)
3768 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003769}
3770
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003771static void
3772window_schedule_redraw_task(struct window *window)
3773{
3774 if (window->configure_requests)
3775 return;
3776 if (!window->redraw_task_scheduled) {
3777 window->redraw_task.run = idle_redraw;
3778 display_defer(window->display, &window->redraw_task);
3779 window->redraw_task_scheduled = 1;
3780 }
3781}
3782
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003783void
3784window_schedule_redraw(struct window *window)
3785{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003786 struct surface *surface;
3787
Pekka Paalanen71233882013-04-25 13:57:53 +03003788 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3789
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003790 wl_list_for_each(surface, &window->subsurface_list, link)
3791 surface->redraw_needed = 1;
3792
3793 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003794}
3795
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003796int
3797window_is_fullscreen(struct window *window)
3798{
3799 return window->type == TYPE_FULLSCREEN;
3800}
3801
MoD063a8822013-03-02 23:43:57 +00003802static void
3803configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3804{
3805 struct window *window = data;
3806
3807 wl_callback_destroy(callback);
3808 window->configure_requests--;
3809
3810 if (!window->configure_requests)
3811 window_schedule_redraw(window);
3812}
3813
3814static struct wl_callback_listener configure_request_listener = {
3815 configure_request_completed,
3816};
3817
3818static void
3819window_defer_redraw_until_configure(struct window* window)
3820{
3821 struct wl_callback *callback;
3822
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003823 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003824 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003825 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003826 }
3827
3828 callback = wl_display_sync(window->display->display);
3829 wl_callback_add_listener(callback, &configure_request_listener, window);
3830 window->configure_requests++;
3831}
3832
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003833void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003834window_set_fullscreen(struct window *window, int fullscreen)
3835{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003836 if (!window->display->shell)
3837 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003838
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003839 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003840 return;
3841
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003842 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003843 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003844 if (window->type == TYPE_TOPLEVEL) {
3845 window->saved_allocation = window->main_surface->allocation;
3846 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003847 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003848 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003849 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003850 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003851 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003852 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003853 if (window->saved_type == TYPE_MAXIMIZED) {
3854 window_set_maximized(window, 1);
3855 } else {
3856 window->type = TYPE_TOPLEVEL;
3857 wl_shell_surface_set_toplevel(window->shell_surface);
3858 window_schedule_resize(window,
3859 window->saved_allocation.width,
3860 window->saved_allocation.height);
3861 }
3862
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003863 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003864}
3865
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003866void
3867window_set_fullscreen_method(struct window *window,
3868 enum wl_shell_surface_fullscreen_method method)
3869{
3870 window->fullscreen_method = method;
3871}
3872
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003873int
3874window_is_maximized(struct window *window)
3875{
3876 return window->type == TYPE_MAXIMIZED;
3877}
3878
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003879void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003880window_set_maximized(struct window *window, int maximized)
3881{
3882 if (!window->display->shell)
3883 return;
3884
3885 if ((window->type == TYPE_MAXIMIZED) == maximized)
3886 return;
3887
3888 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003889 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003890 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3891 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003892 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003893 } else if (window->type == TYPE_FULLSCREEN) {
3894 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3895 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003896 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003897 } else {
3898 wl_shell_surface_set_toplevel(window->shell_surface);
3899 window->type = TYPE_TOPLEVEL;
3900 window_schedule_resize(window,
3901 window->saved_allocation.width,
3902 window->saved_allocation.height);
3903 }
3904}
3905
3906void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003907window_set_user_data(struct window *window, void *data)
3908{
3909 window->user_data = data;
3910}
3911
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003912void *
3913window_get_user_data(struct window *window)
3914{
3915 return window->user_data;
3916}
3917
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003918void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003919window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003920 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003921{
3922 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003923}
3924
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003925void
3926window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003927 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003928{
3929 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003930}
3931
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003932void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003933window_set_data_handler(struct window *window, window_data_handler_t handler)
3934{
3935 window->data_handler = handler;
3936}
3937
3938void
3939window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3940{
3941 window->drop_handler = handler;
3942}
3943
3944void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003945window_set_close_handler(struct window *window,
3946 window_close_handler_t handler)
3947{
3948 window->close_handler = handler;
3949}
3950
3951void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003952window_set_fullscreen_handler(struct window *window,
3953 window_fullscreen_handler_t handler)
3954{
3955 window->fullscreen_handler = handler;
3956}
3957
3958void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003959window_set_output_handler(struct window *window,
3960 window_output_handler_t handler)
3961{
3962 window->output_handler = handler;
3963}
3964
3965void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003966window_set_title(struct window *window, const char *title)
3967{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003968 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003969 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003970 if (window->shell_surface)
3971 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003972}
3973
3974const char *
3975window_get_title(struct window *window)
3976{
3977 return window->title;
3978}
3979
3980void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003981window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3982{
3983 struct text_cursor_position *text_cursor_position =
3984 window->display->text_cursor_position;
3985
Scott Moreau9295ce02012-06-01 12:46:10 -06003986 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003987 return;
3988
3989 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003990 window->main_surface->surface,
3991 wl_fixed_from_int(x),
3992 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003993}
3994
3995void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003996window_damage(struct window *window, int32_t x, int32_t y,
3997 int32_t width, int32_t height)
3998{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003999 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004000}
4001
Casey Dahlin9074db52012-04-19 22:50:09 -04004002static void
4003surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004004 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004005{
Rob Bradford7507b572012-05-15 17:55:34 +01004006 struct window *window = data;
4007 struct output *output;
4008 struct output *output_found = NULL;
4009 struct window_output *window_output;
4010
4011 wl_list_for_each(output, &window->display->output_list, link) {
4012 if (output->output == wl_output) {
4013 output_found = output;
4014 break;
4015 }
4016 }
4017
4018 if (!output_found)
4019 return;
4020
4021 window_output = malloc (sizeof *window_output);
4022 window_output->output = output_found;
4023
4024 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004025
4026 if (window->output_handler)
4027 window->output_handler(window, output_found, 1,
4028 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004029}
4030
4031static void
4032surface_leave(void *data,
4033 struct wl_surface *wl_surface, struct wl_output *output)
4034{
Rob Bradford7507b572012-05-15 17:55:34 +01004035 struct window *window = data;
4036 struct window_output *window_output;
4037 struct window_output *window_output_found = NULL;
4038
4039 wl_list_for_each(window_output, &window->window_output_list, link) {
4040 if (window_output->output->output == output) {
4041 window_output_found = window_output;
4042 break;
4043 }
4044 }
4045
4046 if (window_output_found) {
4047 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004048
4049 if (window->output_handler)
4050 window->output_handler(window, window_output->output,
4051 0, window->user_data);
4052
Rob Bradford7507b572012-05-15 17:55:34 +01004053 free(window_output_found);
4054 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004055}
4056
4057static const struct wl_surface_listener surface_listener = {
4058 surface_enter,
4059 surface_leave
4060};
4061
Pekka Paalanen4e373742013-02-13 16:17:13 +02004062static struct surface *
4063surface_create(struct window *window)
4064{
4065 struct display *display = window->display;
4066 struct surface *surface;
4067
4068 surface = calloc(1, sizeof *surface);
4069 if (!surface)
4070 return NULL;
4071
4072 surface->window = window;
4073 surface->surface = wl_compositor_create_surface(display->compositor);
4074 wl_surface_add_listener(surface->surface, &surface_listener, window);
4075
Pekka Paalanen35e82632013-04-25 13:57:48 +03004076 wl_list_insert(&window->subsurface_list, &surface->link);
4077
Pekka Paalanen4e373742013-02-13 16:17:13 +02004078 return surface;
4079}
4080
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004081static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004082window_create_internal(struct display *display,
4083 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004084{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004085 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004086 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004087
4088 window = malloc(sizeof *window);
4089 if (window == NULL)
4090 return NULL;
4091
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004092 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004093 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004094 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004095 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004096
4097 surface = surface_create(window);
4098 window->main_surface = surface;
4099
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004100 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004101 window->shell_surface =
4102 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004103 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004104 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004105
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004106 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004107 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004108 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004109
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004110 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004111#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004112 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004113#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004114 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004115#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004116 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004117 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004118
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004119 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004120 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004121 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004122
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004123 if (window->shell_surface) {
4124 wl_shell_surface_set_user_data(window->shell_surface, window);
4125 wl_shell_surface_add_listener(window->shell_surface,
4126 &shell_surface_listener, window);
4127 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004128
Rob Bradford7507b572012-05-15 17:55:34 +01004129 wl_list_init (&window->window_output_list);
4130
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004131 return window;
4132}
4133
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004134struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004135window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004136{
4137 struct window *window;
4138
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004139 window = window_create_internal(display, NULL, TYPE_NONE);
4140 if (!window)
4141 return NULL;
4142
4143 return window;
4144}
4145
4146struct window *
4147window_create_custom(struct display *display)
4148{
4149 struct window *window;
4150
4151 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004152 if (!window)
4153 return NULL;
4154
4155 return window;
4156}
4157
4158struct window *
4159window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004160 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004161{
4162 struct window *window;
4163
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004164 window = window_create_internal(parent->display,
4165 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004166 if (!window)
4167 return NULL;
4168
4169 window->x = x;
4170 window->y = y;
4171
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004172 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004173 wl_shell_surface_set_transient(
4174 window->shell_surface,
4175 window->parent->main_surface->surface,
4176 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004177
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004178 return window;
4179}
4180
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004181static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004182menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004183{
4184 int next;
4185
4186 next = (sy - 8) / 20;
4187 if (menu->current != next) {
4188 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004189 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004190 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004191}
4192
4193static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004194menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004195 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004196 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004197{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004198 struct menu *menu = data;
4199
4200 if (widget == menu->widget)
4201 menu_set_item(data, y);
4202
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004203 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004204}
4205
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004206static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004207menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004208 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004209{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004210 struct menu *menu = data;
4211
4212 if (widget == menu->widget)
4213 menu_set_item(data, y);
4214
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004215 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004216}
4217
4218static void
4219menu_leave_handler(struct widget *widget, struct input *input, void *data)
4220{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004221 struct menu *menu = data;
4222
4223 if (widget == menu->widget)
4224 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004225}
4226
4227static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004228menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004229 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004230 uint32_t button, enum wl_pointer_button_state state,
4231 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004232
4233{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004234 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004235
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004236 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4237 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004238 /* Either relase after press-drag-release or
4239 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004240 menu->func(menu->window->parent,
4241 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004242 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004243 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004244 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004245 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004246 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004247}
4248
4249static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004250menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004251{
4252 cairo_t *cr;
4253 const int32_t r = 3, margin = 3;
4254 struct menu *menu = data;
4255 int32_t width, height, i;
4256
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004257 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004258 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4259 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4260 cairo_paint(cr);
4261
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004262 width = widget->allocation.width;
4263 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004264 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004265
4266 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004267 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4268 cairo_fill(cr);
4269
4270 for (i = 0; i < menu->count; i++) {
4271 if (i == menu->current) {
4272 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4273 cairo_rectangle(cr, margin, i * 20 + margin,
4274 width - 2 * margin, 20);
4275 cairo_fill(cr);
4276 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4277 cairo_move_to(cr, 10, i * 20 + 16);
4278 cairo_show_text(cr, menu->entries[i]);
4279 } else {
4280 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4281 cairo_move_to(cr, 10, i * 20 + 16);
4282 cairo_show_text(cr, menu->entries[i]);
4283 }
4284 }
4285
4286 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004287}
4288
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004289void
4290window_show_menu(struct display *display,
4291 struct input *input, uint32_t time, struct window *parent,
4292 int32_t x, int32_t y,
4293 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004294{
4295 struct window *window;
4296 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004297 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004298
4299 menu = malloc(sizeof *menu);
4300 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004301 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004302
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004303 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004304 if (!window) {
4305 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004306 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004307 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004308
4309 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004310 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004311 menu->entries = entries;
4312 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004313 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004314 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004315 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004316 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004317 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004318 window->type = TYPE_MENU;
4319 window->x = x;
4320 window->y = y;
4321
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004322 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004323 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004324 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004325 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004326 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004327
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004328 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004329 widget_set_enter_handler(menu->widget, menu_enter_handler);
4330 widget_set_leave_handler(menu->widget, menu_leave_handler);
4331 widget_set_motion_handler(menu->widget, menu_motion_handler);
4332 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004333
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004334 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004335 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004336}
4337
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004338void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004339window_set_buffer_type(struct window *window, enum window_buffer_type type)
4340{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004341 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004342}
4343
Pekka Paalanen35e82632013-04-25 13:57:48 +03004344struct widget *
4345window_add_subsurface(struct window *window, void *data,
4346 enum subsurface_mode default_mode)
4347{
4348 struct widget *widget;
4349 struct surface *surface;
4350 struct wl_surface *parent;
4351 struct wl_subcompositor *subcompo = window->display->subcompositor;
4352
4353 if (!subcompo)
4354 return NULL;
4355
4356 surface = surface_create(window);
4357 widget = widget_create(window, surface, data);
4358 wl_list_init(&widget->link);
4359 surface->widget = widget;
4360
4361 parent = window->main_surface->surface;
4362 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4363 surface->surface,
4364 parent);
4365 surface->synchronized = 1;
4366
4367 switch (default_mode) {
4368 case SUBSURFACE_SYNCHRONIZED:
4369 surface->synchronized_default = 1;
4370 break;
4371 case SUBSURFACE_DESYNCHRONIZED:
4372 surface->synchronized_default = 0;
4373 break;
4374 default:
4375 assert(!"bad enum subsurface_mode");
4376 }
4377
4378 return widget;
4379}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004380
4381static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004382display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004383 struct wl_output *wl_output,
4384 int x, int y,
4385 int physical_width,
4386 int physical_height,
4387 int subpixel,
4388 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004389 const char *model,
4390 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004391{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004392 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004393
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004394 output->allocation.x = x;
4395 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004396 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004397}
4398
4399static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004400display_handle_done(void *data,
4401 struct wl_output *wl_output)
4402{
4403}
4404
4405static void
4406display_handle_scale(void *data,
4407 struct wl_output *wl_output,
4408 uint32_t scale)
4409{
4410 struct output *output = data;
4411
4412 output->scale = scale;
4413}
4414
4415static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004416display_handle_mode(void *data,
4417 struct wl_output *wl_output,
4418 uint32_t flags,
4419 int width,
4420 int height,
4421 int refresh)
4422{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004423 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004424 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004425
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004426 if (flags & WL_OUTPUT_MODE_CURRENT) {
4427 output->allocation.width = width;
4428 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004429 if (display->output_configure_handler)
4430 (*display->output_configure_handler)(
4431 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004432 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004433}
4434
4435static const struct wl_output_listener output_listener = {
4436 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004437 display_handle_mode,
4438 display_handle_done,
4439 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004440};
4441
4442static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004443display_add_output(struct display *d, uint32_t id)
4444{
4445 struct output *output;
4446
4447 output = malloc(sizeof *output);
4448 if (output == NULL)
4449 return;
4450
4451 memset(output, 0, sizeof *output);
4452 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004453 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004454 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004455 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004456 wl_list_insert(d->output_list.prev, &output->link);
4457
4458 wl_output_add_listener(output->output, &output_listener, output);
4459}
4460
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004461static void
4462output_destroy(struct output *output)
4463{
4464 if (output->destroy_handler)
4465 (*output->destroy_handler)(output, output->user_data);
4466
4467 wl_output_destroy(output->output);
4468 wl_list_remove(&output->link);
4469 free(output);
4470}
4471
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004472void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004473display_set_global_handler(struct display *display,
4474 display_global_handler_t handler)
4475{
4476 struct global *global;
4477
4478 display->global_handler = handler;
4479 if (!handler)
4480 return;
4481
4482 wl_list_for_each(global, &display->global_list, link)
4483 display->global_handler(display,
4484 global->name, global->interface,
4485 global->version, display->user_data);
4486}
4487
4488void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004489display_set_output_configure_handler(struct display *display,
4490 display_output_handler_t handler)
4491{
4492 struct output *output;
4493
4494 display->output_configure_handler = handler;
4495 if (!handler)
4496 return;
4497
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004498 wl_list_for_each(output, &display->output_list, link) {
4499 if (output->allocation.width == 0 &&
4500 output->allocation.height == 0)
4501 continue;
4502
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004503 (*display->output_configure_handler)(output,
4504 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004505 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004506}
4507
4508void
4509output_set_user_data(struct output *output, void *data)
4510{
4511 output->user_data = data;
4512}
4513
4514void *
4515output_get_user_data(struct output *output)
4516{
4517 return output->user_data;
4518}
4519
4520void
4521output_set_destroy_handler(struct output *output,
4522 display_output_handler_t handler)
4523{
4524 output->destroy_handler = handler;
4525 /* FIXME: implement this, once we have way to remove outputs */
4526}
4527
4528void
Scott Moreau4e072362012-09-29 02:03:11 -06004529output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004530{
Scott Moreau4e072362012-09-29 02:03:11 -06004531 struct rectangle allocation = output->allocation;
4532
4533 switch (output->transform) {
4534 case WL_OUTPUT_TRANSFORM_90:
4535 case WL_OUTPUT_TRANSFORM_270:
4536 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4537 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4538 /* Swap width and height */
4539 allocation.width = output->allocation.height;
4540 allocation.height = output->allocation.width;
4541 break;
4542 }
4543
4544 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004545}
4546
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004547struct wl_output *
4548output_get_wl_output(struct output *output)
4549{
4550 return output->output;
4551}
4552
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004553enum wl_output_transform
4554output_get_transform(struct output *output)
4555{
4556 return output->transform;
4557}
4558
Alexander Larssonafd319a2013-05-22 14:41:27 +02004559uint32_t
4560output_get_scale(struct output *output)
4561{
4562 return output->scale;
4563}
4564
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004565static void
Daniel Stone97f68542012-05-30 16:32:01 +01004566fini_xkb(struct input *input)
4567{
4568 xkb_state_unref(input->xkb.state);
4569 xkb_map_unref(input->xkb.keymap);
4570}
4571
4572static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004573display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004574{
4575 struct input *input;
4576
4577 input = malloc(sizeof *input);
4578 if (input == NULL)
4579 return;
4580
4581 memset(input, 0, sizeof *input);
4582 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004583 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004584 input->pointer_focus = NULL;
4585 input->keyboard_focus = NULL;
4586 wl_list_insert(d->input_list.prev, &input->link);
4587
Daniel Stone37816df2012-05-16 18:45:18 +01004588 wl_seat_add_listener(input->seat, &seat_listener, input);
4589 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004590
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004591 input->data_device =
4592 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004593 input->seat);
4594 wl_data_device_add_listener(input->data_device, &data_device_listener,
4595 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004596
4597 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004598
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004599 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4600 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004601 input->repeat_task.run = keyboard_repeat_func;
4602 display_watch_fd(d, input->repeat_timer_fd,
4603 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004604}
4605
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004606static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004607input_destroy(struct input *input)
4608{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004609 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004610 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004611
4612 if (input->drag_offer)
4613 data_offer_destroy(input->drag_offer);
4614
4615 if (input->selection_offer)
4616 data_offer_destroy(input->selection_offer);
4617
4618 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004619 fini_xkb(input);
4620
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004621 wl_surface_destroy(input->pointer_surface);
4622
Pekka Paalanene1207c72011-12-16 12:02:09 +02004623 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004624 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004625 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004626 free(input);
4627}
4628
4629static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004630init_workspace_manager(struct display *d, uint32_t id)
4631{
4632 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004633 wl_registry_bind(d->registry, id,
4634 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004635 if (d->workspace_manager != NULL)
4636 workspace_manager_add_listener(d->workspace_manager,
4637 &workspace_manager_listener,
4638 d);
4639}
4640
4641static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004642registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4643 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004644{
4645 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004646 struct global *global;
4647
4648 global = malloc(sizeof *global);
4649 global->name = id;
4650 global->interface = strdup(interface);
4651 global->version = version;
4652 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004653
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004654 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004655 d->compositor = wl_registry_bind(registry, id,
4656 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004657 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004658 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004659 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004660 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004661 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004662 d->shell = wl_registry_bind(registry,
4663 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004664 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004665 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004666 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4667 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004668 wl_registry_bind(registry, id,
4669 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004670 } else if (strcmp(interface, "text_cursor_position") == 0) {
4671 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004672 wl_registry_bind(registry, id,
4673 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004674 } else if (strcmp(interface, "workspace_manager") == 0) {
4675 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004676 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4677 d->subcompositor =
4678 wl_registry_bind(registry, id,
4679 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004680 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004681
4682 if (d->global_handler)
4683 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004684}
4685
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004686static void
4687registry_handle_global_remove(void *data, struct wl_registry *registry,
4688 uint32_t name)
4689{
4690 struct display *d = data;
4691 struct global *global;
4692 struct global *tmp;
4693
4694 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4695 if (global->name != name)
4696 continue;
4697
4698 /* XXX: Should destroy bound globals, and call
4699 * the counterpart of display::global_handler
4700 */
4701 wl_list_remove(&global->link);
4702 free(global->interface);
4703 free(global);
4704 }
4705}
4706
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004707void *
4708display_bind(struct display *display, uint32_t name,
4709 const struct wl_interface *interface, uint32_t version)
4710{
4711 return wl_registry_bind(display->registry, name, interface, version);
4712}
4713
4714static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004715 registry_handle_global,
4716 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004717};
4718
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004719#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004720static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004721init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004722{
4723 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004724 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004725
Rob Clark6396ed32012-03-11 19:48:41 -05004726#ifdef USE_CAIRO_GLESV2
4727# define GL_BIT EGL_OPENGL_ES2_BIT
4728#else
4729# define GL_BIT EGL_OPENGL_BIT
4730#endif
4731
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004732 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004733 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004734 EGL_RED_SIZE, 1,
4735 EGL_GREEN_SIZE, 1,
4736 EGL_BLUE_SIZE, 1,
4737 EGL_ALPHA_SIZE, 1,
4738 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004739 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004740 EGL_NONE
4741 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004742
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004743#ifdef USE_CAIRO_GLESV2
4744 static const EGLint context_attribs[] = {
4745 EGL_CONTEXT_CLIENT_VERSION, 2,
4746 EGL_NONE
4747 };
4748 EGLint api = EGL_OPENGL_ES_API;
4749#else
4750 EGLint *context_attribs = NULL;
4751 EGLint api = EGL_OPENGL_API;
4752#endif
4753
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004754 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004755 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004756 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004757 return -1;
4758 }
4759
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004760 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004761 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004762 return -1;
4763 }
4764
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004765 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4766 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004767 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004768 return -1;
4769 }
4770
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004771 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004772 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004773 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004774 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004775 return -1;
4776 }
4777
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004778 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004779 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004780 return -1;
4781 }
4782
Benjamin Franzke0c991632011-09-27 21:57:31 +02004783 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4784 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004785 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004786 return -1;
4787 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004788
4789 return 0;
4790}
4791
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004792static void
4793fini_egl(struct display *display)
4794{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004795 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004796
4797 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4798 EGL_NO_CONTEXT);
4799
4800 eglTerminate(display->dpy);
4801 eglReleaseThread();
4802}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004803#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004804
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004805static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004806init_dummy_surface(struct display *display)
4807{
4808 int len;
4809 void *data;
4810
4811 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4812 data = malloc(len);
4813 display->dummy_surface =
4814 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4815 1, 1, len);
4816 display->dummy_surface_data = data;
4817}
4818
4819static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004820handle_display_data(struct task *task, uint32_t events)
4821{
4822 struct display *display =
4823 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004824 struct epoll_event ep;
4825 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004826
4827 display->display_fd_events = events;
4828
4829 if (events & EPOLLERR || events & EPOLLHUP) {
4830 display_exit(display);
4831 return;
4832 }
4833
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004834 if (events & EPOLLIN) {
4835 ret = wl_display_dispatch(display->display);
4836 if (ret == -1) {
4837 display_exit(display);
4838 return;
4839 }
4840 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004841
4842 if (events & EPOLLOUT) {
4843 ret = wl_display_flush(display->display);
4844 if (ret == 0) {
4845 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4846 ep.data.ptr = &display->display_task;
4847 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4848 display->display_fd, &ep);
4849 } else if (ret == -1 && errno != EAGAIN) {
4850 display_exit(display);
4851 return;
4852 }
4853 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004854}
4855
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004856static void
4857log_handler(const char *format, va_list args)
4858{
4859 vfprintf(stderr, format, args);
4860}
4861
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004862struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004863display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004864{
4865 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004866
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004867 wl_log_set_handler_client(log_handler);
4868
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004869 d = malloc(sizeof *d);
4870 if (d == NULL)
4871 return NULL;
4872
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004873 memset(d, 0, sizeof *d);
4874
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004875 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004876 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004877 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004878 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004879 return NULL;
4880 }
4881
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004882 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004883 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004884 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004885 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4886 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004887
4888 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004889 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004890 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004891 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004892
Daniel Stone97f68542012-05-30 16:32:01 +01004893 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004894 if (d->xkb_context == NULL) {
4895 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004896 return NULL;
4897 }
4898
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004899 d->workspace = 0;
4900 d->workspace_count = 1;
4901
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004902 d->registry = wl_display_get_registry(d->display);
4903 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004904
4905 if (wl_display_dispatch(d->display) < 0) {
4906 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4907 return NULL;
4908 }
4909
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004910#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004911 if (init_egl(d) < 0)
4912 fprintf(stderr, "EGL does not seem to work, "
4913 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004914#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004915
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004916 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004917
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004918 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004919
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004920 wl_list_init(&d->window_list);
4921
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004922 init_dummy_surface(d);
4923
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004924 return d;
4925}
4926
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004927static void
4928display_destroy_outputs(struct display *display)
4929{
4930 struct output *tmp;
4931 struct output *output;
4932
4933 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4934 output_destroy(output);
4935}
4936
Pekka Paalanene1207c72011-12-16 12:02:09 +02004937static void
4938display_destroy_inputs(struct display *display)
4939{
4940 struct input *tmp;
4941 struct input *input;
4942
4943 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4944 input_destroy(input);
4945}
4946
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004947void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004948display_destroy(struct display *display)
4949{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004950 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004951 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4952 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004953
4954 if (!wl_list_empty(&display->deferred_list))
4955 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4956
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004957 cairo_surface_destroy(display->dummy_surface);
4958 free(display->dummy_surface_data);
4959
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004960 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004961 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004962
Daniel Stone97f68542012-05-30 16:32:01 +01004963 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004964
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004965 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004966 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004967
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004968#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004969 if (display->argb_device)
4970 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004971#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004972
Pekka Paalanen35e82632013-04-25 13:57:48 +03004973 if (display->subcompositor)
4974 wl_subcompositor_destroy(display->subcompositor);
4975
Pekka Paalanenc2052982011-12-16 11:41:32 +02004976 if (display->shell)
4977 wl_shell_destroy(display->shell);
4978
4979 if (display->shm)
4980 wl_shm_destroy(display->shm);
4981
4982 if (display->data_device_manager)
4983 wl_data_device_manager_destroy(display->data_device_manager);
4984
4985 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004986 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004987
4988 close(display->epoll_fd);
4989
U. Artie Eoff44874d92012-10-02 21:12:35 -07004990 if (!(display->display_fd_events & EPOLLERR) &&
4991 !(display->display_fd_events & EPOLLHUP))
4992 wl_display_flush(display->display);
4993
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004994 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004995 free(display);
4996}
4997
4998void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004999display_set_user_data(struct display *display, void *data)
5000{
5001 display->user_data = data;
5002}
5003
5004void *
5005display_get_user_data(struct display *display)
5006{
5007 return display->user_data;
5008}
5009
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005010struct wl_display *
5011display_get_display(struct display *display)
5012{
5013 return display->display;
5014}
5015
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005016struct output *
5017display_get_output(struct display *display)
5018{
5019 return container_of(display->output_list.next, struct output, link);
5020}
5021
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005022struct wl_compositor *
5023display_get_compositor(struct display *display)
5024{
5025 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005026}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005027
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005028uint32_t
5029display_get_serial(struct display *display)
5030{
5031 return display->serial;
5032}
5033
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005034EGLDisplay
5035display_get_egl_display(struct display *d)
5036{
5037 return d->dpy;
5038}
5039
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005040struct wl_data_source *
5041display_create_data_source(struct display *display)
5042{
5043 return wl_data_device_manager_create_data_source(display->data_device_manager);
5044}
5045
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005046EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005047display_get_argb_egl_config(struct display *d)
5048{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005049 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005050}
5051
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005052struct wl_shell *
5053display_get_shell(struct display *display)
5054{
5055 return display->shell;
5056}
5057
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005058int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005059display_acquire_window_surface(struct display *display,
5060 struct window *window,
5061 EGLContext ctx)
5062{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005063 struct surface *surface = window->main_surface;
5064
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005065 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005066 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005067
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005068 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005069 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005070}
5071
5072void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005073display_release_window_surface(struct display *display,
5074 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005075{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005076 struct surface *surface = window->main_surface;
5077
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005078 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005079 return;
5080
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005081 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005082}
5083
5084void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005085display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005086{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005087 wl_list_insert(&display->deferred_list, &task->link);
5088}
5089
5090void
5091display_watch_fd(struct display *display,
5092 int fd, uint32_t events, struct task *task)
5093{
5094 struct epoll_event ep;
5095
5096 ep.events = events;
5097 ep.data.ptr = task;
5098 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5099}
5100
5101void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005102display_unwatch_fd(struct display *display, int fd)
5103{
5104 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5105}
5106
5107void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005108display_run(struct display *display)
5109{
5110 struct task *task;
5111 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005112 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005113
Pekka Paalanen826d7952011-12-15 10:14:07 +02005114 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005115 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005116 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005117 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005118 struct task, link);
5119 wl_list_remove(&task->link);
5120 task->run(task, 0);
5121 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005122
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005123 wl_display_dispatch_pending(display->display);
5124
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005125 if (!display->running)
5126 break;
5127
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005128 ret = wl_display_flush(display->display);
5129 if (ret < 0 && errno == EAGAIN) {
5130 ep[0].events =
5131 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5132 ep[0].data.ptr = &display->display_task;
5133
5134 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5135 display->display_fd, &ep[0]);
5136 } else if (ret < 0) {
5137 break;
5138 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005139
5140 count = epoll_wait(display->epoll_fd,
5141 ep, ARRAY_LENGTH(ep), -1);
5142 for (i = 0; i < count; i++) {
5143 task = ep[i].data.ptr;
5144 task->run(task, ep[i].events);
5145 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005146 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005147}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005148
5149void
5150display_exit(struct display *display)
5151{
5152 display->running = 0;
5153}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005154
5155void
5156keysym_modifiers_add(struct wl_array *modifiers_map,
5157 const char *name)
5158{
5159 size_t len = strlen(name) + 1;
5160 char *p;
5161
5162 p = wl_array_add(modifiers_map, len);
5163
5164 if (p == NULL)
5165 return;
5166
5167 strncpy(p, name, len);
5168}
5169
5170static xkb_mod_index_t
5171keysym_modifiers_get_index(struct wl_array *modifiers_map,
5172 const char *name)
5173{
5174 xkb_mod_index_t index = 0;
5175 char *p = modifiers_map->data;
5176
5177 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5178 if (strcmp(p, name) == 0)
5179 return index;
5180
5181 index++;
5182 p += strlen(p) + 1;
5183 }
5184
5185 return XKB_MOD_INVALID;
5186}
5187
5188xkb_mod_mask_t
5189keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5190 const char *name)
5191{
5192 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5193
5194 if (index == XKB_MOD_INVALID)
5195 return XKB_MOD_INVALID;
5196
5197 return 1 << index;
5198}