blob: edac77b57852f7382c14fb890eb50bf3b6aa844d [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;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200325
326 display_output_handler_t destroy_handler;
327 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500328};
329
Martin Minarik1998b152012-05-10 02:04:35 +0200330enum frame_button_action {
331 FRAME_BUTTON_NULL = 0,
332 FRAME_BUTTON_ICON = 1,
333 FRAME_BUTTON_CLOSE = 2,
334 FRAME_BUTTON_MINIMIZE = 3,
335 FRAME_BUTTON_MAXIMIZE = 4,
336};
337
338enum frame_button_pointer {
339 FRAME_BUTTON_DEFAULT = 0,
340 FRAME_BUTTON_OVER = 1,
341 FRAME_BUTTON_ACTIVE = 2,
342};
343
344enum frame_button_align {
345 FRAME_BUTTON_RIGHT = 0,
346 FRAME_BUTTON_LEFT = 1,
347};
348
349enum frame_button_decoration {
350 FRAME_BUTTON_NONE = 0,
351 FRAME_BUTTON_FANCY = 1,
352};
353
354struct frame_button {
355 struct widget *widget;
356 struct frame *frame;
357 cairo_surface_t *icon;
358 enum frame_button_action type;
359 enum frame_button_pointer state;
360 struct wl_list link; /* buttons_list */
361 enum frame_button_align align;
362 enum frame_button_decoration decoration;
363};
364
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500365struct frame {
366 struct widget *widget;
367 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200368 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500369};
370
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500371struct menu {
372 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500373 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500374 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500375 const char **entries;
376 uint32_t time;
377 int current;
378 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400379 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500380 menu_func_t func;
381};
382
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300383struct tooltip {
384 struct widget *parent;
385 struct window *window;
386 struct widget *widget;
387 char *entry;
388 struct task tooltip_task;
389 int tooltip_fd;
390 float x, y;
391};
392
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700393struct shm_pool {
394 struct wl_shm_pool *pool;
395 size_t size;
396 size_t used;
397 void *data;
398};
399
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400400enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300401 CURSOR_DEFAULT = 100,
402 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400403};
404
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500405enum window_location {
406 WINDOW_INTERIOR = 0,
407 WINDOW_RESIZING_TOP = 1,
408 WINDOW_RESIZING_BOTTOM = 2,
409 WINDOW_RESIZING_LEFT = 4,
410 WINDOW_RESIZING_TOP_LEFT = 5,
411 WINDOW_RESIZING_BOTTOM_LEFT = 6,
412 WINDOW_RESIZING_RIGHT = 8,
413 WINDOW_RESIZING_TOP_RIGHT = 9,
414 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
415 WINDOW_RESIZING_MASK = 15,
416 WINDOW_EXTERIOR = 16,
417 WINDOW_TITLEBAR = 17,
418 WINDOW_CLIENT_AREA = 18,
419};
420
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200421static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422
Pekka Paalanen97777442013-05-22 10:20:05 +0300423/* #define DEBUG */
424
425#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300426
427static void
428debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
429__attribute__ ((format (printf, 4, 5)));
430
431static void
432debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
433{
434 va_list ap;
435 struct timeval tv;
436
437 gettimeofday(&tv, NULL);
438 fprintf(stderr, "%8ld.%03ld ",
439 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
440
441 if (proxy)
442 fprintf(stderr, "%s@%d ",
443 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
444
445 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
446 fprintf(stderr, "%s ", func);
447
448 va_start(ap, fmt);
449 vfprintf(stderr, fmt, ap);
450 va_end(ap);
451}
452
453#define DBG(fmt, ...) \
454 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
455
456#define DBG_OBJ(obj, fmt, ...) \
457 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
458
459#else
460
461#define DBG(...) do {} while (0)
462#define DBG_OBJ(...) do {} while (0)
463
464#endif
465
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500466#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400467
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200468struct egl_window_surface {
469 struct toysurface base;
470 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100471 struct display *display;
472 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200473 struct wl_egl_window *egl_window;
474 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100475};
476
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200477static struct egl_window_surface *
478to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100479{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200480 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100481}
482
483static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200484egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200485 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100486{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200487 struct egl_window_surface *surface = to_egl_window_surface(base);
488
489 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
490 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
491
492 return cairo_surface_reference(surface->cairo_surface);
493}
494
495static void
496egl_window_surface_swap(struct toysurface *base,
497 struct rectangle *server_allocation)
498{
499 struct egl_window_surface *surface = to_egl_window_surface(base);
500
501 cairo_gl_surface_swapbuffers(surface->cairo_surface);
502 wl_egl_window_get_attached_size(surface->egl_window,
503 &server_allocation->width,
504 &server_allocation->height);
505}
506
507static int
508egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
509{
510 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200511 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100512
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200513 device = cairo_surface_get_device(surface->cairo_surface);
514 if (!device)
515 return -1;
516
517 if (!ctx) {
518 if (device == surface->display->argb_device)
519 ctx = surface->display->argb_ctx;
520 else
521 assert(0);
522 }
523
524 cairo_device_flush(device);
525 cairo_device_acquire(device);
526 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
527 surface->egl_surface, ctx))
528 fprintf(stderr, "failed to make surface current\n");
529
530 return 0;
531}
532
533static void
534egl_window_surface_release(struct toysurface *base)
535{
536 struct egl_window_surface *surface = to_egl_window_surface(base);
537 cairo_device_t *device;
538
539 device = cairo_surface_get_device(surface->cairo_surface);
540 if (!device)
541 return;
542
543 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
544 surface->display->argb_ctx))
545 fprintf(stderr, "failed to make context current\n");
546
547 cairo_device_release(device);
548}
549
550static void
551egl_window_surface_destroy(struct toysurface *base)
552{
553 struct egl_window_surface *surface = to_egl_window_surface(base);
554 struct display *d = surface->display;
555
556 cairo_surface_destroy(surface->cairo_surface);
557 eglDestroySurface(d->dpy, surface->egl_surface);
558 wl_egl_window_destroy(surface->egl_window);
559 surface->surface = NULL;
560
561 free(surface);
562}
563
564static struct toysurface *
565egl_window_surface_create(struct display *display,
566 struct wl_surface *wl_surface,
567 uint32_t flags,
568 struct rectangle *rectangle)
569{
570 struct egl_window_surface *surface;
571
Pekka Paalanenb3627362012-11-19 17:16:00 +0200572 if (display->dpy == EGL_NO_DISPLAY)
573 return NULL;
574
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200575 surface = calloc(1, sizeof *surface);
576 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100577 return NULL;
578
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200579 surface->base.prepare = egl_window_surface_prepare;
580 surface->base.swap = egl_window_surface_swap;
581 surface->base.acquire = egl_window_surface_acquire;
582 surface->base.release = egl_window_surface_release;
583 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100584
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200585 surface->display = display;
586 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400587
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200588 surface->egl_window = wl_egl_window_create(surface->surface,
589 rectangle->width,
590 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100591
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200592 surface->egl_surface = eglCreateWindowSurface(display->dpy,
593 display->argb_config,
594 surface->egl_window,
595 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100596
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200597 surface->cairo_surface =
598 cairo_gl_surface_create_for_egl(display->argb_device,
599 surface->egl_surface,
600 rectangle->width,
601 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100602
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200603 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100604}
605
Pekka Paalanenb3627362012-11-19 17:16:00 +0200606#else
607
608static struct toysurface *
609egl_window_surface_create(struct display *display,
610 struct wl_surface *wl_surface,
611 uint32_t flags,
612 struct rectangle *rectangle)
613{
614 return NULL;
615}
616
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617#endif
618
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200619struct shm_surface_data {
620 struct wl_buffer *buffer;
621 struct shm_pool *pool;
622};
623
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624struct wl_buffer *
625display_get_buffer_for_surface(struct display *display,
626 cairo_surface_t *surface)
627{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200628 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200630 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631
632 return data->buffer;
633}
634
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500635static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700636shm_pool_destroy(struct shm_pool *pool);
637
638static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400639shm_surface_data_destroy(void *p)
640{
641 struct shm_surface_data *data = p;
642
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200643 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700644 if (data->pool)
645 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300646
647 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400648}
649
Kristian Høgsberg16626282012-04-03 11:21:27 -0400650static struct wl_shm_pool *
651make_shm_pool(struct display *display, int size, void **data)
652{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400653 struct wl_shm_pool *pool;
654 int fd;
655
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300656 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400657 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300658 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
659 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400660 return NULL;
661 }
662
663 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400664 if (*data == MAP_FAILED) {
665 fprintf(stderr, "mmap failed: %m\n");
666 close(fd);
667 return NULL;
668 }
669
670 pool = wl_shm_create_pool(display->shm, fd, size);
671
672 close(fd);
673
674 return pool;
675}
676
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700677static struct shm_pool *
678shm_pool_create(struct display *display, size_t size)
679{
680 struct shm_pool *pool = malloc(sizeof *pool);
681
682 if (!pool)
683 return NULL;
684
685 pool->pool = make_shm_pool(display, size, &pool->data);
686 if (!pool->pool) {
687 free(pool);
688 return NULL;
689 }
690
691 pool->size = size;
692 pool->used = 0;
693
694 return pool;
695}
696
697static void *
698shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
699{
700 if (pool->used + size > pool->size)
701 return NULL;
702
703 *offset = pool->used;
704 pool->used += size;
705
706 return (char *) pool->data + *offset;
707}
708
709/* destroy the pool. this does not unmap the memory though */
710static void
711shm_pool_destroy(struct shm_pool *pool)
712{
713 munmap(pool->data, pool->size);
714 wl_shm_pool_destroy(pool->pool);
715 free(pool);
716}
717
718/* Start allocating from the beginning of the pool again */
719static void
720shm_pool_reset(struct shm_pool *pool)
721{
722 pool->used = 0;
723}
724
725static int
726data_length_for_shm_surface(struct rectangle *rect)
727{
728 int stride;
729
730 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
731 rect->width);
732 return stride * rect->height;
733}
734
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500735static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700736display_create_shm_surface_from_pool(struct display *display,
737 struct rectangle *rectangle,
738 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400739{
740 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400741 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400742 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700743 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400744 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400745
746 data = malloc(sizeof *data);
747 if (data == NULL)
748 return NULL;
749
750 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
751 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700752 length = stride * rectangle->height;
753 data->pool = NULL;
754 map = shm_pool_allocate(pool, length, &offset);
755
756 if (!map) {
757 free(data);
758 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400759 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400760
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400761 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400762 CAIRO_FORMAT_ARGB32,
763 rectangle->width,
764 rectangle->height,
765 stride);
766
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200767 cairo_surface_set_user_data(surface, &shm_surface_data_key,
768 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400769
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400770 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500771 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400772 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500773 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400774
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200775 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
776 rectangle->width,
777 rectangle->height,
778 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400779
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700780 return surface;
781}
782
783static cairo_surface_t *
784display_create_shm_surface(struct display *display,
785 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200786 struct shm_pool *alternate_pool,
787 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700788{
789 struct shm_surface_data *data;
790 struct shm_pool *pool;
791 cairo_surface_t *surface;
792
Pekka Paalanen99436862012-11-19 17:15:59 +0200793 if (alternate_pool) {
794 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700795 surface = display_create_shm_surface_from_pool(display,
796 rectangle,
797 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200798 alternate_pool);
799 if (surface) {
800 data = cairo_surface_get_user_data(surface,
801 &shm_surface_data_key);
802 goto out;
803 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700804 }
805
806 pool = shm_pool_create(display,
807 data_length_for_shm_surface(rectangle));
808 if (!pool)
809 return NULL;
810
811 surface =
812 display_create_shm_surface_from_pool(display, rectangle,
813 flags, pool);
814
815 if (!surface) {
816 shm_pool_destroy(pool);
817 return NULL;
818 }
819
820 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200821 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700822 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823
Pekka Paalanen99436862012-11-19 17:15:59 +0200824out:
825 if (data_ret)
826 *data_ret = data;
827
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400828 return surface;
829}
830
nobled7b87cb02011-02-01 18:51:47 +0000831static int
832check_size(struct rectangle *rect)
833{
834 if (rect->width && rect->height)
835 return 0;
836
837 fprintf(stderr, "tried to create surface of "
838 "width: %d, height: %d\n", rect->width, rect->height);
839 return -1;
840}
841
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400842cairo_surface_t *
843display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100844 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400845 struct rectangle *rectangle,
846 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847{
nobled7b87cb02011-02-01 18:51:47 +0000848 if (check_size(rectangle) < 0)
849 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200850
851 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200852 return display_create_shm_surface(display, rectangle, flags,
853 NULL, NULL);
854}
855
Pekka Paalanena4eda732012-11-19 17:16:02 +0200856struct shm_surface_leaf {
857 cairo_surface_t *cairo_surface;
858 /* 'data' is automatically destroyed, when 'cairo_surface' is */
859 struct shm_surface_data *data;
860
861 struct shm_pool *resize_pool;
862 int busy;
863};
864
865static void
866shm_surface_leaf_release(struct shm_surface_leaf *leaf)
867{
868 if (leaf->cairo_surface)
869 cairo_surface_destroy(leaf->cairo_surface);
870 /* leaf->data already destroyed via cairo private */
871
872 if (leaf->resize_pool)
873 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200874
875 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200876}
877
Pekka Paalanenaef02542013-04-25 13:57:47 +0300878#define MAX_LEAVES 3
879
Pekka Paalanen99436862012-11-19 17:15:59 +0200880struct shm_surface {
881 struct toysurface base;
882 struct display *display;
883 struct wl_surface *surface;
884 uint32_t flags;
885 int dx, dy;
886
Pekka Paalanenaef02542013-04-25 13:57:47 +0300887 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200888 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200889};
890
891static struct shm_surface *
892to_shm_surface(struct toysurface *base)
893{
894 return container_of(base, struct shm_surface, base);
895}
896
Pekka Paalanena4eda732012-11-19 17:16:02 +0200897static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300898shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
899{
900#ifdef DEBUG
901 struct shm_surface_leaf *leaf;
902 char bufs[MAX_LEAVES + 1];
903 int i;
904
905 for (i = 0; i < MAX_LEAVES; i++) {
906 leaf = &surface->leaf[i];
907
908 if (leaf->busy)
909 bufs[i] = 'b';
910 else if (leaf->cairo_surface)
911 bufs[i] = 'a';
912 else
913 bufs[i] = ' ';
914 }
915
916 bufs[MAX_LEAVES] = '\0';
917 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
918#endif
919}
920
921static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200922shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
923{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200924 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300925 struct shm_surface_leaf *leaf;
926 int i;
927 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300928
929 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200930
Pekka Paalanenaef02542013-04-25 13:57:47 +0300931 for (i = 0; i < MAX_LEAVES; i++) {
932 leaf = &surface->leaf[i];
933 if (leaf->data && leaf->data->buffer == buffer) {
934 leaf->busy = 0;
935 break;
936 }
937 }
938 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200939
Pekka Paalanenaef02542013-04-25 13:57:47 +0300940 /* Leave one free leaf with storage, release others */
941 free_found = 0;
942 for (i = 0; i < MAX_LEAVES; i++) {
943 leaf = &surface->leaf[i];
944
945 if (!leaf->cairo_surface || leaf->busy)
946 continue;
947
948 if (!free_found)
949 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300950 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300951 shm_surface_leaf_release(leaf);
952 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300953
Pekka Paalanen97777442013-05-22 10:20:05 +0300954 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200955}
956
957static const struct wl_buffer_listener shm_surface_buffer_listener = {
958 shm_surface_buffer_release
959};
960
Pekka Paalanen99436862012-11-19 17:15:59 +0200961static cairo_surface_t *
962shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200963 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200964{
Pekka Paalanenec076692012-11-30 13:37:27 +0200965 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200966 struct shm_surface *surface = to_shm_surface(base);
967 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanenaef02542013-04-25 13:57:47 +0300968 struct shm_surface_leaf *leaf = NULL;
969 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200970
971 surface->dx = dx;
972 surface->dy = dy;
973
Pekka Paalanenaef02542013-04-25 13:57:47 +0300974 /* pick a free buffer, preferrably one that already has storage */
975 for (i = 0; i < MAX_LEAVES; i++) {
976 if (surface->leaf[i].busy)
977 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200978
Pekka Paalanenaef02542013-04-25 13:57:47 +0300979 if (!leaf || surface->leaf[i].cairo_surface)
980 leaf = &surface->leaf[i];
981 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300982 DBG_OBJ(surface->surface, "pick leaf %d\n",
983 (int)(leaf - &surface->leaf[0]));
984
Pekka Paalanenaef02542013-04-25 13:57:47 +0300985 if (!leaf) {
986 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +0200987 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +0300988 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200989 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200990 }
991
Pekka Paalanena4eda732012-11-19 17:16:02 +0200992 if (!resize_hint && leaf->resize_pool) {
993 cairo_surface_destroy(leaf->cairo_surface);
994 leaf->cairo_surface = NULL;
995 shm_pool_destroy(leaf->resize_pool);
996 leaf->resize_pool = NULL;
997 }
998
999 if (leaf->cairo_surface &&
1000 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1001 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001002 goto out;
1003
Pekka Paalanena4eda732012-11-19 17:16:02 +02001004 if (leaf->cairo_surface)
1005 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001006
Pekka Paalanena4eda732012-11-19 17:16:02 +02001007 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001008 /* Create a big pool to allocate from, while continuously
1009 * resizing. Mmapping a new pool in the server
1010 * is relatively expensive, so reusing a pool performs
1011 * better, but may temporarily reserve unneeded memory.
1012 */
1013 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001014 leaf->resize_pool = shm_pool_create(surface->display,
1015 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001016 }
1017
Pekka Paalanena4eda732012-11-19 17:16:02 +02001018 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001019 display_create_shm_surface(surface->display, &rect,
1020 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001021 leaf->resize_pool,
1022 &leaf->data);
1023 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001024 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001025
1026out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001027 surface->current = leaf;
1028
1029 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001030}
1031
1032static void
1033shm_surface_swap(struct toysurface *base,
1034 struct rectangle *server_allocation)
1035{
1036 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001037 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001038
1039 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001040 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001041 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001042 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001043
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001045 surface->dx, surface->dy);
1046 wl_surface_damage(surface->surface, 0, 0,
1047 server_allocation->width, server_allocation->height);
1048 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049
Pekka Paalanen71233882013-04-25 13:57:53 +03001050 DBG_OBJ(surface->surface, "leaf %d busy\n",
1051 (int)(leaf - &surface->leaf[0]));
1052
Pekka Paalanena4eda732012-11-19 17:16:02 +02001053 leaf->busy = 1;
1054 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001055}
1056
1057static int
1058shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1059{
1060 return -1;
1061}
1062
1063static void
1064shm_surface_release(struct toysurface *base)
1065{
1066}
1067
1068static void
1069shm_surface_destroy(struct toysurface *base)
1070{
1071 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001072 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001073
Pekka Paalanenaef02542013-04-25 13:57:47 +03001074 for (i = 0; i < MAX_LEAVES; i++)
1075 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
1077 free(surface);
1078}
1079
1080static struct toysurface *
1081shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1082 uint32_t flags, struct rectangle *rectangle)
1083{
1084 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001085 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001086
1087 surface = calloc(1, sizeof *surface);
1088 if (!surface)
1089 return NULL;
1090
1091 surface->base.prepare = shm_surface_prepare;
1092 surface->base.swap = shm_surface_swap;
1093 surface->base.acquire = shm_surface_acquire;
1094 surface->base.release = shm_surface_release;
1095 surface->base.destroy = shm_surface_destroy;
1096
1097 surface->display = display;
1098 surface->surface = wl_surface;
1099 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001100
1101 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001102}
1103
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001104/*
1105 * The following correspondences between file names and cursors was copied
1106 * from: https://bugs.kde.org/attachment.cgi?id=67313
1107 */
1108
1109static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001110 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001111 "sw-resize",
1112 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001113};
1114
1115static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001116 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001117 "se-resize",
1118 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001119};
1120
1121static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001122 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001123 "s-resize",
1124 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001125};
1126
1127static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001128 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001129 "closedhand",
1130 "208530c400c041818281048008011002"
1131};
1132
1133static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001134 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001135 "default",
1136 "top_left_arrow",
1137 "left-arrow"
1138};
1139
1140static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001141 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001142 "w-resize",
1143 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001144};
1145
1146static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001147 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001148 "e-resize",
1149 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001150};
1151
1152static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001153 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001154 "nw-resize",
1155 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001156};
1157
1158static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001159 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001160 "ne-resize",
1161 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001162};
1163
1164static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001165 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001166 "n-resize",
1167 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001168};
1169
1170static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001171 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001172 "ibeam",
1173 "text"
1174};
1175
1176static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001177 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001178 "pointer",
1179 "pointing_hand",
1180 "e29285e634086352946a0e7090d73106"
1181};
1182
1183static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001184 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001185 "wait",
1186 "0426c94ea35c87780ff01dc239897213"
1187};
1188
1189struct cursor_alternatives {
1190 const char **names;
1191 size_t count;
1192};
1193
1194static const struct cursor_alternatives cursors[] = {
1195 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1196 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1197 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1198 {grabbings, ARRAY_LENGTH(grabbings)},
1199 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1200 {left_sides, ARRAY_LENGTH(left_sides)},
1201 {right_sides, ARRAY_LENGTH(right_sides)},
1202 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1203 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1204 {top_sides, ARRAY_LENGTH(top_sides)},
1205 {xterms, ARRAY_LENGTH(xterms)},
1206 {hand1s, ARRAY_LENGTH(hand1s)},
1207 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208};
1209
1210static void
1211create_cursors(struct display *display)
1212{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001213 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001214 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001215 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001216 unsigned int i, j;
1217 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001218 struct config_key shell_keys[] = {
1219 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001220 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001221 };
1222 struct config_section cs[] = {
1223 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1224 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001225
Ossama Othmana50e6e42013-05-14 09:48:26 -07001226 config_fd = open_config_file("weston.ini");
1227 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1228 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001229
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001230 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001231 display->cursors =
1232 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1233
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001234 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235 cursor = NULL;
1236 for (j = 0; !cursor && j < cursors[i].count; ++j)
1237 cursor = wl_cursor_theme_get_cursor(
1238 display->cursor_theme, cursors[i].names[j]);
1239
1240 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001241 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242 cursors[i].names[0]);
1243
1244 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001245 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001246}
1247
1248static void
1249destroy_cursors(struct display *display)
1250{
1251 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001252 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001253}
1254
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001255struct wl_cursor_image *
1256display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001260 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261}
1262
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001263static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001264surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001265{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001266 if (!surface->cairo_surface)
1267 return;
1268
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001269 if (surface->opaque_region) {
1270 wl_surface_set_opaque_region(surface->surface,
1271 surface->opaque_region);
1272 wl_region_destroy(surface->opaque_region);
1273 surface->opaque_region = NULL;
1274 }
1275
1276 if (surface->input_region) {
1277 wl_surface_set_input_region(surface->surface,
1278 surface->input_region);
1279 wl_region_destroy(surface->input_region);
1280 surface->input_region = NULL;
1281 }
1282
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001283 surface->toysurface->swap(surface->toysurface,
1284 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001285
Pekka Paalanen89dee002013-02-13 16:17:20 +02001286 cairo_surface_destroy(surface->cairo_surface);
1287 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001288}
1289
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001290int
1291window_has_focus(struct window *window)
1292{
1293 return window->focus_count > 0;
1294}
1295
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001296static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001297window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001298{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001299 struct surface *surface;
1300
Pekka Paalanen89dee002013-02-13 16:17:20 +02001301 if (window->type == TYPE_NONE) {
1302 window->type = TYPE_TOPLEVEL;
1303 if (window->shell_surface)
1304 wl_shell_surface_set_toplevel(window->shell_surface);
1305 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001306
Pekka Paalanen35e82632013-04-25 13:57:48 +03001307 wl_list_for_each(surface, &window->subsurface_list, link) {
1308 if (surface == window->main_surface)
1309 continue;
1310
1311 surface_flush(surface);
1312 }
1313
Pekka Paalanen89dee002013-02-13 16:17:20 +02001314 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001315}
1316
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001317struct display *
1318window_get_display(struct window *window)
1319{
1320 return window->display;
1321}
1322
Pekka Paalanen89dee002013-02-13 16:17:20 +02001323static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001324surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001325{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001326 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001327 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001328
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001329 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001330 case WL_OUTPUT_TRANSFORM_90:
1331 case WL_OUTPUT_TRANSFORM_270:
1332 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1333 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001334 allocation.width = surface->allocation.height;
1335 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001336 break;
1337 default:
1338 break;
1339 }
1340
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001341 if (!surface->toysurface && display->dpy &&
1342 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001343 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001344 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001345 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001346 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001347 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001348 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001349
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001350 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001351 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352 surface->surface,
1353 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001354
Pekka Paalanen89dee002013-02-13 16:17:20 +02001355 surface->cairo_surface = surface->toysurface->prepare(
1356 surface->toysurface, dx, dy,
1357 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001358}
1359
1360static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001361window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001362{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001363 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001364 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001365 int dx = 0;
1366 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001367
Pekka Paalanenec076692012-11-30 13:37:27 +02001368 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001369 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001370
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001371 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1372 dx = surface->server_allocation.width -
1373 surface->allocation.width;
1374
1375 if (window->resize_edges & WINDOW_RESIZING_TOP)
1376 dy = surface->server_allocation.height -
1377 surface->allocation.height;
1378
1379 window->resize_edges = 0;
1380
Pekka Paalanen89dee002013-02-13 16:17:20 +02001381 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001382}
1383
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001384int
1385window_get_buffer_transform(struct window *window)
1386{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001387 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001388}
1389
1390void
1391window_set_buffer_transform(struct window *window,
1392 enum wl_output_transform transform)
1393{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001394 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001395 wl_surface_set_buffer_transform(window->main_surface->surface,
1396 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001397}
1398
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001399static void frame_destroy(struct frame *frame);
1400
Pekka Paalanen4e373742013-02-13 16:17:13 +02001401static void
1402surface_destroy(struct surface *surface)
1403{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001404 if (surface->frame_cb)
1405 wl_callback_destroy(surface->frame_cb);
1406
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001407 if (surface->input_region)
1408 wl_region_destroy(surface->input_region);
1409
1410 if (surface->opaque_region)
1411 wl_region_destroy(surface->opaque_region);
1412
Pekka Paalanen35e82632013-04-25 13:57:48 +03001413 if (surface->subsurface)
1414 wl_subsurface_destroy(surface->subsurface);
1415
Pekka Paalanen4e373742013-02-13 16:17:13 +02001416 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001417
1418 if (surface->toysurface)
1419 surface->toysurface->destroy(surface->toysurface);
1420
Pekka Paalanen35e82632013-04-25 13:57:48 +03001421 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001422 free(surface);
1423}
1424
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001425void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001426window_destroy(struct window *window)
1427{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001428 struct display *display = window->display;
1429 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001430 struct window_output *window_output;
1431 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001432
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001433 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001434
1435 wl_list_for_each(input, &display->input_list, link) {
1436 if (input->pointer_focus == window)
1437 input->pointer_focus = NULL;
1438 if (input->keyboard_focus == window)
1439 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001440 if (input->focus_widget &&
1441 input->focus_widget->window == window)
1442 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001443 }
1444
Rob Bradford7507b572012-05-15 17:55:34 +01001445 wl_list_for_each_safe(window_output, window_output_tmp,
1446 &window->window_output_list, link) {
1447 free (window_output);
1448 }
1449
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001450 if (window->frame)
1451 frame_destroy(window->frame);
1452
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001453 if (window->shell_surface)
1454 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001455
1456 surface_destroy(window->main_surface);
1457
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001458 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001459
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001460 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001461 free(window);
1462}
1463
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001464static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001465widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001466{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001467 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001468
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001469 wl_list_for_each(child, &widget->child_list, link) {
1470 target = widget_find_widget(child, x, y);
1471 if (target)
1472 return target;
1473 }
1474
1475 if (widget->allocation.x <= x &&
1476 x < widget->allocation.x + widget->allocation.width &&
1477 widget->allocation.y <= y &&
1478 y < widget->allocation.y + widget->allocation.height) {
1479 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001480 }
1481
1482 return NULL;
1483}
1484
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001485static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001486window_find_widget(struct window *window, int32_t x, int32_t y)
1487{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001488 struct surface *surface;
1489 struct widget *widget;
1490
1491 wl_list_for_each(surface, &window->subsurface_list, link) {
1492 widget = widget_find_widget(surface->widget, x, y);
1493 if (widget)
1494 return widget;
1495 }
1496
1497 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001498}
1499
1500static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001501widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001502{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001503 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001504
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001505 widget = malloc(sizeof *widget);
1506 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001507 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001508 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001509 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001510 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001511 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001512 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001513 widget->tooltip = NULL;
1514 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001515 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001516
1517 return widget;
1518}
1519
1520struct widget *
1521window_add_widget(struct window *window, void *data)
1522{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001523 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001524
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001525 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001526 wl_list_init(&widget->link);
1527 window->main_surface->widget = widget;
1528
1529 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001530}
1531
1532struct widget *
1533widget_add_widget(struct widget *parent, void *data)
1534{
1535 struct widget *widget;
1536
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001537 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001538 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001539
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001540 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001541}
1542
1543void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001544widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001545{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001546 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001547 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001548 struct input *input;
1549
Pekka Paalanen35e82632013-04-25 13:57:48 +03001550 /* Destroy the sub-surface along with the root widget */
1551 if (surface->widget == widget && surface->subsurface)
1552 surface_destroy(widget->surface);
1553
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001554 if (widget->tooltip) {
1555 free(widget->tooltip);
1556 widget->tooltip = NULL;
1557 }
1558
Pekka Paalanene156fb62012-01-19 13:51:38 +02001559 wl_list_for_each(input, &display->input_list, link) {
1560 if (input->focus_widget == widget)
1561 input->focus_widget = NULL;
1562 }
1563
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001564 wl_list_remove(&widget->link);
1565 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001566}
1567
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001569widget_set_default_cursor(struct widget *widget, int cursor)
1570{
1571 widget->default_cursor = cursor;
1572}
1573
1574void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001575widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001576{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001577 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001578}
1579
1580void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001581widget_set_size(struct widget *widget, int32_t width, int32_t height)
1582{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001583 widget->allocation.width = width;
1584 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001585}
1586
1587void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001588widget_set_allocation(struct widget *widget,
1589 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001590{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001591 widget->allocation.x = x;
1592 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001593 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001594}
1595
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001596void
1597widget_set_transparent(struct widget *widget, int transparent)
1598{
1599 widget->opaque = !transparent;
1600}
1601
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001602void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001603widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001604{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001605 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001606}
1607
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001608static cairo_surface_t *
1609widget_get_cairo_surface(struct widget *widget)
1610{
1611 struct surface *surface = widget->surface;
1612 struct window *window = widget->window;
1613
1614 if (!surface->cairo_surface) {
1615 if (surface == window->main_surface)
1616 window_create_main_surface(window);
1617 else
1618 surface_create_surface(surface, 0, 0, 0);
1619 }
1620
1621 return surface->cairo_surface;
1622}
1623
Alexander Larsson15901f02013-05-22 14:41:25 +02001624static void
1625widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1626{
1627 struct surface *surface = widget->surface;
1628 double angle;
1629 cairo_matrix_t m;
1630 enum wl_output_transform transform;
1631 int surface_width, surface_height;
1632 int translate_x, translate_y;
1633
1634 surface_width = surface->allocation.width;
1635 surface_height = surface->allocation.height;
1636
1637 transform = window_get_buffer_transform(widget->window);
1638 switch (transform) {
1639 case WL_OUTPUT_TRANSFORM_FLIPPED:
1640 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1641 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1642 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1643 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1644 break;
1645 default:
1646 cairo_matrix_init_identity(&m);
1647 break;
1648 }
1649
1650 switch (transform) {
1651 case WL_OUTPUT_TRANSFORM_NORMAL:
1652 default:
1653 angle = 0;
1654 translate_x = 0;
1655 translate_y = 0;
1656 break;
1657 case WL_OUTPUT_TRANSFORM_FLIPPED:
1658 angle = 0;
1659 translate_x = surface_width;
1660 translate_y = 0;
1661 break;
1662 case WL_OUTPUT_TRANSFORM_90:
1663 angle = M_PI_2;
1664 translate_x = surface_height;
1665 translate_y = 0;
1666 break;
1667 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1668 angle = M_PI_2;
1669 translate_x = surface_height;
1670 translate_y = surface_width;
1671 break;
1672 case WL_OUTPUT_TRANSFORM_180:
1673 angle = M_PI;
1674 translate_x = surface_width;
1675 translate_y = surface_height;
1676 break;
1677 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1678 angle = M_PI;
1679 translate_x = 0;
1680 translate_y = surface_height;
1681 break;
1682 case WL_OUTPUT_TRANSFORM_270:
1683 angle = M_PI + M_PI_2;
1684 translate_x = 0;
1685 translate_y = surface_width;
1686 break;
1687 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1688 angle = M_PI + M_PI_2;
1689 translate_x = 0;
1690 translate_y = 0;
1691 break;
1692 }
1693
1694 cairo_translate(cr, translate_x, translate_y);
1695 cairo_rotate(cr, angle);
1696 cairo_transform(cr, &m);
1697}
1698
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001699cairo_t *
1700widget_cairo_create(struct widget *widget)
1701{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001702 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001703 cairo_surface_t *cairo_surface;
1704 cairo_t *cr;
1705
1706 cairo_surface = widget_get_cairo_surface(widget);
1707 cr = cairo_create(cairo_surface);
1708
Alexander Larsson15901f02013-05-22 14:41:25 +02001709 widget_cairo_update_transform(widget, cr);
1710
Pekka Paalanen35e82632013-04-25 13:57:48 +03001711 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1712
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001713 return cr;
1714}
1715
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001716struct wl_surface *
1717widget_get_wl_surface(struct widget *widget)
1718{
1719 return widget->surface->surface;
1720}
1721
1722uint32_t
1723widget_get_last_time(struct widget *widget)
1724{
1725 return widget->surface->last_time;
1726}
1727
1728void
1729widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1730{
1731 struct wl_compositor *comp = widget->window->display->compositor;
1732 struct surface *surface = widget->surface;
1733
1734 if (!surface->input_region)
1735 surface->input_region = wl_compositor_create_region(comp);
1736
1737 if (rect) {
1738 wl_region_add(surface->input_region,
1739 rect->x, rect->y, rect->width, rect->height);
1740 }
1741}
1742
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001743void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001744widget_set_resize_handler(struct widget *widget,
1745 widget_resize_handler_t handler)
1746{
1747 widget->resize_handler = handler;
1748}
1749
1750void
1751widget_set_redraw_handler(struct widget *widget,
1752 widget_redraw_handler_t handler)
1753{
1754 widget->redraw_handler = handler;
1755}
1756
1757void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001758widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001759{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001760 widget->enter_handler = handler;
1761}
1762
1763void
1764widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1765{
1766 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001767}
1768
1769void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001770widget_set_motion_handler(struct widget *widget,
1771 widget_motion_handler_t handler)
1772{
1773 widget->motion_handler = handler;
1774}
1775
1776void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001777widget_set_button_handler(struct widget *widget,
1778 widget_button_handler_t handler)
1779{
1780 widget->button_handler = handler;
1781}
1782
1783void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001784widget_set_axis_handler(struct widget *widget,
1785 widget_axis_handler_t handler)
1786{
1787 widget->axis_handler = handler;
1788}
1789
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001790static void
1791window_schedule_redraw_task(struct window *window);
1792
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001793void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001794widget_schedule_redraw(struct widget *widget)
1795{
Pekka Paalanen71233882013-04-25 13:57:53 +03001796 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001797 widget->surface->redraw_needed = 1;
1798 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001799}
1800
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001801cairo_surface_t *
1802window_get_surface(struct window *window)
1803{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001804 cairo_surface_t *cairo_surface;
1805
1806 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1807
1808 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001809}
1810
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001811struct wl_surface *
1812window_get_wl_surface(struct window *window)
1813{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001814 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001815}
1816
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001817struct wl_shell_surface *
1818window_get_wl_shell_surface(struct window *window)
1819{
1820 return window->shell_surface;
1821}
1822
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001823static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001824tooltip_redraw_handler(struct widget *widget, void *data)
1825{
1826 cairo_t *cr;
1827 const int32_t r = 3;
1828 struct tooltip *tooltip = data;
1829 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001830
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001831 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001832 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1833 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1834 cairo_paint(cr);
1835
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001836 width = widget->allocation.width;
1837 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001838 rounded_rect(cr, 0, 0, width, height, r);
1839
1840 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1841 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1842 cairo_fill(cr);
1843
1844 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1845 cairo_move_to(cr, 10, 16);
1846 cairo_show_text(cr, tooltip->entry);
1847 cairo_destroy(cr);
1848}
1849
1850static cairo_text_extents_t
1851get_text_extents(struct tooltip *tooltip)
1852{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001853 cairo_t *cr;
1854 cairo_text_extents_t extents;
1855
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001856 /* Use the dummy_surface because tooltip's surface was not
1857 * created yet, and parent does not have a valid surface
1858 * outside repaint, either.
1859 */
1860 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001861 cairo_text_extents(cr, tooltip->entry, &extents);
1862 cairo_destroy(cr);
1863
1864 return extents;
1865}
1866
1867static int
1868window_create_tooltip(struct tooltip *tooltip)
1869{
1870 struct widget *parent = tooltip->parent;
1871 struct display *display = parent->window->display;
1872 struct window *window;
1873 const int offset_y = 27;
1874 const int margin = 3;
1875 cairo_text_extents_t extents;
1876
1877 if (tooltip->widget)
1878 return 0;
1879
1880 window = window_create_transient(display, parent->window, tooltip->x,
1881 tooltip->y + offset_y,
1882 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1883 if (!window)
1884 return -1;
1885
1886 tooltip->window = window;
1887 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1888
1889 extents = get_text_extents(tooltip);
1890 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1891 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1892
1893 return 0;
1894}
1895
1896void
1897widget_destroy_tooltip(struct widget *parent)
1898{
1899 struct tooltip *tooltip = parent->tooltip;
1900
1901 parent->tooltip_count = 0;
1902 if (!tooltip)
1903 return;
1904
1905 if (tooltip->widget) {
1906 widget_destroy(tooltip->widget);
1907 window_destroy(tooltip->window);
1908 tooltip->widget = NULL;
1909 tooltip->window = NULL;
1910 }
1911
1912 close(tooltip->tooltip_fd);
1913 free(tooltip->entry);
1914 free(tooltip);
1915 parent->tooltip = NULL;
1916}
1917
1918static void
1919tooltip_func(struct task *task, uint32_t events)
1920{
1921 struct tooltip *tooltip =
1922 container_of(task, struct tooltip, tooltip_task);
1923 uint64_t exp;
1924
Martin Olsson8df662a2012-07-08 03:03:47 +02001925 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1926 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001927 window_create_tooltip(tooltip);
1928}
1929
1930#define TOOLTIP_TIMEOUT 500
1931static int
1932tooltip_timer_reset(struct tooltip *tooltip)
1933{
1934 struct itimerspec its;
1935
1936 its.it_interval.tv_sec = 0;
1937 its.it_interval.tv_nsec = 0;
1938 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1939 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1940 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1941 fprintf(stderr, "could not set timerfd\n: %m");
1942 return -1;
1943 }
1944
1945 return 0;
1946}
1947
1948int
1949widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1950{
1951 struct tooltip *tooltip = parent->tooltip;
1952
1953 parent->tooltip_count++;
1954 if (tooltip) {
1955 tooltip->x = x;
1956 tooltip->y = y;
1957 tooltip_timer_reset(tooltip);
1958 return 0;
1959 }
1960
1961 /* the handler might be triggered too fast via input device motion, so
1962 * we need this check here to make sure tooltip is fully initialized */
1963 if (parent->tooltip_count > 1)
1964 return 0;
1965
1966 tooltip = malloc(sizeof *tooltip);
1967 if (!tooltip)
1968 return -1;
1969
1970 parent->tooltip = tooltip;
1971 tooltip->parent = parent;
1972 tooltip->widget = NULL;
1973 tooltip->window = NULL;
1974 tooltip->x = x;
1975 tooltip->y = y;
1976 tooltip->entry = strdup(entry);
1977 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1978 if (tooltip->tooltip_fd < 0) {
1979 fprintf(stderr, "could not create timerfd\n: %m");
1980 return -1;
1981 }
1982
1983 tooltip->tooltip_task.run = tooltip_func;
1984 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1985 EPOLLIN, &tooltip->tooltip_task);
1986 tooltip_timer_reset(tooltip);
1987
1988 return 0;
1989}
1990
1991static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001992workspace_manager_state(void *data,
1993 struct workspace_manager *workspace_manager,
1994 uint32_t current,
1995 uint32_t count)
1996{
1997 struct display *display = data;
1998
1999 display->workspace = current;
2000 display->workspace_count = count;
2001}
2002
2003static const struct workspace_manager_listener workspace_manager_listener = {
2004 workspace_manager_state
2005};
2006
2007static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002008frame_resize_handler(struct widget *widget,
2009 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002010{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002011 struct frame *frame = data;
2012 struct widget *child = frame->child;
2013 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002014 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002015 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002016 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002017 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002018 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002019 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002020 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002021
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002022 switch (widget->window->type) {
2023 case TYPE_FULLSCREEN:
2024 decoration_width = 0;
2025 decoration_height = 0;
2026
2027 allocation.x = 0;
2028 allocation.y = 0;
2029 allocation.width = width;
2030 allocation.height = height;
2031 opaque_margin = 0;
2032
2033 wl_list_for_each(button, &frame->buttons_list, link)
2034 button->widget->opaque = 1;
2035 break;
2036 case TYPE_MAXIMIZED:
2037 decoration_width = t->width * 2;
2038 decoration_height = t->width + t->titlebar_height;
2039
2040 allocation.x = t->width;
2041 allocation.y = t->titlebar_height;
2042 allocation.width = width - decoration_width;
2043 allocation.height = height - decoration_height;
2044
2045 opaque_margin = 0;
2046
2047 wl_list_for_each(button, &frame->buttons_list, link)
2048 button->widget->opaque = 0;
2049 break;
2050 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002051 decoration_width = (t->width + t->margin) * 2;
2052 decoration_height = t->width +
2053 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002054
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002055 allocation.x = t->width + t->margin;
2056 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002057 allocation.width = width - decoration_width;
2058 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002059
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002060 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002061
2062 wl_list_for_each(button, &frame->buttons_list, link)
2063 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002064 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002065 }
2066
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002067 widget_set_allocation(child, allocation.x, allocation.y,
2068 allocation.width, allocation.height);
2069
2070 if (child->resize_handler)
2071 child->resize_handler(child,
2072 allocation.width,
2073 allocation.height,
2074 child->user_data);
2075
Scott Moreauf7e498c2012-05-14 11:39:29 -06002076 width = child->allocation.width + decoration_width;
2077 height = child->allocation.height + decoration_height;
2078
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002079 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2080
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002081 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002082 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002083 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002084 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002085 shadow_margin, shadow_margin,
2086 width - 2 * shadow_margin,
2087 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002088 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002089 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002090 }
2091
Scott Moreauf7e498c2012-05-14 11:39:29 -06002092 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002093
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002094 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002095 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002096 opaque_margin, opaque_margin,
2097 widget->allocation.width - 2 * opaque_margin,
2098 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002099
2100 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002101 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2102 x_l = t->width + shadow_margin;
2103 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002104 wl_list_for_each(button, &frame->buttons_list, link) {
2105 const int button_padding = 4;
2106 w = cairo_image_surface_get_width(button->icon);
2107 h = cairo_image_surface_get_height(button->icon);
2108
2109 if (button->decoration == FRAME_BUTTON_FANCY)
2110 w += 10;
2111
2112 if (button->align == FRAME_BUTTON_LEFT) {
2113 widget_set_allocation(button->widget,
2114 x_l, y , w + 1, h + 1);
2115 x_l += w;
2116 x_l += button_padding;
2117 } else {
2118 x_r -= w;
2119 widget_set_allocation(button->widget,
2120 x_r, y , w + 1, h + 1);
2121 x_r -= button_padding;
2122 }
2123 }
2124}
2125
2126static int
2127frame_button_enter_handler(struct widget *widget,
2128 struct input *input, float x, float y, void *data)
2129{
2130 struct frame_button *frame_button = data;
2131
2132 widget_schedule_redraw(frame_button->widget);
2133 frame_button->state = FRAME_BUTTON_OVER;
2134
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002135 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002136}
2137
2138static void
2139frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2140{
2141 struct frame_button *frame_button = data;
2142
2143 widget_schedule_redraw(frame_button->widget);
2144 frame_button->state = FRAME_BUTTON_DEFAULT;
2145}
2146
2147static void
2148frame_button_button_handler(struct widget *widget,
2149 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002150 uint32_t button,
2151 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002152{
2153 struct frame_button *frame_button = data;
2154 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002155 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002156
2157 if (button != BTN_LEFT)
2158 return;
2159
2160 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002161 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002162 frame_button->state = FRAME_BUTTON_ACTIVE;
2163 widget_schedule_redraw(frame_button->widget);
2164
2165 if (frame_button->type == FRAME_BUTTON_ICON)
2166 window_show_frame_menu(window, input, time);
2167 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002168 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002169 frame_button->state = FRAME_BUTTON_DEFAULT;
2170 widget_schedule_redraw(frame_button->widget);
2171 break;
2172 }
2173
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002174 if (!was_pressed)
2175 return;
2176
Martin Minarik1998b152012-05-10 02:04:35 +02002177 switch (frame_button->type) {
2178 case FRAME_BUTTON_CLOSE:
2179 if (window->close_handler)
2180 window->close_handler(window->parent,
2181 window->user_data);
2182 else
2183 display_exit(window->display);
2184 break;
2185 case FRAME_BUTTON_MINIMIZE:
2186 fprintf(stderr,"Minimize stub\n");
2187 break;
2188 case FRAME_BUTTON_MAXIMIZE:
2189 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2190 break;
2191 default:
2192 /* Unknown operation */
2193 break;
2194 }
2195}
2196
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002197static int
2198frame_button_motion_handler(struct widget *widget,
2199 struct input *input, uint32_t time,
2200 float x, float y, void *data)
2201{
2202 struct frame_button *frame_button = data;
2203 enum frame_button_pointer previous_button_state = frame_button->state;
2204
2205 /* only track state for a pressed button */
2206 if (input->grab != widget)
2207 return CURSOR_LEFT_PTR;
2208
2209 if (x > widget->allocation.x &&
2210 x < (widget->allocation.x + widget->allocation.width) &&
2211 y > widget->allocation.y &&
2212 y < (widget->allocation.y + widget->allocation.height)) {
2213 frame_button->state = FRAME_BUTTON_ACTIVE;
2214 } else {
2215 frame_button->state = FRAME_BUTTON_DEFAULT;
2216 }
2217
2218 if (frame_button->state != previous_button_state)
2219 widget_schedule_redraw(frame_button->widget);
2220
2221 return CURSOR_LEFT_PTR;
2222}
2223
Martin Minarik1998b152012-05-10 02:04:35 +02002224static void
2225frame_button_redraw_handler(struct widget *widget, void *data)
2226{
2227 struct frame_button *frame_button = data;
2228 cairo_t *cr;
2229 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002230
2231 x = widget->allocation.x;
2232 y = widget->allocation.y;
2233 width = widget->allocation.width;
2234 height = widget->allocation.height;
2235
2236 if (!width)
2237 return;
2238 if (!height)
2239 return;
2240 if (widget->opaque)
2241 return;
2242
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002243 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002244
2245 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2246 cairo_set_line_width(cr, 1);
2247
2248 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2249 cairo_rectangle (cr, x, y, 25, 16);
2250
2251 cairo_stroke_preserve(cr);
2252
2253 switch (frame_button->state) {
2254 case FRAME_BUTTON_DEFAULT:
2255 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2256 break;
2257 case FRAME_BUTTON_OVER:
2258 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2259 break;
2260 case FRAME_BUTTON_ACTIVE:
2261 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2262 break;
2263 }
2264
2265 cairo_fill (cr);
2266
2267 x += 4;
2268 }
2269
2270 cairo_set_source_surface(cr, frame_button->icon, x, y);
2271 cairo_paint(cr);
2272
2273 cairo_destroy(cr);
2274}
2275
2276static struct widget *
2277frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2278 enum frame_button_align align, enum frame_button_decoration style)
2279{
2280 struct frame_button *frame_button;
2281 const char *icon = data;
2282
2283 frame_button = malloc (sizeof *frame_button);
2284 memset(frame_button, 0, sizeof *frame_button);
2285
2286 frame_button->icon = cairo_image_surface_create_from_png(icon);
2287 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2288 frame_button->frame = frame;
2289 frame_button->type = type;
2290 frame_button->align = align;
2291 frame_button->decoration = style;
2292
2293 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2294
2295 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2296 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2297 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2298 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002299 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002300 return frame_button->widget;
2301}
2302
2303static void
2304frame_button_destroy(struct frame_button *frame_button)
2305{
2306 widget_destroy(frame_button->widget);
2307 wl_list_remove(&frame_button->link);
2308 cairo_surface_destroy(frame_button->icon);
2309 free(frame_button);
2310
2311 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002312}
2313
2314static void
2315frame_redraw_handler(struct widget *widget, void *data)
2316{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002317 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002318 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002319 struct theme *t = window->display->theme;
2320 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002321
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002322 if (window->type == TYPE_FULLSCREEN)
2323 return;
2324
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002325 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002326
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002327 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002328 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002329 if (window->type == TYPE_MAXIMIZED)
2330 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002331 theme_render_frame(t, cr, widget->allocation.width,
2332 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002333
2334 cairo_destroy(cr);
2335}
2336
2337static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002338frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002339{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002340 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002341 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002342 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002343
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002344 if (window->type != TYPE_TOPLEVEL)
2345 return CURSOR_LEFT_PTR;
2346
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002347 location = theme_get_location(t, input->sx, input->sy,
2348 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002349 frame->widget->allocation.height,
2350 window->type == TYPE_MAXIMIZED ?
2351 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002352
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002353 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002354 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002355 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002356 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002357 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002358 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002359 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002360 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002361 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002362 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002363 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002364 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002365 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002366 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002367 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002368 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002369 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002370 case THEME_LOCATION_EXTERIOR:
2371 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002372 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002373 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002374 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002375}
2376
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002377static void
2378frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002379{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002380 struct display *display;
2381
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002382 switch (index) {
2383 case 0: /* close */
2384 if (window->close_handler)
2385 window->close_handler(window->parent,
2386 window->user_data);
2387 else
2388 display_exit(window->display);
2389 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002390 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002391 display = window->display;
2392 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002393 workspace_manager_move_surface(
2394 display->workspace_manager,
2395 window->main_surface->surface,
2396 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002397 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002398 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002399 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002400 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002401 workspace_manager_move_surface(
2402 display->workspace_manager,
2403 window->main_surface->surface,
2404 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002405 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002406 case 3: /* fullscreen */
2407 /* we don't have a way to get out of fullscreen for now */
2408 if (window->fullscreen_handler)
2409 window->fullscreen_handler(window, window->user_data);
2410 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002411 }
2412}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002413
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002414void
2415window_show_frame_menu(struct window *window,
2416 struct input *input, uint32_t time)
2417{
2418 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002419 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002420
2421 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002422 "Close",
2423 "Move to workspace above", "Move to workspace below",
2424 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002425 };
2426
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002427 if (window->fullscreen_handler)
2428 count = ARRAY_LENGTH(entries);
2429 else
2430 count = ARRAY_LENGTH(entries) - 1;
2431
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002432 input_get_position(input, &x, &y);
2433 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002434 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002435}
2436
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002437static int
2438frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002439 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002440{
2441 return frame_get_pointer_image_for_location(data, input);
2442}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002443
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002444static int
2445frame_motion_handler(struct widget *widget,
2446 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002447 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002448{
2449 return frame_get_pointer_image_for_location(data, input);
2450}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002451
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002452static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002453frame_button_handler(struct widget *widget,
2454 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002455 uint32_t button, enum wl_pointer_button_state state,
2456 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002457
2458{
2459 struct frame *frame = data;
2460 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002461 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002462 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002463
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002464 if (window->type != TYPE_TOPLEVEL)
2465 return;
2466
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002467 location = theme_get_location(display->theme, input->sx, input->sy,
2468 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002469 frame->widget->allocation.height,
2470 window->type == TYPE_MAXIMIZED ?
2471 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002472
Daniel Stone4dbadb12012-05-30 16:31:51 +01002473 if (window->display->shell && button == BTN_LEFT &&
2474 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002475 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002476 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002477 if (!window->shell_surface)
2478 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002479 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002480 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002481 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002482 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002483 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002484 case THEME_LOCATION_RESIZING_TOP:
2485 case THEME_LOCATION_RESIZING_BOTTOM:
2486 case THEME_LOCATION_RESIZING_LEFT:
2487 case THEME_LOCATION_RESIZING_RIGHT:
2488 case THEME_LOCATION_RESIZING_TOP_LEFT:
2489 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2490 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2491 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002492 if (!window->shell_surface)
2493 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002494 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002495
Pekka Paalanen99436862012-11-19 17:15:59 +02002496 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002497 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002498 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002499 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002500 break;
2501 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002502 } else if (button == BTN_RIGHT &&
2503 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002504 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002505 }
2506}
2507
2508struct widget *
2509frame_create(struct window *window, void *data)
2510{
2511 struct frame *frame;
2512
2513 frame = malloc(sizeof *frame);
2514 memset(frame, 0, sizeof *frame);
2515
2516 frame->widget = window_add_widget(window, frame);
2517 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002518
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002519 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2520 widget_set_resize_handler(frame->widget, frame_resize_handler);
2521 widget_set_enter_handler(frame->widget, frame_enter_handler);
2522 widget_set_motion_handler(frame->widget, frame_motion_handler);
2523 widget_set_button_handler(frame->widget, frame_button_handler);
2524
Martin Minarik1998b152012-05-10 02:04:35 +02002525 /* Create empty list for frame buttons */
2526 wl_list_init(&frame->buttons_list);
2527
2528 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2529 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2530
2531 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2532 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2533
2534 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2535 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2536
2537 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2538 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2539
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002540 window->frame = frame;
2541
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002542 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002543}
2544
Kristian Høgsberga1627922012-06-20 17:30:03 -04002545void
2546frame_set_child_size(struct widget *widget, int child_width, int child_height)
2547{
2548 struct display *display = widget->window->display;
2549 struct theme *t = display->theme;
2550 int decoration_width, decoration_height;
2551 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002552 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002553
2554 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002555 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002556 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002557 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002558
2559 width = child_width + decoration_width;
2560 height = child_height + decoration_height;
2561 } else {
2562 width = child_width;
2563 height = child_height;
2564 }
2565
2566 window_schedule_resize(widget->window, width, height);
2567}
2568
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002569static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002570frame_destroy(struct frame *frame)
2571{
Martin Minarik1998b152012-05-10 02:04:35 +02002572 struct frame_button *button, *tmp;
2573
2574 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2575 frame_button_destroy(button);
2576
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002577 /* frame->child must be destroyed by the application */
2578 widget_destroy(frame->widget);
2579 free(frame);
2580}
2581
2582static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002583input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002584 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002585{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002586 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002587 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002588
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002589 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002590 return;
2591
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002592 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002593 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002594 widget = old;
2595 if (input->grab)
2596 widget = input->grab;
2597 if (widget->leave_handler)
2598 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002599 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002600 }
2601
2602 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002603 widget = focus;
2604 if (input->grab)
2605 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002606 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002607 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002608 cursor = widget->enter_handler(focus, input, x, y,
2609 widget->user_data);
2610 else
2611 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002612
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002613 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002614 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002615}
2616
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002617void
2618input_grab(struct input *input, struct widget *widget, uint32_t button)
2619{
2620 input->grab = widget;
2621 input->grab_button = button;
2622}
2623
2624void
2625input_ungrab(struct input *input)
2626{
2627 struct widget *widget;
2628
2629 input->grab = NULL;
2630 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002631 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002632 input->sx, input->sy);
2633 input_set_focus_widget(input, widget, input->sx, input->sy);
2634 }
2635}
2636
2637static void
2638input_remove_pointer_focus(struct input *input)
2639{
2640 struct window *window = input->pointer_focus;
2641
2642 if (!window)
2643 return;
2644
2645 input_set_focus_widget(input, NULL, 0, 0);
2646
2647 input->pointer_focus = NULL;
2648 input->current_cursor = CURSOR_UNSET;
2649}
2650
2651static void
2652pointer_handle_enter(void *data, struct wl_pointer *pointer,
2653 uint32_t serial, struct wl_surface *surface,
2654 wl_fixed_t sx_w, wl_fixed_t sy_w)
2655{
2656 struct input *input = data;
2657 struct window *window;
2658 struct widget *widget;
2659 float sx = wl_fixed_to_double(sx_w);
2660 float sy = wl_fixed_to_double(sy_w);
2661
2662 if (!surface) {
2663 /* enter event for a window we've just destroyed */
2664 return;
2665 }
2666
2667 input->display->serial = serial;
2668 input->pointer_enter_serial = serial;
2669 input->pointer_focus = wl_surface_get_user_data(surface);
2670 window = input->pointer_focus;
2671
Pekka Paalanen99436862012-11-19 17:15:59 +02002672 if (window->resizing) {
2673 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002674 /* Schedule a redraw to free the pool */
2675 window_schedule_redraw(window);
2676 }
2677
2678 input->sx = sx;
2679 input->sy = sy;
2680
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002681 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002682 input_set_focus_widget(input, widget, sx, sy);
2683}
2684
2685static void
2686pointer_handle_leave(void *data, struct wl_pointer *pointer,
2687 uint32_t serial, struct wl_surface *surface)
2688{
2689 struct input *input = data;
2690
2691 input->display->serial = serial;
2692 input_remove_pointer_focus(input);
2693}
2694
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002695static void
Daniel Stone37816df2012-05-16 18:45:18 +01002696pointer_handle_motion(void *data, struct wl_pointer *pointer,
2697 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002698{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002699 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002700 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002701 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002702 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002703 float sx = wl_fixed_to_double(sx_w);
2704 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002705
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002706 input->sx = sx;
2707 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002708
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002709 if (!window)
2710 return;
2711
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002712 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002713 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002714 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002715 }
2716
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002717 if (input->grab)
2718 widget = input->grab;
2719 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002720 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002721 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002722 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002723 input, time, sx, sy,
2724 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002725 else
2726 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002727
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002728 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002729}
2730
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002731static void
Daniel Stone37816df2012-05-16 18:45:18 +01002732pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002733 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002734{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002735 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002736 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002737 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002738
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002739 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002740 if (input->focus_widget && input->grab == NULL &&
2741 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002742 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002743
Neil Roberts6b28aad2012-01-23 19:11:18 +00002744 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002745 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002746 (*widget->button_handler)(widget,
2747 input, time,
2748 button, state,
2749 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002750
Daniel Stone4dbadb12012-05-30 16:31:51 +01002751 if (input->grab && input->grab_button == button &&
2752 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002753 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002754}
2755
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002756static void
Daniel Stone37816df2012-05-16 18:45:18 +01002757pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002758 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002759{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002760 struct input *input = data;
2761 struct widget *widget;
2762
2763 widget = input->focus_widget;
2764 if (input->grab)
2765 widget = input->grab;
2766 if (widget && widget->axis_handler)
2767 (*widget->axis_handler)(widget,
2768 input, time,
2769 axis, value,
2770 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002771}
2772
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002773static const struct wl_pointer_listener pointer_listener = {
2774 pointer_handle_enter,
2775 pointer_handle_leave,
2776 pointer_handle_motion,
2777 pointer_handle_button,
2778 pointer_handle_axis,
2779};
2780
2781static void
2782input_remove_keyboard_focus(struct input *input)
2783{
2784 struct window *window = input->keyboard_focus;
2785 struct itimerspec its;
2786
2787 its.it_interval.tv_sec = 0;
2788 its.it_interval.tv_nsec = 0;
2789 its.it_value.tv_sec = 0;
2790 its.it_value.tv_nsec = 0;
2791 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2792
2793 if (!window)
2794 return;
2795
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002796 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002797 if (window->keyboard_focus_handler)
2798 (*window->keyboard_focus_handler)(window, NULL,
2799 window->user_data);
2800
2801 input->keyboard_focus = NULL;
2802}
2803
2804static void
2805keyboard_repeat_func(struct task *task, uint32_t events)
2806{
2807 struct input *input =
2808 container_of(task, struct input, repeat_task);
2809 struct window *window = input->keyboard_focus;
2810 uint64_t exp;
2811
2812 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2813 /* If we change the timer between the fd becoming
2814 * readable and getting here, there'll be nothing to
2815 * read and we get EAGAIN. */
2816 return;
2817
2818 if (window && window->key_handler) {
2819 (*window->key_handler)(window, input, input->repeat_time,
2820 input->repeat_key, input->repeat_sym,
2821 WL_KEYBOARD_KEY_STATE_PRESSED,
2822 window->user_data);
2823 }
2824}
2825
2826static void
2827keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2828 uint32_t format, int fd, uint32_t size)
2829{
2830 struct input *input = data;
2831 char *map_str;
2832
2833 if (!data) {
2834 close(fd);
2835 return;
2836 }
2837
2838 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2839 close(fd);
2840 return;
2841 }
2842
2843 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2844 if (map_str == MAP_FAILED) {
2845 close(fd);
2846 return;
2847 }
2848
2849 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2850 map_str,
2851 XKB_KEYMAP_FORMAT_TEXT_V1,
2852 0);
2853 munmap(map_str, size);
2854 close(fd);
2855
2856 if (!input->xkb.keymap) {
2857 fprintf(stderr, "failed to compile keymap\n");
2858 return;
2859 }
2860
2861 input->xkb.state = xkb_state_new(input->xkb.keymap);
2862 if (!input->xkb.state) {
2863 fprintf(stderr, "failed to create XKB state\n");
2864 xkb_map_unref(input->xkb.keymap);
2865 input->xkb.keymap = NULL;
2866 return;
2867 }
2868
2869 input->xkb.control_mask =
2870 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2871 input->xkb.alt_mask =
2872 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2873 input->xkb.shift_mask =
2874 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2875}
2876
2877static void
2878keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2879 uint32_t serial, struct wl_surface *surface,
2880 struct wl_array *keys)
2881{
2882 struct input *input = data;
2883 struct window *window;
2884
2885 input->display->serial = serial;
2886 input->keyboard_focus = wl_surface_get_user_data(surface);
2887
2888 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002889 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002890 if (window->keyboard_focus_handler)
2891 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002892 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002893}
2894
2895static void
2896keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2897 uint32_t serial, struct wl_surface *surface)
2898{
2899 struct input *input = data;
2900
2901 input->display->serial = serial;
2902 input_remove_keyboard_focus(input);
2903}
2904
Scott Moreau210d0792012-03-22 10:47:01 -06002905static void
Daniel Stone37816df2012-05-16 18:45:18 +01002906keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002907 uint32_t serial, uint32_t time, uint32_t key,
2908 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002909{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002910 struct input *input = data;
2911 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002912 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002913 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002914 const xkb_keysym_t *syms;
2915 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002916 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002917
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002918 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002919 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002920 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002921 return;
2922
Daniel Stone97f68542012-05-30 16:32:01 +01002923 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002924
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002925 sym = XKB_KEY_NoSymbol;
2926 if (num_syms == 1)
2927 sym = syms[0];
2928
2929 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002930 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002931 window_set_maximized(window,
2932 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002933 } else if (sym == XKB_KEY_F11 &&
2934 window->fullscreen_handler &&
2935 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2936 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002937 } else if (sym == XKB_KEY_F4 &&
2938 input->modifiers == MOD_ALT_MASK &&
2939 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2940 if (window->close_handler)
2941 window->close_handler(window->parent,
2942 window->user_data);
2943 else
2944 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002945 } else if (window->key_handler) {
2946 (*window->key_handler)(window, input, time, key,
2947 sym, state, window->user_data);
2948 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002949
2950 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2951 key == input->repeat_key) {
2952 its.it_interval.tv_sec = 0;
2953 its.it_interval.tv_nsec = 0;
2954 its.it_value.tv_sec = 0;
2955 its.it_value.tv_nsec = 0;
2956 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2957 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2958 input->repeat_sym = sym;
2959 input->repeat_key = key;
2960 input->repeat_time = time;
2961 its.it_interval.tv_sec = 0;
2962 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2963 its.it_value.tv_sec = 0;
2964 its.it_value.tv_nsec = 400 * 1000 * 1000;
2965 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2966 }
2967}
2968
2969static void
Daniel Stone351eb612012-05-31 15:27:47 -04002970keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2971 uint32_t serial, uint32_t mods_depressed,
2972 uint32_t mods_latched, uint32_t mods_locked,
2973 uint32_t group)
2974{
2975 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002976 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002977
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002978 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2979 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002980 mask = xkb_state_serialize_mods(input->xkb.state,
2981 XKB_STATE_DEPRESSED |
2982 XKB_STATE_LATCHED);
2983 input->modifiers = 0;
2984 if (mask & input->xkb.control_mask)
2985 input->modifiers |= MOD_CONTROL_MASK;
2986 if (mask & input->xkb.alt_mask)
2987 input->modifiers |= MOD_ALT_MASK;
2988 if (mask & input->xkb.shift_mask)
2989 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002990}
2991
Daniel Stone37816df2012-05-16 18:45:18 +01002992static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002993 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002994 keyboard_handle_enter,
2995 keyboard_handle_leave,
2996 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002997 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002998};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002999
3000static void
Daniel Stone37816df2012-05-16 18:45:18 +01003001seat_handle_capabilities(void *data, struct wl_seat *seat,
3002 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003003{
Daniel Stone37816df2012-05-16 18:45:18 +01003004 struct input *input = data;
3005
3006 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3007 input->pointer = wl_seat_get_pointer(seat);
3008 wl_pointer_set_user_data(input->pointer, input);
3009 wl_pointer_add_listener(input->pointer, &pointer_listener,
3010 input);
3011 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3012 wl_pointer_destroy(input->pointer);
3013 input->pointer = NULL;
3014 }
3015
3016 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3017 input->keyboard = wl_seat_get_keyboard(seat);
3018 wl_keyboard_set_user_data(input->keyboard, input);
3019 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3020 input);
3021 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3022 wl_keyboard_destroy(input->keyboard);
3023 input->keyboard = NULL;
3024 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003025}
3026
Daniel Stone37816df2012-05-16 18:45:18 +01003027static const struct wl_seat_listener seat_listener = {
3028 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003029};
3030
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003031void
3032input_get_position(struct input *input, int32_t *x, int32_t *y)
3033{
3034 *x = input->sx;
3035 *y = input->sy;
3036}
3037
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003038struct display *
3039input_get_display(struct input *input)
3040{
3041 return input->display;
3042}
3043
Daniel Stone37816df2012-05-16 18:45:18 +01003044struct wl_seat *
3045input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003046{
Daniel Stone37816df2012-05-16 18:45:18 +01003047 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003048}
3049
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003050uint32_t
3051input_get_modifiers(struct input *input)
3052{
3053 return input->modifiers;
3054}
3055
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003056struct widget *
3057input_get_focus_widget(struct input *input)
3058{
3059 return input->focus_widget;
3060}
3061
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003062struct data_offer {
3063 struct wl_data_offer *offer;
3064 struct input *input;
3065 struct wl_array types;
3066 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003067
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003068 struct task io_task;
3069 int fd;
3070 data_func_t func;
3071 int32_t x, y;
3072 void *user_data;
3073};
3074
3075static void
3076data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3077{
3078 struct data_offer *offer = data;
3079 char **p;
3080
3081 p = wl_array_add(&offer->types, sizeof *p);
3082 *p = strdup(type);
3083}
3084
3085static const struct wl_data_offer_listener data_offer_listener = {
3086 data_offer_offer,
3087};
3088
3089static void
3090data_offer_destroy(struct data_offer *offer)
3091{
3092 char **p;
3093
3094 offer->refcount--;
3095 if (offer->refcount == 0) {
3096 wl_data_offer_destroy(offer->offer);
3097 for (p = offer->types.data; *p; p++)
3098 free(*p);
3099 wl_array_release(&offer->types);
3100 free(offer);
3101 }
3102}
3103
3104static void
3105data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003106 struct wl_data_device *data_device,
3107 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003108{
3109 struct data_offer *offer;
3110
3111 offer = malloc(sizeof *offer);
3112
3113 wl_array_init(&offer->types);
3114 offer->refcount = 1;
3115 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003116 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003117 wl_data_offer_add_listener(offer->offer,
3118 &data_offer_listener, offer);
3119}
3120
3121static void
3122data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003123 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003124 wl_fixed_t x_w, wl_fixed_t y_w,
3125 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003126{
3127 struct input *input = data;
3128 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003129 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003130 float x = wl_fixed_to_double(x_w);
3131 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003132 char **p;
3133
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003134 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003135 window = wl_surface_get_user_data(surface);
3136 input->pointer_focus = window;
3137
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003138 if (offer) {
3139 input->drag_offer = wl_data_offer_get_user_data(offer);
3140
3141 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3142 *p = NULL;
3143
3144 types_data = input->drag_offer->types.data;
3145 } else {
3146 input->drag_offer = NULL;
3147 types_data = NULL;
3148 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003149
3150 window = input->pointer_focus;
3151 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003152 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003153 window->user_data);
3154}
3155
3156static void
3157data_device_leave(void *data, struct wl_data_device *data_device)
3158{
3159 struct input *input = data;
3160
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003161 if (input->drag_offer) {
3162 data_offer_destroy(input->drag_offer);
3163 input->drag_offer = NULL;
3164 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003165}
3166
3167static void
3168data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003169 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003170{
3171 struct input *input = data;
3172 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003173 float x = wl_fixed_to_double(x_w);
3174 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003175 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003176
3177 input->sx = x;
3178 input->sy = y;
3179
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003180 if (input->drag_offer)
3181 types_data = input->drag_offer->types.data;
3182 else
3183 types_data = NULL;
3184
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003185 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003186 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003187 window->user_data);
3188}
3189
3190static void
3191data_device_drop(void *data, struct wl_data_device *data_device)
3192{
3193 struct input *input = data;
3194 struct window *window = input->pointer_focus;
3195
3196 if (window->drop_handler)
3197 window->drop_handler(window, input,
3198 input->sx, input->sy, window->user_data);
3199}
3200
3201static void
3202data_device_selection(void *data,
3203 struct wl_data_device *wl_data_device,
3204 struct wl_data_offer *offer)
3205{
3206 struct input *input = data;
3207 char **p;
3208
3209 if (input->selection_offer)
3210 data_offer_destroy(input->selection_offer);
3211
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003212 if (offer) {
3213 input->selection_offer = wl_data_offer_get_user_data(offer);
3214 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3215 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003216 } else {
3217 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003218 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003219}
3220
3221static const struct wl_data_device_listener data_device_listener = {
3222 data_device_data_offer,
3223 data_device_enter,
3224 data_device_leave,
3225 data_device_motion,
3226 data_device_drop,
3227 data_device_selection
3228};
3229
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003230static void
3231input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003232{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003233 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003234 struct wl_cursor *cursor;
3235 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003236
Daniel Stone80972742012-11-07 17:51:39 +11003237 if (!input->pointer)
3238 return;
3239
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003240 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003241 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003242 return;
3243
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003244 if (index >= (int) cursor->image_count) {
3245 fprintf(stderr, "cursor index out of range\n");
3246 return;
3247 }
3248
3249 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003250 buffer = wl_cursor_image_get_buffer(image);
3251 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003252 return;
3253
Kristian Høgsbergae277372012-08-01 09:41:08 -04003254 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003255 input->pointer_surface,
3256 image->hotspot_x, image->hotspot_y);
3257 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3258 wl_surface_damage(input->pointer_surface, 0, 0,
3259 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003260 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003261}
3262
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003263static const struct wl_callback_listener pointer_surface_listener;
3264
3265static void
3266pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3267 uint32_t time)
3268{
3269 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003270 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003271 int i;
3272
3273 if (callback) {
3274 assert(callback == input->cursor_frame_cb);
3275 wl_callback_destroy(callback);
3276 input->cursor_frame_cb = NULL;
3277 }
3278
Daniel Stone80972742012-11-07 17:51:39 +11003279 if (!input->pointer)
3280 return;
3281
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003282 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003283 wl_pointer_set_cursor(input->pointer,
3284 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003285 NULL, 0, 0);
3286 return;
3287 }
3288
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003289 if (input->current_cursor == CURSOR_UNSET)
3290 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003291 cursor = input->display->cursors[input->current_cursor];
3292 if (!cursor)
3293 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003294
3295 /* FIXME We don't have the current time on the first call so we set
3296 * the animation start to the time of the first frame callback. */
3297 if (time == 0)
3298 input->cursor_anim_start = 0;
3299 else if (input->cursor_anim_start == 0)
3300 input->cursor_anim_start = time;
3301
3302 if (time == 0 || input->cursor_anim_start == 0)
3303 i = 0;
3304 else
3305 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3306
Pekka Paalanenbc106382012-10-10 12:49:31 +03003307 if (cursor->image_count > 1) {
3308 input->cursor_frame_cb =
3309 wl_surface_frame(input->pointer_surface);
3310 wl_callback_add_listener(input->cursor_frame_cb,
3311 &pointer_surface_listener, input);
3312 }
3313
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003314 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003315}
3316
3317static const struct wl_callback_listener pointer_surface_listener = {
3318 pointer_surface_frame_callback
3319};
3320
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003321void
3322input_set_pointer_image(struct input *input, int pointer)
3323{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003324 int force = 0;
3325
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003326 if (!input->pointer)
3327 return;
3328
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003329 if (input->pointer_enter_serial > input->cursor_serial)
3330 force = 1;
3331
3332 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003333 return;
3334
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003335 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003336 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003337 if (!input->cursor_frame_cb)
3338 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003339 else if (force) {
3340 /* The current frame callback may be stuck if, for instance,
3341 * the set cursor request was processed by the server after
3342 * this client lost the focus. In this case the cursor surface
3343 * might not be mapped and the frame callback wouldn't ever
3344 * complete. Send a set_cursor and attach to try to map the
3345 * cursor surface again so that the callback will finish */
3346 input_set_pointer_image_index(input, 0);
3347 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003348}
3349
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003350struct wl_data_device *
3351input_get_data_device(struct input *input)
3352{
3353 return input->data_device;
3354}
3355
3356void
3357input_set_selection(struct input *input,
3358 struct wl_data_source *source, uint32_t time)
3359{
3360 wl_data_device_set_selection(input->data_device, source, time);
3361}
3362
3363void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003364input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003365{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003366 wl_data_offer_accept(input->drag_offer->offer,
3367 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003368}
3369
3370static void
3371offer_io_func(struct task *task, uint32_t events)
3372{
3373 struct data_offer *offer =
3374 container_of(task, struct data_offer, io_task);
3375 unsigned int len;
3376 char buffer[4096];
3377
3378 len = read(offer->fd, buffer, sizeof buffer);
3379 offer->func(buffer, len,
3380 offer->x, offer->y, offer->user_data);
3381
3382 if (len == 0) {
3383 close(offer->fd);
3384 data_offer_destroy(offer);
3385 }
3386}
3387
3388static void
3389data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3390 data_func_t func, void *user_data)
3391{
3392 int p[2];
3393
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003394 if (pipe2(p, O_CLOEXEC) == -1)
3395 return;
3396
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003397 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3398 close(p[1]);
3399
3400 offer->io_task.run = offer_io_func;
3401 offer->fd = p[0];
3402 offer->func = func;
3403 offer->refcount++;
3404 offer->user_data = user_data;
3405
3406 display_watch_fd(offer->input->display,
3407 offer->fd, EPOLLIN, &offer->io_task);
3408}
3409
3410void
3411input_receive_drag_data(struct input *input, const char *mime_type,
3412 data_func_t func, void *data)
3413{
3414 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3415 input->drag_offer->x = input->sx;
3416 input->drag_offer->y = input->sy;
3417}
3418
3419int
3420input_receive_selection_data(struct input *input, const char *mime_type,
3421 data_func_t func, void *data)
3422{
3423 char **p;
3424
3425 if (input->selection_offer == NULL)
3426 return -1;
3427
3428 for (p = input->selection_offer->types.data; *p; p++)
3429 if (strcmp(mime_type, *p) == 0)
3430 break;
3431
3432 if (*p == NULL)
3433 return -1;
3434
3435 data_offer_receive_data(input->selection_offer,
3436 mime_type, func, data);
3437 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003438}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003439
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003440int
3441input_receive_selection_data_to_fd(struct input *input,
3442 const char *mime_type, int fd)
3443{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003444 if (input->selection_offer)
3445 wl_data_offer_receive(input->selection_offer->offer,
3446 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003447
3448 return 0;
3449}
3450
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003451void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003452window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003453{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003454 if (!window->shell_surface)
3455 return;
3456
Daniel Stone37816df2012-05-16 18:45:18 +01003457 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003458}
3459
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003460static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003461surface_set_synchronized(struct surface *surface)
3462{
3463 if (!surface->subsurface)
3464 return;
3465
3466 if (surface->synchronized)
3467 return;
3468
3469 wl_subsurface_set_sync(surface->subsurface);
3470 surface->synchronized = 1;
3471}
3472
3473static void
3474surface_set_synchronized_default(struct surface *surface)
3475{
3476 if (!surface->subsurface)
3477 return;
3478
3479 if (surface->synchronized == surface->synchronized_default)
3480 return;
3481
3482 if (surface->synchronized_default)
3483 wl_subsurface_set_sync(surface->subsurface);
3484 else
3485 wl_subsurface_set_desync(surface->subsurface);
3486
3487 surface->synchronized = surface->synchronized_default;
3488}
3489
3490static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003491surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003492{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003493 struct widget *widget = surface->widget;
3494 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003495
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003496 if (surface->input_region) {
3497 wl_region_destroy(surface->input_region);
3498 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003499 }
3500
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003501 if (surface->opaque_region)
3502 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003503
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003504 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003505
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003506 if (widget->resize_handler)
3507 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003508 widget->allocation.width,
3509 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003510 widget->user_data);
3511
Pekka Paalanen35e82632013-04-25 13:57:48 +03003512 if (surface->subsurface &&
3513 (surface->allocation.x != widget->allocation.x ||
3514 surface->allocation.y != widget->allocation.y)) {
3515 wl_subsurface_set_position(surface->subsurface,
3516 widget->allocation.x,
3517 widget->allocation.y);
3518 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003519 if (surface->allocation.width != widget->allocation.width ||
3520 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003521 window_schedule_redraw(widget->window);
3522 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003523 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003524
3525 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003526 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003527 widget->allocation.width,
3528 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003529}
3530
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003531static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003532hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3533{
3534 /*
3535 * This hack should be removed, when EGL respects
3536 * eglSwapInterval(0).
3537 *
3538 * If this window has sub-surfaces, especially a free-running
3539 * EGL-widget, we need to post the parent surface once with
3540 * all the old state to guarantee, that the EGL-widget will
3541 * receive its frame callback soon. Otherwise, a forced call
3542 * to eglSwapBuffers may end up blocking, waiting for a frame
3543 * event that will never come, because we will commit the parent
3544 * surface with all new state only after eglSwapBuffers returns.
3545 *
3546 * This assumes, that:
3547 * 1. When the EGL widget's resize hook is called, it pauses.
3548 * 2. When the EGL widget's redraw hook is called, it forces a
3549 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3550 * In a single threaded application condition 1 is a no-op.
3551 *
3552 * XXX: This should actually be after the surface_resize() calls,
3553 * but cannot, because then it would commit the incomplete state
3554 * accumulated from the widget resize hooks.
3555 */
3556 if (window->subsurface_list.next != &window->main_surface->link ||
3557 window->subsurface_list.prev != &window->main_surface->link)
3558 wl_surface_commit(window->main_surface->surface);
3559}
3560
3561static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003562idle_resize(struct window *window)
3563{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003564 struct surface *surface;
3565
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003566 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003567 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003568
Pekka Paalanen71233882013-04-25 13:57:53 +03003569 DBG("from %dx%d to %dx%d\n",
3570 window->main_surface->server_allocation.width,
3571 window->main_surface->server_allocation.height,
3572 window->pending_allocation.width,
3573 window->pending_allocation.height);
3574
Pekka Paalanene9297f82013-04-25 13:57:51 +03003575 hack_prevent_EGL_sub_surface_deadlock(window);
3576
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003577 widget_set_allocation(window->main_surface->widget,
3578 window->pending_allocation.x,
3579 window->pending_allocation.y,
3580 window->pending_allocation.width,
3581 window->pending_allocation.height);
3582
3583 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003584
3585 /* The main surface is in the list, too. Main surface's
3586 * resize_handler is responsible for calling widget_set_allocation()
3587 * on all sub-surface root widgets, so they will be resized
3588 * properly.
3589 */
3590 wl_list_for_each(surface, &window->subsurface_list, link) {
3591 if (surface == window->main_surface)
3592 continue;
3593
3594 surface_set_synchronized(surface);
3595 surface_resize(surface);
3596 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003597}
3598
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003599void
3600window_schedule_resize(struct window *window, int width, int height)
3601{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003602 window->pending_allocation.x = 0;
3603 window->pending_allocation.y = 0;
3604 window->pending_allocation.width = width;
3605 window->pending_allocation.height = height;
3606
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003607 if (window->min_allocation.width == 0)
3608 window->min_allocation = window->pending_allocation;
3609 if (window->pending_allocation.width < window->min_allocation.width)
3610 window->pending_allocation.width = window->min_allocation.width;
3611 if (window->pending_allocation.height < window->min_allocation.height)
3612 window->pending_allocation.height = window->min_allocation.height;
3613
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003614 window->resize_needed = 1;
3615 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003616}
3617
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003618void
3619widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3620{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003621 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003622}
3623
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003624static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003625handle_ping(void *data, struct wl_shell_surface *shell_surface,
3626 uint32_t serial)
3627{
3628 wl_shell_surface_pong(shell_surface, serial);
3629}
3630
3631static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003632handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003633 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003634{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003635 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003636
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003637 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003638 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003639}
3640
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003641static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003642menu_destroy(struct menu *menu)
3643{
3644 widget_destroy(menu->widget);
3645 window_destroy(menu->window);
3646 free(menu);
3647}
3648
3649static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003650handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3651{
3652 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003653 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003654
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003655 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003656 * device. Or just use wl_callback. And this really needs to
3657 * be a window vfunc that the menu can set. And we need the
3658 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003659
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003660 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003661 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003662}
3663
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003664static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003665 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003666 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003667 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003668};
3669
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003670void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003671window_get_allocation(struct window *window,
3672 struct rectangle *allocation)
3673{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003674 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003675}
3676
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003677static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003678widget_redraw(struct widget *widget)
3679{
3680 struct widget *child;
3681
3682 if (widget->redraw_handler)
3683 widget->redraw_handler(widget, widget->user_data);
3684 wl_list_for_each(child, &widget->child_list, link)
3685 widget_redraw(child);
3686}
3687
3688static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003689frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3690{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003691 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003692
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003693 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003694 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003695 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003696 surface->frame_cb = NULL;
3697
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003698 surface->last_time = time;
3699
Pekka Paalanen71233882013-04-25 13:57:53 +03003700 if (surface->redraw_needed || surface->window->redraw_needed) {
3701 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003702 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003703 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003704}
3705
3706static const struct wl_callback_listener listener = {
3707 frame_callback
3708};
3709
3710static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003711surface_redraw(struct surface *surface)
3712{
Pekka Paalanen71233882013-04-25 13:57:53 +03003713 DBG_OBJ(surface->surface, "begin\n");
3714
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003715 if (!surface->window->redraw_needed && !surface->redraw_needed)
3716 return;
3717
3718 /* Whole-window redraw forces a redraw even if the previous has
3719 * not yet hit the screen.
3720 */
3721 if (surface->frame_cb) {
3722 if (!surface->window->redraw_needed)
3723 return;
3724
Pekka Paalanen71233882013-04-25 13:57:53 +03003725 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003726 wl_callback_destroy(surface->frame_cb);
3727 }
3728
3729 surface->frame_cb = wl_surface_frame(surface->surface);
3730 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003731 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003732
3733 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003734 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003735 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003736 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003737}
3738
3739static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003740idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003741{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003742 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003743 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003744
Pekka Paalanen71233882013-04-25 13:57:53 +03003745 DBG(" --------- \n");
3746
Pekka Paalaneneebff542013-04-25 13:57:52 +03003747 wl_list_init(&window->redraw_task.link);
3748 window->redraw_task_scheduled = 0;
3749
3750 if (window->resize_needed) {
3751 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003752 if (window->main_surface->frame_cb) {
3753 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003754 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003755 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003756
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003757 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003758 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003759
Pekka Paalanen35e82632013-04-25 13:57:48 +03003760 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003761 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003762
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003763 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003764 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003765
3766 wl_list_for_each(surface, &window->subsurface_list, link)
3767 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003768}
3769
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003770static void
3771window_schedule_redraw_task(struct window *window)
3772{
3773 if (window->configure_requests)
3774 return;
3775 if (!window->redraw_task_scheduled) {
3776 window->redraw_task.run = idle_redraw;
3777 display_defer(window->display, &window->redraw_task);
3778 window->redraw_task_scheduled = 1;
3779 }
3780}
3781
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003782void
3783window_schedule_redraw(struct window *window)
3784{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003785 struct surface *surface;
3786
Pekka Paalanen71233882013-04-25 13:57:53 +03003787 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3788
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003789 wl_list_for_each(surface, &window->subsurface_list, link)
3790 surface->redraw_needed = 1;
3791
3792 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003793}
3794
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003795int
3796window_is_fullscreen(struct window *window)
3797{
3798 return window->type == TYPE_FULLSCREEN;
3799}
3800
MoD063a8822013-03-02 23:43:57 +00003801static void
3802configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3803{
3804 struct window *window = data;
3805
3806 wl_callback_destroy(callback);
3807 window->configure_requests--;
3808
3809 if (!window->configure_requests)
3810 window_schedule_redraw(window);
3811}
3812
3813static struct wl_callback_listener configure_request_listener = {
3814 configure_request_completed,
3815};
3816
3817static void
3818window_defer_redraw_until_configure(struct window* window)
3819{
3820 struct wl_callback *callback;
3821
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003822 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003823 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003824 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003825 }
3826
3827 callback = wl_display_sync(window->display->display);
3828 wl_callback_add_listener(callback, &configure_request_listener, window);
3829 window->configure_requests++;
3830}
3831
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003832void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003833window_set_fullscreen(struct window *window, int fullscreen)
3834{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003835 if (!window->display->shell)
3836 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003837
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003838 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003839 return;
3840
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003841 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003842 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003843 if (window->type == TYPE_TOPLEVEL) {
3844 window->saved_allocation = window->main_surface->allocation;
3845 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003846 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003847 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003848 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003849 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003850 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003851 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003852 if (window->saved_type == TYPE_MAXIMIZED) {
3853 window_set_maximized(window, 1);
3854 } else {
3855 window->type = TYPE_TOPLEVEL;
3856 wl_shell_surface_set_toplevel(window->shell_surface);
3857 window_schedule_resize(window,
3858 window->saved_allocation.width,
3859 window->saved_allocation.height);
3860 }
3861
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003862 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003863}
3864
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003865void
3866window_set_fullscreen_method(struct window *window,
3867 enum wl_shell_surface_fullscreen_method method)
3868{
3869 window->fullscreen_method = method;
3870}
3871
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003872int
3873window_is_maximized(struct window *window)
3874{
3875 return window->type == TYPE_MAXIMIZED;
3876}
3877
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003878void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003879window_set_maximized(struct window *window, int maximized)
3880{
3881 if (!window->display->shell)
3882 return;
3883
3884 if ((window->type == TYPE_MAXIMIZED) == maximized)
3885 return;
3886
3887 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003888 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003889 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3890 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003891 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003892 } else if (window->type == TYPE_FULLSCREEN) {
3893 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3894 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003895 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003896 } else {
3897 wl_shell_surface_set_toplevel(window->shell_surface);
3898 window->type = TYPE_TOPLEVEL;
3899 window_schedule_resize(window,
3900 window->saved_allocation.width,
3901 window->saved_allocation.height);
3902 }
3903}
3904
3905void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003906window_set_user_data(struct window *window, void *data)
3907{
3908 window->user_data = data;
3909}
3910
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003911void *
3912window_get_user_data(struct window *window)
3913{
3914 return window->user_data;
3915}
3916
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003917void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003918window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003919 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003920{
3921 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003922}
3923
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003924void
3925window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003926 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003927{
3928 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003929}
3930
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003931void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003932window_set_data_handler(struct window *window, window_data_handler_t handler)
3933{
3934 window->data_handler = handler;
3935}
3936
3937void
3938window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3939{
3940 window->drop_handler = handler;
3941}
3942
3943void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003944window_set_close_handler(struct window *window,
3945 window_close_handler_t handler)
3946{
3947 window->close_handler = handler;
3948}
3949
3950void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003951window_set_fullscreen_handler(struct window *window,
3952 window_fullscreen_handler_t handler)
3953{
3954 window->fullscreen_handler = handler;
3955}
3956
3957void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003958window_set_output_handler(struct window *window,
3959 window_output_handler_t handler)
3960{
3961 window->output_handler = handler;
3962}
3963
3964void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003965window_set_title(struct window *window, const char *title)
3966{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003967 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003968 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003969 if (window->shell_surface)
3970 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003971}
3972
3973const char *
3974window_get_title(struct window *window)
3975{
3976 return window->title;
3977}
3978
3979void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003980window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3981{
3982 struct text_cursor_position *text_cursor_position =
3983 window->display->text_cursor_position;
3984
Scott Moreau9295ce02012-06-01 12:46:10 -06003985 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003986 return;
3987
3988 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003989 window->main_surface->surface,
3990 wl_fixed_from_int(x),
3991 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003992}
3993
3994void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003995window_damage(struct window *window, int32_t x, int32_t y,
3996 int32_t width, int32_t height)
3997{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003998 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003999}
4000
Casey Dahlin9074db52012-04-19 22:50:09 -04004001static void
4002surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004003 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004004{
Rob Bradford7507b572012-05-15 17:55:34 +01004005 struct window *window = data;
4006 struct output *output;
4007 struct output *output_found = NULL;
4008 struct window_output *window_output;
4009
4010 wl_list_for_each(output, &window->display->output_list, link) {
4011 if (output->output == wl_output) {
4012 output_found = output;
4013 break;
4014 }
4015 }
4016
4017 if (!output_found)
4018 return;
4019
4020 window_output = malloc (sizeof *window_output);
4021 window_output->output = output_found;
4022
4023 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004024
4025 if (window->output_handler)
4026 window->output_handler(window, output_found, 1,
4027 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004028}
4029
4030static void
4031surface_leave(void *data,
4032 struct wl_surface *wl_surface, struct wl_output *output)
4033{
Rob Bradford7507b572012-05-15 17:55:34 +01004034 struct window *window = data;
4035 struct window_output *window_output;
4036 struct window_output *window_output_found = NULL;
4037
4038 wl_list_for_each(window_output, &window->window_output_list, link) {
4039 if (window_output->output->output == output) {
4040 window_output_found = window_output;
4041 break;
4042 }
4043 }
4044
4045 if (window_output_found) {
4046 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004047
4048 if (window->output_handler)
4049 window->output_handler(window, window_output->output,
4050 0, window->user_data);
4051
Rob Bradford7507b572012-05-15 17:55:34 +01004052 free(window_output_found);
4053 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004054}
4055
4056static const struct wl_surface_listener surface_listener = {
4057 surface_enter,
4058 surface_leave
4059};
4060
Pekka Paalanen4e373742013-02-13 16:17:13 +02004061static struct surface *
4062surface_create(struct window *window)
4063{
4064 struct display *display = window->display;
4065 struct surface *surface;
4066
4067 surface = calloc(1, sizeof *surface);
4068 if (!surface)
4069 return NULL;
4070
4071 surface->window = window;
4072 surface->surface = wl_compositor_create_surface(display->compositor);
4073 wl_surface_add_listener(surface->surface, &surface_listener, window);
4074
Pekka Paalanen35e82632013-04-25 13:57:48 +03004075 wl_list_insert(&window->subsurface_list, &surface->link);
4076
Pekka Paalanen4e373742013-02-13 16:17:13 +02004077 return surface;
4078}
4079
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004080static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004081window_create_internal(struct display *display,
4082 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004083{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004084 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004085 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004086
4087 window = malloc(sizeof *window);
4088 if (window == NULL)
4089 return NULL;
4090
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004091 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004092 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004093 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004094 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004095
4096 surface = surface_create(window);
4097 window->main_surface = surface;
4098
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004099 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004100 window->shell_surface =
4101 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004102 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004103 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004104
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004105 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004106 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004107 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004108
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004109 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004110#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004111 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004112#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004113 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004114#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004115 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004116 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004117
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004118 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004119 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004120 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004121
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004122 if (window->shell_surface) {
4123 wl_shell_surface_set_user_data(window->shell_surface, window);
4124 wl_shell_surface_add_listener(window->shell_surface,
4125 &shell_surface_listener, window);
4126 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004127
Rob Bradford7507b572012-05-15 17:55:34 +01004128 wl_list_init (&window->window_output_list);
4129
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004130 return window;
4131}
4132
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004133struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004134window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004135{
4136 struct window *window;
4137
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004138 window = window_create_internal(display, NULL, TYPE_NONE);
4139 if (!window)
4140 return NULL;
4141
4142 return window;
4143}
4144
4145struct window *
4146window_create_custom(struct display *display)
4147{
4148 struct window *window;
4149
4150 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004151 if (!window)
4152 return NULL;
4153
4154 return window;
4155}
4156
4157struct window *
4158window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004159 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004160{
4161 struct window *window;
4162
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004163 window = window_create_internal(parent->display,
4164 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004165 if (!window)
4166 return NULL;
4167
4168 window->x = x;
4169 window->y = y;
4170
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004171 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004172 wl_shell_surface_set_transient(
4173 window->shell_surface,
4174 window->parent->main_surface->surface,
4175 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004176
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004177 return window;
4178}
4179
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004180static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004181menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004182{
4183 int next;
4184
4185 next = (sy - 8) / 20;
4186 if (menu->current != next) {
4187 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004188 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004189 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004190}
4191
4192static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004193menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004194 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004195 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004196{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004197 struct menu *menu = data;
4198
4199 if (widget == menu->widget)
4200 menu_set_item(data, y);
4201
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004202 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004203}
4204
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004205static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004206menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004207 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004208{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004209 struct menu *menu = data;
4210
4211 if (widget == menu->widget)
4212 menu_set_item(data, y);
4213
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004214 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004215}
4216
4217static void
4218menu_leave_handler(struct widget *widget, struct input *input, void *data)
4219{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004220 struct menu *menu = data;
4221
4222 if (widget == menu->widget)
4223 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004224}
4225
4226static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004227menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004228 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004229 uint32_t button, enum wl_pointer_button_state state,
4230 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004231
4232{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004233 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004234
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004235 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4236 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004237 /* Either relase after press-drag-release or
4238 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004239 menu->func(menu->window->parent,
4240 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004241 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004242 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004243 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004244 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004245 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004246}
4247
4248static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004249menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004250{
4251 cairo_t *cr;
4252 const int32_t r = 3, margin = 3;
4253 struct menu *menu = data;
4254 int32_t width, height, i;
4255
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004256 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004257 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4258 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4259 cairo_paint(cr);
4260
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004261 width = widget->allocation.width;
4262 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004263 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004264
4265 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004266 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4267 cairo_fill(cr);
4268
4269 for (i = 0; i < menu->count; i++) {
4270 if (i == menu->current) {
4271 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4272 cairo_rectangle(cr, margin, i * 20 + margin,
4273 width - 2 * margin, 20);
4274 cairo_fill(cr);
4275 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4276 cairo_move_to(cr, 10, i * 20 + 16);
4277 cairo_show_text(cr, menu->entries[i]);
4278 } else {
4279 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4280 cairo_move_to(cr, 10, i * 20 + 16);
4281 cairo_show_text(cr, menu->entries[i]);
4282 }
4283 }
4284
4285 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004286}
4287
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004288void
4289window_show_menu(struct display *display,
4290 struct input *input, uint32_t time, struct window *parent,
4291 int32_t x, int32_t y,
4292 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004293{
4294 struct window *window;
4295 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004296 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004297
4298 menu = malloc(sizeof *menu);
4299 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004300 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004301
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004302 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004303 if (!window) {
4304 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004305 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004306 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004307
4308 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004309 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004310 menu->entries = entries;
4311 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004312 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004313 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004314 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004315 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004316 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004317 window->type = TYPE_MENU;
4318 window->x = x;
4319 window->y = y;
4320
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004321 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004322 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004323 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004324 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004325 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004326
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004327 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004328 widget_set_enter_handler(menu->widget, menu_enter_handler);
4329 widget_set_leave_handler(menu->widget, menu_leave_handler);
4330 widget_set_motion_handler(menu->widget, menu_motion_handler);
4331 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004332
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004333 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004334 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004335}
4336
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004337void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004338window_set_buffer_type(struct window *window, enum window_buffer_type type)
4339{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004340 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004341}
4342
Pekka Paalanen35e82632013-04-25 13:57:48 +03004343struct widget *
4344window_add_subsurface(struct window *window, void *data,
4345 enum subsurface_mode default_mode)
4346{
4347 struct widget *widget;
4348 struct surface *surface;
4349 struct wl_surface *parent;
4350 struct wl_subcompositor *subcompo = window->display->subcompositor;
4351
4352 if (!subcompo)
4353 return NULL;
4354
4355 surface = surface_create(window);
4356 widget = widget_create(window, surface, data);
4357 wl_list_init(&widget->link);
4358 surface->widget = widget;
4359
4360 parent = window->main_surface->surface;
4361 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4362 surface->surface,
4363 parent);
4364 surface->synchronized = 1;
4365
4366 switch (default_mode) {
4367 case SUBSURFACE_SYNCHRONIZED:
4368 surface->synchronized_default = 1;
4369 break;
4370 case SUBSURFACE_DESYNCHRONIZED:
4371 surface->synchronized_default = 0;
4372 break;
4373 default:
4374 assert(!"bad enum subsurface_mode");
4375 }
4376
4377 return widget;
4378}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004379
4380static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004381display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004382 struct wl_output *wl_output,
4383 int x, int y,
4384 int physical_width,
4385 int physical_height,
4386 int subpixel,
4387 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004388 const char *model,
4389 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004390{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004391 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004392
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004393 output->allocation.x = x;
4394 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004395 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004396}
4397
4398static void
4399display_handle_mode(void *data,
4400 struct wl_output *wl_output,
4401 uint32_t flags,
4402 int width,
4403 int height,
4404 int refresh)
4405{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004406 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004407 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004408
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004409 if (flags & WL_OUTPUT_MODE_CURRENT) {
4410 output->allocation.width = width;
4411 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004412 if (display->output_configure_handler)
4413 (*display->output_configure_handler)(
4414 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004415 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004416}
4417
4418static const struct wl_output_listener output_listener = {
4419 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004420 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004421};
4422
4423static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004424display_add_output(struct display *d, uint32_t id)
4425{
4426 struct output *output;
4427
4428 output = malloc(sizeof *output);
4429 if (output == NULL)
4430 return;
4431
4432 memset(output, 0, sizeof *output);
4433 output->display = d;
4434 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004435 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004436 wl_list_insert(d->output_list.prev, &output->link);
4437
4438 wl_output_add_listener(output->output, &output_listener, output);
4439}
4440
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004441static void
4442output_destroy(struct output *output)
4443{
4444 if (output->destroy_handler)
4445 (*output->destroy_handler)(output, output->user_data);
4446
4447 wl_output_destroy(output->output);
4448 wl_list_remove(&output->link);
4449 free(output);
4450}
4451
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004452void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004453display_set_global_handler(struct display *display,
4454 display_global_handler_t handler)
4455{
4456 struct global *global;
4457
4458 display->global_handler = handler;
4459 if (!handler)
4460 return;
4461
4462 wl_list_for_each(global, &display->global_list, link)
4463 display->global_handler(display,
4464 global->name, global->interface,
4465 global->version, display->user_data);
4466}
4467
4468void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004469display_set_output_configure_handler(struct display *display,
4470 display_output_handler_t handler)
4471{
4472 struct output *output;
4473
4474 display->output_configure_handler = handler;
4475 if (!handler)
4476 return;
4477
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004478 wl_list_for_each(output, &display->output_list, link) {
4479 if (output->allocation.width == 0 &&
4480 output->allocation.height == 0)
4481 continue;
4482
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004483 (*display->output_configure_handler)(output,
4484 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004485 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004486}
4487
4488void
4489output_set_user_data(struct output *output, void *data)
4490{
4491 output->user_data = data;
4492}
4493
4494void *
4495output_get_user_data(struct output *output)
4496{
4497 return output->user_data;
4498}
4499
4500void
4501output_set_destroy_handler(struct output *output,
4502 display_output_handler_t handler)
4503{
4504 output->destroy_handler = handler;
4505 /* FIXME: implement this, once we have way to remove outputs */
4506}
4507
4508void
Scott Moreau4e072362012-09-29 02:03:11 -06004509output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004510{
Scott Moreau4e072362012-09-29 02:03:11 -06004511 struct rectangle allocation = output->allocation;
4512
4513 switch (output->transform) {
4514 case WL_OUTPUT_TRANSFORM_90:
4515 case WL_OUTPUT_TRANSFORM_270:
4516 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4517 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4518 /* Swap width and height */
4519 allocation.width = output->allocation.height;
4520 allocation.height = output->allocation.width;
4521 break;
4522 }
4523
4524 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004525}
4526
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004527struct wl_output *
4528output_get_wl_output(struct output *output)
4529{
4530 return output->output;
4531}
4532
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004533enum wl_output_transform
4534output_get_transform(struct output *output)
4535{
4536 return output->transform;
4537}
4538
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004539static void
Daniel Stone97f68542012-05-30 16:32:01 +01004540fini_xkb(struct input *input)
4541{
4542 xkb_state_unref(input->xkb.state);
4543 xkb_map_unref(input->xkb.keymap);
4544}
4545
4546static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004547display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004548{
4549 struct input *input;
4550
4551 input = malloc(sizeof *input);
4552 if (input == NULL)
4553 return;
4554
4555 memset(input, 0, sizeof *input);
4556 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004557 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004558 input->pointer_focus = NULL;
4559 input->keyboard_focus = NULL;
4560 wl_list_insert(d->input_list.prev, &input->link);
4561
Daniel Stone37816df2012-05-16 18:45:18 +01004562 wl_seat_add_listener(input->seat, &seat_listener, input);
4563 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004564
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004565 input->data_device =
4566 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004567 input->seat);
4568 wl_data_device_add_listener(input->data_device, &data_device_listener,
4569 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004570
4571 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004572
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004573 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4574 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004575 input->repeat_task.run = keyboard_repeat_func;
4576 display_watch_fd(d, input->repeat_timer_fd,
4577 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004578}
4579
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004580static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004581input_destroy(struct input *input)
4582{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004583 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004584 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004585
4586 if (input->drag_offer)
4587 data_offer_destroy(input->drag_offer);
4588
4589 if (input->selection_offer)
4590 data_offer_destroy(input->selection_offer);
4591
4592 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004593 fini_xkb(input);
4594
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004595 wl_surface_destroy(input->pointer_surface);
4596
Pekka Paalanene1207c72011-12-16 12:02:09 +02004597 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004598 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004599 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004600 free(input);
4601}
4602
4603static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004604init_workspace_manager(struct display *d, uint32_t id)
4605{
4606 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004607 wl_registry_bind(d->registry, id,
4608 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004609 if (d->workspace_manager != NULL)
4610 workspace_manager_add_listener(d->workspace_manager,
4611 &workspace_manager_listener,
4612 d);
4613}
4614
4615static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004616registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4617 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004618{
4619 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004620 struct global *global;
4621
4622 global = malloc(sizeof *global);
4623 global->name = id;
4624 global->interface = strdup(interface);
4625 global->version = version;
4626 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004627
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004628 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004629 d->compositor = wl_registry_bind(registry, id,
4630 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004631 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004632 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004633 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004634 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004635 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004636 d->shell = wl_registry_bind(registry,
4637 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004638 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004639 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004640 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4641 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004642 wl_registry_bind(registry, id,
4643 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004644 } else if (strcmp(interface, "text_cursor_position") == 0) {
4645 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004646 wl_registry_bind(registry, id,
4647 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004648 } else if (strcmp(interface, "workspace_manager") == 0) {
4649 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004650 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4651 d->subcompositor =
4652 wl_registry_bind(registry, id,
4653 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004654 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004655
4656 if (d->global_handler)
4657 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004658}
4659
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004660static void
4661registry_handle_global_remove(void *data, struct wl_registry *registry,
4662 uint32_t name)
4663{
4664 struct display *d = data;
4665 struct global *global;
4666 struct global *tmp;
4667
4668 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4669 if (global->name != name)
4670 continue;
4671
4672 /* XXX: Should destroy bound globals, and call
4673 * the counterpart of display::global_handler
4674 */
4675 wl_list_remove(&global->link);
4676 free(global->interface);
4677 free(global);
4678 }
4679}
4680
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004681void *
4682display_bind(struct display *display, uint32_t name,
4683 const struct wl_interface *interface, uint32_t version)
4684{
4685 return wl_registry_bind(display->registry, name, interface, version);
4686}
4687
4688static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004689 registry_handle_global,
4690 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004691};
4692
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004693#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004694static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004695init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004696{
4697 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004698 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004699
Rob Clark6396ed32012-03-11 19:48:41 -05004700#ifdef USE_CAIRO_GLESV2
4701# define GL_BIT EGL_OPENGL_ES2_BIT
4702#else
4703# define GL_BIT EGL_OPENGL_BIT
4704#endif
4705
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004706 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004707 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004708 EGL_RED_SIZE, 1,
4709 EGL_GREEN_SIZE, 1,
4710 EGL_BLUE_SIZE, 1,
4711 EGL_ALPHA_SIZE, 1,
4712 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004713 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004714 EGL_NONE
4715 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004716
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004717#ifdef USE_CAIRO_GLESV2
4718 static const EGLint context_attribs[] = {
4719 EGL_CONTEXT_CLIENT_VERSION, 2,
4720 EGL_NONE
4721 };
4722 EGLint api = EGL_OPENGL_ES_API;
4723#else
4724 EGLint *context_attribs = NULL;
4725 EGLint api = EGL_OPENGL_API;
4726#endif
4727
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004728 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004729 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004730 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004731 return -1;
4732 }
4733
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004734 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004735 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004736 return -1;
4737 }
4738
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004739 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4740 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004741 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004742 return -1;
4743 }
4744
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004745 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004746 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004747 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004748 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004749 return -1;
4750 }
4751
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004752 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004753 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004754 return -1;
4755 }
4756
Benjamin Franzke0c991632011-09-27 21:57:31 +02004757 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4758 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004759 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004760 return -1;
4761 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004762
4763 return 0;
4764}
4765
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004766static void
4767fini_egl(struct display *display)
4768{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004769 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004770
4771 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4772 EGL_NO_CONTEXT);
4773
4774 eglTerminate(display->dpy);
4775 eglReleaseThread();
4776}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004777#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004778
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004779static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004780init_dummy_surface(struct display *display)
4781{
4782 int len;
4783 void *data;
4784
4785 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4786 data = malloc(len);
4787 display->dummy_surface =
4788 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4789 1, 1, len);
4790 display->dummy_surface_data = data;
4791}
4792
4793static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004794handle_display_data(struct task *task, uint32_t events)
4795{
4796 struct display *display =
4797 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004798 struct epoll_event ep;
4799 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004800
4801 display->display_fd_events = events;
4802
4803 if (events & EPOLLERR || events & EPOLLHUP) {
4804 display_exit(display);
4805 return;
4806 }
4807
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004808 if (events & EPOLLIN) {
4809 ret = wl_display_dispatch(display->display);
4810 if (ret == -1) {
4811 display_exit(display);
4812 return;
4813 }
4814 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004815
4816 if (events & EPOLLOUT) {
4817 ret = wl_display_flush(display->display);
4818 if (ret == 0) {
4819 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4820 ep.data.ptr = &display->display_task;
4821 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4822 display->display_fd, &ep);
4823 } else if (ret == -1 && errno != EAGAIN) {
4824 display_exit(display);
4825 return;
4826 }
4827 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004828}
4829
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004830static void
4831log_handler(const char *format, va_list args)
4832{
4833 vfprintf(stderr, format, args);
4834}
4835
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004836struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004837display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004838{
4839 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004840
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004841 wl_log_set_handler_client(log_handler);
4842
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004843 d = malloc(sizeof *d);
4844 if (d == NULL)
4845 return NULL;
4846
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004847 memset(d, 0, sizeof *d);
4848
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004849 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004850 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004851 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004852 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004853 return NULL;
4854 }
4855
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004856 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004857 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004858 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004859 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4860 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004861
4862 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004863 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004864 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004865 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004866
Daniel Stone97f68542012-05-30 16:32:01 +01004867 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004868 if (d->xkb_context == NULL) {
4869 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004870 return NULL;
4871 }
4872
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004873 d->workspace = 0;
4874 d->workspace_count = 1;
4875
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004876 d->registry = wl_display_get_registry(d->display);
4877 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004878
4879 if (wl_display_dispatch(d->display) < 0) {
4880 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4881 return NULL;
4882 }
4883
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004884#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004885 if (init_egl(d) < 0)
4886 fprintf(stderr, "EGL does not seem to work, "
4887 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004888#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004889
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004890 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004891
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004892 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004893
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004894 wl_list_init(&d->window_list);
4895
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004896 init_dummy_surface(d);
4897
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004898 return d;
4899}
4900
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004901static void
4902display_destroy_outputs(struct display *display)
4903{
4904 struct output *tmp;
4905 struct output *output;
4906
4907 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4908 output_destroy(output);
4909}
4910
Pekka Paalanene1207c72011-12-16 12:02:09 +02004911static void
4912display_destroy_inputs(struct display *display)
4913{
4914 struct input *tmp;
4915 struct input *input;
4916
4917 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4918 input_destroy(input);
4919}
4920
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004921void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004922display_destroy(struct display *display)
4923{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004924 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004925 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4926 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004927
4928 if (!wl_list_empty(&display->deferred_list))
4929 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4930
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004931 cairo_surface_destroy(display->dummy_surface);
4932 free(display->dummy_surface_data);
4933
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004934 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004935 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004936
Daniel Stone97f68542012-05-30 16:32:01 +01004937 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004938
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004939 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004940 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004941
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004942#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004943 if (display->argb_device)
4944 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004945#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004946
Pekka Paalanen35e82632013-04-25 13:57:48 +03004947 if (display->subcompositor)
4948 wl_subcompositor_destroy(display->subcompositor);
4949
Pekka Paalanenc2052982011-12-16 11:41:32 +02004950 if (display->shell)
4951 wl_shell_destroy(display->shell);
4952
4953 if (display->shm)
4954 wl_shm_destroy(display->shm);
4955
4956 if (display->data_device_manager)
4957 wl_data_device_manager_destroy(display->data_device_manager);
4958
4959 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004960 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004961
4962 close(display->epoll_fd);
4963
U. Artie Eoff44874d92012-10-02 21:12:35 -07004964 if (!(display->display_fd_events & EPOLLERR) &&
4965 !(display->display_fd_events & EPOLLHUP))
4966 wl_display_flush(display->display);
4967
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004968 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004969 free(display);
4970}
4971
4972void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004973display_set_user_data(struct display *display, void *data)
4974{
4975 display->user_data = data;
4976}
4977
4978void *
4979display_get_user_data(struct display *display)
4980{
4981 return display->user_data;
4982}
4983
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004984struct wl_display *
4985display_get_display(struct display *display)
4986{
4987 return display->display;
4988}
4989
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004990struct output *
4991display_get_output(struct display *display)
4992{
4993 return container_of(display->output_list.next, struct output, link);
4994}
4995
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004996struct wl_compositor *
4997display_get_compositor(struct display *display)
4998{
4999 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005000}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005001
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005002uint32_t
5003display_get_serial(struct display *display)
5004{
5005 return display->serial;
5006}
5007
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005008EGLDisplay
5009display_get_egl_display(struct display *d)
5010{
5011 return d->dpy;
5012}
5013
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005014struct wl_data_source *
5015display_create_data_source(struct display *display)
5016{
5017 return wl_data_device_manager_create_data_source(display->data_device_manager);
5018}
5019
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005020EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005021display_get_argb_egl_config(struct display *d)
5022{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005023 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005024}
5025
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005026struct wl_shell *
5027display_get_shell(struct display *display)
5028{
5029 return display->shell;
5030}
5031
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005032int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005033display_acquire_window_surface(struct display *display,
5034 struct window *window,
5035 EGLContext ctx)
5036{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005037 struct surface *surface = window->main_surface;
5038
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005039 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005040 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005041
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005042 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005043 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005044}
5045
5046void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005047display_release_window_surface(struct display *display,
5048 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005049{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005050 struct surface *surface = window->main_surface;
5051
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005052 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005053 return;
5054
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005055 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005056}
5057
5058void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005059display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005060{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005061 wl_list_insert(&display->deferred_list, &task->link);
5062}
5063
5064void
5065display_watch_fd(struct display *display,
5066 int fd, uint32_t events, struct task *task)
5067{
5068 struct epoll_event ep;
5069
5070 ep.events = events;
5071 ep.data.ptr = task;
5072 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5073}
5074
5075void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005076display_unwatch_fd(struct display *display, int fd)
5077{
5078 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5079}
5080
5081void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005082display_run(struct display *display)
5083{
5084 struct task *task;
5085 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005086 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005087
Pekka Paalanen826d7952011-12-15 10:14:07 +02005088 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005089 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005090 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005091 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005092 struct task, link);
5093 wl_list_remove(&task->link);
5094 task->run(task, 0);
5095 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005096
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005097 wl_display_dispatch_pending(display->display);
5098
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005099 if (!display->running)
5100 break;
5101
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005102 ret = wl_display_flush(display->display);
5103 if (ret < 0 && errno == EAGAIN) {
5104 ep[0].events =
5105 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5106 ep[0].data.ptr = &display->display_task;
5107
5108 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5109 display->display_fd, &ep[0]);
5110 } else if (ret < 0) {
5111 break;
5112 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005113
5114 count = epoll_wait(display->epoll_fd,
5115 ep, ARRAY_LENGTH(ep), -1);
5116 for (i = 0; i < count; i++) {
5117 task = ep[i].data.ptr;
5118 task->run(task, ep[i].events);
5119 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005120 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005121}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005122
5123void
5124display_exit(struct display *display)
5125{
5126 display->running = 0;
5127}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005128
5129void
5130keysym_modifiers_add(struct wl_array *modifiers_map,
5131 const char *name)
5132{
5133 size_t len = strlen(name) + 1;
5134 char *p;
5135
5136 p = wl_array_add(modifiers_map, len);
5137
5138 if (p == NULL)
5139 return;
5140
5141 strncpy(p, name, len);
5142}
5143
5144static xkb_mod_index_t
5145keysym_modifiers_get_index(struct wl_array *modifiers_map,
5146 const char *name)
5147{
5148 xkb_mod_index_t index = 0;
5149 char *p = modifiers_map->data;
5150
5151 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5152 if (strcmp(p, name) == 0)
5153 return index;
5154
5155 index++;
5156 p += strlen(p) + 1;
5157 }
5158
5159 return XKB_MOD_INVALID;
5160}
5161
5162xkb_mod_mask_t
5163keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5164 const char *name)
5165{
5166 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5167
5168 if (index == XKB_MOD_INVALID)
5169 return XKB_MOD_INVALID;
5170
5171 return 1 << index;
5172}