blob: 60b78a5c27971096525b12f7c1c413c250c92a9b [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.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200155 * width,height are the new buffer 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;
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200211 uint32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200212
213 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300214
215 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200216};
217
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500218struct window {
219 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500220 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100221 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500222 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200223 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400224 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500225 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500226 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400227 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400228 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300229 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400230 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400231 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100232 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400233 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400234 int focus_count;
235
Pekka Paalanen99436862012-11-19 17:15:59 +0200236 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200237 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000238 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400239
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500240 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500241 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400242 window_data_handler_t data_handler;
243 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500244 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400245 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200246 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400247
Pekka Paalanen4e373742013-02-13 16:17:13 +0200248 struct surface *main_surface;
249 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300250
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200251 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500252
Pekka Paalanen35e82632013-04-25 13:57:48 +0300253 /* struct surface::link, contains also main_surface */
254 struct wl_list subsurface_list;
255
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500256 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400257 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500258};
259
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500260struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500261 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200262 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300263 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500264 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400265 struct wl_list link;
266 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500267 widget_resize_handler_t resize_handler;
268 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500269 widget_enter_handler_t enter_handler;
270 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500271 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500272 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200273 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400274 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500275 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300276 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500277 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400278};
279
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400280struct input {
281 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100282 struct wl_seat *seat;
283 struct wl_pointer *pointer;
284 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400285 struct window *pointer_focus;
286 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300287 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300288 uint32_t cursor_anim_start;
289 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300290 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400291 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400292 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400293 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400294 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400295 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400296
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500297 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500298 struct widget *grab;
299 uint32_t grab_button;
300
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400301 struct wl_data_device *data_device;
302 struct data_offer *drag_offer;
303 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100304
305 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100306 struct xkb_keymap *keymap;
307 struct xkb_state *state;
308 xkb_mod_mask_t control_mask;
309 xkb_mod_mask_t alt_mask;
310 xkb_mod_mask_t shift_mask;
311 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400312
313 struct task repeat_task;
314 int repeat_timer_fd;
315 uint32_t repeat_sym;
316 uint32_t repeat_key;
317 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400318};
319
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500320struct output {
321 struct display *display;
322 struct wl_output *output;
323 struct rectangle allocation;
324 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600325 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200326 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200327
328 display_output_handler_t destroy_handler;
329 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500330};
331
Martin Minarik1998b152012-05-10 02:04:35 +0200332enum frame_button_action {
333 FRAME_BUTTON_NULL = 0,
334 FRAME_BUTTON_ICON = 1,
335 FRAME_BUTTON_CLOSE = 2,
336 FRAME_BUTTON_MINIMIZE = 3,
337 FRAME_BUTTON_MAXIMIZE = 4,
338};
339
340enum frame_button_pointer {
341 FRAME_BUTTON_DEFAULT = 0,
342 FRAME_BUTTON_OVER = 1,
343 FRAME_BUTTON_ACTIVE = 2,
344};
345
346enum frame_button_align {
347 FRAME_BUTTON_RIGHT = 0,
348 FRAME_BUTTON_LEFT = 1,
349};
350
351enum frame_button_decoration {
352 FRAME_BUTTON_NONE = 0,
353 FRAME_BUTTON_FANCY = 1,
354};
355
356struct frame_button {
357 struct widget *widget;
358 struct frame *frame;
359 cairo_surface_t *icon;
360 enum frame_button_action type;
361 enum frame_button_pointer state;
362 struct wl_list link; /* buttons_list */
363 enum frame_button_align align;
364 enum frame_button_decoration decoration;
365};
366
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500367struct frame {
368 struct widget *widget;
369 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200370 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500371};
372
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500373struct menu {
374 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500375 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500376 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500377 const char **entries;
378 uint32_t time;
379 int current;
380 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400381 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500382 menu_func_t func;
383};
384
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300385struct tooltip {
386 struct widget *parent;
387 struct window *window;
388 struct widget *widget;
389 char *entry;
390 struct task tooltip_task;
391 int tooltip_fd;
392 float x, y;
393};
394
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700395struct shm_pool {
396 struct wl_shm_pool *pool;
397 size_t size;
398 size_t used;
399 void *data;
400};
401
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400402enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300403 CURSOR_DEFAULT = 100,
404 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400405};
406
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500407enum window_location {
408 WINDOW_INTERIOR = 0,
409 WINDOW_RESIZING_TOP = 1,
410 WINDOW_RESIZING_BOTTOM = 2,
411 WINDOW_RESIZING_LEFT = 4,
412 WINDOW_RESIZING_TOP_LEFT = 5,
413 WINDOW_RESIZING_BOTTOM_LEFT = 6,
414 WINDOW_RESIZING_RIGHT = 8,
415 WINDOW_RESIZING_TOP_RIGHT = 9,
416 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
417 WINDOW_RESIZING_MASK = 15,
418 WINDOW_EXTERIOR = 16,
419 WINDOW_TITLEBAR = 17,
420 WINDOW_CLIENT_AREA = 18,
421};
422
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200423static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424
Pekka Paalanen97777442013-05-22 10:20:05 +0300425/* #define DEBUG */
426
427#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300428
429static void
430debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
431__attribute__ ((format (printf, 4, 5)));
432
433static void
434debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
435{
436 va_list ap;
437 struct timeval tv;
438
439 gettimeofday(&tv, NULL);
440 fprintf(stderr, "%8ld.%03ld ",
441 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
442
443 if (proxy)
444 fprintf(stderr, "%s@%d ",
445 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
446
447 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
448 fprintf(stderr, "%s ", func);
449
450 va_start(ap, fmt);
451 vfprintf(stderr, fmt, ap);
452 va_end(ap);
453}
454
455#define DBG(fmt, ...) \
456 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
457
458#define DBG_OBJ(obj, fmt, ...) \
459 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
460
461#else
462
463#define DBG(...) do {} while (0)
464#define DBG_OBJ(...) do {} while (0)
465
466#endif
467
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500468#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200470struct egl_window_surface {
471 struct toysurface base;
472 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100473 struct display *display;
474 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200475 struct wl_egl_window *egl_window;
476 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100477};
478
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200479static struct egl_window_surface *
480to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100481{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200482 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100483}
484
485static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200486egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200487 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100488{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200489 struct egl_window_surface *surface = to_egl_window_surface(base);
490
491 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
492 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
493
494 return cairo_surface_reference(surface->cairo_surface);
495}
496
497static void
498egl_window_surface_swap(struct toysurface *base,
499 struct rectangle *server_allocation)
500{
501 struct egl_window_surface *surface = to_egl_window_surface(base);
502
503 cairo_gl_surface_swapbuffers(surface->cairo_surface);
504 wl_egl_window_get_attached_size(surface->egl_window,
505 &server_allocation->width,
506 &server_allocation->height);
507}
508
509static int
510egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
511{
512 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200513 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100514
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515 device = cairo_surface_get_device(surface->cairo_surface);
516 if (!device)
517 return -1;
518
519 if (!ctx) {
520 if (device == surface->display->argb_device)
521 ctx = surface->display->argb_ctx;
522 else
523 assert(0);
524 }
525
526 cairo_device_flush(device);
527 cairo_device_acquire(device);
528 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
529 surface->egl_surface, ctx))
530 fprintf(stderr, "failed to make surface current\n");
531
532 return 0;
533}
534
535static void
536egl_window_surface_release(struct toysurface *base)
537{
538 struct egl_window_surface *surface = to_egl_window_surface(base);
539 cairo_device_t *device;
540
541 device = cairo_surface_get_device(surface->cairo_surface);
542 if (!device)
543 return;
544
545 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
546 surface->display->argb_ctx))
547 fprintf(stderr, "failed to make context current\n");
548
549 cairo_device_release(device);
550}
551
552static void
553egl_window_surface_destroy(struct toysurface *base)
554{
555 struct egl_window_surface *surface = to_egl_window_surface(base);
556 struct display *d = surface->display;
557
558 cairo_surface_destroy(surface->cairo_surface);
559 eglDestroySurface(d->dpy, surface->egl_surface);
560 wl_egl_window_destroy(surface->egl_window);
561 surface->surface = NULL;
562
563 free(surface);
564}
565
566static struct toysurface *
567egl_window_surface_create(struct display *display,
568 struct wl_surface *wl_surface,
569 uint32_t flags,
570 struct rectangle *rectangle)
571{
572 struct egl_window_surface *surface;
573
Pekka Paalanenb3627362012-11-19 17:16:00 +0200574 if (display->dpy == EGL_NO_DISPLAY)
575 return NULL;
576
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200577 surface = calloc(1, sizeof *surface);
578 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100579 return NULL;
580
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200581 surface->base.prepare = egl_window_surface_prepare;
582 surface->base.swap = egl_window_surface_swap;
583 surface->base.acquire = egl_window_surface_acquire;
584 surface->base.release = egl_window_surface_release;
585 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100586
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200587 surface->display = display;
588 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400589
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200590 surface->egl_window = wl_egl_window_create(surface->surface,
591 rectangle->width,
592 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100593
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200594 surface->egl_surface = eglCreateWindowSurface(display->dpy,
595 display->argb_config,
596 surface->egl_window,
597 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100598
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200599 surface->cairo_surface =
600 cairo_gl_surface_create_for_egl(display->argb_device,
601 surface->egl_surface,
602 rectangle->width,
603 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100604
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200605 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100606}
607
Pekka Paalanenb3627362012-11-19 17:16:00 +0200608#else
609
610static struct toysurface *
611egl_window_surface_create(struct display *display,
612 struct wl_surface *wl_surface,
613 uint32_t flags,
614 struct rectangle *rectangle)
615{
616 return NULL;
617}
618
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400619#endif
620
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200621struct shm_surface_data {
622 struct wl_buffer *buffer;
623 struct shm_pool *pool;
624};
625
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626struct wl_buffer *
627display_get_buffer_for_surface(struct display *display,
628 cairo_surface_t *surface)
629{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200630 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200632 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633
634 return data->buffer;
635}
636
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500637static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700638shm_pool_destroy(struct shm_pool *pool);
639
640static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400641shm_surface_data_destroy(void *p)
642{
643 struct shm_surface_data *data = p;
644
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200645 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700646 if (data->pool)
647 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300648
649 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650}
651
Kristian Høgsberg16626282012-04-03 11:21:27 -0400652static struct wl_shm_pool *
653make_shm_pool(struct display *display, int size, void **data)
654{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400655 struct wl_shm_pool *pool;
656 int fd;
657
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300658 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400659 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300660 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
661 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400662 return NULL;
663 }
664
665 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400666 if (*data == MAP_FAILED) {
667 fprintf(stderr, "mmap failed: %m\n");
668 close(fd);
669 return NULL;
670 }
671
672 pool = wl_shm_create_pool(display->shm, fd, size);
673
674 close(fd);
675
676 return pool;
677}
678
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700679static struct shm_pool *
680shm_pool_create(struct display *display, size_t size)
681{
682 struct shm_pool *pool = malloc(sizeof *pool);
683
684 if (!pool)
685 return NULL;
686
687 pool->pool = make_shm_pool(display, size, &pool->data);
688 if (!pool->pool) {
689 free(pool);
690 return NULL;
691 }
692
693 pool->size = size;
694 pool->used = 0;
695
696 return pool;
697}
698
699static void *
700shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
701{
702 if (pool->used + size > pool->size)
703 return NULL;
704
705 *offset = pool->used;
706 pool->used += size;
707
708 return (char *) pool->data + *offset;
709}
710
711/* destroy the pool. this does not unmap the memory though */
712static void
713shm_pool_destroy(struct shm_pool *pool)
714{
715 munmap(pool->data, pool->size);
716 wl_shm_pool_destroy(pool->pool);
717 free(pool);
718}
719
720/* Start allocating from the beginning of the pool again */
721static void
722shm_pool_reset(struct shm_pool *pool)
723{
724 pool->used = 0;
725}
726
727static int
728data_length_for_shm_surface(struct rectangle *rect)
729{
730 int stride;
731
732 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
733 rect->width);
734 return stride * rect->height;
735}
736
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500737static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700738display_create_shm_surface_from_pool(struct display *display,
739 struct rectangle *rectangle,
740 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400741{
742 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400743 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400744 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700745 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400746 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400747
748 data = malloc(sizeof *data);
749 if (data == NULL)
750 return NULL;
751
752 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
753 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700754 length = stride * rectangle->height;
755 data->pool = NULL;
756 map = shm_pool_allocate(pool, length, &offset);
757
758 if (!map) {
759 free(data);
760 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400761 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400762
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400763 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400764 CAIRO_FORMAT_ARGB32,
765 rectangle->width,
766 rectangle->height,
767 stride);
768
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200769 cairo_surface_set_user_data(surface, &shm_surface_data_key,
770 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400771
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400772 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500773 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400774 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500775 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400776
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200777 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
778 rectangle->width,
779 rectangle->height,
780 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400781
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700782 return surface;
783}
784
785static cairo_surface_t *
786display_create_shm_surface(struct display *display,
787 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200788 struct shm_pool *alternate_pool,
789 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700790{
791 struct shm_surface_data *data;
792 struct shm_pool *pool;
793 cairo_surface_t *surface;
794
Pekka Paalanen99436862012-11-19 17:15:59 +0200795 if (alternate_pool) {
796 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700797 surface = display_create_shm_surface_from_pool(display,
798 rectangle,
799 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200800 alternate_pool);
801 if (surface) {
802 data = cairo_surface_get_user_data(surface,
803 &shm_surface_data_key);
804 goto out;
805 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700806 }
807
808 pool = shm_pool_create(display,
809 data_length_for_shm_surface(rectangle));
810 if (!pool)
811 return NULL;
812
813 surface =
814 display_create_shm_surface_from_pool(display, rectangle,
815 flags, pool);
816
817 if (!surface) {
818 shm_pool_destroy(pool);
819 return NULL;
820 }
821
822 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200823 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700824 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400825
Pekka Paalanen99436862012-11-19 17:15:59 +0200826out:
827 if (data_ret)
828 *data_ret = data;
829
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830 return surface;
831}
832
nobled7b87cb02011-02-01 18:51:47 +0000833static int
834check_size(struct rectangle *rect)
835{
836 if (rect->width && rect->height)
837 return 0;
838
839 fprintf(stderr, "tried to create surface of "
840 "width: %d, height: %d\n", rect->width, rect->height);
841 return -1;
842}
843
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400844cairo_surface_t *
845display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400847 struct rectangle *rectangle,
848 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400849{
nobled7b87cb02011-02-01 18:51:47 +0000850 if (check_size(rectangle) < 0)
851 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200852
853 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200854 return display_create_shm_surface(display, rectangle, flags,
855 NULL, NULL);
856}
857
Pekka Paalanena4eda732012-11-19 17:16:02 +0200858struct shm_surface_leaf {
859 cairo_surface_t *cairo_surface;
860 /* 'data' is automatically destroyed, when 'cairo_surface' is */
861 struct shm_surface_data *data;
862
863 struct shm_pool *resize_pool;
864 int busy;
865};
866
867static void
868shm_surface_leaf_release(struct shm_surface_leaf *leaf)
869{
870 if (leaf->cairo_surface)
871 cairo_surface_destroy(leaf->cairo_surface);
872 /* leaf->data already destroyed via cairo private */
873
874 if (leaf->resize_pool)
875 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200876
877 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200878}
879
Pekka Paalanenaef02542013-04-25 13:57:47 +0300880#define MAX_LEAVES 3
881
Pekka Paalanen99436862012-11-19 17:15:59 +0200882struct shm_surface {
883 struct toysurface base;
884 struct display *display;
885 struct wl_surface *surface;
886 uint32_t flags;
887 int dx, dy;
888
Pekka Paalanenaef02542013-04-25 13:57:47 +0300889 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200890 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200891};
892
893static struct shm_surface *
894to_shm_surface(struct toysurface *base)
895{
896 return container_of(base, struct shm_surface, base);
897}
898
Pekka Paalanena4eda732012-11-19 17:16:02 +0200899static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300900shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
901{
902#ifdef DEBUG
903 struct shm_surface_leaf *leaf;
904 char bufs[MAX_LEAVES + 1];
905 int i;
906
907 for (i = 0; i < MAX_LEAVES; i++) {
908 leaf = &surface->leaf[i];
909
910 if (leaf->busy)
911 bufs[i] = 'b';
912 else if (leaf->cairo_surface)
913 bufs[i] = 'a';
914 else
915 bufs[i] = ' ';
916 }
917
918 bufs[MAX_LEAVES] = '\0';
919 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
920#endif
921}
922
923static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200924shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
925{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200926 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300927 struct shm_surface_leaf *leaf;
928 int i;
929 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300930
931 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200932
Pekka Paalanenaef02542013-04-25 13:57:47 +0300933 for (i = 0; i < MAX_LEAVES; i++) {
934 leaf = &surface->leaf[i];
935 if (leaf->data && leaf->data->buffer == buffer) {
936 leaf->busy = 0;
937 break;
938 }
939 }
940 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200941
Pekka Paalanenaef02542013-04-25 13:57:47 +0300942 /* Leave one free leaf with storage, release others */
943 free_found = 0;
944 for (i = 0; i < MAX_LEAVES; i++) {
945 leaf = &surface->leaf[i];
946
947 if (!leaf->cairo_surface || leaf->busy)
948 continue;
949
950 if (!free_found)
951 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300952 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300953 shm_surface_leaf_release(leaf);
954 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300955
Pekka Paalanen97777442013-05-22 10:20:05 +0300956 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200957}
958
959static const struct wl_buffer_listener shm_surface_buffer_listener = {
960 shm_surface_buffer_release
961};
962
Pekka Paalanen99436862012-11-19 17:15:59 +0200963static cairo_surface_t *
964shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200965 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200966{
Pekka Paalanenec076692012-11-30 13:37:27 +0200967 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200968 struct shm_surface *surface = to_shm_surface(base);
969 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanenaef02542013-04-25 13:57:47 +0300970 struct shm_surface_leaf *leaf = NULL;
971 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200972
973 surface->dx = dx;
974 surface->dy = dy;
975
Pekka Paalanenaef02542013-04-25 13:57:47 +0300976 /* pick a free buffer, preferrably one that already has storage */
977 for (i = 0; i < MAX_LEAVES; i++) {
978 if (surface->leaf[i].busy)
979 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200980
Pekka Paalanenaef02542013-04-25 13:57:47 +0300981 if (!leaf || surface->leaf[i].cairo_surface)
982 leaf = &surface->leaf[i];
983 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300984 DBG_OBJ(surface->surface, "pick leaf %d\n",
985 (int)(leaf - &surface->leaf[0]));
986
Pekka Paalanenaef02542013-04-25 13:57:47 +0300987 if (!leaf) {
988 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +0200989 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +0300990 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200991 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200992 }
993
Pekka Paalanena4eda732012-11-19 17:16:02 +0200994 if (!resize_hint && leaf->resize_pool) {
995 cairo_surface_destroy(leaf->cairo_surface);
996 leaf->cairo_surface = NULL;
997 shm_pool_destroy(leaf->resize_pool);
998 leaf->resize_pool = NULL;
999 }
1000
1001 if (leaf->cairo_surface &&
1002 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1003 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001004 goto out;
1005
Pekka Paalanena4eda732012-11-19 17:16:02 +02001006 if (leaf->cairo_surface)
1007 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001008
Pekka Paalanena4eda732012-11-19 17:16:02 +02001009 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001010 /* Create a big pool to allocate from, while continuously
1011 * resizing. Mmapping a new pool in the server
1012 * is relatively expensive, so reusing a pool performs
1013 * better, but may temporarily reserve unneeded memory.
1014 */
1015 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001016 leaf->resize_pool = shm_pool_create(surface->display,
1017 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001018 }
1019
Pekka Paalanena4eda732012-11-19 17:16:02 +02001020 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001021 display_create_shm_surface(surface->display, &rect,
1022 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001023 leaf->resize_pool,
1024 &leaf->data);
1025 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001026 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001027
1028out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001029 surface->current = leaf;
1030
1031 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001032}
1033
1034static void
1035shm_surface_swap(struct toysurface *base,
1036 struct rectangle *server_allocation)
1037{
1038 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001039 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001040
1041 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001042 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001043 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001045
Pekka Paalanena4eda732012-11-19 17:16:02 +02001046 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001047 surface->dx, surface->dy);
1048 wl_surface_damage(surface->surface, 0, 0,
1049 server_allocation->width, server_allocation->height);
1050 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001051
Pekka Paalanen71233882013-04-25 13:57:53 +03001052 DBG_OBJ(surface->surface, "leaf %d busy\n",
1053 (int)(leaf - &surface->leaf[0]));
1054
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055 leaf->busy = 1;
1056 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001057}
1058
1059static int
1060shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1061{
1062 return -1;
1063}
1064
1065static void
1066shm_surface_release(struct toysurface *base)
1067{
1068}
1069
1070static void
1071shm_surface_destroy(struct toysurface *base)
1072{
1073 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001074 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001075
Pekka Paalanenaef02542013-04-25 13:57:47 +03001076 for (i = 0; i < MAX_LEAVES; i++)
1077 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001078
1079 free(surface);
1080}
1081
1082static struct toysurface *
1083shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1084 uint32_t flags, struct rectangle *rectangle)
1085{
1086 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001087 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001088
1089 surface = calloc(1, sizeof *surface);
1090 if (!surface)
1091 return NULL;
1092
1093 surface->base.prepare = shm_surface_prepare;
1094 surface->base.swap = shm_surface_swap;
1095 surface->base.acquire = shm_surface_acquire;
1096 surface->base.release = shm_surface_release;
1097 surface->base.destroy = shm_surface_destroy;
1098
1099 surface->display = display;
1100 surface->surface = wl_surface;
1101 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001102
1103 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001104}
1105
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001106/*
1107 * The following correspondences between file names and cursors was copied
1108 * from: https://bugs.kde.org/attachment.cgi?id=67313
1109 */
1110
1111static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001112 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001113 "sw-resize",
1114 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001115};
1116
1117static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001118 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001119 "se-resize",
1120 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001121};
1122
1123static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001124 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001125 "s-resize",
1126 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001127};
1128
1129static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001130 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001131 "closedhand",
1132 "208530c400c041818281048008011002"
1133};
1134
1135static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001136 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001137 "default",
1138 "top_left_arrow",
1139 "left-arrow"
1140};
1141
1142static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001143 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001144 "w-resize",
1145 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001146};
1147
1148static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001149 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001150 "e-resize",
1151 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001152};
1153
1154static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001155 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001156 "nw-resize",
1157 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001158};
1159
1160static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001161 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001162 "ne-resize",
1163 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001164};
1165
1166static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001167 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001168 "n-resize",
1169 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001170};
1171
1172static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001173 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001174 "ibeam",
1175 "text"
1176};
1177
1178static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001179 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001180 "pointer",
1181 "pointing_hand",
1182 "e29285e634086352946a0e7090d73106"
1183};
1184
1185static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001186 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001187 "wait",
1188 "0426c94ea35c87780ff01dc239897213"
1189};
1190
1191struct cursor_alternatives {
1192 const char **names;
1193 size_t count;
1194};
1195
1196static const struct cursor_alternatives cursors[] = {
1197 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1198 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1199 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1200 {grabbings, ARRAY_LENGTH(grabbings)},
1201 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1202 {left_sides, ARRAY_LENGTH(left_sides)},
1203 {right_sides, ARRAY_LENGTH(right_sides)},
1204 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1205 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1206 {top_sides, ARRAY_LENGTH(top_sides)},
1207 {xterms, ARRAY_LENGTH(xterms)},
1208 {hand1s, ARRAY_LENGTH(hand1s)},
1209 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001210};
1211
1212static void
1213create_cursors(struct display *display)
1214{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001215 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001216 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001217 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001218 unsigned int i, j;
1219 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001220 struct config_key shell_keys[] = {
1221 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001222 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001223 };
1224 struct config_section cs[] = {
1225 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1226 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227
Ossama Othmana50e6e42013-05-14 09:48:26 -07001228 config_fd = open_config_file("weston.ini");
1229 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1230 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001231
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001232 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 display->cursors =
1234 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1235
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001236 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001237 cursor = NULL;
1238 for (j = 0; !cursor && j < cursors[i].count; ++j)
1239 cursor = wl_cursor_theme_get_cursor(
1240 display->cursor_theme, cursors[i].names[j]);
1241
1242 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001243 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001244 cursors[i].names[0]);
1245
1246 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001247 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001248}
1249
1250static void
1251destroy_cursors(struct display *display)
1252{
1253 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001254 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001255}
1256
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001257struct wl_cursor_image *
1258display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001260 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001261
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001262 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001263}
1264
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001265static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001266surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001267{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001268 if (!surface->cairo_surface)
1269 return;
1270
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001271 if (surface->opaque_region) {
1272 wl_surface_set_opaque_region(surface->surface,
1273 surface->opaque_region);
1274 wl_region_destroy(surface->opaque_region);
1275 surface->opaque_region = NULL;
1276 }
1277
1278 if (surface->input_region) {
1279 wl_surface_set_input_region(surface->surface,
1280 surface->input_region);
1281 wl_region_destroy(surface->input_region);
1282 surface->input_region = NULL;
1283 }
1284
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001285 surface->toysurface->swap(surface->toysurface,
1286 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001287
Pekka Paalanen89dee002013-02-13 16:17:20 +02001288 cairo_surface_destroy(surface->cairo_surface);
1289 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001290}
1291
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001292int
1293window_has_focus(struct window *window)
1294{
1295 return window->focus_count > 0;
1296}
1297
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001298static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001299window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001300{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001301 struct surface *surface;
1302
Pekka Paalanen89dee002013-02-13 16:17:20 +02001303 if (window->type == TYPE_NONE) {
1304 window->type = TYPE_TOPLEVEL;
1305 if (window->shell_surface)
1306 wl_shell_surface_set_toplevel(window->shell_surface);
1307 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001308
Pekka Paalanen35e82632013-04-25 13:57:48 +03001309 wl_list_for_each(surface, &window->subsurface_list, link) {
1310 if (surface == window->main_surface)
1311 continue;
1312
1313 surface_flush(surface);
1314 }
1315
Pekka Paalanen89dee002013-02-13 16:17:20 +02001316 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001317}
1318
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001319struct display *
1320window_get_display(struct window *window)
1321{
1322 return window->display;
1323}
1324
Pekka Paalanen89dee002013-02-13 16:17:20 +02001325static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001326surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001327{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001328 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001329 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001330
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001331 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001332 case WL_OUTPUT_TRANSFORM_90:
1333 case WL_OUTPUT_TRANSFORM_270:
1334 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1335 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001336 allocation.width = surface->allocation.height;
1337 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001338 break;
1339 default:
1340 break;
1341 }
1342
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001343 allocation.width *= surface->buffer_scale;
1344 allocation.height *= surface->buffer_scale;
1345
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001346 if (!surface->toysurface && display->dpy &&
1347 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001348 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001349 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001350 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001351 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001352 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001353 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001354
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001355 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001356 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001357 surface->surface,
1358 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001359
Pekka Paalanen89dee002013-02-13 16:17:20 +02001360 surface->cairo_surface = surface->toysurface->prepare(
1361 surface->toysurface, dx, dy,
1362 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001363}
1364
1365static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001366window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001367{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001368 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001369 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001370 int dx = 0;
1371 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001372
Pekka Paalanenec076692012-11-30 13:37:27 +02001373 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001374 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001375
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001376 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1377 dx = surface->server_allocation.width -
1378 surface->allocation.width;
1379
1380 if (window->resize_edges & WINDOW_RESIZING_TOP)
1381 dy = surface->server_allocation.height -
1382 surface->allocation.height;
1383
1384 window->resize_edges = 0;
1385
Pekka Paalanen89dee002013-02-13 16:17:20 +02001386 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001387}
1388
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001389int
1390window_get_buffer_transform(struct window *window)
1391{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001392 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001393}
1394
1395void
1396window_set_buffer_transform(struct window *window,
1397 enum wl_output_transform transform)
1398{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001399 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001400 wl_surface_set_buffer_transform(window->main_surface->surface,
1401 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001402}
1403
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001404void
1405window_set_buffer_scale(struct window *window,
1406 uint32_t scale)
1407{
1408 window->main_surface->buffer_scale = scale;
1409 wl_surface_set_buffer_scale(window->main_surface->surface,
1410 scale);
1411}
1412
1413uint32_t
1414window_get_buffer_scale(struct window *window)
1415{
1416 return window->main_surface->buffer_scale;
1417}
1418
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001419static void frame_destroy(struct frame *frame);
1420
Pekka Paalanen4e373742013-02-13 16:17:13 +02001421static void
1422surface_destroy(struct surface *surface)
1423{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001424 if (surface->frame_cb)
1425 wl_callback_destroy(surface->frame_cb);
1426
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001427 if (surface->input_region)
1428 wl_region_destroy(surface->input_region);
1429
1430 if (surface->opaque_region)
1431 wl_region_destroy(surface->opaque_region);
1432
Pekka Paalanen35e82632013-04-25 13:57:48 +03001433 if (surface->subsurface)
1434 wl_subsurface_destroy(surface->subsurface);
1435
Pekka Paalanen4e373742013-02-13 16:17:13 +02001436 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001437
1438 if (surface->toysurface)
1439 surface->toysurface->destroy(surface->toysurface);
1440
Pekka Paalanen35e82632013-04-25 13:57:48 +03001441 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001442 free(surface);
1443}
1444
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001445void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001446window_destroy(struct window *window)
1447{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001448 struct display *display = window->display;
1449 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001450 struct window_output *window_output;
1451 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001452
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001453 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001454
1455 wl_list_for_each(input, &display->input_list, link) {
1456 if (input->pointer_focus == window)
1457 input->pointer_focus = NULL;
1458 if (input->keyboard_focus == window)
1459 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001460 if (input->focus_widget &&
1461 input->focus_widget->window == window)
1462 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001463 }
1464
Rob Bradford7507b572012-05-15 17:55:34 +01001465 wl_list_for_each_safe(window_output, window_output_tmp,
1466 &window->window_output_list, link) {
1467 free (window_output);
1468 }
1469
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001470 if (window->frame)
1471 frame_destroy(window->frame);
1472
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001473 if (window->shell_surface)
1474 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001475
1476 surface_destroy(window->main_surface);
1477
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001478 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001479
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001480 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001481 free(window);
1482}
1483
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001484static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001485widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001486{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001487 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001488
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001489 wl_list_for_each(child, &widget->child_list, link) {
1490 target = widget_find_widget(child, x, y);
1491 if (target)
1492 return target;
1493 }
1494
1495 if (widget->allocation.x <= x &&
1496 x < widget->allocation.x + widget->allocation.width &&
1497 widget->allocation.y <= y &&
1498 y < widget->allocation.y + widget->allocation.height) {
1499 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001500 }
1501
1502 return NULL;
1503}
1504
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001505static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001506window_find_widget(struct window *window, int32_t x, int32_t y)
1507{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001508 struct surface *surface;
1509 struct widget *widget;
1510
1511 wl_list_for_each(surface, &window->subsurface_list, link) {
1512 widget = widget_find_widget(surface->widget, x, y);
1513 if (widget)
1514 return widget;
1515 }
1516
1517 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001518}
1519
1520static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001521widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001522{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001523 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001524
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001525 widget = malloc(sizeof *widget);
1526 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001527 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001528 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001529 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001530 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001531 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001532 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001533 widget->tooltip = NULL;
1534 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001535 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001536
1537 return widget;
1538}
1539
1540struct widget *
1541window_add_widget(struct window *window, void *data)
1542{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001543 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001544
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001545 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001546 wl_list_init(&widget->link);
1547 window->main_surface->widget = widget;
1548
1549 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001550}
1551
1552struct widget *
1553widget_add_widget(struct widget *parent, void *data)
1554{
1555 struct widget *widget;
1556
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001557 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001558 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001559
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001560 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001561}
1562
1563void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001564widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001565{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001566 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001567 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001568 struct input *input;
1569
Pekka Paalanen35e82632013-04-25 13:57:48 +03001570 /* Destroy the sub-surface along with the root widget */
1571 if (surface->widget == widget && surface->subsurface)
1572 surface_destroy(widget->surface);
1573
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001574 if (widget->tooltip) {
1575 free(widget->tooltip);
1576 widget->tooltip = NULL;
1577 }
1578
Pekka Paalanene156fb62012-01-19 13:51:38 +02001579 wl_list_for_each(input, &display->input_list, link) {
1580 if (input->focus_widget == widget)
1581 input->focus_widget = NULL;
1582 }
1583
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001584 wl_list_remove(&widget->link);
1585 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001586}
1587
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001588void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001589widget_set_default_cursor(struct widget *widget, int cursor)
1590{
1591 widget->default_cursor = cursor;
1592}
1593
1594void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001595widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001596{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001597 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001598}
1599
1600void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001601widget_set_size(struct widget *widget, int32_t width, int32_t height)
1602{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001603 widget->allocation.width = width;
1604 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001605}
1606
1607void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001608widget_set_allocation(struct widget *widget,
1609 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001610{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001611 widget->allocation.x = x;
1612 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001613 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001614}
1615
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001616void
1617widget_set_transparent(struct widget *widget, int transparent)
1618{
1619 widget->opaque = !transparent;
1620}
1621
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001622void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001623widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001624{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001625 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001626}
1627
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001628static cairo_surface_t *
1629widget_get_cairo_surface(struct widget *widget)
1630{
1631 struct surface *surface = widget->surface;
1632 struct window *window = widget->window;
1633
1634 if (!surface->cairo_surface) {
1635 if (surface == window->main_surface)
1636 window_create_main_surface(window);
1637 else
1638 surface_create_surface(surface, 0, 0, 0);
1639 }
1640
1641 return surface->cairo_surface;
1642}
1643
Alexander Larsson15901f02013-05-22 14:41:25 +02001644static void
1645widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1646{
1647 struct surface *surface = widget->surface;
1648 double angle;
1649 cairo_matrix_t m;
1650 enum wl_output_transform transform;
1651 int surface_width, surface_height;
1652 int translate_x, translate_y;
1653
1654 surface_width = surface->allocation.width;
1655 surface_height = surface->allocation.height;
1656
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001657 transform = surface->buffer_transform;
1658
Alexander Larsson15901f02013-05-22 14:41:25 +02001659 switch (transform) {
1660 case WL_OUTPUT_TRANSFORM_FLIPPED:
1661 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1662 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1663 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1664 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1665 break;
1666 default:
1667 cairo_matrix_init_identity(&m);
1668 break;
1669 }
1670
1671 switch (transform) {
1672 case WL_OUTPUT_TRANSFORM_NORMAL:
1673 default:
1674 angle = 0;
1675 translate_x = 0;
1676 translate_y = 0;
1677 break;
1678 case WL_OUTPUT_TRANSFORM_FLIPPED:
1679 angle = 0;
1680 translate_x = surface_width;
1681 translate_y = 0;
1682 break;
1683 case WL_OUTPUT_TRANSFORM_90:
1684 angle = M_PI_2;
1685 translate_x = surface_height;
1686 translate_y = 0;
1687 break;
1688 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1689 angle = M_PI_2;
1690 translate_x = surface_height;
1691 translate_y = surface_width;
1692 break;
1693 case WL_OUTPUT_TRANSFORM_180:
1694 angle = M_PI;
1695 translate_x = surface_width;
1696 translate_y = surface_height;
1697 break;
1698 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1699 angle = M_PI;
1700 translate_x = 0;
1701 translate_y = surface_height;
1702 break;
1703 case WL_OUTPUT_TRANSFORM_270:
1704 angle = M_PI + M_PI_2;
1705 translate_x = 0;
1706 translate_y = surface_width;
1707 break;
1708 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1709 angle = M_PI + M_PI_2;
1710 translate_x = 0;
1711 translate_y = 0;
1712 break;
1713 }
1714
1715 cairo_translate(cr, translate_x, translate_y);
1716 cairo_rotate(cr, angle);
1717 cairo_transform(cr, &m);
1718}
1719
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001720cairo_t *
1721widget_cairo_create(struct widget *widget)
1722{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001723 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001724 cairo_surface_t *cairo_surface;
1725 cairo_t *cr;
1726
1727 cairo_surface = widget_get_cairo_surface(widget);
1728 cr = cairo_create(cairo_surface);
1729
Alexander Larsson15901f02013-05-22 14:41:25 +02001730 widget_cairo_update_transform(widget, cr);
1731
Pekka Paalanen35e82632013-04-25 13:57:48 +03001732 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1733
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001734 return cr;
1735}
1736
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001737struct wl_surface *
1738widget_get_wl_surface(struct widget *widget)
1739{
1740 return widget->surface->surface;
1741}
1742
1743uint32_t
1744widget_get_last_time(struct widget *widget)
1745{
1746 return widget->surface->last_time;
1747}
1748
1749void
1750widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1751{
1752 struct wl_compositor *comp = widget->window->display->compositor;
1753 struct surface *surface = widget->surface;
1754
1755 if (!surface->input_region)
1756 surface->input_region = wl_compositor_create_region(comp);
1757
1758 if (rect) {
1759 wl_region_add(surface->input_region,
1760 rect->x, rect->y, rect->width, rect->height);
1761 }
1762}
1763
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001764void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001765widget_set_resize_handler(struct widget *widget,
1766 widget_resize_handler_t handler)
1767{
1768 widget->resize_handler = handler;
1769}
1770
1771void
1772widget_set_redraw_handler(struct widget *widget,
1773 widget_redraw_handler_t handler)
1774{
1775 widget->redraw_handler = handler;
1776}
1777
1778void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001779widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001780{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001781 widget->enter_handler = handler;
1782}
1783
1784void
1785widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1786{
1787 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001788}
1789
1790void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001791widget_set_motion_handler(struct widget *widget,
1792 widget_motion_handler_t handler)
1793{
1794 widget->motion_handler = handler;
1795}
1796
1797void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001798widget_set_button_handler(struct widget *widget,
1799 widget_button_handler_t handler)
1800{
1801 widget->button_handler = handler;
1802}
1803
1804void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001805widget_set_axis_handler(struct widget *widget,
1806 widget_axis_handler_t handler)
1807{
1808 widget->axis_handler = handler;
1809}
1810
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001811static void
1812window_schedule_redraw_task(struct window *window);
1813
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001814void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001815widget_schedule_redraw(struct widget *widget)
1816{
Pekka Paalanen71233882013-04-25 13:57:53 +03001817 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001818 widget->surface->redraw_needed = 1;
1819 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001820}
1821
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001822cairo_surface_t *
1823window_get_surface(struct window *window)
1824{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001825 cairo_surface_t *cairo_surface;
1826
1827 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1828
1829 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001830}
1831
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001832struct wl_surface *
1833window_get_wl_surface(struct window *window)
1834{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001835 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001836}
1837
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001838struct wl_shell_surface *
1839window_get_wl_shell_surface(struct window *window)
1840{
1841 return window->shell_surface;
1842}
1843
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001844static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001845tooltip_redraw_handler(struct widget *widget, void *data)
1846{
1847 cairo_t *cr;
1848 const int32_t r = 3;
1849 struct tooltip *tooltip = data;
1850 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001851
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001852 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001853 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1854 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1855 cairo_paint(cr);
1856
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001857 width = widget->allocation.width;
1858 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001859 rounded_rect(cr, 0, 0, width, height, r);
1860
1861 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1862 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1863 cairo_fill(cr);
1864
1865 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1866 cairo_move_to(cr, 10, 16);
1867 cairo_show_text(cr, tooltip->entry);
1868 cairo_destroy(cr);
1869}
1870
1871static cairo_text_extents_t
1872get_text_extents(struct tooltip *tooltip)
1873{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001874 cairo_t *cr;
1875 cairo_text_extents_t extents;
1876
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001877 /* Use the dummy_surface because tooltip's surface was not
1878 * created yet, and parent does not have a valid surface
1879 * outside repaint, either.
1880 */
1881 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001882 cairo_text_extents(cr, tooltip->entry, &extents);
1883 cairo_destroy(cr);
1884
1885 return extents;
1886}
1887
1888static int
1889window_create_tooltip(struct tooltip *tooltip)
1890{
1891 struct widget *parent = tooltip->parent;
1892 struct display *display = parent->window->display;
1893 struct window *window;
1894 const int offset_y = 27;
1895 const int margin = 3;
1896 cairo_text_extents_t extents;
1897
1898 if (tooltip->widget)
1899 return 0;
1900
1901 window = window_create_transient(display, parent->window, tooltip->x,
1902 tooltip->y + offset_y,
1903 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1904 if (!window)
1905 return -1;
1906
1907 tooltip->window = window;
1908 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1909
1910 extents = get_text_extents(tooltip);
1911 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1912 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1913
1914 return 0;
1915}
1916
1917void
1918widget_destroy_tooltip(struct widget *parent)
1919{
1920 struct tooltip *tooltip = parent->tooltip;
1921
1922 parent->tooltip_count = 0;
1923 if (!tooltip)
1924 return;
1925
1926 if (tooltip->widget) {
1927 widget_destroy(tooltip->widget);
1928 window_destroy(tooltip->window);
1929 tooltip->widget = NULL;
1930 tooltip->window = NULL;
1931 }
1932
1933 close(tooltip->tooltip_fd);
1934 free(tooltip->entry);
1935 free(tooltip);
1936 parent->tooltip = NULL;
1937}
1938
1939static void
1940tooltip_func(struct task *task, uint32_t events)
1941{
1942 struct tooltip *tooltip =
1943 container_of(task, struct tooltip, tooltip_task);
1944 uint64_t exp;
1945
Martin Olsson8df662a2012-07-08 03:03:47 +02001946 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1947 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001948 window_create_tooltip(tooltip);
1949}
1950
1951#define TOOLTIP_TIMEOUT 500
1952static int
1953tooltip_timer_reset(struct tooltip *tooltip)
1954{
1955 struct itimerspec its;
1956
1957 its.it_interval.tv_sec = 0;
1958 its.it_interval.tv_nsec = 0;
1959 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1960 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1961 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1962 fprintf(stderr, "could not set timerfd\n: %m");
1963 return -1;
1964 }
1965
1966 return 0;
1967}
1968
1969int
1970widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1971{
1972 struct tooltip *tooltip = parent->tooltip;
1973
1974 parent->tooltip_count++;
1975 if (tooltip) {
1976 tooltip->x = x;
1977 tooltip->y = y;
1978 tooltip_timer_reset(tooltip);
1979 return 0;
1980 }
1981
1982 /* the handler might be triggered too fast via input device motion, so
1983 * we need this check here to make sure tooltip is fully initialized */
1984 if (parent->tooltip_count > 1)
1985 return 0;
1986
1987 tooltip = malloc(sizeof *tooltip);
1988 if (!tooltip)
1989 return -1;
1990
1991 parent->tooltip = tooltip;
1992 tooltip->parent = parent;
1993 tooltip->widget = NULL;
1994 tooltip->window = NULL;
1995 tooltip->x = x;
1996 tooltip->y = y;
1997 tooltip->entry = strdup(entry);
1998 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1999 if (tooltip->tooltip_fd < 0) {
2000 fprintf(stderr, "could not create timerfd\n: %m");
2001 return -1;
2002 }
2003
2004 tooltip->tooltip_task.run = tooltip_func;
2005 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2006 EPOLLIN, &tooltip->tooltip_task);
2007 tooltip_timer_reset(tooltip);
2008
2009 return 0;
2010}
2011
2012static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002013workspace_manager_state(void *data,
2014 struct workspace_manager *workspace_manager,
2015 uint32_t current,
2016 uint32_t count)
2017{
2018 struct display *display = data;
2019
2020 display->workspace = current;
2021 display->workspace_count = count;
2022}
2023
2024static const struct workspace_manager_listener workspace_manager_listener = {
2025 workspace_manager_state
2026};
2027
2028static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002029frame_resize_handler(struct widget *widget,
2030 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002031{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002032 struct frame *frame = data;
2033 struct widget *child = frame->child;
2034 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002035 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002036 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002037 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002038 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002039 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002040 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002041 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002042
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002043 switch (widget->window->type) {
2044 case TYPE_FULLSCREEN:
2045 decoration_width = 0;
2046 decoration_height = 0;
2047
2048 allocation.x = 0;
2049 allocation.y = 0;
2050 allocation.width = width;
2051 allocation.height = height;
2052 opaque_margin = 0;
2053
2054 wl_list_for_each(button, &frame->buttons_list, link)
2055 button->widget->opaque = 1;
2056 break;
2057 case TYPE_MAXIMIZED:
2058 decoration_width = t->width * 2;
2059 decoration_height = t->width + t->titlebar_height;
2060
2061 allocation.x = t->width;
2062 allocation.y = t->titlebar_height;
2063 allocation.width = width - decoration_width;
2064 allocation.height = height - decoration_height;
2065
2066 opaque_margin = 0;
2067
2068 wl_list_for_each(button, &frame->buttons_list, link)
2069 button->widget->opaque = 0;
2070 break;
2071 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002072 decoration_width = (t->width + t->margin) * 2;
2073 decoration_height = t->width +
2074 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002075
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002076 allocation.x = t->width + t->margin;
2077 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002078 allocation.width = width - decoration_width;
2079 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002080
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002081 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002082
2083 wl_list_for_each(button, &frame->buttons_list, link)
2084 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002085 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002086 }
2087
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002088 widget_set_allocation(child, allocation.x, allocation.y,
2089 allocation.width, allocation.height);
2090
2091 if (child->resize_handler)
2092 child->resize_handler(child,
2093 allocation.width,
2094 allocation.height,
2095 child->user_data);
2096
Scott Moreauf7e498c2012-05-14 11:39:29 -06002097 width = child->allocation.width + decoration_width;
2098 height = child->allocation.height + decoration_height;
2099
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002100 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2101
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002102 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002103 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002104 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002105 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002106 shadow_margin, shadow_margin,
2107 width - 2 * shadow_margin,
2108 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002109 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002110 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002111 }
2112
Scott Moreauf7e498c2012-05-14 11:39:29 -06002113 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002114
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002115 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002116 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002117 opaque_margin, opaque_margin,
2118 widget->allocation.width - 2 * opaque_margin,
2119 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002120
2121 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002122 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2123 x_l = t->width + shadow_margin;
2124 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002125 wl_list_for_each(button, &frame->buttons_list, link) {
2126 const int button_padding = 4;
2127 w = cairo_image_surface_get_width(button->icon);
2128 h = cairo_image_surface_get_height(button->icon);
2129
2130 if (button->decoration == FRAME_BUTTON_FANCY)
2131 w += 10;
2132
2133 if (button->align == FRAME_BUTTON_LEFT) {
2134 widget_set_allocation(button->widget,
2135 x_l, y , w + 1, h + 1);
2136 x_l += w;
2137 x_l += button_padding;
2138 } else {
2139 x_r -= w;
2140 widget_set_allocation(button->widget,
2141 x_r, y , w + 1, h + 1);
2142 x_r -= button_padding;
2143 }
2144 }
2145}
2146
2147static int
2148frame_button_enter_handler(struct widget *widget,
2149 struct input *input, float x, float y, void *data)
2150{
2151 struct frame_button *frame_button = data;
2152
2153 widget_schedule_redraw(frame_button->widget);
2154 frame_button->state = FRAME_BUTTON_OVER;
2155
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002156 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002157}
2158
2159static void
2160frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2161{
2162 struct frame_button *frame_button = data;
2163
2164 widget_schedule_redraw(frame_button->widget);
2165 frame_button->state = FRAME_BUTTON_DEFAULT;
2166}
2167
2168static void
2169frame_button_button_handler(struct widget *widget,
2170 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002171 uint32_t button,
2172 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002173{
2174 struct frame_button *frame_button = data;
2175 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002176 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002177
2178 if (button != BTN_LEFT)
2179 return;
2180
2181 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002182 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002183 frame_button->state = FRAME_BUTTON_ACTIVE;
2184 widget_schedule_redraw(frame_button->widget);
2185
2186 if (frame_button->type == FRAME_BUTTON_ICON)
2187 window_show_frame_menu(window, input, time);
2188 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002189 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002190 frame_button->state = FRAME_BUTTON_DEFAULT;
2191 widget_schedule_redraw(frame_button->widget);
2192 break;
2193 }
2194
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002195 if (!was_pressed)
2196 return;
2197
Martin Minarik1998b152012-05-10 02:04:35 +02002198 switch (frame_button->type) {
2199 case FRAME_BUTTON_CLOSE:
2200 if (window->close_handler)
2201 window->close_handler(window->parent,
2202 window->user_data);
2203 else
2204 display_exit(window->display);
2205 break;
2206 case FRAME_BUTTON_MINIMIZE:
2207 fprintf(stderr,"Minimize stub\n");
2208 break;
2209 case FRAME_BUTTON_MAXIMIZE:
2210 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2211 break;
2212 default:
2213 /* Unknown operation */
2214 break;
2215 }
2216}
2217
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002218static int
2219frame_button_motion_handler(struct widget *widget,
2220 struct input *input, uint32_t time,
2221 float x, float y, void *data)
2222{
2223 struct frame_button *frame_button = data;
2224 enum frame_button_pointer previous_button_state = frame_button->state;
2225
2226 /* only track state for a pressed button */
2227 if (input->grab != widget)
2228 return CURSOR_LEFT_PTR;
2229
2230 if (x > widget->allocation.x &&
2231 x < (widget->allocation.x + widget->allocation.width) &&
2232 y > widget->allocation.y &&
2233 y < (widget->allocation.y + widget->allocation.height)) {
2234 frame_button->state = FRAME_BUTTON_ACTIVE;
2235 } else {
2236 frame_button->state = FRAME_BUTTON_DEFAULT;
2237 }
2238
2239 if (frame_button->state != previous_button_state)
2240 widget_schedule_redraw(frame_button->widget);
2241
2242 return CURSOR_LEFT_PTR;
2243}
2244
Martin Minarik1998b152012-05-10 02:04:35 +02002245static void
2246frame_button_redraw_handler(struct widget *widget, void *data)
2247{
2248 struct frame_button *frame_button = data;
2249 cairo_t *cr;
2250 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002251
2252 x = widget->allocation.x;
2253 y = widget->allocation.y;
2254 width = widget->allocation.width;
2255 height = widget->allocation.height;
2256
2257 if (!width)
2258 return;
2259 if (!height)
2260 return;
2261 if (widget->opaque)
2262 return;
2263
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002264 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002265
2266 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2267 cairo_set_line_width(cr, 1);
2268
2269 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2270 cairo_rectangle (cr, x, y, 25, 16);
2271
2272 cairo_stroke_preserve(cr);
2273
2274 switch (frame_button->state) {
2275 case FRAME_BUTTON_DEFAULT:
2276 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2277 break;
2278 case FRAME_BUTTON_OVER:
2279 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2280 break;
2281 case FRAME_BUTTON_ACTIVE:
2282 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2283 break;
2284 }
2285
2286 cairo_fill (cr);
2287
2288 x += 4;
2289 }
2290
2291 cairo_set_source_surface(cr, frame_button->icon, x, y);
2292 cairo_paint(cr);
2293
2294 cairo_destroy(cr);
2295}
2296
2297static struct widget *
2298frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2299 enum frame_button_align align, enum frame_button_decoration style)
2300{
2301 struct frame_button *frame_button;
2302 const char *icon = data;
2303
2304 frame_button = malloc (sizeof *frame_button);
2305 memset(frame_button, 0, sizeof *frame_button);
2306
2307 frame_button->icon = cairo_image_surface_create_from_png(icon);
2308 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2309 frame_button->frame = frame;
2310 frame_button->type = type;
2311 frame_button->align = align;
2312 frame_button->decoration = style;
2313
2314 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2315
2316 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2317 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2318 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2319 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002320 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002321 return frame_button->widget;
2322}
2323
2324static void
2325frame_button_destroy(struct frame_button *frame_button)
2326{
2327 widget_destroy(frame_button->widget);
2328 wl_list_remove(&frame_button->link);
2329 cairo_surface_destroy(frame_button->icon);
2330 free(frame_button);
2331
2332 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002333}
2334
2335static void
2336frame_redraw_handler(struct widget *widget, void *data)
2337{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002338 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002339 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002340 struct theme *t = window->display->theme;
2341 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002342
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002343 if (window->type == TYPE_FULLSCREEN)
2344 return;
2345
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002346 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002347
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002348 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002349 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002350 if (window->type == TYPE_MAXIMIZED)
2351 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002352 theme_render_frame(t, cr, widget->allocation.width,
2353 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002354
2355 cairo_destroy(cr);
2356}
2357
2358static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002359frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002360{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002361 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002362 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002363 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002364
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002365 if (window->type != TYPE_TOPLEVEL)
2366 return CURSOR_LEFT_PTR;
2367
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002368 location = theme_get_location(t, input->sx, input->sy,
2369 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002370 frame->widget->allocation.height,
2371 window->type == TYPE_MAXIMIZED ?
2372 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002373
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002374 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002375 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002376 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002377 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002378 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002379 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002380 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002381 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002382 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002383 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002384 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002385 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002386 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002387 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002388 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002389 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002390 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002391 case THEME_LOCATION_EXTERIOR:
2392 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002393 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002394 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002395 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002396}
2397
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002398static void
2399frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002400{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002401 struct display *display;
2402
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002403 switch (index) {
2404 case 0: /* close */
2405 if (window->close_handler)
2406 window->close_handler(window->parent,
2407 window->user_data);
2408 else
2409 display_exit(window->display);
2410 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002411 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002412 display = window->display;
2413 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002414 workspace_manager_move_surface(
2415 display->workspace_manager,
2416 window->main_surface->surface,
2417 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002418 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002419 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002420 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002421 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002422 workspace_manager_move_surface(
2423 display->workspace_manager,
2424 window->main_surface->surface,
2425 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002426 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002427 case 3: /* fullscreen */
2428 /* we don't have a way to get out of fullscreen for now */
2429 if (window->fullscreen_handler)
2430 window->fullscreen_handler(window, window->user_data);
2431 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002432 }
2433}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002434
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002435void
2436window_show_frame_menu(struct window *window,
2437 struct input *input, uint32_t time)
2438{
2439 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002440 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002441
2442 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002443 "Close",
2444 "Move to workspace above", "Move to workspace below",
2445 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002446 };
2447
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002448 if (window->fullscreen_handler)
2449 count = ARRAY_LENGTH(entries);
2450 else
2451 count = ARRAY_LENGTH(entries) - 1;
2452
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002453 input_get_position(input, &x, &y);
2454 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002455 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002456}
2457
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002458static int
2459frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002460 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002461{
2462 return frame_get_pointer_image_for_location(data, input);
2463}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002464
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002465static int
2466frame_motion_handler(struct widget *widget,
2467 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002468 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002469{
2470 return frame_get_pointer_image_for_location(data, input);
2471}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002472
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002473static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002474frame_button_handler(struct widget *widget,
2475 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002476 uint32_t button, enum wl_pointer_button_state state,
2477 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002478
2479{
2480 struct frame *frame = data;
2481 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002482 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002483 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002484
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002485 if (window->type != TYPE_TOPLEVEL)
2486 return;
2487
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002488 location = theme_get_location(display->theme, input->sx, input->sy,
2489 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002490 frame->widget->allocation.height,
2491 window->type == TYPE_MAXIMIZED ?
2492 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002493
Daniel Stone4dbadb12012-05-30 16:31:51 +01002494 if (window->display->shell && button == BTN_LEFT &&
2495 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002496 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002497 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498 if (!window->shell_surface)
2499 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002500 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002501 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002502 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002503 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002504 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002505 case THEME_LOCATION_RESIZING_TOP:
2506 case THEME_LOCATION_RESIZING_BOTTOM:
2507 case THEME_LOCATION_RESIZING_LEFT:
2508 case THEME_LOCATION_RESIZING_RIGHT:
2509 case THEME_LOCATION_RESIZING_TOP_LEFT:
2510 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2511 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2512 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002513 if (!window->shell_surface)
2514 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002515 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002516
Pekka Paalanen99436862012-11-19 17:15:59 +02002517 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002518 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002519 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002520 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002521 break;
2522 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002523 } else if (button == BTN_RIGHT &&
2524 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002525 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002526 }
2527}
2528
2529struct widget *
2530frame_create(struct window *window, void *data)
2531{
2532 struct frame *frame;
2533
2534 frame = malloc(sizeof *frame);
2535 memset(frame, 0, sizeof *frame);
2536
2537 frame->widget = window_add_widget(window, frame);
2538 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002539
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002540 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2541 widget_set_resize_handler(frame->widget, frame_resize_handler);
2542 widget_set_enter_handler(frame->widget, frame_enter_handler);
2543 widget_set_motion_handler(frame->widget, frame_motion_handler);
2544 widget_set_button_handler(frame->widget, frame_button_handler);
2545
Martin Minarik1998b152012-05-10 02:04:35 +02002546 /* Create empty list for frame buttons */
2547 wl_list_init(&frame->buttons_list);
2548
2549 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2550 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2551
2552 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2553 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2554
2555 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2556 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2557
2558 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2559 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2560
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002561 window->frame = frame;
2562
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002563 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002564}
2565
Kristian Høgsberga1627922012-06-20 17:30:03 -04002566void
2567frame_set_child_size(struct widget *widget, int child_width, int child_height)
2568{
2569 struct display *display = widget->window->display;
2570 struct theme *t = display->theme;
2571 int decoration_width, decoration_height;
2572 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002573 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002574
2575 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002576 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002577 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002578 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002579
2580 width = child_width + decoration_width;
2581 height = child_height + decoration_height;
2582 } else {
2583 width = child_width;
2584 height = child_height;
2585 }
2586
2587 window_schedule_resize(widget->window, width, height);
2588}
2589
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002590static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002591frame_destroy(struct frame *frame)
2592{
Martin Minarik1998b152012-05-10 02:04:35 +02002593 struct frame_button *button, *tmp;
2594
2595 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2596 frame_button_destroy(button);
2597
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002598 /* frame->child must be destroyed by the application */
2599 widget_destroy(frame->widget);
2600 free(frame);
2601}
2602
2603static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002604input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002605 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002606{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002607 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002608 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002609
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002610 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002611 return;
2612
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002613 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002614 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002615 widget = old;
2616 if (input->grab)
2617 widget = input->grab;
2618 if (widget->leave_handler)
2619 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002620 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002621 }
2622
2623 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002624 widget = focus;
2625 if (input->grab)
2626 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002627 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002628 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002629 cursor = widget->enter_handler(focus, input, x, y,
2630 widget->user_data);
2631 else
2632 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002633
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002634 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002635 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002636}
2637
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002638void
2639input_grab(struct input *input, struct widget *widget, uint32_t button)
2640{
2641 input->grab = widget;
2642 input->grab_button = button;
2643}
2644
2645void
2646input_ungrab(struct input *input)
2647{
2648 struct widget *widget;
2649
2650 input->grab = NULL;
2651 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002652 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002653 input->sx, input->sy);
2654 input_set_focus_widget(input, widget, input->sx, input->sy);
2655 }
2656}
2657
2658static void
2659input_remove_pointer_focus(struct input *input)
2660{
2661 struct window *window = input->pointer_focus;
2662
2663 if (!window)
2664 return;
2665
2666 input_set_focus_widget(input, NULL, 0, 0);
2667
2668 input->pointer_focus = NULL;
2669 input->current_cursor = CURSOR_UNSET;
2670}
2671
2672static void
2673pointer_handle_enter(void *data, struct wl_pointer *pointer,
2674 uint32_t serial, struct wl_surface *surface,
2675 wl_fixed_t sx_w, wl_fixed_t sy_w)
2676{
2677 struct input *input = data;
2678 struct window *window;
2679 struct widget *widget;
2680 float sx = wl_fixed_to_double(sx_w);
2681 float sy = wl_fixed_to_double(sy_w);
2682
2683 if (!surface) {
2684 /* enter event for a window we've just destroyed */
2685 return;
2686 }
2687
2688 input->display->serial = serial;
2689 input->pointer_enter_serial = serial;
2690 input->pointer_focus = wl_surface_get_user_data(surface);
2691 window = input->pointer_focus;
2692
Pekka Paalanen99436862012-11-19 17:15:59 +02002693 if (window->resizing) {
2694 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002695 /* Schedule a redraw to free the pool */
2696 window_schedule_redraw(window);
2697 }
2698
2699 input->sx = sx;
2700 input->sy = sy;
2701
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002702 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002703 input_set_focus_widget(input, widget, sx, sy);
2704}
2705
2706static void
2707pointer_handle_leave(void *data, struct wl_pointer *pointer,
2708 uint32_t serial, struct wl_surface *surface)
2709{
2710 struct input *input = data;
2711
2712 input->display->serial = serial;
2713 input_remove_pointer_focus(input);
2714}
2715
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002716static void
Daniel Stone37816df2012-05-16 18:45:18 +01002717pointer_handle_motion(void *data, struct wl_pointer *pointer,
2718 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002719{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002720 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002721 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002722 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002723 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002724 float sx = wl_fixed_to_double(sx_w);
2725 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002726
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002727 input->sx = sx;
2728 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002729
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002730 if (!window)
2731 return;
2732
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002733 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002734 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002735 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002736 }
2737
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002738 if (input->grab)
2739 widget = input->grab;
2740 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002741 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002742 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002743 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002744 input, time, sx, sy,
2745 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002746 else
2747 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002748
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002749 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002750}
2751
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002752static void
Daniel Stone37816df2012-05-16 18:45:18 +01002753pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002754 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002755{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002756 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002757 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002758 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002759
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002760 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002761 if (input->focus_widget && input->grab == NULL &&
2762 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002763 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002764
Neil Roberts6b28aad2012-01-23 19:11:18 +00002765 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002766 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002767 (*widget->button_handler)(widget,
2768 input, time,
2769 button, state,
2770 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002771
Daniel Stone4dbadb12012-05-30 16:31:51 +01002772 if (input->grab && input->grab_button == button &&
2773 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002774 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002775}
2776
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002777static void
Daniel Stone37816df2012-05-16 18:45:18 +01002778pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002779 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002780{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002781 struct input *input = data;
2782 struct widget *widget;
2783
2784 widget = input->focus_widget;
2785 if (input->grab)
2786 widget = input->grab;
2787 if (widget && widget->axis_handler)
2788 (*widget->axis_handler)(widget,
2789 input, time,
2790 axis, value,
2791 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002792}
2793
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002794static const struct wl_pointer_listener pointer_listener = {
2795 pointer_handle_enter,
2796 pointer_handle_leave,
2797 pointer_handle_motion,
2798 pointer_handle_button,
2799 pointer_handle_axis,
2800};
2801
2802static void
2803input_remove_keyboard_focus(struct input *input)
2804{
2805 struct window *window = input->keyboard_focus;
2806 struct itimerspec its;
2807
2808 its.it_interval.tv_sec = 0;
2809 its.it_interval.tv_nsec = 0;
2810 its.it_value.tv_sec = 0;
2811 its.it_value.tv_nsec = 0;
2812 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2813
2814 if (!window)
2815 return;
2816
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002817 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002818 if (window->keyboard_focus_handler)
2819 (*window->keyboard_focus_handler)(window, NULL,
2820 window->user_data);
2821
2822 input->keyboard_focus = NULL;
2823}
2824
2825static void
2826keyboard_repeat_func(struct task *task, uint32_t events)
2827{
2828 struct input *input =
2829 container_of(task, struct input, repeat_task);
2830 struct window *window = input->keyboard_focus;
2831 uint64_t exp;
2832
2833 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2834 /* If we change the timer between the fd becoming
2835 * readable and getting here, there'll be nothing to
2836 * read and we get EAGAIN. */
2837 return;
2838
2839 if (window && window->key_handler) {
2840 (*window->key_handler)(window, input, input->repeat_time,
2841 input->repeat_key, input->repeat_sym,
2842 WL_KEYBOARD_KEY_STATE_PRESSED,
2843 window->user_data);
2844 }
2845}
2846
2847static void
2848keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2849 uint32_t format, int fd, uint32_t size)
2850{
2851 struct input *input = data;
2852 char *map_str;
2853
2854 if (!data) {
2855 close(fd);
2856 return;
2857 }
2858
2859 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2860 close(fd);
2861 return;
2862 }
2863
2864 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2865 if (map_str == MAP_FAILED) {
2866 close(fd);
2867 return;
2868 }
2869
2870 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2871 map_str,
2872 XKB_KEYMAP_FORMAT_TEXT_V1,
2873 0);
2874 munmap(map_str, size);
2875 close(fd);
2876
2877 if (!input->xkb.keymap) {
2878 fprintf(stderr, "failed to compile keymap\n");
2879 return;
2880 }
2881
2882 input->xkb.state = xkb_state_new(input->xkb.keymap);
2883 if (!input->xkb.state) {
2884 fprintf(stderr, "failed to create XKB state\n");
2885 xkb_map_unref(input->xkb.keymap);
2886 input->xkb.keymap = NULL;
2887 return;
2888 }
2889
2890 input->xkb.control_mask =
2891 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2892 input->xkb.alt_mask =
2893 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2894 input->xkb.shift_mask =
2895 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2896}
2897
2898static void
2899keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2900 uint32_t serial, struct wl_surface *surface,
2901 struct wl_array *keys)
2902{
2903 struct input *input = data;
2904 struct window *window;
2905
2906 input->display->serial = serial;
2907 input->keyboard_focus = wl_surface_get_user_data(surface);
2908
2909 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002910 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002911 if (window->keyboard_focus_handler)
2912 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002913 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002914}
2915
2916static void
2917keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2918 uint32_t serial, struct wl_surface *surface)
2919{
2920 struct input *input = data;
2921
2922 input->display->serial = serial;
2923 input_remove_keyboard_focus(input);
2924}
2925
Scott Moreau210d0792012-03-22 10:47:01 -06002926static void
Daniel Stone37816df2012-05-16 18:45:18 +01002927keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002928 uint32_t serial, uint32_t time, uint32_t key,
2929 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002930{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002931 struct input *input = data;
2932 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002933 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002934 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002935 const xkb_keysym_t *syms;
2936 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002937 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002938
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002939 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002940 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002941 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002942 return;
2943
Daniel Stone97f68542012-05-30 16:32:01 +01002944 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002945
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002946 sym = XKB_KEY_NoSymbol;
2947 if (num_syms == 1)
2948 sym = syms[0];
2949
2950 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002951 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002952 window_set_maximized(window,
2953 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002954 } else if (sym == XKB_KEY_F11 &&
2955 window->fullscreen_handler &&
2956 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2957 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002958 } else if (sym == XKB_KEY_F4 &&
2959 input->modifiers == MOD_ALT_MASK &&
2960 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2961 if (window->close_handler)
2962 window->close_handler(window->parent,
2963 window->user_data);
2964 else
2965 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002966 } else if (window->key_handler) {
2967 (*window->key_handler)(window, input, time, key,
2968 sym, state, window->user_data);
2969 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002970
2971 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2972 key == input->repeat_key) {
2973 its.it_interval.tv_sec = 0;
2974 its.it_interval.tv_nsec = 0;
2975 its.it_value.tv_sec = 0;
2976 its.it_value.tv_nsec = 0;
2977 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2978 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2979 input->repeat_sym = sym;
2980 input->repeat_key = key;
2981 input->repeat_time = time;
2982 its.it_interval.tv_sec = 0;
2983 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2984 its.it_value.tv_sec = 0;
2985 its.it_value.tv_nsec = 400 * 1000 * 1000;
2986 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2987 }
2988}
2989
2990static void
Daniel Stone351eb612012-05-31 15:27:47 -04002991keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2992 uint32_t serial, uint32_t mods_depressed,
2993 uint32_t mods_latched, uint32_t mods_locked,
2994 uint32_t group)
2995{
2996 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002997 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002998
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002999 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3000 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003001 mask = xkb_state_serialize_mods(input->xkb.state,
3002 XKB_STATE_DEPRESSED |
3003 XKB_STATE_LATCHED);
3004 input->modifiers = 0;
3005 if (mask & input->xkb.control_mask)
3006 input->modifiers |= MOD_CONTROL_MASK;
3007 if (mask & input->xkb.alt_mask)
3008 input->modifiers |= MOD_ALT_MASK;
3009 if (mask & input->xkb.shift_mask)
3010 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003011}
3012
Daniel Stone37816df2012-05-16 18:45:18 +01003013static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003014 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003015 keyboard_handle_enter,
3016 keyboard_handle_leave,
3017 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003018 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003019};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003020
3021static void
Daniel Stone37816df2012-05-16 18:45:18 +01003022seat_handle_capabilities(void *data, struct wl_seat *seat,
3023 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003024{
Daniel Stone37816df2012-05-16 18:45:18 +01003025 struct input *input = data;
3026
3027 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3028 input->pointer = wl_seat_get_pointer(seat);
3029 wl_pointer_set_user_data(input->pointer, input);
3030 wl_pointer_add_listener(input->pointer, &pointer_listener,
3031 input);
3032 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3033 wl_pointer_destroy(input->pointer);
3034 input->pointer = NULL;
3035 }
3036
3037 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3038 input->keyboard = wl_seat_get_keyboard(seat);
3039 wl_keyboard_set_user_data(input->keyboard, input);
3040 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3041 input);
3042 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3043 wl_keyboard_destroy(input->keyboard);
3044 input->keyboard = NULL;
3045 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003046}
3047
Daniel Stone37816df2012-05-16 18:45:18 +01003048static const struct wl_seat_listener seat_listener = {
3049 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003050};
3051
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003052void
3053input_get_position(struct input *input, int32_t *x, int32_t *y)
3054{
3055 *x = input->sx;
3056 *y = input->sy;
3057}
3058
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003059struct display *
3060input_get_display(struct input *input)
3061{
3062 return input->display;
3063}
3064
Daniel Stone37816df2012-05-16 18:45:18 +01003065struct wl_seat *
3066input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003067{
Daniel Stone37816df2012-05-16 18:45:18 +01003068 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003069}
3070
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003071uint32_t
3072input_get_modifiers(struct input *input)
3073{
3074 return input->modifiers;
3075}
3076
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003077struct widget *
3078input_get_focus_widget(struct input *input)
3079{
3080 return input->focus_widget;
3081}
3082
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003083struct data_offer {
3084 struct wl_data_offer *offer;
3085 struct input *input;
3086 struct wl_array types;
3087 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003088
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003089 struct task io_task;
3090 int fd;
3091 data_func_t func;
3092 int32_t x, y;
3093 void *user_data;
3094};
3095
3096static void
3097data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3098{
3099 struct data_offer *offer = data;
3100 char **p;
3101
3102 p = wl_array_add(&offer->types, sizeof *p);
3103 *p = strdup(type);
3104}
3105
3106static const struct wl_data_offer_listener data_offer_listener = {
3107 data_offer_offer,
3108};
3109
3110static void
3111data_offer_destroy(struct data_offer *offer)
3112{
3113 char **p;
3114
3115 offer->refcount--;
3116 if (offer->refcount == 0) {
3117 wl_data_offer_destroy(offer->offer);
3118 for (p = offer->types.data; *p; p++)
3119 free(*p);
3120 wl_array_release(&offer->types);
3121 free(offer);
3122 }
3123}
3124
3125static void
3126data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003127 struct wl_data_device *data_device,
3128 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003129{
3130 struct data_offer *offer;
3131
3132 offer = malloc(sizeof *offer);
3133
3134 wl_array_init(&offer->types);
3135 offer->refcount = 1;
3136 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003137 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003138 wl_data_offer_add_listener(offer->offer,
3139 &data_offer_listener, offer);
3140}
3141
3142static void
3143data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003144 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003145 wl_fixed_t x_w, wl_fixed_t y_w,
3146 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003147{
3148 struct input *input = data;
3149 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003150 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003151 float x = wl_fixed_to_double(x_w);
3152 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003153 char **p;
3154
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003155 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003156 window = wl_surface_get_user_data(surface);
3157 input->pointer_focus = window;
3158
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003159 if (offer) {
3160 input->drag_offer = wl_data_offer_get_user_data(offer);
3161
3162 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3163 *p = NULL;
3164
3165 types_data = input->drag_offer->types.data;
3166 } else {
3167 input->drag_offer = NULL;
3168 types_data = NULL;
3169 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003170
3171 window = input->pointer_focus;
3172 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003173 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003174 window->user_data);
3175}
3176
3177static void
3178data_device_leave(void *data, struct wl_data_device *data_device)
3179{
3180 struct input *input = data;
3181
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003182 if (input->drag_offer) {
3183 data_offer_destroy(input->drag_offer);
3184 input->drag_offer = NULL;
3185 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003186}
3187
3188static void
3189data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003190 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003191{
3192 struct input *input = data;
3193 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003194 float x = wl_fixed_to_double(x_w);
3195 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003196 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003197
3198 input->sx = x;
3199 input->sy = y;
3200
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003201 if (input->drag_offer)
3202 types_data = input->drag_offer->types.data;
3203 else
3204 types_data = NULL;
3205
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003206 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003207 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003208 window->user_data);
3209}
3210
3211static void
3212data_device_drop(void *data, struct wl_data_device *data_device)
3213{
3214 struct input *input = data;
3215 struct window *window = input->pointer_focus;
3216
3217 if (window->drop_handler)
3218 window->drop_handler(window, input,
3219 input->sx, input->sy, window->user_data);
3220}
3221
3222static void
3223data_device_selection(void *data,
3224 struct wl_data_device *wl_data_device,
3225 struct wl_data_offer *offer)
3226{
3227 struct input *input = data;
3228 char **p;
3229
3230 if (input->selection_offer)
3231 data_offer_destroy(input->selection_offer);
3232
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003233 if (offer) {
3234 input->selection_offer = wl_data_offer_get_user_data(offer);
3235 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3236 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003237 } else {
3238 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003239 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003240}
3241
3242static const struct wl_data_device_listener data_device_listener = {
3243 data_device_data_offer,
3244 data_device_enter,
3245 data_device_leave,
3246 data_device_motion,
3247 data_device_drop,
3248 data_device_selection
3249};
3250
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003251static void
3252input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003253{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003254 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003255 struct wl_cursor *cursor;
3256 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003257
Daniel Stone80972742012-11-07 17:51:39 +11003258 if (!input->pointer)
3259 return;
3260
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003261 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003262 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003263 return;
3264
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003265 if (index >= (int) cursor->image_count) {
3266 fprintf(stderr, "cursor index out of range\n");
3267 return;
3268 }
3269
3270 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003271 buffer = wl_cursor_image_get_buffer(image);
3272 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003273 return;
3274
Kristian Høgsbergae277372012-08-01 09:41:08 -04003275 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003276 input->pointer_surface,
3277 image->hotspot_x, image->hotspot_y);
3278 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3279 wl_surface_damage(input->pointer_surface, 0, 0,
3280 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003281 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003282}
3283
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003284static const struct wl_callback_listener pointer_surface_listener;
3285
3286static void
3287pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3288 uint32_t time)
3289{
3290 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003291 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003292 int i;
3293
3294 if (callback) {
3295 assert(callback == input->cursor_frame_cb);
3296 wl_callback_destroy(callback);
3297 input->cursor_frame_cb = NULL;
3298 }
3299
Daniel Stone80972742012-11-07 17:51:39 +11003300 if (!input->pointer)
3301 return;
3302
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003303 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003304 wl_pointer_set_cursor(input->pointer,
3305 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003306 NULL, 0, 0);
3307 return;
3308 }
3309
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003310 if (input->current_cursor == CURSOR_UNSET)
3311 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003312 cursor = input->display->cursors[input->current_cursor];
3313 if (!cursor)
3314 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003315
3316 /* FIXME We don't have the current time on the first call so we set
3317 * the animation start to the time of the first frame callback. */
3318 if (time == 0)
3319 input->cursor_anim_start = 0;
3320 else if (input->cursor_anim_start == 0)
3321 input->cursor_anim_start = time;
3322
3323 if (time == 0 || input->cursor_anim_start == 0)
3324 i = 0;
3325 else
3326 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3327
Pekka Paalanenbc106382012-10-10 12:49:31 +03003328 if (cursor->image_count > 1) {
3329 input->cursor_frame_cb =
3330 wl_surface_frame(input->pointer_surface);
3331 wl_callback_add_listener(input->cursor_frame_cb,
3332 &pointer_surface_listener, input);
3333 }
3334
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003335 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003336}
3337
3338static const struct wl_callback_listener pointer_surface_listener = {
3339 pointer_surface_frame_callback
3340};
3341
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003342void
3343input_set_pointer_image(struct input *input, int pointer)
3344{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003345 int force = 0;
3346
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003347 if (!input->pointer)
3348 return;
3349
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003350 if (input->pointer_enter_serial > input->cursor_serial)
3351 force = 1;
3352
3353 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003354 return;
3355
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003356 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003357 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003358 if (!input->cursor_frame_cb)
3359 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003360 else if (force) {
3361 /* The current frame callback may be stuck if, for instance,
3362 * the set cursor request was processed by the server after
3363 * this client lost the focus. In this case the cursor surface
3364 * might not be mapped and the frame callback wouldn't ever
3365 * complete. Send a set_cursor and attach to try to map the
3366 * cursor surface again so that the callback will finish */
3367 input_set_pointer_image_index(input, 0);
3368 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003369}
3370
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003371struct wl_data_device *
3372input_get_data_device(struct input *input)
3373{
3374 return input->data_device;
3375}
3376
3377void
3378input_set_selection(struct input *input,
3379 struct wl_data_source *source, uint32_t time)
3380{
3381 wl_data_device_set_selection(input->data_device, source, time);
3382}
3383
3384void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003385input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003386{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003387 wl_data_offer_accept(input->drag_offer->offer,
3388 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003389}
3390
3391static void
3392offer_io_func(struct task *task, uint32_t events)
3393{
3394 struct data_offer *offer =
3395 container_of(task, struct data_offer, io_task);
3396 unsigned int len;
3397 char buffer[4096];
3398
3399 len = read(offer->fd, buffer, sizeof buffer);
3400 offer->func(buffer, len,
3401 offer->x, offer->y, offer->user_data);
3402
3403 if (len == 0) {
3404 close(offer->fd);
3405 data_offer_destroy(offer);
3406 }
3407}
3408
3409static void
3410data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3411 data_func_t func, void *user_data)
3412{
3413 int p[2];
3414
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003415 if (pipe2(p, O_CLOEXEC) == -1)
3416 return;
3417
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003418 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3419 close(p[1]);
3420
3421 offer->io_task.run = offer_io_func;
3422 offer->fd = p[0];
3423 offer->func = func;
3424 offer->refcount++;
3425 offer->user_data = user_data;
3426
3427 display_watch_fd(offer->input->display,
3428 offer->fd, EPOLLIN, &offer->io_task);
3429}
3430
3431void
3432input_receive_drag_data(struct input *input, const char *mime_type,
3433 data_func_t func, void *data)
3434{
3435 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3436 input->drag_offer->x = input->sx;
3437 input->drag_offer->y = input->sy;
3438}
3439
3440int
3441input_receive_selection_data(struct input *input, const char *mime_type,
3442 data_func_t func, void *data)
3443{
3444 char **p;
3445
3446 if (input->selection_offer == NULL)
3447 return -1;
3448
3449 for (p = input->selection_offer->types.data; *p; p++)
3450 if (strcmp(mime_type, *p) == 0)
3451 break;
3452
3453 if (*p == NULL)
3454 return -1;
3455
3456 data_offer_receive_data(input->selection_offer,
3457 mime_type, func, data);
3458 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003459}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003460
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003461int
3462input_receive_selection_data_to_fd(struct input *input,
3463 const char *mime_type, int fd)
3464{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003465 if (input->selection_offer)
3466 wl_data_offer_receive(input->selection_offer->offer,
3467 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003468
3469 return 0;
3470}
3471
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003472void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003473window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003474{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003475 if (!window->shell_surface)
3476 return;
3477
Daniel Stone37816df2012-05-16 18:45:18 +01003478 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003479}
3480
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003481static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003482surface_set_synchronized(struct surface *surface)
3483{
3484 if (!surface->subsurface)
3485 return;
3486
3487 if (surface->synchronized)
3488 return;
3489
3490 wl_subsurface_set_sync(surface->subsurface);
3491 surface->synchronized = 1;
3492}
3493
3494static void
3495surface_set_synchronized_default(struct surface *surface)
3496{
3497 if (!surface->subsurface)
3498 return;
3499
3500 if (surface->synchronized == surface->synchronized_default)
3501 return;
3502
3503 if (surface->synchronized_default)
3504 wl_subsurface_set_sync(surface->subsurface);
3505 else
3506 wl_subsurface_set_desync(surface->subsurface);
3507
3508 surface->synchronized = surface->synchronized_default;
3509}
3510
3511static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003512surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003513{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003514 struct widget *widget = surface->widget;
3515 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003516
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003517 if (surface->input_region) {
3518 wl_region_destroy(surface->input_region);
3519 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003520 }
3521
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003522 if (surface->opaque_region)
3523 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003524
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003525 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003526
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003527 if (widget->resize_handler)
3528 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003529 widget->allocation.width,
3530 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003531 widget->user_data);
3532
Pekka Paalanen35e82632013-04-25 13:57:48 +03003533 if (surface->subsurface &&
3534 (surface->allocation.x != widget->allocation.x ||
3535 surface->allocation.y != widget->allocation.y)) {
3536 wl_subsurface_set_position(surface->subsurface,
3537 widget->allocation.x,
3538 widget->allocation.y);
3539 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003540 if (surface->allocation.width != widget->allocation.width ||
3541 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003542 window_schedule_redraw(widget->window);
3543 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003544 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003545
3546 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003547 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003548 widget->allocation.width,
3549 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003550}
3551
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003552static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003553hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3554{
3555 /*
3556 * This hack should be removed, when EGL respects
3557 * eglSwapInterval(0).
3558 *
3559 * If this window has sub-surfaces, especially a free-running
3560 * EGL-widget, we need to post the parent surface once with
3561 * all the old state to guarantee, that the EGL-widget will
3562 * receive its frame callback soon. Otherwise, a forced call
3563 * to eglSwapBuffers may end up blocking, waiting for a frame
3564 * event that will never come, because we will commit the parent
3565 * surface with all new state only after eglSwapBuffers returns.
3566 *
3567 * This assumes, that:
3568 * 1. When the EGL widget's resize hook is called, it pauses.
3569 * 2. When the EGL widget's redraw hook is called, it forces a
3570 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3571 * In a single threaded application condition 1 is a no-op.
3572 *
3573 * XXX: This should actually be after the surface_resize() calls,
3574 * but cannot, because then it would commit the incomplete state
3575 * accumulated from the widget resize hooks.
3576 */
3577 if (window->subsurface_list.next != &window->main_surface->link ||
3578 window->subsurface_list.prev != &window->main_surface->link)
3579 wl_surface_commit(window->main_surface->surface);
3580}
3581
3582static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003583idle_resize(struct window *window)
3584{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003585 struct surface *surface;
3586
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003587 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003588 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003589
Pekka Paalanen71233882013-04-25 13:57:53 +03003590 DBG("from %dx%d to %dx%d\n",
3591 window->main_surface->server_allocation.width,
3592 window->main_surface->server_allocation.height,
3593 window->pending_allocation.width,
3594 window->pending_allocation.height);
3595
Pekka Paalanene9297f82013-04-25 13:57:51 +03003596 hack_prevent_EGL_sub_surface_deadlock(window);
3597
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003598 widget_set_allocation(window->main_surface->widget,
3599 window->pending_allocation.x,
3600 window->pending_allocation.y,
3601 window->pending_allocation.width,
3602 window->pending_allocation.height);
3603
3604 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003605
3606 /* The main surface is in the list, too. Main surface's
3607 * resize_handler is responsible for calling widget_set_allocation()
3608 * on all sub-surface root widgets, so they will be resized
3609 * properly.
3610 */
3611 wl_list_for_each(surface, &window->subsurface_list, link) {
3612 if (surface == window->main_surface)
3613 continue;
3614
3615 surface_set_synchronized(surface);
3616 surface_resize(surface);
3617 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003618}
3619
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003620void
3621window_schedule_resize(struct window *window, int width, int height)
3622{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003623 window->pending_allocation.x = 0;
3624 window->pending_allocation.y = 0;
3625 window->pending_allocation.width = width;
3626 window->pending_allocation.height = height;
3627
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003628 if (window->min_allocation.width == 0)
3629 window->min_allocation = window->pending_allocation;
3630 if (window->pending_allocation.width < window->min_allocation.width)
3631 window->pending_allocation.width = window->min_allocation.width;
3632 if (window->pending_allocation.height < window->min_allocation.height)
3633 window->pending_allocation.height = window->min_allocation.height;
3634
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003635 window->resize_needed = 1;
3636 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003637}
3638
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003639void
3640widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3641{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003642 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003643}
3644
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003645static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003646handle_ping(void *data, struct wl_shell_surface *shell_surface,
3647 uint32_t serial)
3648{
3649 wl_shell_surface_pong(shell_surface, serial);
3650}
3651
3652static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003653handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003654 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003655{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003656 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003657
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003658 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003659 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003660}
3661
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003662static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003663menu_destroy(struct menu *menu)
3664{
3665 widget_destroy(menu->widget);
3666 window_destroy(menu->window);
3667 free(menu);
3668}
3669
3670static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003671handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3672{
3673 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003674 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003675
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003676 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003677 * device. Or just use wl_callback. And this really needs to
3678 * be a window vfunc that the menu can set. And we need the
3679 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003680
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003681 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003682 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003683}
3684
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003685static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003686 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003687 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003688 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003689};
3690
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003691void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003692window_get_allocation(struct window *window,
3693 struct rectangle *allocation)
3694{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003695 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003696}
3697
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003698static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003699widget_redraw(struct widget *widget)
3700{
3701 struct widget *child;
3702
3703 if (widget->redraw_handler)
3704 widget->redraw_handler(widget, widget->user_data);
3705 wl_list_for_each(child, &widget->child_list, link)
3706 widget_redraw(child);
3707}
3708
3709static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003710frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3711{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003712 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003713
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003714 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003715 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003716 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003717 surface->frame_cb = NULL;
3718
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003719 surface->last_time = time;
3720
Pekka Paalanen71233882013-04-25 13:57:53 +03003721 if (surface->redraw_needed || surface->window->redraw_needed) {
3722 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003723 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003724 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003725}
3726
3727static const struct wl_callback_listener listener = {
3728 frame_callback
3729};
3730
3731static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003732surface_redraw(struct surface *surface)
3733{
Pekka Paalanen71233882013-04-25 13:57:53 +03003734 DBG_OBJ(surface->surface, "begin\n");
3735
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003736 if (!surface->window->redraw_needed && !surface->redraw_needed)
3737 return;
3738
3739 /* Whole-window redraw forces a redraw even if the previous has
3740 * not yet hit the screen.
3741 */
3742 if (surface->frame_cb) {
3743 if (!surface->window->redraw_needed)
3744 return;
3745
Pekka Paalanen71233882013-04-25 13:57:53 +03003746 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003747 wl_callback_destroy(surface->frame_cb);
3748 }
3749
3750 surface->frame_cb = wl_surface_frame(surface->surface);
3751 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003752 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003753
3754 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003755 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003756 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003757 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003758}
3759
3760static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003761idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003762{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003763 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003764 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003765
Pekka Paalanen71233882013-04-25 13:57:53 +03003766 DBG(" --------- \n");
3767
Pekka Paalaneneebff542013-04-25 13:57:52 +03003768 wl_list_init(&window->redraw_task.link);
3769 window->redraw_task_scheduled = 0;
3770
3771 if (window->resize_needed) {
3772 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003773 if (window->main_surface->frame_cb) {
3774 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003775 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003776 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003777
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003778 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003779 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003780
Pekka Paalanen35e82632013-04-25 13:57:48 +03003781 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003782 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003783
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003784 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003785 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003786
3787 wl_list_for_each(surface, &window->subsurface_list, link)
3788 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003789}
3790
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003791static void
3792window_schedule_redraw_task(struct window *window)
3793{
3794 if (window->configure_requests)
3795 return;
3796 if (!window->redraw_task_scheduled) {
3797 window->redraw_task.run = idle_redraw;
3798 display_defer(window->display, &window->redraw_task);
3799 window->redraw_task_scheduled = 1;
3800 }
3801}
3802
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003803void
3804window_schedule_redraw(struct window *window)
3805{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003806 struct surface *surface;
3807
Pekka Paalanen71233882013-04-25 13:57:53 +03003808 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3809
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003810 wl_list_for_each(surface, &window->subsurface_list, link)
3811 surface->redraw_needed = 1;
3812
3813 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003814}
3815
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003816int
3817window_is_fullscreen(struct window *window)
3818{
3819 return window->type == TYPE_FULLSCREEN;
3820}
3821
MoD063a8822013-03-02 23:43:57 +00003822static void
3823configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3824{
3825 struct window *window = data;
3826
3827 wl_callback_destroy(callback);
3828 window->configure_requests--;
3829
3830 if (!window->configure_requests)
3831 window_schedule_redraw(window);
3832}
3833
3834static struct wl_callback_listener configure_request_listener = {
3835 configure_request_completed,
3836};
3837
3838static void
3839window_defer_redraw_until_configure(struct window* window)
3840{
3841 struct wl_callback *callback;
3842
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003843 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003844 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003845 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003846 }
3847
3848 callback = wl_display_sync(window->display->display);
3849 wl_callback_add_listener(callback, &configure_request_listener, window);
3850 window->configure_requests++;
3851}
3852
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003853void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003854window_set_fullscreen(struct window *window, int fullscreen)
3855{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003856 if (!window->display->shell)
3857 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003858
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003859 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003860 return;
3861
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003862 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003863 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003864 if (window->type == TYPE_TOPLEVEL) {
3865 window->saved_allocation = window->main_surface->allocation;
3866 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003867 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003868 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003869 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003870 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003871 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003872 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003873 if (window->saved_type == TYPE_MAXIMIZED) {
3874 window_set_maximized(window, 1);
3875 } else {
3876 window->type = TYPE_TOPLEVEL;
3877 wl_shell_surface_set_toplevel(window->shell_surface);
3878 window_schedule_resize(window,
3879 window->saved_allocation.width,
3880 window->saved_allocation.height);
3881 }
3882
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003883 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003884}
3885
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003886void
3887window_set_fullscreen_method(struct window *window,
3888 enum wl_shell_surface_fullscreen_method method)
3889{
3890 window->fullscreen_method = method;
3891}
3892
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003893int
3894window_is_maximized(struct window *window)
3895{
3896 return window->type == TYPE_MAXIMIZED;
3897}
3898
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003899void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003900window_set_maximized(struct window *window, int maximized)
3901{
3902 if (!window->display->shell)
3903 return;
3904
3905 if ((window->type == TYPE_MAXIMIZED) == maximized)
3906 return;
3907
3908 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003909 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003910 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3911 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003912 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003913 } else if (window->type == TYPE_FULLSCREEN) {
3914 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3915 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003916 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003917 } else {
3918 wl_shell_surface_set_toplevel(window->shell_surface);
3919 window->type = TYPE_TOPLEVEL;
3920 window_schedule_resize(window,
3921 window->saved_allocation.width,
3922 window->saved_allocation.height);
3923 }
3924}
3925
3926void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003927window_set_user_data(struct window *window, void *data)
3928{
3929 window->user_data = data;
3930}
3931
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003932void *
3933window_get_user_data(struct window *window)
3934{
3935 return window->user_data;
3936}
3937
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003938void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003939window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003940 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003941{
3942 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003943}
3944
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003945void
3946window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003947 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003948{
3949 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003950}
3951
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003952void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003953window_set_data_handler(struct window *window, window_data_handler_t handler)
3954{
3955 window->data_handler = handler;
3956}
3957
3958void
3959window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3960{
3961 window->drop_handler = handler;
3962}
3963
3964void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003965window_set_close_handler(struct window *window,
3966 window_close_handler_t handler)
3967{
3968 window->close_handler = handler;
3969}
3970
3971void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003972window_set_fullscreen_handler(struct window *window,
3973 window_fullscreen_handler_t handler)
3974{
3975 window->fullscreen_handler = handler;
3976}
3977
3978void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003979window_set_output_handler(struct window *window,
3980 window_output_handler_t handler)
3981{
3982 window->output_handler = handler;
3983}
3984
3985void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003986window_set_title(struct window *window, const char *title)
3987{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003988 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003989 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003990 if (window->shell_surface)
3991 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003992}
3993
3994const char *
3995window_get_title(struct window *window)
3996{
3997 return window->title;
3998}
3999
4000void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004001window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4002{
4003 struct text_cursor_position *text_cursor_position =
4004 window->display->text_cursor_position;
4005
Scott Moreau9295ce02012-06-01 12:46:10 -06004006 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004007 return;
4008
4009 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004010 window->main_surface->surface,
4011 wl_fixed_from_int(x),
4012 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004013}
4014
4015void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004016window_damage(struct window *window, int32_t x, int32_t y,
4017 int32_t width, int32_t height)
4018{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004019 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004020}
4021
Casey Dahlin9074db52012-04-19 22:50:09 -04004022static void
4023surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004024 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004025{
Rob Bradford7507b572012-05-15 17:55:34 +01004026 struct window *window = data;
4027 struct output *output;
4028 struct output *output_found = NULL;
4029 struct window_output *window_output;
4030
4031 wl_list_for_each(output, &window->display->output_list, link) {
4032 if (output->output == wl_output) {
4033 output_found = output;
4034 break;
4035 }
4036 }
4037
4038 if (!output_found)
4039 return;
4040
4041 window_output = malloc (sizeof *window_output);
4042 window_output->output = output_found;
4043
4044 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004045
4046 if (window->output_handler)
4047 window->output_handler(window, output_found, 1,
4048 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004049}
4050
4051static void
4052surface_leave(void *data,
4053 struct wl_surface *wl_surface, struct wl_output *output)
4054{
Rob Bradford7507b572012-05-15 17:55:34 +01004055 struct window *window = data;
4056 struct window_output *window_output;
4057 struct window_output *window_output_found = NULL;
4058
4059 wl_list_for_each(window_output, &window->window_output_list, link) {
4060 if (window_output->output->output == output) {
4061 window_output_found = window_output;
4062 break;
4063 }
4064 }
4065
4066 if (window_output_found) {
4067 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004068
4069 if (window->output_handler)
4070 window->output_handler(window, window_output->output,
4071 0, window->user_data);
4072
Rob Bradford7507b572012-05-15 17:55:34 +01004073 free(window_output_found);
4074 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004075}
4076
4077static const struct wl_surface_listener surface_listener = {
4078 surface_enter,
4079 surface_leave
4080};
4081
Pekka Paalanen4e373742013-02-13 16:17:13 +02004082static struct surface *
4083surface_create(struct window *window)
4084{
4085 struct display *display = window->display;
4086 struct surface *surface;
4087
4088 surface = calloc(1, sizeof *surface);
4089 if (!surface)
4090 return NULL;
4091
4092 surface->window = window;
4093 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004094 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004095 wl_surface_add_listener(surface->surface, &surface_listener, window);
4096
Pekka Paalanen35e82632013-04-25 13:57:48 +03004097 wl_list_insert(&window->subsurface_list, &surface->link);
4098
Pekka Paalanen4e373742013-02-13 16:17:13 +02004099 return surface;
4100}
4101
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004102static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004103window_create_internal(struct display *display,
4104 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004105{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004106 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004107 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004108
4109 window = malloc(sizeof *window);
4110 if (window == NULL)
4111 return NULL;
4112
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004113 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004114 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004115 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004116 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004117
4118 surface = surface_create(window);
4119 window->main_surface = surface;
4120
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004121 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004122 window->shell_surface =
4123 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004124 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004125 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004126
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004127 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004128 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004129 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004130
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004131 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004132#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004133 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004134#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004135 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004136#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004137 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004138 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004139
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004140 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004141 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004142 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004143
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004144 if (window->shell_surface) {
4145 wl_shell_surface_set_user_data(window->shell_surface, window);
4146 wl_shell_surface_add_listener(window->shell_surface,
4147 &shell_surface_listener, window);
4148 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004149
Rob Bradford7507b572012-05-15 17:55:34 +01004150 wl_list_init (&window->window_output_list);
4151
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004152 return window;
4153}
4154
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004155struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004156window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004157{
4158 struct window *window;
4159
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004160 window = window_create_internal(display, NULL, TYPE_NONE);
4161 if (!window)
4162 return NULL;
4163
4164 return window;
4165}
4166
4167struct window *
4168window_create_custom(struct display *display)
4169{
4170 struct window *window;
4171
4172 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004173 if (!window)
4174 return NULL;
4175
4176 return window;
4177}
4178
4179struct window *
4180window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004181 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004182{
4183 struct window *window;
4184
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004185 window = window_create_internal(parent->display,
4186 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004187 if (!window)
4188 return NULL;
4189
4190 window->x = x;
4191 window->y = y;
4192
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004193 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004194 wl_shell_surface_set_transient(
4195 window->shell_surface,
4196 window->parent->main_surface->surface,
4197 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004198
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004199 return window;
4200}
4201
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004202static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004203menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004204{
4205 int next;
4206
4207 next = (sy - 8) / 20;
4208 if (menu->current != next) {
4209 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004210 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004211 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004212}
4213
4214static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004215menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004216 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004217 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004218{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004219 struct menu *menu = data;
4220
4221 if (widget == menu->widget)
4222 menu_set_item(data, y);
4223
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004224 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004225}
4226
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004227static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004228menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004229 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004230{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004231 struct menu *menu = data;
4232
4233 if (widget == menu->widget)
4234 menu_set_item(data, y);
4235
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004236 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004237}
4238
4239static void
4240menu_leave_handler(struct widget *widget, struct input *input, void *data)
4241{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004242 struct menu *menu = data;
4243
4244 if (widget == menu->widget)
4245 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004246}
4247
4248static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004249menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004250 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004251 uint32_t button, enum wl_pointer_button_state state,
4252 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004253
4254{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004255 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004256
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004257 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4258 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004259 /* Either relase after press-drag-release or
4260 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004261 menu->func(menu->window->parent,
4262 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004263 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004264 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004265 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004266 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004267 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004268}
4269
4270static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004271menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004272{
4273 cairo_t *cr;
4274 const int32_t r = 3, margin = 3;
4275 struct menu *menu = data;
4276 int32_t width, height, i;
4277
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004278 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004279 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4280 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4281 cairo_paint(cr);
4282
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004283 width = widget->allocation.width;
4284 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004285 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004286
4287 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004288 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4289 cairo_fill(cr);
4290
4291 for (i = 0; i < menu->count; i++) {
4292 if (i == menu->current) {
4293 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4294 cairo_rectangle(cr, margin, i * 20 + margin,
4295 width - 2 * margin, 20);
4296 cairo_fill(cr);
4297 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4298 cairo_move_to(cr, 10, i * 20 + 16);
4299 cairo_show_text(cr, menu->entries[i]);
4300 } else {
4301 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4302 cairo_move_to(cr, 10, i * 20 + 16);
4303 cairo_show_text(cr, menu->entries[i]);
4304 }
4305 }
4306
4307 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004308}
4309
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004310void
4311window_show_menu(struct display *display,
4312 struct input *input, uint32_t time, struct window *parent,
4313 int32_t x, int32_t y,
4314 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004315{
4316 struct window *window;
4317 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004318 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004319
4320 menu = malloc(sizeof *menu);
4321 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004322 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004323
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004324 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004325 if (!window) {
4326 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004327 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004328 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004329
4330 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004331 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004332 menu->entries = entries;
4333 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004334 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004335 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004336 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004337 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004338 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004339 window->type = TYPE_MENU;
4340 window->x = x;
4341 window->y = y;
4342
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004343 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004344 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004345 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004346 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004347 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004348
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004349 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004350 widget_set_enter_handler(menu->widget, menu_enter_handler);
4351 widget_set_leave_handler(menu->widget, menu_leave_handler);
4352 widget_set_motion_handler(menu->widget, menu_motion_handler);
4353 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004354
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004355 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004356 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004357}
4358
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004359void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004360window_set_buffer_type(struct window *window, enum window_buffer_type type)
4361{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004362 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004363}
4364
Pekka Paalanen35e82632013-04-25 13:57:48 +03004365struct widget *
4366window_add_subsurface(struct window *window, void *data,
4367 enum subsurface_mode default_mode)
4368{
4369 struct widget *widget;
4370 struct surface *surface;
4371 struct wl_surface *parent;
4372 struct wl_subcompositor *subcompo = window->display->subcompositor;
4373
4374 if (!subcompo)
4375 return NULL;
4376
4377 surface = surface_create(window);
4378 widget = widget_create(window, surface, data);
4379 wl_list_init(&widget->link);
4380 surface->widget = widget;
4381
4382 parent = window->main_surface->surface;
4383 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4384 surface->surface,
4385 parent);
4386 surface->synchronized = 1;
4387
4388 switch (default_mode) {
4389 case SUBSURFACE_SYNCHRONIZED:
4390 surface->synchronized_default = 1;
4391 break;
4392 case SUBSURFACE_DESYNCHRONIZED:
4393 surface->synchronized_default = 0;
4394 break;
4395 default:
4396 assert(!"bad enum subsurface_mode");
4397 }
4398
4399 return widget;
4400}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004401
4402static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004403display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004404 struct wl_output *wl_output,
4405 int x, int y,
4406 int physical_width,
4407 int physical_height,
4408 int subpixel,
4409 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004410 const char *model,
4411 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004412{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004413 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004414
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004415 output->allocation.x = x;
4416 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004417 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004418}
4419
4420static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004421display_handle_done(void *data,
4422 struct wl_output *wl_output)
4423{
4424}
4425
4426static void
4427display_handle_scale(void *data,
4428 struct wl_output *wl_output,
4429 uint32_t scale)
4430{
4431 struct output *output = data;
4432
4433 output->scale = scale;
4434}
4435
4436static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004437display_handle_mode(void *data,
4438 struct wl_output *wl_output,
4439 uint32_t flags,
4440 int width,
4441 int height,
4442 int refresh)
4443{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004444 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004445 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004446
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004447 if (flags & WL_OUTPUT_MODE_CURRENT) {
4448 output->allocation.width = width;
4449 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004450 if (display->output_configure_handler)
4451 (*display->output_configure_handler)(
4452 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004453 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004454}
4455
4456static const struct wl_output_listener output_listener = {
4457 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004458 display_handle_mode,
4459 display_handle_done,
4460 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004461};
4462
4463static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004464display_add_output(struct display *d, uint32_t id)
4465{
4466 struct output *output;
4467
4468 output = malloc(sizeof *output);
4469 if (output == NULL)
4470 return;
4471
4472 memset(output, 0, sizeof *output);
4473 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004474 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004475 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004476 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004477 wl_list_insert(d->output_list.prev, &output->link);
4478
4479 wl_output_add_listener(output->output, &output_listener, output);
4480}
4481
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004482static void
4483output_destroy(struct output *output)
4484{
4485 if (output->destroy_handler)
4486 (*output->destroy_handler)(output, output->user_data);
4487
4488 wl_output_destroy(output->output);
4489 wl_list_remove(&output->link);
4490 free(output);
4491}
4492
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004493void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004494display_set_global_handler(struct display *display,
4495 display_global_handler_t handler)
4496{
4497 struct global *global;
4498
4499 display->global_handler = handler;
4500 if (!handler)
4501 return;
4502
4503 wl_list_for_each(global, &display->global_list, link)
4504 display->global_handler(display,
4505 global->name, global->interface,
4506 global->version, display->user_data);
4507}
4508
4509void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004510display_set_output_configure_handler(struct display *display,
4511 display_output_handler_t handler)
4512{
4513 struct output *output;
4514
4515 display->output_configure_handler = handler;
4516 if (!handler)
4517 return;
4518
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004519 wl_list_for_each(output, &display->output_list, link) {
4520 if (output->allocation.width == 0 &&
4521 output->allocation.height == 0)
4522 continue;
4523
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004524 (*display->output_configure_handler)(output,
4525 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004526 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004527}
4528
4529void
4530output_set_user_data(struct output *output, void *data)
4531{
4532 output->user_data = data;
4533}
4534
4535void *
4536output_get_user_data(struct output *output)
4537{
4538 return output->user_data;
4539}
4540
4541void
4542output_set_destroy_handler(struct output *output,
4543 display_output_handler_t handler)
4544{
4545 output->destroy_handler = handler;
4546 /* FIXME: implement this, once we have way to remove outputs */
4547}
4548
4549void
Scott Moreau4e072362012-09-29 02:03:11 -06004550output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004551{
Scott Moreau4e072362012-09-29 02:03:11 -06004552 struct rectangle allocation = output->allocation;
4553
4554 switch (output->transform) {
4555 case WL_OUTPUT_TRANSFORM_90:
4556 case WL_OUTPUT_TRANSFORM_270:
4557 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4558 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4559 /* Swap width and height */
4560 allocation.width = output->allocation.height;
4561 allocation.height = output->allocation.width;
4562 break;
4563 }
4564
4565 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004566}
4567
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004568struct wl_output *
4569output_get_wl_output(struct output *output)
4570{
4571 return output->output;
4572}
4573
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004574enum wl_output_transform
4575output_get_transform(struct output *output)
4576{
4577 return output->transform;
4578}
4579
Alexander Larssonafd319a2013-05-22 14:41:27 +02004580uint32_t
4581output_get_scale(struct output *output)
4582{
4583 return output->scale;
4584}
4585
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004586static void
Daniel Stone97f68542012-05-30 16:32:01 +01004587fini_xkb(struct input *input)
4588{
4589 xkb_state_unref(input->xkb.state);
4590 xkb_map_unref(input->xkb.keymap);
4591}
4592
4593static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004594display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004595{
4596 struct input *input;
4597
4598 input = malloc(sizeof *input);
4599 if (input == NULL)
4600 return;
4601
4602 memset(input, 0, sizeof *input);
4603 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004604 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004605 input->pointer_focus = NULL;
4606 input->keyboard_focus = NULL;
4607 wl_list_insert(d->input_list.prev, &input->link);
4608
Daniel Stone37816df2012-05-16 18:45:18 +01004609 wl_seat_add_listener(input->seat, &seat_listener, input);
4610 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004611
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004612 input->data_device =
4613 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004614 input->seat);
4615 wl_data_device_add_listener(input->data_device, &data_device_listener,
4616 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004617
4618 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004619
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004620 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4621 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004622 input->repeat_task.run = keyboard_repeat_func;
4623 display_watch_fd(d, input->repeat_timer_fd,
4624 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004625}
4626
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004627static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004628input_destroy(struct input *input)
4629{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004630 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004631 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004632
4633 if (input->drag_offer)
4634 data_offer_destroy(input->drag_offer);
4635
4636 if (input->selection_offer)
4637 data_offer_destroy(input->selection_offer);
4638
4639 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004640 fini_xkb(input);
4641
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004642 wl_surface_destroy(input->pointer_surface);
4643
Pekka Paalanene1207c72011-12-16 12:02:09 +02004644 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004645 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004646 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004647 free(input);
4648}
4649
4650static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004651init_workspace_manager(struct display *d, uint32_t id)
4652{
4653 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004654 wl_registry_bind(d->registry, id,
4655 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004656 if (d->workspace_manager != NULL)
4657 workspace_manager_add_listener(d->workspace_manager,
4658 &workspace_manager_listener,
4659 d);
4660}
4661
4662static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004663registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4664 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004665{
4666 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004667 struct global *global;
4668
4669 global = malloc(sizeof *global);
4670 global->name = id;
4671 global->interface = strdup(interface);
4672 global->version = version;
4673 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004674
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004675 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004676 d->compositor = wl_registry_bind(registry, id,
4677 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004678 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004679 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004680 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004681 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004682 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004683 d->shell = wl_registry_bind(registry,
4684 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004685 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004686 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004687 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4688 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004689 wl_registry_bind(registry, id,
4690 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004691 } else if (strcmp(interface, "text_cursor_position") == 0) {
4692 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004693 wl_registry_bind(registry, id,
4694 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004695 } else if (strcmp(interface, "workspace_manager") == 0) {
4696 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004697 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4698 d->subcompositor =
4699 wl_registry_bind(registry, id,
4700 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004701 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004702
4703 if (d->global_handler)
4704 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004705}
4706
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004707static void
4708registry_handle_global_remove(void *data, struct wl_registry *registry,
4709 uint32_t name)
4710{
4711 struct display *d = data;
4712 struct global *global;
4713 struct global *tmp;
4714
4715 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4716 if (global->name != name)
4717 continue;
4718
4719 /* XXX: Should destroy bound globals, and call
4720 * the counterpart of display::global_handler
4721 */
4722 wl_list_remove(&global->link);
4723 free(global->interface);
4724 free(global);
4725 }
4726}
4727
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004728void *
4729display_bind(struct display *display, uint32_t name,
4730 const struct wl_interface *interface, uint32_t version)
4731{
4732 return wl_registry_bind(display->registry, name, interface, version);
4733}
4734
4735static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004736 registry_handle_global,
4737 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004738};
4739
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004740#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004741static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004742init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004743{
4744 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004745 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004746
Rob Clark6396ed32012-03-11 19:48:41 -05004747#ifdef USE_CAIRO_GLESV2
4748# define GL_BIT EGL_OPENGL_ES2_BIT
4749#else
4750# define GL_BIT EGL_OPENGL_BIT
4751#endif
4752
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004753 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004754 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004755 EGL_RED_SIZE, 1,
4756 EGL_GREEN_SIZE, 1,
4757 EGL_BLUE_SIZE, 1,
4758 EGL_ALPHA_SIZE, 1,
4759 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004760 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004761 EGL_NONE
4762 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004763
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004764#ifdef USE_CAIRO_GLESV2
4765 static const EGLint context_attribs[] = {
4766 EGL_CONTEXT_CLIENT_VERSION, 2,
4767 EGL_NONE
4768 };
4769 EGLint api = EGL_OPENGL_ES_API;
4770#else
4771 EGLint *context_attribs = NULL;
4772 EGLint api = EGL_OPENGL_API;
4773#endif
4774
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004775 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004776 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004777 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004778 return -1;
4779 }
4780
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004781 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004782 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004783 return -1;
4784 }
4785
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004786 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4787 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004788 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004789 return -1;
4790 }
4791
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004792 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004793 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004794 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004795 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004796 return -1;
4797 }
4798
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004799 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004800 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004801 return -1;
4802 }
4803
Benjamin Franzke0c991632011-09-27 21:57:31 +02004804 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4805 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004806 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004807 return -1;
4808 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004809
4810 return 0;
4811}
4812
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004813static void
4814fini_egl(struct display *display)
4815{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004816 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004817
4818 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4819 EGL_NO_CONTEXT);
4820
4821 eglTerminate(display->dpy);
4822 eglReleaseThread();
4823}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004824#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004825
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004826static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004827init_dummy_surface(struct display *display)
4828{
4829 int len;
4830 void *data;
4831
4832 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4833 data = malloc(len);
4834 display->dummy_surface =
4835 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4836 1, 1, len);
4837 display->dummy_surface_data = data;
4838}
4839
4840static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004841handle_display_data(struct task *task, uint32_t events)
4842{
4843 struct display *display =
4844 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004845 struct epoll_event ep;
4846 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004847
4848 display->display_fd_events = events;
4849
4850 if (events & EPOLLERR || events & EPOLLHUP) {
4851 display_exit(display);
4852 return;
4853 }
4854
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004855 if (events & EPOLLIN) {
4856 ret = wl_display_dispatch(display->display);
4857 if (ret == -1) {
4858 display_exit(display);
4859 return;
4860 }
4861 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004862
4863 if (events & EPOLLOUT) {
4864 ret = wl_display_flush(display->display);
4865 if (ret == 0) {
4866 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4867 ep.data.ptr = &display->display_task;
4868 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4869 display->display_fd, &ep);
4870 } else if (ret == -1 && errno != EAGAIN) {
4871 display_exit(display);
4872 return;
4873 }
4874 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004875}
4876
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004877static void
4878log_handler(const char *format, va_list args)
4879{
4880 vfprintf(stderr, format, args);
4881}
4882
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004883struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004884display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004885{
4886 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004887
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004888 wl_log_set_handler_client(log_handler);
4889
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004890 d = malloc(sizeof *d);
4891 if (d == NULL)
4892 return NULL;
4893
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004894 memset(d, 0, sizeof *d);
4895
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004896 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004897 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004898 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004899 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004900 return NULL;
4901 }
4902
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004903 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004904 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004905 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004906 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4907 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004908
4909 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004910 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004911 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004912 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004913
Daniel Stone97f68542012-05-30 16:32:01 +01004914 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004915 if (d->xkb_context == NULL) {
4916 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004917 return NULL;
4918 }
4919
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004920 d->workspace = 0;
4921 d->workspace_count = 1;
4922
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004923 d->registry = wl_display_get_registry(d->display);
4924 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004925
4926 if (wl_display_dispatch(d->display) < 0) {
4927 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4928 return NULL;
4929 }
4930
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004931#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004932 if (init_egl(d) < 0)
4933 fprintf(stderr, "EGL does not seem to work, "
4934 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004935#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004936
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004937 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004938
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004939 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004940
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004941 wl_list_init(&d->window_list);
4942
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004943 init_dummy_surface(d);
4944
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004945 return d;
4946}
4947
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004948static void
4949display_destroy_outputs(struct display *display)
4950{
4951 struct output *tmp;
4952 struct output *output;
4953
4954 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4955 output_destroy(output);
4956}
4957
Pekka Paalanene1207c72011-12-16 12:02:09 +02004958static void
4959display_destroy_inputs(struct display *display)
4960{
4961 struct input *tmp;
4962 struct input *input;
4963
4964 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4965 input_destroy(input);
4966}
4967
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004968void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004969display_destroy(struct display *display)
4970{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004971 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004972 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4973 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004974
4975 if (!wl_list_empty(&display->deferred_list))
4976 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4977
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004978 cairo_surface_destroy(display->dummy_surface);
4979 free(display->dummy_surface_data);
4980
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004981 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004982 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004983
Daniel Stone97f68542012-05-30 16:32:01 +01004984 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004985
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004986 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004987 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004988
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004989#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004990 if (display->argb_device)
4991 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004992#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004993
Pekka Paalanen35e82632013-04-25 13:57:48 +03004994 if (display->subcompositor)
4995 wl_subcompositor_destroy(display->subcompositor);
4996
Pekka Paalanenc2052982011-12-16 11:41:32 +02004997 if (display->shell)
4998 wl_shell_destroy(display->shell);
4999
5000 if (display->shm)
5001 wl_shm_destroy(display->shm);
5002
5003 if (display->data_device_manager)
5004 wl_data_device_manager_destroy(display->data_device_manager);
5005
5006 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005007 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005008
5009 close(display->epoll_fd);
5010
U. Artie Eoff44874d92012-10-02 21:12:35 -07005011 if (!(display->display_fd_events & EPOLLERR) &&
5012 !(display->display_fd_events & EPOLLHUP))
5013 wl_display_flush(display->display);
5014
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005015 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005016 free(display);
5017}
5018
5019void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005020display_set_user_data(struct display *display, void *data)
5021{
5022 display->user_data = data;
5023}
5024
5025void *
5026display_get_user_data(struct display *display)
5027{
5028 return display->user_data;
5029}
5030
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005031struct wl_display *
5032display_get_display(struct display *display)
5033{
5034 return display->display;
5035}
5036
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005037struct output *
5038display_get_output(struct display *display)
5039{
5040 return container_of(display->output_list.next, struct output, link);
5041}
5042
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005043struct wl_compositor *
5044display_get_compositor(struct display *display)
5045{
5046 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005047}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005048
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005049uint32_t
5050display_get_serial(struct display *display)
5051{
5052 return display->serial;
5053}
5054
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005055EGLDisplay
5056display_get_egl_display(struct display *d)
5057{
5058 return d->dpy;
5059}
5060
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005061struct wl_data_source *
5062display_create_data_source(struct display *display)
5063{
5064 return wl_data_device_manager_create_data_source(display->data_device_manager);
5065}
5066
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005067EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005068display_get_argb_egl_config(struct display *d)
5069{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005070 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005071}
5072
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005073struct wl_shell *
5074display_get_shell(struct display *display)
5075{
5076 return display->shell;
5077}
5078
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005079int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005080display_acquire_window_surface(struct display *display,
5081 struct window *window,
5082 EGLContext ctx)
5083{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005084 struct surface *surface = window->main_surface;
5085
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005086 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005087 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005088
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005089 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005090 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005091}
5092
5093void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005094display_release_window_surface(struct display *display,
5095 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005096{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005097 struct surface *surface = window->main_surface;
5098
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005099 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005100 return;
5101
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005102 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005103}
5104
5105void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005106display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005107{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005108 wl_list_insert(&display->deferred_list, &task->link);
5109}
5110
5111void
5112display_watch_fd(struct display *display,
5113 int fd, uint32_t events, struct task *task)
5114{
5115 struct epoll_event ep;
5116
5117 ep.events = events;
5118 ep.data.ptr = task;
5119 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5120}
5121
5122void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005123display_unwatch_fd(struct display *display, int fd)
5124{
5125 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5126}
5127
5128void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005129display_run(struct display *display)
5130{
5131 struct task *task;
5132 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005133 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005134
Pekka Paalanen826d7952011-12-15 10:14:07 +02005135 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005136 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005137 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005138 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005139 struct task, link);
5140 wl_list_remove(&task->link);
5141 task->run(task, 0);
5142 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005143
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005144 wl_display_dispatch_pending(display->display);
5145
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005146 if (!display->running)
5147 break;
5148
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005149 ret = wl_display_flush(display->display);
5150 if (ret < 0 && errno == EAGAIN) {
5151 ep[0].events =
5152 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5153 ep[0].data.ptr = &display->display_task;
5154
5155 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5156 display->display_fd, &ep[0]);
5157 } else if (ret < 0) {
5158 break;
5159 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005160
5161 count = epoll_wait(display->epoll_fd,
5162 ep, ARRAY_LENGTH(ep), -1);
5163 for (i = 0; i < count; i++) {
5164 task = ep[i].data.ptr;
5165 task->run(task, ep[i].events);
5166 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005167 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005168}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005169
5170void
5171display_exit(struct display *display)
5172{
5173 display->running = 0;
5174}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005175
5176void
5177keysym_modifiers_add(struct wl_array *modifiers_map,
5178 const char *name)
5179{
5180 size_t len = strlen(name) + 1;
5181 char *p;
5182
5183 p = wl_array_add(modifiers_map, len);
5184
5185 if (p == NULL)
5186 return;
5187
5188 strncpy(p, name, len);
5189}
5190
5191static xkb_mod_index_t
5192keysym_modifiers_get_index(struct wl_array *modifiers_map,
5193 const char *name)
5194{
5195 xkb_mod_index_t index = 0;
5196 char *p = modifiers_map->data;
5197
5198 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5199 if (strcmp(p, name) == 0)
5200 return index;
5201
5202 index++;
5203 p += strlen(p) + 1;
5204 }
5205
5206 return XKB_MOD_INVALID;
5207}
5208
5209xkb_mod_mask_t
5210keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5211 const char *name)
5212{
5213 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5214
5215 if (index == XKB_MOD_INVALID)
5216 return XKB_MOD_INVALID;
5217
5218 return 1 << index;
5219}