blob: 7d45acd478e8eb61a4236929d04a9dd4d06e6f89 [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;
3421 void *user_data;
3422};
3423
3424static void
3425data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3426{
3427 struct data_offer *offer = data;
3428 char **p;
3429
3430 p = wl_array_add(&offer->types, sizeof *p);
3431 *p = strdup(type);
3432}
3433
3434static const struct wl_data_offer_listener data_offer_listener = {
3435 data_offer_offer,
3436};
3437
3438static void
3439data_offer_destroy(struct data_offer *offer)
3440{
3441 char **p;
3442
3443 offer->refcount--;
3444 if (offer->refcount == 0) {
3445 wl_data_offer_destroy(offer->offer);
3446 for (p = offer->types.data; *p; p++)
3447 free(*p);
3448 wl_array_release(&offer->types);
3449 free(offer);
3450 }
3451}
3452
3453static void
3454data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003455 struct wl_data_device *data_device,
3456 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003457{
3458 struct data_offer *offer;
3459
Brian Lovinbc919262013-08-07 15:34:59 -07003460 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003461
3462 wl_array_init(&offer->types);
3463 offer->refcount = 1;
3464 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003465 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003466 wl_data_offer_add_listener(offer->offer,
3467 &data_offer_listener, offer);
3468}
3469
3470static void
3471data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003472 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003473 wl_fixed_t x_w, wl_fixed_t y_w,
3474 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003475{
3476 struct input *input = data;
3477 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003478 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003479 float x = wl_fixed_to_double(x_w);
3480 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003481 char **p;
3482
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003483 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003484 input->drag_enter_serial = serial;
3485 input->drag_focus = window,
3486 input->drag_x = x;
3487 input->drag_y = y;
3488
3489 if (!input->touch_grab)
3490 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003491
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003492 if (offer) {
3493 input->drag_offer = wl_data_offer_get_user_data(offer);
3494
3495 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3496 *p = NULL;
3497
3498 types_data = input->drag_offer->types.data;
3499 } else {
3500 input->drag_offer = NULL;
3501 types_data = NULL;
3502 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003503
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003504 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003505 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003506 window->user_data);
3507}
3508
3509static void
3510data_device_leave(void *data, struct wl_data_device *data_device)
3511{
3512 struct input *input = data;
3513
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003514 if (input->drag_offer) {
3515 data_offer_destroy(input->drag_offer);
3516 input->drag_offer = NULL;
3517 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003518}
3519
3520static void
3521data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003522 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003523{
3524 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003525 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003526 float x = wl_fixed_to_double(x_w);
3527 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003528 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003529
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003530 input->drag_x = x;
3531 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003532
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003533 if (input->drag_offer)
3534 types_data = input->drag_offer->types.data;
3535 else
3536 types_data = NULL;
3537
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003538 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003539 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003540 window->user_data);
3541}
3542
3543static void
3544data_device_drop(void *data, struct wl_data_device *data_device)
3545{
3546 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003547 struct window *window = input->drag_focus;
3548 float x, y;
3549
3550 x = input->drag_x;
3551 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003552
3553 if (window->drop_handler)
3554 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003555 x, y, window->user_data);
3556
3557 if (input->touch_grab)
3558 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003559}
3560
3561static void
3562data_device_selection(void *data,
3563 struct wl_data_device *wl_data_device,
3564 struct wl_data_offer *offer)
3565{
3566 struct input *input = data;
3567 char **p;
3568
3569 if (input->selection_offer)
3570 data_offer_destroy(input->selection_offer);
3571
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003572 if (offer) {
3573 input->selection_offer = wl_data_offer_get_user_data(offer);
3574 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3575 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003576 } else {
3577 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003578 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003579}
3580
3581static const struct wl_data_device_listener data_device_listener = {
3582 data_device_data_offer,
3583 data_device_enter,
3584 data_device_leave,
3585 data_device_motion,
3586 data_device_drop,
3587 data_device_selection
3588};
3589
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003590static void
3591input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003592{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003593 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003594 struct wl_cursor *cursor;
3595 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003596
Daniel Stone80972742012-11-07 17:51:39 +11003597 if (!input->pointer)
3598 return;
3599
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003600 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003601 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003602 return;
3603
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003604 if (index >= (int) cursor->image_count) {
3605 fprintf(stderr, "cursor index out of range\n");
3606 return;
3607 }
3608
3609 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003610 buffer = wl_cursor_image_get_buffer(image);
3611 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003612 return;
3613
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003614 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3615 wl_surface_damage(input->pointer_surface, 0, 0,
3616 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003617 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003618 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3619 input->pointer_surface,
3620 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003621}
3622
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003623static const struct wl_callback_listener pointer_surface_listener;
3624
Derek Foreman493d9792015-03-04 16:26:25 -06003625static bool
3626input_set_pointer_special(struct input *input)
3627{
3628 if (input->current_cursor == CURSOR_BLANK) {
3629 wl_pointer_set_cursor(input->pointer,
3630 input->pointer_enter_serial,
3631 NULL, 0, 0);
3632 return true;
3633 }
3634
3635 if (input->current_cursor == CURSOR_UNSET)
3636 return true;
3637
3638 return false;
3639}
3640
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003641static void
Derek Foreman118a4292015-04-22 17:23:35 -05003642schedule_pointer_image_update(struct input *input,
3643 struct wl_cursor *cursor,
3644 uint32_t duration,
3645 bool force_frame)
3646{
3647 /* Some silly cursor sets have enormous pauses in them. In these
3648 * cases it's better to use a timer even if it results in less
3649 * accurate presentation, since it will save us having to set the
3650 * same cursor image over and over again.
3651 *
3652 * This is really not the way we're supposed to time any kind of
3653 * animation, but we're pretending it's OK here because we don't
3654 * want animated cursors with long delays to needlessly hog CPU.
3655 *
3656 * We use force_frame to ensure we don't accumulate large timing
3657 * errors by running off the wrong clock.
3658 */
3659 if (!force_frame && duration > 100) {
3660 struct timespec tp;
3661
3662 clock_gettime(CLOCK_MONOTONIC, &tp);
3663 input->cursor_timer_start = tp.tv_sec * 1000
3664 + tp.tv_nsec / 1000000;
3665 cursor_delay_timer_reset(input, duration);
3666 return;
3667 }
3668
3669 /* for short durations we'll just spin on frame callbacks for
3670 * accurate timing - the way any kind of timing sensitive animation
3671 * should really be done. */
3672 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3673 wl_callback_add_listener(input->cursor_frame_cb,
3674 &pointer_surface_listener, input);
3675
3676}
3677
3678static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003679pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3680 uint32_t time)
3681{
3682 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003683 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003684 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003685 uint32_t duration;
3686 bool force_frame = true;
3687
3688 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003689
3690 if (callback) {
3691 assert(callback == input->cursor_frame_cb);
3692 wl_callback_destroy(callback);
3693 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003694 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003695 }
3696
Daniel Stone80972742012-11-07 17:51:39 +11003697 if (!input->pointer)
3698 return;
3699
Derek Foreman493d9792015-03-04 16:26:25 -06003700 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003701 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003702
Daniel Stonea494f1d2012-06-18 19:31:12 +01003703 cursor = input->display->cursors[input->current_cursor];
3704 if (!cursor)
3705 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003706
3707 /* FIXME We don't have the current time on the first call so we set
3708 * the animation start to the time of the first frame callback. */
3709 if (time == 0)
3710 input->cursor_anim_start = 0;
3711 else if (input->cursor_anim_start == 0)
3712 input->cursor_anim_start = time;
3713
Derek Foreman118a4292015-04-22 17:23:35 -05003714 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003715
Derek Foreman118a4292015-04-22 17:23:35 -05003716 if (time == 0 || input->cursor_anim_start == 0) {
3717 duration = 0;
3718 i = 0;
3719 } else
3720 i = wl_cursor_frame_and_duration(
3721 cursor,
3722 time - input->cursor_anim_start,
3723 &duration);
3724
3725 if (cursor->image_count > 1)
3726 schedule_pointer_image_update(input, cursor, duration,
3727 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003728
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003729 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003730}
3731
Derek Foreman118a4292015-04-22 17:23:35 -05003732static void
3733cursor_timer_func(struct task *task, uint32_t events)
3734{
3735 struct input *input = container_of(task, struct input, cursor_task);
3736 struct timespec tp;
3737 struct wl_cursor *cursor;
3738 uint32_t time;
3739 uint64_t exp;
3740
3741 if (!input->cursor_timer_running)
3742 return;
3743
3744 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3745 return;
3746
3747 cursor = input->display->cursors[input->current_cursor];
3748 if (!cursor)
3749 return;
3750
3751 clock_gettime(CLOCK_MONOTONIC, &tp);
3752 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3753 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3754}
3755
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003756static const struct wl_callback_listener pointer_surface_listener = {
3757 pointer_surface_frame_callback
3758};
3759
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003760void
3761input_set_pointer_image(struct input *input, int pointer)
3762{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003763 int force = 0;
3764
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003765 if (!input->pointer)
3766 return;
3767
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003768 if (input->pointer_enter_serial > input->cursor_serial)
3769 force = 1;
3770
3771 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003772 return;
3773
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003774 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003775 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003776 if (!input->cursor_frame_cb)
3777 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003778 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003779 /* The current frame callback may be stuck if, for instance,
3780 * the set cursor request was processed by the server after
3781 * this client lost the focus. In this case the cursor surface
3782 * might not be mapped and the frame callback wouldn't ever
3783 * complete. Send a set_cursor and attach to try to map the
3784 * cursor surface again so that the callback will finish */
3785 input_set_pointer_image_index(input, 0);
3786 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003787}
3788
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003789struct wl_data_device *
3790input_get_data_device(struct input *input)
3791{
3792 return input->data_device;
3793}
3794
3795void
3796input_set_selection(struct input *input,
3797 struct wl_data_source *source, uint32_t time)
3798{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003799 if (input->data_device)
3800 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003801}
3802
3803void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003804input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003805{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003806 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003807 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003808}
3809
3810static void
3811offer_io_func(struct task *task, uint32_t events)
3812{
3813 struct data_offer *offer =
3814 container_of(task, struct data_offer, io_task);
3815 unsigned int len;
3816 char buffer[4096];
3817
3818 len = read(offer->fd, buffer, sizeof buffer);
3819 offer->func(buffer, len,
3820 offer->x, offer->y, offer->user_data);
3821
3822 if (len == 0) {
3823 close(offer->fd);
3824 data_offer_destroy(offer);
3825 }
3826}
3827
3828static void
3829data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3830 data_func_t func, void *user_data)
3831{
3832 int p[2];
3833
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003834 if (pipe2(p, O_CLOEXEC) == -1)
3835 return;
3836
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003837 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3838 close(p[1]);
3839
3840 offer->io_task.run = offer_io_func;
3841 offer->fd = p[0];
3842 offer->func = func;
3843 offer->refcount++;
3844 offer->user_data = user_data;
3845
3846 display_watch_fd(offer->input->display,
3847 offer->fd, EPOLLIN, &offer->io_task);
3848}
3849
3850void
3851input_receive_drag_data(struct input *input, const char *mime_type,
3852 data_func_t func, void *data)
3853{
3854 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003855 input->drag_offer->x = input->drag_x;
3856 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003857}
3858
3859int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003860input_receive_drag_data_to_fd(struct input *input,
3861 const char *mime_type, int fd)
3862{
3863 if (input->drag_offer)
3864 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3865
3866 return 0;
3867}
3868
3869int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003870input_receive_selection_data(struct input *input, const char *mime_type,
3871 data_func_t func, void *data)
3872{
3873 char **p;
3874
3875 if (input->selection_offer == NULL)
3876 return -1;
3877
3878 for (p = input->selection_offer->types.data; *p; p++)
3879 if (strcmp(mime_type, *p) == 0)
3880 break;
3881
3882 if (*p == NULL)
3883 return -1;
3884
3885 data_offer_receive_data(input->selection_offer,
3886 mime_type, func, data);
3887 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003888}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003889
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003890int
3891input_receive_selection_data_to_fd(struct input *input,
3892 const char *mime_type, int fd)
3893{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003894 if (input->selection_offer)
3895 wl_data_offer_receive(input->selection_offer->offer,
3896 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003897
3898 return 0;
3899}
3900
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003901void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003902window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003903{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003904 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003905 return;
3906
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003907 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003908}
3909
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003910static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003911surface_set_synchronized(struct surface *surface)
3912{
3913 if (!surface->subsurface)
3914 return;
3915
3916 if (surface->synchronized)
3917 return;
3918
3919 wl_subsurface_set_sync(surface->subsurface);
3920 surface->synchronized = 1;
3921}
3922
3923static void
3924surface_set_synchronized_default(struct surface *surface)
3925{
3926 if (!surface->subsurface)
3927 return;
3928
3929 if (surface->synchronized == surface->synchronized_default)
3930 return;
3931
3932 if (surface->synchronized_default)
3933 wl_subsurface_set_sync(surface->subsurface);
3934 else
3935 wl_subsurface_set_desync(surface->subsurface);
3936
3937 surface->synchronized = surface->synchronized_default;
3938}
3939
3940static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003941surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003942{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003943 struct widget *widget = surface->widget;
3944 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003945
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003946 if (surface->input_region) {
3947 wl_region_destroy(surface->input_region);
3948 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003949 }
3950
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003951 if (surface->opaque_region)
3952 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003953
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003954 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003955
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003956 if (widget->resize_handler)
3957 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003958 widget->allocation.width,
3959 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003960 widget->user_data);
3961
Pekka Paalanen35e82632013-04-25 13:57:48 +03003962 if (surface->subsurface &&
3963 (surface->allocation.x != widget->allocation.x ||
3964 surface->allocation.y != widget->allocation.y)) {
3965 wl_subsurface_set_position(surface->subsurface,
3966 widget->allocation.x,
3967 widget->allocation.y);
3968 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003969 if (surface->allocation.width != widget->allocation.width ||
3970 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003971 window_schedule_redraw(widget->window);
3972 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003973 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003974
3975 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003976 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003977 widget->allocation.width,
3978 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003979}
3980
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003981static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003982window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003983{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003984 struct surface *surface;
3985
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003986 widget_set_allocation(window->main_surface->widget,
3987 window->pending_allocation.x,
3988 window->pending_allocation.y,
3989 window->pending_allocation.width,
3990 window->pending_allocation.height);
3991
3992 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003993
3994 /* The main surface is in the list, too. Main surface's
3995 * resize_handler is responsible for calling widget_set_allocation()
3996 * on all sub-surface root widgets, so they will be resized
3997 * properly.
3998 */
3999 wl_list_for_each(surface, &window->subsurface_list, link) {
4000 if (surface == window->main_surface)
4001 continue;
4002
4003 surface_set_synchronized(surface);
4004 surface_resize(surface);
4005 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004006
4007 if (!window->fullscreen && !window->maximized)
4008 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004009}
4010
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004011static void
4012idle_resize(struct window *window)
4013{
4014 window->resize_needed = 0;
4015 window->redraw_needed = 1;
4016
4017 DBG("from %dx%d to %dx%d\n",
4018 window->main_surface->server_allocation.width,
4019 window->main_surface->server_allocation.height,
4020 window->pending_allocation.width,
4021 window->pending_allocation.height);
4022
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004023 window_do_resize(window);
4024}
4025
4026static void
4027undo_resize(struct window *window)
4028{
4029 window->pending_allocation.width =
4030 window->main_surface->server_allocation.width;
4031 window->pending_allocation.height =
4032 window->main_surface->server_allocation.height;
4033
4034 DBG("back to %dx%d\n",
4035 window->main_surface->server_allocation.width,
4036 window->main_surface->server_allocation.height);
4037
4038 window_do_resize(window);
4039
4040 if (window->pending_allocation.width == 0 &&
4041 window->pending_allocation.height == 0) {
4042 fprintf(stderr, "Error: Could not draw a surface, "
4043 "most likely due to insufficient disk space in "
4044 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4045 exit(EXIT_FAILURE);
4046 }
4047}
4048
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004049void
4050window_schedule_resize(struct window *window, int width, int height)
4051{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004052 /* We should probably get these numbers from the theme. */
4053 const int min_width = 200, min_height = 200;
4054
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004055 window->pending_allocation.x = 0;
4056 window->pending_allocation.y = 0;
4057 window->pending_allocation.width = width;
4058 window->pending_allocation.height = height;
4059
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004060 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004061 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004062 window->min_allocation.width = min_width;
4063 else
4064 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004065 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004066 window->min_allocation.height = min_height;
4067 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004068 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004069 }
4070
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004071 if (window->pending_allocation.width < window->min_allocation.width)
4072 window->pending_allocation.width = window->min_allocation.width;
4073 if (window->pending_allocation.height < window->min_allocation.height)
4074 window->pending_allocation.height = window->min_allocation.height;
4075
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004076 window->resize_needed = 1;
4077 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004078}
4079
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004080void
4081widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4082{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004083 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004084}
4085
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004086static int
4087window_get_shadow_margin(struct window *window)
4088{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004089 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004090 return frame_get_shadow_margin(window->frame->frame);
4091 else
4092 return 0;
4093}
4094
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004095static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004096handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004097 int32_t width, int32_t height,
4098 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004099{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004100 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004101 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004102
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004103 window->maximized = 0;
4104 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004105 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004106 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004107
4108 wl_array_for_each(p, states) {
4109 uint32_t state = *p;
4110 switch (state) {
4111 case XDG_SURFACE_STATE_MAXIMIZED:
4112 window->maximized = 1;
4113 break;
4114 case XDG_SURFACE_STATE_FULLSCREEN:
4115 window->fullscreen = 1;
4116 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004117 case XDG_SURFACE_STATE_RESIZING:
4118 window->resizing = 1;
4119 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004120 case XDG_SURFACE_STATE_ACTIVATED:
4121 window->focused = 1;
4122 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004123 default:
4124 /* Unknown state */
4125 break;
4126 }
4127 }
4128
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004129 if (window->frame) {
4130 if (window->maximized) {
4131 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4132 } else {
4133 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4134 }
4135
4136 if (window->focused) {
4137 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4138 } else {
4139 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4140 }
4141 }
4142
Jasper St. Pierref184c382014-05-06 08:33:27 -04004143 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004144 /* The width / height params are for window geometry,
4145 * but window_schedule_resize takes allocation. Add
4146 * on the shadow margin to get the difference. */
4147 int margin = window_get_shadow_margin(window);
4148
4149 window_schedule_resize(window,
4150 width + margin * 2,
4151 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004152 } else {
4153 window_schedule_resize(window,
4154 window->saved_allocation.width,
4155 window->saved_allocation.height);
4156 }
4157
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004158 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004159
4160 if (window->state_changed_handler)
4161 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004162}
4163
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004164static void
4165handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4166{
4167 struct window *window = data;
4168 window_close(window);
4169}
4170
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004171static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004172 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004173 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004174};
4175
4176static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004177window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004178{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004179 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004180
4181 if (!window->xdg_surface)
4182 return;
4183
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004184 if (window->parent == window->last_parent)
4185 return;
4186
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004187 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004188 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004189 else
4190 parent_surface = NULL;
4191
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004192 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004193 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004194}
4195
4196static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004197window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004198{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004199 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004200 frame_input_rect(window->frame->frame,
4201 &geometry->x,
4202 &geometry->y,
4203 &geometry->width,
4204 &geometry->height);
4205 else
4206 window_get_allocation(window, geometry);
4207}
4208
4209static void
4210window_sync_geometry(struct window *window)
4211{
4212 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004213
4214 if (!window->xdg_surface)
4215 return;
4216
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004217 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004218 if (geometry.x == window->last_geometry.x &&
4219 geometry.y == window->last_geometry.y &&
4220 geometry.width == window->last_geometry.width &&
4221 geometry.height == window->last_geometry.height)
4222 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004223
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004224 xdg_surface_set_window_geometry(window->xdg_surface,
4225 geometry.x,
4226 geometry.y,
4227 geometry.width,
4228 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004229 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004230}
4231
4232static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004233window_flush(struct window *window)
4234{
4235 struct surface *surface;
4236
4237 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004238 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004239 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004240 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004241 }
4242 }
4243
4244 wl_list_for_each(surface, &window->subsurface_list, link) {
4245 if (surface == window->main_surface)
4246 continue;
4247
4248 surface_flush(surface);
4249 }
4250
4251 surface_flush(window->main_surface);
4252}
4253
4254static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004255menu_destroy(struct menu *menu)
4256{
4257 widget_destroy(menu->widget);
4258 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004259 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004260 free(menu);
4261}
4262
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004263void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004264window_get_allocation(struct window *window,
4265 struct rectangle *allocation)
4266{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004267 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004268}
4269
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004270static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004271widget_redraw(struct widget *widget)
4272{
4273 struct widget *child;
4274
4275 if (widget->redraw_handler)
4276 widget->redraw_handler(widget, widget->user_data);
4277 wl_list_for_each(child, &widget->child_list, link)
4278 widget_redraw(child);
4279}
4280
4281static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004282frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4283{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004284 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004285
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004286 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004287 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004288 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004289 surface->frame_cb = NULL;
4290
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004291 surface->last_time = time;
4292
Pekka Paalanen71233882013-04-25 13:57:53 +03004293 if (surface->redraw_needed || surface->window->redraw_needed) {
4294 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004295 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004296 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004297}
4298
4299static const struct wl_callback_listener listener = {
4300 frame_callback
4301};
4302
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004303static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004304surface_redraw(struct surface *surface)
4305{
Pekka Paalanen71233882013-04-25 13:57:53 +03004306 DBG_OBJ(surface->surface, "begin\n");
4307
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004308 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004309 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004310
4311 /* Whole-window redraw forces a redraw even if the previous has
4312 * not yet hit the screen.
4313 */
4314 if (surface->frame_cb) {
4315 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004316 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004317
Pekka Paalanen71233882013-04-25 13:57:53 +03004318 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004319 wl_callback_destroy(surface->frame_cb);
4320 }
4321
Neil Roberts97b747c2013-12-19 16:17:12 +00004322 if (surface->widget->use_cairo &&
4323 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004324 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004325 return -1;
4326 }
4327
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004328 surface->frame_cb = wl_surface_frame(surface->surface);
4329 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004330 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004331
4332 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004333 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004334 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004335 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004336 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004337}
4338
4339static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004340idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004341{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004342 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004343 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004344 int failed = 0;
4345 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004346
Pekka Paalanen71233882013-04-25 13:57:53 +03004347 DBG(" --------- \n");
4348
Pekka Paalaneneebff542013-04-25 13:57:52 +03004349 wl_list_init(&window->redraw_task.link);
4350 window->redraw_task_scheduled = 0;
4351
4352 if (window->resize_needed) {
4353 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004354 if (window->main_surface->frame_cb) {
4355 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004356 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004357 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004358
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004359 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004360 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004361 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004362
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004363 if (surface_redraw(window->main_surface) < 0) {
4364 /*
4365 * Only main_surface failure will cause us to undo the resize.
4366 * If sub-surfaces fail, they will just be broken with old
4367 * content.
4368 */
4369 failed = 1;
4370 } else {
4371 wl_list_for_each(surface, &window->subsurface_list, link) {
4372 if (surface == window->main_surface)
4373 continue;
4374
4375 surface_redraw(surface);
4376 }
4377 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004378
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004379 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004380 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004381
4382 wl_list_for_each(surface, &window->subsurface_list, link)
4383 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004384
4385 if (resized && failed) {
4386 /* Restore widget tree to correspond to what is on screen. */
4387 undo_resize(window);
4388 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004389}
4390
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004391static void
4392window_schedule_redraw_task(struct window *window)
4393{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004394 if (!window->redraw_task_scheduled) {
4395 window->redraw_task.run = idle_redraw;
4396 display_defer(window->display, &window->redraw_task);
4397 window->redraw_task_scheduled = 1;
4398 }
4399}
4400
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004401void
4402window_schedule_redraw(struct window *window)
4403{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004404 struct surface *surface;
4405
Pekka Paalanen71233882013-04-25 13:57:53 +03004406 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4407
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004408 wl_list_for_each(surface, &window->subsurface_list, link)
4409 surface->redraw_needed = 1;
4410
4411 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004412}
4413
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004414int
4415window_is_fullscreen(struct window *window)
4416{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004417 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004418}
4419
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004420void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004421window_set_fullscreen(struct window *window, int fullscreen)
4422{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004423 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004424 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004425
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004426 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004427 return;
4428
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004429 if (fullscreen)
4430 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4431 else
4432 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004433}
4434
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004435int
4436window_is_maximized(struct window *window)
4437{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004438 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004439}
4440
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004441void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004442window_set_maximized(struct window *window, int maximized)
4443{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004444 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004445 return;
4446
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004447 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004448 return;
4449
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004450 if (maximized)
4451 xdg_surface_set_maximized(window->xdg_surface);
4452 else
4453 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004454}
4455
Jasper St. Pierrede680992014-04-10 17:23:49 -07004456int
4457window_is_resizing(struct window *window)
4458{
4459 return window->resizing;
4460}
4461
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004462void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004463window_set_minimized(struct window *window)
4464{
4465 if (!window->xdg_surface)
4466 return;
4467
4468 xdg_surface_set_minimized(window->xdg_surface);
4469}
4470
4471void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004472window_set_user_data(struct window *window, void *data)
4473{
4474 window->user_data = data;
4475}
4476
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004477void *
4478window_get_user_data(struct window *window)
4479{
4480 return window->user_data;
4481}
4482
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004483void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004484window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004485 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004486{
4487 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004488}
4489
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004490void
4491window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004492 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004493{
4494 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004495}
4496
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004497void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004498window_set_data_handler(struct window *window, window_data_handler_t handler)
4499{
4500 window->data_handler = handler;
4501}
4502
4503void
4504window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4505{
4506 window->drop_handler = handler;
4507}
4508
4509void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004510window_set_close_handler(struct window *window,
4511 window_close_handler_t handler)
4512{
4513 window->close_handler = handler;
4514}
4515
4516void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004517window_set_fullscreen_handler(struct window *window,
4518 window_fullscreen_handler_t handler)
4519{
4520 window->fullscreen_handler = handler;
4521}
4522
4523void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004524window_set_output_handler(struct window *window,
4525 window_output_handler_t handler)
4526{
4527 window->output_handler = handler;
4528}
4529
4530void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004531window_set_state_changed_handler(struct window *window,
4532 window_state_changed_handler_t handler)
4533{
4534 window->state_changed_handler = handler;
4535}
4536
4537void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004538window_set_title(struct window *window, const char *title)
4539{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004540 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004541 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004542 if (window->frame) {
4543 frame_set_title(window->frame->frame, title);
4544 widget_schedule_redraw(window->frame->widget);
4545 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004546 if (window->xdg_surface)
4547 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004548}
4549
4550const char *
4551window_get_title(struct window *window)
4552{
4553 return window->title;
4554}
4555
4556void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004557window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4558{
4559 struct text_cursor_position *text_cursor_position =
4560 window->display->text_cursor_position;
4561
Scott Moreau9295ce02012-06-01 12:46:10 -06004562 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004563 return;
4564
4565 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004566 window->main_surface->surface,
4567 wl_fixed_from_int(x),
4568 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004569}
4570
Casey Dahlin9074db52012-04-19 22:50:09 -04004571static void
4572surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004573 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004574{
Rob Bradford7507b572012-05-15 17:55:34 +01004575 struct window *window = data;
4576 struct output *output;
4577 struct output *output_found = NULL;
4578 struct window_output *window_output;
4579
4580 wl_list_for_each(output, &window->display->output_list, link) {
4581 if (output->output == wl_output) {
4582 output_found = output;
4583 break;
4584 }
4585 }
4586
4587 if (!output_found)
4588 return;
4589
Brian Lovinbc919262013-08-07 15:34:59 -07004590 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004591 window_output->output = output_found;
4592
4593 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004594
4595 if (window->output_handler)
4596 window->output_handler(window, output_found, 1,
4597 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004598}
4599
4600static void
4601surface_leave(void *data,
4602 struct wl_surface *wl_surface, struct wl_output *output)
4603{
Rob Bradford7507b572012-05-15 17:55:34 +01004604 struct window *window = data;
4605 struct window_output *window_output;
4606 struct window_output *window_output_found = NULL;
4607
4608 wl_list_for_each(window_output, &window->window_output_list, link) {
4609 if (window_output->output->output == output) {
4610 window_output_found = window_output;
4611 break;
4612 }
4613 }
4614
4615 if (window_output_found) {
4616 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004617
4618 if (window->output_handler)
4619 window->output_handler(window, window_output->output,
4620 0, window->user_data);
4621
Rob Bradford7507b572012-05-15 17:55:34 +01004622 free(window_output_found);
4623 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004624}
4625
4626static const struct wl_surface_listener surface_listener = {
4627 surface_enter,
4628 surface_leave
4629};
4630
Pekka Paalanen4e373742013-02-13 16:17:13 +02004631static struct surface *
4632surface_create(struct window *window)
4633{
4634 struct display *display = window->display;
4635 struct surface *surface;
4636
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004637 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004638 surface->window = window;
4639 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004640 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004641 wl_surface_add_listener(surface->surface, &surface_listener, window);
4642
Pekka Paalanen35e82632013-04-25 13:57:48 +03004643 wl_list_insert(&window->subsurface_list, &surface->link);
4644
Pekka Paalanen4e373742013-02-13 16:17:13 +02004645 return surface;
4646}
4647
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004648static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004649get_preferred_buffer_type(struct display *display)
4650{
4651#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004652 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004653 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4654#endif
4655
4656 return WINDOW_BUFFER_TYPE_SHM;
4657}
4658
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004659static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004660window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004661{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004662 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004663 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004664
Peter Huttererf3d62272013-08-08 11:57:05 +10004665 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004666 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004667 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004668
4669 surface = surface_create(window);
4670 window->main_surface = surface;
4671
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004672 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004673
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004674 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004675 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004676
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004677 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004678
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004679 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004680 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004681 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004682
Rob Bradford7507b572012-05-15 17:55:34 +01004683 wl_list_init (&window->window_output_list);
4684
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004685 return window;
4686}
4687
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004688struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004689window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004690{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004691 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004692 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004693
4694 window = window_create_internal(display, 0);
4695
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004696 if (window->display->xdg_shell) {
4697 window->xdg_surface =
4698 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4699 window->main_surface->surface);
4700 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004701
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004702 xdg_surface_set_user_data(window->xdg_surface, window);
4703 xdg_surface_add_listener(window->xdg_surface,
4704 &xdg_surface_listener, window);
4705 } else if (display->ivi_application) {
4706 /* auto generation of ivi_id based on process id + basement of id */
4707 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4708 window->ivi_surface =
4709 ivi_application_surface_create(display->ivi_application,
4710 id_ivisurf, window->main_surface->surface);
4711 fail_on_null(window->ivi_surface);
4712
4713 ivi_surface_add_listener(window->ivi_surface,
4714 &ivi_surface_listener, window);
4715 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004716
4717 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004718}
4719
4720struct window *
4721window_create_custom(struct display *display)
4722{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004723 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004724}
4725
Jasper St. Pierre53686042013-12-09 15:26:25 -05004726void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004727window_set_parent(struct window *window,
4728 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004729{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004730 window->parent = parent_window;
4731 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004732}
4733
4734struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004735window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004736{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004737 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004738}
4739
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004740static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004741menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004742{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004743 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004744 int next;
4745
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004746 frame_interior(menu->frame, &x, &y, &width, &height);
4747 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004748 if (menu->current != next) {
4749 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004750 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004751 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004752}
4753
4754static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004755menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004756 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004757 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004758{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004759 struct menu *menu = data;
4760
4761 if (widget == menu->widget)
4762 menu_set_item(data, y);
4763
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004764 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004765}
4766
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004767static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004768menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004769 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004770{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004771 struct menu *menu = data;
4772
4773 if (widget == menu->widget)
4774 menu_set_item(data, y);
4775
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004776 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004777}
4778
4779static void
4780menu_leave_handler(struct widget *widget, struct input *input, void *data)
4781{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004782 struct menu *menu = data;
4783
4784 if (widget == menu->widget)
4785 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004786}
4787
4788static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004789menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004790 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004791 uint32_t button, enum wl_pointer_button_state state,
4792 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004793
4794{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004795 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004796
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004797 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4798 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004799 /* Either relase after press-drag-release or
4800 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004801 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05004802 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004803 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004804 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004805 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004806 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004807}
4808
4809static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004810menu_touch_up_handler(struct widget *widget,
4811 struct input *input,
4812 uint32_t serial,
4813 uint32_t time,
4814 int32_t id,
4815 void *data)
4816{
4817 struct menu *menu = data;
4818
4819 input_ungrab(input);
4820 menu_destroy(menu);
4821}
4822
4823static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004824menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004825{
4826 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004827 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004828 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004829
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004830 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004831
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004832 frame_repaint(menu->frame, cr);
4833 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004834
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004835 theme_set_background_source(menu->window->display->theme,
4836 cr, THEME_FRAME_ACTIVE);
4837 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004838 cairo_fill(cr);
4839
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004840 cairo_select_font_face(cr, "sans",
4841 CAIRO_FONT_SLANT_NORMAL,
4842 CAIRO_FONT_WEIGHT_NORMAL);
4843 cairo_set_font_size(cr, 12);
4844
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004845 for (i = 0; i < menu->count; i++) {
4846 if (i == menu->current) {
4847 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004848 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004849 cairo_fill(cr);
4850 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004851 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004852 cairo_show_text(cr, menu->entries[i]);
4853 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004854 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4855 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004856 cairo_show_text(cr, menu->entries[i]);
4857 }
4858 }
4859
4860 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004861}
4862
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004863static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004864handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004865{
4866 struct window *window = data;
4867 struct menu *menu = window->main_surface->widget->user_data;
4868
4869 input_ungrab(menu->input);
4870 menu_destroy(menu);
4871}
4872
4873static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004874 handle_popup_popup_done,
4875};
4876
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004877static struct menu *
4878create_menu(struct display *display,
4879 struct input *input, uint32_t time,
4880 menu_func_t func, const char **entries, int count,
4881 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004882{
4883 struct window *window;
4884 struct menu *menu;
4885
4886 menu = malloc(sizeof *menu);
4887 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004888 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004889
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004890 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004891 if (!window) {
4892 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004893 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004894 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004895
4896 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004897 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004898 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004899 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004900 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004901 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004902 menu->entries = entries;
4903 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004904 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004905 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004906 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004907 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004908 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004909
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004910 input_ungrab(input);
4911
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004912 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004913 widget_set_enter_handler(menu->widget, menu_enter_handler);
4914 widget_set_leave_handler(menu->widget, menu_leave_handler);
4915 widget_set_motion_handler(menu->widget, menu_motion_handler);
4916 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004917 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004918
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004919 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004920 frame_resize_inside(menu->frame, 200, count * 20);
4921 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4922 window_schedule_resize(window, frame_width(menu->frame),
4923 frame_height(menu->frame));
4924
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004925 return menu;
4926}
4927
4928struct window *
4929window_create_menu(struct display *display,
4930 struct input *input, uint32_t time,
4931 menu_func_t func, const char **entries, int count,
4932 void *user_data)
4933{
4934 struct menu *menu;
4935 menu = create_menu(display, input, time, func, entries, count, user_data);
4936
4937 if (menu == NULL)
4938 return NULL;
4939
4940 return menu->window;
4941}
4942
4943void
4944window_show_menu(struct display *display,
4945 struct input *input, uint32_t time, struct window *parent,
4946 int32_t x, int32_t y,
4947 menu_func_t func, const char **entries, int count)
4948{
4949 struct menu *menu;
4950 struct window *window;
4951 int32_t ix, iy;
4952
4953 menu = create_menu(display, input, time, func, entries, count, parent);
4954
4955 if (menu == NULL)
4956 return;
4957
4958 window = menu->window;
4959
4960 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4961 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4962
4963 window->x = x;
4964 window->y = y;
4965
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004966 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004967
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004968 if (!display->xdg_shell)
4969 return;
4970
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004971 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4972 window->main_surface->surface,
4973 parent->main_surface->surface,
4974 input->seat,
4975 display_get_serial(window->display),
4976 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004977 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004978 fail_on_null(window->xdg_popup);
4979
4980 xdg_popup_set_user_data(window->xdg_popup, window);
4981 xdg_popup_add_listener(window->xdg_popup,
4982 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004983}
4984
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004985void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004986window_set_buffer_type(struct window *window, enum window_buffer_type type)
4987{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004988 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004989}
4990
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004991enum window_buffer_type
4992window_get_buffer_type(struct window *window)
4993{
4994 return window->main_surface->buffer_type;
4995}
4996
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004997void
4998window_set_preferred_format(struct window *window,
4999 enum preferred_format format)
5000{
5001 window->preferred_format = format;
5002}
5003
Pekka Paalanen35e82632013-04-25 13:57:48 +03005004struct widget *
5005window_add_subsurface(struct window *window, void *data,
5006 enum subsurface_mode default_mode)
5007{
5008 struct widget *widget;
5009 struct surface *surface;
5010 struct wl_surface *parent;
5011 struct wl_subcompositor *subcompo = window->display->subcompositor;
5012
Pekka Paalanen35e82632013-04-25 13:57:48 +03005013 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005014 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005015 widget = widget_create(window, surface, data);
5016 wl_list_init(&widget->link);
5017 surface->widget = widget;
5018
5019 parent = window->main_surface->surface;
5020 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5021 surface->surface,
5022 parent);
5023 surface->synchronized = 1;
5024
5025 switch (default_mode) {
5026 case SUBSURFACE_SYNCHRONIZED:
5027 surface->synchronized_default = 1;
5028 break;
5029 case SUBSURFACE_DESYNCHRONIZED:
5030 surface->synchronized_default = 0;
5031 break;
5032 default:
5033 assert(!"bad enum subsurface_mode");
5034 }
5035
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005036 window->resize_needed = 1;
5037 window_schedule_redraw(window);
5038
Pekka Paalanen35e82632013-04-25 13:57:48 +03005039 return widget;
5040}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005041
5042static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005043display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005044 struct wl_output *wl_output,
5045 int x, int y,
5046 int physical_width,
5047 int physical_height,
5048 int subpixel,
5049 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005050 const char *model,
5051 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005052{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005053 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005054
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005055 output->allocation.x = x;
5056 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005057 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005058
5059 if (output->make)
5060 free(output->make);
5061 output->make = strdup(make);
5062
5063 if (output->model)
5064 free(output->model);
5065 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005066}
5067
5068static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005069display_handle_done(void *data,
5070 struct wl_output *wl_output)
5071{
5072}
5073
5074static void
5075display_handle_scale(void *data,
5076 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005077 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005078{
5079 struct output *output = data;
5080
5081 output->scale = scale;
5082}
5083
5084static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005085display_handle_mode(void *data,
5086 struct wl_output *wl_output,
5087 uint32_t flags,
5088 int width,
5089 int height,
5090 int refresh)
5091{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005092 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005093 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005094
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005095 if (flags & WL_OUTPUT_MODE_CURRENT) {
5096 output->allocation.width = width;
5097 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005098 if (display->output_configure_handler)
5099 (*display->output_configure_handler)(
5100 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005101 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005102}
5103
5104static const struct wl_output_listener output_listener = {
5105 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005106 display_handle_mode,
5107 display_handle_done,
5108 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005109};
5110
5111static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005112display_add_output(struct display *d, uint32_t id)
5113{
5114 struct output *output;
5115
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005116 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005117 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005118 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005119 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005120 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005121 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005122 wl_list_insert(d->output_list.prev, &output->link);
5123
5124 wl_output_add_listener(output->output, &output_listener, output);
5125}
5126
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005127static void
5128output_destroy(struct output *output)
5129{
5130 if (output->destroy_handler)
5131 (*output->destroy_handler)(output, output->user_data);
5132
5133 wl_output_destroy(output->output);
5134 wl_list_remove(&output->link);
5135 free(output);
5136}
5137
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005138static void
5139display_destroy_output(struct display *d, uint32_t id)
5140{
5141 struct output *output;
5142
5143 wl_list_for_each(output, &d->output_list, link) {
5144 if (output->server_output_id == id) {
5145 output_destroy(output);
5146 break;
5147 }
5148 }
5149}
5150
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005151void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005152display_set_global_handler(struct display *display,
5153 display_global_handler_t handler)
5154{
5155 struct global *global;
5156
5157 display->global_handler = handler;
5158 if (!handler)
5159 return;
5160
5161 wl_list_for_each(global, &display->global_list, link)
5162 display->global_handler(display,
5163 global->name, global->interface,
5164 global->version, display->user_data);
5165}
5166
5167void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005168display_set_global_handler_remove(struct display *display,
5169 display_global_handler_t remove_handler)
5170{
5171 display->global_handler_remove = remove_handler;
5172 if (!remove_handler)
5173 return;
5174}
5175
5176void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005177display_set_output_configure_handler(struct display *display,
5178 display_output_handler_t handler)
5179{
5180 struct output *output;
5181
5182 display->output_configure_handler = handler;
5183 if (!handler)
5184 return;
5185
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005186 wl_list_for_each(output, &display->output_list, link) {
5187 if (output->allocation.width == 0 &&
5188 output->allocation.height == 0)
5189 continue;
5190
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005191 (*display->output_configure_handler)(output,
5192 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005193 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005194}
5195
5196void
5197output_set_user_data(struct output *output, void *data)
5198{
5199 output->user_data = data;
5200}
5201
5202void *
5203output_get_user_data(struct output *output)
5204{
5205 return output->user_data;
5206}
5207
5208void
5209output_set_destroy_handler(struct output *output,
5210 display_output_handler_t handler)
5211{
5212 output->destroy_handler = handler;
5213 /* FIXME: implement this, once we have way to remove outputs */
5214}
5215
5216void
Scott Moreau4e072362012-09-29 02:03:11 -06005217output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005218{
Scott Moreau4e072362012-09-29 02:03:11 -06005219 struct rectangle allocation = output->allocation;
5220
5221 switch (output->transform) {
5222 case WL_OUTPUT_TRANSFORM_90:
5223 case WL_OUTPUT_TRANSFORM_270:
5224 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5225 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5226 /* Swap width and height */
5227 allocation.width = output->allocation.height;
5228 allocation.height = output->allocation.width;
5229 break;
5230 }
5231
5232 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005233}
5234
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005235struct wl_output *
5236output_get_wl_output(struct output *output)
5237{
5238 return output->output;
5239}
5240
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005241enum wl_output_transform
5242output_get_transform(struct output *output)
5243{
5244 return output->transform;
5245}
5246
Alexander Larssonafd319a2013-05-22 14:41:27 +02005247uint32_t
5248output_get_scale(struct output *output)
5249{
5250 return output->scale;
5251}
5252
Jason Ekstrand738715d2014-04-02 19:53:50 -05005253const char *
5254output_get_make(struct output *output)
5255{
5256 return output->make;
5257}
5258
5259const char *
5260output_get_model(struct output *output)
5261{
5262 return output->model;
5263}
5264
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005265static void
Daniel Stone97f68542012-05-30 16:32:01 +01005266fini_xkb(struct input *input)
5267{
5268 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005269 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005270}
5271
5272static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005273display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005274{
5275 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005276 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005277
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005278 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005279 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005280 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005281 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005282 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005283 input->pointer_focus = NULL;
5284 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005285 input->seat_version = seat_version;
5286
Rusty Lynch041815a2013-08-08 21:20:38 -07005287 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005288 wl_list_insert(d->input_list.prev, &input->link);
5289
Daniel Stone37816df2012-05-16 18:45:18 +01005290 wl_seat_add_listener(input->seat, &seat_listener, input);
5291 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005292
Jason Ekstranda669bd52014-04-02 19:53:51 -05005293 if (d->data_device_manager) {
5294 input->data_device =
5295 wl_data_device_manager_get_data_device(d->data_device_manager,
5296 input->seat);
5297 wl_data_device_add_listener(input->data_device,
5298 &data_device_listener,
5299 input);
5300 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005301
5302 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005303 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005304
Derek Foreman118a4292015-04-22 17:23:35 -05005305 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5306 TFD_CLOEXEC | TFD_NONBLOCK);
5307 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5308 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005309 set_repeat_info(input, 40, 400);
5310
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005311 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5312 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005313 input->repeat_task.run = keyboard_repeat_func;
5314 display_watch_fd(d, input->repeat_timer_fd,
5315 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005316}
5317
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005318static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005319input_destroy(struct input *input)
5320{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005321 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005322 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005323
5324 if (input->drag_offer)
5325 data_offer_destroy(input->drag_offer);
5326
5327 if (input->selection_offer)
5328 data_offer_destroy(input->selection_offer);
5329
kabeer khan6ce67ec2014-10-20 11:55:29 +05305330 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005331 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305332 wl_data_device_release(input->data_device);
5333 else
5334 wl_data_device_destroy(input->data_device);
5335 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005336 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005337 if (input->touch)
5338 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005339 if (input->pointer)
5340 wl_pointer_release(input->pointer);
5341 if (input->keyboard)
5342 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005343 } else {
5344 if (input->touch)
5345 wl_touch_destroy(input->touch);
5346 if (input->pointer)
5347 wl_pointer_destroy(input->pointer);
5348 if (input->keyboard)
5349 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005350 }
5351
Daniel Stone97f68542012-05-30 16:32:01 +01005352 fini_xkb(input);
5353
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005354 wl_surface_destroy(input->pointer_surface);
5355
Pekka Paalanene1207c72011-12-16 12:02:09 +02005356 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005357 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005358 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005359 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005360 free(input);
5361}
5362
5363static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005364shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5365{
5366 struct display *d = data;
5367
5368 if (format == WL_SHM_FORMAT_RGB565)
5369 d->has_rgb565 = 1;
5370}
5371
5372struct wl_shm_listener shm_listener = {
5373 shm_format
5374};
5375
5376static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005377xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5378{
5379 xdg_shell_pong(shell, serial);
5380}
5381
5382static const struct xdg_shell_listener xdg_shell_listener = {
5383 xdg_shell_ping,
5384};
5385
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005386#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005387#ifdef static_assert
5388static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5389 "Interface version doesn't match implementation version");
5390#endif
5391
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005392static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005393registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5394 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005395{
5396 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005397 struct global *global;
5398
Brian Lovinbc919262013-08-07 15:34:59 -07005399 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005400 global->name = id;
5401 global->interface = strdup(interface);
5402 global->version = version;
5403 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005404
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005405 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005406 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005407 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005408 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005409 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005410 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005411 display_add_input(d, id, version);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005412 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005413 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005414 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005415 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305416 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005417 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005418 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305419 &wl_data_device_manager_interface,
5420 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005421 } else if (strcmp(interface, "xdg_shell") == 0) {
5422 d->xdg_shell = wl_registry_bind(registry, id,
5423 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005424 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005425 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005426 } else if (strcmp(interface, "text_cursor_position") == 0) {
5427 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005428 wl_registry_bind(registry, id,
5429 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005430 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5431 d->subcompositor =
5432 wl_registry_bind(registry, id,
5433 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005434 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005435 else if (strcmp(interface, "ivi_application") == 0) {
5436 d->ivi_application =
5437 wl_registry_bind(registry, id,
5438 &ivi_application_interface, 1);
5439 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005440
5441 if (d->global_handler)
5442 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005443}
5444
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005445static void
5446registry_handle_global_remove(void *data, struct wl_registry *registry,
5447 uint32_t name)
5448{
5449 struct display *d = data;
5450 struct global *global;
5451 struct global *tmp;
5452
5453 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5454 if (global->name != name)
5455 continue;
5456
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005457 if (strcmp(global->interface, "wl_output") == 0)
5458 display_destroy_output(d, name);
5459
5460 /* XXX: Should destroy remaining bound globals */
5461
5462 if (d->global_handler_remove)
5463 d->global_handler_remove(d, name, global->interface,
5464 global->version, d->user_data);
5465
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005466 wl_list_remove(&global->link);
5467 free(global->interface);
5468 free(global);
5469 }
5470}
5471
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005472void *
5473display_bind(struct display *display, uint32_t name,
5474 const struct wl_interface *interface, uint32_t version)
5475{
5476 return wl_registry_bind(display->registry, name, interface, version);
5477}
5478
5479static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005480 registry_handle_global,
5481 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005482};
5483
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005484#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005485static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005486init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005487{
5488 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005489 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005490
Rob Clark6396ed32012-03-11 19:48:41 -05005491#ifdef USE_CAIRO_GLESV2
5492# define GL_BIT EGL_OPENGL_ES2_BIT
5493#else
5494# define GL_BIT EGL_OPENGL_BIT
5495#endif
5496
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005497 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005498 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005499 EGL_RED_SIZE, 1,
5500 EGL_GREEN_SIZE, 1,
5501 EGL_BLUE_SIZE, 1,
5502 EGL_ALPHA_SIZE, 1,
5503 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005504 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005505 EGL_NONE
5506 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005507
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005508#ifdef USE_CAIRO_GLESV2
5509 static const EGLint context_attribs[] = {
5510 EGL_CONTEXT_CLIENT_VERSION, 2,
5511 EGL_NONE
5512 };
5513 EGLint api = EGL_OPENGL_ES_API;
5514#else
5515 EGLint *context_attribs = NULL;
5516 EGLint api = EGL_OPENGL_API;
5517#endif
5518
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005519 d->dpy =
5520 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5521 d->display, NULL);
5522
Yuval Fledel45568f62010-12-06 09:18:12 -05005523 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005524 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005525 return -1;
5526 }
5527
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005528 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005529 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005530 return -1;
5531 }
5532
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005533 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5534 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005535 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005536 return -1;
5537 }
5538
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005539 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005540 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005541 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005542 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005543 return -1;
5544 }
5545
Benjamin Franzke0c991632011-09-27 21:57:31 +02005546 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5547 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005548 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005549 return -1;
5550 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005551
5552 return 0;
5553}
5554
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005555static void
5556fini_egl(struct display *display)
5557{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005558 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005559
5560 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5561 EGL_NO_CONTEXT);
5562
5563 eglTerminate(display->dpy);
5564 eglReleaseThread();
5565}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005566#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005567
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005568static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005569init_dummy_surface(struct display *display)
5570{
5571 int len;
5572 void *data;
5573
5574 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005575 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005576 display->dummy_surface =
5577 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5578 1, 1, len);
5579 display->dummy_surface_data = data;
5580}
5581
5582static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005583handle_display_data(struct task *task, uint32_t events)
5584{
5585 struct display *display =
5586 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005587 struct epoll_event ep;
5588 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005589
5590 display->display_fd_events = events;
5591
5592 if (events & EPOLLERR || events & EPOLLHUP) {
5593 display_exit(display);
5594 return;
5595 }
5596
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005597 if (events & EPOLLIN) {
5598 ret = wl_display_dispatch(display->display);
5599 if (ret == -1) {
5600 display_exit(display);
5601 return;
5602 }
5603 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005604
5605 if (events & EPOLLOUT) {
5606 ret = wl_display_flush(display->display);
5607 if (ret == 0) {
5608 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5609 ep.data.ptr = &display->display_task;
5610 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5611 display->display_fd, &ep);
5612 } else if (ret == -1 && errno != EAGAIN) {
5613 display_exit(display);
5614 return;
5615 }
5616 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005617}
5618
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005619static void
5620log_handler(const char *format, va_list args)
5621{
5622 vfprintf(stderr, format, args);
5623}
5624
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005625struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005626display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005627{
5628 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005629
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005630 wl_log_set_handler_client(log_handler);
5631
Peter Huttererf3d62272013-08-08 11:57:05 +10005632 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005633 if (d == NULL)
5634 return NULL;
5635
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005636 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005637 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005638 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005639 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005640 return NULL;
5641 }
5642
Rob Bradford5ab9c752013-07-26 16:29:43 +01005643 d->xkb_context = xkb_context_new(0);
5644 if (d->xkb_context == NULL) {
5645 fprintf(stderr, "Failed to create XKB context\n");
5646 free(d);
5647 return NULL;
5648 }
5649
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005650 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005651 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005652 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005653 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5654 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005655
5656 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005657 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005658 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005659 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005660
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005661 d->registry = wl_display_get_registry(d->display);
5662 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005663
Marek Chalupaa519d062014-12-05 13:49:40 +01005664 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005665 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5666 return NULL;
5667 }
5668
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005669#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005670 if (init_egl(d) < 0)
5671 fprintf(stderr, "EGL does not seem to work, "
5672 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005673#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005674
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005675 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005676
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005677 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005678
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005679 wl_list_init(&d->window_list);
5680
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005681 init_dummy_surface(d);
5682
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005683 return d;
5684}
5685
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005686static void
5687display_destroy_outputs(struct display *display)
5688{
5689 struct output *tmp;
5690 struct output *output;
5691
5692 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5693 output_destroy(output);
5694}
5695
Pekka Paalanene1207c72011-12-16 12:02:09 +02005696static void
5697display_destroy_inputs(struct display *display)
5698{
5699 struct input *tmp;
5700 struct input *input;
5701
5702 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5703 input_destroy(input);
5704}
5705
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005706void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005707display_destroy(struct display *display)
5708{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005709 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005710 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5711 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005712
5713 if (!wl_list_empty(&display->deferred_list))
5714 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5715
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005716 cairo_surface_destroy(display->dummy_surface);
5717 free(display->dummy_surface_data);
5718
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005719 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005720 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005721
Daniel Stone97f68542012-05-30 16:32:01 +01005722 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005723
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005724 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005725 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005726
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005727#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005728 if (display->argb_device)
5729 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005730#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005731
Pekka Paalanen35e82632013-04-25 13:57:48 +03005732 if (display->subcompositor)
5733 wl_subcompositor_destroy(display->subcompositor);
5734
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005735 if (display->xdg_shell)
5736 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005737
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005738 if (display->ivi_application)
5739 ivi_application_destroy(display->ivi_application);
5740
Pekka Paalanenc2052982011-12-16 11:41:32 +02005741 if (display->shm)
5742 wl_shm_destroy(display->shm);
5743
5744 if (display->data_device_manager)
5745 wl_data_device_manager_destroy(display->data_device_manager);
5746
5747 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005748 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005749
5750 close(display->epoll_fd);
5751
U. Artie Eoff44874d92012-10-02 21:12:35 -07005752 if (!(display->display_fd_events & EPOLLERR) &&
5753 !(display->display_fd_events & EPOLLHUP))
5754 wl_display_flush(display->display);
5755
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005756 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005757 free(display);
5758}
5759
5760void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005761display_set_user_data(struct display *display, void *data)
5762{
5763 display->user_data = data;
5764}
5765
5766void *
5767display_get_user_data(struct display *display)
5768{
5769 return display->user_data;
5770}
5771
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005772struct wl_display *
5773display_get_display(struct display *display)
5774{
5775 return display->display;
5776}
5777
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005778int
5779display_has_subcompositor(struct display *display)
5780{
5781 if (display->subcompositor)
5782 return 1;
5783
5784 wl_display_roundtrip(display->display);
5785
5786 return display->subcompositor != NULL;
5787}
5788
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005789cairo_device_t *
5790display_get_cairo_device(struct display *display)
5791{
5792 return display->argb_device;
5793}
5794
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005795struct output *
5796display_get_output(struct display *display)
5797{
5798 return container_of(display->output_list.next, struct output, link);
5799}
5800
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005801struct wl_compositor *
5802display_get_compositor(struct display *display)
5803{
5804 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005805}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005806
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005807uint32_t
5808display_get_serial(struct display *display)
5809{
5810 return display->serial;
5811}
5812
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005813EGLDisplay
5814display_get_egl_display(struct display *d)
5815{
5816 return d->dpy;
5817}
5818
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005819struct wl_data_source *
5820display_create_data_source(struct display *display)
5821{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005822 if (display->data_device_manager)
5823 return wl_data_device_manager_create_data_source(display->data_device_manager);
5824 else
5825 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005826}
5827
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005828EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005829display_get_argb_egl_config(struct display *d)
5830{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005831 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005832}
5833
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005834int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005835display_acquire_window_surface(struct display *display,
5836 struct window *window,
5837 EGLContext ctx)
5838{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005839 struct surface *surface = window->main_surface;
5840
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005841 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005842 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005843
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005844 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005845 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005846}
5847
5848void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005849display_release_window_surface(struct display *display,
5850 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005851{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005852 struct surface *surface = window->main_surface;
5853
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005854 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005855 return;
5856
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005857 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005858}
5859
5860void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005861display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005862{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005863 wl_list_insert(&display->deferred_list, &task->link);
5864}
5865
5866void
5867display_watch_fd(struct display *display,
5868 int fd, uint32_t events, struct task *task)
5869{
5870 struct epoll_event ep;
5871
5872 ep.events = events;
5873 ep.data.ptr = task;
5874 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5875}
5876
5877void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005878display_unwatch_fd(struct display *display, int fd)
5879{
5880 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5881}
5882
5883void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005884display_run(struct display *display)
5885{
5886 struct task *task;
5887 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005888 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005889
Pekka Paalanen826d7952011-12-15 10:14:07 +02005890 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005891 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005892 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005893 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005894 struct task, link);
5895 wl_list_remove(&task->link);
5896 task->run(task, 0);
5897 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005898
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005899 wl_display_dispatch_pending(display->display);
5900
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005901 if (!display->running)
5902 break;
5903
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005904 ret = wl_display_flush(display->display);
5905 if (ret < 0 && errno == EAGAIN) {
5906 ep[0].events =
5907 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5908 ep[0].data.ptr = &display->display_task;
5909
5910 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5911 display->display_fd, &ep[0]);
5912 } else if (ret < 0) {
5913 break;
5914 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005915
5916 count = epoll_wait(display->epoll_fd,
5917 ep, ARRAY_LENGTH(ep), -1);
5918 for (i = 0; i < count; i++) {
5919 task = ep[i].data.ptr;
5920 task->run(task, ep[i].events);
5921 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005922 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005923}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005924
5925void
5926display_exit(struct display *display)
5927{
5928 display->running = 0;
5929}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005930
5931void
5932keysym_modifiers_add(struct wl_array *modifiers_map,
5933 const char *name)
5934{
5935 size_t len = strlen(name) + 1;
5936 char *p;
5937
5938 p = wl_array_add(modifiers_map, len);
5939
5940 if (p == NULL)
5941 return;
5942
5943 strncpy(p, name, len);
5944}
5945
5946static xkb_mod_index_t
5947keysym_modifiers_get_index(struct wl_array *modifiers_map,
5948 const char *name)
5949{
5950 xkb_mod_index_t index = 0;
5951 char *p = modifiers_map->data;
5952
5953 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5954 if (strcmp(p, name) == 0)
5955 return index;
5956
5957 index++;
5958 p += strlen(p) + 1;
5959 }
5960
5961 return XKB_MOD_INVALID;
5962}
5963
5964xkb_mod_mask_t
5965keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5966 const char *name)
5967{
5968 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5969
5970 if (index == XKB_MOD_INVALID)
5971 return XKB_MOD_INVALID;
5972
5973 return 1 << index;
5974}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005975
5976void *
5977fail_on_null(void *p)
5978{
5979 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005980 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005981 exit(EXIT_FAILURE);
5982 }
5983
5984 return p;
5985}
5986
5987void *
5988xmalloc(size_t s)
5989{
5990 return fail_on_null(malloc(s));
5991}
5992
Peter Huttererf3d62272013-08-08 11:57:05 +10005993void *
5994xzalloc(size_t s)
5995{
5996 return fail_on_null(zalloc(s));
5997}
5998
Kristian Høgsbergce278412013-07-25 15:20:20 -07005999char *
6000xstrdup(const char *s)
6001{
6002 return fail_on_null(strdup(s));
6003}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08006004
6005void *
6006xrealloc(char *p, size_t s)
6007{
6008 return fail_on_null(realloc(p, s));
6009}