blob: 858ba779d2228166ea1d194ebca9389e64c42380 [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;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001653 uint32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001654
1655 surface_width = surface->allocation.width;
1656 surface_height = surface->allocation.height;
1657
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001658 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001659 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001660
Alexander Larsson15901f02013-05-22 14:41:25 +02001661 switch (transform) {
1662 case WL_OUTPUT_TRANSFORM_FLIPPED:
1663 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1664 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1665 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1666 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1667 break;
1668 default:
1669 cairo_matrix_init_identity(&m);
1670 break;
1671 }
1672
1673 switch (transform) {
1674 case WL_OUTPUT_TRANSFORM_NORMAL:
1675 default:
1676 angle = 0;
1677 translate_x = 0;
1678 translate_y = 0;
1679 break;
1680 case WL_OUTPUT_TRANSFORM_FLIPPED:
1681 angle = 0;
1682 translate_x = surface_width;
1683 translate_y = 0;
1684 break;
1685 case WL_OUTPUT_TRANSFORM_90:
1686 angle = M_PI_2;
1687 translate_x = surface_height;
1688 translate_y = 0;
1689 break;
1690 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1691 angle = M_PI_2;
1692 translate_x = surface_height;
1693 translate_y = surface_width;
1694 break;
1695 case WL_OUTPUT_TRANSFORM_180:
1696 angle = M_PI;
1697 translate_x = surface_width;
1698 translate_y = surface_height;
1699 break;
1700 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1701 angle = M_PI;
1702 translate_x = 0;
1703 translate_y = surface_height;
1704 break;
1705 case WL_OUTPUT_TRANSFORM_270:
1706 angle = M_PI + M_PI_2;
1707 translate_x = 0;
1708 translate_y = surface_width;
1709 break;
1710 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1711 angle = M_PI + M_PI_2;
1712 translate_x = 0;
1713 translate_y = 0;
1714 break;
1715 }
1716
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001717 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001718 cairo_translate(cr, translate_x, translate_y);
1719 cairo_rotate(cr, angle);
1720 cairo_transform(cr, &m);
1721}
1722
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001723cairo_t *
1724widget_cairo_create(struct widget *widget)
1725{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001726 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001727 cairo_surface_t *cairo_surface;
1728 cairo_t *cr;
1729
1730 cairo_surface = widget_get_cairo_surface(widget);
1731 cr = cairo_create(cairo_surface);
1732
Alexander Larsson15901f02013-05-22 14:41:25 +02001733 widget_cairo_update_transform(widget, cr);
1734
Pekka Paalanen35e82632013-04-25 13:57:48 +03001735 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1736
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001737 return cr;
1738}
1739
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001740struct wl_surface *
1741widget_get_wl_surface(struct widget *widget)
1742{
1743 return widget->surface->surface;
1744}
1745
1746uint32_t
1747widget_get_last_time(struct widget *widget)
1748{
1749 return widget->surface->last_time;
1750}
1751
1752void
1753widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1754{
1755 struct wl_compositor *comp = widget->window->display->compositor;
1756 struct surface *surface = widget->surface;
1757
1758 if (!surface->input_region)
1759 surface->input_region = wl_compositor_create_region(comp);
1760
1761 if (rect) {
1762 wl_region_add(surface->input_region,
1763 rect->x, rect->y, rect->width, rect->height);
1764 }
1765}
1766
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001767void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001768widget_set_resize_handler(struct widget *widget,
1769 widget_resize_handler_t handler)
1770{
1771 widget->resize_handler = handler;
1772}
1773
1774void
1775widget_set_redraw_handler(struct widget *widget,
1776 widget_redraw_handler_t handler)
1777{
1778 widget->redraw_handler = handler;
1779}
1780
1781void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001782widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001783{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001784 widget->enter_handler = handler;
1785}
1786
1787void
1788widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1789{
1790 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001791}
1792
1793void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001794widget_set_motion_handler(struct widget *widget,
1795 widget_motion_handler_t handler)
1796{
1797 widget->motion_handler = handler;
1798}
1799
1800void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001801widget_set_button_handler(struct widget *widget,
1802 widget_button_handler_t handler)
1803{
1804 widget->button_handler = handler;
1805}
1806
1807void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001808widget_set_axis_handler(struct widget *widget,
1809 widget_axis_handler_t handler)
1810{
1811 widget->axis_handler = handler;
1812}
1813
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001814static void
1815window_schedule_redraw_task(struct window *window);
1816
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001817void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001818widget_schedule_redraw(struct widget *widget)
1819{
Pekka Paalanen71233882013-04-25 13:57:53 +03001820 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001821 widget->surface->redraw_needed = 1;
1822 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001823}
1824
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001825cairo_surface_t *
1826window_get_surface(struct window *window)
1827{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001828 cairo_surface_t *cairo_surface;
1829
1830 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1831
1832 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001833}
1834
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001835struct wl_surface *
1836window_get_wl_surface(struct window *window)
1837{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001838 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001839}
1840
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001841struct wl_shell_surface *
1842window_get_wl_shell_surface(struct window *window)
1843{
1844 return window->shell_surface;
1845}
1846
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001847static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001848tooltip_redraw_handler(struct widget *widget, void *data)
1849{
1850 cairo_t *cr;
1851 const int32_t r = 3;
1852 struct tooltip *tooltip = data;
1853 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001854
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001855 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001856 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1857 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1858 cairo_paint(cr);
1859
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001860 width = widget->allocation.width;
1861 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001862 rounded_rect(cr, 0, 0, width, height, r);
1863
1864 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1865 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1866 cairo_fill(cr);
1867
1868 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1869 cairo_move_to(cr, 10, 16);
1870 cairo_show_text(cr, tooltip->entry);
1871 cairo_destroy(cr);
1872}
1873
1874static cairo_text_extents_t
1875get_text_extents(struct tooltip *tooltip)
1876{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001877 cairo_t *cr;
1878 cairo_text_extents_t extents;
1879
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001880 /* Use the dummy_surface because tooltip's surface was not
1881 * created yet, and parent does not have a valid surface
1882 * outside repaint, either.
1883 */
1884 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001885 cairo_text_extents(cr, tooltip->entry, &extents);
1886 cairo_destroy(cr);
1887
1888 return extents;
1889}
1890
1891static int
1892window_create_tooltip(struct tooltip *tooltip)
1893{
1894 struct widget *parent = tooltip->parent;
1895 struct display *display = parent->window->display;
1896 struct window *window;
1897 const int offset_y = 27;
1898 const int margin = 3;
1899 cairo_text_extents_t extents;
1900
1901 if (tooltip->widget)
1902 return 0;
1903
1904 window = window_create_transient(display, parent->window, tooltip->x,
1905 tooltip->y + offset_y,
1906 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1907 if (!window)
1908 return -1;
1909
1910 tooltip->window = window;
1911 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1912
1913 extents = get_text_extents(tooltip);
1914 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1915 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1916
1917 return 0;
1918}
1919
1920void
1921widget_destroy_tooltip(struct widget *parent)
1922{
1923 struct tooltip *tooltip = parent->tooltip;
1924
1925 parent->tooltip_count = 0;
1926 if (!tooltip)
1927 return;
1928
1929 if (tooltip->widget) {
1930 widget_destroy(tooltip->widget);
1931 window_destroy(tooltip->window);
1932 tooltip->widget = NULL;
1933 tooltip->window = NULL;
1934 }
1935
1936 close(tooltip->tooltip_fd);
1937 free(tooltip->entry);
1938 free(tooltip);
1939 parent->tooltip = NULL;
1940}
1941
1942static void
1943tooltip_func(struct task *task, uint32_t events)
1944{
1945 struct tooltip *tooltip =
1946 container_of(task, struct tooltip, tooltip_task);
1947 uint64_t exp;
1948
Martin Olsson8df662a2012-07-08 03:03:47 +02001949 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1950 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001951 window_create_tooltip(tooltip);
1952}
1953
1954#define TOOLTIP_TIMEOUT 500
1955static int
1956tooltip_timer_reset(struct tooltip *tooltip)
1957{
1958 struct itimerspec its;
1959
1960 its.it_interval.tv_sec = 0;
1961 its.it_interval.tv_nsec = 0;
1962 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1963 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1964 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1965 fprintf(stderr, "could not set timerfd\n: %m");
1966 return -1;
1967 }
1968
1969 return 0;
1970}
1971
1972int
1973widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1974{
1975 struct tooltip *tooltip = parent->tooltip;
1976
1977 parent->tooltip_count++;
1978 if (tooltip) {
1979 tooltip->x = x;
1980 tooltip->y = y;
1981 tooltip_timer_reset(tooltip);
1982 return 0;
1983 }
1984
1985 /* the handler might be triggered too fast via input device motion, so
1986 * we need this check here to make sure tooltip is fully initialized */
1987 if (parent->tooltip_count > 1)
1988 return 0;
1989
1990 tooltip = malloc(sizeof *tooltip);
1991 if (!tooltip)
1992 return -1;
1993
1994 parent->tooltip = tooltip;
1995 tooltip->parent = parent;
1996 tooltip->widget = NULL;
1997 tooltip->window = NULL;
1998 tooltip->x = x;
1999 tooltip->y = y;
2000 tooltip->entry = strdup(entry);
2001 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2002 if (tooltip->tooltip_fd < 0) {
2003 fprintf(stderr, "could not create timerfd\n: %m");
2004 return -1;
2005 }
2006
2007 tooltip->tooltip_task.run = tooltip_func;
2008 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2009 EPOLLIN, &tooltip->tooltip_task);
2010 tooltip_timer_reset(tooltip);
2011
2012 return 0;
2013}
2014
2015static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002016workspace_manager_state(void *data,
2017 struct workspace_manager *workspace_manager,
2018 uint32_t current,
2019 uint32_t count)
2020{
2021 struct display *display = data;
2022
2023 display->workspace = current;
2024 display->workspace_count = count;
2025}
2026
2027static const struct workspace_manager_listener workspace_manager_listener = {
2028 workspace_manager_state
2029};
2030
2031static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002032frame_resize_handler(struct widget *widget,
2033 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002034{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002035 struct frame *frame = data;
2036 struct widget *child = frame->child;
2037 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002038 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002039 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002040 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002041 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002042 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002043 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002044 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002045
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002046 switch (widget->window->type) {
2047 case TYPE_FULLSCREEN:
2048 decoration_width = 0;
2049 decoration_height = 0;
2050
2051 allocation.x = 0;
2052 allocation.y = 0;
2053 allocation.width = width;
2054 allocation.height = height;
2055 opaque_margin = 0;
2056
2057 wl_list_for_each(button, &frame->buttons_list, link)
2058 button->widget->opaque = 1;
2059 break;
2060 case TYPE_MAXIMIZED:
2061 decoration_width = t->width * 2;
2062 decoration_height = t->width + t->titlebar_height;
2063
2064 allocation.x = t->width;
2065 allocation.y = t->titlebar_height;
2066 allocation.width = width - decoration_width;
2067 allocation.height = height - decoration_height;
2068
2069 opaque_margin = 0;
2070
2071 wl_list_for_each(button, &frame->buttons_list, link)
2072 button->widget->opaque = 0;
2073 break;
2074 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002075 decoration_width = (t->width + t->margin) * 2;
2076 decoration_height = t->width +
2077 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002078
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002079 allocation.x = t->width + t->margin;
2080 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002081 allocation.width = width - decoration_width;
2082 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002083
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002084 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002085
2086 wl_list_for_each(button, &frame->buttons_list, link)
2087 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002088 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002089 }
2090
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002091 widget_set_allocation(child, allocation.x, allocation.y,
2092 allocation.width, allocation.height);
2093
2094 if (child->resize_handler)
2095 child->resize_handler(child,
2096 allocation.width,
2097 allocation.height,
2098 child->user_data);
2099
Scott Moreauf7e498c2012-05-14 11:39:29 -06002100 width = child->allocation.width + decoration_width;
2101 height = child->allocation.height + decoration_height;
2102
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002103 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2104
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002105 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002106 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002107 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002108 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002109 shadow_margin, shadow_margin,
2110 width - 2 * shadow_margin,
2111 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002112 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002113 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002114 }
2115
Scott Moreauf7e498c2012-05-14 11:39:29 -06002116 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002117
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002118 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002119 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002120 opaque_margin, opaque_margin,
2121 widget->allocation.width - 2 * opaque_margin,
2122 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002123
2124 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002125 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2126 x_l = t->width + shadow_margin;
2127 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002128 wl_list_for_each(button, &frame->buttons_list, link) {
2129 const int button_padding = 4;
2130 w = cairo_image_surface_get_width(button->icon);
2131 h = cairo_image_surface_get_height(button->icon);
2132
2133 if (button->decoration == FRAME_BUTTON_FANCY)
2134 w += 10;
2135
2136 if (button->align == FRAME_BUTTON_LEFT) {
2137 widget_set_allocation(button->widget,
2138 x_l, y , w + 1, h + 1);
2139 x_l += w;
2140 x_l += button_padding;
2141 } else {
2142 x_r -= w;
2143 widget_set_allocation(button->widget,
2144 x_r, y , w + 1, h + 1);
2145 x_r -= button_padding;
2146 }
2147 }
2148}
2149
2150static int
2151frame_button_enter_handler(struct widget *widget,
2152 struct input *input, float x, float y, void *data)
2153{
2154 struct frame_button *frame_button = data;
2155
2156 widget_schedule_redraw(frame_button->widget);
2157 frame_button->state = FRAME_BUTTON_OVER;
2158
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002159 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002160}
2161
2162static void
2163frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2164{
2165 struct frame_button *frame_button = data;
2166
2167 widget_schedule_redraw(frame_button->widget);
2168 frame_button->state = FRAME_BUTTON_DEFAULT;
2169}
2170
2171static void
2172frame_button_button_handler(struct widget *widget,
2173 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002174 uint32_t button,
2175 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002176{
2177 struct frame_button *frame_button = data;
2178 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002179 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002180
2181 if (button != BTN_LEFT)
2182 return;
2183
2184 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002185 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002186 frame_button->state = FRAME_BUTTON_ACTIVE;
2187 widget_schedule_redraw(frame_button->widget);
2188
2189 if (frame_button->type == FRAME_BUTTON_ICON)
2190 window_show_frame_menu(window, input, time);
2191 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002192 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002193 frame_button->state = FRAME_BUTTON_DEFAULT;
2194 widget_schedule_redraw(frame_button->widget);
2195 break;
2196 }
2197
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002198 if (!was_pressed)
2199 return;
2200
Martin Minarik1998b152012-05-10 02:04:35 +02002201 switch (frame_button->type) {
2202 case FRAME_BUTTON_CLOSE:
2203 if (window->close_handler)
2204 window->close_handler(window->parent,
2205 window->user_data);
2206 else
2207 display_exit(window->display);
2208 break;
2209 case FRAME_BUTTON_MINIMIZE:
2210 fprintf(stderr,"Minimize stub\n");
2211 break;
2212 case FRAME_BUTTON_MAXIMIZE:
2213 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2214 break;
2215 default:
2216 /* Unknown operation */
2217 break;
2218 }
2219}
2220
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002221static int
2222frame_button_motion_handler(struct widget *widget,
2223 struct input *input, uint32_t time,
2224 float x, float y, void *data)
2225{
2226 struct frame_button *frame_button = data;
2227 enum frame_button_pointer previous_button_state = frame_button->state;
2228
2229 /* only track state for a pressed button */
2230 if (input->grab != widget)
2231 return CURSOR_LEFT_PTR;
2232
2233 if (x > widget->allocation.x &&
2234 x < (widget->allocation.x + widget->allocation.width) &&
2235 y > widget->allocation.y &&
2236 y < (widget->allocation.y + widget->allocation.height)) {
2237 frame_button->state = FRAME_BUTTON_ACTIVE;
2238 } else {
2239 frame_button->state = FRAME_BUTTON_DEFAULT;
2240 }
2241
2242 if (frame_button->state != previous_button_state)
2243 widget_schedule_redraw(frame_button->widget);
2244
2245 return CURSOR_LEFT_PTR;
2246}
2247
Martin Minarik1998b152012-05-10 02:04:35 +02002248static void
2249frame_button_redraw_handler(struct widget *widget, void *data)
2250{
2251 struct frame_button *frame_button = data;
2252 cairo_t *cr;
2253 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002254
2255 x = widget->allocation.x;
2256 y = widget->allocation.y;
2257 width = widget->allocation.width;
2258 height = widget->allocation.height;
2259
2260 if (!width)
2261 return;
2262 if (!height)
2263 return;
2264 if (widget->opaque)
2265 return;
2266
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002267 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002268
2269 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2270 cairo_set_line_width(cr, 1);
2271
2272 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2273 cairo_rectangle (cr, x, y, 25, 16);
2274
2275 cairo_stroke_preserve(cr);
2276
2277 switch (frame_button->state) {
2278 case FRAME_BUTTON_DEFAULT:
2279 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2280 break;
2281 case FRAME_BUTTON_OVER:
2282 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2283 break;
2284 case FRAME_BUTTON_ACTIVE:
2285 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2286 break;
2287 }
2288
2289 cairo_fill (cr);
2290
2291 x += 4;
2292 }
2293
2294 cairo_set_source_surface(cr, frame_button->icon, x, y);
2295 cairo_paint(cr);
2296
2297 cairo_destroy(cr);
2298}
2299
2300static struct widget *
2301frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2302 enum frame_button_align align, enum frame_button_decoration style)
2303{
2304 struct frame_button *frame_button;
2305 const char *icon = data;
2306
2307 frame_button = malloc (sizeof *frame_button);
2308 memset(frame_button, 0, sizeof *frame_button);
2309
2310 frame_button->icon = cairo_image_surface_create_from_png(icon);
2311 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2312 frame_button->frame = frame;
2313 frame_button->type = type;
2314 frame_button->align = align;
2315 frame_button->decoration = style;
2316
2317 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2318
2319 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2320 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2321 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2322 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002323 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002324 return frame_button->widget;
2325}
2326
2327static void
2328frame_button_destroy(struct frame_button *frame_button)
2329{
2330 widget_destroy(frame_button->widget);
2331 wl_list_remove(&frame_button->link);
2332 cairo_surface_destroy(frame_button->icon);
2333 free(frame_button);
2334
2335 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002336}
2337
2338static void
2339frame_redraw_handler(struct widget *widget, void *data)
2340{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002342 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002343 struct theme *t = window->display->theme;
2344 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002345
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002346 if (window->type == TYPE_FULLSCREEN)
2347 return;
2348
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002349 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002350
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002351 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002352 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002353 if (window->type == TYPE_MAXIMIZED)
2354 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002355 theme_render_frame(t, cr, widget->allocation.width,
2356 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002357
2358 cairo_destroy(cr);
2359}
2360
2361static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002362frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002363{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002364 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002365 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002366 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002367
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002368 if (window->type != TYPE_TOPLEVEL)
2369 return CURSOR_LEFT_PTR;
2370
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002371 location = theme_get_location(t, input->sx, input->sy,
2372 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002373 frame->widget->allocation.height,
2374 window->type == TYPE_MAXIMIZED ?
2375 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002376
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002377 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002378 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002379 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002380 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002381 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002382 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002383 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002384 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002385 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002386 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002387 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002388 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002389 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002390 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002391 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002392 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002393 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002394 case THEME_LOCATION_EXTERIOR:
2395 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002396 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002397 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002398 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002399}
2400
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002401static void
2402frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002403{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002404 struct display *display;
2405
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002406 switch (index) {
2407 case 0: /* close */
2408 if (window->close_handler)
2409 window->close_handler(window->parent,
2410 window->user_data);
2411 else
2412 display_exit(window->display);
2413 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002414 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002415 display = window->display;
2416 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002417 workspace_manager_move_surface(
2418 display->workspace_manager,
2419 window->main_surface->surface,
2420 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002421 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002422 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002423 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002424 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002425 workspace_manager_move_surface(
2426 display->workspace_manager,
2427 window->main_surface->surface,
2428 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002429 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002430 case 3: /* fullscreen */
2431 /* we don't have a way to get out of fullscreen for now */
2432 if (window->fullscreen_handler)
2433 window->fullscreen_handler(window, window->user_data);
2434 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002435 }
2436}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002437
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002438void
2439window_show_frame_menu(struct window *window,
2440 struct input *input, uint32_t time)
2441{
2442 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002443 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002444
2445 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002446 "Close",
2447 "Move to workspace above", "Move to workspace below",
2448 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002449 };
2450
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002451 if (window->fullscreen_handler)
2452 count = ARRAY_LENGTH(entries);
2453 else
2454 count = ARRAY_LENGTH(entries) - 1;
2455
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002456 input_get_position(input, &x, &y);
2457 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002458 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002459}
2460
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002461static int
2462frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002463 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002464{
2465 return frame_get_pointer_image_for_location(data, input);
2466}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002467
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002468static int
2469frame_motion_handler(struct widget *widget,
2470 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002471 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002472{
2473 return frame_get_pointer_image_for_location(data, input);
2474}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002475
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002476static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002477frame_button_handler(struct widget *widget,
2478 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002479 uint32_t button, enum wl_pointer_button_state state,
2480 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002481
2482{
2483 struct frame *frame = data;
2484 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002485 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002486 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002487
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002488 if (window->type != TYPE_TOPLEVEL)
2489 return;
2490
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002491 location = theme_get_location(display->theme, input->sx, input->sy,
2492 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002493 frame->widget->allocation.height,
2494 window->type == TYPE_MAXIMIZED ?
2495 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002496
Daniel Stone4dbadb12012-05-30 16:31:51 +01002497 if (window->display->shell && button == BTN_LEFT &&
2498 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002499 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002500 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002501 if (!window->shell_surface)
2502 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002503 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002504 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002505 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002506 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002507 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002508 case THEME_LOCATION_RESIZING_TOP:
2509 case THEME_LOCATION_RESIZING_BOTTOM:
2510 case THEME_LOCATION_RESIZING_LEFT:
2511 case THEME_LOCATION_RESIZING_RIGHT:
2512 case THEME_LOCATION_RESIZING_TOP_LEFT:
2513 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2514 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2515 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002516 if (!window->shell_surface)
2517 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002518 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002519
Pekka Paalanen99436862012-11-19 17:15:59 +02002520 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002521 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002522 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002523 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002524 break;
2525 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002526 } else if (button == BTN_RIGHT &&
2527 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002528 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002529 }
2530}
2531
2532struct widget *
2533frame_create(struct window *window, void *data)
2534{
2535 struct frame *frame;
2536
2537 frame = malloc(sizeof *frame);
2538 memset(frame, 0, sizeof *frame);
2539
2540 frame->widget = window_add_widget(window, frame);
2541 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002542
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002543 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2544 widget_set_resize_handler(frame->widget, frame_resize_handler);
2545 widget_set_enter_handler(frame->widget, frame_enter_handler);
2546 widget_set_motion_handler(frame->widget, frame_motion_handler);
2547 widget_set_button_handler(frame->widget, frame_button_handler);
2548
Martin Minarik1998b152012-05-10 02:04:35 +02002549 /* Create empty list for frame buttons */
2550 wl_list_init(&frame->buttons_list);
2551
2552 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2553 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2554
2555 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2556 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2557
2558 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2559 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2560
2561 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2562 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2563
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002564 window->frame = frame;
2565
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002566 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002567}
2568
Kristian Høgsberga1627922012-06-20 17:30:03 -04002569void
2570frame_set_child_size(struct widget *widget, int child_width, int child_height)
2571{
2572 struct display *display = widget->window->display;
2573 struct theme *t = display->theme;
2574 int decoration_width, decoration_height;
2575 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002576 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002577
2578 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002579 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002580 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002581 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002582
2583 width = child_width + decoration_width;
2584 height = child_height + decoration_height;
2585 } else {
2586 width = child_width;
2587 height = child_height;
2588 }
2589
2590 window_schedule_resize(widget->window, width, height);
2591}
2592
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002593static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002594frame_destroy(struct frame *frame)
2595{
Martin Minarik1998b152012-05-10 02:04:35 +02002596 struct frame_button *button, *tmp;
2597
2598 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2599 frame_button_destroy(button);
2600
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002601 /* frame->child must be destroyed by the application */
2602 widget_destroy(frame->widget);
2603 free(frame);
2604}
2605
2606static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002607input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002608 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002609{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002610 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002611 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002612
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002613 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002614 return;
2615
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002616 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002617 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002618 widget = old;
2619 if (input->grab)
2620 widget = input->grab;
2621 if (widget->leave_handler)
2622 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002623 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002624 }
2625
2626 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002627 widget = focus;
2628 if (input->grab)
2629 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002630 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002631 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002632 cursor = widget->enter_handler(focus, input, x, y,
2633 widget->user_data);
2634 else
2635 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002636
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002637 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002638 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002639}
2640
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002641void
2642input_grab(struct input *input, struct widget *widget, uint32_t button)
2643{
2644 input->grab = widget;
2645 input->grab_button = button;
2646}
2647
2648void
2649input_ungrab(struct input *input)
2650{
2651 struct widget *widget;
2652
2653 input->grab = NULL;
2654 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002655 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002656 input->sx, input->sy);
2657 input_set_focus_widget(input, widget, input->sx, input->sy);
2658 }
2659}
2660
2661static void
2662input_remove_pointer_focus(struct input *input)
2663{
2664 struct window *window = input->pointer_focus;
2665
2666 if (!window)
2667 return;
2668
2669 input_set_focus_widget(input, NULL, 0, 0);
2670
2671 input->pointer_focus = NULL;
2672 input->current_cursor = CURSOR_UNSET;
2673}
2674
2675static void
2676pointer_handle_enter(void *data, struct wl_pointer *pointer,
2677 uint32_t serial, struct wl_surface *surface,
2678 wl_fixed_t sx_w, wl_fixed_t sy_w)
2679{
2680 struct input *input = data;
2681 struct window *window;
2682 struct widget *widget;
2683 float sx = wl_fixed_to_double(sx_w);
2684 float sy = wl_fixed_to_double(sy_w);
2685
2686 if (!surface) {
2687 /* enter event for a window we've just destroyed */
2688 return;
2689 }
2690
2691 input->display->serial = serial;
2692 input->pointer_enter_serial = serial;
2693 input->pointer_focus = wl_surface_get_user_data(surface);
2694 window = input->pointer_focus;
2695
Pekka Paalanen99436862012-11-19 17:15:59 +02002696 if (window->resizing) {
2697 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002698 /* Schedule a redraw to free the pool */
2699 window_schedule_redraw(window);
2700 }
2701
2702 input->sx = sx;
2703 input->sy = sy;
2704
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002705 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002706 input_set_focus_widget(input, widget, sx, sy);
2707}
2708
2709static void
2710pointer_handle_leave(void *data, struct wl_pointer *pointer,
2711 uint32_t serial, struct wl_surface *surface)
2712{
2713 struct input *input = data;
2714
2715 input->display->serial = serial;
2716 input_remove_pointer_focus(input);
2717}
2718
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002719static void
Daniel Stone37816df2012-05-16 18:45:18 +01002720pointer_handle_motion(void *data, struct wl_pointer *pointer,
2721 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002722{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002723 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002724 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002725 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002726 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002727 float sx = wl_fixed_to_double(sx_w);
2728 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002729
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002730 input->sx = sx;
2731 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002732
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002733 if (!window)
2734 return;
2735
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002736 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002737 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002738 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002739 }
2740
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002741 if (input->grab)
2742 widget = input->grab;
2743 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002744 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002745 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002746 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002747 input, time, sx, sy,
2748 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002749 else
2750 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002751
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002752 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002753}
2754
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002755static void
Daniel Stone37816df2012-05-16 18:45:18 +01002756pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002757 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002758{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002759 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002760 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002761 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002762
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002763 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002764 if (input->focus_widget && input->grab == NULL &&
2765 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002766 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002767
Neil Roberts6b28aad2012-01-23 19:11:18 +00002768 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002769 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002770 (*widget->button_handler)(widget,
2771 input, time,
2772 button, state,
2773 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002774
Daniel Stone4dbadb12012-05-30 16:31:51 +01002775 if (input->grab && input->grab_button == button &&
2776 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002777 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002778}
2779
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002780static void
Daniel Stone37816df2012-05-16 18:45:18 +01002781pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002782 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002783{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002784 struct input *input = data;
2785 struct widget *widget;
2786
2787 widget = input->focus_widget;
2788 if (input->grab)
2789 widget = input->grab;
2790 if (widget && widget->axis_handler)
2791 (*widget->axis_handler)(widget,
2792 input, time,
2793 axis, value,
2794 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002795}
2796
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002797static const struct wl_pointer_listener pointer_listener = {
2798 pointer_handle_enter,
2799 pointer_handle_leave,
2800 pointer_handle_motion,
2801 pointer_handle_button,
2802 pointer_handle_axis,
2803};
2804
2805static void
2806input_remove_keyboard_focus(struct input *input)
2807{
2808 struct window *window = input->keyboard_focus;
2809 struct itimerspec its;
2810
2811 its.it_interval.tv_sec = 0;
2812 its.it_interval.tv_nsec = 0;
2813 its.it_value.tv_sec = 0;
2814 its.it_value.tv_nsec = 0;
2815 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2816
2817 if (!window)
2818 return;
2819
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002820 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002821 if (window->keyboard_focus_handler)
2822 (*window->keyboard_focus_handler)(window, NULL,
2823 window->user_data);
2824
2825 input->keyboard_focus = NULL;
2826}
2827
2828static void
2829keyboard_repeat_func(struct task *task, uint32_t events)
2830{
2831 struct input *input =
2832 container_of(task, struct input, repeat_task);
2833 struct window *window = input->keyboard_focus;
2834 uint64_t exp;
2835
2836 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2837 /* If we change the timer between the fd becoming
2838 * readable and getting here, there'll be nothing to
2839 * read and we get EAGAIN. */
2840 return;
2841
2842 if (window && window->key_handler) {
2843 (*window->key_handler)(window, input, input->repeat_time,
2844 input->repeat_key, input->repeat_sym,
2845 WL_KEYBOARD_KEY_STATE_PRESSED,
2846 window->user_data);
2847 }
2848}
2849
2850static void
2851keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2852 uint32_t format, int fd, uint32_t size)
2853{
2854 struct input *input = data;
2855 char *map_str;
2856
2857 if (!data) {
2858 close(fd);
2859 return;
2860 }
2861
2862 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2863 close(fd);
2864 return;
2865 }
2866
2867 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2868 if (map_str == MAP_FAILED) {
2869 close(fd);
2870 return;
2871 }
2872
2873 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2874 map_str,
2875 XKB_KEYMAP_FORMAT_TEXT_V1,
2876 0);
2877 munmap(map_str, size);
2878 close(fd);
2879
2880 if (!input->xkb.keymap) {
2881 fprintf(stderr, "failed to compile keymap\n");
2882 return;
2883 }
2884
2885 input->xkb.state = xkb_state_new(input->xkb.keymap);
2886 if (!input->xkb.state) {
2887 fprintf(stderr, "failed to create XKB state\n");
2888 xkb_map_unref(input->xkb.keymap);
2889 input->xkb.keymap = NULL;
2890 return;
2891 }
2892
2893 input->xkb.control_mask =
2894 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2895 input->xkb.alt_mask =
2896 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2897 input->xkb.shift_mask =
2898 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2899}
2900
2901static void
2902keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2903 uint32_t serial, struct wl_surface *surface,
2904 struct wl_array *keys)
2905{
2906 struct input *input = data;
2907 struct window *window;
2908
2909 input->display->serial = serial;
2910 input->keyboard_focus = wl_surface_get_user_data(surface);
2911
2912 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002913 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002914 if (window->keyboard_focus_handler)
2915 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002916 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002917}
2918
2919static void
2920keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2921 uint32_t serial, struct wl_surface *surface)
2922{
2923 struct input *input = data;
2924
2925 input->display->serial = serial;
2926 input_remove_keyboard_focus(input);
2927}
2928
Scott Moreau210d0792012-03-22 10:47:01 -06002929static void
Daniel Stone37816df2012-05-16 18:45:18 +01002930keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002931 uint32_t serial, uint32_t time, uint32_t key,
2932 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002933{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002934 struct input *input = data;
2935 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002936 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002937 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002938 const xkb_keysym_t *syms;
2939 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002940 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002941
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002942 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002943 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002944 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002945 return;
2946
Daniel Stone97f68542012-05-30 16:32:01 +01002947 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002948
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002949 sym = XKB_KEY_NoSymbol;
2950 if (num_syms == 1)
2951 sym = syms[0];
2952
2953 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002954 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002955 window_set_maximized(window,
2956 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002957 } else if (sym == XKB_KEY_F11 &&
2958 window->fullscreen_handler &&
2959 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2960 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002961 } else if (sym == XKB_KEY_F4 &&
2962 input->modifiers == MOD_ALT_MASK &&
2963 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2964 if (window->close_handler)
2965 window->close_handler(window->parent,
2966 window->user_data);
2967 else
2968 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002969 } else if (window->key_handler) {
2970 (*window->key_handler)(window, input, time, key,
2971 sym, state, window->user_data);
2972 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002973
2974 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2975 key == input->repeat_key) {
2976 its.it_interval.tv_sec = 0;
2977 its.it_interval.tv_nsec = 0;
2978 its.it_value.tv_sec = 0;
2979 its.it_value.tv_nsec = 0;
2980 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2981 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2982 input->repeat_sym = sym;
2983 input->repeat_key = key;
2984 input->repeat_time = time;
2985 its.it_interval.tv_sec = 0;
2986 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2987 its.it_value.tv_sec = 0;
2988 its.it_value.tv_nsec = 400 * 1000 * 1000;
2989 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2990 }
2991}
2992
2993static void
Daniel Stone351eb612012-05-31 15:27:47 -04002994keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2995 uint32_t serial, uint32_t mods_depressed,
2996 uint32_t mods_latched, uint32_t mods_locked,
2997 uint32_t group)
2998{
2999 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003000 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003001
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003002 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3003 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003004 mask = xkb_state_serialize_mods(input->xkb.state,
3005 XKB_STATE_DEPRESSED |
3006 XKB_STATE_LATCHED);
3007 input->modifiers = 0;
3008 if (mask & input->xkb.control_mask)
3009 input->modifiers |= MOD_CONTROL_MASK;
3010 if (mask & input->xkb.alt_mask)
3011 input->modifiers |= MOD_ALT_MASK;
3012 if (mask & input->xkb.shift_mask)
3013 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003014}
3015
Daniel Stone37816df2012-05-16 18:45:18 +01003016static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003017 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003018 keyboard_handle_enter,
3019 keyboard_handle_leave,
3020 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003021 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003022};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003023
3024static void
Daniel Stone37816df2012-05-16 18:45:18 +01003025seat_handle_capabilities(void *data, struct wl_seat *seat,
3026 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003027{
Daniel Stone37816df2012-05-16 18:45:18 +01003028 struct input *input = data;
3029
3030 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3031 input->pointer = wl_seat_get_pointer(seat);
3032 wl_pointer_set_user_data(input->pointer, input);
3033 wl_pointer_add_listener(input->pointer, &pointer_listener,
3034 input);
3035 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3036 wl_pointer_destroy(input->pointer);
3037 input->pointer = NULL;
3038 }
3039
3040 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3041 input->keyboard = wl_seat_get_keyboard(seat);
3042 wl_keyboard_set_user_data(input->keyboard, input);
3043 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3044 input);
3045 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3046 wl_keyboard_destroy(input->keyboard);
3047 input->keyboard = NULL;
3048 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003049}
3050
Daniel Stone37816df2012-05-16 18:45:18 +01003051static const struct wl_seat_listener seat_listener = {
3052 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003053};
3054
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003055void
3056input_get_position(struct input *input, int32_t *x, int32_t *y)
3057{
3058 *x = input->sx;
3059 *y = input->sy;
3060}
3061
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003062struct display *
3063input_get_display(struct input *input)
3064{
3065 return input->display;
3066}
3067
Daniel Stone37816df2012-05-16 18:45:18 +01003068struct wl_seat *
3069input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003070{
Daniel Stone37816df2012-05-16 18:45:18 +01003071 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003072}
3073
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003074uint32_t
3075input_get_modifiers(struct input *input)
3076{
3077 return input->modifiers;
3078}
3079
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003080struct widget *
3081input_get_focus_widget(struct input *input)
3082{
3083 return input->focus_widget;
3084}
3085
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003086struct data_offer {
3087 struct wl_data_offer *offer;
3088 struct input *input;
3089 struct wl_array types;
3090 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003091
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003092 struct task io_task;
3093 int fd;
3094 data_func_t func;
3095 int32_t x, y;
3096 void *user_data;
3097};
3098
3099static void
3100data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3101{
3102 struct data_offer *offer = data;
3103 char **p;
3104
3105 p = wl_array_add(&offer->types, sizeof *p);
3106 *p = strdup(type);
3107}
3108
3109static const struct wl_data_offer_listener data_offer_listener = {
3110 data_offer_offer,
3111};
3112
3113static void
3114data_offer_destroy(struct data_offer *offer)
3115{
3116 char **p;
3117
3118 offer->refcount--;
3119 if (offer->refcount == 0) {
3120 wl_data_offer_destroy(offer->offer);
3121 for (p = offer->types.data; *p; p++)
3122 free(*p);
3123 wl_array_release(&offer->types);
3124 free(offer);
3125 }
3126}
3127
3128static void
3129data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003130 struct wl_data_device *data_device,
3131 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003132{
3133 struct data_offer *offer;
3134
3135 offer = malloc(sizeof *offer);
3136
3137 wl_array_init(&offer->types);
3138 offer->refcount = 1;
3139 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003140 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003141 wl_data_offer_add_listener(offer->offer,
3142 &data_offer_listener, offer);
3143}
3144
3145static void
3146data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003147 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003148 wl_fixed_t x_w, wl_fixed_t y_w,
3149 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003150{
3151 struct input *input = data;
3152 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003153 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003154 float x = wl_fixed_to_double(x_w);
3155 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003156 char **p;
3157
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003158 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003159 window = wl_surface_get_user_data(surface);
3160 input->pointer_focus = window;
3161
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003162 if (offer) {
3163 input->drag_offer = wl_data_offer_get_user_data(offer);
3164
3165 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3166 *p = NULL;
3167
3168 types_data = input->drag_offer->types.data;
3169 } else {
3170 input->drag_offer = NULL;
3171 types_data = NULL;
3172 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003173
3174 window = input->pointer_focus;
3175 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003176 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003177 window->user_data);
3178}
3179
3180static void
3181data_device_leave(void *data, struct wl_data_device *data_device)
3182{
3183 struct input *input = data;
3184
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003185 if (input->drag_offer) {
3186 data_offer_destroy(input->drag_offer);
3187 input->drag_offer = NULL;
3188 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003189}
3190
3191static void
3192data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003193 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003194{
3195 struct input *input = data;
3196 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003197 float x = wl_fixed_to_double(x_w);
3198 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003199 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003200
3201 input->sx = x;
3202 input->sy = y;
3203
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003204 if (input->drag_offer)
3205 types_data = input->drag_offer->types.data;
3206 else
3207 types_data = NULL;
3208
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003209 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003210 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003211 window->user_data);
3212}
3213
3214static void
3215data_device_drop(void *data, struct wl_data_device *data_device)
3216{
3217 struct input *input = data;
3218 struct window *window = input->pointer_focus;
3219
3220 if (window->drop_handler)
3221 window->drop_handler(window, input,
3222 input->sx, input->sy, window->user_data);
3223}
3224
3225static void
3226data_device_selection(void *data,
3227 struct wl_data_device *wl_data_device,
3228 struct wl_data_offer *offer)
3229{
3230 struct input *input = data;
3231 char **p;
3232
3233 if (input->selection_offer)
3234 data_offer_destroy(input->selection_offer);
3235
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003236 if (offer) {
3237 input->selection_offer = wl_data_offer_get_user_data(offer);
3238 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3239 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003240 } else {
3241 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003242 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003243}
3244
3245static const struct wl_data_device_listener data_device_listener = {
3246 data_device_data_offer,
3247 data_device_enter,
3248 data_device_leave,
3249 data_device_motion,
3250 data_device_drop,
3251 data_device_selection
3252};
3253
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003254static void
3255input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003256{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003257 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003258 struct wl_cursor *cursor;
3259 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003260
Daniel Stone80972742012-11-07 17:51:39 +11003261 if (!input->pointer)
3262 return;
3263
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003264 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003265 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003266 return;
3267
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003268 if (index >= (int) cursor->image_count) {
3269 fprintf(stderr, "cursor index out of range\n");
3270 return;
3271 }
3272
3273 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003274 buffer = wl_cursor_image_get_buffer(image);
3275 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003276 return;
3277
Kristian Høgsbergae277372012-08-01 09:41:08 -04003278 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003279 input->pointer_surface,
3280 image->hotspot_x, image->hotspot_y);
3281 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3282 wl_surface_damage(input->pointer_surface, 0, 0,
3283 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003284 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003285}
3286
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003287static const struct wl_callback_listener pointer_surface_listener;
3288
3289static void
3290pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3291 uint32_t time)
3292{
3293 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003294 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003295 int i;
3296
3297 if (callback) {
3298 assert(callback == input->cursor_frame_cb);
3299 wl_callback_destroy(callback);
3300 input->cursor_frame_cb = NULL;
3301 }
3302
Daniel Stone80972742012-11-07 17:51:39 +11003303 if (!input->pointer)
3304 return;
3305
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003306 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003307 wl_pointer_set_cursor(input->pointer,
3308 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003309 NULL, 0, 0);
3310 return;
3311 }
3312
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003313 if (input->current_cursor == CURSOR_UNSET)
3314 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003315 cursor = input->display->cursors[input->current_cursor];
3316 if (!cursor)
3317 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003318
3319 /* FIXME We don't have the current time on the first call so we set
3320 * the animation start to the time of the first frame callback. */
3321 if (time == 0)
3322 input->cursor_anim_start = 0;
3323 else if (input->cursor_anim_start == 0)
3324 input->cursor_anim_start = time;
3325
3326 if (time == 0 || input->cursor_anim_start == 0)
3327 i = 0;
3328 else
3329 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3330
Pekka Paalanenbc106382012-10-10 12:49:31 +03003331 if (cursor->image_count > 1) {
3332 input->cursor_frame_cb =
3333 wl_surface_frame(input->pointer_surface);
3334 wl_callback_add_listener(input->cursor_frame_cb,
3335 &pointer_surface_listener, input);
3336 }
3337
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003338 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003339}
3340
3341static const struct wl_callback_listener pointer_surface_listener = {
3342 pointer_surface_frame_callback
3343};
3344
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003345void
3346input_set_pointer_image(struct input *input, int pointer)
3347{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003348 int force = 0;
3349
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003350 if (!input->pointer)
3351 return;
3352
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003353 if (input->pointer_enter_serial > input->cursor_serial)
3354 force = 1;
3355
3356 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003357 return;
3358
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003359 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003360 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003361 if (!input->cursor_frame_cb)
3362 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003363 else if (force) {
3364 /* The current frame callback may be stuck if, for instance,
3365 * the set cursor request was processed by the server after
3366 * this client lost the focus. In this case the cursor surface
3367 * might not be mapped and the frame callback wouldn't ever
3368 * complete. Send a set_cursor and attach to try to map the
3369 * cursor surface again so that the callback will finish */
3370 input_set_pointer_image_index(input, 0);
3371 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003372}
3373
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003374struct wl_data_device *
3375input_get_data_device(struct input *input)
3376{
3377 return input->data_device;
3378}
3379
3380void
3381input_set_selection(struct input *input,
3382 struct wl_data_source *source, uint32_t time)
3383{
3384 wl_data_device_set_selection(input->data_device, source, time);
3385}
3386
3387void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003388input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003389{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003390 wl_data_offer_accept(input->drag_offer->offer,
3391 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003392}
3393
3394static void
3395offer_io_func(struct task *task, uint32_t events)
3396{
3397 struct data_offer *offer =
3398 container_of(task, struct data_offer, io_task);
3399 unsigned int len;
3400 char buffer[4096];
3401
3402 len = read(offer->fd, buffer, sizeof buffer);
3403 offer->func(buffer, len,
3404 offer->x, offer->y, offer->user_data);
3405
3406 if (len == 0) {
3407 close(offer->fd);
3408 data_offer_destroy(offer);
3409 }
3410}
3411
3412static void
3413data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3414 data_func_t func, void *user_data)
3415{
3416 int p[2];
3417
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003418 if (pipe2(p, O_CLOEXEC) == -1)
3419 return;
3420
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003421 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3422 close(p[1]);
3423
3424 offer->io_task.run = offer_io_func;
3425 offer->fd = p[0];
3426 offer->func = func;
3427 offer->refcount++;
3428 offer->user_data = user_data;
3429
3430 display_watch_fd(offer->input->display,
3431 offer->fd, EPOLLIN, &offer->io_task);
3432}
3433
3434void
3435input_receive_drag_data(struct input *input, const char *mime_type,
3436 data_func_t func, void *data)
3437{
3438 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3439 input->drag_offer->x = input->sx;
3440 input->drag_offer->y = input->sy;
3441}
3442
3443int
3444input_receive_selection_data(struct input *input, const char *mime_type,
3445 data_func_t func, void *data)
3446{
3447 char **p;
3448
3449 if (input->selection_offer == NULL)
3450 return -1;
3451
3452 for (p = input->selection_offer->types.data; *p; p++)
3453 if (strcmp(mime_type, *p) == 0)
3454 break;
3455
3456 if (*p == NULL)
3457 return -1;
3458
3459 data_offer_receive_data(input->selection_offer,
3460 mime_type, func, data);
3461 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003462}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003463
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003464int
3465input_receive_selection_data_to_fd(struct input *input,
3466 const char *mime_type, int fd)
3467{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003468 if (input->selection_offer)
3469 wl_data_offer_receive(input->selection_offer->offer,
3470 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003471
3472 return 0;
3473}
3474
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003475void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003476window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003477{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003478 if (!window->shell_surface)
3479 return;
3480
Daniel Stone37816df2012-05-16 18:45:18 +01003481 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003482}
3483
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003484static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003485surface_set_synchronized(struct surface *surface)
3486{
3487 if (!surface->subsurface)
3488 return;
3489
3490 if (surface->synchronized)
3491 return;
3492
3493 wl_subsurface_set_sync(surface->subsurface);
3494 surface->synchronized = 1;
3495}
3496
3497static void
3498surface_set_synchronized_default(struct surface *surface)
3499{
3500 if (!surface->subsurface)
3501 return;
3502
3503 if (surface->synchronized == surface->synchronized_default)
3504 return;
3505
3506 if (surface->synchronized_default)
3507 wl_subsurface_set_sync(surface->subsurface);
3508 else
3509 wl_subsurface_set_desync(surface->subsurface);
3510
3511 surface->synchronized = surface->synchronized_default;
3512}
3513
3514static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003515surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003516{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003517 struct widget *widget = surface->widget;
3518 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003519
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003520 if (surface->input_region) {
3521 wl_region_destroy(surface->input_region);
3522 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003523 }
3524
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003525 if (surface->opaque_region)
3526 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003527
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003528 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003529
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003530 if (widget->resize_handler)
3531 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003532 widget->allocation.width,
3533 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003534 widget->user_data);
3535
Pekka Paalanen35e82632013-04-25 13:57:48 +03003536 if (surface->subsurface &&
3537 (surface->allocation.x != widget->allocation.x ||
3538 surface->allocation.y != widget->allocation.y)) {
3539 wl_subsurface_set_position(surface->subsurface,
3540 widget->allocation.x,
3541 widget->allocation.y);
3542 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003543 if (surface->allocation.width != widget->allocation.width ||
3544 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003545 window_schedule_redraw(widget->window);
3546 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003547 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003548
3549 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003550 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003551 widget->allocation.width,
3552 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003553}
3554
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003555static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003556hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3557{
3558 /*
3559 * This hack should be removed, when EGL respects
3560 * eglSwapInterval(0).
3561 *
3562 * If this window has sub-surfaces, especially a free-running
3563 * EGL-widget, we need to post the parent surface once with
3564 * all the old state to guarantee, that the EGL-widget will
3565 * receive its frame callback soon. Otherwise, a forced call
3566 * to eglSwapBuffers may end up blocking, waiting for a frame
3567 * event that will never come, because we will commit the parent
3568 * surface with all new state only after eglSwapBuffers returns.
3569 *
3570 * This assumes, that:
3571 * 1. When the EGL widget's resize hook is called, it pauses.
3572 * 2. When the EGL widget's redraw hook is called, it forces a
3573 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3574 * In a single threaded application condition 1 is a no-op.
3575 *
3576 * XXX: This should actually be after the surface_resize() calls,
3577 * but cannot, because then it would commit the incomplete state
3578 * accumulated from the widget resize hooks.
3579 */
3580 if (window->subsurface_list.next != &window->main_surface->link ||
3581 window->subsurface_list.prev != &window->main_surface->link)
3582 wl_surface_commit(window->main_surface->surface);
3583}
3584
3585static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003586idle_resize(struct window *window)
3587{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003588 struct surface *surface;
3589
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003590 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003591 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003592
Pekka Paalanen71233882013-04-25 13:57:53 +03003593 DBG("from %dx%d to %dx%d\n",
3594 window->main_surface->server_allocation.width,
3595 window->main_surface->server_allocation.height,
3596 window->pending_allocation.width,
3597 window->pending_allocation.height);
3598
Pekka Paalanene9297f82013-04-25 13:57:51 +03003599 hack_prevent_EGL_sub_surface_deadlock(window);
3600
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003601 widget_set_allocation(window->main_surface->widget,
3602 window->pending_allocation.x,
3603 window->pending_allocation.y,
3604 window->pending_allocation.width,
3605 window->pending_allocation.height);
3606
3607 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003608
3609 /* The main surface is in the list, too. Main surface's
3610 * resize_handler is responsible for calling widget_set_allocation()
3611 * on all sub-surface root widgets, so they will be resized
3612 * properly.
3613 */
3614 wl_list_for_each(surface, &window->subsurface_list, link) {
3615 if (surface == window->main_surface)
3616 continue;
3617
3618 surface_set_synchronized(surface);
3619 surface_resize(surface);
3620 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003621}
3622
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003623void
3624window_schedule_resize(struct window *window, int width, int height)
3625{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003626 window->pending_allocation.x = 0;
3627 window->pending_allocation.y = 0;
3628 window->pending_allocation.width = width;
3629 window->pending_allocation.height = height;
3630
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003631 if (window->min_allocation.width == 0)
3632 window->min_allocation = window->pending_allocation;
3633 if (window->pending_allocation.width < window->min_allocation.width)
3634 window->pending_allocation.width = window->min_allocation.width;
3635 if (window->pending_allocation.height < window->min_allocation.height)
3636 window->pending_allocation.height = window->min_allocation.height;
3637
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003638 window->resize_needed = 1;
3639 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003640}
3641
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003642void
3643widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3644{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003645 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003646}
3647
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003648static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003649handle_ping(void *data, struct wl_shell_surface *shell_surface,
3650 uint32_t serial)
3651{
3652 wl_shell_surface_pong(shell_surface, serial);
3653}
3654
3655static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003656handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003657 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003658{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003659 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003660
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003661 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003662 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003663}
3664
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003665static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003666menu_destroy(struct menu *menu)
3667{
3668 widget_destroy(menu->widget);
3669 window_destroy(menu->window);
3670 free(menu);
3671}
3672
3673static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003674handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3675{
3676 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003677 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003678
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003679 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003680 * device. Or just use wl_callback. And this really needs to
3681 * be a window vfunc that the menu can set. And we need the
3682 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003683
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003684 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003685 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003686}
3687
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003688static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003689 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003690 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003691 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003692};
3693
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003694void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003695window_get_allocation(struct window *window,
3696 struct rectangle *allocation)
3697{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003698 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003699}
3700
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003701static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003702widget_redraw(struct widget *widget)
3703{
3704 struct widget *child;
3705
3706 if (widget->redraw_handler)
3707 widget->redraw_handler(widget, widget->user_data);
3708 wl_list_for_each(child, &widget->child_list, link)
3709 widget_redraw(child);
3710}
3711
3712static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003713frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3714{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003715 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003716
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003717 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003718 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003719 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003720 surface->frame_cb = NULL;
3721
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003722 surface->last_time = time;
3723
Pekka Paalanen71233882013-04-25 13:57:53 +03003724 if (surface->redraw_needed || surface->window->redraw_needed) {
3725 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003726 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003727 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003728}
3729
3730static const struct wl_callback_listener listener = {
3731 frame_callback
3732};
3733
3734static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003735surface_redraw(struct surface *surface)
3736{
Pekka Paalanen71233882013-04-25 13:57:53 +03003737 DBG_OBJ(surface->surface, "begin\n");
3738
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003739 if (!surface->window->redraw_needed && !surface->redraw_needed)
3740 return;
3741
3742 /* Whole-window redraw forces a redraw even if the previous has
3743 * not yet hit the screen.
3744 */
3745 if (surface->frame_cb) {
3746 if (!surface->window->redraw_needed)
3747 return;
3748
Pekka Paalanen71233882013-04-25 13:57:53 +03003749 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003750 wl_callback_destroy(surface->frame_cb);
3751 }
3752
3753 surface->frame_cb = wl_surface_frame(surface->surface);
3754 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003755 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003756
3757 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003758 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003759 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003760 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003761}
3762
3763static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003764idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003765{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003766 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003767 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003768
Pekka Paalanen71233882013-04-25 13:57:53 +03003769 DBG(" --------- \n");
3770
Pekka Paalaneneebff542013-04-25 13:57:52 +03003771 wl_list_init(&window->redraw_task.link);
3772 window->redraw_task_scheduled = 0;
3773
3774 if (window->resize_needed) {
3775 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003776 if (window->main_surface->frame_cb) {
3777 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003778 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003779 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003780
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003781 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003782 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003783
Pekka Paalanen35e82632013-04-25 13:57:48 +03003784 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003785 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003786
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003787 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003788 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003789
3790 wl_list_for_each(surface, &window->subsurface_list, link)
3791 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003792}
3793
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003794static void
3795window_schedule_redraw_task(struct window *window)
3796{
3797 if (window->configure_requests)
3798 return;
3799 if (!window->redraw_task_scheduled) {
3800 window->redraw_task.run = idle_redraw;
3801 display_defer(window->display, &window->redraw_task);
3802 window->redraw_task_scheduled = 1;
3803 }
3804}
3805
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003806void
3807window_schedule_redraw(struct window *window)
3808{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003809 struct surface *surface;
3810
Pekka Paalanen71233882013-04-25 13:57:53 +03003811 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3812
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003813 wl_list_for_each(surface, &window->subsurface_list, link)
3814 surface->redraw_needed = 1;
3815
3816 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003817}
3818
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003819int
3820window_is_fullscreen(struct window *window)
3821{
3822 return window->type == TYPE_FULLSCREEN;
3823}
3824
MoD063a8822013-03-02 23:43:57 +00003825static void
3826configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3827{
3828 struct window *window = data;
3829
3830 wl_callback_destroy(callback);
3831 window->configure_requests--;
3832
3833 if (!window->configure_requests)
3834 window_schedule_redraw(window);
3835}
3836
3837static struct wl_callback_listener configure_request_listener = {
3838 configure_request_completed,
3839};
3840
3841static void
3842window_defer_redraw_until_configure(struct window* window)
3843{
3844 struct wl_callback *callback;
3845
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003846 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003847 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003848 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003849 }
3850
3851 callback = wl_display_sync(window->display->display);
3852 wl_callback_add_listener(callback, &configure_request_listener, window);
3853 window->configure_requests++;
3854}
3855
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003856void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003857window_set_fullscreen(struct window *window, int fullscreen)
3858{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003859 if (!window->display->shell)
3860 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003861
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003862 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003863 return;
3864
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003865 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003866 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003867 if (window->type == TYPE_TOPLEVEL) {
3868 window->saved_allocation = window->main_surface->allocation;
3869 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003870 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003871 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003872 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003873 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003874 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003875 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003876 if (window->saved_type == TYPE_MAXIMIZED) {
3877 window_set_maximized(window, 1);
3878 } else {
3879 window->type = TYPE_TOPLEVEL;
3880 wl_shell_surface_set_toplevel(window->shell_surface);
3881 window_schedule_resize(window,
3882 window->saved_allocation.width,
3883 window->saved_allocation.height);
3884 }
3885
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003886 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003887}
3888
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003889void
3890window_set_fullscreen_method(struct window *window,
3891 enum wl_shell_surface_fullscreen_method method)
3892{
3893 window->fullscreen_method = method;
3894}
3895
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003896int
3897window_is_maximized(struct window *window)
3898{
3899 return window->type == TYPE_MAXIMIZED;
3900}
3901
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003902void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003903window_set_maximized(struct window *window, int maximized)
3904{
3905 if (!window->display->shell)
3906 return;
3907
3908 if ((window->type == TYPE_MAXIMIZED) == maximized)
3909 return;
3910
3911 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003912 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003913 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3914 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003915 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003916 } else if (window->type == TYPE_FULLSCREEN) {
3917 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3918 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003919 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003920 } else {
3921 wl_shell_surface_set_toplevel(window->shell_surface);
3922 window->type = TYPE_TOPLEVEL;
3923 window_schedule_resize(window,
3924 window->saved_allocation.width,
3925 window->saved_allocation.height);
3926 }
3927}
3928
3929void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003930window_set_user_data(struct window *window, void *data)
3931{
3932 window->user_data = data;
3933}
3934
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003935void *
3936window_get_user_data(struct window *window)
3937{
3938 return window->user_data;
3939}
3940
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003941void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003942window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003943 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003944{
3945 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003946}
3947
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003948void
3949window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003950 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003951{
3952 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003953}
3954
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003955void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003956window_set_data_handler(struct window *window, window_data_handler_t handler)
3957{
3958 window->data_handler = handler;
3959}
3960
3961void
3962window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3963{
3964 window->drop_handler = handler;
3965}
3966
3967void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003968window_set_close_handler(struct window *window,
3969 window_close_handler_t handler)
3970{
3971 window->close_handler = handler;
3972}
3973
3974void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003975window_set_fullscreen_handler(struct window *window,
3976 window_fullscreen_handler_t handler)
3977{
3978 window->fullscreen_handler = handler;
3979}
3980
3981void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003982window_set_output_handler(struct window *window,
3983 window_output_handler_t handler)
3984{
3985 window->output_handler = handler;
3986}
3987
3988void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003989window_set_title(struct window *window, const char *title)
3990{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003991 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003992 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003993 if (window->shell_surface)
3994 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003995}
3996
3997const char *
3998window_get_title(struct window *window)
3999{
4000 return window->title;
4001}
4002
4003void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004004window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4005{
4006 struct text_cursor_position *text_cursor_position =
4007 window->display->text_cursor_position;
4008
Scott Moreau9295ce02012-06-01 12:46:10 -06004009 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004010 return;
4011
4012 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004013 window->main_surface->surface,
4014 wl_fixed_from_int(x),
4015 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004016}
4017
4018void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004019window_damage(struct window *window, int32_t x, int32_t y,
4020 int32_t width, int32_t height)
4021{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004022 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004023}
4024
Casey Dahlin9074db52012-04-19 22:50:09 -04004025static void
4026surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004027 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004028{
Rob Bradford7507b572012-05-15 17:55:34 +01004029 struct window *window = data;
4030 struct output *output;
4031 struct output *output_found = NULL;
4032 struct window_output *window_output;
4033
4034 wl_list_for_each(output, &window->display->output_list, link) {
4035 if (output->output == wl_output) {
4036 output_found = output;
4037 break;
4038 }
4039 }
4040
4041 if (!output_found)
4042 return;
4043
4044 window_output = malloc (sizeof *window_output);
4045 window_output->output = output_found;
4046
4047 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004048
4049 if (window->output_handler)
4050 window->output_handler(window, output_found, 1,
4051 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004052}
4053
4054static void
4055surface_leave(void *data,
4056 struct wl_surface *wl_surface, struct wl_output *output)
4057{
Rob Bradford7507b572012-05-15 17:55:34 +01004058 struct window *window = data;
4059 struct window_output *window_output;
4060 struct window_output *window_output_found = NULL;
4061
4062 wl_list_for_each(window_output, &window->window_output_list, link) {
4063 if (window_output->output->output == output) {
4064 window_output_found = window_output;
4065 break;
4066 }
4067 }
4068
4069 if (window_output_found) {
4070 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004071
4072 if (window->output_handler)
4073 window->output_handler(window, window_output->output,
4074 0, window->user_data);
4075
Rob Bradford7507b572012-05-15 17:55:34 +01004076 free(window_output_found);
4077 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004078}
4079
4080static const struct wl_surface_listener surface_listener = {
4081 surface_enter,
4082 surface_leave
4083};
4084
Pekka Paalanen4e373742013-02-13 16:17:13 +02004085static struct surface *
4086surface_create(struct window *window)
4087{
4088 struct display *display = window->display;
4089 struct surface *surface;
4090
4091 surface = calloc(1, sizeof *surface);
4092 if (!surface)
4093 return NULL;
4094
4095 surface->window = window;
4096 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004097 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004098 wl_surface_add_listener(surface->surface, &surface_listener, window);
4099
Pekka Paalanen35e82632013-04-25 13:57:48 +03004100 wl_list_insert(&window->subsurface_list, &surface->link);
4101
Pekka Paalanen4e373742013-02-13 16:17:13 +02004102 return surface;
4103}
4104
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004105static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004106window_create_internal(struct display *display,
4107 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004108{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004109 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004110 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004111
4112 window = malloc(sizeof *window);
4113 if (window == NULL)
4114 return NULL;
4115
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004116 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004117 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004118 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004119 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004120
4121 surface = surface_create(window);
4122 window->main_surface = surface;
4123
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004124 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004125 window->shell_surface =
4126 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004127 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004128 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004129
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004130 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004131 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004132 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004133
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004134 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004135#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004136 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004137#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004138 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004139#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004140 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004141 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004142
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004143 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004144 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004145 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004146
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004147 if (window->shell_surface) {
4148 wl_shell_surface_set_user_data(window->shell_surface, window);
4149 wl_shell_surface_add_listener(window->shell_surface,
4150 &shell_surface_listener, window);
4151 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004152
Rob Bradford7507b572012-05-15 17:55:34 +01004153 wl_list_init (&window->window_output_list);
4154
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004155 return window;
4156}
4157
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004158struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004159window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004160{
4161 struct window *window;
4162
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004163 window = window_create_internal(display, NULL, TYPE_NONE);
4164 if (!window)
4165 return NULL;
4166
4167 return window;
4168}
4169
4170struct window *
4171window_create_custom(struct display *display)
4172{
4173 struct window *window;
4174
4175 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004176 if (!window)
4177 return NULL;
4178
4179 return window;
4180}
4181
4182struct window *
4183window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004184 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004185{
4186 struct window *window;
4187
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004188 window = window_create_internal(parent->display,
4189 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004190 if (!window)
4191 return NULL;
4192
4193 window->x = x;
4194 window->y = y;
4195
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004196 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004197 wl_shell_surface_set_transient(
4198 window->shell_surface,
4199 window->parent->main_surface->surface,
4200 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004201
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004202 return window;
4203}
4204
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004205static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004206menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004207{
4208 int next;
4209
4210 next = (sy - 8) / 20;
4211 if (menu->current != next) {
4212 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004213 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004214 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004215}
4216
4217static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004218menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004219 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004220 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004221{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004222 struct menu *menu = data;
4223
4224 if (widget == menu->widget)
4225 menu_set_item(data, y);
4226
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004227 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004228}
4229
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004230static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004231menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004232 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004233{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004234 struct menu *menu = data;
4235
4236 if (widget == menu->widget)
4237 menu_set_item(data, y);
4238
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004239 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004240}
4241
4242static void
4243menu_leave_handler(struct widget *widget, struct input *input, void *data)
4244{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004245 struct menu *menu = data;
4246
4247 if (widget == menu->widget)
4248 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004249}
4250
4251static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004252menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004253 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004254 uint32_t button, enum wl_pointer_button_state state,
4255 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004256
4257{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004258 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004259
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004260 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4261 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004262 /* Either relase after press-drag-release or
4263 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004264 menu->func(menu->window->parent,
4265 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004266 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004267 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004268 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004269 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004270 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004271}
4272
4273static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004274menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004275{
4276 cairo_t *cr;
4277 const int32_t r = 3, margin = 3;
4278 struct menu *menu = data;
4279 int32_t width, height, i;
4280
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004281 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004282 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4283 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4284 cairo_paint(cr);
4285
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004286 width = widget->allocation.width;
4287 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004288 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004289
4290 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004291 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4292 cairo_fill(cr);
4293
4294 for (i = 0; i < menu->count; i++) {
4295 if (i == menu->current) {
4296 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4297 cairo_rectangle(cr, margin, i * 20 + margin,
4298 width - 2 * margin, 20);
4299 cairo_fill(cr);
4300 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4301 cairo_move_to(cr, 10, i * 20 + 16);
4302 cairo_show_text(cr, menu->entries[i]);
4303 } else {
4304 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4305 cairo_move_to(cr, 10, i * 20 + 16);
4306 cairo_show_text(cr, menu->entries[i]);
4307 }
4308 }
4309
4310 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004311}
4312
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004313void
4314window_show_menu(struct display *display,
4315 struct input *input, uint32_t time, struct window *parent,
4316 int32_t x, int32_t y,
4317 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004318{
4319 struct window *window;
4320 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004321 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004322
4323 menu = malloc(sizeof *menu);
4324 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004325 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004326
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004327 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004328 if (!window) {
4329 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004330 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004331 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004332
4333 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004334 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004335 menu->entries = entries;
4336 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004337 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004338 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004339 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004340 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004341 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004342 window->type = TYPE_MENU;
4343 window->x = x;
4344 window->y = y;
4345
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004346 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004347 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004348 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004349 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004350 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004351
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004352 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004353 widget_set_enter_handler(menu->widget, menu_enter_handler);
4354 widget_set_leave_handler(menu->widget, menu_leave_handler);
4355 widget_set_motion_handler(menu->widget, menu_motion_handler);
4356 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004357
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004358 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004359 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004360}
4361
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004362void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004363window_set_buffer_type(struct window *window, enum window_buffer_type type)
4364{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004365 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004366}
4367
Pekka Paalanen35e82632013-04-25 13:57:48 +03004368struct widget *
4369window_add_subsurface(struct window *window, void *data,
4370 enum subsurface_mode default_mode)
4371{
4372 struct widget *widget;
4373 struct surface *surface;
4374 struct wl_surface *parent;
4375 struct wl_subcompositor *subcompo = window->display->subcompositor;
4376
4377 if (!subcompo)
4378 return NULL;
4379
4380 surface = surface_create(window);
4381 widget = widget_create(window, surface, data);
4382 wl_list_init(&widget->link);
4383 surface->widget = widget;
4384
4385 parent = window->main_surface->surface;
4386 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4387 surface->surface,
4388 parent);
4389 surface->synchronized = 1;
4390
4391 switch (default_mode) {
4392 case SUBSURFACE_SYNCHRONIZED:
4393 surface->synchronized_default = 1;
4394 break;
4395 case SUBSURFACE_DESYNCHRONIZED:
4396 surface->synchronized_default = 0;
4397 break;
4398 default:
4399 assert(!"bad enum subsurface_mode");
4400 }
4401
4402 return widget;
4403}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004404
4405static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004406display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004407 struct wl_output *wl_output,
4408 int x, int y,
4409 int physical_width,
4410 int physical_height,
4411 int subpixel,
4412 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004413 const char *model,
4414 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004415{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004416 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004417
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004418 output->allocation.x = x;
4419 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004420 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004421}
4422
4423static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004424display_handle_done(void *data,
4425 struct wl_output *wl_output)
4426{
4427}
4428
4429static void
4430display_handle_scale(void *data,
4431 struct wl_output *wl_output,
4432 uint32_t scale)
4433{
4434 struct output *output = data;
4435
4436 output->scale = scale;
4437}
4438
4439static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004440display_handle_mode(void *data,
4441 struct wl_output *wl_output,
4442 uint32_t flags,
4443 int width,
4444 int height,
4445 int refresh)
4446{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004447 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004448 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004449
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004450 if (flags & WL_OUTPUT_MODE_CURRENT) {
4451 output->allocation.width = width;
4452 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004453 if (display->output_configure_handler)
4454 (*display->output_configure_handler)(
4455 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004456 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004457}
4458
4459static const struct wl_output_listener output_listener = {
4460 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004461 display_handle_mode,
4462 display_handle_done,
4463 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004464};
4465
4466static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004467display_add_output(struct display *d, uint32_t id)
4468{
4469 struct output *output;
4470
4471 output = malloc(sizeof *output);
4472 if (output == NULL)
4473 return;
4474
4475 memset(output, 0, sizeof *output);
4476 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004477 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004478 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004479 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004480 wl_list_insert(d->output_list.prev, &output->link);
4481
4482 wl_output_add_listener(output->output, &output_listener, output);
4483}
4484
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004485static void
4486output_destroy(struct output *output)
4487{
4488 if (output->destroy_handler)
4489 (*output->destroy_handler)(output, output->user_data);
4490
4491 wl_output_destroy(output->output);
4492 wl_list_remove(&output->link);
4493 free(output);
4494}
4495
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004496void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004497display_set_global_handler(struct display *display,
4498 display_global_handler_t handler)
4499{
4500 struct global *global;
4501
4502 display->global_handler = handler;
4503 if (!handler)
4504 return;
4505
4506 wl_list_for_each(global, &display->global_list, link)
4507 display->global_handler(display,
4508 global->name, global->interface,
4509 global->version, display->user_data);
4510}
4511
4512void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004513display_set_output_configure_handler(struct display *display,
4514 display_output_handler_t handler)
4515{
4516 struct output *output;
4517
4518 display->output_configure_handler = handler;
4519 if (!handler)
4520 return;
4521
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004522 wl_list_for_each(output, &display->output_list, link) {
4523 if (output->allocation.width == 0 &&
4524 output->allocation.height == 0)
4525 continue;
4526
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004527 (*display->output_configure_handler)(output,
4528 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004529 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004530}
4531
4532void
4533output_set_user_data(struct output *output, void *data)
4534{
4535 output->user_data = data;
4536}
4537
4538void *
4539output_get_user_data(struct output *output)
4540{
4541 return output->user_data;
4542}
4543
4544void
4545output_set_destroy_handler(struct output *output,
4546 display_output_handler_t handler)
4547{
4548 output->destroy_handler = handler;
4549 /* FIXME: implement this, once we have way to remove outputs */
4550}
4551
4552void
Scott Moreau4e072362012-09-29 02:03:11 -06004553output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004554{
Scott Moreau4e072362012-09-29 02:03:11 -06004555 struct rectangle allocation = output->allocation;
4556
4557 switch (output->transform) {
4558 case WL_OUTPUT_TRANSFORM_90:
4559 case WL_OUTPUT_TRANSFORM_270:
4560 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4561 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4562 /* Swap width and height */
4563 allocation.width = output->allocation.height;
4564 allocation.height = output->allocation.width;
4565 break;
4566 }
4567
4568 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004569}
4570
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004571struct wl_output *
4572output_get_wl_output(struct output *output)
4573{
4574 return output->output;
4575}
4576
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004577enum wl_output_transform
4578output_get_transform(struct output *output)
4579{
4580 return output->transform;
4581}
4582
Alexander Larssonafd319a2013-05-22 14:41:27 +02004583uint32_t
4584output_get_scale(struct output *output)
4585{
4586 return output->scale;
4587}
4588
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004589static void
Daniel Stone97f68542012-05-30 16:32:01 +01004590fini_xkb(struct input *input)
4591{
4592 xkb_state_unref(input->xkb.state);
4593 xkb_map_unref(input->xkb.keymap);
4594}
4595
4596static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004597display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004598{
4599 struct input *input;
4600
4601 input = malloc(sizeof *input);
4602 if (input == NULL)
4603 return;
4604
4605 memset(input, 0, sizeof *input);
4606 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004607 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004608 input->pointer_focus = NULL;
4609 input->keyboard_focus = NULL;
4610 wl_list_insert(d->input_list.prev, &input->link);
4611
Daniel Stone37816df2012-05-16 18:45:18 +01004612 wl_seat_add_listener(input->seat, &seat_listener, input);
4613 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004614
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004615 input->data_device =
4616 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004617 input->seat);
4618 wl_data_device_add_listener(input->data_device, &data_device_listener,
4619 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004620
4621 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004622
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004623 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4624 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004625 input->repeat_task.run = keyboard_repeat_func;
4626 display_watch_fd(d, input->repeat_timer_fd,
4627 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004628}
4629
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004630static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004631input_destroy(struct input *input)
4632{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004633 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004634 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004635
4636 if (input->drag_offer)
4637 data_offer_destroy(input->drag_offer);
4638
4639 if (input->selection_offer)
4640 data_offer_destroy(input->selection_offer);
4641
4642 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004643 fini_xkb(input);
4644
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004645 wl_surface_destroy(input->pointer_surface);
4646
Pekka Paalanene1207c72011-12-16 12:02:09 +02004647 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004648 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004649 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004650 free(input);
4651}
4652
4653static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004654init_workspace_manager(struct display *d, uint32_t id)
4655{
4656 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004657 wl_registry_bind(d->registry, id,
4658 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004659 if (d->workspace_manager != NULL)
4660 workspace_manager_add_listener(d->workspace_manager,
4661 &workspace_manager_listener,
4662 d);
4663}
4664
4665static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004666registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4667 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004668{
4669 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004670 struct global *global;
4671
4672 global = malloc(sizeof *global);
4673 global->name = id;
4674 global->interface = strdup(interface);
4675 global->version = version;
4676 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004677
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004678 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004679 d->compositor = wl_registry_bind(registry, id,
4680 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004681 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004682 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004683 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004684 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004685 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004686 d->shell = wl_registry_bind(registry,
4687 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004688 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004689 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004690 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4691 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004692 wl_registry_bind(registry, id,
4693 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004694 } else if (strcmp(interface, "text_cursor_position") == 0) {
4695 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004696 wl_registry_bind(registry, id,
4697 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004698 } else if (strcmp(interface, "workspace_manager") == 0) {
4699 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004700 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4701 d->subcompositor =
4702 wl_registry_bind(registry, id,
4703 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004704 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004705
4706 if (d->global_handler)
4707 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004708}
4709
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004710static void
4711registry_handle_global_remove(void *data, struct wl_registry *registry,
4712 uint32_t name)
4713{
4714 struct display *d = data;
4715 struct global *global;
4716 struct global *tmp;
4717
4718 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4719 if (global->name != name)
4720 continue;
4721
4722 /* XXX: Should destroy bound globals, and call
4723 * the counterpart of display::global_handler
4724 */
4725 wl_list_remove(&global->link);
4726 free(global->interface);
4727 free(global);
4728 }
4729}
4730
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004731void *
4732display_bind(struct display *display, uint32_t name,
4733 const struct wl_interface *interface, uint32_t version)
4734{
4735 return wl_registry_bind(display->registry, name, interface, version);
4736}
4737
4738static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004739 registry_handle_global,
4740 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004741};
4742
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004743#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004744static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004745init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004746{
4747 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004748 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004749
Rob Clark6396ed32012-03-11 19:48:41 -05004750#ifdef USE_CAIRO_GLESV2
4751# define GL_BIT EGL_OPENGL_ES2_BIT
4752#else
4753# define GL_BIT EGL_OPENGL_BIT
4754#endif
4755
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004756 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004757 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004758 EGL_RED_SIZE, 1,
4759 EGL_GREEN_SIZE, 1,
4760 EGL_BLUE_SIZE, 1,
4761 EGL_ALPHA_SIZE, 1,
4762 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004763 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004764 EGL_NONE
4765 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004766
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004767#ifdef USE_CAIRO_GLESV2
4768 static const EGLint context_attribs[] = {
4769 EGL_CONTEXT_CLIENT_VERSION, 2,
4770 EGL_NONE
4771 };
4772 EGLint api = EGL_OPENGL_ES_API;
4773#else
4774 EGLint *context_attribs = NULL;
4775 EGLint api = EGL_OPENGL_API;
4776#endif
4777
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004778 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004779 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004780 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004781 return -1;
4782 }
4783
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004784 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004785 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004786 return -1;
4787 }
4788
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004789 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4790 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004791 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004792 return -1;
4793 }
4794
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004795 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004796 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004797 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004798 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004799 return -1;
4800 }
4801
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004802 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004803 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004804 return -1;
4805 }
4806
Benjamin Franzke0c991632011-09-27 21:57:31 +02004807 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4808 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004809 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004810 return -1;
4811 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004812
4813 return 0;
4814}
4815
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004816static void
4817fini_egl(struct display *display)
4818{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004819 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004820
4821 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4822 EGL_NO_CONTEXT);
4823
4824 eglTerminate(display->dpy);
4825 eglReleaseThread();
4826}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004827#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004828
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004829static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004830init_dummy_surface(struct display *display)
4831{
4832 int len;
4833 void *data;
4834
4835 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4836 data = malloc(len);
4837 display->dummy_surface =
4838 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4839 1, 1, len);
4840 display->dummy_surface_data = data;
4841}
4842
4843static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004844handle_display_data(struct task *task, uint32_t events)
4845{
4846 struct display *display =
4847 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004848 struct epoll_event ep;
4849 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004850
4851 display->display_fd_events = events;
4852
4853 if (events & EPOLLERR || events & EPOLLHUP) {
4854 display_exit(display);
4855 return;
4856 }
4857
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004858 if (events & EPOLLIN) {
4859 ret = wl_display_dispatch(display->display);
4860 if (ret == -1) {
4861 display_exit(display);
4862 return;
4863 }
4864 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004865
4866 if (events & EPOLLOUT) {
4867 ret = wl_display_flush(display->display);
4868 if (ret == 0) {
4869 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4870 ep.data.ptr = &display->display_task;
4871 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4872 display->display_fd, &ep);
4873 } else if (ret == -1 && errno != EAGAIN) {
4874 display_exit(display);
4875 return;
4876 }
4877 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004878}
4879
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004880static void
4881log_handler(const char *format, va_list args)
4882{
4883 vfprintf(stderr, format, args);
4884}
4885
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004886struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004887display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004888{
4889 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004890
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004891 wl_log_set_handler_client(log_handler);
4892
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004893 d = malloc(sizeof *d);
4894 if (d == NULL)
4895 return NULL;
4896
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004897 memset(d, 0, sizeof *d);
4898
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004899 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004900 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004901 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004902 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004903 return NULL;
4904 }
4905
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004906 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004907 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004908 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004909 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4910 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004911
4912 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004913 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004914 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004915 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004916
Daniel Stone97f68542012-05-30 16:32:01 +01004917 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004918 if (d->xkb_context == NULL) {
4919 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004920 return NULL;
4921 }
4922
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004923 d->workspace = 0;
4924 d->workspace_count = 1;
4925
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004926 d->registry = wl_display_get_registry(d->display);
4927 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004928
4929 if (wl_display_dispatch(d->display) < 0) {
4930 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4931 return NULL;
4932 }
4933
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004934#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004935 if (init_egl(d) < 0)
4936 fprintf(stderr, "EGL does not seem to work, "
4937 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004938#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004939
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004940 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004941
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004942 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004943
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004944 wl_list_init(&d->window_list);
4945
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004946 init_dummy_surface(d);
4947
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004948 return d;
4949}
4950
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004951static void
4952display_destroy_outputs(struct display *display)
4953{
4954 struct output *tmp;
4955 struct output *output;
4956
4957 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4958 output_destroy(output);
4959}
4960
Pekka Paalanene1207c72011-12-16 12:02:09 +02004961static void
4962display_destroy_inputs(struct display *display)
4963{
4964 struct input *tmp;
4965 struct input *input;
4966
4967 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4968 input_destroy(input);
4969}
4970
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004971void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004972display_destroy(struct display *display)
4973{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004974 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004975 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4976 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004977
4978 if (!wl_list_empty(&display->deferred_list))
4979 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4980
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004981 cairo_surface_destroy(display->dummy_surface);
4982 free(display->dummy_surface_data);
4983
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004984 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004985 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004986
Daniel Stone97f68542012-05-30 16:32:01 +01004987 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004988
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004989 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004990 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004991
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004992#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004993 if (display->argb_device)
4994 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004995#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004996
Pekka Paalanen35e82632013-04-25 13:57:48 +03004997 if (display->subcompositor)
4998 wl_subcompositor_destroy(display->subcompositor);
4999
Pekka Paalanenc2052982011-12-16 11:41:32 +02005000 if (display->shell)
5001 wl_shell_destroy(display->shell);
5002
5003 if (display->shm)
5004 wl_shm_destroy(display->shm);
5005
5006 if (display->data_device_manager)
5007 wl_data_device_manager_destroy(display->data_device_manager);
5008
5009 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005010 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005011
5012 close(display->epoll_fd);
5013
U. Artie Eoff44874d92012-10-02 21:12:35 -07005014 if (!(display->display_fd_events & EPOLLERR) &&
5015 !(display->display_fd_events & EPOLLHUP))
5016 wl_display_flush(display->display);
5017
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005018 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005019 free(display);
5020}
5021
5022void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005023display_set_user_data(struct display *display, void *data)
5024{
5025 display->user_data = data;
5026}
5027
5028void *
5029display_get_user_data(struct display *display)
5030{
5031 return display->user_data;
5032}
5033
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005034struct wl_display *
5035display_get_display(struct display *display)
5036{
5037 return display->display;
5038}
5039
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005040struct output *
5041display_get_output(struct display *display)
5042{
5043 return container_of(display->output_list.next, struct output, link);
5044}
5045
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005046struct wl_compositor *
5047display_get_compositor(struct display *display)
5048{
5049 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005050}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005051
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005052uint32_t
5053display_get_serial(struct display *display)
5054{
5055 return display->serial;
5056}
5057
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005058EGLDisplay
5059display_get_egl_display(struct display *d)
5060{
5061 return d->dpy;
5062}
5063
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005064struct wl_data_source *
5065display_create_data_source(struct display *display)
5066{
5067 return wl_data_device_manager_create_data_source(display->data_device_manager);
5068}
5069
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005070EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005071display_get_argb_egl_config(struct display *d)
5072{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005073 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005074}
5075
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005076struct wl_shell *
5077display_get_shell(struct display *display)
5078{
5079 return display->shell;
5080}
5081
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005082int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005083display_acquire_window_surface(struct display *display,
5084 struct window *window,
5085 EGLContext ctx)
5086{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005087 struct surface *surface = window->main_surface;
5088
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005089 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005090 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005091
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005092 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005093 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005094}
5095
5096void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005097display_release_window_surface(struct display *display,
5098 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005099{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005100 struct surface *surface = window->main_surface;
5101
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005102 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005103 return;
5104
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005105 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005106}
5107
5108void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005109display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005110{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005111 wl_list_insert(&display->deferred_list, &task->link);
5112}
5113
5114void
5115display_watch_fd(struct display *display,
5116 int fd, uint32_t events, struct task *task)
5117{
5118 struct epoll_event ep;
5119
5120 ep.events = events;
5121 ep.data.ptr = task;
5122 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5123}
5124
5125void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005126display_unwatch_fd(struct display *display, int fd)
5127{
5128 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5129}
5130
5131void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005132display_run(struct display *display)
5133{
5134 struct task *task;
5135 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005136 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005137
Pekka Paalanen826d7952011-12-15 10:14:07 +02005138 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005139 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005140 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005141 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005142 struct task, link);
5143 wl_list_remove(&task->link);
5144 task->run(task, 0);
5145 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005146
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005147 wl_display_dispatch_pending(display->display);
5148
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005149 if (!display->running)
5150 break;
5151
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005152 ret = wl_display_flush(display->display);
5153 if (ret < 0 && errno == EAGAIN) {
5154 ep[0].events =
5155 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5156 ep[0].data.ptr = &display->display_task;
5157
5158 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5159 display->display_fd, &ep[0]);
5160 } else if (ret < 0) {
5161 break;
5162 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005163
5164 count = epoll_wait(display->epoll_fd,
5165 ep, ARRAY_LENGTH(ep), -1);
5166 for (i = 0; i < count; i++) {
5167 task = ep[i].data.ptr;
5168 task->run(task, ep[i].events);
5169 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005170 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005171}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005172
5173void
5174display_exit(struct display *display)
5175{
5176 display->running = 0;
5177}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005178
5179void
5180keysym_modifiers_add(struct wl_array *modifiers_map,
5181 const char *name)
5182{
5183 size_t len = strlen(name) + 1;
5184 char *p;
5185
5186 p = wl_array_add(modifiers_map, len);
5187
5188 if (p == NULL)
5189 return;
5190
5191 strncpy(p, name, len);
5192}
5193
5194static xkb_mod_index_t
5195keysym_modifiers_get_index(struct wl_array *modifiers_map,
5196 const char *name)
5197{
5198 xkb_mod_index_t index = 0;
5199 char *p = modifiers_map->data;
5200
5201 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5202 if (strcmp(p, name) == 0)
5203 return index;
5204
5205 index++;
5206 p += strlen(p) + 1;
5207 }
5208
5209 return XKB_MOD_INVALID;
5210}
5211
5212xkb_mod_mask_t
5213keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5214 const char *name)
5215{
5216 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5217
5218 if (index == XKB_MOD_INVALID)
5219 return XKB_MOD_INVALID;
5220
5221 return 1 << index;
5222}