blob: 5cd33dd9635ec1ad79b7d8f6b0185f98907b4c54 [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 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030030#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Derek Foreman493d9792015-03-04 16:26:25 -060042#include <stdbool.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040043
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070069#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070070#include "shared/helpers.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080071#include "xdg-shell-unstable-v5-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060072#include "text-cursor-position-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070073#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050074
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050075#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050076
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090077#include <sys/types.h>
78#include "ivi-application-client-protocol.h"
79#define IVI_SURFACE_ID 9000
80
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070081struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030082
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040083struct global {
84 uint32_t name;
85 char *interface;
86 uint32_t version;
87 struct wl_list link;
88};
89
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050090struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050091 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040092 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050093 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030094 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040095 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040096 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060097 struct text_cursor_position *text_cursor_position;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050098 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090099 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400100 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500101 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200102 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200103 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400104 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400105
106 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700107 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400108 struct task display_task;
109
110 int epoll_fd;
111 struct wl_list deferred_list;
112
Pekka Paalanen826d7952011-12-15 10:14:07 +0200113 int running;
114
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400115 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400116 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400117 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500118 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400119
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400120 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400121
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300122 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300123 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400124
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200125 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400126 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800127 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200128
129 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100130
131 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200132
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200133 /* A hack to get text extents for tooltips */
134 cairo_surface_t *dummy_surface;
135 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200136
137 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530138 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500139};
140
Rob Bradford7507b572012-05-15 17:55:34 +0100141struct window_output {
142 struct output *output;
143 struct wl_list link;
144};
145
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200146struct toysurface {
147 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500148 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800149 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200151 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200152 * If flags has SURFACE_HINT_RESIZE set, the user is
153 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200154 * Returns the Cairo surface to draw to.
155 */
156 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200157 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200158 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200159
160 /*
161 * Post the surface to the server, returning the server allocation
162 * rectangle. The Cairo surface from prepare() must be destroyed
163 * after calling this.
164 */
165 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200166 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200167 struct rectangle *server_allocation);
168
169 /*
170 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800171 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200172 */
173 int (*acquire)(struct toysurface *base, EGLContext ctx);
174
175 /*
176 * Release the toysurface from the EGL context, returning control
177 * to Cairo.
178 */
179 void (*release)(struct toysurface *base);
180
181 /*
182 * Destroy the toysurface, including the Cairo surface, any
183 * backing storage, and the Wayland protocol objects.
184 */
185 void (*destroy)(struct toysurface *base);
186};
187
Pekka Paalanen4e373742013-02-13 16:17:13 +0200188struct surface {
189 struct window *window;
190
191 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300192 struct wl_subsurface *subsurface;
193 int synchronized;
194 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200195 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200196 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300197 int redraw_needed;
198 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300199 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200200
201 struct rectangle allocation;
202 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200203
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200204 struct wl_region *input_region;
205 struct wl_region *opaque_region;
206
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200207 enum window_buffer_type buffer_type;
208 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200209 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200210
211 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300212
213 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200214};
215
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500216struct window {
217 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100218 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500219 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200220 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400221 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500222 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200223 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500224 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400225 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300226 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400227 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400228 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500229 int custom;
230 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400231
Pekka Paalanen99436862012-11-19 17:15:59 +0200232 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400233
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500234 int fullscreen;
235 int maximized;
236
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200237 enum preferred_format preferred_format;
238
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500239 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500240 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400241 window_data_handler_t data_handler;
242 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500243 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400244 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200245 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700246 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400247
Pekka Paalanen4e373742013-02-13 16:17:13 +0200248 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500249 struct xdg_surface *xdg_surface;
250 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300251
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700252 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800253 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500254
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900255 struct ivi_surface *ivi_surface;
256
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500257 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500258
Pekka Paalanen35e82632013-04-25 13:57:48 +0300259 /* struct surface::link, contains also main_surface */
260 struct wl_list subsurface_list;
261
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500262 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400263 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500264};
265
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500266struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500267 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200268 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300269 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500270 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400271 struct wl_list link;
272 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500273 widget_resize_handler_t resize_handler;
274 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500275 widget_enter_handler_t enter_handler;
276 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500277 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500278 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700279 widget_touch_down_handler_t touch_down_handler;
280 widget_touch_up_handler_t touch_up_handler;
281 widget_touch_motion_handler_t touch_motion_handler;
282 widget_touch_frame_handler_t touch_frame_handler;
283 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200284 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000285 widget_pointer_frame_handler_t pointer_frame_handler;
286 widget_axis_source_handler_t axis_source_handler;
287 widget_axis_stop_handler_t axis_stop_handler;
288 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400289 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500290 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300291 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500292 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000293 /* If this is set to false then no cairo surface will be
294 * created before redrawing the surface. This is useful if the
295 * redraw handler is going to do completely custom rendering
296 * such as using EGL directly */
297 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400298};
299
Rusty Lynch041815a2013-08-08 21:20:38 -0700300struct touch_point {
301 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800302 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700303 struct widget *widget;
304 struct wl_list link;
305};
306
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400307struct input {
308 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100309 struct wl_seat *seat;
310 struct wl_pointer *pointer;
311 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700312 struct wl_touch *touch;
313 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct window *pointer_focus;
315 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700316 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300317 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300318 uint32_t cursor_anim_start;
319 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500320 uint32_t cursor_timer_start;
321 uint32_t cursor_anim_current;
322 int cursor_delay_fd;
323 bool cursor_timer_running;
324 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300325 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400326 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400327 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400328 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400329 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400330 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400331
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500332 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500333 struct widget *grab;
334 uint32_t grab_button;
335
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400336 struct wl_data_device *data_device;
337 struct data_offer *drag_offer;
338 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800339 uint32_t touch_grab;
340 int32_t touch_grab_id;
341 float drag_x, drag_y;
342 struct window *drag_focus;
343 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100344
345 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100346 struct xkb_keymap *keymap;
347 struct xkb_state *state;
348 xkb_mod_mask_t control_mask;
349 xkb_mod_mask_t alt_mask;
350 xkb_mod_mask_t shift_mask;
351 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400352
Jonny Lamb06959082014-08-12 14:58:27 +0200353 int32_t repeat_rate_sec;
354 int32_t repeat_rate_nsec;
355 int32_t repeat_delay_sec;
356 int32_t repeat_delay_nsec;
357
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400358 struct task repeat_task;
359 int repeat_timer_fd;
360 uint32_t repeat_sym;
361 uint32_t repeat_key;
362 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500363 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400364};
365
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500366struct output {
367 struct display *display;
368 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800369 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500370 struct rectangle allocation;
371 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600372 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200373 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500374 char *make;
375 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200376
377 display_output_handler_t destroy_handler;
378 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500379};
380
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500381struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500382 struct widget *widget;
383 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500384 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800385
386 uint32_t last_time;
387 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800388 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500389};
390
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500391struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400392 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500393 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500394 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500395 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700396 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500397 const char **entries;
398 uint32_t time;
399 int current;
400 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400401 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500402 menu_func_t func;
403};
404
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300405struct tooltip {
406 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300407 struct widget *widget;
408 char *entry;
409 struct task tooltip_task;
410 int tooltip_fd;
411 float x, y;
412};
413
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700414struct shm_pool {
415 struct wl_shm_pool *pool;
416 size_t size;
417 size_t used;
418 void *data;
419};
420
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400421enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300422 CURSOR_DEFAULT = 100,
423 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400424};
425
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200426static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427
Pekka Paalanen97777442013-05-22 10:20:05 +0300428/* #define DEBUG */
429
430#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300431
432static void
433debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
434__attribute__ ((format (printf, 4, 5)));
435
436static void
437debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
438{
439 va_list ap;
440 struct timeval tv;
441
442 gettimeofday(&tv, NULL);
443 fprintf(stderr, "%8ld.%03ld ",
444 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
445
446 if (proxy)
447 fprintf(stderr, "%s@%d ",
448 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
449
450 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
451 fprintf(stderr, "%s ", func);
452
453 va_start(ap, fmt);
454 vfprintf(stderr, fmt, ap);
455 va_end(ap);
456}
457
458#define DBG(fmt, ...) \
459 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
460
461#define DBG_OBJ(obj, fmt, ...) \
462 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
463
464#else
465
466#define DBG(...) do {} while (0)
467#define DBG_OBJ(...) do {} while (0)
468
469#endif
470
Alexander Larsson1818e312013-05-22 14:41:31 +0200471static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200472surface_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 +0200473{
474 int32_t tmp;
475
476 switch (buffer_transform) {
477 case WL_OUTPUT_TRANSFORM_90:
478 case WL_OUTPUT_TRANSFORM_270:
479 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
480 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
481 tmp = *width;
482 *width = *height;
483 *height = tmp;
484 break;
485 default:
486 break;
487 }
488
489 *width *= buffer_scale;
490 *height *= buffer_scale;
491}
492
493static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200494buffer_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 +0200495{
496 int32_t tmp;
497
498 switch (buffer_transform) {
499 case WL_OUTPUT_TRANSFORM_90:
500 case WL_OUTPUT_TRANSFORM_270:
501 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
502 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
503 tmp = *width;
504 *width = *height;
505 *height = tmp;
506 break;
507 default:
508 break;
509 }
510
511 *width /= buffer_scale;
512 *height /= buffer_scale;
513}
514
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500515#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517struct egl_window_surface {
518 struct toysurface base;
519 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520 struct display *display;
521 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 struct wl_egl_window *egl_window;
523 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100524};
525
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526static struct egl_window_surface *
527to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100528{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200529 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100530}
531
532static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200534 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200535 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100536{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200537 struct egl_window_surface *surface = to_egl_window_surface(base);
538
Alexander Larsson1818e312013-05-22 14:41:31 +0200539 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
540
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200541 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
542 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
543
544 return cairo_surface_reference(surface->cairo_surface);
545}
546
547static void
548egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200549 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200550 struct rectangle *server_allocation)
551{
552 struct egl_window_surface *surface = to_egl_window_surface(base);
553
554 cairo_gl_surface_swapbuffers(surface->cairo_surface);
555 wl_egl_window_get_attached_size(surface->egl_window,
556 &server_allocation->width,
557 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200558
559 buffer_to_surface_size (buffer_transform, buffer_scale,
560 &server_allocation->width,
561 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200562}
563
564static int
565egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
566{
567 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200568 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100569
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200570 device = cairo_surface_get_device(surface->cairo_surface);
571 if (!device)
572 return -1;
573
574 if (!ctx) {
575 if (device == surface->display->argb_device)
576 ctx = surface->display->argb_ctx;
577 else
578 assert(0);
579 }
580
581 cairo_device_flush(device);
582 cairo_device_acquire(device);
583 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
584 surface->egl_surface, ctx))
585 fprintf(stderr, "failed to make surface current\n");
586
587 return 0;
588}
589
590static void
591egl_window_surface_release(struct toysurface *base)
592{
593 struct egl_window_surface *surface = to_egl_window_surface(base);
594 cairo_device_t *device;
595
596 device = cairo_surface_get_device(surface->cairo_surface);
597 if (!device)
598 return;
599
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200600 if (!eglMakeCurrent(surface->display->dpy,
601 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200602 fprintf(stderr, "failed to make context current\n");
603
604 cairo_device_release(device);
605}
606
607static void
608egl_window_surface_destroy(struct toysurface *base)
609{
610 struct egl_window_surface *surface = to_egl_window_surface(base);
611 struct display *d = surface->display;
612
613 cairo_surface_destroy(surface->cairo_surface);
614 eglDestroySurface(d->dpy, surface->egl_surface);
615 wl_egl_window_destroy(surface->egl_window);
616 surface->surface = NULL;
617
618 free(surface);
619}
620
621static struct toysurface *
622egl_window_surface_create(struct display *display,
623 struct wl_surface *wl_surface,
624 uint32_t flags,
625 struct rectangle *rectangle)
626{
627 struct egl_window_surface *surface;
628
Pekka Paalanenb3627362012-11-19 17:16:00 +0200629 if (display->dpy == EGL_NO_DISPLAY)
630 return NULL;
631
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200632 surface = calloc(1, sizeof *surface);
633 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634 return NULL;
635
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200636 surface->base.prepare = egl_window_surface_prepare;
637 surface->base.swap = egl_window_surface_swap;
638 surface->base.acquire = egl_window_surface_acquire;
639 surface->base.release = egl_window_surface_release;
640 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200642 surface->display = display;
643 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400644
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200645 surface->egl_window = wl_egl_window_create(surface->surface,
646 rectangle->width,
647 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100649 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100650 weston_platform_create_egl_surface(display->dpy,
651 display->argb_config,
652 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100653
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200654 surface->cairo_surface =
655 cairo_gl_surface_create_for_egl(display->argb_device,
656 surface->egl_surface,
657 rectangle->width,
658 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100659
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200660 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100661}
662
Pekka Paalanenb3627362012-11-19 17:16:00 +0200663#else
664
665static struct toysurface *
666egl_window_surface_create(struct display *display,
667 struct wl_surface *wl_surface,
668 uint32_t flags,
669 struct rectangle *rectangle)
670{
671 return NULL;
672}
673
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674#endif
675
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200676struct shm_surface_data {
677 struct wl_buffer *buffer;
678 struct shm_pool *pool;
679};
680
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681struct wl_buffer *
682display_get_buffer_for_surface(struct display *display,
683 cairo_surface_t *surface)
684{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200685 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400686
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200687 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400688
689 return data->buffer;
690}
691
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500692static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700693shm_pool_destroy(struct shm_pool *pool);
694
695static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400696shm_surface_data_destroy(void *p)
697{
698 struct shm_surface_data *data = p;
699
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200700 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700701 if (data->pool)
702 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300703
704 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705}
706
Kristian Høgsberg16626282012-04-03 11:21:27 -0400707static struct wl_shm_pool *
708make_shm_pool(struct display *display, int size, void **data)
709{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400710 struct wl_shm_pool *pool;
711 int fd;
712
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300713 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400714 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300715 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
716 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400717 return NULL;
718 }
719
720 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400721 if (*data == MAP_FAILED) {
722 fprintf(stderr, "mmap failed: %m\n");
723 close(fd);
724 return NULL;
725 }
726
727 pool = wl_shm_create_pool(display->shm, fd, size);
728
729 close(fd);
730
731 return pool;
732}
733
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700734static struct shm_pool *
735shm_pool_create(struct display *display, size_t size)
736{
737 struct shm_pool *pool = malloc(sizeof *pool);
738
739 if (!pool)
740 return NULL;
741
742 pool->pool = make_shm_pool(display, size, &pool->data);
743 if (!pool->pool) {
744 free(pool);
745 return NULL;
746 }
747
748 pool->size = size;
749 pool->used = 0;
750
751 return pool;
752}
753
754static void *
755shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
756{
757 if (pool->used + size > pool->size)
758 return NULL;
759
760 *offset = pool->used;
761 pool->used += size;
762
763 return (char *) pool->data + *offset;
764}
765
766/* destroy the pool. this does not unmap the memory though */
767static void
768shm_pool_destroy(struct shm_pool *pool)
769{
770 munmap(pool->data, pool->size);
771 wl_shm_pool_destroy(pool->pool);
772 free(pool);
773}
774
775/* Start allocating from the beginning of the pool again */
776static void
777shm_pool_reset(struct shm_pool *pool)
778{
779 pool->used = 0;
780}
781
782static int
783data_length_for_shm_surface(struct rectangle *rect)
784{
785 int stride;
786
787 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
788 rect->width);
789 return stride * rect->height;
790}
791
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500792static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700793display_create_shm_surface_from_pool(struct display *display,
794 struct rectangle *rectangle,
795 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400796{
797 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400798 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400799 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200800 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700801 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400802 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400803
804 data = malloc(sizeof *data);
805 if (data == NULL)
806 return NULL;
807
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200808 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
809 cairo_format = CAIRO_FORMAT_RGB16_565;
810 else
811 cairo_format = CAIRO_FORMAT_ARGB32;
812
813 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700814 length = stride * rectangle->height;
815 data->pool = NULL;
816 map = shm_pool_allocate(pool, length, &offset);
817
818 if (!map) {
819 free(data);
820 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400821 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400822
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400823 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200824 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400825 rectangle->width,
826 rectangle->height,
827 stride);
828
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200829 cairo_surface_set_user_data(surface, &shm_surface_data_key,
830 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200832 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
833 format = WL_SHM_FORMAT_RGB565;
834 else {
835 if (flags & SURFACE_OPAQUE)
836 format = WL_SHM_FORMAT_XRGB8888;
837 else
838 format = WL_SHM_FORMAT_ARGB8888;
839 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400840
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200841 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
842 rectangle->width,
843 rectangle->height,
844 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400845
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700846 return surface;
847}
848
849static cairo_surface_t *
850display_create_shm_surface(struct display *display,
851 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200852 struct shm_pool *alternate_pool,
853 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700854{
855 struct shm_surface_data *data;
856 struct shm_pool *pool;
857 cairo_surface_t *surface;
858
Pekka Paalanen99436862012-11-19 17:15:59 +0200859 if (alternate_pool) {
860 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700861 surface = display_create_shm_surface_from_pool(display,
862 rectangle,
863 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200864 alternate_pool);
865 if (surface) {
866 data = cairo_surface_get_user_data(surface,
867 &shm_surface_data_key);
868 goto out;
869 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700870 }
871
872 pool = shm_pool_create(display,
873 data_length_for_shm_surface(rectangle));
874 if (!pool)
875 return NULL;
876
877 surface =
878 display_create_shm_surface_from_pool(display, rectangle,
879 flags, pool);
880
881 if (!surface) {
882 shm_pool_destroy(pool);
883 return NULL;
884 }
885
886 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200887 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700888 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889
Pekka Paalanen99436862012-11-19 17:15:59 +0200890out:
891 if (data_ret)
892 *data_ret = data;
893
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894 return surface;
895}
896
nobled7b87cb02011-02-01 18:51:47 +0000897static int
898check_size(struct rectangle *rect)
899{
900 if (rect->width && rect->height)
901 return 0;
902
903 fprintf(stderr, "tried to create surface of "
904 "width: %d, height: %d\n", rect->width, rect->height);
905 return -1;
906}
907
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400908cairo_surface_t *
909display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100910 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400911 struct rectangle *rectangle,
912 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400913{
nobled7b87cb02011-02-01 18:51:47 +0000914 if (check_size(rectangle) < 0)
915 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200916
917 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200918 return display_create_shm_surface(display, rectangle, flags,
919 NULL, NULL);
920}
921
Pekka Paalanena4eda732012-11-19 17:16:02 +0200922struct shm_surface_leaf {
923 cairo_surface_t *cairo_surface;
924 /* 'data' is automatically destroyed, when 'cairo_surface' is */
925 struct shm_surface_data *data;
926
927 struct shm_pool *resize_pool;
928 int busy;
929};
930
931static void
932shm_surface_leaf_release(struct shm_surface_leaf *leaf)
933{
934 if (leaf->cairo_surface)
935 cairo_surface_destroy(leaf->cairo_surface);
936 /* leaf->data already destroyed via cairo private */
937
938 if (leaf->resize_pool)
939 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200940
941 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942}
943
Pekka Paalanenaef02542013-04-25 13:57:47 +0300944#define MAX_LEAVES 3
945
Pekka Paalanen99436862012-11-19 17:15:59 +0200946struct shm_surface {
947 struct toysurface base;
948 struct display *display;
949 struct wl_surface *surface;
950 uint32_t flags;
951 int dx, dy;
952
Pekka Paalanenaef02542013-04-25 13:57:47 +0300953 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200954 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200955};
956
957static struct shm_surface *
958to_shm_surface(struct toysurface *base)
959{
960 return container_of(base, struct shm_surface, base);
961}
962
Pekka Paalanena4eda732012-11-19 17:16:02 +0200963static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300964shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
965{
966#ifdef DEBUG
967 struct shm_surface_leaf *leaf;
968 char bufs[MAX_LEAVES + 1];
969 int i;
970
971 for (i = 0; i < MAX_LEAVES; i++) {
972 leaf = &surface->leaf[i];
973
974 if (leaf->busy)
975 bufs[i] = 'b';
976 else if (leaf->cairo_surface)
977 bufs[i] = 'a';
978 else
979 bufs[i] = ' ';
980 }
981
982 bufs[MAX_LEAVES] = '\0';
983 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
984#endif
985}
986
987static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200988shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
989{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200990 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300991 struct shm_surface_leaf *leaf;
992 int i;
993 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300994
995 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200996
Pekka Paalanenaef02542013-04-25 13:57:47 +0300997 for (i = 0; i < MAX_LEAVES; i++) {
998 leaf = &surface->leaf[i];
999 if (leaf->data && leaf->data->buffer == buffer) {
1000 leaf->busy = 0;
1001 break;
1002 }
1003 }
1004 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001005
Pekka Paalanenaef02542013-04-25 13:57:47 +03001006 /* Leave one free leaf with storage, release others */
1007 free_found = 0;
1008 for (i = 0; i < MAX_LEAVES; i++) {
1009 leaf = &surface->leaf[i];
1010
1011 if (!leaf->cairo_surface || leaf->busy)
1012 continue;
1013
1014 if (!free_found)
1015 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001016 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001017 shm_surface_leaf_release(leaf);
1018 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001019
Pekka Paalanen97777442013-05-22 10:20:05 +03001020 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001021}
1022
1023static const struct wl_buffer_listener shm_surface_buffer_listener = {
1024 shm_surface_buffer_release
1025};
1026
Pekka Paalanen99436862012-11-19 17:15:59 +02001027static cairo_surface_t *
1028shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001029 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001030 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001031{
Pekka Paalanenec076692012-11-30 13:37:27 +02001032 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001033 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001034 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001035 struct shm_surface_leaf *leaf = NULL;
1036 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001037
1038 surface->dx = dx;
1039 surface->dy = dy;
1040
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001041 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001042 for (i = 0; i < MAX_LEAVES; i++) {
1043 if (surface->leaf[i].busy)
1044 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001045
Pekka Paalanenaef02542013-04-25 13:57:47 +03001046 if (!leaf || surface->leaf[i].cairo_surface)
1047 leaf = &surface->leaf[i];
1048 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001049 DBG_OBJ(surface->surface, "pick leaf %d\n",
1050 (int)(leaf - &surface->leaf[0]));
1051
Pekka Paalanenaef02542013-04-25 13:57:47 +03001052 if (!leaf) {
1053 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001054 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001055 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001056 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001057 }
1058
Pekka Paalanena4eda732012-11-19 17:16:02 +02001059 if (!resize_hint && leaf->resize_pool) {
1060 cairo_surface_destroy(leaf->cairo_surface);
1061 leaf->cairo_surface = NULL;
1062 shm_pool_destroy(leaf->resize_pool);
1063 leaf->resize_pool = NULL;
1064 }
1065
Alexander Larsson1818e312013-05-22 14:41:31 +02001066 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1067
Pekka Paalanena4eda732012-11-19 17:16:02 +02001068 if (leaf->cairo_surface &&
1069 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1070 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001071 goto out;
1072
Pekka Paalanena4eda732012-11-19 17:16:02 +02001073 if (leaf->cairo_surface)
1074 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001075
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001076#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001077 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001078 /* Create a big pool to allocate from, while continuously
1079 * resizing. Mmapping a new pool in the server
1080 * is relatively expensive, so reusing a pool performs
1081 * better, but may temporarily reserve unneeded memory.
1082 */
1083 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 leaf->resize_pool = shm_pool_create(surface->display,
1085 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001086 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001087#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001088
Alexander Larsson1818e312013-05-22 14:41:31 +02001089 rect.width = width;
1090 rect.height = height;
1091
Pekka Paalanena4eda732012-11-19 17:16:02 +02001092 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001093 display_create_shm_surface(surface->display, &rect,
1094 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001095 leaf->resize_pool,
1096 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001097 if (!leaf->cairo_surface)
1098 return NULL;
1099
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001101 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001102
1103out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 surface->current = leaf;
1105
1106 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001107}
1108
1109static void
1110shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001111 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001112 struct rectangle *server_allocation)
1113{
1114 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001115 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001116
1117 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001118 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001119 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001121
Alexander Larsson1818e312013-05-22 14:41:31 +02001122 buffer_to_surface_size (buffer_transform, buffer_scale,
1123 &server_allocation->width,
1124 &server_allocation->height);
1125
Pekka Paalanena4eda732012-11-19 17:16:02 +02001126 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001127 surface->dx, surface->dy);
1128 wl_surface_damage(surface->surface, 0, 0,
1129 server_allocation->width, server_allocation->height);
1130 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001131
Pekka Paalanen71233882013-04-25 13:57:53 +03001132 DBG_OBJ(surface->surface, "leaf %d busy\n",
1133 (int)(leaf - &surface->leaf[0]));
1134
Pekka Paalanena4eda732012-11-19 17:16:02 +02001135 leaf->busy = 1;
1136 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001137}
1138
1139static int
1140shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1141{
1142 return -1;
1143}
1144
1145static void
1146shm_surface_release(struct toysurface *base)
1147{
1148}
1149
1150static void
1151shm_surface_destroy(struct toysurface *base)
1152{
1153 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001154 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001155
Pekka Paalanenaef02542013-04-25 13:57:47 +03001156 for (i = 0; i < MAX_LEAVES; i++)
1157 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001158
1159 free(surface);
1160}
1161
1162static struct toysurface *
1163shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1164 uint32_t flags, struct rectangle *rectangle)
1165{
1166 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001167 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001168
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001169 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001170 surface->base.prepare = shm_surface_prepare;
1171 surface->base.swap = shm_surface_swap;
1172 surface->base.acquire = shm_surface_acquire;
1173 surface->base.release = shm_surface_release;
1174 surface->base.destroy = shm_surface_destroy;
1175
1176 surface->display = display;
1177 surface->surface = wl_surface;
1178 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001179
1180 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001181}
1182
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001183/*
1184 * The following correspondences between file names and cursors was copied
1185 * from: https://bugs.kde.org/attachment.cgi?id=67313
1186 */
1187
1188static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001189 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001190 "sw-resize",
1191 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001192};
1193
1194static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001195 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001196 "se-resize",
1197 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001198};
1199
1200static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001201 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001202 "s-resize",
1203 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001204};
1205
1206static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001207 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001208 "closedhand",
1209 "208530c400c041818281048008011002"
1210};
1211
1212static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001213 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001214 "default",
1215 "top_left_arrow",
1216 "left-arrow"
1217};
1218
1219static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001220 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001221 "w-resize",
1222 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001223};
1224
1225static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001226 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001227 "e-resize",
1228 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001229};
1230
1231static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001233 "nw-resize",
1234 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235};
1236
1237static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001238 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001239 "ne-resize",
1240 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001241};
1242
1243static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001244 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001245 "n-resize",
1246 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001247};
1248
1249static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001250 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001251 "ibeam",
1252 "text"
1253};
1254
1255static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001256 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001257 "pointer",
1258 "pointing_hand",
1259 "e29285e634086352946a0e7090d73106"
1260};
1261
1262static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001263 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001264 "wait",
1265 "0426c94ea35c87780ff01dc239897213"
1266};
1267
1268struct cursor_alternatives {
1269 const char **names;
1270 size_t count;
1271};
1272
1273static const struct cursor_alternatives cursors[] = {
1274 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1275 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1276 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1277 {grabbings, ARRAY_LENGTH(grabbings)},
1278 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1279 {left_sides, ARRAY_LENGTH(left_sides)},
1280 {right_sides, ARRAY_LENGTH(right_sides)},
1281 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1282 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1283 {top_sides, ARRAY_LENGTH(top_sides)},
1284 {xterms, ARRAY_LENGTH(xterms)},
1285 {hand1s, ARRAY_LENGTH(hand1s)},
1286 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001287};
1288
1289static void
1290create_cursors(struct display *display)
1291{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001292 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001293 struct weston_config *config;
1294 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001295 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001296 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001297 unsigned int i, j;
1298 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001299
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001300 config_file = weston_config_get_name_from_env();
1301 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001302 s = weston_config_get_section(config, "shell", NULL, NULL);
1303 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1304 weston_config_section_get_int(s, "cursor-size", &size, 32);
1305 weston_config_destroy(config);
1306
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001307 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001308 if (!display->cursor_theme) {
1309 fprintf(stderr, "could not load theme '%s'\n", theme);
1310 return;
1311 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001312 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001313 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001314 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001315
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001316 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001317 cursor = NULL;
1318 for (j = 0; !cursor && j < cursors[i].count; ++j)
1319 cursor = wl_cursor_theme_get_cursor(
1320 display->cursor_theme, cursors[i].names[j]);
1321
1322 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001323 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001324 cursors[i].names[0]);
1325
1326 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001327 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001328}
1329
1330static void
1331destroy_cursors(struct display *display)
1332{
1333 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001334 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001335}
1336
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001337struct wl_cursor_image *
1338display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001339{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001340 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001341
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001342 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001343}
1344
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001345static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001346surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001347{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001348 if (!surface->cairo_surface)
1349 return;
1350
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001351 if (surface->opaque_region) {
1352 wl_surface_set_opaque_region(surface->surface,
1353 surface->opaque_region);
1354 wl_region_destroy(surface->opaque_region);
1355 surface->opaque_region = NULL;
1356 }
1357
1358 if (surface->input_region) {
1359 wl_surface_set_input_region(surface->surface,
1360 surface->input_region);
1361 wl_region_destroy(surface->input_region);
1362 surface->input_region = NULL;
1363 }
1364
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001365 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001366 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001367 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001368
Pekka Paalanen89dee002013-02-13 16:17:20 +02001369 cairo_surface_destroy(surface->cairo_surface);
1370 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001371}
1372
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001373int
1374window_has_focus(struct window *window)
1375{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001376 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001377}
1378
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001379static void
1380window_close(struct window *window)
1381{
1382 if (window->close_handler)
1383 window->close_handler(window->user_data);
1384 else
1385 display_exit(window->display);
1386}
1387
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001388struct display *
1389window_get_display(struct window *window)
1390{
1391 return window->display;
1392}
1393
Pekka Paalanen89dee002013-02-13 16:17:20 +02001394static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001395handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1396 int32_t width, int32_t height)
1397{
1398 struct window *window = data;
1399
1400 window_schedule_resize(window, width, height);
1401}
1402
1403static const struct ivi_surface_listener ivi_surface_listener = {
1404 handle_ivi_surface_configure,
1405};
1406
1407static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001408surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001409{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001410 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001411 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001412
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001413 if (!surface->toysurface && display->dpy &&
1414 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001415 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001416 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001417 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001418 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001419 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001420 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001421
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001422 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001423 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001424 surface->surface,
1425 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001426
Pekka Paalanen89dee002013-02-13 16:17:20 +02001427 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001428 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001429 allocation.width, allocation.height, flags,
1430 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001431}
1432
1433static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001434window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001435{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001436 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001437 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001438
Pekka Paalanenec076692012-11-30 13:37:27 +02001439 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001440 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001441
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001442 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1443 flags |= SURFACE_HINT_RGB565;
1444
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001445 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001446}
1447
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001448int
1449window_get_buffer_transform(struct window *window)
1450{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001451 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001452}
1453
1454void
1455window_set_buffer_transform(struct window *window,
1456 enum wl_output_transform transform)
1457{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001458 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001459 wl_surface_set_buffer_transform(window->main_surface->surface,
1460 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001461}
1462
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001463void
1464window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001465 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001466{
1467 window->main_surface->buffer_scale = scale;
1468 wl_surface_set_buffer_scale(window->main_surface->surface,
1469 scale);
1470}
1471
1472uint32_t
1473window_get_buffer_scale(struct window *window)
1474{
1475 return window->main_surface->buffer_scale;
1476}
1477
Alexander Larssond68f5232013-05-22 14:41:33 +02001478uint32_t
1479window_get_output_scale(struct window *window)
1480{
1481 struct window_output *window_output;
1482 struct window_output *window_output_tmp;
1483 int scale = 1;
1484
1485 wl_list_for_each_safe(window_output, window_output_tmp,
1486 &window->window_output_list, link) {
1487 if (window_output->output->scale > scale)
1488 scale = window_output->output->scale;
1489 }
1490
1491 return scale;
1492}
1493
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001494static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001495
Pekka Paalanen4e373742013-02-13 16:17:13 +02001496static void
1497surface_destroy(struct surface *surface)
1498{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001499 if (surface->frame_cb)
1500 wl_callback_destroy(surface->frame_cb);
1501
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001502 if (surface->input_region)
1503 wl_region_destroy(surface->input_region);
1504
1505 if (surface->opaque_region)
1506 wl_region_destroy(surface->opaque_region);
1507
Pekka Paalanen35e82632013-04-25 13:57:48 +03001508 if (surface->subsurface)
1509 wl_subsurface_destroy(surface->subsurface);
1510
Pekka Paalanen4e373742013-02-13 16:17:13 +02001511 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001512
1513 if (surface->toysurface)
1514 surface->toysurface->destroy(surface->toysurface);
1515
Pekka Paalanen35e82632013-04-25 13:57:48 +03001516 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001517 free(surface);
1518}
1519
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001520void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001521window_destroy(struct window *window)
1522{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001523 struct display *display = window->display;
1524 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001525 struct window_output *window_output;
1526 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001527
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001528 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001529
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001530 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001531 if (input->touch_focus == window)
1532 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001533 if (input->pointer_focus == window)
1534 input->pointer_focus = NULL;
1535 if (input->keyboard_focus == window)
1536 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001537 if (input->focus_widget &&
1538 input->focus_widget->window == window)
1539 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001540 }
1541
Rob Bradford7507b572012-05-15 17:55:34 +01001542 wl_list_for_each_safe(window_output, window_output_tmp,
1543 &window->window_output_list, link) {
1544 free (window_output);
1545 }
1546
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001547 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001548 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001549
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001550 if (window->xdg_surface)
1551 xdg_surface_destroy(window->xdg_surface);
1552 if (window->xdg_popup)
1553 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001554
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001555 if (window->ivi_surface)
1556 ivi_surface_destroy(window->ivi_surface);
1557
Pekka Paalanen4e373742013-02-13 16:17:13 +02001558 surface_destroy(window->main_surface);
1559
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001560 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001561
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001562 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001563 free(window);
1564}
1565
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001566static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001567widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001569 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001570
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001571 wl_list_for_each(child, &widget->child_list, link) {
1572 target = widget_find_widget(child, x, y);
1573 if (target)
1574 return target;
1575 }
1576
1577 if (widget->allocation.x <= x &&
1578 x < widget->allocation.x + widget->allocation.width &&
1579 widget->allocation.y <= y &&
1580 y < widget->allocation.y + widget->allocation.height) {
1581 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001582 }
1583
1584 return NULL;
1585}
1586
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001588window_find_widget(struct window *window, int32_t x, int32_t y)
1589{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001590 struct surface *surface;
1591 struct widget *widget;
1592
1593 wl_list_for_each(surface, &window->subsurface_list, link) {
1594 widget = widget_find_widget(surface->widget, x, y);
1595 if (widget)
1596 return widget;
1597 }
1598
1599 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001600}
1601
1602static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001603widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001604{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001605 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001606
Peter Huttererf3d62272013-08-08 11:57:05 +10001607 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001608 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001609 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001610 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001611 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001613 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001614 widget->tooltip = NULL;
1615 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001616 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001617 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001618
1619 return widget;
1620}
1621
1622struct widget *
1623window_add_widget(struct window *window, void *data)
1624{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001625 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001626
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001627 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001628 wl_list_init(&widget->link);
1629 window->main_surface->widget = widget;
1630
1631 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001632}
1633
1634struct widget *
1635widget_add_widget(struct widget *parent, void *data)
1636{
1637 struct widget *widget;
1638
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001639 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001640 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001641
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001642 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001643}
1644
1645void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001646widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001648 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001649 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001650 struct input *input;
1651
Pekka Paalanen35e82632013-04-25 13:57:48 +03001652 /* Destroy the sub-surface along with the root widget */
1653 if (surface->widget == widget && surface->subsurface)
1654 surface_destroy(widget->surface);
1655
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001656 if (widget->tooltip)
1657 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001658
Pekka Paalanene156fb62012-01-19 13:51:38 +02001659 wl_list_for_each(input, &display->input_list, link) {
1660 if (input->focus_widget == widget)
1661 input->focus_widget = NULL;
1662 }
1663
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001664 wl_list_remove(&widget->link);
1665 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001666}
1667
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001668void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001669widget_set_default_cursor(struct widget *widget, int cursor)
1670{
1671 widget->default_cursor = cursor;
1672}
1673
1674void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001675widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001676{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001677 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001678}
1679
1680void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001681widget_set_size(struct widget *widget, int32_t width, int32_t height)
1682{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001683 widget->allocation.width = width;
1684 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001685}
1686
1687void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001688widget_set_allocation(struct widget *widget,
1689 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001691 widget->allocation.x = x;
1692 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001693 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694}
1695
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001696void
1697widget_set_transparent(struct widget *widget, int transparent)
1698{
1699 widget->opaque = !transparent;
1700}
1701
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001702void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001703widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001704{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001705 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706}
1707
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001708static cairo_surface_t *
1709widget_get_cairo_surface(struct widget *widget)
1710{
1711 struct surface *surface = widget->surface;
1712 struct window *window = widget->window;
1713
Neil Roberts97b747c2013-12-19 16:17:12 +00001714 assert(widget->use_cairo);
1715
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001716 if (!surface->cairo_surface) {
1717 if (surface == window->main_surface)
1718 window_create_main_surface(window);
1719 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001720 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001721 }
1722
1723 return surface->cairo_surface;
1724}
1725
Alexander Larsson15901f02013-05-22 14:41:25 +02001726static void
1727widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1728{
1729 struct surface *surface = widget->surface;
1730 double angle;
1731 cairo_matrix_t m;
1732 enum wl_output_transform transform;
1733 int surface_width, surface_height;
1734 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001735 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001736
1737 surface_width = surface->allocation.width;
1738 surface_height = surface->allocation.height;
1739
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001740 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001741 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001742
Alexander Larsson15901f02013-05-22 14:41:25 +02001743 switch (transform) {
1744 case WL_OUTPUT_TRANSFORM_FLIPPED:
1745 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1748 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1749 break;
1750 default:
1751 cairo_matrix_init_identity(&m);
1752 break;
1753 }
1754
1755 switch (transform) {
1756 case WL_OUTPUT_TRANSFORM_NORMAL:
1757 default:
1758 angle = 0;
1759 translate_x = 0;
1760 translate_y = 0;
1761 break;
1762 case WL_OUTPUT_TRANSFORM_FLIPPED:
1763 angle = 0;
1764 translate_x = surface_width;
1765 translate_y = 0;
1766 break;
1767 case WL_OUTPUT_TRANSFORM_90:
1768 angle = M_PI_2;
1769 translate_x = surface_height;
1770 translate_y = 0;
1771 break;
1772 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1773 angle = M_PI_2;
1774 translate_x = surface_height;
1775 translate_y = surface_width;
1776 break;
1777 case WL_OUTPUT_TRANSFORM_180:
1778 angle = M_PI;
1779 translate_x = surface_width;
1780 translate_y = surface_height;
1781 break;
1782 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1783 angle = M_PI;
1784 translate_x = 0;
1785 translate_y = surface_height;
1786 break;
1787 case WL_OUTPUT_TRANSFORM_270:
1788 angle = M_PI + M_PI_2;
1789 translate_x = 0;
1790 translate_y = surface_width;
1791 break;
1792 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1793 angle = M_PI + M_PI_2;
1794 translate_x = 0;
1795 translate_y = 0;
1796 break;
1797 }
1798
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001799 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001800 cairo_translate(cr, translate_x, translate_y);
1801 cairo_rotate(cr, angle);
1802 cairo_transform(cr, &m);
1803}
1804
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001805cairo_t *
1806widget_cairo_create(struct widget *widget)
1807{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001808 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001809 cairo_surface_t *cairo_surface;
1810 cairo_t *cr;
1811
1812 cairo_surface = widget_get_cairo_surface(widget);
1813 cr = cairo_create(cairo_surface);
1814
Alexander Larsson15901f02013-05-22 14:41:25 +02001815 widget_cairo_update_transform(widget, cr);
1816
Pekka Paalanen35e82632013-04-25 13:57:48 +03001817 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1818
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001819 return cr;
1820}
1821
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001822struct wl_surface *
1823widget_get_wl_surface(struct widget *widget)
1824{
1825 return widget->surface->surface;
1826}
1827
Neil Roberts5e10a042013-09-09 00:40:17 +01001828struct wl_subsurface *
1829widget_get_wl_subsurface(struct widget *widget)
1830{
1831 return widget->surface->subsurface;
1832}
1833
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001834uint32_t
1835widget_get_last_time(struct widget *widget)
1836{
1837 return widget->surface->last_time;
1838}
1839
1840void
1841widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1842{
1843 struct wl_compositor *comp = widget->window->display->compositor;
1844 struct surface *surface = widget->surface;
1845
1846 if (!surface->input_region)
1847 surface->input_region = wl_compositor_create_region(comp);
1848
1849 if (rect) {
1850 wl_region_add(surface->input_region,
1851 rect->x, rect->y, rect->width, rect->height);
1852 }
1853}
1854
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001855void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001856widget_set_resize_handler(struct widget *widget,
1857 widget_resize_handler_t handler)
1858{
1859 widget->resize_handler = handler;
1860}
1861
1862void
1863widget_set_redraw_handler(struct widget *widget,
1864 widget_redraw_handler_t handler)
1865{
1866 widget->redraw_handler = handler;
1867}
1868
1869void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001870widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001871{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001872 widget->enter_handler = handler;
1873}
1874
1875void
1876widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1877{
1878 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001879}
1880
1881void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001882widget_set_motion_handler(struct widget *widget,
1883 widget_motion_handler_t handler)
1884{
1885 widget->motion_handler = handler;
1886}
1887
1888void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001889widget_set_button_handler(struct widget *widget,
1890 widget_button_handler_t handler)
1891{
1892 widget->button_handler = handler;
1893}
1894
1895void
Rusty Lynch041815a2013-08-08 21:20:38 -07001896widget_set_touch_up_handler(struct widget *widget,
1897 widget_touch_up_handler_t handler)
1898{
1899 widget->touch_up_handler = handler;
1900}
1901
1902void
1903widget_set_touch_down_handler(struct widget *widget,
1904 widget_touch_down_handler_t handler)
1905{
1906 widget->touch_down_handler = handler;
1907}
1908
1909void
1910widget_set_touch_motion_handler(struct widget *widget,
1911 widget_touch_motion_handler_t handler)
1912{
1913 widget->touch_motion_handler = handler;
1914}
1915
1916void
1917widget_set_touch_frame_handler(struct widget *widget,
1918 widget_touch_frame_handler_t handler)
1919{
1920 widget->touch_frame_handler = handler;
1921}
1922
1923void
1924widget_set_touch_cancel_handler(struct widget *widget,
1925 widget_touch_cancel_handler_t handler)
1926{
1927 widget->touch_cancel_handler = handler;
1928}
1929
1930void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001931widget_set_axis_handler(struct widget *widget,
1932 widget_axis_handler_t handler)
1933{
1934 widget->axis_handler = handler;
1935}
1936
Peter Hutterer87743e92016-01-18 16:38:22 +10001937void
1938widget_set_pointer_frame_handler(struct widget *widget,
1939 widget_pointer_frame_handler_t handler)
1940{
1941 widget->pointer_frame_handler = handler;
1942}
1943
1944void
1945widget_set_axis_handlers(struct widget *widget,
1946 widget_axis_handler_t axis_handler,
1947 widget_axis_source_handler_t axis_source_handler,
1948 widget_axis_stop_handler_t axis_stop_handler,
1949 widget_axis_discrete_handler_t axis_discrete_handler)
1950{
1951 widget->axis_handler = axis_handler;
1952 widget->axis_source_handler = axis_source_handler;
1953 widget->axis_stop_handler = axis_stop_handler;
1954 widget->axis_discrete_handler = axis_discrete_handler;
1955}
1956
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001957static void
1958window_schedule_redraw_task(struct window *window);
1959
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001960void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001961widget_schedule_redraw(struct widget *widget)
1962{
Pekka Paalanen71233882013-04-25 13:57:53 +03001963 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001964 widget->surface->redraw_needed = 1;
1965 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001966}
1967
Neil Roberts97b747c2013-12-19 16:17:12 +00001968void
1969widget_set_use_cairo(struct widget *widget,
1970 int use_cairo)
1971{
1972 widget->use_cairo = use_cairo;
1973}
1974
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001975cairo_surface_t *
1976window_get_surface(struct window *window)
1977{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001978 cairo_surface_t *cairo_surface;
1979
1980 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1981
1982 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001983}
1984
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001985struct wl_surface *
1986window_get_wl_surface(struct window *window)
1987{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001988 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001989}
1990
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001991static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001992tooltip_redraw_handler(struct widget *widget, void *data)
1993{
1994 cairo_t *cr;
1995 const int32_t r = 3;
1996 struct tooltip *tooltip = data;
1997 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001998
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001999 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002000 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002001 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2002 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2003 cairo_paint(cr);
2004
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002005 width = widget->allocation.width;
2006 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002007 rounded_rect(cr, 0, 0, width, height, r);
2008
2009 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2010 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2011 cairo_fill(cr);
2012
2013 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2014 cairo_move_to(cr, 10, 16);
2015 cairo_show_text(cr, tooltip->entry);
2016 cairo_destroy(cr);
2017}
2018
2019static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002020get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002021{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002022 cairo_t *cr;
2023 cairo_text_extents_t extents;
2024
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002025 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002026 * created yet, and parent does not have a valid surface
2027 * outside repaint, either.
2028 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002029 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002030 cairo_text_extents(cr, tooltip->entry, &extents);
2031 cairo_destroy(cr);
2032
2033 return extents;
2034}
2035
2036static int
2037window_create_tooltip(struct tooltip *tooltip)
2038{
2039 struct widget *parent = tooltip->parent;
2040 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002041 const int offset_y = 27;
2042 const int margin = 3;
2043 cairo_text_extents_t extents;
2044
2045 if (tooltip->widget)
2046 return 0;
2047
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002048 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002049
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002050 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002051 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002052 widget_set_allocation(tooltip->widget,
2053 tooltip->x, tooltip->y + offset_y,
2054 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002055
2056 return 0;
2057}
2058
2059void
2060widget_destroy_tooltip(struct widget *parent)
2061{
2062 struct tooltip *tooltip = parent->tooltip;
2063
2064 parent->tooltip_count = 0;
2065 if (!tooltip)
2066 return;
2067
2068 if (tooltip->widget) {
2069 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002070 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002071 }
2072
2073 close(tooltip->tooltip_fd);
2074 free(tooltip->entry);
2075 free(tooltip);
2076 parent->tooltip = NULL;
2077}
2078
2079static void
2080tooltip_func(struct task *task, uint32_t events)
2081{
2082 struct tooltip *tooltip =
2083 container_of(task, struct tooltip, tooltip_task);
2084 uint64_t exp;
2085
Martin Olsson8df662a2012-07-08 03:03:47 +02002086 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2087 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002088 window_create_tooltip(tooltip);
2089}
2090
2091#define TOOLTIP_TIMEOUT 500
2092static int
2093tooltip_timer_reset(struct tooltip *tooltip)
2094{
2095 struct itimerspec its;
2096
2097 its.it_interval.tv_sec = 0;
2098 its.it_interval.tv_nsec = 0;
2099 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2100 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2101 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2102 fprintf(stderr, "could not set timerfd\n: %m");
2103 return -1;
2104 }
2105
2106 return 0;
2107}
2108
2109int
2110widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2111{
2112 struct tooltip *tooltip = parent->tooltip;
2113
2114 parent->tooltip_count++;
2115 if (tooltip) {
2116 tooltip->x = x;
2117 tooltip->y = y;
2118 tooltip_timer_reset(tooltip);
2119 return 0;
2120 }
2121
2122 /* the handler might be triggered too fast via input device motion, so
2123 * we need this check here to make sure tooltip is fully initialized */
2124 if (parent->tooltip_count > 1)
2125 return 0;
2126
2127 tooltip = malloc(sizeof *tooltip);
2128 if (!tooltip)
2129 return -1;
2130
2131 parent->tooltip = tooltip;
2132 tooltip->parent = parent;
2133 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002134 tooltip->x = x;
2135 tooltip->y = y;
2136 tooltip->entry = strdup(entry);
2137 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2138 if (tooltip->tooltip_fd < 0) {
2139 fprintf(stderr, "could not create timerfd\n: %m");
2140 return -1;
2141 }
2142
2143 tooltip->tooltip_task.run = tooltip_func;
2144 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2145 EPOLLIN, &tooltip->tooltip_task);
2146 tooltip_timer_reset(tooltip);
2147
2148 return 0;
2149}
2150
2151static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002152frame_resize_handler(struct widget *widget,
2153 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002154{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002155 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002156 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002157 struct rectangle interior;
2158 struct rectangle input;
2159 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002160
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002161 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002162 interior.x = 0;
2163 interior.y = 0;
2164 interior.width = width;
2165 interior.height = height;
2166 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002167 frame_resize(frame->frame, width, height);
2168 frame_interior(frame->frame, &interior.x, &interior.y,
2169 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002170 }
2171
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 widget_set_allocation(child, interior.x, interior.y,
2173 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002175 if (child->resize_handler) {
2176 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177 child->user_data);
2178
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002179 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002180 width = child->allocation.width;
2181 height = child->allocation.height;
2182 } else {
2183 frame_resize_inside(frame->frame,
2184 child->allocation.width,
2185 child->allocation.height);
2186 width = frame_width(frame->frame);
2187 height = frame_height(frame->frame);
2188 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002189 }
2190
Scott Moreauf7e498c2012-05-14 11:39:29 -06002191 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002192
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 widget->surface->input_region =
2194 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002195 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002196 frame_input_rect(frame->frame, &input.x, &input.y,
2197 &input.width, &input.height);
2198 wl_region_add(widget->surface->input_region,
2199 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002200 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002201 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002202 }
2203
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002204 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002205
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002206 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002207 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002208 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2209 &opaque.width, &opaque.height);
2210
2211 wl_region_add(widget->surface->opaque_region,
2212 opaque.x, opaque.y,
2213 opaque.width, opaque.height);
2214 } else {
2215 wl_region_add(widget->surface->opaque_region,
2216 0, 0, width, height);
2217 }
Martin Minarik1998b152012-05-10 02:04:35 +02002218 }
2219
Martin Minarik1998b152012-05-10 02:04:35 +02002220
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002221 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222}
2223
2224static void
2225frame_redraw_handler(struct widget *widget, void *data)
2226{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002227 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002228 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002229 struct window *window = widget->window;
2230
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002231 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002232 return;
2233
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002234 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002235
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002236 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002237
2238 cairo_destroy(cr);
2239}
2240
2241static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002242frame_get_pointer_image_for_location(struct window_frame *frame,
2243 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002244{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002245 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002246
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002247 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002248 return CURSOR_LEFT_PTR;
2249
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002250 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002251 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002252 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002253 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002254 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002255 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002256 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002257 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002258 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002259 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002260 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002261 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002262 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002263 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002264 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002265 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002266 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002267 case THEME_LOCATION_EXTERIOR:
2268 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002269 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002270 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002271 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002272}
2273
Pekka Paalanen26237862014-09-10 15:10:30 +03002274static void
2275frame_menu_func(void *data, struct input *input, int index)
2276{
2277 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002278
2279 switch (index) {
2280 case 0: /* close */
2281 window_close(window);
2282 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002283 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002284 /* we don't have a way to get out of fullscreen for now */
2285 if (window->fullscreen_handler)
2286 window->fullscreen_handler(window, window->user_data);
2287 break;
2288 }
2289}
2290
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002291void
2292window_show_frame_menu(struct window *window,
2293 struct input *input, uint32_t time)
2294{
2295 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002296 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002297
Pekka Paalanen26237862014-09-10 15:10:30 +03002298 static const char *entries[] = {
2299 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002300 "Fullscreen"
2301 };
2302
2303 if (window->fullscreen_handler)
2304 count = ARRAY_LENGTH(entries);
2305 else
2306 count = ARRAY_LENGTH(entries) - 1;
2307
2308 input_get_position(input, &x, &y);
2309 window_show_menu(window->display, input, time, window,
2310 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002311}
2312
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002313static int
2314frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002315 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002316{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002317 struct window_frame *frame = data;
2318 enum theme_location location;
2319
2320 location = frame_pointer_enter(frame->frame, input, x, y);
2321 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2322 widget_schedule_redraw(frame->widget);
2323
2324 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002325}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002326
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002327static int
2328frame_motion_handler(struct widget *widget,
2329 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002330 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002331{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002332 struct window_frame *frame = data;
2333 enum theme_location location;
2334
2335 location = frame_pointer_motion(frame->frame, input, x, y);
2336 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2337 widget_schedule_redraw(frame->widget);
2338
2339 return frame_get_pointer_image_for_location(data, location);
2340}
2341
2342static void
2343frame_leave_handler(struct widget *widget,
2344 struct input *input, void *data)
2345{
2346 struct window_frame *frame = data;
2347
2348 frame_pointer_leave(frame->frame, input);
2349 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2350 widget_schedule_redraw(frame->widget);
2351}
2352
2353static void
2354frame_handle_status(struct window_frame *frame, struct input *input,
2355 uint32_t time, enum theme_location location)
2356{
2357 struct window *window = frame->widget->window;
2358 uint32_t status;
2359
2360 status = frame_status(frame->frame);
2361 if (status & FRAME_STATUS_REPAINT)
2362 widget_schedule_redraw(frame->widget);
2363
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002364 if (status & FRAME_STATUS_MINIMIZE) {
2365 window_set_minimized(window);
2366 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2367 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002368
2369 if (status & FRAME_STATUS_MENU) {
2370 window_show_frame_menu(window, input, time);
2371 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2372 }
2373
2374 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002375 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002376 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2377 }
2378
2379 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002380 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002381 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002382 }
2383
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002384 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002385 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002386 xdg_surface_move(window->xdg_surface,
2387 input_get_seat(input),
2388 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002389
2390 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2391 }
2392
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002393 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002394 input_ungrab(input);
2395
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002396 xdg_surface_resize(window->xdg_surface,
2397 input_get_seat(input),
2398 window->display->serial,
2399 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002400
2401 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2402 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002403}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002404
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002405#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002406static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002407frame_button_handler(struct widget *widget,
2408 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002409 uint32_t button, enum wl_pointer_button_state state,
2410 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002411
2412{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002413 struct window_frame *frame = data;
2414 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002415
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002416 frame->double_click = 0;
2417 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2418 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2419 frame->double_click = 1;
2420 frame->did_double = 1;
2421 } else
2422 frame->did_double = 0;
2423
2424 frame->last_time = time;
2425 } else if (frame->did_double == 1) {
2426 frame->double_click = 1;
2427 frame->did_double = 0;
2428 }
2429
2430 if (frame->double_click)
2431 location = frame_double_click(frame->frame, input,
2432 button, state);
2433 else
2434 location = frame_pointer_button(frame->frame, input,
2435 button, state);
2436
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002437 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002438}
2439
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002440static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002441frame_touch_down_handler(struct widget *widget, struct input *input,
2442 uint32_t serial, uint32_t time, int32_t id,
2443 float x, float y, void *data)
2444{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002445 struct window_frame *frame = data;
2446
Xiong Zhang382de462014-06-12 11:06:26 +08002447 frame->double_click = 0;
2448 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2449 frame->last_id == id) {
2450 frame->double_click = 1;
2451 frame->did_double = 1;
2452 frame->double_id = id;
2453 } else
2454 frame->did_double = 0;
2455
2456 frame->last_time = time;
2457 frame->last_id = id;
2458
2459 if (frame->double_click)
2460 frame_double_touch_down(frame->frame, input, id, x, y);
2461 else
2462 frame_touch_down(frame->frame, input, id, x, y);
2463
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002464 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2465}
2466
2467static void
2468frame_touch_up_handler(struct widget *widget,
2469 struct input *input, uint32_t serial, uint32_t time,
2470 int32_t id, void *data)
2471{
2472 struct window_frame *frame = data;
2473
Xiong Zhang382de462014-06-12 11:06:26 +08002474 if (frame->double_id == id && frame->did_double) {
2475 frame->did_double = 0;
2476 frame->double_id = 0;
2477 frame_double_touch_up(frame->frame, input, id);
2478 } else
2479 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002480 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002481}
2482
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002483struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002484window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002485{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002486 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002487 uint32_t buttons;
2488
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002489 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002490 buttons = FRAME_BUTTON_NONE;
2491 } else {
2492 buttons = FRAME_BUTTON_ALL;
2493 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002494
Peter Huttererf3d62272013-08-08 11:57:05 +10002495 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002496 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002497 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002498
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002499 frame->widget = window_add_widget(window, frame);
2500 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002501
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002502 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2503 widget_set_resize_handler(frame->widget, frame_resize_handler);
2504 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002505 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002506 widget_set_motion_handler(frame->widget, frame_motion_handler);
2507 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002508 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002509 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002510
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002511 window->frame = frame;
2512
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002513 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002514}
2515
Kristian Høgsberga1627922012-06-20 17:30:03 -04002516void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002517window_frame_set_child_size(struct widget *widget, int child_width,
2518 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002519{
2520 struct display *display = widget->window->display;
2521 struct theme *t = display->theme;
2522 int decoration_width, decoration_height;
2523 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002524 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002525
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002526 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002527 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002528 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002529 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002530
2531 width = child_width + decoration_width;
2532 height = child_height + decoration_height;
2533 } else {
2534 width = child_width;
2535 height = child_height;
2536 }
2537
2538 window_schedule_resize(widget->window, width, height);
2539}
2540
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002541static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002542window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002543{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002544 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002545
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002546 /* frame->child must be destroyed by the application */
2547 widget_destroy(frame->widget);
2548 free(frame);
2549}
2550
2551static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002552input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002553 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002554{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002555 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002556 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002557
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002558 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002559 return;
2560
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002561 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002562 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002563 widget = old;
2564 if (input->grab)
2565 widget = input->grab;
2566 if (widget->leave_handler)
2567 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002568 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002569 }
2570
2571 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002572 widget = focus;
2573 if (input->grab)
2574 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002575 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002576 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002577 cursor = widget->enter_handler(focus, input, x, y,
2578 widget->user_data);
2579 else
2580 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002581
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002582 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002583 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002584}
2585
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002586void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002587touch_grab(struct input *input, int32_t touch_id)
2588{
2589 input->touch_grab = 1;
2590 input->touch_grab_id = touch_id;
2591}
2592
2593void
2594touch_ungrab(struct input *input)
2595{
2596 struct touch_point *tp, *tmp;
2597
2598 input->touch_grab = 0;
2599
2600 wl_list_for_each_safe(tp, tmp,
2601 &input->touch_point_list, link) {
2602 if (tp->id != input->touch_grab_id)
2603 continue;
2604 wl_list_remove(&tp->link);
2605 free(tp);
2606
2607 return;
2608 }
2609}
2610
2611void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002612input_grab(struct input *input, struct widget *widget, uint32_t button)
2613{
2614 input->grab = widget;
2615 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002616
2617 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002618}
2619
2620void
2621input_ungrab(struct input *input)
2622{
2623 struct widget *widget;
2624
2625 input->grab = NULL;
2626 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002627 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002628 input->sx, input->sy);
2629 input_set_focus_widget(input, widget, input->sx, input->sy);
2630 }
2631}
2632
2633static void
Derek Foreman118a4292015-04-22 17:23:35 -05002634cursor_delay_timer_reset(struct input *input, uint32_t duration)
2635{
2636 struct itimerspec its;
2637
2638 if (!duration)
2639 input->cursor_timer_running = false;
2640 else
2641 input->cursor_timer_running = true;
2642
2643 its.it_interval.tv_sec = 0;
2644 its.it_interval.tv_nsec = 0;
2645 its.it_value.tv_sec = duration / 1000;
2646 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2647 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2648 fprintf(stderr, "could not set cursor timerfd\n: %m");
2649}
2650
2651static void cancel_pointer_image_update(struct input *input)
2652{
2653 if (input->cursor_timer_running)
2654 cursor_delay_timer_reset(input, 0);
2655}
2656
2657static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002658input_remove_pointer_focus(struct input *input)
2659{
2660 struct window *window = input->pointer_focus;
2661
2662 if (!window)
2663 return;
2664
2665 input_set_focus_widget(input, NULL, 0, 0);
2666
2667 input->pointer_focus = NULL;
2668 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002669 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002670}
2671
2672static void
2673pointer_handle_enter(void *data, struct wl_pointer *pointer,
2674 uint32_t serial, struct wl_surface *surface,
2675 wl_fixed_t sx_w, wl_fixed_t sy_w)
2676{
2677 struct input *input = data;
2678 struct window *window;
2679 struct widget *widget;
2680 float sx = wl_fixed_to_double(sx_w);
2681 float sy = wl_fixed_to_double(sy_w);
2682
2683 if (!surface) {
2684 /* enter event for a window we've just destroyed */
2685 return;
2686 }
2687
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002688 window = wl_surface_get_user_data(surface);
2689 if (surface != window->main_surface->surface) {
2690 DBG("Ignoring input event from subsurface %p\n", surface);
2691 return;
2692 }
2693
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002694 input->display->serial = serial;
2695 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002696 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002697
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002698 input->sx = sx;
2699 input->sy = sy;
2700
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002701 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002702 input_set_focus_widget(input, widget, sx, sy);
2703}
2704
2705static void
2706pointer_handle_leave(void *data, struct wl_pointer *pointer,
2707 uint32_t serial, struct wl_surface *surface)
2708{
2709 struct input *input = data;
2710
2711 input->display->serial = serial;
2712 input_remove_pointer_focus(input);
2713}
2714
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002715static void
Daniel Stone37816df2012-05-16 18:45:18 +01002716pointer_handle_motion(void *data, struct wl_pointer *pointer,
2717 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002718{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002719 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002720 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002721 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002722 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002723 float sx = wl_fixed_to_double(sx_w);
2724 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002725
Paul Winwoodb22bf572013-08-29 10:52:54 +01002726 if (!window)
2727 return;
2728
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002729 input->sx = sx;
2730 input->sy = sy;
2731
Rob Bradford5f087742013-07-11 19:41:27 +01002732 /* when making the window smaller - e.g. after a unmaximise we might
2733 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002734 * based on the old surface dimensions. However, if we have an active
2735 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002736 */
Derek Foreman46812b62015-08-26 17:13:27 -05002737 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002738 sy < window->main_surface->allocation.y ||
2739 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002740 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002741 return;
2742
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002743 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002744 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002745 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002746 }
2747
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002748 if (input->grab)
2749 widget = input->grab;
2750 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002751 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002752 if (widget) {
2753 if (widget->motion_handler)
2754 cursor = widget->motion_handler(input->focus_widget,
2755 input, time, sx, sy,
2756 widget->user_data);
2757 else
2758 cursor = widget->default_cursor;
2759 } else
2760 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002761
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002762 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002763}
2764
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002765static void
Daniel Stone37816df2012-05-16 18:45:18 +01002766pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002767 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002768{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002769 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002770 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002771 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002772
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002773 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002774 if (input->focus_widget && input->grab == NULL &&
2775 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002776 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002777
Neil Roberts6b28aad2012-01-23 19:11:18 +00002778 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002779 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002780 (*widget->button_handler)(widget,
2781 input, time,
2782 button, state,
2783 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002784
Daniel Stone4dbadb12012-05-30 16:31:51 +01002785 if (input->grab && input->grab_button == button &&
2786 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002787 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002788}
2789
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002790static void
Daniel Stone37816df2012-05-16 18:45:18 +01002791pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002792 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002793{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002794 struct input *input = data;
2795 struct widget *widget;
2796
2797 widget = input->focus_widget;
2798 if (input->grab)
2799 widget = input->grab;
2800 if (widget && widget->axis_handler)
2801 (*widget->axis_handler)(widget,
2802 input, time,
2803 axis, value,
2804 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002805}
2806
Peter Hutterer87743e92016-01-18 16:38:22 +10002807static void
2808pointer_handle_frame(void *data, struct wl_pointer *pointer)
2809{
2810 struct input *input = data;
2811 struct widget *widget;
2812
2813 widget = input->focus_widget;
2814 if (input->grab)
2815 widget = input->grab;
2816 if (widget && widget->pointer_frame_handler)
2817 (*widget->pointer_frame_handler)(widget,
2818 input,
2819 widget->user_data);
2820}
2821
2822static void
2823pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2824 uint32_t source)
2825{
2826 struct input *input = data;
2827 struct widget *widget;
2828
2829 widget = input->focus_widget;
2830 if (input->grab)
2831 widget = input->grab;
2832 if (widget && widget->axis_source_handler)
2833 (*widget->axis_source_handler)(widget,
2834 input,
2835 source,
2836 widget->user_data);
2837}
2838
2839static void
2840pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2841 uint32_t time, uint32_t axis)
2842{
2843 struct input *input = data;
2844 struct widget *widget;
2845
2846 widget = input->focus_widget;
2847 if (input->grab)
2848 widget = input->grab;
2849 if (widget && widget->axis_stop_handler)
2850 (*widget->axis_stop_handler)(widget,
2851 input, time,
2852 axis,
2853 widget->user_data);
2854}
2855
2856static void
2857pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2858 uint32_t axis, int32_t discrete)
2859{
2860 struct input *input = data;
2861 struct widget *widget;
2862
2863 widget = input->focus_widget;
2864 if (input->grab)
2865 widget = input->grab;
2866 if (widget && widget->axis_discrete_handler)
2867 (*widget->axis_discrete_handler)(widget,
2868 input,
2869 axis,
2870 discrete,
2871 widget->user_data);
2872}
2873
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002874static const struct wl_pointer_listener pointer_listener = {
2875 pointer_handle_enter,
2876 pointer_handle_leave,
2877 pointer_handle_motion,
2878 pointer_handle_button,
2879 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002880 pointer_handle_frame,
2881 pointer_handle_axis_source,
2882 pointer_handle_axis_stop,
2883 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002884};
2885
2886static void
2887input_remove_keyboard_focus(struct input *input)
2888{
2889 struct window *window = input->keyboard_focus;
2890 struct itimerspec its;
2891
2892 its.it_interval.tv_sec = 0;
2893 its.it_interval.tv_nsec = 0;
2894 its.it_value.tv_sec = 0;
2895 its.it_value.tv_nsec = 0;
2896 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2897
2898 if (!window)
2899 return;
2900
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002901 if (window->keyboard_focus_handler)
2902 (*window->keyboard_focus_handler)(window, NULL,
2903 window->user_data);
2904
2905 input->keyboard_focus = NULL;
2906}
2907
2908static void
2909keyboard_repeat_func(struct task *task, uint32_t events)
2910{
2911 struct input *input =
2912 container_of(task, struct input, repeat_task);
2913 struct window *window = input->keyboard_focus;
2914 uint64_t exp;
2915
2916 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2917 /* If we change the timer between the fd becoming
2918 * readable and getting here, there'll be nothing to
2919 * read and we get EAGAIN. */
2920 return;
2921
2922 if (window && window->key_handler) {
2923 (*window->key_handler)(window, input, input->repeat_time,
2924 input->repeat_key, input->repeat_sym,
2925 WL_KEYBOARD_KEY_STATE_PRESSED,
2926 window->user_data);
2927 }
2928}
2929
2930static void
2931keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2932 uint32_t format, int fd, uint32_t size)
2933{
2934 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002935 struct xkb_keymap *keymap;
2936 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002937 char *map_str;
2938
2939 if (!data) {
2940 close(fd);
2941 return;
2942 }
2943
2944 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2945 close(fd);
2946 return;
2947 }
2948
2949 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2950 if (map_str == MAP_FAILED) {
2951 close(fd);
2952 return;
2953 }
2954
Ran Benita2e1968f2014-08-19 23:59:51 +03002955 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2956 map_str,
2957 XKB_KEYMAP_FORMAT_TEXT_V1,
2958 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002959 munmap(map_str, size);
2960 close(fd);
2961
Rui Matos3eccb862013-10-10 19:44:22 +02002962 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002963 fprintf(stderr, "failed to compile keymap\n");
2964 return;
2965 }
2966
Rui Matos3eccb862013-10-10 19:44:22 +02002967 state = xkb_state_new(keymap);
2968 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002969 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002970 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002971 return;
2972 }
2973
Rui Matos3eccb862013-10-10 19:44:22 +02002974 xkb_keymap_unref(input->xkb.keymap);
2975 xkb_state_unref(input->xkb.state);
2976 input->xkb.keymap = keymap;
2977 input->xkb.state = state;
2978
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002979 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002980 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002981 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002982 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002983 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002984 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002985}
2986
2987static void
2988keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2989 uint32_t serial, struct wl_surface *surface,
2990 struct wl_array *keys)
2991{
2992 struct input *input = data;
2993 struct window *window;
2994
2995 input->display->serial = serial;
2996 input->keyboard_focus = wl_surface_get_user_data(surface);
2997
2998 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002999 if (window->keyboard_focus_handler)
3000 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003001 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003002}
3003
3004static void
3005keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3006 uint32_t serial, struct wl_surface *surface)
3007{
3008 struct input *input = data;
3009
3010 input->display->serial = serial;
3011 input_remove_keyboard_focus(input);
3012}
3013
Scott Moreau210d0792012-03-22 10:47:01 -06003014static void
Daniel Stone37816df2012-05-16 18:45:18 +01003015keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003016 uint32_t serial, uint32_t time, uint32_t key,
3017 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003018{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003019 struct input *input = data;
3020 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003021 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003022 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003023 const xkb_keysym_t *syms;
3024 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003025 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003026
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003027 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003028 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003029 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003030 return;
3031
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003032 /* We only use input grabs for pointer events for now, so just
3033 * ignore key presses if a grab is active. We expand the key
3034 * event delivery mechanism to route events to widgets to
3035 * properly handle key grabs. In the meantime, this prevents
3036 * key event devlivery while a grab is active. */
3037 if (input->grab && input->grab_button == 0)
3038 return;
3039
Ran Benita2e1968f2014-08-19 23:59:51 +03003040 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003041
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003042 sym = XKB_KEY_NoSymbol;
3043 if (num_syms == 1)
3044 sym = syms[0];
3045
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003046
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003047 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003048 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003049 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003050 } else if (sym == XKB_KEY_F11 &&
3051 window->fullscreen_handler &&
3052 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3053 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003054 } else if (sym == XKB_KEY_F4 &&
3055 input->modifiers == MOD_ALT_MASK &&
3056 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003057 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003058 } else if (window->key_handler) {
3059 (*window->key_handler)(window, input, time, key,
3060 sym, state, window->user_data);
3061 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003062
3063 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3064 key == input->repeat_key) {
3065 its.it_interval.tv_sec = 0;
3066 its.it_interval.tv_nsec = 0;
3067 its.it_value.tv_sec = 0;
3068 its.it_value.tv_nsec = 0;
3069 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003070 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3071 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003072 input->repeat_sym = sym;
3073 input->repeat_key = key;
3074 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003075 its.it_interval.tv_sec = input->repeat_rate_sec;
3076 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3077 its.it_value.tv_sec = input->repeat_delay_sec;
3078 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003079 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3080 }
3081}
3082
3083static void
Daniel Stone351eb612012-05-31 15:27:47 -04003084keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3085 uint32_t serial, uint32_t mods_depressed,
3086 uint32_t mods_latched, uint32_t mods_locked,
3087 uint32_t group)
3088{
3089 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003090 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003091
Matt Ropere61561f2013-06-24 16:52:43 +01003092 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3093 if (!input->xkb.keymap)
3094 return;
3095
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003096 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3097 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003098 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003099 XKB_STATE_MODS_DEPRESSED |
3100 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003101 input->modifiers = 0;
3102 if (mask & input->xkb.control_mask)
3103 input->modifiers |= MOD_CONTROL_MASK;
3104 if (mask & input->xkb.alt_mask)
3105 input->modifiers |= MOD_ALT_MASK;
3106 if (mask & input->xkb.shift_mask)
3107 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003108}
3109
Jonny Lamb06959082014-08-12 14:58:27 +02003110static void
3111set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3112{
3113 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3114 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3115
3116 /* a rate of zero disables any repeating, regardless of the delay's
3117 * value */
3118 if (rate == 0)
3119 return;
3120
3121 if (rate == 1)
3122 input->repeat_rate_sec = 1;
3123 else
3124 input->repeat_rate_nsec = 1000000000 / rate;
3125
3126 input->repeat_delay_sec = delay / 1000;
3127 delay -= (input->repeat_delay_sec * 1000);
3128 input->repeat_delay_nsec = delay * 1000 * 1000;
3129}
3130
3131static void
3132keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3133 int32_t rate, int32_t delay)
3134{
3135 struct input *input = data;
3136
3137 set_repeat_info(input, rate, delay);
3138}
3139
Daniel Stone37816df2012-05-16 18:45:18 +01003140static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003141 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003142 keyboard_handle_enter,
3143 keyboard_handle_leave,
3144 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003145 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003146 keyboard_handle_repeat_info
3147
Daniel Stone37816df2012-05-16 18:45:18 +01003148};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003149
3150static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003151touch_handle_down(void *data, struct wl_touch *wl_touch,
3152 uint32_t serial, uint32_t time, struct wl_surface *surface,
3153 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3154{
3155 struct input *input = data;
3156 struct widget *widget;
3157 float sx = wl_fixed_to_double(x_w);
3158 float sy = wl_fixed_to_double(y_w);
3159
Rusty Lynch1084da52013-08-15 09:10:08 -07003160 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003161 input->touch_focus = wl_surface_get_user_data(surface);
3162 if (!input->touch_focus) {
3163 DBG("Failed to find to touch focus for surface %p\n", surface);
3164 return;
3165 }
3166
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003167 if (surface != input->touch_focus->main_surface->surface) {
3168 DBG("Ignoring input event from subsurface %p\n", surface);
3169 input->touch_focus = NULL;
3170 return;
3171 }
3172
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003173 if (input->grab)
3174 widget = input->grab;
3175 else
3176 widget = window_find_widget(input->touch_focus,
3177 wl_fixed_to_double(x_w),
3178 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003179 if (widget) {
3180 struct touch_point *tp = xmalloc(sizeof *tp);
3181 if (tp) {
3182 tp->id = id;
3183 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003184 tp->x = sx;
3185 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003186 wl_list_insert(&input->touch_point_list, &tp->link);
3187
3188 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003189 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003190 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003191 sx, sy,
3192 widget->user_data);
3193 }
3194 }
3195}
3196
3197static void
3198touch_handle_up(void *data, struct wl_touch *wl_touch,
3199 uint32_t serial, uint32_t time, int32_t id)
3200{
3201 struct input *input = data;
3202 struct touch_point *tp, *tmp;
3203
Rusty Lynch041815a2013-08-08 21:20:38 -07003204 if (!input->touch_focus) {
3205 DBG("No touch focus found for touch up event!\n");
3206 return;
3207 }
3208
3209 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3210 if (tp->id != id)
3211 continue;
3212
3213 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003214 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003215 time, id,
3216 tp->widget->user_data);
3217
3218 wl_list_remove(&tp->link);
3219 free(tp);
3220
3221 return;
3222 }
3223}
3224
3225static void
3226touch_handle_motion(void *data, struct wl_touch *wl_touch,
3227 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3228{
3229 struct input *input = data;
3230 struct touch_point *tp;
3231 float sx = wl_fixed_to_double(x_w);
3232 float sy = wl_fixed_to_double(y_w);
3233
3234 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3235
3236 if (!input->touch_focus) {
3237 DBG("No touch focus found for touch motion event!\n");
3238 return;
3239 }
3240
3241 wl_list_for_each(tp, &input->touch_point_list, link) {
3242 if (tp->id != id)
3243 continue;
3244
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003245 tp->x = sx;
3246 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003247 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003248 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003249 id, sx, sy,
3250 tp->widget->user_data);
3251 return;
3252 }
3253}
3254
3255static void
3256touch_handle_frame(void *data, struct wl_touch *wl_touch)
3257{
3258 struct input *input = data;
3259 struct touch_point *tp, *tmp;
3260
3261 DBG("touch_handle_frame\n");
3262
3263 if (!input->touch_focus) {
3264 DBG("No touch focus found for touch frame event!\n");
3265 return;
3266 }
3267
3268 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3269 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003270 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003271 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003272 }
3273}
3274
3275static void
3276touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3277{
3278 struct input *input = data;
3279 struct touch_point *tp, *tmp;
3280
3281 DBG("touch_handle_cancel\n");
3282
3283 if (!input->touch_focus) {
3284 DBG("No touch focus found for touch cancel event!\n");
3285 return;
3286 }
3287
3288 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3289 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003290 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3291 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003292
3293 wl_list_remove(&tp->link);
3294 free(tp);
3295 }
3296}
3297
3298static const struct wl_touch_listener touch_listener = {
3299 touch_handle_down,
3300 touch_handle_up,
3301 touch_handle_motion,
3302 touch_handle_frame,
3303 touch_handle_cancel,
3304};
3305
3306static void
Daniel Stone37816df2012-05-16 18:45:18 +01003307seat_handle_capabilities(void *data, struct wl_seat *seat,
3308 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003309{
Daniel Stone37816df2012-05-16 18:45:18 +01003310 struct input *input = data;
3311
3312 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3313 input->pointer = wl_seat_get_pointer(seat);
3314 wl_pointer_set_user_data(input->pointer, input);
3315 wl_pointer_add_listener(input->pointer, &pointer_listener,
3316 input);
3317 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003318 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003319 wl_pointer_release(input->pointer);
3320 else
3321 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003322 input->pointer = NULL;
3323 }
3324
3325 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3326 input->keyboard = wl_seat_get_keyboard(seat);
3327 wl_keyboard_set_user_data(input->keyboard, input);
3328 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3329 input);
3330 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003331 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003332 wl_keyboard_release(input->keyboard);
3333 else
3334 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003335 input->keyboard = NULL;
3336 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003337
3338 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3339 input->touch = wl_seat_get_touch(seat);
3340 wl_touch_set_user_data(input->touch, input);
3341 wl_touch_add_listener(input->touch, &touch_listener, input);
3342 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003343 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003344 wl_touch_release(input->touch);
3345 else
3346 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003347 input->touch = NULL;
3348 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003349}
3350
Rob Bradford08031182013-08-13 20:11:03 +01003351static void
3352seat_handle_name(void *data, struct wl_seat *seat,
3353 const char *name)
3354{
3355
3356}
3357
Daniel Stone37816df2012-05-16 18:45:18 +01003358static const struct wl_seat_listener seat_listener = {
3359 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003360 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003361};
3362
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003363void
3364input_get_position(struct input *input, int32_t *x, int32_t *y)
3365{
3366 *x = input->sx;
3367 *y = input->sy;
3368}
3369
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003370int
3371input_get_touch(struct input *input, int32_t id, float *x, float *y)
3372{
3373 struct touch_point *tp;
3374
3375 wl_list_for_each(tp, &input->touch_point_list, link) {
3376 if (tp->id != id)
3377 continue;
3378
3379 *x = tp->x;
3380 *y = tp->y;
3381 return 0;
3382 }
3383
3384 return -1;
3385}
3386
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003387struct display *
3388input_get_display(struct input *input)
3389{
3390 return input->display;
3391}
3392
Daniel Stone37816df2012-05-16 18:45:18 +01003393struct wl_seat *
3394input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003395{
Daniel Stone37816df2012-05-16 18:45:18 +01003396 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003397}
3398
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003399uint32_t
3400input_get_modifiers(struct input *input)
3401{
3402 return input->modifiers;
3403}
3404
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003405struct widget *
3406input_get_focus_widget(struct input *input)
3407{
3408 return input->focus_widget;
3409}
3410
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003411struct data_offer {
3412 struct wl_data_offer *offer;
3413 struct input *input;
3414 struct wl_array types;
3415 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003416
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003417 struct task io_task;
3418 int fd;
3419 data_func_t func;
3420 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003421 uint32_t dnd_action;
3422 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003423 void *user_data;
3424};
3425
3426static void
3427data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3428{
3429 struct data_offer *offer = data;
3430 char **p;
3431
3432 p = wl_array_add(&offer->types, sizeof *p);
3433 *p = strdup(type);
3434}
3435
Carlos Garnacho9c931792016-01-18 23:52:12 +01003436static void
3437data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3438{
3439 struct data_offer *offer = data;
3440
3441 offer->source_actions = source_actions;
3442}
3443
3444static void
3445data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3446{
3447 struct data_offer *offer = data;
3448
3449 offer->dnd_action = dnd_action;
3450}
3451
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003452static const struct wl_data_offer_listener data_offer_listener = {
3453 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003454 data_offer_source_actions,
3455 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003456};
3457
3458static void
3459data_offer_destroy(struct data_offer *offer)
3460{
3461 char **p;
3462
3463 offer->refcount--;
3464 if (offer->refcount == 0) {
3465 wl_data_offer_destroy(offer->offer);
3466 for (p = offer->types.data; *p; p++)
3467 free(*p);
3468 wl_array_release(&offer->types);
3469 free(offer);
3470 }
3471}
3472
3473static void
3474data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003475 struct wl_data_device *data_device,
3476 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003477{
3478 struct data_offer *offer;
3479
Brian Lovinbc919262013-08-07 15:34:59 -07003480 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003481
3482 wl_array_init(&offer->types);
3483 offer->refcount = 1;
3484 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003485 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003486 wl_data_offer_add_listener(offer->offer,
3487 &data_offer_listener, offer);
3488}
3489
3490static void
3491data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003492 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003493 wl_fixed_t x_w, wl_fixed_t y_w,
3494 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003495{
3496 struct input *input = data;
3497 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003498 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003499 float x = wl_fixed_to_double(x_w);
3500 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003501 char **p;
3502
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003503 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003504 input->drag_enter_serial = serial;
3505 input->drag_focus = window,
3506 input->drag_x = x;
3507 input->drag_y = y;
3508
3509 if (!input->touch_grab)
3510 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003511
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003512 if (offer) {
3513 input->drag_offer = wl_data_offer_get_user_data(offer);
3514
3515 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3516 *p = NULL;
3517
3518 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003519
3520 if (input->display->data_device_manager_version >=
3521 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3522 wl_data_offer_set_actions(offer,
3523 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3524 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3525 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3526 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003527 } else {
3528 input->drag_offer = NULL;
3529 types_data = NULL;
3530 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003531
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003532 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003533 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003534 window->user_data);
3535}
3536
3537static void
3538data_device_leave(void *data, struct wl_data_device *data_device)
3539{
3540 struct input *input = data;
3541
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003542 if (input->drag_offer) {
3543 data_offer_destroy(input->drag_offer);
3544 input->drag_offer = NULL;
3545 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003546}
3547
3548static void
3549data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003550 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003551{
3552 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003553 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003554 float x = wl_fixed_to_double(x_w);
3555 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003556 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003557
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003558 input->drag_x = x;
3559 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003560
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003561 if (input->drag_offer)
3562 types_data = input->drag_offer->types.data;
3563 else
3564 types_data = NULL;
3565
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003566 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003567 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003568 window->user_data);
3569}
3570
3571static void
3572data_device_drop(void *data, struct wl_data_device *data_device)
3573{
3574 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003575 struct window *window = input->drag_focus;
3576 float x, y;
3577
3578 x = input->drag_x;
3579 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003580
3581 if (window->drop_handler)
3582 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003583 x, y, window->user_data);
3584
3585 if (input->touch_grab)
3586 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003587}
3588
3589static void
3590data_device_selection(void *data,
3591 struct wl_data_device *wl_data_device,
3592 struct wl_data_offer *offer)
3593{
3594 struct input *input = data;
3595 char **p;
3596
3597 if (input->selection_offer)
3598 data_offer_destroy(input->selection_offer);
3599
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003600 if (offer) {
3601 input->selection_offer = wl_data_offer_get_user_data(offer);
3602 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3603 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003604 } else {
3605 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003606 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003607}
3608
3609static const struct wl_data_device_listener data_device_listener = {
3610 data_device_data_offer,
3611 data_device_enter,
3612 data_device_leave,
3613 data_device_motion,
3614 data_device_drop,
3615 data_device_selection
3616};
3617
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003618static void
3619input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003620{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003621 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003622 struct wl_cursor *cursor;
3623 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003624
Daniel Stone80972742012-11-07 17:51:39 +11003625 if (!input->pointer)
3626 return;
3627
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003628 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003629 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003630 return;
3631
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003632 if (index >= (int) cursor->image_count) {
3633 fprintf(stderr, "cursor index out of range\n");
3634 return;
3635 }
3636
3637 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003638 buffer = wl_cursor_image_get_buffer(image);
3639 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003640 return;
3641
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003642 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3643 wl_surface_damage(input->pointer_surface, 0, 0,
3644 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003645 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003646 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3647 input->pointer_surface,
3648 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003649}
3650
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003651static const struct wl_callback_listener pointer_surface_listener;
3652
Derek Foreman493d9792015-03-04 16:26:25 -06003653static bool
3654input_set_pointer_special(struct input *input)
3655{
3656 if (input->current_cursor == CURSOR_BLANK) {
3657 wl_pointer_set_cursor(input->pointer,
3658 input->pointer_enter_serial,
3659 NULL, 0, 0);
3660 return true;
3661 }
3662
3663 if (input->current_cursor == CURSOR_UNSET)
3664 return true;
3665
3666 return false;
3667}
3668
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003669static void
Derek Foreman118a4292015-04-22 17:23:35 -05003670schedule_pointer_image_update(struct input *input,
3671 struct wl_cursor *cursor,
3672 uint32_t duration,
3673 bool force_frame)
3674{
3675 /* Some silly cursor sets have enormous pauses in them. In these
3676 * cases it's better to use a timer even if it results in less
3677 * accurate presentation, since it will save us having to set the
3678 * same cursor image over and over again.
3679 *
3680 * This is really not the way we're supposed to time any kind of
3681 * animation, but we're pretending it's OK here because we don't
3682 * want animated cursors with long delays to needlessly hog CPU.
3683 *
3684 * We use force_frame to ensure we don't accumulate large timing
3685 * errors by running off the wrong clock.
3686 */
3687 if (!force_frame && duration > 100) {
3688 struct timespec tp;
3689
3690 clock_gettime(CLOCK_MONOTONIC, &tp);
3691 input->cursor_timer_start = tp.tv_sec * 1000
3692 + tp.tv_nsec / 1000000;
3693 cursor_delay_timer_reset(input, duration);
3694 return;
3695 }
3696
3697 /* for short durations we'll just spin on frame callbacks for
3698 * accurate timing - the way any kind of timing sensitive animation
3699 * should really be done. */
3700 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3701 wl_callback_add_listener(input->cursor_frame_cb,
3702 &pointer_surface_listener, input);
3703
3704}
3705
3706static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003707pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3708 uint32_t time)
3709{
3710 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003711 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003712 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003713 uint32_t duration;
3714 bool force_frame = true;
3715
3716 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003717
3718 if (callback) {
3719 assert(callback == input->cursor_frame_cb);
3720 wl_callback_destroy(callback);
3721 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003722 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003723 }
3724
Daniel Stone80972742012-11-07 17:51:39 +11003725 if (!input->pointer)
3726 return;
3727
Derek Foreman493d9792015-03-04 16:26:25 -06003728 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003729 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003730
Daniel Stonea494f1d2012-06-18 19:31:12 +01003731 cursor = input->display->cursors[input->current_cursor];
3732 if (!cursor)
3733 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003734
3735 /* FIXME We don't have the current time on the first call so we set
3736 * the animation start to the time of the first frame callback. */
3737 if (time == 0)
3738 input->cursor_anim_start = 0;
3739 else if (input->cursor_anim_start == 0)
3740 input->cursor_anim_start = time;
3741
Derek Foreman118a4292015-04-22 17:23:35 -05003742 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003743
Derek Foreman118a4292015-04-22 17:23:35 -05003744 if (time == 0 || input->cursor_anim_start == 0) {
3745 duration = 0;
3746 i = 0;
3747 } else
3748 i = wl_cursor_frame_and_duration(
3749 cursor,
3750 time - input->cursor_anim_start,
3751 &duration);
3752
3753 if (cursor->image_count > 1)
3754 schedule_pointer_image_update(input, cursor, duration,
3755 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003756
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003757 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003758}
3759
Derek Foreman118a4292015-04-22 17:23:35 -05003760static void
3761cursor_timer_func(struct task *task, uint32_t events)
3762{
3763 struct input *input = container_of(task, struct input, cursor_task);
3764 struct timespec tp;
3765 struct wl_cursor *cursor;
3766 uint32_t time;
3767 uint64_t exp;
3768
3769 if (!input->cursor_timer_running)
3770 return;
3771
3772 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3773 return;
3774
3775 cursor = input->display->cursors[input->current_cursor];
3776 if (!cursor)
3777 return;
3778
3779 clock_gettime(CLOCK_MONOTONIC, &tp);
3780 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3781 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3782}
3783
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003784static const struct wl_callback_listener pointer_surface_listener = {
3785 pointer_surface_frame_callback
3786};
3787
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003788void
3789input_set_pointer_image(struct input *input, int pointer)
3790{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003791 int force = 0;
3792
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003793 if (!input->pointer)
3794 return;
3795
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003796 if (input->pointer_enter_serial > input->cursor_serial)
3797 force = 1;
3798
3799 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003800 return;
3801
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003802 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003803 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003804 if (!input->cursor_frame_cb)
3805 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003806 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003807 /* The current frame callback may be stuck if, for instance,
3808 * the set cursor request was processed by the server after
3809 * this client lost the focus. In this case the cursor surface
3810 * might not be mapped and the frame callback wouldn't ever
3811 * complete. Send a set_cursor and attach to try to map the
3812 * cursor surface again so that the callback will finish */
3813 input_set_pointer_image_index(input, 0);
3814 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003815}
3816
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003817struct wl_data_device *
3818input_get_data_device(struct input *input)
3819{
3820 return input->data_device;
3821}
3822
3823void
3824input_set_selection(struct input *input,
3825 struct wl_data_source *source, uint32_t time)
3826{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003827 if (input->data_device)
3828 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003829}
3830
3831void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003832input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003833{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003834 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003835 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003836}
3837
3838static void
3839offer_io_func(struct task *task, uint32_t events)
3840{
3841 struct data_offer *offer =
3842 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003843 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003844 unsigned int len;
3845 char buffer[4096];
3846
3847 len = read(offer->fd, buffer, sizeof buffer);
3848 offer->func(buffer, len,
3849 offer->x, offer->y, offer->user_data);
3850
3851 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003852 if (display->data_device_manager_version >=
3853 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3854 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003855 close(offer->fd);
3856 data_offer_destroy(offer);
3857 }
3858}
3859
3860static void
3861data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3862 data_func_t func, void *user_data)
3863{
3864 int p[2];
3865
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003866 if (pipe2(p, O_CLOEXEC) == -1)
3867 return;
3868
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003869 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3870 close(p[1]);
3871
3872 offer->io_task.run = offer_io_func;
3873 offer->fd = p[0];
3874 offer->func = func;
3875 offer->refcount++;
3876 offer->user_data = user_data;
3877
3878 display_watch_fd(offer->input->display,
3879 offer->fd, EPOLLIN, &offer->io_task);
3880}
3881
3882void
3883input_receive_drag_data(struct input *input, const char *mime_type,
3884 data_func_t func, void *data)
3885{
3886 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003887 input->drag_offer->x = input->drag_x;
3888 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003889}
3890
3891int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003892input_receive_drag_data_to_fd(struct input *input,
3893 const char *mime_type, int fd)
3894{
3895 if (input->drag_offer)
3896 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3897
3898 return 0;
3899}
3900
3901int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003902input_receive_selection_data(struct input *input, const char *mime_type,
3903 data_func_t func, void *data)
3904{
3905 char **p;
3906
3907 if (input->selection_offer == NULL)
3908 return -1;
3909
3910 for (p = input->selection_offer->types.data; *p; p++)
3911 if (strcmp(mime_type, *p) == 0)
3912 break;
3913
3914 if (*p == NULL)
3915 return -1;
3916
3917 data_offer_receive_data(input->selection_offer,
3918 mime_type, func, data);
3919 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003920}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003921
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003922int
3923input_receive_selection_data_to_fd(struct input *input,
3924 const char *mime_type, int fd)
3925{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003926 if (input->selection_offer)
3927 wl_data_offer_receive(input->selection_offer->offer,
3928 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003929
3930 return 0;
3931}
3932
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003933void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003934window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003935{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003936 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003937 return;
3938
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003939 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003940}
3941
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003942static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003943surface_set_synchronized(struct surface *surface)
3944{
3945 if (!surface->subsurface)
3946 return;
3947
3948 if (surface->synchronized)
3949 return;
3950
3951 wl_subsurface_set_sync(surface->subsurface);
3952 surface->synchronized = 1;
3953}
3954
3955static void
3956surface_set_synchronized_default(struct surface *surface)
3957{
3958 if (!surface->subsurface)
3959 return;
3960
3961 if (surface->synchronized == surface->synchronized_default)
3962 return;
3963
3964 if (surface->synchronized_default)
3965 wl_subsurface_set_sync(surface->subsurface);
3966 else
3967 wl_subsurface_set_desync(surface->subsurface);
3968
3969 surface->synchronized = surface->synchronized_default;
3970}
3971
3972static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003973surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003974{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003975 struct widget *widget = surface->widget;
3976 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003977
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003978 if (surface->input_region) {
3979 wl_region_destroy(surface->input_region);
3980 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003981 }
3982
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003983 if (surface->opaque_region)
3984 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003985
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003986 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003987
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003988 if (widget->resize_handler)
3989 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003990 widget->allocation.width,
3991 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003992 widget->user_data);
3993
Pekka Paalanen35e82632013-04-25 13:57:48 +03003994 if (surface->subsurface &&
3995 (surface->allocation.x != widget->allocation.x ||
3996 surface->allocation.y != widget->allocation.y)) {
3997 wl_subsurface_set_position(surface->subsurface,
3998 widget->allocation.x,
3999 widget->allocation.y);
4000 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004001 if (surface->allocation.width != widget->allocation.width ||
4002 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004003 window_schedule_redraw(widget->window);
4004 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004005 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004006
4007 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004008 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004009 widget->allocation.width,
4010 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004011}
4012
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004013static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004014window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004015{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004016 struct surface *surface;
4017
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004018 widget_set_allocation(window->main_surface->widget,
4019 window->pending_allocation.x,
4020 window->pending_allocation.y,
4021 window->pending_allocation.width,
4022 window->pending_allocation.height);
4023
4024 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004025
4026 /* The main surface is in the list, too. Main surface's
4027 * resize_handler is responsible for calling widget_set_allocation()
4028 * on all sub-surface root widgets, so they will be resized
4029 * properly.
4030 */
4031 wl_list_for_each(surface, &window->subsurface_list, link) {
4032 if (surface == window->main_surface)
4033 continue;
4034
4035 surface_set_synchronized(surface);
4036 surface_resize(surface);
4037 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004038
4039 if (!window->fullscreen && !window->maximized)
4040 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004041}
4042
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004043static void
4044idle_resize(struct window *window)
4045{
4046 window->resize_needed = 0;
4047 window->redraw_needed = 1;
4048
4049 DBG("from %dx%d to %dx%d\n",
4050 window->main_surface->server_allocation.width,
4051 window->main_surface->server_allocation.height,
4052 window->pending_allocation.width,
4053 window->pending_allocation.height);
4054
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004055 window_do_resize(window);
4056}
4057
4058static void
4059undo_resize(struct window *window)
4060{
4061 window->pending_allocation.width =
4062 window->main_surface->server_allocation.width;
4063 window->pending_allocation.height =
4064 window->main_surface->server_allocation.height;
4065
4066 DBG("back to %dx%d\n",
4067 window->main_surface->server_allocation.width,
4068 window->main_surface->server_allocation.height);
4069
4070 window_do_resize(window);
4071
4072 if (window->pending_allocation.width == 0 &&
4073 window->pending_allocation.height == 0) {
4074 fprintf(stderr, "Error: Could not draw a surface, "
4075 "most likely due to insufficient disk space in "
4076 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4077 exit(EXIT_FAILURE);
4078 }
4079}
4080
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004081void
4082window_schedule_resize(struct window *window, int width, int height)
4083{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004084 /* We should probably get these numbers from the theme. */
4085 const int min_width = 200, min_height = 200;
4086
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004087 window->pending_allocation.x = 0;
4088 window->pending_allocation.y = 0;
4089 window->pending_allocation.width = width;
4090 window->pending_allocation.height = height;
4091
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004092 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004093 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004094 window->min_allocation.width = min_width;
4095 else
4096 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004097 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004098 window->min_allocation.height = min_height;
4099 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004100 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004101 }
4102
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004103 if (window->pending_allocation.width < window->min_allocation.width)
4104 window->pending_allocation.width = window->min_allocation.width;
4105 if (window->pending_allocation.height < window->min_allocation.height)
4106 window->pending_allocation.height = window->min_allocation.height;
4107
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004108 window->resize_needed = 1;
4109 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004110}
4111
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004112void
4113widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4114{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004115 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004116}
4117
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004118static int
4119window_get_shadow_margin(struct window *window)
4120{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004121 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004122 return frame_get_shadow_margin(window->frame->frame);
4123 else
4124 return 0;
4125}
4126
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004127static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004128handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004129 int32_t width, int32_t height,
4130 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004131{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004132 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004133 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004134
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004135 window->maximized = 0;
4136 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004137 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004138 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004139
4140 wl_array_for_each(p, states) {
4141 uint32_t state = *p;
4142 switch (state) {
4143 case XDG_SURFACE_STATE_MAXIMIZED:
4144 window->maximized = 1;
4145 break;
4146 case XDG_SURFACE_STATE_FULLSCREEN:
4147 window->fullscreen = 1;
4148 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004149 case XDG_SURFACE_STATE_RESIZING:
4150 window->resizing = 1;
4151 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004152 case XDG_SURFACE_STATE_ACTIVATED:
4153 window->focused = 1;
4154 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004155 default:
4156 /* Unknown state */
4157 break;
4158 }
4159 }
4160
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004161 if (window->frame) {
4162 if (window->maximized) {
4163 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4164 } else {
4165 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4166 }
4167
4168 if (window->focused) {
4169 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4170 } else {
4171 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4172 }
4173 }
4174
Jasper St. Pierref184c382014-05-06 08:33:27 -04004175 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004176 /* The width / height params are for window geometry,
4177 * but window_schedule_resize takes allocation. Add
4178 * on the shadow margin to get the difference. */
4179 int margin = window_get_shadow_margin(window);
4180
4181 window_schedule_resize(window,
4182 width + margin * 2,
4183 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004184 } else {
4185 window_schedule_resize(window,
4186 window->saved_allocation.width,
4187 window->saved_allocation.height);
4188 }
4189
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004190 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004191
4192 if (window->state_changed_handler)
4193 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004194}
4195
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004196static void
4197handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4198{
4199 struct window *window = data;
4200 window_close(window);
4201}
4202
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004203static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004204 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004205 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004206};
4207
4208static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004209window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004210{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004211 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004212
4213 if (!window->xdg_surface)
4214 return;
4215
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004216 if (window->parent == window->last_parent)
4217 return;
4218
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004219 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004220 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004221 else
4222 parent_surface = NULL;
4223
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004224 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004225 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004226}
4227
4228static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004229window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004230{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004231 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004232 frame_input_rect(window->frame->frame,
4233 &geometry->x,
4234 &geometry->y,
4235 &geometry->width,
4236 &geometry->height);
4237 else
4238 window_get_allocation(window, geometry);
4239}
4240
4241static void
4242window_sync_geometry(struct window *window)
4243{
4244 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004245
4246 if (!window->xdg_surface)
4247 return;
4248
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004249 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004250 if (geometry.x == window->last_geometry.x &&
4251 geometry.y == window->last_geometry.y &&
4252 geometry.width == window->last_geometry.width &&
4253 geometry.height == window->last_geometry.height)
4254 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004255
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004256 xdg_surface_set_window_geometry(window->xdg_surface,
4257 geometry.x,
4258 geometry.y,
4259 geometry.width,
4260 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004261 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004262}
4263
4264static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004265window_flush(struct window *window)
4266{
4267 struct surface *surface;
4268
4269 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004270 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004271 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004272 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004273 }
4274 }
4275
4276 wl_list_for_each(surface, &window->subsurface_list, link) {
4277 if (surface == window->main_surface)
4278 continue;
4279
4280 surface_flush(surface);
4281 }
4282
4283 surface_flush(window->main_surface);
4284}
4285
4286static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004287menu_destroy(struct menu *menu)
4288{
4289 widget_destroy(menu->widget);
4290 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004291 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004292 free(menu);
4293}
4294
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004295void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004296window_get_allocation(struct window *window,
4297 struct rectangle *allocation)
4298{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004299 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004300}
4301
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004302static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004303widget_redraw(struct widget *widget)
4304{
4305 struct widget *child;
4306
4307 if (widget->redraw_handler)
4308 widget->redraw_handler(widget, widget->user_data);
4309 wl_list_for_each(child, &widget->child_list, link)
4310 widget_redraw(child);
4311}
4312
4313static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004314frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4315{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004316 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004317
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004318 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004319 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004320 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004321 surface->frame_cb = NULL;
4322
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004323 surface->last_time = time;
4324
Pekka Paalanen71233882013-04-25 13:57:53 +03004325 if (surface->redraw_needed || surface->window->redraw_needed) {
4326 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004327 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004328 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004329}
4330
4331static const struct wl_callback_listener listener = {
4332 frame_callback
4333};
4334
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004335static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004336surface_redraw(struct surface *surface)
4337{
Pekka Paalanen71233882013-04-25 13:57:53 +03004338 DBG_OBJ(surface->surface, "begin\n");
4339
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004340 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004341 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004342
4343 /* Whole-window redraw forces a redraw even if the previous has
4344 * not yet hit the screen.
4345 */
4346 if (surface->frame_cb) {
4347 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004348 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004349
Pekka Paalanen71233882013-04-25 13:57:53 +03004350 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004351 wl_callback_destroy(surface->frame_cb);
4352 }
4353
Neil Roberts97b747c2013-12-19 16:17:12 +00004354 if (surface->widget->use_cairo &&
4355 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004356 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004357 return -1;
4358 }
4359
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004360 surface->frame_cb = wl_surface_frame(surface->surface);
4361 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004362 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004363
4364 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004365 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004366 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004367 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004368 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004369}
4370
4371static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004372idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004373{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004374 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004375 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004376 int failed = 0;
4377 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004378
Pekka Paalanen71233882013-04-25 13:57:53 +03004379 DBG(" --------- \n");
4380
Pekka Paalaneneebff542013-04-25 13:57:52 +03004381 wl_list_init(&window->redraw_task.link);
4382 window->redraw_task_scheduled = 0;
4383
4384 if (window->resize_needed) {
4385 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004386 if (window->main_surface->frame_cb) {
4387 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004388 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004389 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004390
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004391 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004392 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004393 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004394
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004395 if (surface_redraw(window->main_surface) < 0) {
4396 /*
4397 * Only main_surface failure will cause us to undo the resize.
4398 * If sub-surfaces fail, they will just be broken with old
4399 * content.
4400 */
4401 failed = 1;
4402 } else {
4403 wl_list_for_each(surface, &window->subsurface_list, link) {
4404 if (surface == window->main_surface)
4405 continue;
4406
4407 surface_redraw(surface);
4408 }
4409 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004410
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004411 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004412 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004413
4414 wl_list_for_each(surface, &window->subsurface_list, link)
4415 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004416
4417 if (resized && failed) {
4418 /* Restore widget tree to correspond to what is on screen. */
4419 undo_resize(window);
4420 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004421}
4422
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004423static void
4424window_schedule_redraw_task(struct window *window)
4425{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004426 if (!window->redraw_task_scheduled) {
4427 window->redraw_task.run = idle_redraw;
4428 display_defer(window->display, &window->redraw_task);
4429 window->redraw_task_scheduled = 1;
4430 }
4431}
4432
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004433void
4434window_schedule_redraw(struct window *window)
4435{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004436 struct surface *surface;
4437
Pekka Paalanen71233882013-04-25 13:57:53 +03004438 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4439
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004440 wl_list_for_each(surface, &window->subsurface_list, link)
4441 surface->redraw_needed = 1;
4442
4443 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004444}
4445
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004446int
4447window_is_fullscreen(struct window *window)
4448{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004449 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004450}
4451
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004452void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004453window_set_fullscreen(struct window *window, int fullscreen)
4454{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004455 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004456 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004457
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004458 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004459 return;
4460
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004461 if (fullscreen)
4462 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4463 else
4464 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004465}
4466
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004467int
4468window_is_maximized(struct window *window)
4469{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004470 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004471}
4472
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004473void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004474window_set_maximized(struct window *window, int maximized)
4475{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004476 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004477 return;
4478
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004479 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004480 return;
4481
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004482 if (maximized)
4483 xdg_surface_set_maximized(window->xdg_surface);
4484 else
4485 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004486}
4487
Jasper St. Pierrede680992014-04-10 17:23:49 -07004488int
4489window_is_resizing(struct window *window)
4490{
4491 return window->resizing;
4492}
4493
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004494void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004495window_set_minimized(struct window *window)
4496{
4497 if (!window->xdg_surface)
4498 return;
4499
4500 xdg_surface_set_minimized(window->xdg_surface);
4501}
4502
4503void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004504window_set_user_data(struct window *window, void *data)
4505{
4506 window->user_data = data;
4507}
4508
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004509void *
4510window_get_user_data(struct window *window)
4511{
4512 return window->user_data;
4513}
4514
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004515void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004516window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004517 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004518{
4519 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004520}
4521
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004522void
4523window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004524 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004525{
4526 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004527}
4528
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004529void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004530window_set_data_handler(struct window *window, window_data_handler_t handler)
4531{
4532 window->data_handler = handler;
4533}
4534
4535void
4536window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4537{
4538 window->drop_handler = handler;
4539}
4540
4541void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004542window_set_close_handler(struct window *window,
4543 window_close_handler_t handler)
4544{
4545 window->close_handler = handler;
4546}
4547
4548void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004549window_set_fullscreen_handler(struct window *window,
4550 window_fullscreen_handler_t handler)
4551{
4552 window->fullscreen_handler = handler;
4553}
4554
4555void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004556window_set_output_handler(struct window *window,
4557 window_output_handler_t handler)
4558{
4559 window->output_handler = handler;
4560}
4561
4562void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004563window_set_state_changed_handler(struct window *window,
4564 window_state_changed_handler_t handler)
4565{
4566 window->state_changed_handler = handler;
4567}
4568
4569void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004570window_set_title(struct window *window, const char *title)
4571{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004572 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004573 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004574 if (window->frame) {
4575 frame_set_title(window->frame->frame, title);
4576 widget_schedule_redraw(window->frame->widget);
4577 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004578 if (window->xdg_surface)
4579 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004580}
4581
4582const char *
4583window_get_title(struct window *window)
4584{
4585 return window->title;
4586}
4587
4588void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004589window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4590{
4591 struct text_cursor_position *text_cursor_position =
4592 window->display->text_cursor_position;
4593
Scott Moreau9295ce02012-06-01 12:46:10 -06004594 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004595 return;
4596
4597 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004598 window->main_surface->surface,
4599 wl_fixed_from_int(x),
4600 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004601}
4602
Casey Dahlin9074db52012-04-19 22:50:09 -04004603static void
4604surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004605 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004606{
Rob Bradford7507b572012-05-15 17:55:34 +01004607 struct window *window = data;
4608 struct output *output;
4609 struct output *output_found = NULL;
4610 struct window_output *window_output;
4611
4612 wl_list_for_each(output, &window->display->output_list, link) {
4613 if (output->output == wl_output) {
4614 output_found = output;
4615 break;
4616 }
4617 }
4618
4619 if (!output_found)
4620 return;
4621
Brian Lovinbc919262013-08-07 15:34:59 -07004622 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004623 window_output->output = output_found;
4624
4625 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004626
4627 if (window->output_handler)
4628 window->output_handler(window, output_found, 1,
4629 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004630}
4631
4632static void
4633surface_leave(void *data,
4634 struct wl_surface *wl_surface, struct wl_output *output)
4635{
Rob Bradford7507b572012-05-15 17:55:34 +01004636 struct window *window = data;
4637 struct window_output *window_output;
4638 struct window_output *window_output_found = NULL;
4639
4640 wl_list_for_each(window_output, &window->window_output_list, link) {
4641 if (window_output->output->output == output) {
4642 window_output_found = window_output;
4643 break;
4644 }
4645 }
4646
4647 if (window_output_found) {
4648 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004649
4650 if (window->output_handler)
4651 window->output_handler(window, window_output->output,
4652 0, window->user_data);
4653
Rob Bradford7507b572012-05-15 17:55:34 +01004654 free(window_output_found);
4655 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004656}
4657
4658static const struct wl_surface_listener surface_listener = {
4659 surface_enter,
4660 surface_leave
4661};
4662
Pekka Paalanen4e373742013-02-13 16:17:13 +02004663static struct surface *
4664surface_create(struct window *window)
4665{
4666 struct display *display = window->display;
4667 struct surface *surface;
4668
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004669 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004670 surface->window = window;
4671 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004672 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004673 wl_surface_add_listener(surface->surface, &surface_listener, window);
4674
Pekka Paalanen35e82632013-04-25 13:57:48 +03004675 wl_list_insert(&window->subsurface_list, &surface->link);
4676
Pekka Paalanen4e373742013-02-13 16:17:13 +02004677 return surface;
4678}
4679
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004680static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004681get_preferred_buffer_type(struct display *display)
4682{
4683#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004684 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004685 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4686#endif
4687
4688 return WINDOW_BUFFER_TYPE_SHM;
4689}
4690
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004691static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004692window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004693{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004694 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004695 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004696
Peter Huttererf3d62272013-08-08 11:57:05 +10004697 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004698 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004699 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004700
4701 surface = surface_create(window);
4702 window->main_surface = surface;
4703
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004704 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004705
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004706 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004707 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004708
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004709 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004710
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004711 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004712 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004713 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004714
Rob Bradford7507b572012-05-15 17:55:34 +01004715 wl_list_init (&window->window_output_list);
4716
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004717 return window;
4718}
4719
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004720struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004721window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004722{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004723 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004724 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004725
4726 window = window_create_internal(display, 0);
4727
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004728 if (window->display->xdg_shell) {
4729 window->xdg_surface =
4730 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4731 window->main_surface->surface);
4732 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004733
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004734 xdg_surface_set_user_data(window->xdg_surface, window);
4735 xdg_surface_add_listener(window->xdg_surface,
4736 &xdg_surface_listener, window);
4737 } else if (display->ivi_application) {
4738 /* auto generation of ivi_id based on process id + basement of id */
4739 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4740 window->ivi_surface =
4741 ivi_application_surface_create(display->ivi_application,
4742 id_ivisurf, window->main_surface->surface);
4743 fail_on_null(window->ivi_surface);
4744
4745 ivi_surface_add_listener(window->ivi_surface,
4746 &ivi_surface_listener, window);
4747 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004748
4749 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004750}
4751
4752struct window *
4753window_create_custom(struct display *display)
4754{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004755 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004756}
4757
Jasper St. Pierre53686042013-12-09 15:26:25 -05004758void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004759window_set_parent(struct window *window,
4760 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004761{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004762 window->parent = parent_window;
4763 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004764}
4765
4766struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004767window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004768{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004769 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004770}
4771
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004772static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004773menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004774{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004775 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004776 int next;
4777
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004778 frame_interior(menu->frame, &x, &y, &width, &height);
4779 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004780 if (menu->current != next) {
4781 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004782 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004783 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004784}
4785
4786static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004787menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004788 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004789 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004790{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004791 struct menu *menu = data;
4792
4793 if (widget == menu->widget)
4794 menu_set_item(data, y);
4795
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004796 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004797}
4798
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004799static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004800menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004801 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004802{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004803 struct menu *menu = data;
4804
4805 if (widget == menu->widget)
4806 menu_set_item(data, y);
4807
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004808 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004809}
4810
4811static void
4812menu_leave_handler(struct widget *widget, struct input *input, void *data)
4813{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004814 struct menu *menu = data;
4815
4816 if (widget == menu->widget)
4817 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004818}
4819
4820static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004821menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004822 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004823 uint32_t button, enum wl_pointer_button_state state,
4824 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004825
4826{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004827 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004828
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004829 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4830 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004831 /* Either relase after press-drag-release or
4832 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004833 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05004834 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004835 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004836 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004837 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004838 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004839}
4840
4841static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004842menu_touch_up_handler(struct widget *widget,
4843 struct input *input,
4844 uint32_t serial,
4845 uint32_t time,
4846 int32_t id,
4847 void *data)
4848{
4849 struct menu *menu = data;
4850
4851 input_ungrab(input);
4852 menu_destroy(menu);
4853}
4854
4855static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004856menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004857{
4858 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004859 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004860 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004861
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004862 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004863
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004864 frame_repaint(menu->frame, cr);
4865 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004866
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004867 theme_set_background_source(menu->window->display->theme,
4868 cr, THEME_FRAME_ACTIVE);
4869 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004870 cairo_fill(cr);
4871
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004872 cairo_select_font_face(cr, "sans",
4873 CAIRO_FONT_SLANT_NORMAL,
4874 CAIRO_FONT_WEIGHT_NORMAL);
4875 cairo_set_font_size(cr, 12);
4876
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004877 for (i = 0; i < menu->count; i++) {
4878 if (i == menu->current) {
4879 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004880 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004881 cairo_fill(cr);
4882 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004883 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004884 cairo_show_text(cr, menu->entries[i]);
4885 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004886 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4887 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004888 cairo_show_text(cr, menu->entries[i]);
4889 }
4890 }
4891
4892 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004893}
4894
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004895static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004896handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004897{
4898 struct window *window = data;
4899 struct menu *menu = window->main_surface->widget->user_data;
4900
4901 input_ungrab(menu->input);
4902 menu_destroy(menu);
4903}
4904
4905static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004906 handle_popup_popup_done,
4907};
4908
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004909static struct menu *
4910create_menu(struct display *display,
4911 struct input *input, uint32_t time,
4912 menu_func_t func, const char **entries, int count,
4913 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004914{
4915 struct window *window;
4916 struct menu *menu;
4917
4918 menu = malloc(sizeof *menu);
4919 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004920 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004921
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004922 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004923 if (!window) {
4924 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004925 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004926 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004927
4928 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004929 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004930 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004931 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004932 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004933 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004934 menu->entries = entries;
4935 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004936 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004937 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004938 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004939 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004940 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004941
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004942 input_ungrab(input);
4943
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004944 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004945 widget_set_enter_handler(menu->widget, menu_enter_handler);
4946 widget_set_leave_handler(menu->widget, menu_leave_handler);
4947 widget_set_motion_handler(menu->widget, menu_motion_handler);
4948 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004949 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004950
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004951 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004952 frame_resize_inside(menu->frame, 200, count * 20);
4953 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4954 window_schedule_resize(window, frame_width(menu->frame),
4955 frame_height(menu->frame));
4956
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004957 return menu;
4958}
4959
4960struct window *
4961window_create_menu(struct display *display,
4962 struct input *input, uint32_t time,
4963 menu_func_t func, const char **entries, int count,
4964 void *user_data)
4965{
4966 struct menu *menu;
4967 menu = create_menu(display, input, time, func, entries, count, user_data);
4968
4969 if (menu == NULL)
4970 return NULL;
4971
4972 return menu->window;
4973}
4974
4975void
4976window_show_menu(struct display *display,
4977 struct input *input, uint32_t time, struct window *parent,
4978 int32_t x, int32_t y,
4979 menu_func_t func, const char **entries, int count)
4980{
4981 struct menu *menu;
4982 struct window *window;
4983 int32_t ix, iy;
4984
4985 menu = create_menu(display, input, time, func, entries, count, parent);
4986
4987 if (menu == NULL)
4988 return;
4989
4990 window = menu->window;
4991
4992 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4993 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4994
4995 window->x = x;
4996 window->y = y;
4997
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004998 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004999
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005000 if (!display->xdg_shell)
5001 return;
5002
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005003 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
5004 window->main_surface->surface,
5005 parent->main_surface->surface,
5006 input->seat,
5007 display_get_serial(window->display),
5008 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08005009 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005010 fail_on_null(window->xdg_popup);
5011
5012 xdg_popup_set_user_data(window->xdg_popup, window);
5013 xdg_popup_add_listener(window->xdg_popup,
5014 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005015}
5016
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005017void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005018window_set_buffer_type(struct window *window, enum window_buffer_type type)
5019{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005020 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005021}
5022
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005023enum window_buffer_type
5024window_get_buffer_type(struct window *window)
5025{
5026 return window->main_surface->buffer_type;
5027}
5028
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005029void
5030window_set_preferred_format(struct window *window,
5031 enum preferred_format format)
5032{
5033 window->preferred_format = format;
5034}
5035
Pekka Paalanen35e82632013-04-25 13:57:48 +03005036struct widget *
5037window_add_subsurface(struct window *window, void *data,
5038 enum subsurface_mode default_mode)
5039{
5040 struct widget *widget;
5041 struct surface *surface;
5042 struct wl_surface *parent;
5043 struct wl_subcompositor *subcompo = window->display->subcompositor;
5044
Pekka Paalanen35e82632013-04-25 13:57:48 +03005045 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005046 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005047 widget = widget_create(window, surface, data);
5048 wl_list_init(&widget->link);
5049 surface->widget = widget;
5050
5051 parent = window->main_surface->surface;
5052 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5053 surface->surface,
5054 parent);
5055 surface->synchronized = 1;
5056
5057 switch (default_mode) {
5058 case SUBSURFACE_SYNCHRONIZED:
5059 surface->synchronized_default = 1;
5060 break;
5061 case SUBSURFACE_DESYNCHRONIZED:
5062 surface->synchronized_default = 0;
5063 break;
5064 default:
5065 assert(!"bad enum subsurface_mode");
5066 }
5067
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005068 window->resize_needed = 1;
5069 window_schedule_redraw(window);
5070
Pekka Paalanen35e82632013-04-25 13:57:48 +03005071 return widget;
5072}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005073
5074static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005075display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005076 struct wl_output *wl_output,
5077 int x, int y,
5078 int physical_width,
5079 int physical_height,
5080 int subpixel,
5081 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005082 const char *model,
5083 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005084{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005085 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005086
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005087 output->allocation.x = x;
5088 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005089 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005090
5091 if (output->make)
5092 free(output->make);
5093 output->make = strdup(make);
5094
5095 if (output->model)
5096 free(output->model);
5097 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005098}
5099
5100static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005101display_handle_done(void *data,
5102 struct wl_output *wl_output)
5103{
5104}
5105
5106static void
5107display_handle_scale(void *data,
5108 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005109 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005110{
5111 struct output *output = data;
5112
5113 output->scale = scale;
5114}
5115
5116static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005117display_handle_mode(void *data,
5118 struct wl_output *wl_output,
5119 uint32_t flags,
5120 int width,
5121 int height,
5122 int refresh)
5123{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005124 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005125 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005126
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005127 if (flags & WL_OUTPUT_MODE_CURRENT) {
5128 output->allocation.width = width;
5129 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005130 if (display->output_configure_handler)
5131 (*display->output_configure_handler)(
5132 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005133 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005134}
5135
5136static const struct wl_output_listener output_listener = {
5137 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005138 display_handle_mode,
5139 display_handle_done,
5140 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005141};
5142
5143static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005144display_add_output(struct display *d, uint32_t id)
5145{
5146 struct output *output;
5147
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005148 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005149 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005150 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005151 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005152 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005153 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005154 wl_list_insert(d->output_list.prev, &output->link);
5155
5156 wl_output_add_listener(output->output, &output_listener, output);
5157}
5158
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005159static void
5160output_destroy(struct output *output)
5161{
5162 if (output->destroy_handler)
5163 (*output->destroy_handler)(output, output->user_data);
5164
5165 wl_output_destroy(output->output);
5166 wl_list_remove(&output->link);
5167 free(output);
5168}
5169
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005170static void
5171display_destroy_output(struct display *d, uint32_t id)
5172{
5173 struct output *output;
5174
5175 wl_list_for_each(output, &d->output_list, link) {
5176 if (output->server_output_id == id) {
5177 output_destroy(output);
5178 break;
5179 }
5180 }
5181}
5182
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005183void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005184display_set_global_handler(struct display *display,
5185 display_global_handler_t handler)
5186{
5187 struct global *global;
5188
5189 display->global_handler = handler;
5190 if (!handler)
5191 return;
5192
5193 wl_list_for_each(global, &display->global_list, link)
5194 display->global_handler(display,
5195 global->name, global->interface,
5196 global->version, display->user_data);
5197}
5198
5199void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005200display_set_global_handler_remove(struct display *display,
5201 display_global_handler_t remove_handler)
5202{
5203 display->global_handler_remove = remove_handler;
5204 if (!remove_handler)
5205 return;
5206}
5207
5208void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005209display_set_output_configure_handler(struct display *display,
5210 display_output_handler_t handler)
5211{
5212 struct output *output;
5213
5214 display->output_configure_handler = handler;
5215 if (!handler)
5216 return;
5217
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005218 wl_list_for_each(output, &display->output_list, link) {
5219 if (output->allocation.width == 0 &&
5220 output->allocation.height == 0)
5221 continue;
5222
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005223 (*display->output_configure_handler)(output,
5224 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005225 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005226}
5227
5228void
5229output_set_user_data(struct output *output, void *data)
5230{
5231 output->user_data = data;
5232}
5233
5234void *
5235output_get_user_data(struct output *output)
5236{
5237 return output->user_data;
5238}
5239
5240void
5241output_set_destroy_handler(struct output *output,
5242 display_output_handler_t handler)
5243{
5244 output->destroy_handler = handler;
5245 /* FIXME: implement this, once we have way to remove outputs */
5246}
5247
5248void
Scott Moreau4e072362012-09-29 02:03:11 -06005249output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005250{
Scott Moreau4e072362012-09-29 02:03:11 -06005251 struct rectangle allocation = output->allocation;
5252
5253 switch (output->transform) {
5254 case WL_OUTPUT_TRANSFORM_90:
5255 case WL_OUTPUT_TRANSFORM_270:
5256 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5257 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5258 /* Swap width and height */
5259 allocation.width = output->allocation.height;
5260 allocation.height = output->allocation.width;
5261 break;
5262 }
5263
5264 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005265}
5266
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005267struct wl_output *
5268output_get_wl_output(struct output *output)
5269{
5270 return output->output;
5271}
5272
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005273enum wl_output_transform
5274output_get_transform(struct output *output)
5275{
5276 return output->transform;
5277}
5278
Alexander Larssonafd319a2013-05-22 14:41:27 +02005279uint32_t
5280output_get_scale(struct output *output)
5281{
5282 return output->scale;
5283}
5284
Jason Ekstrand738715d2014-04-02 19:53:50 -05005285const char *
5286output_get_make(struct output *output)
5287{
5288 return output->make;
5289}
5290
5291const char *
5292output_get_model(struct output *output)
5293{
5294 return output->model;
5295}
5296
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005297static void
Daniel Stone97f68542012-05-30 16:32:01 +01005298fini_xkb(struct input *input)
5299{
5300 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005301 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005302}
5303
5304static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005305display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005306{
5307 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005308 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005309
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005310 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005311 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005312 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005313 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005314 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005315 input->pointer_focus = NULL;
5316 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005317 input->seat_version = seat_version;
5318
Rusty Lynch041815a2013-08-08 21:20:38 -07005319 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005320 wl_list_insert(d->input_list.prev, &input->link);
5321
Daniel Stone37816df2012-05-16 18:45:18 +01005322 wl_seat_add_listener(input->seat, &seat_listener, input);
5323 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005324
Jason Ekstranda669bd52014-04-02 19:53:51 -05005325 if (d->data_device_manager) {
5326 input->data_device =
5327 wl_data_device_manager_get_data_device(d->data_device_manager,
5328 input->seat);
5329 wl_data_device_add_listener(input->data_device,
5330 &data_device_listener,
5331 input);
5332 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005333
5334 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005335 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005336
Derek Foreman118a4292015-04-22 17:23:35 -05005337 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5338 TFD_CLOEXEC | TFD_NONBLOCK);
5339 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5340 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005341 set_repeat_info(input, 40, 400);
5342
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005343 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5344 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005345 input->repeat_task.run = keyboard_repeat_func;
5346 display_watch_fd(d, input->repeat_timer_fd,
5347 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005348}
5349
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005350static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005351input_destroy(struct input *input)
5352{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005353 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005354 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005355
5356 if (input->drag_offer)
5357 data_offer_destroy(input->drag_offer);
5358
5359 if (input->selection_offer)
5360 data_offer_destroy(input->selection_offer);
5361
kabeer khan6ce67ec2014-10-20 11:55:29 +05305362 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005363 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305364 wl_data_device_release(input->data_device);
5365 else
5366 wl_data_device_destroy(input->data_device);
5367 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005368 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005369 if (input->touch)
5370 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005371 if (input->pointer)
5372 wl_pointer_release(input->pointer);
5373 if (input->keyboard)
5374 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005375 } else {
5376 if (input->touch)
5377 wl_touch_destroy(input->touch);
5378 if (input->pointer)
5379 wl_pointer_destroy(input->pointer);
5380 if (input->keyboard)
5381 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005382 }
5383
Daniel Stone97f68542012-05-30 16:32:01 +01005384 fini_xkb(input);
5385
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005386 wl_surface_destroy(input->pointer_surface);
5387
Pekka Paalanene1207c72011-12-16 12:02:09 +02005388 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005389 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005390 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005391 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005392 free(input);
5393}
5394
5395static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005396shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5397{
5398 struct display *d = data;
5399
5400 if (format == WL_SHM_FORMAT_RGB565)
5401 d->has_rgb565 = 1;
5402}
5403
5404struct wl_shm_listener shm_listener = {
5405 shm_format
5406};
5407
5408static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005409xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5410{
5411 xdg_shell_pong(shell, serial);
5412}
5413
5414static const struct xdg_shell_listener xdg_shell_listener = {
5415 xdg_shell_ping,
5416};
5417
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005418#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005419#ifdef static_assert
5420static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5421 "Interface version doesn't match implementation version");
5422#endif
5423
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005424static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005425registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5426 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005427{
5428 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005429 struct global *global;
5430
Brian Lovinbc919262013-08-07 15:34:59 -07005431 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005432 global->name = id;
5433 global->interface = strdup(interface);
5434 global->version = version;
5435 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005436
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005437 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005438 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005439 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005440 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005441 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005442 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005443 display_add_input(d, id, version);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005444 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005445 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005446 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005447 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005448 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005449 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005450 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305451 &wl_data_device_manager_interface,
5452 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005453 } else if (strcmp(interface, "xdg_shell") == 0) {
5454 d->xdg_shell = wl_registry_bind(registry, id,
5455 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005456 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005457 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005458 } else if (strcmp(interface, "text_cursor_position") == 0) {
5459 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005460 wl_registry_bind(registry, id,
5461 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005462 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5463 d->subcompositor =
5464 wl_registry_bind(registry, id,
5465 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005466 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005467 else if (strcmp(interface, "ivi_application") == 0) {
5468 d->ivi_application =
5469 wl_registry_bind(registry, id,
5470 &ivi_application_interface, 1);
5471 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005472
5473 if (d->global_handler)
5474 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005475}
5476
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005477static void
5478registry_handle_global_remove(void *data, struct wl_registry *registry,
5479 uint32_t name)
5480{
5481 struct display *d = data;
5482 struct global *global;
5483 struct global *tmp;
5484
5485 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5486 if (global->name != name)
5487 continue;
5488
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005489 if (strcmp(global->interface, "wl_output") == 0)
5490 display_destroy_output(d, name);
5491
5492 /* XXX: Should destroy remaining bound globals */
5493
5494 if (d->global_handler_remove)
5495 d->global_handler_remove(d, name, global->interface,
5496 global->version, d->user_data);
5497
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005498 wl_list_remove(&global->link);
5499 free(global->interface);
5500 free(global);
5501 }
5502}
5503
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005504void *
5505display_bind(struct display *display, uint32_t name,
5506 const struct wl_interface *interface, uint32_t version)
5507{
5508 return wl_registry_bind(display->registry, name, interface, version);
5509}
5510
5511static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005512 registry_handle_global,
5513 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005514};
5515
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005516#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005517static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005518init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005519{
5520 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005521 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005522
Rob Clark6396ed32012-03-11 19:48:41 -05005523#ifdef USE_CAIRO_GLESV2
5524# define GL_BIT EGL_OPENGL_ES2_BIT
5525#else
5526# define GL_BIT EGL_OPENGL_BIT
5527#endif
5528
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005529 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005530 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005531 EGL_RED_SIZE, 1,
5532 EGL_GREEN_SIZE, 1,
5533 EGL_BLUE_SIZE, 1,
5534 EGL_ALPHA_SIZE, 1,
5535 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005536 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005537 EGL_NONE
5538 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005539
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005540#ifdef USE_CAIRO_GLESV2
5541 static const EGLint context_attribs[] = {
5542 EGL_CONTEXT_CLIENT_VERSION, 2,
5543 EGL_NONE
5544 };
5545 EGLint api = EGL_OPENGL_ES_API;
5546#else
5547 EGLint *context_attribs = NULL;
5548 EGLint api = EGL_OPENGL_API;
5549#endif
5550
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005551 d->dpy =
5552 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5553 d->display, NULL);
5554
Yuval Fledel45568f62010-12-06 09:18:12 -05005555 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005556 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005557 return -1;
5558 }
5559
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005560 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005561 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005562 return -1;
5563 }
5564
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005565 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5566 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005567 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005568 return -1;
5569 }
5570
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005571 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005572 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005573 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005574 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005575 return -1;
5576 }
5577
Benjamin Franzke0c991632011-09-27 21:57:31 +02005578 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5579 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005580 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005581 return -1;
5582 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005583
5584 return 0;
5585}
5586
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005587static void
5588fini_egl(struct display *display)
5589{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005590 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005591
5592 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5593 EGL_NO_CONTEXT);
5594
5595 eglTerminate(display->dpy);
5596 eglReleaseThread();
5597}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005598#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005599
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005600static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005601init_dummy_surface(struct display *display)
5602{
5603 int len;
5604 void *data;
5605
5606 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005607 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005608 display->dummy_surface =
5609 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5610 1, 1, len);
5611 display->dummy_surface_data = data;
5612}
5613
5614static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005615handle_display_data(struct task *task, uint32_t events)
5616{
5617 struct display *display =
5618 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005619 struct epoll_event ep;
5620 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005621
5622 display->display_fd_events = events;
5623
5624 if (events & EPOLLERR || events & EPOLLHUP) {
5625 display_exit(display);
5626 return;
5627 }
5628
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005629 if (events & EPOLLIN) {
5630 ret = wl_display_dispatch(display->display);
5631 if (ret == -1) {
5632 display_exit(display);
5633 return;
5634 }
5635 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005636
5637 if (events & EPOLLOUT) {
5638 ret = wl_display_flush(display->display);
5639 if (ret == 0) {
5640 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5641 ep.data.ptr = &display->display_task;
5642 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5643 display->display_fd, &ep);
5644 } else if (ret == -1 && errno != EAGAIN) {
5645 display_exit(display);
5646 return;
5647 }
5648 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005649}
5650
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005651static void
5652log_handler(const char *format, va_list args)
5653{
5654 vfprintf(stderr, format, args);
5655}
5656
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005657struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005658display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005659{
5660 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005661
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005662 wl_log_set_handler_client(log_handler);
5663
Peter Huttererf3d62272013-08-08 11:57:05 +10005664 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005665 if (d == NULL)
5666 return NULL;
5667
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005668 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005669 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005670 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005671 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005672 return NULL;
5673 }
5674
Rob Bradford5ab9c752013-07-26 16:29:43 +01005675 d->xkb_context = xkb_context_new(0);
5676 if (d->xkb_context == NULL) {
5677 fprintf(stderr, "Failed to create XKB context\n");
5678 free(d);
5679 return NULL;
5680 }
5681
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005682 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005683 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005684 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005685 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5686 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005687
5688 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005689 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005690 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005691 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005692
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005693 d->registry = wl_display_get_registry(d->display);
5694 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005695
Marek Chalupaa519d062014-12-05 13:49:40 +01005696 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005697 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5698 return NULL;
5699 }
5700
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005701#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005702 if (init_egl(d) < 0)
5703 fprintf(stderr, "EGL does not seem to work, "
5704 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005705#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005706
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005707 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005708
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005709 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005710
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005711 wl_list_init(&d->window_list);
5712
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005713 init_dummy_surface(d);
5714
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005715 return d;
5716}
5717
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005718static void
5719display_destroy_outputs(struct display *display)
5720{
5721 struct output *tmp;
5722 struct output *output;
5723
5724 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5725 output_destroy(output);
5726}
5727
Pekka Paalanene1207c72011-12-16 12:02:09 +02005728static void
5729display_destroy_inputs(struct display *display)
5730{
5731 struct input *tmp;
5732 struct input *input;
5733
5734 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5735 input_destroy(input);
5736}
5737
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005738void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005739display_destroy(struct display *display)
5740{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005741 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005742 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5743 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005744
5745 if (!wl_list_empty(&display->deferred_list))
5746 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5747
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005748 cairo_surface_destroy(display->dummy_surface);
5749 free(display->dummy_surface_data);
5750
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005751 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005752 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005753
Daniel Stone97f68542012-05-30 16:32:01 +01005754 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005755
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005756 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005757 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005758
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005759#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005760 if (display->argb_device)
5761 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005762#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005763
Pekka Paalanen35e82632013-04-25 13:57:48 +03005764 if (display->subcompositor)
5765 wl_subcompositor_destroy(display->subcompositor);
5766
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005767 if (display->xdg_shell)
5768 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005769
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005770 if (display->ivi_application)
5771 ivi_application_destroy(display->ivi_application);
5772
Pekka Paalanenc2052982011-12-16 11:41:32 +02005773 if (display->shm)
5774 wl_shm_destroy(display->shm);
5775
5776 if (display->data_device_manager)
5777 wl_data_device_manager_destroy(display->data_device_manager);
5778
5779 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005780 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005781
5782 close(display->epoll_fd);
5783
U. Artie Eoff44874d92012-10-02 21:12:35 -07005784 if (!(display->display_fd_events & EPOLLERR) &&
5785 !(display->display_fd_events & EPOLLHUP))
5786 wl_display_flush(display->display);
5787
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005788 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005789 free(display);
5790}
5791
5792void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005793display_set_user_data(struct display *display, void *data)
5794{
5795 display->user_data = data;
5796}
5797
5798void *
5799display_get_user_data(struct display *display)
5800{
5801 return display->user_data;
5802}
5803
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005804struct wl_display *
5805display_get_display(struct display *display)
5806{
5807 return display->display;
5808}
5809
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005810int
5811display_has_subcompositor(struct display *display)
5812{
5813 if (display->subcompositor)
5814 return 1;
5815
5816 wl_display_roundtrip(display->display);
5817
5818 return display->subcompositor != NULL;
5819}
5820
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005821cairo_device_t *
5822display_get_cairo_device(struct display *display)
5823{
5824 return display->argb_device;
5825}
5826
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005827struct output *
5828display_get_output(struct display *display)
5829{
5830 return container_of(display->output_list.next, struct output, link);
5831}
5832
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005833struct wl_compositor *
5834display_get_compositor(struct display *display)
5835{
5836 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005837}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005838
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005839uint32_t
5840display_get_serial(struct display *display)
5841{
5842 return display->serial;
5843}
5844
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005845EGLDisplay
5846display_get_egl_display(struct display *d)
5847{
5848 return d->dpy;
5849}
5850
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005851struct wl_data_source *
5852display_create_data_source(struct display *display)
5853{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005854 if (display->data_device_manager)
5855 return wl_data_device_manager_create_data_source(display->data_device_manager);
5856 else
5857 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005858}
5859
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005860EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005861display_get_argb_egl_config(struct display *d)
5862{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005863 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005864}
5865
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005866int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005867display_acquire_window_surface(struct display *display,
5868 struct window *window,
5869 EGLContext ctx)
5870{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005871 struct surface *surface = window->main_surface;
5872
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005873 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005874 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005875
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005876 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005877 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005878}
5879
5880void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005881display_release_window_surface(struct display *display,
5882 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005883{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005884 struct surface *surface = window->main_surface;
5885
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005886 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005887 return;
5888
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005889 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005890}
5891
5892void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005893display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005894{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005895 wl_list_insert(&display->deferred_list, &task->link);
5896}
5897
5898void
5899display_watch_fd(struct display *display,
5900 int fd, uint32_t events, struct task *task)
5901{
5902 struct epoll_event ep;
5903
5904 ep.events = events;
5905 ep.data.ptr = task;
5906 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5907}
5908
5909void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005910display_unwatch_fd(struct display *display, int fd)
5911{
5912 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5913}
5914
5915void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005916display_run(struct display *display)
5917{
5918 struct task *task;
5919 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005920 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005921
Pekka Paalanen826d7952011-12-15 10:14:07 +02005922 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005923 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005924 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005925 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005926 struct task, link);
5927 wl_list_remove(&task->link);
5928 task->run(task, 0);
5929 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005930
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005931 wl_display_dispatch_pending(display->display);
5932
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005933 if (!display->running)
5934 break;
5935
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005936 ret = wl_display_flush(display->display);
5937 if (ret < 0 && errno == EAGAIN) {
5938 ep[0].events =
5939 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5940 ep[0].data.ptr = &display->display_task;
5941
5942 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5943 display->display_fd, &ep[0]);
5944 } else if (ret < 0) {
5945 break;
5946 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005947
5948 count = epoll_wait(display->epoll_fd,
5949 ep, ARRAY_LENGTH(ep), -1);
5950 for (i = 0; i < count; i++) {
5951 task = ep[i].data.ptr;
5952 task->run(task, ep[i].events);
5953 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005954 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005955}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005956
5957void
5958display_exit(struct display *display)
5959{
5960 display->running = 0;
5961}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005962
Carlos Garnacho9c931792016-01-18 23:52:12 +01005963int
5964display_get_data_device_manager_version(struct display *display)
5965{
5966 return display->data_device_manager_version;
5967}
5968
Jan Arne Petersencd997062012-11-18 19:06:44 +01005969void
5970keysym_modifiers_add(struct wl_array *modifiers_map,
5971 const char *name)
5972{
5973 size_t len = strlen(name) + 1;
5974 char *p;
5975
5976 p = wl_array_add(modifiers_map, len);
5977
5978 if (p == NULL)
5979 return;
5980
5981 strncpy(p, name, len);
5982}
5983
5984static xkb_mod_index_t
5985keysym_modifiers_get_index(struct wl_array *modifiers_map,
5986 const char *name)
5987{
5988 xkb_mod_index_t index = 0;
5989 char *p = modifiers_map->data;
5990
5991 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5992 if (strcmp(p, name) == 0)
5993 return index;
5994
5995 index++;
5996 p += strlen(p) + 1;
5997 }
5998
5999 return XKB_MOD_INVALID;
6000}
6001
6002xkb_mod_mask_t
6003keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6004 const char *name)
6005{
6006 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6007
6008 if (index == XKB_MOD_INVALID)
6009 return XKB_MOD_INVALID;
6010
6011 return 1 << index;
6012}
Kristian Høgsbergce278412013-07-25 15:20:20 -07006013
6014void *
6015fail_on_null(void *p)
6016{
6017 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10006018 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07006019 exit(EXIT_FAILURE);
6020 }
6021
6022 return p;
6023}
6024
6025void *
6026xmalloc(size_t s)
6027{
6028 return fail_on_null(malloc(s));
6029}
6030
Peter Huttererf3d62272013-08-08 11:57:05 +10006031void *
6032xzalloc(size_t s)
6033{
6034 return fail_on_null(zalloc(s));
6035}
6036
Kristian Høgsbergce278412013-07-25 15:20:20 -07006037char *
6038xstrdup(const char *s)
6039{
6040 return fail_on_null(strdup(s));
6041}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08006042
6043void *
6044xrealloc(char *p, size_t s)
6045{
6046 return fail_on_null(realloc(p, s));
6047}