blob: 5099004d5bc0be0c3190402f376fb3ff6d8b6ed3 [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
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025
Kristian Høgsberg61017b12008-11-02 18:51:48 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030029#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030042#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030055#else /* HAVE_CAIRO_EGL */
56typedef void *EGLDisplay;
57typedef void *EGLConfig;
58typedef void *EGLContext;
59#define EGL_NO_DISPLAY ((EGLDisplay)0)
60#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061
Daniel Stone9d4f0302012-02-15 16:33:21 +000062#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030063#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020066#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040067#include "../shared/cairo-util.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050068#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070075struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030076
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040077struct global {
78 uint32_t name;
79 char *interface;
80 uint32_t version;
81 struct wl_list link;
82};
83
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040086 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050087 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030088 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050093 struct xdg_shell *xdg_shell;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800121 display_global_handler_t global_handler_remove;
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;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200133
134 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100135 int seat_version;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530136 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500137};
138
Rob Bradford7507b572012-05-15 17:55:34 +0100139struct window_output {
140 struct output *output;
141 struct wl_list link;
142};
143
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200144struct toysurface {
145 /*
146 * Prepare the surface for drawing. Makes sure there is a surface
147 * of the right size available for rendering, and returns it.
148 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200149 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200150 * If flags has SURFACE_HINT_RESIZE set, the user is
151 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200152 * Returns the Cairo surface to draw to.
153 */
154 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200155 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200156 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157
158 /*
159 * Post the surface to the server, returning the server allocation
160 * rectangle. The Cairo surface from prepare() must be destroyed
161 * after calling this.
162 */
163 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200164 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200165 struct rectangle *server_allocation);
166
167 /*
168 * Make the toysurface current with the given EGL context.
169 * Returns 0 on success, and negative of failure.
170 */
171 int (*acquire)(struct toysurface *base, EGLContext ctx);
172
173 /*
174 * Release the toysurface from the EGL context, returning control
175 * to Cairo.
176 */
177 void (*release)(struct toysurface *base);
178
179 /*
180 * Destroy the toysurface, including the Cairo surface, any
181 * backing storage, and the Wayland protocol objects.
182 */
183 void (*destroy)(struct toysurface *base);
184};
185
Pekka Paalanen4e373742013-02-13 16:17:13 +0200186struct surface {
187 struct window *window;
188
189 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300190 struct wl_subsurface *subsurface;
191 int synchronized;
192 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200193 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200194 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300195 int redraw_needed;
196 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300197 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200198
199 struct rectangle allocation;
200 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200201
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200202 struct wl_region *input_region;
203 struct wl_region *opaque_region;
204
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200205 enum window_buffer_type buffer_type;
206 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200207 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200208
209 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300210
211 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200212};
213
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500214struct window {
215 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100216 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500217 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200218 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400219 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500220 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200221 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500222 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400223 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300224 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400225 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400226 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500227 int custom;
228 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400229
Pekka Paalanen99436862012-11-19 17:15:59 +0200230 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400231
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500232 int fullscreen;
233 int maximized;
234
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200235 enum preferred_format preferred_format;
236
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500237 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500238 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400239 window_data_handler_t data_handler;
240 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500241 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400242 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200243 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700244 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400245
Pekka Paalanen4e373742013-02-13 16:17:13 +0200246 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500247 struct xdg_surface *xdg_surface;
248 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300249
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700250 struct window *parent;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200251 struct wl_surface *last_parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500252
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500253 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500254
Pekka Paalanen35e82632013-04-25 13:57:48 +0300255 /* struct surface::link, contains also main_surface */
256 struct wl_list subsurface_list;
257
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500258 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400259 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500260};
261
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500262struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500263 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200264 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300265 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500266 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400267 struct wl_list link;
268 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500269 widget_resize_handler_t resize_handler;
270 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500271 widget_enter_handler_t enter_handler;
272 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500273 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500274 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700275 widget_touch_down_handler_t touch_down_handler;
276 widget_touch_up_handler_t touch_up_handler;
277 widget_touch_motion_handler_t touch_motion_handler;
278 widget_touch_frame_handler_t touch_frame_handler;
279 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200280 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400281 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500282 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300283 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500284 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000285 /* If this is set to false then no cairo surface will be
286 * created before redrawing the surface. This is useful if the
287 * redraw handler is going to do completely custom rendering
288 * such as using EGL directly */
289 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400290};
291
Rusty Lynch041815a2013-08-08 21:20:38 -0700292struct touch_point {
293 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800294 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700295 struct widget *widget;
296 struct wl_list link;
297};
298
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400299struct input {
300 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100301 struct wl_seat *seat;
302 struct wl_pointer *pointer;
303 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700304 struct wl_touch *touch;
305 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400306 struct window *pointer_focus;
307 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700308 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300309 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300310 uint32_t cursor_anim_start;
311 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300312 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400313 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400314 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400315 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400316 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400317 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400318
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500319 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500320 struct widget *grab;
321 uint32_t grab_button;
322
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400323 struct wl_data_device *data_device;
324 struct data_offer *drag_offer;
325 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800326 uint32_t touch_grab;
327 int32_t touch_grab_id;
328 float drag_x, drag_y;
329 struct window *drag_focus;
330 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100331
332 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100333 struct xkb_keymap *keymap;
334 struct xkb_state *state;
335 xkb_mod_mask_t control_mask;
336 xkb_mod_mask_t alt_mask;
337 xkb_mod_mask_t shift_mask;
338 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400339
Jonny Lamb06959082014-08-12 14:58:27 +0200340 int32_t repeat_rate_sec;
341 int32_t repeat_rate_nsec;
342 int32_t repeat_delay_sec;
343 int32_t repeat_delay_nsec;
344
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400345 struct task repeat_task;
346 int repeat_timer_fd;
347 uint32_t repeat_sym;
348 uint32_t repeat_key;
349 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400350};
351
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500352struct output {
353 struct display *display;
354 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800355 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500356 struct rectangle allocation;
357 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600358 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200359 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500360 char *make;
361 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200362
363 display_output_handler_t destroy_handler;
364 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500365};
366
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500367struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500368 struct widget *widget;
369 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500370 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800371
372 uint32_t last_time;
373 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800374 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500375};
376
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500377struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400378 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500379 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500380 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500381 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700382 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500383 const char **entries;
384 uint32_t time;
385 int current;
386 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400387 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500388 menu_func_t func;
389};
390
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300391struct tooltip {
392 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300393 struct widget *widget;
394 char *entry;
395 struct task tooltip_task;
396 int tooltip_fd;
397 float x, y;
398};
399
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700400struct shm_pool {
401 struct wl_shm_pool *pool;
402 size_t size;
403 size_t used;
404 void *data;
405};
406
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400407enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300408 CURSOR_DEFAULT = 100,
409 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400410};
411
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200412static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400413
Pekka Paalanen97777442013-05-22 10:20:05 +0300414/* #define DEBUG */
415
416#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300417
418static void
419debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
420__attribute__ ((format (printf, 4, 5)));
421
422static void
423debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
424{
425 va_list ap;
426 struct timeval tv;
427
428 gettimeofday(&tv, NULL);
429 fprintf(stderr, "%8ld.%03ld ",
430 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
431
432 if (proxy)
433 fprintf(stderr, "%s@%d ",
434 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
435
436 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
437 fprintf(stderr, "%s ", func);
438
439 va_start(ap, fmt);
440 vfprintf(stderr, fmt, ap);
441 va_end(ap);
442}
443
444#define DBG(fmt, ...) \
445 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
446
447#define DBG_OBJ(obj, fmt, ...) \
448 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
449
450#else
451
452#define DBG(...) do {} while (0)
453#define DBG_OBJ(...) do {} while (0)
454
455#endif
456
Alexander Larsson1818e312013-05-22 14:41:31 +0200457static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200458surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200459{
460 int32_t tmp;
461
462 switch (buffer_transform) {
463 case WL_OUTPUT_TRANSFORM_90:
464 case WL_OUTPUT_TRANSFORM_270:
465 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
466 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
467 tmp = *width;
468 *width = *height;
469 *height = tmp;
470 break;
471 default:
472 break;
473 }
474
475 *width *= buffer_scale;
476 *height *= buffer_scale;
477}
478
479static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200480buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200481{
482 int32_t tmp;
483
484 switch (buffer_transform) {
485 case WL_OUTPUT_TRANSFORM_90:
486 case WL_OUTPUT_TRANSFORM_270:
487 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
488 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
489 tmp = *width;
490 *width = *height;
491 *height = tmp;
492 break;
493 default:
494 break;
495 }
496
497 *width /= buffer_scale;
498 *height /= buffer_scale;
499}
500
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500501#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400502
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200503struct egl_window_surface {
504 struct toysurface base;
505 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100506 struct display *display;
507 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200508 struct wl_egl_window *egl_window;
509 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100510};
511
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200512static struct egl_window_surface *
513to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100514{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100516}
517
518static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200520 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200521 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100522{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 struct egl_window_surface *surface = to_egl_window_surface(base);
524
Alexander Larsson1818e312013-05-22 14:41:31 +0200525 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
526
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
528 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
529
530 return cairo_surface_reference(surface->cairo_surface);
531}
532
533static void
534egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200535 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200536 struct rectangle *server_allocation)
537{
538 struct egl_window_surface *surface = to_egl_window_surface(base);
539
540 cairo_gl_surface_swapbuffers(surface->cairo_surface);
541 wl_egl_window_get_attached_size(surface->egl_window,
542 &server_allocation->width,
543 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200544
545 buffer_to_surface_size (buffer_transform, buffer_scale,
546 &server_allocation->width,
547 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200548}
549
550static int
551egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
552{
553 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200554 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100555
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200556 device = cairo_surface_get_device(surface->cairo_surface);
557 if (!device)
558 return -1;
559
560 if (!ctx) {
561 if (device == surface->display->argb_device)
562 ctx = surface->display->argb_ctx;
563 else
564 assert(0);
565 }
566
567 cairo_device_flush(device);
568 cairo_device_acquire(device);
569 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
570 surface->egl_surface, ctx))
571 fprintf(stderr, "failed to make surface current\n");
572
573 return 0;
574}
575
576static void
577egl_window_surface_release(struct toysurface *base)
578{
579 struct egl_window_surface *surface = to_egl_window_surface(base);
580 cairo_device_t *device;
581
582 device = cairo_surface_get_device(surface->cairo_surface);
583 if (!device)
584 return;
585
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200586 if (!eglMakeCurrent(surface->display->dpy,
587 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200588 fprintf(stderr, "failed to make context current\n");
589
590 cairo_device_release(device);
591}
592
593static void
594egl_window_surface_destroy(struct toysurface *base)
595{
596 struct egl_window_surface *surface = to_egl_window_surface(base);
597 struct display *d = surface->display;
598
599 cairo_surface_destroy(surface->cairo_surface);
600 eglDestroySurface(d->dpy, surface->egl_surface);
601 wl_egl_window_destroy(surface->egl_window);
602 surface->surface = NULL;
603
604 free(surface);
605}
606
607static struct toysurface *
608egl_window_surface_create(struct display *display,
609 struct wl_surface *wl_surface,
610 uint32_t flags,
611 struct rectangle *rectangle)
612{
613 struct egl_window_surface *surface;
614
Pekka Paalanenb3627362012-11-19 17:16:00 +0200615 if (display->dpy == EGL_NO_DISPLAY)
616 return NULL;
617
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200618 surface = calloc(1, sizeof *surface);
619 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100620 return NULL;
621
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200622 surface->base.prepare = egl_window_surface_prepare;
623 surface->base.swap = egl_window_surface_swap;
624 surface->base.acquire = egl_window_surface_acquire;
625 surface->base.release = egl_window_surface_release;
626 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100627
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200628 surface->display = display;
629 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400630
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200631 surface->egl_window = wl_egl_window_create(surface->surface,
632 rectangle->width,
633 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200635 surface->egl_surface = eglCreateWindowSurface(display->dpy,
636 display->argb_config,
637 surface->egl_window,
638 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100639
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200640 surface->cairo_surface =
641 cairo_gl_surface_create_for_egl(display->argb_device,
642 surface->egl_surface,
643 rectangle->width,
644 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100645
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200646 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647}
648
Pekka Paalanenb3627362012-11-19 17:16:00 +0200649#else
650
651static struct toysurface *
652egl_window_surface_create(struct display *display,
653 struct wl_surface *wl_surface,
654 uint32_t flags,
655 struct rectangle *rectangle)
656{
657 return NULL;
658}
659
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400660#endif
661
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200662struct shm_surface_data {
663 struct wl_buffer *buffer;
664 struct shm_pool *pool;
665};
666
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400667struct wl_buffer *
668display_get_buffer_for_surface(struct display *display,
669 cairo_surface_t *surface)
670{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200671 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400672
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200673 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674
675 return data->buffer;
676}
677
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500678static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700679shm_pool_destroy(struct shm_pool *pool);
680
681static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682shm_surface_data_destroy(void *p)
683{
684 struct shm_surface_data *data = p;
685
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200686 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700687 if (data->pool)
688 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300689
690 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400691}
692
Kristian Høgsberg16626282012-04-03 11:21:27 -0400693static struct wl_shm_pool *
694make_shm_pool(struct display *display, int size, void **data)
695{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400696 struct wl_shm_pool *pool;
697 int fd;
698
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300699 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400700 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300701 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
702 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400703 return NULL;
704 }
705
706 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400707 if (*data == MAP_FAILED) {
708 fprintf(stderr, "mmap failed: %m\n");
709 close(fd);
710 return NULL;
711 }
712
713 pool = wl_shm_create_pool(display->shm, fd, size);
714
715 close(fd);
716
717 return pool;
718}
719
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700720static struct shm_pool *
721shm_pool_create(struct display *display, size_t size)
722{
723 struct shm_pool *pool = malloc(sizeof *pool);
724
725 if (!pool)
726 return NULL;
727
728 pool->pool = make_shm_pool(display, size, &pool->data);
729 if (!pool->pool) {
730 free(pool);
731 return NULL;
732 }
733
734 pool->size = size;
735 pool->used = 0;
736
737 return pool;
738}
739
740static void *
741shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
742{
743 if (pool->used + size > pool->size)
744 return NULL;
745
746 *offset = pool->used;
747 pool->used += size;
748
749 return (char *) pool->data + *offset;
750}
751
752/* destroy the pool. this does not unmap the memory though */
753static void
754shm_pool_destroy(struct shm_pool *pool)
755{
756 munmap(pool->data, pool->size);
757 wl_shm_pool_destroy(pool->pool);
758 free(pool);
759}
760
761/* Start allocating from the beginning of the pool again */
762static void
763shm_pool_reset(struct shm_pool *pool)
764{
765 pool->used = 0;
766}
767
768static int
769data_length_for_shm_surface(struct rectangle *rect)
770{
771 int stride;
772
773 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
774 rect->width);
775 return stride * rect->height;
776}
777
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500778static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700779display_create_shm_surface_from_pool(struct display *display,
780 struct rectangle *rectangle,
781 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400782{
783 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400784 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400785 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200786 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700787 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400788 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400789
790 data = malloc(sizeof *data);
791 if (data == NULL)
792 return NULL;
793
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200794 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
795 cairo_format = CAIRO_FORMAT_RGB16_565;
796 else
797 cairo_format = CAIRO_FORMAT_ARGB32;
798
799 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700800 length = stride * rectangle->height;
801 data->pool = NULL;
802 map = shm_pool_allocate(pool, length, &offset);
803
804 if (!map) {
805 free(data);
806 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400807 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400808
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400809 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200810 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400811 rectangle->width,
812 rectangle->height,
813 stride);
814
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200815 cairo_surface_set_user_data(surface, &shm_surface_data_key,
816 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400817
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200818 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
819 format = WL_SHM_FORMAT_RGB565;
820 else {
821 if (flags & SURFACE_OPAQUE)
822 format = WL_SHM_FORMAT_XRGB8888;
823 else
824 format = WL_SHM_FORMAT_ARGB8888;
825 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400826
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200827 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
828 rectangle->width,
829 rectangle->height,
830 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700832 return surface;
833}
834
835static cairo_surface_t *
836display_create_shm_surface(struct display *display,
837 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200838 struct shm_pool *alternate_pool,
839 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700840{
841 struct shm_surface_data *data;
842 struct shm_pool *pool;
843 cairo_surface_t *surface;
844
Pekka Paalanen99436862012-11-19 17:15:59 +0200845 if (alternate_pool) {
846 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700847 surface = display_create_shm_surface_from_pool(display,
848 rectangle,
849 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200850 alternate_pool);
851 if (surface) {
852 data = cairo_surface_get_user_data(surface,
853 &shm_surface_data_key);
854 goto out;
855 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700856 }
857
858 pool = shm_pool_create(display,
859 data_length_for_shm_surface(rectangle));
860 if (!pool)
861 return NULL;
862
863 surface =
864 display_create_shm_surface_from_pool(display, rectangle,
865 flags, pool);
866
867 if (!surface) {
868 shm_pool_destroy(pool);
869 return NULL;
870 }
871
872 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200873 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700874 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400875
Pekka Paalanen99436862012-11-19 17:15:59 +0200876out:
877 if (data_ret)
878 *data_ret = data;
879
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400880 return surface;
881}
882
nobled7b87cb02011-02-01 18:51:47 +0000883static int
884check_size(struct rectangle *rect)
885{
886 if (rect->width && rect->height)
887 return 0;
888
889 fprintf(stderr, "tried to create surface of "
890 "width: %d, height: %d\n", rect->width, rect->height);
891 return -1;
892}
893
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894cairo_surface_t *
895display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100896 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400897 struct rectangle *rectangle,
898 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400899{
nobled7b87cb02011-02-01 18:51:47 +0000900 if (check_size(rectangle) < 0)
901 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200902
903 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200904 return display_create_shm_surface(display, rectangle, flags,
905 NULL, NULL);
906}
907
Pekka Paalanena4eda732012-11-19 17:16:02 +0200908struct shm_surface_leaf {
909 cairo_surface_t *cairo_surface;
910 /* 'data' is automatically destroyed, when 'cairo_surface' is */
911 struct shm_surface_data *data;
912
913 struct shm_pool *resize_pool;
914 int busy;
915};
916
917static void
918shm_surface_leaf_release(struct shm_surface_leaf *leaf)
919{
920 if (leaf->cairo_surface)
921 cairo_surface_destroy(leaf->cairo_surface);
922 /* leaf->data already destroyed via cairo private */
923
924 if (leaf->resize_pool)
925 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200926
927 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200928}
929
Pekka Paalanenaef02542013-04-25 13:57:47 +0300930#define MAX_LEAVES 3
931
Pekka Paalanen99436862012-11-19 17:15:59 +0200932struct shm_surface {
933 struct toysurface base;
934 struct display *display;
935 struct wl_surface *surface;
936 uint32_t flags;
937 int dx, dy;
938
Pekka Paalanenaef02542013-04-25 13:57:47 +0300939 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200940 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200941};
942
943static struct shm_surface *
944to_shm_surface(struct toysurface *base)
945{
946 return container_of(base, struct shm_surface, base);
947}
948
Pekka Paalanena4eda732012-11-19 17:16:02 +0200949static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300950shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
951{
952#ifdef DEBUG
953 struct shm_surface_leaf *leaf;
954 char bufs[MAX_LEAVES + 1];
955 int i;
956
957 for (i = 0; i < MAX_LEAVES; i++) {
958 leaf = &surface->leaf[i];
959
960 if (leaf->busy)
961 bufs[i] = 'b';
962 else if (leaf->cairo_surface)
963 bufs[i] = 'a';
964 else
965 bufs[i] = ' ';
966 }
967
968 bufs[MAX_LEAVES] = '\0';
969 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
970#endif
971}
972
973static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200974shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
975{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200976 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300977 struct shm_surface_leaf *leaf;
978 int i;
979 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300980
981 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200982
Pekka Paalanenaef02542013-04-25 13:57:47 +0300983 for (i = 0; i < MAX_LEAVES; i++) {
984 leaf = &surface->leaf[i];
985 if (leaf->data && leaf->data->buffer == buffer) {
986 leaf->busy = 0;
987 break;
988 }
989 }
990 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200991
Pekka Paalanenaef02542013-04-25 13:57:47 +0300992 /* Leave one free leaf with storage, release others */
993 free_found = 0;
994 for (i = 0; i < MAX_LEAVES; i++) {
995 leaf = &surface->leaf[i];
996
997 if (!leaf->cairo_surface || leaf->busy)
998 continue;
999
1000 if (!free_found)
1001 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001002 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001003 shm_surface_leaf_release(leaf);
1004 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001005
Pekka Paalanen97777442013-05-22 10:20:05 +03001006 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001007}
1008
1009static const struct wl_buffer_listener shm_surface_buffer_listener = {
1010 shm_surface_buffer_release
1011};
1012
Pekka Paalanen99436862012-11-19 17:15:59 +02001013static cairo_surface_t *
1014shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001015 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001016 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001017{
Pekka Paalanenec076692012-11-30 13:37:27 +02001018 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001019 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001020 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001021 struct shm_surface_leaf *leaf = NULL;
1022 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001023
1024 surface->dx = dx;
1025 surface->dy = dy;
1026
Pekka Paalanenaef02542013-04-25 13:57:47 +03001027 /* pick a free buffer, preferrably one that already has storage */
1028 for (i = 0; i < MAX_LEAVES; i++) {
1029 if (surface->leaf[i].busy)
1030 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001031
Pekka Paalanenaef02542013-04-25 13:57:47 +03001032 if (!leaf || surface->leaf[i].cairo_surface)
1033 leaf = &surface->leaf[i];
1034 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001035 DBG_OBJ(surface->surface, "pick leaf %d\n",
1036 (int)(leaf - &surface->leaf[0]));
1037
Pekka Paalanenaef02542013-04-25 13:57:47 +03001038 if (!leaf) {
1039 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001040 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001041 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001042 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001043 }
1044
Pekka Paalanena4eda732012-11-19 17:16:02 +02001045 if (!resize_hint && leaf->resize_pool) {
1046 cairo_surface_destroy(leaf->cairo_surface);
1047 leaf->cairo_surface = NULL;
1048 shm_pool_destroy(leaf->resize_pool);
1049 leaf->resize_pool = NULL;
1050 }
1051
Alexander Larsson1818e312013-05-22 14:41:31 +02001052 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1053
Pekka Paalanena4eda732012-11-19 17:16:02 +02001054 if (leaf->cairo_surface &&
1055 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1056 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001057 goto out;
1058
Pekka Paalanena4eda732012-11-19 17:16:02 +02001059 if (leaf->cairo_surface)
1060 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001061
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001062#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001063 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001064 /* Create a big pool to allocate from, while continuously
1065 * resizing. Mmapping a new pool in the server
1066 * is relatively expensive, so reusing a pool performs
1067 * better, but may temporarily reserve unneeded memory.
1068 */
1069 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001070 leaf->resize_pool = shm_pool_create(surface->display,
1071 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001072 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001073#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001074
Alexander Larsson1818e312013-05-22 14:41:31 +02001075 rect.width = width;
1076 rect.height = height;
1077
Pekka Paalanena4eda732012-11-19 17:16:02 +02001078 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001079 display_create_shm_surface(surface->display, &rect,
1080 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001081 leaf->resize_pool,
1082 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001083 if (!leaf->cairo_surface)
1084 return NULL;
1085
Pekka Paalanena4eda732012-11-19 17:16:02 +02001086 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001087 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001088
1089out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001090 surface->current = leaf;
1091
1092 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001093}
1094
1095static void
1096shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001097 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001098 struct rectangle *server_allocation)
1099{
1100 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001101 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001102
1103 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001105 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001106 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001107
Alexander Larsson1818e312013-05-22 14:41:31 +02001108 buffer_to_surface_size (buffer_transform, buffer_scale,
1109 &server_allocation->width,
1110 &server_allocation->height);
1111
Pekka Paalanena4eda732012-11-19 17:16:02 +02001112 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001113 surface->dx, surface->dy);
1114 wl_surface_damage(surface->surface, 0, 0,
1115 server_allocation->width, server_allocation->height);
1116 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001117
Pekka Paalanen71233882013-04-25 13:57:53 +03001118 DBG_OBJ(surface->surface, "leaf %d busy\n",
1119 (int)(leaf - &surface->leaf[0]));
1120
Pekka Paalanena4eda732012-11-19 17:16:02 +02001121 leaf->busy = 1;
1122 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001123}
1124
1125static int
1126shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1127{
1128 return -1;
1129}
1130
1131static void
1132shm_surface_release(struct toysurface *base)
1133{
1134}
1135
1136static void
1137shm_surface_destroy(struct toysurface *base)
1138{
1139 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001140 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001141
Pekka Paalanenaef02542013-04-25 13:57:47 +03001142 for (i = 0; i < MAX_LEAVES; i++)
1143 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001144
1145 free(surface);
1146}
1147
1148static struct toysurface *
1149shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1150 uint32_t flags, struct rectangle *rectangle)
1151{
1152 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001153 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001154
Brian Lovinbc919262013-08-07 15:34:59 -07001155 surface = xmalloc(sizeof *surface);
1156 memset(surface, 0, sizeof *surface);
1157
Pekka Paalanen99436862012-11-19 17:15:59 +02001158 if (!surface)
1159 return NULL;
1160
1161 surface->base.prepare = shm_surface_prepare;
1162 surface->base.swap = shm_surface_swap;
1163 surface->base.acquire = shm_surface_acquire;
1164 surface->base.release = shm_surface_release;
1165 surface->base.destroy = shm_surface_destroy;
1166
1167 surface->display = display;
1168 surface->surface = wl_surface;
1169 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001170
1171 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001172}
1173
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001174/*
1175 * The following correspondences between file names and cursors was copied
1176 * from: https://bugs.kde.org/attachment.cgi?id=67313
1177 */
1178
1179static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001180 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001181 "sw-resize",
1182 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001183};
1184
1185static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001186 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001187 "se-resize",
1188 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001189};
1190
1191static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001192 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001193 "s-resize",
1194 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001195};
1196
1197static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001198 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001199 "closedhand",
1200 "208530c400c041818281048008011002"
1201};
1202
1203static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001204 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001205 "default",
1206 "top_left_arrow",
1207 "left-arrow"
1208};
1209
1210static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001211 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001212 "w-resize",
1213 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001214};
1215
1216static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001217 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001218 "e-resize",
1219 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001220};
1221
1222static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001223 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001224 "nw-resize",
1225 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001226};
1227
1228static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001229 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001230 "ne-resize",
1231 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001232};
1233
1234static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001235 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001236 "n-resize",
1237 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001238};
1239
1240static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001241 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242 "ibeam",
1243 "text"
1244};
1245
1246static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001247 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248 "pointer",
1249 "pointing_hand",
1250 "e29285e634086352946a0e7090d73106"
1251};
1252
1253static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001254 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001255 "wait",
1256 "0426c94ea35c87780ff01dc239897213"
1257};
1258
1259struct cursor_alternatives {
1260 const char **names;
1261 size_t count;
1262};
1263
1264static const struct cursor_alternatives cursors[] = {
1265 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1266 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1267 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1268 {grabbings, ARRAY_LENGTH(grabbings)},
1269 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1270 {left_sides, ARRAY_LENGTH(left_sides)},
1271 {right_sides, ARRAY_LENGTH(right_sides)},
1272 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1273 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1274 {top_sides, ARRAY_LENGTH(top_sides)},
1275 {xterms, ARRAY_LENGTH(xterms)},
1276 {hand1s, ARRAY_LENGTH(hand1s)},
1277 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001278};
1279
1280static void
1281create_cursors(struct display *display)
1282{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001283 struct weston_config *config;
1284 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001285 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001286 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001287 unsigned int i, j;
1288 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001289
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001290 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001291 s = weston_config_get_section(config, "shell", NULL, NULL);
1292 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1293 weston_config_section_get_int(s, "cursor-size", &size, 32);
1294 weston_config_destroy(config);
1295
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001296 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001297 if (!display->cursor_theme) {
1298 fprintf(stderr, "could not load theme '%s'\n", theme);
1299 return;
1300 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001301 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001302 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001303 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001304
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001305 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001306 cursor = NULL;
1307 for (j = 0; !cursor && j < cursors[i].count; ++j)
1308 cursor = wl_cursor_theme_get_cursor(
1309 display->cursor_theme, cursors[i].names[j]);
1310
1311 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001312 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001313 cursors[i].names[0]);
1314
1315 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001316 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001317}
1318
1319static void
1320destroy_cursors(struct display *display)
1321{
1322 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001323 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001324}
1325
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001326struct wl_cursor_image *
1327display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001328{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001329 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001330
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001331 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001332}
1333
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001334static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001335surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001336{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001337 if (!surface->cairo_surface)
1338 return;
1339
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001340 if (surface->opaque_region) {
1341 wl_surface_set_opaque_region(surface->surface,
1342 surface->opaque_region);
1343 wl_region_destroy(surface->opaque_region);
1344 surface->opaque_region = NULL;
1345 }
1346
1347 if (surface->input_region) {
1348 wl_surface_set_input_region(surface->surface,
1349 surface->input_region);
1350 wl_region_destroy(surface->input_region);
1351 surface->input_region = NULL;
1352 }
1353
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001354 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001355 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001356 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001357
Pekka Paalanen89dee002013-02-13 16:17:20 +02001358 cairo_surface_destroy(surface->cairo_surface);
1359 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001360}
1361
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001362int
1363window_has_focus(struct window *window)
1364{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001365 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001366}
1367
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001368static void
1369window_close(struct window *window)
1370{
1371 if (window->close_handler)
1372 window->close_handler(window->user_data);
1373 else
1374 display_exit(window->display);
1375}
1376
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001377struct display *
1378window_get_display(struct window *window)
1379{
1380 return window->display;
1381}
1382
Pekka Paalanen89dee002013-02-13 16:17:20 +02001383static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001384surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001385{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001386 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001387 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001388
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001389 if (!surface->toysurface && display->dpy &&
1390 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001391 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001392 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001393 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001394 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001395 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001396 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001397
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001398 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001399 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001400 surface->surface,
1401 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001402
Pekka Paalanen89dee002013-02-13 16:17:20 +02001403 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001404 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001405 allocation.width, allocation.height, flags,
1406 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407}
1408
1409static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001410window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001411{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001412 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001413 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001414
Pekka Paalanenec076692012-11-30 13:37:27 +02001415 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001416 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001417
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001418 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1419 flags |= SURFACE_HINT_RGB565;
1420
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001421 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001422}
1423
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001424int
1425window_get_buffer_transform(struct window *window)
1426{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001427 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001428}
1429
1430void
1431window_set_buffer_transform(struct window *window,
1432 enum wl_output_transform transform)
1433{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001434 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001435 wl_surface_set_buffer_transform(window->main_surface->surface,
1436 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001437}
1438
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001439void
1440window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001441 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001442{
1443 window->main_surface->buffer_scale = scale;
1444 wl_surface_set_buffer_scale(window->main_surface->surface,
1445 scale);
1446}
1447
1448uint32_t
1449window_get_buffer_scale(struct window *window)
1450{
1451 return window->main_surface->buffer_scale;
1452}
1453
Alexander Larssond68f5232013-05-22 14:41:33 +02001454uint32_t
1455window_get_output_scale(struct window *window)
1456{
1457 struct window_output *window_output;
1458 struct window_output *window_output_tmp;
1459 int scale = 1;
1460
1461 wl_list_for_each_safe(window_output, window_output_tmp,
1462 &window->window_output_list, link) {
1463 if (window_output->output->scale > scale)
1464 scale = window_output->output->scale;
1465 }
1466
1467 return scale;
1468}
1469
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001470static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001471
Pekka Paalanen4e373742013-02-13 16:17:13 +02001472static void
1473surface_destroy(struct surface *surface)
1474{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001475 if (surface->frame_cb)
1476 wl_callback_destroy(surface->frame_cb);
1477
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001478 if (surface->input_region)
1479 wl_region_destroy(surface->input_region);
1480
1481 if (surface->opaque_region)
1482 wl_region_destroy(surface->opaque_region);
1483
Pekka Paalanen35e82632013-04-25 13:57:48 +03001484 if (surface->subsurface)
1485 wl_subsurface_destroy(surface->subsurface);
1486
Pekka Paalanen4e373742013-02-13 16:17:13 +02001487 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001488
1489 if (surface->toysurface)
1490 surface->toysurface->destroy(surface->toysurface);
1491
Pekka Paalanen35e82632013-04-25 13:57:48 +03001492 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001493 free(surface);
1494}
1495
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001496void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001497window_destroy(struct window *window)
1498{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001499 struct display *display = window->display;
1500 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001501 struct window_output *window_output;
1502 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001503
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001504 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001505
Rusty Lynch1084da52013-08-15 09:10:08 -07001506 wl_list_for_each(input, &display->input_list, link) {
1507 if (input->touch_focus == window)
1508 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001509 if (input->pointer_focus == window)
1510 input->pointer_focus = NULL;
1511 if (input->keyboard_focus == window)
1512 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001513 if (input->focus_widget &&
1514 input->focus_widget->window == window)
1515 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001516 }
1517
Rob Bradford7507b572012-05-15 17:55:34 +01001518 wl_list_for_each_safe(window_output, window_output_tmp,
1519 &window->window_output_list, link) {
1520 free (window_output);
1521 }
1522
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001523 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001524 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001525
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001526 if (window->xdg_surface)
1527 xdg_surface_destroy(window->xdg_surface);
1528 if (window->xdg_popup)
1529 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001530
1531 surface_destroy(window->main_surface);
1532
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001533 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001534
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001535 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001536 free(window);
1537}
1538
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001539static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001540widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001541{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001542 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001543
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001544 wl_list_for_each(child, &widget->child_list, link) {
1545 target = widget_find_widget(child, x, y);
1546 if (target)
1547 return target;
1548 }
1549
1550 if (widget->allocation.x <= x &&
1551 x < widget->allocation.x + widget->allocation.width &&
1552 widget->allocation.y <= y &&
1553 y < widget->allocation.y + widget->allocation.height) {
1554 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001555 }
1556
1557 return NULL;
1558}
1559
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001560static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001561window_find_widget(struct window *window, int32_t x, int32_t y)
1562{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001563 struct surface *surface;
1564 struct widget *widget;
1565
1566 wl_list_for_each(surface, &window->subsurface_list, link) {
1567 widget = widget_find_widget(surface->widget, x, y);
1568 if (widget)
1569 return widget;
1570 }
1571
1572 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001573}
1574
1575static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001576widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001577{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001578 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001579
Peter Huttererf3d62272013-08-08 11:57:05 +10001580 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001581 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001582 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001583 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001584 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001585 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001586 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001587 widget->tooltip = NULL;
1588 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001589 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001590 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001591
1592 return widget;
1593}
1594
1595struct widget *
1596window_add_widget(struct window *window, void *data)
1597{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001598 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001599
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001600 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001601 wl_list_init(&widget->link);
1602 window->main_surface->widget = widget;
1603
1604 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001605}
1606
1607struct widget *
1608widget_add_widget(struct widget *parent, void *data)
1609{
1610 struct widget *widget;
1611
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001612 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001613 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001614
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001615 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001616}
1617
1618void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001620{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001621 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001622 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001623 struct input *input;
1624
Pekka Paalanen35e82632013-04-25 13:57:48 +03001625 /* Destroy the sub-surface along with the root widget */
1626 if (surface->widget == widget && surface->subsurface)
1627 surface_destroy(widget->surface);
1628
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001629 if (widget->tooltip)
1630 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001631
Pekka Paalanene156fb62012-01-19 13:51:38 +02001632 wl_list_for_each(input, &display->input_list, link) {
1633 if (input->focus_widget == widget)
1634 input->focus_widget = NULL;
1635 }
1636
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001637 wl_list_remove(&widget->link);
1638 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001639}
1640
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001641void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001642widget_set_default_cursor(struct widget *widget, int cursor)
1643{
1644 widget->default_cursor = cursor;
1645}
1646
1647void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001648widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001649{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001650 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001651}
1652
1653void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001654widget_set_size(struct widget *widget, int32_t width, int32_t height)
1655{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001656 widget->allocation.width = width;
1657 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001658}
1659
1660void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001661widget_set_allocation(struct widget *widget,
1662 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001664 widget->allocation.x = x;
1665 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001666 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001667}
1668
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001669void
1670widget_set_transparent(struct widget *widget, int transparent)
1671{
1672 widget->opaque = !transparent;
1673}
1674
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001675void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001676widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001677{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001678 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001679}
1680
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001681static cairo_surface_t *
1682widget_get_cairo_surface(struct widget *widget)
1683{
1684 struct surface *surface = widget->surface;
1685 struct window *window = widget->window;
1686
Neil Roberts97b747c2013-12-19 16:17:12 +00001687 assert(widget->use_cairo);
1688
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001689 if (!surface->cairo_surface) {
1690 if (surface == window->main_surface)
1691 window_create_main_surface(window);
1692 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001693 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001694 }
1695
1696 return surface->cairo_surface;
1697}
1698
Alexander Larsson15901f02013-05-22 14:41:25 +02001699static void
1700widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1701{
1702 struct surface *surface = widget->surface;
1703 double angle;
1704 cairo_matrix_t m;
1705 enum wl_output_transform transform;
1706 int surface_width, surface_height;
1707 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001708 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001709
1710 surface_width = surface->allocation.width;
1711 surface_height = surface->allocation.height;
1712
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001713 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001714 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001715
Alexander Larsson15901f02013-05-22 14:41:25 +02001716 switch (transform) {
1717 case WL_OUTPUT_TRANSFORM_FLIPPED:
1718 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1719 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1720 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1721 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1722 break;
1723 default:
1724 cairo_matrix_init_identity(&m);
1725 break;
1726 }
1727
1728 switch (transform) {
1729 case WL_OUTPUT_TRANSFORM_NORMAL:
1730 default:
1731 angle = 0;
1732 translate_x = 0;
1733 translate_y = 0;
1734 break;
1735 case WL_OUTPUT_TRANSFORM_FLIPPED:
1736 angle = 0;
1737 translate_x = surface_width;
1738 translate_y = 0;
1739 break;
1740 case WL_OUTPUT_TRANSFORM_90:
1741 angle = M_PI_2;
1742 translate_x = surface_height;
1743 translate_y = 0;
1744 break;
1745 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1746 angle = M_PI_2;
1747 translate_x = surface_height;
1748 translate_y = surface_width;
1749 break;
1750 case WL_OUTPUT_TRANSFORM_180:
1751 angle = M_PI;
1752 translate_x = surface_width;
1753 translate_y = surface_height;
1754 break;
1755 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1756 angle = M_PI;
1757 translate_x = 0;
1758 translate_y = surface_height;
1759 break;
1760 case WL_OUTPUT_TRANSFORM_270:
1761 angle = M_PI + M_PI_2;
1762 translate_x = 0;
1763 translate_y = surface_width;
1764 break;
1765 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1766 angle = M_PI + M_PI_2;
1767 translate_x = 0;
1768 translate_y = 0;
1769 break;
1770 }
1771
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001772 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001773 cairo_translate(cr, translate_x, translate_y);
1774 cairo_rotate(cr, angle);
1775 cairo_transform(cr, &m);
1776}
1777
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001778cairo_t *
1779widget_cairo_create(struct widget *widget)
1780{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001781 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001782 cairo_surface_t *cairo_surface;
1783 cairo_t *cr;
1784
1785 cairo_surface = widget_get_cairo_surface(widget);
1786 cr = cairo_create(cairo_surface);
1787
Alexander Larsson15901f02013-05-22 14:41:25 +02001788 widget_cairo_update_transform(widget, cr);
1789
Pekka Paalanen35e82632013-04-25 13:57:48 +03001790 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1791
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001792 return cr;
1793}
1794
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001795struct wl_surface *
1796widget_get_wl_surface(struct widget *widget)
1797{
1798 return widget->surface->surface;
1799}
1800
Neil Roberts5e10a042013-09-09 00:40:17 +01001801struct wl_subsurface *
1802widget_get_wl_subsurface(struct widget *widget)
1803{
1804 return widget->surface->subsurface;
1805}
1806
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001807uint32_t
1808widget_get_last_time(struct widget *widget)
1809{
1810 return widget->surface->last_time;
1811}
1812
1813void
1814widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1815{
1816 struct wl_compositor *comp = widget->window->display->compositor;
1817 struct surface *surface = widget->surface;
1818
1819 if (!surface->input_region)
1820 surface->input_region = wl_compositor_create_region(comp);
1821
1822 if (rect) {
1823 wl_region_add(surface->input_region,
1824 rect->x, rect->y, rect->width, rect->height);
1825 }
1826}
1827
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001828void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001829widget_set_resize_handler(struct widget *widget,
1830 widget_resize_handler_t handler)
1831{
1832 widget->resize_handler = handler;
1833}
1834
1835void
1836widget_set_redraw_handler(struct widget *widget,
1837 widget_redraw_handler_t handler)
1838{
1839 widget->redraw_handler = handler;
1840}
1841
1842void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001843widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001844{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001845 widget->enter_handler = handler;
1846}
1847
1848void
1849widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1850{
1851 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001852}
1853
1854void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001855widget_set_motion_handler(struct widget *widget,
1856 widget_motion_handler_t handler)
1857{
1858 widget->motion_handler = handler;
1859}
1860
1861void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001862widget_set_button_handler(struct widget *widget,
1863 widget_button_handler_t handler)
1864{
1865 widget->button_handler = handler;
1866}
1867
1868void
Rusty Lynch041815a2013-08-08 21:20:38 -07001869widget_set_touch_up_handler(struct widget *widget,
1870 widget_touch_up_handler_t handler)
1871{
1872 widget->touch_up_handler = handler;
1873}
1874
1875void
1876widget_set_touch_down_handler(struct widget *widget,
1877 widget_touch_down_handler_t handler)
1878{
1879 widget->touch_down_handler = handler;
1880}
1881
1882void
1883widget_set_touch_motion_handler(struct widget *widget,
1884 widget_touch_motion_handler_t handler)
1885{
1886 widget->touch_motion_handler = handler;
1887}
1888
1889void
1890widget_set_touch_frame_handler(struct widget *widget,
1891 widget_touch_frame_handler_t handler)
1892{
1893 widget->touch_frame_handler = handler;
1894}
1895
1896void
1897widget_set_touch_cancel_handler(struct widget *widget,
1898 widget_touch_cancel_handler_t handler)
1899{
1900 widget->touch_cancel_handler = handler;
1901}
1902
1903void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001904widget_set_axis_handler(struct widget *widget,
1905 widget_axis_handler_t handler)
1906{
1907 widget->axis_handler = handler;
1908}
1909
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001910static void
1911window_schedule_redraw_task(struct window *window);
1912
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001913void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001914widget_schedule_redraw(struct widget *widget)
1915{
Pekka Paalanen71233882013-04-25 13:57:53 +03001916 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001917 widget->surface->redraw_needed = 1;
1918 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001919}
1920
Neil Roberts97b747c2013-12-19 16:17:12 +00001921void
1922widget_set_use_cairo(struct widget *widget,
1923 int use_cairo)
1924{
1925 widget->use_cairo = use_cairo;
1926}
1927
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001928cairo_surface_t *
1929window_get_surface(struct window *window)
1930{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001931 cairo_surface_t *cairo_surface;
1932
1933 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1934
1935 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001936}
1937
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001938struct wl_surface *
1939window_get_wl_surface(struct window *window)
1940{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001941 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001942}
1943
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001944static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001945tooltip_redraw_handler(struct widget *widget, void *data)
1946{
1947 cairo_t *cr;
1948 const int32_t r = 3;
1949 struct tooltip *tooltip = data;
1950 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001951
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001952 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001953 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001954 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1955 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1956 cairo_paint(cr);
1957
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001958 width = widget->allocation.width;
1959 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001960 rounded_rect(cr, 0, 0, width, height, r);
1961
1962 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1963 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1964 cairo_fill(cr);
1965
1966 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1967 cairo_move_to(cr, 10, 16);
1968 cairo_show_text(cr, tooltip->entry);
1969 cairo_destroy(cr);
1970}
1971
1972static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001973get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001974{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001975 cairo_t *cr;
1976 cairo_text_extents_t extents;
1977
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001978 /* Use the dummy_surface because tooltip's surface was not
1979 * created yet, and parent does not have a valid surface
1980 * outside repaint, either.
1981 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001982 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001983 cairo_text_extents(cr, tooltip->entry, &extents);
1984 cairo_destroy(cr);
1985
1986 return extents;
1987}
1988
1989static int
1990window_create_tooltip(struct tooltip *tooltip)
1991{
1992 struct widget *parent = tooltip->parent;
1993 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001994 const int offset_y = 27;
1995 const int margin = 3;
1996 cairo_text_extents_t extents;
1997
1998 if (tooltip->widget)
1999 return 0;
2000
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002001 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002002
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002003 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002004 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002005 widget_set_allocation(tooltip->widget,
2006 tooltip->x, tooltip->y + offset_y,
2007 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002008
2009 return 0;
2010}
2011
2012void
2013widget_destroy_tooltip(struct widget *parent)
2014{
2015 struct tooltip *tooltip = parent->tooltip;
2016
2017 parent->tooltip_count = 0;
2018 if (!tooltip)
2019 return;
2020
2021 if (tooltip->widget) {
2022 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002023 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002024 }
2025
2026 close(tooltip->tooltip_fd);
2027 free(tooltip->entry);
2028 free(tooltip);
2029 parent->tooltip = NULL;
2030}
2031
2032static void
2033tooltip_func(struct task *task, uint32_t events)
2034{
2035 struct tooltip *tooltip =
2036 container_of(task, struct tooltip, tooltip_task);
2037 uint64_t exp;
2038
Martin Olsson8df662a2012-07-08 03:03:47 +02002039 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2040 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002041 window_create_tooltip(tooltip);
2042}
2043
2044#define TOOLTIP_TIMEOUT 500
2045static int
2046tooltip_timer_reset(struct tooltip *tooltip)
2047{
2048 struct itimerspec its;
2049
2050 its.it_interval.tv_sec = 0;
2051 its.it_interval.tv_nsec = 0;
2052 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2053 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2054 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2055 fprintf(stderr, "could not set timerfd\n: %m");
2056 return -1;
2057 }
2058
2059 return 0;
2060}
2061
2062int
2063widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2064{
2065 struct tooltip *tooltip = parent->tooltip;
2066
2067 parent->tooltip_count++;
2068 if (tooltip) {
2069 tooltip->x = x;
2070 tooltip->y = y;
2071 tooltip_timer_reset(tooltip);
2072 return 0;
2073 }
2074
2075 /* the handler might be triggered too fast via input device motion, so
2076 * we need this check here to make sure tooltip is fully initialized */
2077 if (parent->tooltip_count > 1)
2078 return 0;
2079
2080 tooltip = malloc(sizeof *tooltip);
2081 if (!tooltip)
2082 return -1;
2083
2084 parent->tooltip = tooltip;
2085 tooltip->parent = parent;
2086 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002087 tooltip->x = x;
2088 tooltip->y = y;
2089 tooltip->entry = strdup(entry);
2090 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2091 if (tooltip->tooltip_fd < 0) {
2092 fprintf(stderr, "could not create timerfd\n: %m");
2093 return -1;
2094 }
2095
2096 tooltip->tooltip_task.run = tooltip_func;
2097 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2098 EPOLLIN, &tooltip->tooltip_task);
2099 tooltip_timer_reset(tooltip);
2100
2101 return 0;
2102}
2103
2104static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002105workspace_manager_state(void *data,
2106 struct workspace_manager *workspace_manager,
2107 uint32_t current,
2108 uint32_t count)
2109{
2110 struct display *display = data;
2111
2112 display->workspace = current;
2113 display->workspace_count = count;
2114}
2115
2116static const struct workspace_manager_listener workspace_manager_listener = {
2117 workspace_manager_state
2118};
2119
2120static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002121frame_resize_handler(struct widget *widget,
2122 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002123{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002124 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002125 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002126 struct rectangle interior;
2127 struct rectangle input;
2128 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002129
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002130 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002131 interior.x = 0;
2132 interior.y = 0;
2133 interior.width = width;
2134 interior.height = height;
2135 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002136 frame_resize(frame->frame, width, height);
2137 frame_interior(frame->frame, &interior.x, &interior.y,
2138 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002139 }
2140
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002141 widget_set_allocation(child, interior.x, interior.y,
2142 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002143
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002144 if (child->resize_handler) {
2145 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002146 child->user_data);
2147
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002148 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002149 width = child->allocation.width;
2150 height = child->allocation.height;
2151 } else {
2152 frame_resize_inside(frame->frame,
2153 child->allocation.width,
2154 child->allocation.height);
2155 width = frame_width(frame->frame);
2156 height = frame_height(frame->frame);
2157 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002158 }
2159
Scott Moreauf7e498c2012-05-14 11:39:29 -06002160 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002161
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002162 widget->surface->input_region =
2163 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002164 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002165 frame_input_rect(frame->frame, &input.x, &input.y,
2166 &input.width, &input.height);
2167 wl_region_add(widget->surface->input_region,
2168 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002169 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002170 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002171 }
2172
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002173 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002174
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002175 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002176 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002177 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2178 &opaque.width, &opaque.height);
2179
2180 wl_region_add(widget->surface->opaque_region,
2181 opaque.x, opaque.y,
2182 opaque.width, opaque.height);
2183 } else {
2184 wl_region_add(widget->surface->opaque_region,
2185 0, 0, width, height);
2186 }
Martin Minarik1998b152012-05-10 02:04:35 +02002187 }
2188
Martin Minarik1998b152012-05-10 02:04:35 +02002189
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191}
2192
2193static void
2194frame_redraw_handler(struct widget *widget, void *data)
2195{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002196 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002198 struct window *window = widget->window;
2199
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002200 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002201 return;
2202
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002203 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002204
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002205 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002206
2207 cairo_destroy(cr);
2208}
2209
2210static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002211frame_get_pointer_image_for_location(struct window_frame *frame,
2212 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002213{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002214 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002215
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002216 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002217 return CURSOR_LEFT_PTR;
2218
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002219 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002220 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002221 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002222 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002223 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002224 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002225 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002226 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002227 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002228 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002229 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002230 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002231 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002232 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002233 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002234 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002235 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002236 case THEME_LOCATION_EXTERIOR:
2237 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002238 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002239 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002240 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002241}
2242
Pekka Paalanen26237862014-09-10 15:10:30 +03002243static void
2244frame_menu_func(void *data, struct input *input, int index)
2245{
2246 struct window *window = data;
2247 struct display *display;
2248
2249 switch (index) {
2250 case 0: /* close */
2251 window_close(window);
2252 break;
2253 case 1: /* move to workspace above */
2254 display = window->display;
2255 if (display->workspace > 0)
2256 workspace_manager_move_surface(
2257 display->workspace_manager,
2258 window->main_surface->surface,
2259 display->workspace - 1);
2260 break;
2261 case 2: /* move to workspace below */
2262 display = window->display;
2263 if (display->workspace < display->workspace_count - 1)
2264 workspace_manager_move_surface(
2265 display->workspace_manager,
2266 window->main_surface->surface,
2267 display->workspace + 1);
2268 break;
2269 case 3: /* fullscreen */
2270 /* we don't have a way to get out of fullscreen for now */
2271 if (window->fullscreen_handler)
2272 window->fullscreen_handler(window, window->user_data);
2273 break;
2274 }
2275}
2276
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002277void
2278window_show_frame_menu(struct window *window,
2279 struct input *input, uint32_t time)
2280{
2281 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002282 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002283
Pekka Paalanen26237862014-09-10 15:10:30 +03002284 static const char *entries[] = {
2285 "Close",
2286 "Move to workspace above", "Move to workspace below",
2287 "Fullscreen"
2288 };
2289
2290 if (window->fullscreen_handler)
2291 count = ARRAY_LENGTH(entries);
2292 else
2293 count = ARRAY_LENGTH(entries) - 1;
2294
2295 input_get_position(input, &x, &y);
2296 window_show_menu(window->display, input, time, window,
2297 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002298}
2299
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002300static int
2301frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002302 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002303{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002304 struct window_frame *frame = data;
2305 enum theme_location location;
2306
2307 location = frame_pointer_enter(frame->frame, input, x, y);
2308 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2309 widget_schedule_redraw(frame->widget);
2310
2311 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002312}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002313
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002314static int
2315frame_motion_handler(struct widget *widget,
2316 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002317 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002318{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002319 struct window_frame *frame = data;
2320 enum theme_location location;
2321
2322 location = frame_pointer_motion(frame->frame, input, x, y);
2323 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2324 widget_schedule_redraw(frame->widget);
2325
2326 return frame_get_pointer_image_for_location(data, location);
2327}
2328
2329static void
2330frame_leave_handler(struct widget *widget,
2331 struct input *input, void *data)
2332{
2333 struct window_frame *frame = data;
2334
2335 frame_pointer_leave(frame->frame, input);
2336 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2337 widget_schedule_redraw(frame->widget);
2338}
2339
2340static void
2341frame_handle_status(struct window_frame *frame, struct input *input,
2342 uint32_t time, enum theme_location location)
2343{
2344 struct window *window = frame->widget->window;
2345 uint32_t status;
2346
2347 status = frame_status(frame->frame);
2348 if (status & FRAME_STATUS_REPAINT)
2349 widget_schedule_redraw(frame->widget);
2350
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002351 if (status & FRAME_STATUS_MINIMIZE) {
2352 window_set_minimized(window);
2353 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2354 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002355
2356 if (status & FRAME_STATUS_MENU) {
2357 window_show_frame_menu(window, input, time);
2358 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2359 }
2360
2361 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002362 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002363 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2364 }
2365
2366 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002367 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002368 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002369 }
2370
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002371 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002372 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002373 xdg_surface_move(window->xdg_surface,
2374 input_get_seat(input),
2375 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002376
2377 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2378 }
2379
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002380 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002381 input_ungrab(input);
2382
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002383 xdg_surface_resize(window->xdg_surface,
2384 input_get_seat(input),
2385 window->display->serial,
2386 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002387
2388 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2389 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002390}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002391
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002392#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002393static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002394frame_button_handler(struct widget *widget,
2395 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002396 uint32_t button, enum wl_pointer_button_state state,
2397 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002398
2399{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002400 struct window_frame *frame = data;
2401 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002402
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002403 frame->double_click = 0;
2404 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2405 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2406 frame->double_click = 1;
2407 frame->did_double = 1;
2408 } else
2409 frame->did_double = 0;
2410
2411 frame->last_time = time;
2412 } else if (frame->did_double == 1) {
2413 frame->double_click = 1;
2414 frame->did_double = 0;
2415 }
2416
2417 if (frame->double_click)
2418 location = frame_double_click(frame->frame, input,
2419 button, state);
2420 else
2421 location = frame_pointer_button(frame->frame, input,
2422 button, state);
2423
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002424 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002425}
2426
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002427static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002428frame_touch_down_handler(struct widget *widget, struct input *input,
2429 uint32_t serial, uint32_t time, int32_t id,
2430 float x, float y, void *data)
2431{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002432 struct window_frame *frame = data;
2433
Xiong Zhang382de462014-06-12 11:06:26 +08002434 frame->double_click = 0;
2435 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2436 frame->last_id == id) {
2437 frame->double_click = 1;
2438 frame->did_double = 1;
2439 frame->double_id = id;
2440 } else
2441 frame->did_double = 0;
2442
2443 frame->last_time = time;
2444 frame->last_id = id;
2445
2446 if (frame->double_click)
2447 frame_double_touch_down(frame->frame, input, id, x, y);
2448 else
2449 frame_touch_down(frame->frame, input, id, x, y);
2450
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002451 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2452}
2453
2454static void
2455frame_touch_up_handler(struct widget *widget,
2456 struct input *input, uint32_t serial, uint32_t time,
2457 int32_t id, void *data)
2458{
2459 struct window_frame *frame = data;
2460
Xiong Zhang382de462014-06-12 11:06:26 +08002461 if (frame->double_id == id && frame->did_double) {
2462 frame->did_double = 0;
2463 frame->double_id = 0;
2464 frame_double_touch_up(frame->frame, input, id);
2465 } else
2466 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002467 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002468}
2469
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002470struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002471window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002472{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002473 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002474 uint32_t buttons;
2475
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002476 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002477 buttons = FRAME_BUTTON_NONE;
2478 } else {
2479 buttons = FRAME_BUTTON_ALL;
2480 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002481
Peter Huttererf3d62272013-08-08 11:57:05 +10002482 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002483 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002484 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002485
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002486 frame->widget = window_add_widget(window, frame);
2487 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002488
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002489 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2490 widget_set_resize_handler(frame->widget, frame_resize_handler);
2491 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002492 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002493 widget_set_motion_handler(frame->widget, frame_motion_handler);
2494 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002495 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002496 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002497
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002498 window->frame = frame;
2499
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002500 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002501}
2502
Kristian Høgsberga1627922012-06-20 17:30:03 -04002503void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002504window_frame_set_child_size(struct widget *widget, int child_width,
2505 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002506{
2507 struct display *display = widget->window->display;
2508 struct theme *t = display->theme;
2509 int decoration_width, decoration_height;
2510 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002511 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002512
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002513 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002514 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002515 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002516 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002517
2518 width = child_width + decoration_width;
2519 height = child_height + decoration_height;
2520 } else {
2521 width = child_width;
2522 height = child_height;
2523 }
2524
2525 window_schedule_resize(widget->window, width, height);
2526}
2527
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002528static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002529window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002530{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002531 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002532
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002533 /* frame->child must be destroyed by the application */
2534 widget_destroy(frame->widget);
2535 free(frame);
2536}
2537
2538static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002539input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002540 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002541{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002542 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002543 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002544
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002545 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002546 return;
2547
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002548 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002549 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002550 widget = old;
2551 if (input->grab)
2552 widget = input->grab;
2553 if (widget->leave_handler)
2554 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002555 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002556 }
2557
2558 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002559 widget = focus;
2560 if (input->grab)
2561 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002562 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002563 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002564 cursor = widget->enter_handler(focus, input, x, y,
2565 widget->user_data);
2566 else
2567 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002568
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002569 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002570 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002571}
2572
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002573void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002574touch_grab(struct input *input, int32_t touch_id)
2575{
2576 input->touch_grab = 1;
2577 input->touch_grab_id = touch_id;
2578}
2579
2580void
2581touch_ungrab(struct input *input)
2582{
2583 struct touch_point *tp, *tmp;
2584
2585 input->touch_grab = 0;
2586
2587 wl_list_for_each_safe(tp, tmp,
2588 &input->touch_point_list, link) {
2589 if (tp->id != input->touch_grab_id)
2590 continue;
2591 wl_list_remove(&tp->link);
2592 free(tp);
2593
2594 return;
2595 }
2596}
2597
2598void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002599input_grab(struct input *input, struct widget *widget, uint32_t button)
2600{
2601 input->grab = widget;
2602 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002603
2604 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002605}
2606
2607void
2608input_ungrab(struct input *input)
2609{
2610 struct widget *widget;
2611
2612 input->grab = NULL;
2613 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002614 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002615 input->sx, input->sy);
2616 input_set_focus_widget(input, widget, input->sx, input->sy);
2617 }
2618}
2619
2620static void
2621input_remove_pointer_focus(struct input *input)
2622{
2623 struct window *window = input->pointer_focus;
2624
2625 if (!window)
2626 return;
2627
2628 input_set_focus_widget(input, NULL, 0, 0);
2629
2630 input->pointer_focus = NULL;
2631 input->current_cursor = CURSOR_UNSET;
2632}
2633
2634static void
2635pointer_handle_enter(void *data, struct wl_pointer *pointer,
2636 uint32_t serial, struct wl_surface *surface,
2637 wl_fixed_t sx_w, wl_fixed_t sy_w)
2638{
2639 struct input *input = data;
2640 struct window *window;
2641 struct widget *widget;
2642 float sx = wl_fixed_to_double(sx_w);
2643 float sy = wl_fixed_to_double(sy_w);
2644
2645 if (!surface) {
2646 /* enter event for a window we've just destroyed */
2647 return;
2648 }
2649
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002650 window = wl_surface_get_user_data(surface);
2651 if (surface != window->main_surface->surface) {
2652 DBG("Ignoring input event from subsurface %p\n", surface);
2653 return;
2654 }
2655
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002656 input->display->serial = serial;
2657 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002658 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002659
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002660 input->sx = sx;
2661 input->sy = sy;
2662
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002663 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002664 input_set_focus_widget(input, widget, sx, sy);
2665}
2666
2667static void
2668pointer_handle_leave(void *data, struct wl_pointer *pointer,
2669 uint32_t serial, struct wl_surface *surface)
2670{
2671 struct input *input = data;
2672
2673 input->display->serial = serial;
2674 input_remove_pointer_focus(input);
2675}
2676
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002677static void
Daniel Stone37816df2012-05-16 18:45:18 +01002678pointer_handle_motion(void *data, struct wl_pointer *pointer,
2679 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002680{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002681 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002682 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002683 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002684 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002685 float sx = wl_fixed_to_double(sx_w);
2686 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002687
Paul Winwoodb22bf572013-08-29 10:52:54 +01002688 if (!window)
2689 return;
2690
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002691 input->sx = sx;
2692 input->sy = sy;
2693
Rob Bradford5f087742013-07-11 19:41:27 +01002694 /* when making the window smaller - e.g. after a unmaximise we might
2695 * still have a pending motion event that the compositor has picked
2696 * based on the old surface dimensions
2697 */
2698 if (sx > window->main_surface->allocation.width ||
2699 sy > window->main_surface->allocation.height)
2700 return;
2701
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002702 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002703 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002704 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002705 }
2706
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002707 if (input->grab)
2708 widget = input->grab;
2709 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002710 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002711 if (widget) {
2712 if (widget->motion_handler)
2713 cursor = widget->motion_handler(input->focus_widget,
2714 input, time, sx, sy,
2715 widget->user_data);
2716 else
2717 cursor = widget->default_cursor;
2718 } else
2719 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002720
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002721 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002722}
2723
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002724static void
Daniel Stone37816df2012-05-16 18:45:18 +01002725pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002726 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002727{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002728 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002729 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002730 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002731
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002732 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002733 if (input->focus_widget && input->grab == NULL &&
2734 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002735 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002736
Neil Roberts6b28aad2012-01-23 19:11:18 +00002737 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002738 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002739 (*widget->button_handler)(widget,
2740 input, time,
2741 button, state,
2742 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002743
Daniel Stone4dbadb12012-05-30 16:31:51 +01002744 if (input->grab && input->grab_button == button &&
2745 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002746 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002747}
2748
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002749static void
Daniel Stone37816df2012-05-16 18:45:18 +01002750pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002751 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002752{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002753 struct input *input = data;
2754 struct widget *widget;
2755
2756 widget = input->focus_widget;
2757 if (input->grab)
2758 widget = input->grab;
2759 if (widget && widget->axis_handler)
2760 (*widget->axis_handler)(widget,
2761 input, time,
2762 axis, value,
2763 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002764}
2765
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002766static const struct wl_pointer_listener pointer_listener = {
2767 pointer_handle_enter,
2768 pointer_handle_leave,
2769 pointer_handle_motion,
2770 pointer_handle_button,
2771 pointer_handle_axis,
2772};
2773
2774static void
2775input_remove_keyboard_focus(struct input *input)
2776{
2777 struct window *window = input->keyboard_focus;
2778 struct itimerspec its;
2779
2780 its.it_interval.tv_sec = 0;
2781 its.it_interval.tv_nsec = 0;
2782 its.it_value.tv_sec = 0;
2783 its.it_value.tv_nsec = 0;
2784 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2785
2786 if (!window)
2787 return;
2788
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002789 if (window->keyboard_focus_handler)
2790 (*window->keyboard_focus_handler)(window, NULL,
2791 window->user_data);
2792
2793 input->keyboard_focus = NULL;
2794}
2795
2796static void
2797keyboard_repeat_func(struct task *task, uint32_t events)
2798{
2799 struct input *input =
2800 container_of(task, struct input, repeat_task);
2801 struct window *window = input->keyboard_focus;
2802 uint64_t exp;
2803
2804 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2805 /* If we change the timer between the fd becoming
2806 * readable and getting here, there'll be nothing to
2807 * read and we get EAGAIN. */
2808 return;
2809
2810 if (window && window->key_handler) {
2811 (*window->key_handler)(window, input, input->repeat_time,
2812 input->repeat_key, input->repeat_sym,
2813 WL_KEYBOARD_KEY_STATE_PRESSED,
2814 window->user_data);
2815 }
2816}
2817
2818static void
2819keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2820 uint32_t format, int fd, uint32_t size)
2821{
2822 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002823 struct xkb_keymap *keymap;
2824 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002825 char *map_str;
2826
2827 if (!data) {
2828 close(fd);
2829 return;
2830 }
2831
2832 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2833 close(fd);
2834 return;
2835 }
2836
2837 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2838 if (map_str == MAP_FAILED) {
2839 close(fd);
2840 return;
2841 }
2842
Ran Benita2e1968f2014-08-19 23:59:51 +03002843 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2844 map_str,
2845 XKB_KEYMAP_FORMAT_TEXT_V1,
2846 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002847 munmap(map_str, size);
2848 close(fd);
2849
Rui Matos3eccb862013-10-10 19:44:22 +02002850 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002851 fprintf(stderr, "failed to compile keymap\n");
2852 return;
2853 }
2854
Rui Matos3eccb862013-10-10 19:44:22 +02002855 state = xkb_state_new(keymap);
2856 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002857 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002858 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002859 return;
2860 }
2861
Rui Matos3eccb862013-10-10 19:44:22 +02002862 xkb_keymap_unref(input->xkb.keymap);
2863 xkb_state_unref(input->xkb.state);
2864 input->xkb.keymap = keymap;
2865 input->xkb.state = state;
2866
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002867 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002868 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002869 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002870 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002871 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002872 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002873}
2874
2875static void
2876keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2877 uint32_t serial, struct wl_surface *surface,
2878 struct wl_array *keys)
2879{
2880 struct input *input = data;
2881 struct window *window;
2882
2883 input->display->serial = serial;
2884 input->keyboard_focus = wl_surface_get_user_data(surface);
2885
2886 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002887 if (window->keyboard_focus_handler)
2888 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002889 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002890}
2891
2892static void
2893keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2894 uint32_t serial, struct wl_surface *surface)
2895{
2896 struct input *input = data;
2897
2898 input->display->serial = serial;
2899 input_remove_keyboard_focus(input);
2900}
2901
Scott Moreau210d0792012-03-22 10:47:01 -06002902static void
Daniel Stone37816df2012-05-16 18:45:18 +01002903keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002904 uint32_t serial, uint32_t time, uint32_t key,
2905 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002906{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002907 struct input *input = data;
2908 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002909 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002910 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002911 const xkb_keysym_t *syms;
2912 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002913 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002914
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002915 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002916 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002917 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002918 return;
2919
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002920 /* We only use input grabs for pointer events for now, so just
2921 * ignore key presses if a grab is active. We expand the key
2922 * event delivery mechanism to route events to widgets to
2923 * properly handle key grabs. In the meantime, this prevents
2924 * key event devlivery while a grab is active. */
2925 if (input->grab && input->grab_button == 0)
2926 return;
2927
Ran Benita2e1968f2014-08-19 23:59:51 +03002928 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002929
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002930 sym = XKB_KEY_NoSymbol;
2931 if (num_syms == 1)
2932 sym = syms[0];
2933
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002934
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002935 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002936 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002937 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002938 } else if (sym == XKB_KEY_F11 &&
2939 window->fullscreen_handler &&
2940 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2941 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002942 } else if (sym == XKB_KEY_F4 &&
2943 input->modifiers == MOD_ALT_MASK &&
2944 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002945 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002946 } else if (window->key_handler) {
2947 (*window->key_handler)(window, input, time, key,
2948 sym, state, window->user_data);
2949 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002950
2951 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2952 key == input->repeat_key) {
2953 its.it_interval.tv_sec = 0;
2954 its.it_interval.tv_nsec = 0;
2955 its.it_value.tv_sec = 0;
2956 its.it_value.tv_nsec = 0;
2957 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002958 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2959 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002960 input->repeat_sym = sym;
2961 input->repeat_key = key;
2962 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002963 its.it_interval.tv_sec = input->repeat_rate_sec;
2964 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2965 its.it_value.tv_sec = input->repeat_delay_sec;
2966 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002967 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2968 }
2969}
2970
2971static void
Daniel Stone351eb612012-05-31 15:27:47 -04002972keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2973 uint32_t serial, uint32_t mods_depressed,
2974 uint32_t mods_latched, uint32_t mods_locked,
2975 uint32_t group)
2976{
2977 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002978 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002979
Matt Ropere61561f2013-06-24 16:52:43 +01002980 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2981 if (!input->xkb.keymap)
2982 return;
2983
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002984 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2985 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002986 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002987 XKB_STATE_MODS_DEPRESSED |
2988 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002989 input->modifiers = 0;
2990 if (mask & input->xkb.control_mask)
2991 input->modifiers |= MOD_CONTROL_MASK;
2992 if (mask & input->xkb.alt_mask)
2993 input->modifiers |= MOD_ALT_MASK;
2994 if (mask & input->xkb.shift_mask)
2995 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002996}
2997
Jonny Lamb06959082014-08-12 14:58:27 +02002998static void
2999set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3000{
3001 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3002 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3003
3004 /* a rate of zero disables any repeating, regardless of the delay's
3005 * value */
3006 if (rate == 0)
3007 return;
3008
3009 if (rate == 1)
3010 input->repeat_rate_sec = 1;
3011 else
3012 input->repeat_rate_nsec = 1000000000 / rate;
3013
3014 input->repeat_delay_sec = delay / 1000;
3015 delay -= (input->repeat_delay_sec * 1000);
3016 input->repeat_delay_nsec = delay * 1000 * 1000;
3017}
3018
3019static void
3020keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3021 int32_t rate, int32_t delay)
3022{
3023 struct input *input = data;
3024
3025 set_repeat_info(input, rate, delay);
3026}
3027
Daniel Stone37816df2012-05-16 18:45:18 +01003028static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003029 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003030 keyboard_handle_enter,
3031 keyboard_handle_leave,
3032 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003033 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003034 keyboard_handle_repeat_info
3035
Daniel Stone37816df2012-05-16 18:45:18 +01003036};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003037
3038static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003039touch_handle_down(void *data, struct wl_touch *wl_touch,
3040 uint32_t serial, uint32_t time, struct wl_surface *surface,
3041 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3042{
3043 struct input *input = data;
3044 struct widget *widget;
3045 float sx = wl_fixed_to_double(x_w);
3046 float sy = wl_fixed_to_double(y_w);
3047
Rusty Lynch1084da52013-08-15 09:10:08 -07003048 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003049 input->touch_focus = wl_surface_get_user_data(surface);
3050 if (!input->touch_focus) {
3051 DBG("Failed to find to touch focus for surface %p\n", surface);
3052 return;
3053 }
3054
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003055 if (surface != input->touch_focus->main_surface->surface) {
3056 DBG("Ignoring input event from subsurface %p\n", surface);
3057 input->touch_focus = NULL;
3058 return;
3059 }
3060
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003061 if (input->grab)
3062 widget = input->grab;
3063 else
3064 widget = window_find_widget(input->touch_focus,
3065 wl_fixed_to_double(x_w),
3066 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003067 if (widget) {
3068 struct touch_point *tp = xmalloc(sizeof *tp);
3069 if (tp) {
3070 tp->id = id;
3071 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003072 tp->x = sx;
3073 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003074 wl_list_insert(&input->touch_point_list, &tp->link);
3075
3076 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003077 (*widget->touch_down_handler)(widget, input,
3078 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003079 sx, sy,
3080 widget->user_data);
3081 }
3082 }
3083}
3084
3085static void
3086touch_handle_up(void *data, struct wl_touch *wl_touch,
3087 uint32_t serial, uint32_t time, int32_t id)
3088{
3089 struct input *input = data;
3090 struct touch_point *tp, *tmp;
3091
Rusty Lynch041815a2013-08-08 21:20:38 -07003092 if (!input->touch_focus) {
3093 DBG("No touch focus found for touch up event!\n");
3094 return;
3095 }
3096
3097 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3098 if (tp->id != id)
3099 continue;
3100
3101 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003102 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003103 time, id,
3104 tp->widget->user_data);
3105
3106 wl_list_remove(&tp->link);
3107 free(tp);
3108
3109 return;
3110 }
3111}
3112
3113static void
3114touch_handle_motion(void *data, struct wl_touch *wl_touch,
3115 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3116{
3117 struct input *input = data;
3118 struct touch_point *tp;
3119 float sx = wl_fixed_to_double(x_w);
3120 float sy = wl_fixed_to_double(y_w);
3121
3122 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3123
3124 if (!input->touch_focus) {
3125 DBG("No touch focus found for touch motion event!\n");
3126 return;
3127 }
3128
3129 wl_list_for_each(tp, &input->touch_point_list, link) {
3130 if (tp->id != id)
3131 continue;
3132
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003133 tp->x = sx;
3134 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003135 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003136 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003137 id, sx, sy,
3138 tp->widget->user_data);
3139 return;
3140 }
3141}
3142
3143static void
3144touch_handle_frame(void *data, struct wl_touch *wl_touch)
3145{
3146 struct input *input = data;
3147 struct touch_point *tp, *tmp;
3148
3149 DBG("touch_handle_frame\n");
3150
3151 if (!input->touch_focus) {
3152 DBG("No touch focus found for touch frame event!\n");
3153 return;
3154 }
3155
3156 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3157 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003158 (*tp->widget->touch_frame_handler)(tp->widget, input,
3159 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003160 }
3161}
3162
3163static void
3164touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3165{
3166 struct input *input = data;
3167 struct touch_point *tp, *tmp;
3168
3169 DBG("touch_handle_cancel\n");
3170
3171 if (!input->touch_focus) {
3172 DBG("No touch focus found for touch cancel event!\n");
3173 return;
3174 }
3175
3176 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3177 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003178 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3179 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003180
3181 wl_list_remove(&tp->link);
3182 free(tp);
3183 }
3184}
3185
3186static const struct wl_touch_listener touch_listener = {
3187 touch_handle_down,
3188 touch_handle_up,
3189 touch_handle_motion,
3190 touch_handle_frame,
3191 touch_handle_cancel,
3192};
3193
3194static void
Daniel Stone37816df2012-05-16 18:45:18 +01003195seat_handle_capabilities(void *data, struct wl_seat *seat,
3196 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003197{
Daniel Stone37816df2012-05-16 18:45:18 +01003198 struct input *input = data;
3199
3200 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3201 input->pointer = wl_seat_get_pointer(seat);
3202 wl_pointer_set_user_data(input->pointer, input);
3203 wl_pointer_add_listener(input->pointer, &pointer_listener,
3204 input);
3205 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3206 wl_pointer_destroy(input->pointer);
3207 input->pointer = NULL;
3208 }
3209
3210 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3211 input->keyboard = wl_seat_get_keyboard(seat);
3212 wl_keyboard_set_user_data(input->keyboard, input);
3213 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3214 input);
3215 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3216 wl_keyboard_destroy(input->keyboard);
3217 input->keyboard = NULL;
3218 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003219
3220 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3221 input->touch = wl_seat_get_touch(seat);
3222 wl_touch_set_user_data(input->touch, input);
3223 wl_touch_add_listener(input->touch, &touch_listener, input);
3224 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3225 wl_touch_destroy(input->touch);
3226 input->touch = NULL;
3227 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003228}
3229
Rob Bradford08031182013-08-13 20:11:03 +01003230static void
3231seat_handle_name(void *data, struct wl_seat *seat,
3232 const char *name)
3233{
3234
3235}
3236
Daniel Stone37816df2012-05-16 18:45:18 +01003237static const struct wl_seat_listener seat_listener = {
3238 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003239 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003240};
3241
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003242void
3243input_get_position(struct input *input, int32_t *x, int32_t *y)
3244{
3245 *x = input->sx;
3246 *y = input->sy;
3247}
3248
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003249int
3250input_get_touch(struct input *input, int32_t id, float *x, float *y)
3251{
3252 struct touch_point *tp;
3253
3254 wl_list_for_each(tp, &input->touch_point_list, link) {
3255 if (tp->id != id)
3256 continue;
3257
3258 *x = tp->x;
3259 *y = tp->y;
3260 return 0;
3261 }
3262
3263 return -1;
3264}
3265
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003266struct display *
3267input_get_display(struct input *input)
3268{
3269 return input->display;
3270}
3271
Daniel Stone37816df2012-05-16 18:45:18 +01003272struct wl_seat *
3273input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003274{
Daniel Stone37816df2012-05-16 18:45:18 +01003275 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003276}
3277
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003278uint32_t
3279input_get_modifiers(struct input *input)
3280{
3281 return input->modifiers;
3282}
3283
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003284struct widget *
3285input_get_focus_widget(struct input *input)
3286{
3287 return input->focus_widget;
3288}
3289
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003290struct data_offer {
3291 struct wl_data_offer *offer;
3292 struct input *input;
3293 struct wl_array types;
3294 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003295
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003296 struct task io_task;
3297 int fd;
3298 data_func_t func;
3299 int32_t x, y;
3300 void *user_data;
3301};
3302
3303static void
3304data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3305{
3306 struct data_offer *offer = data;
3307 char **p;
3308
3309 p = wl_array_add(&offer->types, sizeof *p);
3310 *p = strdup(type);
3311}
3312
3313static const struct wl_data_offer_listener data_offer_listener = {
3314 data_offer_offer,
3315};
3316
3317static void
3318data_offer_destroy(struct data_offer *offer)
3319{
3320 char **p;
3321
3322 offer->refcount--;
3323 if (offer->refcount == 0) {
3324 wl_data_offer_destroy(offer->offer);
3325 for (p = offer->types.data; *p; p++)
3326 free(*p);
3327 wl_array_release(&offer->types);
3328 free(offer);
3329 }
3330}
3331
3332static void
3333data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003334 struct wl_data_device *data_device,
3335 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003336{
3337 struct data_offer *offer;
3338
Brian Lovinbc919262013-08-07 15:34:59 -07003339 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003340
3341 wl_array_init(&offer->types);
3342 offer->refcount = 1;
3343 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003344 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003345 wl_data_offer_add_listener(offer->offer,
3346 &data_offer_listener, offer);
3347}
3348
3349static void
3350data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003351 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003352 wl_fixed_t x_w, wl_fixed_t y_w,
3353 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003354{
3355 struct input *input = data;
3356 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003357 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003358 float x = wl_fixed_to_double(x_w);
3359 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003360 char **p;
3361
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003362 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003363 input->drag_enter_serial = serial;
3364 input->drag_focus = window,
3365 input->drag_x = x;
3366 input->drag_y = y;
3367
3368 if (!input->touch_grab)
3369 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003370
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003371 if (offer) {
3372 input->drag_offer = wl_data_offer_get_user_data(offer);
3373
3374 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3375 *p = NULL;
3376
3377 types_data = input->drag_offer->types.data;
3378 } else {
3379 input->drag_offer = NULL;
3380 types_data = NULL;
3381 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003382
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003383 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003384 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003385 window->user_data);
3386}
3387
3388static void
3389data_device_leave(void *data, struct wl_data_device *data_device)
3390{
3391 struct input *input = data;
3392
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003393 if (input->drag_offer) {
3394 data_offer_destroy(input->drag_offer);
3395 input->drag_offer = NULL;
3396 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003397}
3398
3399static void
3400data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003401 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003402{
3403 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003404 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003405 float x = wl_fixed_to_double(x_w);
3406 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003407 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003408
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003409 input->drag_x = x;
3410 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003411
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003412 if (input->drag_offer)
3413 types_data = input->drag_offer->types.data;
3414 else
3415 types_data = NULL;
3416
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003417 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003418 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003419 window->user_data);
3420}
3421
3422static void
3423data_device_drop(void *data, struct wl_data_device *data_device)
3424{
3425 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003426 struct window *window = input->drag_focus;
3427 float x, y;
3428
3429 x = input->drag_x;
3430 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003431
3432 if (window->drop_handler)
3433 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003434 x, y, window->user_data);
3435
3436 if (input->touch_grab)
3437 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003438}
3439
3440static void
3441data_device_selection(void *data,
3442 struct wl_data_device *wl_data_device,
3443 struct wl_data_offer *offer)
3444{
3445 struct input *input = data;
3446 char **p;
3447
3448 if (input->selection_offer)
3449 data_offer_destroy(input->selection_offer);
3450
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003451 if (offer) {
3452 input->selection_offer = wl_data_offer_get_user_data(offer);
3453 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3454 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003455 } else {
3456 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003457 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003458}
3459
3460static const struct wl_data_device_listener data_device_listener = {
3461 data_device_data_offer,
3462 data_device_enter,
3463 data_device_leave,
3464 data_device_motion,
3465 data_device_drop,
3466 data_device_selection
3467};
3468
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003469static void
3470input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003471{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003472 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003473 struct wl_cursor *cursor;
3474 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003475
Daniel Stone80972742012-11-07 17:51:39 +11003476 if (!input->pointer)
3477 return;
3478
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003479 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003480 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003481 return;
3482
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003483 if (index >= (int) cursor->image_count) {
3484 fprintf(stderr, "cursor index out of range\n");
3485 return;
3486 }
3487
3488 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003489 buffer = wl_cursor_image_get_buffer(image);
3490 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003491 return;
3492
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003493 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3494 wl_surface_damage(input->pointer_surface, 0, 0,
3495 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003496 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003497 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3498 input->pointer_surface,
3499 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003500}
3501
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003502static const struct wl_callback_listener pointer_surface_listener;
3503
3504static void
3505pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3506 uint32_t time)
3507{
3508 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003509 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003510 int i;
3511
3512 if (callback) {
3513 assert(callback == input->cursor_frame_cb);
3514 wl_callback_destroy(callback);
3515 input->cursor_frame_cb = NULL;
3516 }
3517
Daniel Stone80972742012-11-07 17:51:39 +11003518 if (!input->pointer)
3519 return;
3520
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003521 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003522 wl_pointer_set_cursor(input->pointer,
3523 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003524 NULL, 0, 0);
3525 return;
3526 }
3527
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003528 if (input->current_cursor == CURSOR_UNSET)
3529 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003530 cursor = input->display->cursors[input->current_cursor];
3531 if (!cursor)
3532 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003533
3534 /* FIXME We don't have the current time on the first call so we set
3535 * the animation start to the time of the first frame callback. */
3536 if (time == 0)
3537 input->cursor_anim_start = 0;
3538 else if (input->cursor_anim_start == 0)
3539 input->cursor_anim_start = time;
3540
3541 if (time == 0 || input->cursor_anim_start == 0)
3542 i = 0;
3543 else
3544 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3545
Pekka Paalanenbc106382012-10-10 12:49:31 +03003546 if (cursor->image_count > 1) {
3547 input->cursor_frame_cb =
3548 wl_surface_frame(input->pointer_surface);
3549 wl_callback_add_listener(input->cursor_frame_cb,
3550 &pointer_surface_listener, input);
3551 }
3552
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003553 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003554}
3555
3556static const struct wl_callback_listener pointer_surface_listener = {
3557 pointer_surface_frame_callback
3558};
3559
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003560void
3561input_set_pointer_image(struct input *input, int pointer)
3562{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003563 int force = 0;
3564
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003565 if (!input->pointer)
3566 return;
3567
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003568 if (input->pointer_enter_serial > input->cursor_serial)
3569 force = 1;
3570
3571 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003572 return;
3573
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003574 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003575 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003576 if (!input->cursor_frame_cb)
3577 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003578 else if (force) {
3579 /* The current frame callback may be stuck if, for instance,
3580 * the set cursor request was processed by the server after
3581 * this client lost the focus. In this case the cursor surface
3582 * might not be mapped and the frame callback wouldn't ever
3583 * complete. Send a set_cursor and attach to try to map the
3584 * cursor surface again so that the callback will finish */
3585 input_set_pointer_image_index(input, 0);
3586 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003587}
3588
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003589struct wl_data_device *
3590input_get_data_device(struct input *input)
3591{
3592 return input->data_device;
3593}
3594
3595void
3596input_set_selection(struct input *input,
3597 struct wl_data_source *source, uint32_t time)
3598{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003599 if (input->data_device)
3600 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003601}
3602
3603void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003604input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003605{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003606 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003607 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003608}
3609
3610static void
3611offer_io_func(struct task *task, uint32_t events)
3612{
3613 struct data_offer *offer =
3614 container_of(task, struct data_offer, io_task);
3615 unsigned int len;
3616 char buffer[4096];
3617
3618 len = read(offer->fd, buffer, sizeof buffer);
3619 offer->func(buffer, len,
3620 offer->x, offer->y, offer->user_data);
3621
3622 if (len == 0) {
3623 close(offer->fd);
3624 data_offer_destroy(offer);
3625 }
3626}
3627
3628static void
3629data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3630 data_func_t func, void *user_data)
3631{
3632 int p[2];
3633
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003634 if (pipe2(p, O_CLOEXEC) == -1)
3635 return;
3636
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003637 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3638 close(p[1]);
3639
3640 offer->io_task.run = offer_io_func;
3641 offer->fd = p[0];
3642 offer->func = func;
3643 offer->refcount++;
3644 offer->user_data = user_data;
3645
3646 display_watch_fd(offer->input->display,
3647 offer->fd, EPOLLIN, &offer->io_task);
3648}
3649
3650void
3651input_receive_drag_data(struct input *input, const char *mime_type,
3652 data_func_t func, void *data)
3653{
3654 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003655 input->drag_offer->x = input->drag_x;
3656 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003657}
3658
3659int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003660input_receive_drag_data_to_fd(struct input *input,
3661 const char *mime_type, int fd)
3662{
3663 if (input->drag_offer)
3664 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3665
3666 return 0;
3667}
3668
3669int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003670input_receive_selection_data(struct input *input, const char *mime_type,
3671 data_func_t func, void *data)
3672{
3673 char **p;
3674
3675 if (input->selection_offer == NULL)
3676 return -1;
3677
3678 for (p = input->selection_offer->types.data; *p; p++)
3679 if (strcmp(mime_type, *p) == 0)
3680 break;
3681
3682 if (*p == NULL)
3683 return -1;
3684
3685 data_offer_receive_data(input->selection_offer,
3686 mime_type, func, data);
3687 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003688}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003689
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003690int
3691input_receive_selection_data_to_fd(struct input *input,
3692 const char *mime_type, int fd)
3693{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003694 if (input->selection_offer)
3695 wl_data_offer_receive(input->selection_offer->offer,
3696 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003697
3698 return 0;
3699}
3700
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003701void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003702window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003703{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003704 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003705 return;
3706
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003707 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003708}
3709
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003710static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003711surface_set_synchronized(struct surface *surface)
3712{
3713 if (!surface->subsurface)
3714 return;
3715
3716 if (surface->synchronized)
3717 return;
3718
3719 wl_subsurface_set_sync(surface->subsurface);
3720 surface->synchronized = 1;
3721}
3722
3723static void
3724surface_set_synchronized_default(struct surface *surface)
3725{
3726 if (!surface->subsurface)
3727 return;
3728
3729 if (surface->synchronized == surface->synchronized_default)
3730 return;
3731
3732 if (surface->synchronized_default)
3733 wl_subsurface_set_sync(surface->subsurface);
3734 else
3735 wl_subsurface_set_desync(surface->subsurface);
3736
3737 surface->synchronized = surface->synchronized_default;
3738}
3739
3740static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003741surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003742{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003743 struct widget *widget = surface->widget;
3744 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003745
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003746 if (surface->input_region) {
3747 wl_region_destroy(surface->input_region);
3748 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003749 }
3750
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003751 if (surface->opaque_region)
3752 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003753
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003754 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003755
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003756 if (widget->resize_handler)
3757 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003758 widget->allocation.width,
3759 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003760 widget->user_data);
3761
Pekka Paalanen35e82632013-04-25 13:57:48 +03003762 if (surface->subsurface &&
3763 (surface->allocation.x != widget->allocation.x ||
3764 surface->allocation.y != widget->allocation.y)) {
3765 wl_subsurface_set_position(surface->subsurface,
3766 widget->allocation.x,
3767 widget->allocation.y);
3768 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003769 if (surface->allocation.width != widget->allocation.width ||
3770 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003771 window_schedule_redraw(widget->window);
3772 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003773 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003774
3775 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003776 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003777 widget->allocation.width,
3778 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003779}
3780
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003781static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003782hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3783{
3784 /*
3785 * This hack should be removed, when EGL respects
3786 * eglSwapInterval(0).
3787 *
3788 * If this window has sub-surfaces, especially a free-running
3789 * EGL-widget, we need to post the parent surface once with
3790 * all the old state to guarantee, that the EGL-widget will
3791 * receive its frame callback soon. Otherwise, a forced call
3792 * to eglSwapBuffers may end up blocking, waiting for a frame
3793 * event that will never come, because we will commit the parent
3794 * surface with all new state only after eglSwapBuffers returns.
3795 *
3796 * This assumes, that:
3797 * 1. When the EGL widget's resize hook is called, it pauses.
3798 * 2. When the EGL widget's redraw hook is called, it forces a
3799 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3800 * In a single threaded application condition 1 is a no-op.
3801 *
3802 * XXX: This should actually be after the surface_resize() calls,
3803 * but cannot, because then it would commit the incomplete state
3804 * accumulated from the widget resize hooks.
3805 */
3806 if (window->subsurface_list.next != &window->main_surface->link ||
3807 window->subsurface_list.prev != &window->main_surface->link)
3808 wl_surface_commit(window->main_surface->surface);
3809}
3810
3811static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003812window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003813{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003814 struct surface *surface;
3815
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003816 widget_set_allocation(window->main_surface->widget,
3817 window->pending_allocation.x,
3818 window->pending_allocation.y,
3819 window->pending_allocation.width,
3820 window->pending_allocation.height);
3821
3822 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003823
3824 /* The main surface is in the list, too. Main surface's
3825 * resize_handler is responsible for calling widget_set_allocation()
3826 * on all sub-surface root widgets, so they will be resized
3827 * properly.
3828 */
3829 wl_list_for_each(surface, &window->subsurface_list, link) {
3830 if (surface == window->main_surface)
3831 continue;
3832
3833 surface_set_synchronized(surface);
3834 surface_resize(surface);
3835 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003836
3837 if (!window->fullscreen && !window->maximized)
3838 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003839}
3840
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003841static void
3842idle_resize(struct window *window)
3843{
3844 window->resize_needed = 0;
3845 window->redraw_needed = 1;
3846
3847 DBG("from %dx%d to %dx%d\n",
3848 window->main_surface->server_allocation.width,
3849 window->main_surface->server_allocation.height,
3850 window->pending_allocation.width,
3851 window->pending_allocation.height);
3852
3853 hack_prevent_EGL_sub_surface_deadlock(window);
3854
3855 window_do_resize(window);
3856}
3857
3858static void
3859undo_resize(struct window *window)
3860{
3861 window->pending_allocation.width =
3862 window->main_surface->server_allocation.width;
3863 window->pending_allocation.height =
3864 window->main_surface->server_allocation.height;
3865
3866 DBG("back to %dx%d\n",
3867 window->main_surface->server_allocation.width,
3868 window->main_surface->server_allocation.height);
3869
3870 window_do_resize(window);
3871
3872 if (window->pending_allocation.width == 0 &&
3873 window->pending_allocation.height == 0) {
3874 fprintf(stderr, "Error: Could not draw a surface, "
3875 "most likely due to insufficient disk space in "
3876 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3877 exit(EXIT_FAILURE);
3878 }
3879}
3880
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003881void
3882window_schedule_resize(struct window *window, int width, int height)
3883{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003884 /* We should probably get these numbers from the theme. */
3885 const int min_width = 200, min_height = 200;
3886
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003887 window->pending_allocation.x = 0;
3888 window->pending_allocation.y = 0;
3889 window->pending_allocation.width = width;
3890 window->pending_allocation.height = height;
3891
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003892 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003893 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003894 window->min_allocation.width = min_width;
3895 else
3896 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003897 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003898 window->min_allocation.height = min_height;
3899 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003900 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003901 }
3902
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003903 if (window->pending_allocation.width < window->min_allocation.width)
3904 window->pending_allocation.width = window->min_allocation.width;
3905 if (window->pending_allocation.height < window->min_allocation.height)
3906 window->pending_allocation.height = window->min_allocation.height;
3907
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003908 window->resize_needed = 1;
3909 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003910}
3911
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003912void
3913widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3914{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003915 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003916}
3917
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003918static int
3919window_get_shadow_margin(struct window *window)
3920{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003921 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003922 return frame_get_shadow_margin(window->frame->frame);
3923 else
3924 return 0;
3925}
3926
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003927static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003928handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003929 int32_t width, int32_t height,
3930 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003931{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003932 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003933 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003934
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003935 window->maximized = 0;
3936 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003937 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003938 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003939
3940 wl_array_for_each(p, states) {
3941 uint32_t state = *p;
3942 switch (state) {
3943 case XDG_SURFACE_STATE_MAXIMIZED:
3944 window->maximized = 1;
3945 break;
3946 case XDG_SURFACE_STATE_FULLSCREEN:
3947 window->fullscreen = 1;
3948 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003949 case XDG_SURFACE_STATE_RESIZING:
3950 window->resizing = 1;
3951 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003952 case XDG_SURFACE_STATE_ACTIVATED:
3953 window->focused = 1;
3954 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003955 default:
3956 /* Unknown state */
3957 break;
3958 }
3959 }
3960
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003961 if (window->frame) {
3962 if (window->maximized) {
3963 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3964 } else {
3965 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3966 }
3967
3968 if (window->focused) {
3969 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3970 } else {
3971 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3972 }
3973 }
3974
Jasper St. Pierref184c382014-05-06 08:33:27 -04003975 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003976 /* The width / height params are for window geometry,
3977 * but window_schedule_resize takes allocation. Add
3978 * on the shadow margin to get the difference. */
3979 int margin = window_get_shadow_margin(window);
3980
3981 window_schedule_resize(window,
3982 width + margin * 2,
3983 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04003984 } else {
3985 window_schedule_resize(window,
3986 window->saved_allocation.width,
3987 window->saved_allocation.height);
3988 }
3989
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07003990 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07003991
3992 if (window->state_changed_handler)
3993 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003994}
3995
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003996static void
3997handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3998{
3999 struct window *window = data;
4000 window_close(window);
4001}
4002
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004003static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004004 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004005 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004006};
4007
4008static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004009window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004010{
4011 struct wl_surface *parent_surface;
4012
4013 if (!window->xdg_surface)
4014 return;
4015
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004016 if (window->parent)
4017 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004018 else
4019 parent_surface = NULL;
4020
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004021 if (parent_surface == window->last_parent_surface)
4022 return;
4023
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004024 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004025 window->last_parent_surface = parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004026}
4027
4028static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004029window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004030{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004031 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004032 frame_input_rect(window->frame->frame,
4033 &geometry->x,
4034 &geometry->y,
4035 &geometry->width,
4036 &geometry->height);
4037 else
4038 window_get_allocation(window, geometry);
4039}
4040
4041static void
4042window_sync_geometry(struct window *window)
4043{
4044 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004045
4046 if (!window->xdg_surface)
4047 return;
4048
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004049 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004050 if (geometry.x == window->last_geometry.x &&
4051 geometry.y == window->last_geometry.y &&
4052 geometry.width == window->last_geometry.width &&
4053 geometry.height == window->last_geometry.height)
4054 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004055
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004056 xdg_surface_set_window_geometry(window->xdg_surface,
4057 geometry.x,
4058 geometry.y,
4059 geometry.width,
4060 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004061 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004062}
4063
4064static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004065window_flush(struct window *window)
4066{
4067 struct surface *surface;
4068
4069 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004070 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004071 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004072 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004073 }
4074 }
4075
4076 wl_list_for_each(surface, &window->subsurface_list, link) {
4077 if (surface == window->main_surface)
4078 continue;
4079
4080 surface_flush(surface);
4081 }
4082
4083 surface_flush(window->main_surface);
4084}
4085
4086static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004087menu_destroy(struct menu *menu)
4088{
4089 widget_destroy(menu->widget);
4090 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004091 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004092 free(menu);
4093}
4094
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004095void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004096window_get_allocation(struct window *window,
4097 struct rectangle *allocation)
4098{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004099 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004100}
4101
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004102static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004103widget_redraw(struct widget *widget)
4104{
4105 struct widget *child;
4106
4107 if (widget->redraw_handler)
4108 widget->redraw_handler(widget, widget->user_data);
4109 wl_list_for_each(child, &widget->child_list, link)
4110 widget_redraw(child);
4111}
4112
4113static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004114frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4115{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004116 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004117
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004118 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004119 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004120 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004121 surface->frame_cb = NULL;
4122
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004123 surface->last_time = time;
4124
Pekka Paalanen71233882013-04-25 13:57:53 +03004125 if (surface->redraw_needed || surface->window->redraw_needed) {
4126 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004127 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004128 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004129}
4130
4131static const struct wl_callback_listener listener = {
4132 frame_callback
4133};
4134
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004135static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004136surface_redraw(struct surface *surface)
4137{
Pekka Paalanen71233882013-04-25 13:57:53 +03004138 DBG_OBJ(surface->surface, "begin\n");
4139
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004140 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004141 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004142
4143 /* Whole-window redraw forces a redraw even if the previous has
4144 * not yet hit the screen.
4145 */
4146 if (surface->frame_cb) {
4147 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004148 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004149
Pekka Paalanen71233882013-04-25 13:57:53 +03004150 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004151 wl_callback_destroy(surface->frame_cb);
4152 }
4153
Neil Roberts97b747c2013-12-19 16:17:12 +00004154 if (surface->widget->use_cairo &&
4155 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004156 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4157 return -1;
4158 }
4159
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004160 surface->frame_cb = wl_surface_frame(surface->surface);
4161 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004162 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004163
4164 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004165 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004166 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004167 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004168 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004169}
4170
4171static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004172idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004173{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004174 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004175 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004176 int failed = 0;
4177 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004178
Pekka Paalanen71233882013-04-25 13:57:53 +03004179 DBG(" --------- \n");
4180
Pekka Paalaneneebff542013-04-25 13:57:52 +03004181 wl_list_init(&window->redraw_task.link);
4182 window->redraw_task_scheduled = 0;
4183
4184 if (window->resize_needed) {
4185 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004186 if (window->main_surface->frame_cb) {
4187 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004188 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004189 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004190
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004191 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004192 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004193 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004194
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004195 if (surface_redraw(window->main_surface) < 0) {
4196 /*
4197 * Only main_surface failure will cause us to undo the resize.
4198 * If sub-surfaces fail, they will just be broken with old
4199 * content.
4200 */
4201 failed = 1;
4202 } else {
4203 wl_list_for_each(surface, &window->subsurface_list, link) {
4204 if (surface == window->main_surface)
4205 continue;
4206
4207 surface_redraw(surface);
4208 }
4209 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004210
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004211 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004212 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004213
4214 wl_list_for_each(surface, &window->subsurface_list, link)
4215 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004216
4217 if (resized && failed) {
4218 /* Restore widget tree to correspond to what is on screen. */
4219 undo_resize(window);
4220 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004221}
4222
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004223static void
4224window_schedule_redraw_task(struct window *window)
4225{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004226 if (!window->redraw_task_scheduled) {
4227 window->redraw_task.run = idle_redraw;
4228 display_defer(window->display, &window->redraw_task);
4229 window->redraw_task_scheduled = 1;
4230 }
4231}
4232
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004233void
4234window_schedule_redraw(struct window *window)
4235{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004236 struct surface *surface;
4237
Pekka Paalanen71233882013-04-25 13:57:53 +03004238 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4239
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004240 wl_list_for_each(surface, &window->subsurface_list, link)
4241 surface->redraw_needed = 1;
4242
4243 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004244}
4245
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004246int
4247window_is_fullscreen(struct window *window)
4248{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004249 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004250}
4251
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004252void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004253window_set_fullscreen(struct window *window, int fullscreen)
4254{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004255 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004256 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004257
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004258 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004259 return;
4260
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004261 if (fullscreen)
4262 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4263 else
4264 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004265}
4266
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004267int
4268window_is_maximized(struct window *window)
4269{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004270 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004271}
4272
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004273void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004274window_set_maximized(struct window *window, int maximized)
4275{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004276 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004277 return;
4278
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004279 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004280 return;
4281
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004282 if (maximized)
4283 xdg_surface_set_maximized(window->xdg_surface);
4284 else
4285 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004286}
4287
Jasper St. Pierrede680992014-04-10 17:23:49 -07004288int
4289window_is_resizing(struct window *window)
4290{
4291 return window->resizing;
4292}
4293
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004294void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004295window_set_minimized(struct window *window)
4296{
4297 if (!window->xdg_surface)
4298 return;
4299
4300 xdg_surface_set_minimized(window->xdg_surface);
4301}
4302
4303void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004304window_set_user_data(struct window *window, void *data)
4305{
4306 window->user_data = data;
4307}
4308
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004309void *
4310window_get_user_data(struct window *window)
4311{
4312 return window->user_data;
4313}
4314
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004315void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004316window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004317 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004318{
4319 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004320}
4321
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004322void
4323window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004324 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004325{
4326 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004327}
4328
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004329void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004330window_set_data_handler(struct window *window, window_data_handler_t handler)
4331{
4332 window->data_handler = handler;
4333}
4334
4335void
4336window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4337{
4338 window->drop_handler = handler;
4339}
4340
4341void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004342window_set_close_handler(struct window *window,
4343 window_close_handler_t handler)
4344{
4345 window->close_handler = handler;
4346}
4347
4348void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004349window_set_fullscreen_handler(struct window *window,
4350 window_fullscreen_handler_t handler)
4351{
4352 window->fullscreen_handler = handler;
4353}
4354
4355void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004356window_set_output_handler(struct window *window,
4357 window_output_handler_t handler)
4358{
4359 window->output_handler = handler;
4360}
4361
4362void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004363window_set_state_changed_handler(struct window *window,
4364 window_state_changed_handler_t handler)
4365{
4366 window->state_changed_handler = handler;
4367}
4368
4369void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004370window_set_title(struct window *window, const char *title)
4371{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004372 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004373 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004374 if (window->frame) {
4375 frame_set_title(window->frame->frame, title);
4376 widget_schedule_redraw(window->frame->widget);
4377 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004378 if (window->xdg_surface)
4379 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004380}
4381
4382const char *
4383window_get_title(struct window *window)
4384{
4385 return window->title;
4386}
4387
4388void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004389window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4390{
4391 struct text_cursor_position *text_cursor_position =
4392 window->display->text_cursor_position;
4393
Scott Moreau9295ce02012-06-01 12:46:10 -06004394 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004395 return;
4396
4397 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004398 window->main_surface->surface,
4399 wl_fixed_from_int(x),
4400 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004401}
4402
4403void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004404window_damage(struct window *window, int32_t x, int32_t y,
4405 int32_t width, int32_t height)
4406{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004407 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004408}
4409
Casey Dahlin9074db52012-04-19 22:50:09 -04004410static void
4411surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004412 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004413{
Rob Bradford7507b572012-05-15 17:55:34 +01004414 struct window *window = data;
4415 struct output *output;
4416 struct output *output_found = NULL;
4417 struct window_output *window_output;
4418
4419 wl_list_for_each(output, &window->display->output_list, link) {
4420 if (output->output == wl_output) {
4421 output_found = output;
4422 break;
4423 }
4424 }
4425
4426 if (!output_found)
4427 return;
4428
Brian Lovinbc919262013-08-07 15:34:59 -07004429 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004430 window_output->output = output_found;
4431
4432 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004433
4434 if (window->output_handler)
4435 window->output_handler(window, output_found, 1,
4436 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004437}
4438
4439static void
4440surface_leave(void *data,
4441 struct wl_surface *wl_surface, struct wl_output *output)
4442{
Rob Bradford7507b572012-05-15 17:55:34 +01004443 struct window *window = data;
4444 struct window_output *window_output;
4445 struct window_output *window_output_found = NULL;
4446
4447 wl_list_for_each(window_output, &window->window_output_list, link) {
4448 if (window_output->output->output == output) {
4449 window_output_found = window_output;
4450 break;
4451 }
4452 }
4453
4454 if (window_output_found) {
4455 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004456
4457 if (window->output_handler)
4458 window->output_handler(window, window_output->output,
4459 0, window->user_data);
4460
Rob Bradford7507b572012-05-15 17:55:34 +01004461 free(window_output_found);
4462 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004463}
4464
4465static const struct wl_surface_listener surface_listener = {
4466 surface_enter,
4467 surface_leave
4468};
4469
Pekka Paalanen4e373742013-02-13 16:17:13 +02004470static struct surface *
4471surface_create(struct window *window)
4472{
4473 struct display *display = window->display;
4474 struct surface *surface;
4475
Brian Lovinbc919262013-08-07 15:34:59 -07004476 surface = xmalloc(sizeof *surface);
4477 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004478 if (!surface)
4479 return NULL;
4480
4481 surface->window = window;
4482 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004483 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004484 wl_surface_add_listener(surface->surface, &surface_listener, window);
4485
Pekka Paalanen35e82632013-04-25 13:57:48 +03004486 wl_list_insert(&window->subsurface_list, &surface->link);
4487
Pekka Paalanen4e373742013-02-13 16:17:13 +02004488 return surface;
4489}
4490
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004491static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004492get_preferred_buffer_type(struct display *display)
4493{
4494#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004495 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004496 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4497#endif
4498
4499 return WINDOW_BUFFER_TYPE_SHM;
4500}
4501
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004502static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004503window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004504{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004505 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004506 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004507
Peter Huttererf3d62272013-08-08 11:57:05 +10004508 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004509 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004510 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004511
4512 surface = surface_create(window);
4513 window->main_surface = surface;
4514
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004515 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004516
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004517 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004518 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004519
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004520 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004521
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004522 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004523 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004524 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004525
Rob Bradford7507b572012-05-15 17:55:34 +01004526 wl_list_init (&window->window_output_list);
4527
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004528 return window;
4529}
4530
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004531struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004532window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004533{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004534 struct window *window;
4535
4536 window = window_create_internal(display, 0);
4537
4538 window->xdg_surface =
4539 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4540 window->main_surface->surface);
4541 fail_on_null(window->xdg_surface);
4542
4543 xdg_surface_set_user_data(window->xdg_surface, window);
4544 xdg_surface_add_listener(window->xdg_surface,
4545 &xdg_surface_listener, window);
4546
4547 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004548}
4549
4550struct window *
4551window_create_custom(struct display *display)
4552{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004553 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004554}
4555
Jasper St. Pierre53686042013-12-09 15:26:25 -05004556void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004557window_set_parent(struct window *window,
4558 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004559{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004560 window->parent = parent_window;
4561 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004562}
4563
4564struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004565window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004566{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004567 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004568}
4569
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004570static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004571menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004572{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004573 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004574 int next;
4575
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004576 frame_interior(menu->frame, &x, &y, &width, &height);
4577 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004578 if (menu->current != next) {
4579 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004580 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004581 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004582}
4583
4584static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004585menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004586 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004587 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004588{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004589 struct menu *menu = data;
4590
4591 if (widget == menu->widget)
4592 menu_set_item(data, y);
4593
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004594 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004595}
4596
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004597static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004598menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004599 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004600{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004601 struct menu *menu = data;
4602
4603 if (widget == menu->widget)
4604 menu_set_item(data, y);
4605
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004606 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004607}
4608
4609static void
4610menu_leave_handler(struct widget *widget, struct input *input, void *data)
4611{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004612 struct menu *menu = data;
4613
4614 if (widget == menu->widget)
4615 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004616}
4617
4618static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004619menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004620 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004621 uint32_t button, enum wl_pointer_button_state state,
4622 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004623
4624{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004625 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004626
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004627 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4628 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004629 /* Either relase after press-drag-release or
4630 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004631 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004632 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004633 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004634 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004635 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004636 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004637}
4638
4639static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004640menu_touch_up_handler(struct widget *widget,
4641 struct input *input,
4642 uint32_t serial,
4643 uint32_t time,
4644 int32_t id,
4645 void *data)
4646{
4647 struct menu *menu = data;
4648
4649 input_ungrab(input);
4650 menu_destroy(menu);
4651}
4652
4653static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004654menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004655{
4656 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004657 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004658 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004659
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004660 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004661
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004662 frame_repaint(menu->frame, cr);
4663 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004664
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004665 theme_set_background_source(menu->window->display->theme,
4666 cr, THEME_FRAME_ACTIVE);
4667 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004668 cairo_fill(cr);
4669
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004670 cairo_select_font_face(cr, "sans",
4671 CAIRO_FONT_SLANT_NORMAL,
4672 CAIRO_FONT_WEIGHT_NORMAL);
4673 cairo_set_font_size(cr, 12);
4674
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004675 for (i = 0; i < menu->count; i++) {
4676 if (i == menu->current) {
4677 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004678 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004679 cairo_fill(cr);
4680 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004681 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004682 cairo_show_text(cr, menu->entries[i]);
4683 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004684 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4685 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004686 cairo_show_text(cr, menu->entries[i]);
4687 }
4688 }
4689
4690 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004691}
4692
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004693static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004694handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4695{
4696 struct window *window = data;
4697 struct menu *menu = window->main_surface->widget->user_data;
4698
4699 input_ungrab(menu->input);
4700 menu_destroy(menu);
4701}
4702
4703static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004704 handle_popup_popup_done,
4705};
4706
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004707static struct menu *
4708create_menu(struct display *display,
4709 struct input *input, uint32_t time,
4710 menu_func_t func, const char **entries, int count,
4711 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004712{
4713 struct window *window;
4714 struct menu *menu;
4715
4716 menu = malloc(sizeof *menu);
4717 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004718 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004719
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004720 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004721 if (!window) {
4722 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004723 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004724 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004725
4726 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004727 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004728 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004729 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004730 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004731 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004732 menu->entries = entries;
4733 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004734 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004735 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004736 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004737 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004738 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004739
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004740 input_ungrab(input);
4741
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004742 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004743 widget_set_enter_handler(menu->widget, menu_enter_handler);
4744 widget_set_leave_handler(menu->widget, menu_leave_handler);
4745 widget_set_motion_handler(menu->widget, menu_motion_handler);
4746 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004747 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004748
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004749 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004750 frame_resize_inside(menu->frame, 200, count * 20);
4751 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4752 window_schedule_resize(window, frame_width(menu->frame),
4753 frame_height(menu->frame));
4754
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004755 return menu;
4756}
4757
4758struct window *
4759window_create_menu(struct display *display,
4760 struct input *input, uint32_t time,
4761 menu_func_t func, const char **entries, int count,
4762 void *user_data)
4763{
4764 struct menu *menu;
4765 menu = create_menu(display, input, time, func, entries, count, user_data);
4766
4767 if (menu == NULL)
4768 return NULL;
4769
4770 return menu->window;
4771}
4772
4773void
4774window_show_menu(struct display *display,
4775 struct input *input, uint32_t time, struct window *parent,
4776 int32_t x, int32_t y,
4777 menu_func_t func, const char **entries, int count)
4778{
4779 struct menu *menu;
4780 struct window *window;
4781 int32_t ix, iy;
4782
4783 menu = create_menu(display, input, time, func, entries, count, parent);
4784
4785 if (menu == NULL)
4786 return;
4787
4788 window = menu->window;
4789
4790 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4791 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4792
4793 window->x = x;
4794 window->y = y;
4795
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004796 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004797
4798 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4799 window->main_surface->surface,
4800 parent->main_surface->surface,
4801 input->seat,
4802 display_get_serial(window->display),
4803 window->x - ix,
4804 window->y - iy,
4805 0);
4806 fail_on_null(window->xdg_popup);
4807
4808 xdg_popup_set_user_data(window->xdg_popup, window);
4809 xdg_popup_add_listener(window->xdg_popup,
4810 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004811}
4812
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004813void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004814window_set_buffer_type(struct window *window, enum window_buffer_type type)
4815{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004816 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004817}
4818
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004819enum window_buffer_type
4820window_get_buffer_type(struct window *window)
4821{
4822 return window->main_surface->buffer_type;
4823}
4824
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004825void
4826window_set_preferred_format(struct window *window,
4827 enum preferred_format format)
4828{
4829 window->preferred_format = format;
4830}
4831
Pekka Paalanen35e82632013-04-25 13:57:48 +03004832struct widget *
4833window_add_subsurface(struct window *window, void *data,
4834 enum subsurface_mode default_mode)
4835{
4836 struct widget *widget;
4837 struct surface *surface;
4838 struct wl_surface *parent;
4839 struct wl_subcompositor *subcompo = window->display->subcompositor;
4840
Pekka Paalanen35e82632013-04-25 13:57:48 +03004841 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004842 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004843 widget = widget_create(window, surface, data);
4844 wl_list_init(&widget->link);
4845 surface->widget = widget;
4846
4847 parent = window->main_surface->surface;
4848 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4849 surface->surface,
4850 parent);
4851 surface->synchronized = 1;
4852
4853 switch (default_mode) {
4854 case SUBSURFACE_SYNCHRONIZED:
4855 surface->synchronized_default = 1;
4856 break;
4857 case SUBSURFACE_DESYNCHRONIZED:
4858 surface->synchronized_default = 0;
4859 break;
4860 default:
4861 assert(!"bad enum subsurface_mode");
4862 }
4863
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004864 window->resize_needed = 1;
4865 window_schedule_redraw(window);
4866
Pekka Paalanen35e82632013-04-25 13:57:48 +03004867 return widget;
4868}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004869
4870static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004871display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004872 struct wl_output *wl_output,
4873 int x, int y,
4874 int physical_width,
4875 int physical_height,
4876 int subpixel,
4877 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004878 const char *model,
4879 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004880{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004881 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004882
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004883 output->allocation.x = x;
4884 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004885 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004886
4887 if (output->make)
4888 free(output->make);
4889 output->make = strdup(make);
4890
4891 if (output->model)
4892 free(output->model);
4893 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004894}
4895
4896static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004897display_handle_done(void *data,
4898 struct wl_output *wl_output)
4899{
4900}
4901
4902static void
4903display_handle_scale(void *data,
4904 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004905 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004906{
4907 struct output *output = data;
4908
4909 output->scale = scale;
4910}
4911
4912static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004913display_handle_mode(void *data,
4914 struct wl_output *wl_output,
4915 uint32_t flags,
4916 int width,
4917 int height,
4918 int refresh)
4919{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004920 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004921 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004922
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004923 if (flags & WL_OUTPUT_MODE_CURRENT) {
4924 output->allocation.width = width;
4925 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004926 if (display->output_configure_handler)
4927 (*display->output_configure_handler)(
4928 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004929 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004930}
4931
4932static const struct wl_output_listener output_listener = {
4933 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004934 display_handle_mode,
4935 display_handle_done,
4936 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004937};
4938
4939static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004940display_add_output(struct display *d, uint32_t id)
4941{
4942 struct output *output;
4943
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004944 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004945 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004946 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004947 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004948 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004949 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004950 wl_list_insert(d->output_list.prev, &output->link);
4951
4952 wl_output_add_listener(output->output, &output_listener, output);
4953}
4954
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004955static void
4956output_destroy(struct output *output)
4957{
4958 if (output->destroy_handler)
4959 (*output->destroy_handler)(output, output->user_data);
4960
4961 wl_output_destroy(output->output);
4962 wl_list_remove(&output->link);
4963 free(output);
4964}
4965
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004966static void
4967display_destroy_output(struct display *d, uint32_t id)
4968{
4969 struct output *output;
4970
4971 wl_list_for_each(output, &d->output_list, link) {
4972 if (output->server_output_id == id) {
4973 output_destroy(output);
4974 break;
4975 }
4976 }
4977}
4978
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004979void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004980display_set_global_handler(struct display *display,
4981 display_global_handler_t handler)
4982{
4983 struct global *global;
4984
4985 display->global_handler = handler;
4986 if (!handler)
4987 return;
4988
4989 wl_list_for_each(global, &display->global_list, link)
4990 display->global_handler(display,
4991 global->name, global->interface,
4992 global->version, display->user_data);
4993}
4994
4995void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004996display_set_global_handler_remove(struct display *display,
4997 display_global_handler_t remove_handler)
4998{
4999 display->global_handler_remove = remove_handler;
5000 if (!remove_handler)
5001 return;
5002}
5003
5004void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005005display_set_output_configure_handler(struct display *display,
5006 display_output_handler_t handler)
5007{
5008 struct output *output;
5009
5010 display->output_configure_handler = handler;
5011 if (!handler)
5012 return;
5013
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005014 wl_list_for_each(output, &display->output_list, link) {
5015 if (output->allocation.width == 0 &&
5016 output->allocation.height == 0)
5017 continue;
5018
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005019 (*display->output_configure_handler)(output,
5020 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005021 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005022}
5023
5024void
5025output_set_user_data(struct output *output, void *data)
5026{
5027 output->user_data = data;
5028}
5029
5030void *
5031output_get_user_data(struct output *output)
5032{
5033 return output->user_data;
5034}
5035
5036void
5037output_set_destroy_handler(struct output *output,
5038 display_output_handler_t handler)
5039{
5040 output->destroy_handler = handler;
5041 /* FIXME: implement this, once we have way to remove outputs */
5042}
5043
5044void
Scott Moreau4e072362012-09-29 02:03:11 -06005045output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005046{
Scott Moreau4e072362012-09-29 02:03:11 -06005047 struct rectangle allocation = output->allocation;
5048
5049 switch (output->transform) {
5050 case WL_OUTPUT_TRANSFORM_90:
5051 case WL_OUTPUT_TRANSFORM_270:
5052 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5053 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5054 /* Swap width and height */
5055 allocation.width = output->allocation.height;
5056 allocation.height = output->allocation.width;
5057 break;
5058 }
5059
5060 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005061}
5062
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005063struct wl_output *
5064output_get_wl_output(struct output *output)
5065{
5066 return output->output;
5067}
5068
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005069enum wl_output_transform
5070output_get_transform(struct output *output)
5071{
5072 return output->transform;
5073}
5074
Alexander Larssonafd319a2013-05-22 14:41:27 +02005075uint32_t
5076output_get_scale(struct output *output)
5077{
5078 return output->scale;
5079}
5080
Jason Ekstrand738715d2014-04-02 19:53:50 -05005081const char *
5082output_get_make(struct output *output)
5083{
5084 return output->make;
5085}
5086
5087const char *
5088output_get_model(struct output *output)
5089{
5090 return output->model;
5091}
5092
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005093static void
Daniel Stone97f68542012-05-30 16:32:01 +01005094fini_xkb(struct input *input)
5095{
5096 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005097 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005098}
5099
Jasper St. Pierre47f10432013-11-12 14:37:20 -05005100#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005101
Daniel Stone97f68542012-05-30 16:32:01 +01005102static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005103display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005104{
5105 struct input *input;
5106
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005107 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005108 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005109 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005110 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005111 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005112 input->pointer_focus = NULL;
5113 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005114 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005115 wl_list_insert(d->input_list.prev, &input->link);
5116
Daniel Stone37816df2012-05-16 18:45:18 +01005117 wl_seat_add_listener(input->seat, &seat_listener, input);
5118 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005119
Jason Ekstranda669bd52014-04-02 19:53:51 -05005120 if (d->data_device_manager) {
5121 input->data_device =
5122 wl_data_device_manager_get_data_device(d->data_device_manager,
5123 input->seat);
5124 wl_data_device_add_listener(input->data_device,
5125 &data_device_listener,
5126 input);
5127 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005128
5129 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005130
Jonny Lamb06959082014-08-12 14:58:27 +02005131 set_repeat_info(input, 40, 400);
5132
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005133 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5134 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005135 input->repeat_task.run = keyboard_repeat_func;
5136 display_watch_fd(d, input->repeat_timer_fd,
5137 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005138}
5139
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005140static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005141input_destroy(struct input *input)
5142{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005143 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005144 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005145
5146 if (input->drag_offer)
5147 data_offer_destroy(input->drag_offer);
5148
5149 if (input->selection_offer)
5150 data_offer_destroy(input->selection_offer);
5151
kabeer khan6ce67ec2014-10-20 11:55:29 +05305152 if (input->data_device) {
5153 if(input->display->data_device_manager_version >= 2)
5154 wl_data_device_release(input->data_device);
5155 else
5156 wl_data_device_destroy(input->data_device);
5157 }
Rob Bradford08031182013-08-13 20:11:03 +01005158 if (input->display->seat_version >= 3) {
5159 if (input->pointer)
5160 wl_pointer_release(input->pointer);
5161 if (input->keyboard)
5162 wl_keyboard_release(input->keyboard);
5163 }
5164
Daniel Stone97f68542012-05-30 16:32:01 +01005165 fini_xkb(input);
5166
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005167 wl_surface_destroy(input->pointer_surface);
5168
Pekka Paalanene1207c72011-12-16 12:02:09 +02005169 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005170 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005171 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005172 free(input);
5173}
5174
5175static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005176init_workspace_manager(struct display *d, uint32_t id)
5177{
5178 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005179 wl_registry_bind(d->registry, id,
5180 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005181 if (d->workspace_manager != NULL)
5182 workspace_manager_add_listener(d->workspace_manager,
5183 &workspace_manager_listener,
5184 d);
5185}
5186
5187static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005188shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5189{
5190 struct display *d = data;
5191
5192 if (format == WL_SHM_FORMAT_RGB565)
5193 d->has_rgb565 = 1;
5194}
5195
5196struct wl_shm_listener shm_listener = {
5197 shm_format
5198};
5199
5200static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005201xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5202{
5203 xdg_shell_pong(shell, serial);
5204}
5205
5206static const struct xdg_shell_listener xdg_shell_listener = {
5207 xdg_shell_ping,
5208};
5209
Pekka Paalanen71182ae2014-08-21 17:47:20 +03005210#define XDG_VERSION 4 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005211#ifdef static_assert
5212static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5213 "Interface version doesn't match implementation version");
5214#endif
5215
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005216static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005217registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5218 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005219{
5220 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005221 struct global *global;
5222
Brian Lovinbc919262013-08-07 15:34:59 -07005223 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005224 global->name = id;
5225 global->interface = strdup(interface);
5226 global->version = version;
5227 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005228
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005229 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005230 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005231 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005232 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005233 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005234 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005235 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005236 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005237 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005238 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005239 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005240 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305241 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005242 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005243 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305244 &wl_data_device_manager_interface,
5245 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005246 } else if (strcmp(interface, "xdg_shell") == 0) {
5247 d->xdg_shell = wl_registry_bind(registry, id,
5248 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005249 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005250 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005251 } else if (strcmp(interface, "text_cursor_position") == 0) {
5252 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005253 wl_registry_bind(registry, id,
5254 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005255 } else if (strcmp(interface, "workspace_manager") == 0) {
5256 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005257 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5258 d->subcompositor =
5259 wl_registry_bind(registry, id,
5260 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005261 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005262
5263 if (d->global_handler)
5264 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005265}
5266
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005267static void
5268registry_handle_global_remove(void *data, struct wl_registry *registry,
5269 uint32_t name)
5270{
5271 struct display *d = data;
5272 struct global *global;
5273 struct global *tmp;
5274
5275 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5276 if (global->name != name)
5277 continue;
5278
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005279 if (strcmp(global->interface, "wl_output") == 0)
5280 display_destroy_output(d, name);
5281
5282 /* XXX: Should destroy remaining bound globals */
5283
5284 if (d->global_handler_remove)
5285 d->global_handler_remove(d, name, global->interface,
5286 global->version, d->user_data);
5287
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005288 wl_list_remove(&global->link);
5289 free(global->interface);
5290 free(global);
5291 }
5292}
5293
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005294void *
5295display_bind(struct display *display, uint32_t name,
5296 const struct wl_interface *interface, uint32_t version)
5297{
5298 return wl_registry_bind(display->registry, name, interface, version);
5299}
5300
5301static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005302 registry_handle_global,
5303 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005304};
5305
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005306#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005307static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005308init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005309{
5310 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005311 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005312
Rob Clark6396ed32012-03-11 19:48:41 -05005313#ifdef USE_CAIRO_GLESV2
5314# define GL_BIT EGL_OPENGL_ES2_BIT
5315#else
5316# define GL_BIT EGL_OPENGL_BIT
5317#endif
5318
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005319 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005320 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005321 EGL_RED_SIZE, 1,
5322 EGL_GREEN_SIZE, 1,
5323 EGL_BLUE_SIZE, 1,
5324 EGL_ALPHA_SIZE, 1,
5325 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005326 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005327 EGL_NONE
5328 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005329
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005330#ifdef USE_CAIRO_GLESV2
5331 static const EGLint context_attribs[] = {
5332 EGL_CONTEXT_CLIENT_VERSION, 2,
5333 EGL_NONE
5334 };
5335 EGLint api = EGL_OPENGL_ES_API;
5336#else
5337 EGLint *context_attribs = NULL;
5338 EGLint api = EGL_OPENGL_API;
5339#endif
5340
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005341 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005342 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005343 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005344 return -1;
5345 }
5346
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005347 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005348 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005349 return -1;
5350 }
5351
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005352 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5353 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005354 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005355 return -1;
5356 }
5357
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005358 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005359 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005360 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005361 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005362 return -1;
5363 }
5364
Benjamin Franzke0c991632011-09-27 21:57:31 +02005365 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5366 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005367 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005368 return -1;
5369 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005370
5371 return 0;
5372}
5373
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005374static void
5375fini_egl(struct display *display)
5376{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005377 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005378
5379 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5380 EGL_NO_CONTEXT);
5381
5382 eglTerminate(display->dpy);
5383 eglReleaseThread();
5384}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005385#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005386
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005387static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005388init_dummy_surface(struct display *display)
5389{
5390 int len;
5391 void *data;
5392
5393 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5394 data = malloc(len);
5395 display->dummy_surface =
5396 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5397 1, 1, len);
5398 display->dummy_surface_data = data;
5399}
5400
5401static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005402handle_display_data(struct task *task, uint32_t events)
5403{
5404 struct display *display =
5405 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005406 struct epoll_event ep;
5407 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005408
5409 display->display_fd_events = events;
5410
5411 if (events & EPOLLERR || events & EPOLLHUP) {
5412 display_exit(display);
5413 return;
5414 }
5415
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005416 if (events & EPOLLIN) {
5417 ret = wl_display_dispatch(display->display);
5418 if (ret == -1) {
5419 display_exit(display);
5420 return;
5421 }
5422 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005423
5424 if (events & EPOLLOUT) {
5425 ret = wl_display_flush(display->display);
5426 if (ret == 0) {
5427 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5428 ep.data.ptr = &display->display_task;
5429 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5430 display->display_fd, &ep);
5431 } else if (ret == -1 && errno != EAGAIN) {
5432 display_exit(display);
5433 return;
5434 }
5435 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005436}
5437
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005438static void
5439log_handler(const char *format, va_list args)
5440{
5441 vfprintf(stderr, format, args);
5442}
5443
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005444struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005445display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005446{
5447 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005448
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005449 wl_log_set_handler_client(log_handler);
5450
Peter Huttererf3d62272013-08-08 11:57:05 +10005451 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005452 if (d == NULL)
5453 return NULL;
5454
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005455 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005456 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005457 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005458 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005459 return NULL;
5460 }
5461
Rob Bradford5ab9c752013-07-26 16:29:43 +01005462 d->xkb_context = xkb_context_new(0);
5463 if (d->xkb_context == NULL) {
5464 fprintf(stderr, "Failed to create XKB context\n");
5465 free(d);
5466 return NULL;
5467 }
5468
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005469 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005470 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005471 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005472 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5473 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005474
5475 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005476 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005477 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005478 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005479
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005480 d->workspace = 0;
5481 d->workspace_count = 1;
5482
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005483 d->registry = wl_display_get_registry(d->display);
5484 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005485
5486 if (wl_display_dispatch(d->display) < 0) {
5487 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5488 return NULL;
5489 }
5490
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005491#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005492 if (init_egl(d) < 0)
5493 fprintf(stderr, "EGL does not seem to work, "
5494 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005495#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005496
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005497 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005498
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005499 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005500
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005501 wl_list_init(&d->window_list);
5502
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005503 init_dummy_surface(d);
5504
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005505 return d;
5506}
5507
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005508static void
5509display_destroy_outputs(struct display *display)
5510{
5511 struct output *tmp;
5512 struct output *output;
5513
5514 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5515 output_destroy(output);
5516}
5517
Pekka Paalanene1207c72011-12-16 12:02:09 +02005518static void
5519display_destroy_inputs(struct display *display)
5520{
5521 struct input *tmp;
5522 struct input *input;
5523
5524 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5525 input_destroy(input);
5526}
5527
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005528void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005529display_destroy(struct display *display)
5530{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005531 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005532 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5533 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005534
5535 if (!wl_list_empty(&display->deferred_list))
5536 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5537
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005538 cairo_surface_destroy(display->dummy_surface);
5539 free(display->dummy_surface_data);
5540
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005541 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005542 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005543
Daniel Stone97f68542012-05-30 16:32:01 +01005544 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005545
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005546 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005547 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005548
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005549#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005550 if (display->argb_device)
5551 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005552#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005553
Pekka Paalanen35e82632013-04-25 13:57:48 +03005554 if (display->subcompositor)
5555 wl_subcompositor_destroy(display->subcompositor);
5556
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005557 if (display->xdg_shell)
5558 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005559
5560 if (display->shm)
5561 wl_shm_destroy(display->shm);
5562
5563 if (display->data_device_manager)
5564 wl_data_device_manager_destroy(display->data_device_manager);
5565
5566 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005567 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005568
5569 close(display->epoll_fd);
5570
U. Artie Eoff44874d92012-10-02 21:12:35 -07005571 if (!(display->display_fd_events & EPOLLERR) &&
5572 !(display->display_fd_events & EPOLLHUP))
5573 wl_display_flush(display->display);
5574
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005575 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005576 free(display);
5577}
5578
5579void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005580display_set_user_data(struct display *display, void *data)
5581{
5582 display->user_data = data;
5583}
5584
5585void *
5586display_get_user_data(struct display *display)
5587{
5588 return display->user_data;
5589}
5590
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005591struct wl_display *
5592display_get_display(struct display *display)
5593{
5594 return display->display;
5595}
5596
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005597int
5598display_has_subcompositor(struct display *display)
5599{
5600 if (display->subcompositor)
5601 return 1;
5602
5603 wl_display_roundtrip(display->display);
5604
5605 return display->subcompositor != NULL;
5606}
5607
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005608cairo_device_t *
5609display_get_cairo_device(struct display *display)
5610{
5611 return display->argb_device;
5612}
5613
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005614struct output *
5615display_get_output(struct display *display)
5616{
5617 return container_of(display->output_list.next, struct output, link);
5618}
5619
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005620struct wl_compositor *
5621display_get_compositor(struct display *display)
5622{
5623 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005624}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005625
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005626uint32_t
5627display_get_serial(struct display *display)
5628{
5629 return display->serial;
5630}
5631
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005632EGLDisplay
5633display_get_egl_display(struct display *d)
5634{
5635 return d->dpy;
5636}
5637
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005638struct wl_data_source *
5639display_create_data_source(struct display *display)
5640{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005641 if (display->data_device_manager)
5642 return wl_data_device_manager_create_data_source(display->data_device_manager);
5643 else
5644 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005645}
5646
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005647EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005648display_get_argb_egl_config(struct display *d)
5649{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005650 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005651}
5652
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005653int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005654display_acquire_window_surface(struct display *display,
5655 struct window *window,
5656 EGLContext ctx)
5657{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005658 struct surface *surface = window->main_surface;
5659
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005660 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005661 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005662
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005663 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005664 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005665}
5666
5667void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005668display_release_window_surface(struct display *display,
5669 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005670{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005671 struct surface *surface = window->main_surface;
5672
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005673 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005674 return;
5675
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005676 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005677}
5678
5679void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005680display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005681{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005682 wl_list_insert(&display->deferred_list, &task->link);
5683}
5684
5685void
5686display_watch_fd(struct display *display,
5687 int fd, uint32_t events, struct task *task)
5688{
5689 struct epoll_event ep;
5690
5691 ep.events = events;
5692 ep.data.ptr = task;
5693 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5694}
5695
5696void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005697display_unwatch_fd(struct display *display, int fd)
5698{
5699 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5700}
5701
5702void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005703display_run(struct display *display)
5704{
5705 struct task *task;
5706 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005707 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005708
Pekka Paalanen826d7952011-12-15 10:14:07 +02005709 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005710 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005711 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005712 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005713 struct task, link);
5714 wl_list_remove(&task->link);
5715 task->run(task, 0);
5716 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005717
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005718 wl_display_dispatch_pending(display->display);
5719
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005720 if (!display->running)
5721 break;
5722
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005723 ret = wl_display_flush(display->display);
5724 if (ret < 0 && errno == EAGAIN) {
5725 ep[0].events =
5726 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5727 ep[0].data.ptr = &display->display_task;
5728
5729 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5730 display->display_fd, &ep[0]);
5731 } else if (ret < 0) {
5732 break;
5733 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005734
5735 count = epoll_wait(display->epoll_fd,
5736 ep, ARRAY_LENGTH(ep), -1);
5737 for (i = 0; i < count; i++) {
5738 task = ep[i].data.ptr;
5739 task->run(task, ep[i].events);
5740 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005741 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005742}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005743
5744void
5745display_exit(struct display *display)
5746{
5747 display->running = 0;
5748}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005749
5750void
5751keysym_modifiers_add(struct wl_array *modifiers_map,
5752 const char *name)
5753{
5754 size_t len = strlen(name) + 1;
5755 char *p;
5756
5757 p = wl_array_add(modifiers_map, len);
5758
5759 if (p == NULL)
5760 return;
5761
5762 strncpy(p, name, len);
5763}
5764
5765static xkb_mod_index_t
5766keysym_modifiers_get_index(struct wl_array *modifiers_map,
5767 const char *name)
5768{
5769 xkb_mod_index_t index = 0;
5770 char *p = modifiers_map->data;
5771
5772 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5773 if (strcmp(p, name) == 0)
5774 return index;
5775
5776 index++;
5777 p += strlen(p) + 1;
5778 }
5779
5780 return XKB_MOD_INVALID;
5781}
5782
5783xkb_mod_mask_t
5784keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5785 const char *name)
5786{
5787 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5788
5789 if (index == XKB_MOD_INVALID)
5790 return XKB_MOD_INVALID;
5791
5792 return 1 << index;
5793}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005794
5795void *
5796fail_on_null(void *p)
5797{
5798 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005799 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005800 exit(EXIT_FAILURE);
5801 }
5802
5803 return p;
5804}
5805
5806void *
5807xmalloc(size_t s)
5808{
5809 return fail_on_null(malloc(s));
5810}
5811
Peter Huttererf3d62272013-08-08 11:57:05 +10005812void *
5813xzalloc(size_t s)
5814{
5815 return fail_on_null(zalloc(s));
5816}
5817
Kristian Høgsbergce278412013-07-25 15:20:20 -07005818char *
5819xstrdup(const char *s)
5820{
5821 return fail_on_null(strdup(s));
5822}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005823
5824void *
5825xrealloc(char *p, size_t s)
5826{
5827 return fail_on_null(realloc(p, s));
5828}