blob: 5d69116e7f39943149310b478cfff877e733a903 [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;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400285 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500286 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300287 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500288 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000289 /* If this is set to false then no cairo surface will be
290 * created before redrawing the surface. This is useful if the
291 * redraw handler is going to do completely custom rendering
292 * such as using EGL directly */
293 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400294};
295
Rusty Lynch041815a2013-08-08 21:20:38 -0700296struct touch_point {
297 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800298 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700299 struct widget *widget;
300 struct wl_list link;
301};
302
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303struct input {
304 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100305 struct wl_seat *seat;
306 struct wl_pointer *pointer;
307 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700308 struct wl_touch *touch;
309 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400310 struct window *pointer_focus;
311 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700312 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300313 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300314 uint32_t cursor_anim_start;
315 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500316 uint32_t cursor_timer_start;
317 uint32_t cursor_anim_current;
318 int cursor_delay_fd;
319 bool cursor_timer_running;
320 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300321 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400322 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400323 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400324 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400325 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400326 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400327
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500328 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500329 struct widget *grab;
330 uint32_t grab_button;
331
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400332 struct wl_data_device *data_device;
333 struct data_offer *drag_offer;
334 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800335 uint32_t touch_grab;
336 int32_t touch_grab_id;
337 float drag_x, drag_y;
338 struct window *drag_focus;
339 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100340
341 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100342 struct xkb_keymap *keymap;
343 struct xkb_state *state;
344 xkb_mod_mask_t control_mask;
345 xkb_mod_mask_t alt_mask;
346 xkb_mod_mask_t shift_mask;
347 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400348
Jonny Lamb06959082014-08-12 14:58:27 +0200349 int32_t repeat_rate_sec;
350 int32_t repeat_rate_nsec;
351 int32_t repeat_delay_sec;
352 int32_t repeat_delay_nsec;
353
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400354 struct task repeat_task;
355 int repeat_timer_fd;
356 uint32_t repeat_sym;
357 uint32_t repeat_key;
358 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500359 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400360};
361
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500362struct output {
363 struct display *display;
364 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800365 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500366 struct rectangle allocation;
367 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600368 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200369 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500370 char *make;
371 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200372
373 display_output_handler_t destroy_handler;
374 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500375};
376
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500377struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500378 struct widget *widget;
379 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500380 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800381
382 uint32_t last_time;
383 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800384 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500385};
386
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500387struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400388 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500389 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500390 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500391 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700392 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500393 const char **entries;
394 uint32_t time;
395 int current;
396 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400397 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500398 menu_func_t func;
399};
400
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300401struct tooltip {
402 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300403 struct widget *widget;
404 char *entry;
405 struct task tooltip_task;
406 int tooltip_fd;
407 float x, y;
408};
409
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700410struct shm_pool {
411 struct wl_shm_pool *pool;
412 size_t size;
413 size_t used;
414 void *data;
415};
416
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400417enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300418 CURSOR_DEFAULT = 100,
419 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400420};
421
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200422static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423
Pekka Paalanen97777442013-05-22 10:20:05 +0300424/* #define DEBUG */
425
426#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300427
428static void
429debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
430__attribute__ ((format (printf, 4, 5)));
431
432static void
433debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
434{
435 va_list ap;
436 struct timeval tv;
437
438 gettimeofday(&tv, NULL);
439 fprintf(stderr, "%8ld.%03ld ",
440 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
441
442 if (proxy)
443 fprintf(stderr, "%s@%d ",
444 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
445
446 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
447 fprintf(stderr, "%s ", func);
448
449 va_start(ap, fmt);
450 vfprintf(stderr, fmt, ap);
451 va_end(ap);
452}
453
454#define DBG(fmt, ...) \
455 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
456
457#define DBG_OBJ(obj, fmt, ...) \
458 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
459
460#else
461
462#define DBG(...) do {} while (0)
463#define DBG_OBJ(...) do {} while (0)
464
465#endif
466
Alexander Larsson1818e312013-05-22 14:41:31 +0200467static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200468surface_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 +0200469{
470 int32_t tmp;
471
472 switch (buffer_transform) {
473 case WL_OUTPUT_TRANSFORM_90:
474 case WL_OUTPUT_TRANSFORM_270:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
476 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
477 tmp = *width;
478 *width = *height;
479 *height = tmp;
480 break;
481 default:
482 break;
483 }
484
485 *width *= buffer_scale;
486 *height *= buffer_scale;
487}
488
489static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200490buffer_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 +0200491{
492 int32_t tmp;
493
494 switch (buffer_transform) {
495 case WL_OUTPUT_TRANSFORM_90:
496 case WL_OUTPUT_TRANSFORM_270:
497 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
498 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
499 tmp = *width;
500 *width = *height;
501 *height = tmp;
502 break;
503 default:
504 break;
505 }
506
507 *width /= buffer_scale;
508 *height /= buffer_scale;
509}
510
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500511#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400512
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200513struct egl_window_surface {
514 struct toysurface base;
515 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100516 struct display *display;
517 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518 struct wl_egl_window *egl_window;
519 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520};
521
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522static struct egl_window_surface *
523to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100524{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200525 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100526}
527
528static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200529egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200530 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200531 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100532{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533 struct egl_window_surface *surface = to_egl_window_surface(base);
534
Alexander Larsson1818e312013-05-22 14:41:31 +0200535 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
536
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200537 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
538 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
539
540 return cairo_surface_reference(surface->cairo_surface);
541}
542
543static void
544egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200545 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200546 struct rectangle *server_allocation)
547{
548 struct egl_window_surface *surface = to_egl_window_surface(base);
549
550 cairo_gl_surface_swapbuffers(surface->cairo_surface);
551 wl_egl_window_get_attached_size(surface->egl_window,
552 &server_allocation->width,
553 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200554
555 buffer_to_surface_size (buffer_transform, buffer_scale,
556 &server_allocation->width,
557 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200558}
559
560static int
561egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
562{
563 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200564 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100565
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200566 device = cairo_surface_get_device(surface->cairo_surface);
567 if (!device)
568 return -1;
569
570 if (!ctx) {
571 if (device == surface->display->argb_device)
572 ctx = surface->display->argb_ctx;
573 else
574 assert(0);
575 }
576
577 cairo_device_flush(device);
578 cairo_device_acquire(device);
579 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
580 surface->egl_surface, ctx))
581 fprintf(stderr, "failed to make surface current\n");
582
583 return 0;
584}
585
586static void
587egl_window_surface_release(struct toysurface *base)
588{
589 struct egl_window_surface *surface = to_egl_window_surface(base);
590 cairo_device_t *device;
591
592 device = cairo_surface_get_device(surface->cairo_surface);
593 if (!device)
594 return;
595
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200596 if (!eglMakeCurrent(surface->display->dpy,
597 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200598 fprintf(stderr, "failed to make context current\n");
599
600 cairo_device_release(device);
601}
602
603static void
604egl_window_surface_destroy(struct toysurface *base)
605{
606 struct egl_window_surface *surface = to_egl_window_surface(base);
607 struct display *d = surface->display;
608
609 cairo_surface_destroy(surface->cairo_surface);
610 eglDestroySurface(d->dpy, surface->egl_surface);
611 wl_egl_window_destroy(surface->egl_window);
612 surface->surface = NULL;
613
614 free(surface);
615}
616
617static struct toysurface *
618egl_window_surface_create(struct display *display,
619 struct wl_surface *wl_surface,
620 uint32_t flags,
621 struct rectangle *rectangle)
622{
623 struct egl_window_surface *surface;
624
Pekka Paalanenb3627362012-11-19 17:16:00 +0200625 if (display->dpy == EGL_NO_DISPLAY)
626 return NULL;
627
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200628 surface = calloc(1, sizeof *surface);
629 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100630 return NULL;
631
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200632 surface->base.prepare = egl_window_surface_prepare;
633 surface->base.swap = egl_window_surface_swap;
634 surface->base.acquire = egl_window_surface_acquire;
635 surface->base.release = egl_window_surface_release;
636 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100637
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200638 surface->display = display;
639 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400640
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200641 surface->egl_window = wl_egl_window_create(surface->surface,
642 rectangle->width,
643 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100644
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100645 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100646 weston_platform_create_egl_surface(display->dpy,
647 display->argb_config,
648 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200650 surface->cairo_surface =
651 cairo_gl_surface_create_for_egl(display->argb_device,
652 surface->egl_surface,
653 rectangle->width,
654 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100655
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200656 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100657}
658
Pekka Paalanenb3627362012-11-19 17:16:00 +0200659#else
660
661static struct toysurface *
662egl_window_surface_create(struct display *display,
663 struct wl_surface *wl_surface,
664 uint32_t flags,
665 struct rectangle *rectangle)
666{
667 return NULL;
668}
669
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670#endif
671
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200672struct shm_surface_data {
673 struct wl_buffer *buffer;
674 struct shm_pool *pool;
675};
676
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400677struct wl_buffer *
678display_get_buffer_for_surface(struct display *display,
679 cairo_surface_t *surface)
680{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200681 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200683 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400684
685 return data->buffer;
686}
687
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500688static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700689shm_pool_destroy(struct shm_pool *pool);
690
691static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400692shm_surface_data_destroy(void *p)
693{
694 struct shm_surface_data *data = p;
695
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200696 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700697 if (data->pool)
698 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300699
700 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400701}
702
Kristian Høgsberg16626282012-04-03 11:21:27 -0400703static struct wl_shm_pool *
704make_shm_pool(struct display *display, int size, void **data)
705{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400706 struct wl_shm_pool *pool;
707 int fd;
708
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300709 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400710 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300711 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
712 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400713 return NULL;
714 }
715
716 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400717 if (*data == MAP_FAILED) {
718 fprintf(stderr, "mmap failed: %m\n");
719 close(fd);
720 return NULL;
721 }
722
723 pool = wl_shm_create_pool(display->shm, fd, size);
724
725 close(fd);
726
727 return pool;
728}
729
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700730static struct shm_pool *
731shm_pool_create(struct display *display, size_t size)
732{
733 struct shm_pool *pool = malloc(sizeof *pool);
734
735 if (!pool)
736 return NULL;
737
738 pool->pool = make_shm_pool(display, size, &pool->data);
739 if (!pool->pool) {
740 free(pool);
741 return NULL;
742 }
743
744 pool->size = size;
745 pool->used = 0;
746
747 return pool;
748}
749
750static void *
751shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
752{
753 if (pool->used + size > pool->size)
754 return NULL;
755
756 *offset = pool->used;
757 pool->used += size;
758
759 return (char *) pool->data + *offset;
760}
761
762/* destroy the pool. this does not unmap the memory though */
763static void
764shm_pool_destroy(struct shm_pool *pool)
765{
766 munmap(pool->data, pool->size);
767 wl_shm_pool_destroy(pool->pool);
768 free(pool);
769}
770
771/* Start allocating from the beginning of the pool again */
772static void
773shm_pool_reset(struct shm_pool *pool)
774{
775 pool->used = 0;
776}
777
778static int
779data_length_for_shm_surface(struct rectangle *rect)
780{
781 int stride;
782
783 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
784 rect->width);
785 return stride * rect->height;
786}
787
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500788static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700789display_create_shm_surface_from_pool(struct display *display,
790 struct rectangle *rectangle,
791 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400792{
793 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400794 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400795 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200796 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700797 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400798 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400799
800 data = malloc(sizeof *data);
801 if (data == NULL)
802 return NULL;
803
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200804 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
805 cairo_format = CAIRO_FORMAT_RGB16_565;
806 else
807 cairo_format = CAIRO_FORMAT_ARGB32;
808
809 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700810 length = stride * rectangle->height;
811 data->pool = NULL;
812 map = shm_pool_allocate(pool, length, &offset);
813
814 if (!map) {
815 free(data);
816 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400817 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400818
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400819 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200820 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821 rectangle->width,
822 rectangle->height,
823 stride);
824
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200825 cairo_surface_set_user_data(surface, &shm_surface_data_key,
826 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400827
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200828 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
829 format = WL_SHM_FORMAT_RGB565;
830 else {
831 if (flags & SURFACE_OPAQUE)
832 format = WL_SHM_FORMAT_XRGB8888;
833 else
834 format = WL_SHM_FORMAT_ARGB8888;
835 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400836
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200837 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
838 rectangle->width,
839 rectangle->height,
840 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400841
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700842 return surface;
843}
844
845static cairo_surface_t *
846display_create_shm_surface(struct display *display,
847 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200848 struct shm_pool *alternate_pool,
849 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700850{
851 struct shm_surface_data *data;
852 struct shm_pool *pool;
853 cairo_surface_t *surface;
854
Pekka Paalanen99436862012-11-19 17:15:59 +0200855 if (alternate_pool) {
856 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700857 surface = display_create_shm_surface_from_pool(display,
858 rectangle,
859 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200860 alternate_pool);
861 if (surface) {
862 data = cairo_surface_get_user_data(surface,
863 &shm_surface_data_key);
864 goto out;
865 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700866 }
867
868 pool = shm_pool_create(display,
869 data_length_for_shm_surface(rectangle));
870 if (!pool)
871 return NULL;
872
873 surface =
874 display_create_shm_surface_from_pool(display, rectangle,
875 flags, pool);
876
877 if (!surface) {
878 shm_pool_destroy(pool);
879 return NULL;
880 }
881
882 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200883 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700884 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400885
Pekka Paalanen99436862012-11-19 17:15:59 +0200886out:
887 if (data_ret)
888 *data_ret = data;
889
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890 return surface;
891}
892
nobled7b87cb02011-02-01 18:51:47 +0000893static int
894check_size(struct rectangle *rect)
895{
896 if (rect->width && rect->height)
897 return 0;
898
899 fprintf(stderr, "tried to create surface of "
900 "width: %d, height: %d\n", rect->width, rect->height);
901 return -1;
902}
903
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400904cairo_surface_t *
905display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400907 struct rectangle *rectangle,
908 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909{
nobled7b87cb02011-02-01 18:51:47 +0000910 if (check_size(rectangle) < 0)
911 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200912
913 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200914 return display_create_shm_surface(display, rectangle, flags,
915 NULL, NULL);
916}
917
Pekka Paalanena4eda732012-11-19 17:16:02 +0200918struct shm_surface_leaf {
919 cairo_surface_t *cairo_surface;
920 /* 'data' is automatically destroyed, when 'cairo_surface' is */
921 struct shm_surface_data *data;
922
923 struct shm_pool *resize_pool;
924 int busy;
925};
926
927static void
928shm_surface_leaf_release(struct shm_surface_leaf *leaf)
929{
930 if (leaf->cairo_surface)
931 cairo_surface_destroy(leaf->cairo_surface);
932 /* leaf->data already destroyed via cairo private */
933
934 if (leaf->resize_pool)
935 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200936
937 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938}
939
Pekka Paalanenaef02542013-04-25 13:57:47 +0300940#define MAX_LEAVES 3
941
Pekka Paalanen99436862012-11-19 17:15:59 +0200942struct shm_surface {
943 struct toysurface base;
944 struct display *display;
945 struct wl_surface *surface;
946 uint32_t flags;
947 int dx, dy;
948
Pekka Paalanenaef02542013-04-25 13:57:47 +0300949 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200950 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200951};
952
953static struct shm_surface *
954to_shm_surface(struct toysurface *base)
955{
956 return container_of(base, struct shm_surface, base);
957}
958
Pekka Paalanena4eda732012-11-19 17:16:02 +0200959static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300960shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
961{
962#ifdef DEBUG
963 struct shm_surface_leaf *leaf;
964 char bufs[MAX_LEAVES + 1];
965 int i;
966
967 for (i = 0; i < MAX_LEAVES; i++) {
968 leaf = &surface->leaf[i];
969
970 if (leaf->busy)
971 bufs[i] = 'b';
972 else if (leaf->cairo_surface)
973 bufs[i] = 'a';
974 else
975 bufs[i] = ' ';
976 }
977
978 bufs[MAX_LEAVES] = '\0';
979 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
980#endif
981}
982
983static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200984shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
985{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200986 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300987 struct shm_surface_leaf *leaf;
988 int i;
989 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300990
991 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200992
Pekka Paalanenaef02542013-04-25 13:57:47 +0300993 for (i = 0; i < MAX_LEAVES; i++) {
994 leaf = &surface->leaf[i];
995 if (leaf->data && leaf->data->buffer == buffer) {
996 leaf->busy = 0;
997 break;
998 }
999 }
1000 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001001
Pekka Paalanenaef02542013-04-25 13:57:47 +03001002 /* Leave one free leaf with storage, release others */
1003 free_found = 0;
1004 for (i = 0; i < MAX_LEAVES; i++) {
1005 leaf = &surface->leaf[i];
1006
1007 if (!leaf->cairo_surface || leaf->busy)
1008 continue;
1009
1010 if (!free_found)
1011 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001012 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001013 shm_surface_leaf_release(leaf);
1014 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001015
Pekka Paalanen97777442013-05-22 10:20:05 +03001016 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001017}
1018
1019static const struct wl_buffer_listener shm_surface_buffer_listener = {
1020 shm_surface_buffer_release
1021};
1022
Pekka Paalanen99436862012-11-19 17:15:59 +02001023static cairo_surface_t *
1024shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001025 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001026 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001027{
Pekka Paalanenec076692012-11-30 13:37:27 +02001028 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001029 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001030 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001031 struct shm_surface_leaf *leaf = NULL;
1032 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001033
1034 surface->dx = dx;
1035 surface->dy = dy;
1036
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001037 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001038 for (i = 0; i < MAX_LEAVES; i++) {
1039 if (surface->leaf[i].busy)
1040 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001041
Pekka Paalanenaef02542013-04-25 13:57:47 +03001042 if (!leaf || surface->leaf[i].cairo_surface)
1043 leaf = &surface->leaf[i];
1044 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001045 DBG_OBJ(surface->surface, "pick leaf %d\n",
1046 (int)(leaf - &surface->leaf[0]));
1047
Pekka Paalanenaef02542013-04-25 13:57:47 +03001048 if (!leaf) {
1049 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001050 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001051 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001052 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001053 }
1054
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055 if (!resize_hint && leaf->resize_pool) {
1056 cairo_surface_destroy(leaf->cairo_surface);
1057 leaf->cairo_surface = NULL;
1058 shm_pool_destroy(leaf->resize_pool);
1059 leaf->resize_pool = NULL;
1060 }
1061
Alexander Larsson1818e312013-05-22 14:41:31 +02001062 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1063
Pekka Paalanena4eda732012-11-19 17:16:02 +02001064 if (leaf->cairo_surface &&
1065 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1066 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001067 goto out;
1068
Pekka Paalanena4eda732012-11-19 17:16:02 +02001069 if (leaf->cairo_surface)
1070 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001071
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001072#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001073 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001074 /* Create a big pool to allocate from, while continuously
1075 * resizing. Mmapping a new pool in the server
1076 * is relatively expensive, so reusing a pool performs
1077 * better, but may temporarily reserve unneeded memory.
1078 */
1079 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001080 leaf->resize_pool = shm_pool_create(surface->display,
1081 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001082 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001083#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001084
Alexander Larsson1818e312013-05-22 14:41:31 +02001085 rect.width = width;
1086 rect.height = height;
1087
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001089 display_create_shm_surface(surface->display, &rect,
1090 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001091 leaf->resize_pool,
1092 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001093 if (!leaf->cairo_surface)
1094 return NULL;
1095
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001097 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001098
1099out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 surface->current = leaf;
1101
1102 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001103}
1104
1105static void
1106shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001107 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001108 struct rectangle *server_allocation)
1109{
1110 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001112
1113 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001114 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001115 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001117
Alexander Larsson1818e312013-05-22 14:41:31 +02001118 buffer_to_surface_size (buffer_transform, buffer_scale,
1119 &server_allocation->width,
1120 &server_allocation->height);
1121
Pekka Paalanena4eda732012-11-19 17:16:02 +02001122 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001123 surface->dx, surface->dy);
1124 wl_surface_damage(surface->surface, 0, 0,
1125 server_allocation->width, server_allocation->height);
1126 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127
Pekka Paalanen71233882013-04-25 13:57:53 +03001128 DBG_OBJ(surface->surface, "leaf %d busy\n",
1129 (int)(leaf - &surface->leaf[0]));
1130
Pekka Paalanena4eda732012-11-19 17:16:02 +02001131 leaf->busy = 1;
1132 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001133}
1134
1135static int
1136shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1137{
1138 return -1;
1139}
1140
1141static void
1142shm_surface_release(struct toysurface *base)
1143{
1144}
1145
1146static void
1147shm_surface_destroy(struct toysurface *base)
1148{
1149 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001150 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001151
Pekka Paalanenaef02542013-04-25 13:57:47 +03001152 for (i = 0; i < MAX_LEAVES; i++)
1153 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001154
1155 free(surface);
1156}
1157
1158static struct toysurface *
1159shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1160 uint32_t flags, struct rectangle *rectangle)
1161{
1162 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001163 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001164
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001165 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001166 surface->base.prepare = shm_surface_prepare;
1167 surface->base.swap = shm_surface_swap;
1168 surface->base.acquire = shm_surface_acquire;
1169 surface->base.release = shm_surface_release;
1170 surface->base.destroy = shm_surface_destroy;
1171
1172 surface->display = display;
1173 surface->surface = wl_surface;
1174 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001175
1176 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001177}
1178
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001179/*
1180 * The following correspondences between file names and cursors was copied
1181 * from: https://bugs.kde.org/attachment.cgi?id=67313
1182 */
1183
1184static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001185 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001186 "sw-resize",
1187 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001188};
1189
1190static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001191 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001192 "se-resize",
1193 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001194};
1195
1196static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001197 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001198 "s-resize",
1199 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001200};
1201
1202static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001203 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001204 "closedhand",
1205 "208530c400c041818281048008011002"
1206};
1207
1208static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001209 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001210 "default",
1211 "top_left_arrow",
1212 "left-arrow"
1213};
1214
1215static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001216 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001217 "w-resize",
1218 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001219};
1220
1221static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001222 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001223 "e-resize",
1224 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001225};
1226
1227static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001228 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001229 "nw-resize",
1230 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001231};
1232
1233static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001234 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001235 "ne-resize",
1236 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001237};
1238
1239static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001240 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001241 "n-resize",
1242 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001243};
1244
1245static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001246 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001247 "ibeam",
1248 "text"
1249};
1250
1251static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001252 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001253 "pointer",
1254 "pointing_hand",
1255 "e29285e634086352946a0e7090d73106"
1256};
1257
1258static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001259 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001260 "wait",
1261 "0426c94ea35c87780ff01dc239897213"
1262};
1263
1264struct cursor_alternatives {
1265 const char **names;
1266 size_t count;
1267};
1268
1269static const struct cursor_alternatives cursors[] = {
1270 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1271 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1272 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1273 {grabbings, ARRAY_LENGTH(grabbings)},
1274 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1275 {left_sides, ARRAY_LENGTH(left_sides)},
1276 {right_sides, ARRAY_LENGTH(right_sides)},
1277 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1278 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1279 {top_sides, ARRAY_LENGTH(top_sides)},
1280 {xterms, ARRAY_LENGTH(xterms)},
1281 {hand1s, ARRAY_LENGTH(hand1s)},
1282 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001283};
1284
1285static void
1286create_cursors(struct display *display)
1287{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001288 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001289 struct weston_config *config;
1290 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001291 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001292 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001293 unsigned int i, j;
1294 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001295
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001296 config_file = weston_config_get_name_from_env();
1297 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001298 s = weston_config_get_section(config, "shell", NULL, NULL);
1299 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1300 weston_config_section_get_int(s, "cursor-size", &size, 32);
1301 weston_config_destroy(config);
1302
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001303 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001304 if (!display->cursor_theme) {
1305 fprintf(stderr, "could not load theme '%s'\n", theme);
1306 return;
1307 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001308 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001309 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001310 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001311
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001312 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001313 cursor = NULL;
1314 for (j = 0; !cursor && j < cursors[i].count; ++j)
1315 cursor = wl_cursor_theme_get_cursor(
1316 display->cursor_theme, cursors[i].names[j]);
1317
1318 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001319 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001320 cursors[i].names[0]);
1321
1322 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001323 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001324}
1325
1326static void
1327destroy_cursors(struct display *display)
1328{
1329 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001330 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001331}
1332
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001333struct wl_cursor_image *
1334display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001335{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001336 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001337
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001338 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001339}
1340
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001341static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001342surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001343{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001344 if (!surface->cairo_surface)
1345 return;
1346
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001347 if (surface->opaque_region) {
1348 wl_surface_set_opaque_region(surface->surface,
1349 surface->opaque_region);
1350 wl_region_destroy(surface->opaque_region);
1351 surface->opaque_region = NULL;
1352 }
1353
1354 if (surface->input_region) {
1355 wl_surface_set_input_region(surface->surface,
1356 surface->input_region);
1357 wl_region_destroy(surface->input_region);
1358 surface->input_region = NULL;
1359 }
1360
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001361 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001362 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001363 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001364
Pekka Paalanen89dee002013-02-13 16:17:20 +02001365 cairo_surface_destroy(surface->cairo_surface);
1366 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001367}
1368
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001369int
1370window_has_focus(struct window *window)
1371{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001372 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001373}
1374
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001375static void
1376window_close(struct window *window)
1377{
1378 if (window->close_handler)
1379 window->close_handler(window->user_data);
1380 else
1381 display_exit(window->display);
1382}
1383
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001384struct display *
1385window_get_display(struct window *window)
1386{
1387 return window->display;
1388}
1389
Pekka Paalanen89dee002013-02-13 16:17:20 +02001390static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001391handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1392 int32_t width, int32_t height)
1393{
1394 struct window *window = data;
1395
1396 window_schedule_resize(window, width, height);
1397}
1398
1399static const struct ivi_surface_listener ivi_surface_listener = {
1400 handle_ivi_surface_configure,
1401};
1402
1403static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001404surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001405{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001406 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001407 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001408
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409 if (!surface->toysurface && display->dpy &&
1410 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001411 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001412 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001413 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001414 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001415 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001416 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001417
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001419 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001420 surface->surface,
1421 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001422
Pekka Paalanen89dee002013-02-13 16:17:20 +02001423 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001424 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001425 allocation.width, allocation.height, flags,
1426 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001427}
1428
1429static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001430window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001431{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001432 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001433 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001434
Pekka Paalanenec076692012-11-30 13:37:27 +02001435 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001436 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001437
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001438 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1439 flags |= SURFACE_HINT_RGB565;
1440
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001441 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001442}
1443
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001444int
1445window_get_buffer_transform(struct window *window)
1446{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001447 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001448}
1449
1450void
1451window_set_buffer_transform(struct window *window,
1452 enum wl_output_transform transform)
1453{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001454 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001455 wl_surface_set_buffer_transform(window->main_surface->surface,
1456 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001457}
1458
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001459void
1460window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001461 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001462{
1463 window->main_surface->buffer_scale = scale;
1464 wl_surface_set_buffer_scale(window->main_surface->surface,
1465 scale);
1466}
1467
1468uint32_t
1469window_get_buffer_scale(struct window *window)
1470{
1471 return window->main_surface->buffer_scale;
1472}
1473
Alexander Larssond68f5232013-05-22 14:41:33 +02001474uint32_t
1475window_get_output_scale(struct window *window)
1476{
1477 struct window_output *window_output;
1478 struct window_output *window_output_tmp;
1479 int scale = 1;
1480
1481 wl_list_for_each_safe(window_output, window_output_tmp,
1482 &window->window_output_list, link) {
1483 if (window_output->output->scale > scale)
1484 scale = window_output->output->scale;
1485 }
1486
1487 return scale;
1488}
1489
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001490static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001491
Pekka Paalanen4e373742013-02-13 16:17:13 +02001492static void
1493surface_destroy(struct surface *surface)
1494{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001495 if (surface->frame_cb)
1496 wl_callback_destroy(surface->frame_cb);
1497
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001498 if (surface->input_region)
1499 wl_region_destroy(surface->input_region);
1500
1501 if (surface->opaque_region)
1502 wl_region_destroy(surface->opaque_region);
1503
Pekka Paalanen35e82632013-04-25 13:57:48 +03001504 if (surface->subsurface)
1505 wl_subsurface_destroy(surface->subsurface);
1506
Pekka Paalanen4e373742013-02-13 16:17:13 +02001507 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001508
1509 if (surface->toysurface)
1510 surface->toysurface->destroy(surface->toysurface);
1511
Pekka Paalanen35e82632013-04-25 13:57:48 +03001512 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001513 free(surface);
1514}
1515
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001516void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001517window_destroy(struct window *window)
1518{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001519 struct display *display = window->display;
1520 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001521 struct window_output *window_output;
1522 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001523
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001524 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001525
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001526 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001527 if (input->touch_focus == window)
1528 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001529 if (input->pointer_focus == window)
1530 input->pointer_focus = NULL;
1531 if (input->keyboard_focus == window)
1532 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001533 if (input->focus_widget &&
1534 input->focus_widget->window == window)
1535 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001536 }
1537
Rob Bradford7507b572012-05-15 17:55:34 +01001538 wl_list_for_each_safe(window_output, window_output_tmp,
1539 &window->window_output_list, link) {
1540 free (window_output);
1541 }
1542
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001543 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001544 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001545
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001546 if (window->xdg_surface)
1547 xdg_surface_destroy(window->xdg_surface);
1548 if (window->xdg_popup)
1549 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001550
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001551 if (window->ivi_surface)
1552 ivi_surface_destroy(window->ivi_surface);
1553
Pekka Paalanen4e373742013-02-13 16:17:13 +02001554 surface_destroy(window->main_surface);
1555
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001556 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001557
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001558 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001559 free(window);
1560}
1561
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001562static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001563widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001564{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001565 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001566
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001567 wl_list_for_each(child, &widget->child_list, link) {
1568 target = widget_find_widget(child, x, y);
1569 if (target)
1570 return target;
1571 }
1572
1573 if (widget->allocation.x <= x &&
1574 x < widget->allocation.x + widget->allocation.width &&
1575 widget->allocation.y <= y &&
1576 y < widget->allocation.y + widget->allocation.height) {
1577 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001578 }
1579
1580 return NULL;
1581}
1582
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001583static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001584window_find_widget(struct window *window, int32_t x, int32_t y)
1585{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001586 struct surface *surface;
1587 struct widget *widget;
1588
1589 wl_list_for_each(surface, &window->subsurface_list, link) {
1590 widget = widget_find_widget(surface->widget, x, y);
1591 if (widget)
1592 return widget;
1593 }
1594
1595 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001596}
1597
1598static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001599widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001600{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001601 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001602
Peter Huttererf3d62272013-08-08 11:57:05 +10001603 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001604 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001605 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001606 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001607 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001608 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001609 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001610 widget->tooltip = NULL;
1611 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001612 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001613 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001614
1615 return widget;
1616}
1617
1618struct widget *
1619window_add_widget(struct window *window, void *data)
1620{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001621 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001622
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001623 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001624 wl_list_init(&widget->link);
1625 window->main_surface->widget = widget;
1626
1627 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001628}
1629
1630struct widget *
1631widget_add_widget(struct widget *parent, void *data)
1632{
1633 struct widget *widget;
1634
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001635 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001636 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001637
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001638 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001639}
1640
1641void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001642widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001643{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001644 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001645 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001646 struct input *input;
1647
Pekka Paalanen35e82632013-04-25 13:57:48 +03001648 /* Destroy the sub-surface along with the root widget */
1649 if (surface->widget == widget && surface->subsurface)
1650 surface_destroy(widget->surface);
1651
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001652 if (widget->tooltip)
1653 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001654
Pekka Paalanene156fb62012-01-19 13:51:38 +02001655 wl_list_for_each(input, &display->input_list, link) {
1656 if (input->focus_widget == widget)
1657 input->focus_widget = NULL;
1658 }
1659
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001660 wl_list_remove(&widget->link);
1661 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001662}
1663
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001665widget_set_default_cursor(struct widget *widget, int cursor)
1666{
1667 widget->default_cursor = cursor;
1668}
1669
1670void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001671widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001672{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001673 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001674}
1675
1676void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001677widget_set_size(struct widget *widget, int32_t width, int32_t height)
1678{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001679 widget->allocation.width = width;
1680 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001681}
1682
1683void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001684widget_set_allocation(struct widget *widget,
1685 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001687 widget->allocation.x = x;
1688 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001689 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690}
1691
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001692void
1693widget_set_transparent(struct widget *widget, int transparent)
1694{
1695 widget->opaque = !transparent;
1696}
1697
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001698void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001699widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001700{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001701 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001702}
1703
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001704static cairo_surface_t *
1705widget_get_cairo_surface(struct widget *widget)
1706{
1707 struct surface *surface = widget->surface;
1708 struct window *window = widget->window;
1709
Neil Roberts97b747c2013-12-19 16:17:12 +00001710 assert(widget->use_cairo);
1711
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001712 if (!surface->cairo_surface) {
1713 if (surface == window->main_surface)
1714 window_create_main_surface(window);
1715 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001716 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001717 }
1718
1719 return surface->cairo_surface;
1720}
1721
Alexander Larsson15901f02013-05-22 14:41:25 +02001722static void
1723widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1724{
1725 struct surface *surface = widget->surface;
1726 double angle;
1727 cairo_matrix_t m;
1728 enum wl_output_transform transform;
1729 int surface_width, surface_height;
1730 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001731 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001732
1733 surface_width = surface->allocation.width;
1734 surface_height = surface->allocation.height;
1735
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001736 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001737 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001738
Alexander Larsson15901f02013-05-22 14:41:25 +02001739 switch (transform) {
1740 case WL_OUTPUT_TRANSFORM_FLIPPED:
1741 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1742 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1743 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1744 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1745 break;
1746 default:
1747 cairo_matrix_init_identity(&m);
1748 break;
1749 }
1750
1751 switch (transform) {
1752 case WL_OUTPUT_TRANSFORM_NORMAL:
1753 default:
1754 angle = 0;
1755 translate_x = 0;
1756 translate_y = 0;
1757 break;
1758 case WL_OUTPUT_TRANSFORM_FLIPPED:
1759 angle = 0;
1760 translate_x = surface_width;
1761 translate_y = 0;
1762 break;
1763 case WL_OUTPUT_TRANSFORM_90:
1764 angle = M_PI_2;
1765 translate_x = surface_height;
1766 translate_y = 0;
1767 break;
1768 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1769 angle = M_PI_2;
1770 translate_x = surface_height;
1771 translate_y = surface_width;
1772 break;
1773 case WL_OUTPUT_TRANSFORM_180:
1774 angle = M_PI;
1775 translate_x = surface_width;
1776 translate_y = surface_height;
1777 break;
1778 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1779 angle = M_PI;
1780 translate_x = 0;
1781 translate_y = surface_height;
1782 break;
1783 case WL_OUTPUT_TRANSFORM_270:
1784 angle = M_PI + M_PI_2;
1785 translate_x = 0;
1786 translate_y = surface_width;
1787 break;
1788 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1789 angle = M_PI + M_PI_2;
1790 translate_x = 0;
1791 translate_y = 0;
1792 break;
1793 }
1794
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001795 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001796 cairo_translate(cr, translate_x, translate_y);
1797 cairo_rotate(cr, angle);
1798 cairo_transform(cr, &m);
1799}
1800
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001801cairo_t *
1802widget_cairo_create(struct widget *widget)
1803{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001804 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001805 cairo_surface_t *cairo_surface;
1806 cairo_t *cr;
1807
1808 cairo_surface = widget_get_cairo_surface(widget);
1809 cr = cairo_create(cairo_surface);
1810
Alexander Larsson15901f02013-05-22 14:41:25 +02001811 widget_cairo_update_transform(widget, cr);
1812
Pekka Paalanen35e82632013-04-25 13:57:48 +03001813 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1814
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001815 return cr;
1816}
1817
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001818struct wl_surface *
1819widget_get_wl_surface(struct widget *widget)
1820{
1821 return widget->surface->surface;
1822}
1823
Neil Roberts5e10a042013-09-09 00:40:17 +01001824struct wl_subsurface *
1825widget_get_wl_subsurface(struct widget *widget)
1826{
1827 return widget->surface->subsurface;
1828}
1829
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001830uint32_t
1831widget_get_last_time(struct widget *widget)
1832{
1833 return widget->surface->last_time;
1834}
1835
1836void
1837widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1838{
1839 struct wl_compositor *comp = widget->window->display->compositor;
1840 struct surface *surface = widget->surface;
1841
1842 if (!surface->input_region)
1843 surface->input_region = wl_compositor_create_region(comp);
1844
1845 if (rect) {
1846 wl_region_add(surface->input_region,
1847 rect->x, rect->y, rect->width, rect->height);
1848 }
1849}
1850
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001851void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001852widget_set_resize_handler(struct widget *widget,
1853 widget_resize_handler_t handler)
1854{
1855 widget->resize_handler = handler;
1856}
1857
1858void
1859widget_set_redraw_handler(struct widget *widget,
1860 widget_redraw_handler_t handler)
1861{
1862 widget->redraw_handler = handler;
1863}
1864
1865void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001866widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001867{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001868 widget->enter_handler = handler;
1869}
1870
1871void
1872widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1873{
1874 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001875}
1876
1877void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001878widget_set_motion_handler(struct widget *widget,
1879 widget_motion_handler_t handler)
1880{
1881 widget->motion_handler = handler;
1882}
1883
1884void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001885widget_set_button_handler(struct widget *widget,
1886 widget_button_handler_t handler)
1887{
1888 widget->button_handler = handler;
1889}
1890
1891void
Rusty Lynch041815a2013-08-08 21:20:38 -07001892widget_set_touch_up_handler(struct widget *widget,
1893 widget_touch_up_handler_t handler)
1894{
1895 widget->touch_up_handler = handler;
1896}
1897
1898void
1899widget_set_touch_down_handler(struct widget *widget,
1900 widget_touch_down_handler_t handler)
1901{
1902 widget->touch_down_handler = handler;
1903}
1904
1905void
1906widget_set_touch_motion_handler(struct widget *widget,
1907 widget_touch_motion_handler_t handler)
1908{
1909 widget->touch_motion_handler = handler;
1910}
1911
1912void
1913widget_set_touch_frame_handler(struct widget *widget,
1914 widget_touch_frame_handler_t handler)
1915{
1916 widget->touch_frame_handler = handler;
1917}
1918
1919void
1920widget_set_touch_cancel_handler(struct widget *widget,
1921 widget_touch_cancel_handler_t handler)
1922{
1923 widget->touch_cancel_handler = handler;
1924}
1925
1926void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001927widget_set_axis_handler(struct widget *widget,
1928 widget_axis_handler_t handler)
1929{
1930 widget->axis_handler = handler;
1931}
1932
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001933static void
1934window_schedule_redraw_task(struct window *window);
1935
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001936void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001937widget_schedule_redraw(struct widget *widget)
1938{
Pekka Paalanen71233882013-04-25 13:57:53 +03001939 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001940 widget->surface->redraw_needed = 1;
1941 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001942}
1943
Neil Roberts97b747c2013-12-19 16:17:12 +00001944void
1945widget_set_use_cairo(struct widget *widget,
1946 int use_cairo)
1947{
1948 widget->use_cairo = use_cairo;
1949}
1950
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001951cairo_surface_t *
1952window_get_surface(struct window *window)
1953{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001954 cairo_surface_t *cairo_surface;
1955
1956 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1957
1958 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001959}
1960
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001961struct wl_surface *
1962window_get_wl_surface(struct window *window)
1963{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001964 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001965}
1966
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001967static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001968tooltip_redraw_handler(struct widget *widget, void *data)
1969{
1970 cairo_t *cr;
1971 const int32_t r = 3;
1972 struct tooltip *tooltip = data;
1973 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001974
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001975 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001976 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001977 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1978 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1979 cairo_paint(cr);
1980
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001981 width = widget->allocation.width;
1982 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001983 rounded_rect(cr, 0, 0, width, height, r);
1984
1985 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1986 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1987 cairo_fill(cr);
1988
1989 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1990 cairo_move_to(cr, 10, 16);
1991 cairo_show_text(cr, tooltip->entry);
1992 cairo_destroy(cr);
1993}
1994
1995static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001996get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001997{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001998 cairo_t *cr;
1999 cairo_text_extents_t extents;
2000
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002001 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002002 * created yet, and parent does not have a valid surface
2003 * outside repaint, either.
2004 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002005 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002006 cairo_text_extents(cr, tooltip->entry, &extents);
2007 cairo_destroy(cr);
2008
2009 return extents;
2010}
2011
2012static int
2013window_create_tooltip(struct tooltip *tooltip)
2014{
2015 struct widget *parent = tooltip->parent;
2016 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002017 const int offset_y = 27;
2018 const int margin = 3;
2019 cairo_text_extents_t extents;
2020
2021 if (tooltip->widget)
2022 return 0;
2023
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002024 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002025
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002026 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002027 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002028 widget_set_allocation(tooltip->widget,
2029 tooltip->x, tooltip->y + offset_y,
2030 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002031
2032 return 0;
2033}
2034
2035void
2036widget_destroy_tooltip(struct widget *parent)
2037{
2038 struct tooltip *tooltip = parent->tooltip;
2039
2040 parent->tooltip_count = 0;
2041 if (!tooltip)
2042 return;
2043
2044 if (tooltip->widget) {
2045 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002046 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002047 }
2048
2049 close(tooltip->tooltip_fd);
2050 free(tooltip->entry);
2051 free(tooltip);
2052 parent->tooltip = NULL;
2053}
2054
2055static void
2056tooltip_func(struct task *task, uint32_t events)
2057{
2058 struct tooltip *tooltip =
2059 container_of(task, struct tooltip, tooltip_task);
2060 uint64_t exp;
2061
Martin Olsson8df662a2012-07-08 03:03:47 +02002062 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2063 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002064 window_create_tooltip(tooltip);
2065}
2066
2067#define TOOLTIP_TIMEOUT 500
2068static int
2069tooltip_timer_reset(struct tooltip *tooltip)
2070{
2071 struct itimerspec its;
2072
2073 its.it_interval.tv_sec = 0;
2074 its.it_interval.tv_nsec = 0;
2075 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2076 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2077 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2078 fprintf(stderr, "could not set timerfd\n: %m");
2079 return -1;
2080 }
2081
2082 return 0;
2083}
2084
2085int
2086widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2087{
2088 struct tooltip *tooltip = parent->tooltip;
2089
2090 parent->tooltip_count++;
2091 if (tooltip) {
2092 tooltip->x = x;
2093 tooltip->y = y;
2094 tooltip_timer_reset(tooltip);
2095 return 0;
2096 }
2097
2098 /* the handler might be triggered too fast via input device motion, so
2099 * we need this check here to make sure tooltip is fully initialized */
2100 if (parent->tooltip_count > 1)
2101 return 0;
2102
2103 tooltip = malloc(sizeof *tooltip);
2104 if (!tooltip)
2105 return -1;
2106
2107 parent->tooltip = tooltip;
2108 tooltip->parent = parent;
2109 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002110 tooltip->x = x;
2111 tooltip->y = y;
2112 tooltip->entry = strdup(entry);
2113 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2114 if (tooltip->tooltip_fd < 0) {
2115 fprintf(stderr, "could not create timerfd\n: %m");
2116 return -1;
2117 }
2118
2119 tooltip->tooltip_task.run = tooltip_func;
2120 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2121 EPOLLIN, &tooltip->tooltip_task);
2122 tooltip_timer_reset(tooltip);
2123
2124 return 0;
2125}
2126
2127static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002128frame_resize_handler(struct widget *widget,
2129 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002130{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002131 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002132 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002133 struct rectangle interior;
2134 struct rectangle input;
2135 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002136
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002137 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002138 interior.x = 0;
2139 interior.y = 0;
2140 interior.width = width;
2141 interior.height = height;
2142 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002143 frame_resize(frame->frame, width, height);
2144 frame_interior(frame->frame, &interior.x, &interior.y,
2145 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002146 }
2147
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002148 widget_set_allocation(child, interior.x, interior.y,
2149 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002150
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002151 if (child->resize_handler) {
2152 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002153 child->user_data);
2154
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002155 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002156 width = child->allocation.width;
2157 height = child->allocation.height;
2158 } else {
2159 frame_resize_inside(frame->frame,
2160 child->allocation.width,
2161 child->allocation.height);
2162 width = frame_width(frame->frame);
2163 height = frame_height(frame->frame);
2164 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002165 }
2166
Scott Moreauf7e498c2012-05-14 11:39:29 -06002167 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002168
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002169 widget->surface->input_region =
2170 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002171 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 frame_input_rect(frame->frame, &input.x, &input.y,
2173 &input.width, &input.height);
2174 wl_region_add(widget->surface->input_region,
2175 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002176 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002177 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002178 }
2179
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002180 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002181
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002182 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002183 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002184 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2185 &opaque.width, &opaque.height);
2186
2187 wl_region_add(widget->surface->opaque_region,
2188 opaque.x, opaque.y,
2189 opaque.width, opaque.height);
2190 } else {
2191 wl_region_add(widget->surface->opaque_region,
2192 0, 0, width, height);
2193 }
Martin Minarik1998b152012-05-10 02:04:35 +02002194 }
2195
Martin Minarik1998b152012-05-10 02:04:35 +02002196
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002198}
2199
2200static void
2201frame_redraw_handler(struct widget *widget, void *data)
2202{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002203 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002204 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002205 struct window *window = widget->window;
2206
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002207 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002208 return;
2209
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002210 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002212 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002213
2214 cairo_destroy(cr);
2215}
2216
2217static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218frame_get_pointer_image_for_location(struct window_frame *frame,
2219 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002220{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002221 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002222
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002223 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002224 return CURSOR_LEFT_PTR;
2225
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002226 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002227 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002228 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002229 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002230 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002231 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002232 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002233 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002234 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002235 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002236 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002237 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002238 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002239 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002240 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002241 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002242 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002243 case THEME_LOCATION_EXTERIOR:
2244 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002245 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002246 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002247 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002248}
2249
Pekka Paalanen26237862014-09-10 15:10:30 +03002250static void
2251frame_menu_func(void *data, struct input *input, int index)
2252{
2253 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002254
2255 switch (index) {
2256 case 0: /* close */
2257 window_close(window);
2258 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002259 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002260 /* we don't have a way to get out of fullscreen for now */
2261 if (window->fullscreen_handler)
2262 window->fullscreen_handler(window, window->user_data);
2263 break;
2264 }
2265}
2266
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002267void
2268window_show_frame_menu(struct window *window,
2269 struct input *input, uint32_t time)
2270{
2271 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002272 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002273
Pekka Paalanen26237862014-09-10 15:10:30 +03002274 static const char *entries[] = {
2275 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002276 "Fullscreen"
2277 };
2278
2279 if (window->fullscreen_handler)
2280 count = ARRAY_LENGTH(entries);
2281 else
2282 count = ARRAY_LENGTH(entries) - 1;
2283
2284 input_get_position(input, &x, &y);
2285 window_show_menu(window->display, input, time, window,
2286 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002287}
2288
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002289static int
2290frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002291 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002292{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002293 struct window_frame *frame = data;
2294 enum theme_location location;
2295
2296 location = frame_pointer_enter(frame->frame, input, x, y);
2297 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2298 widget_schedule_redraw(frame->widget);
2299
2300 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002301}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002302
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002303static int
2304frame_motion_handler(struct widget *widget,
2305 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002306 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002307{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002308 struct window_frame *frame = data;
2309 enum theme_location location;
2310
2311 location = frame_pointer_motion(frame->frame, input, x, y);
2312 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2313 widget_schedule_redraw(frame->widget);
2314
2315 return frame_get_pointer_image_for_location(data, location);
2316}
2317
2318static void
2319frame_leave_handler(struct widget *widget,
2320 struct input *input, void *data)
2321{
2322 struct window_frame *frame = data;
2323
2324 frame_pointer_leave(frame->frame, input);
2325 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2326 widget_schedule_redraw(frame->widget);
2327}
2328
2329static void
2330frame_handle_status(struct window_frame *frame, struct input *input,
2331 uint32_t time, enum theme_location location)
2332{
2333 struct window *window = frame->widget->window;
2334 uint32_t status;
2335
2336 status = frame_status(frame->frame);
2337 if (status & FRAME_STATUS_REPAINT)
2338 widget_schedule_redraw(frame->widget);
2339
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002340 if (status & FRAME_STATUS_MINIMIZE) {
2341 window_set_minimized(window);
2342 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2343 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002344
2345 if (status & FRAME_STATUS_MENU) {
2346 window_show_frame_menu(window, input, time);
2347 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2348 }
2349
2350 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002351 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002352 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2353 }
2354
2355 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002356 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002357 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002358 }
2359
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002360 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002361 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002362 xdg_surface_move(window->xdg_surface,
2363 input_get_seat(input),
2364 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002365
2366 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2367 }
2368
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002369 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002370 input_ungrab(input);
2371
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002372 xdg_surface_resize(window->xdg_surface,
2373 input_get_seat(input),
2374 window->display->serial,
2375 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002376
2377 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2378 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002379}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002380
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002381#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002382static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002383frame_button_handler(struct widget *widget,
2384 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002385 uint32_t button, enum wl_pointer_button_state state,
2386 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002387
2388{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002389 struct window_frame *frame = data;
2390 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002391
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002392 frame->double_click = 0;
2393 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2394 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2395 frame->double_click = 1;
2396 frame->did_double = 1;
2397 } else
2398 frame->did_double = 0;
2399
2400 frame->last_time = time;
2401 } else if (frame->did_double == 1) {
2402 frame->double_click = 1;
2403 frame->did_double = 0;
2404 }
2405
2406 if (frame->double_click)
2407 location = frame_double_click(frame->frame, input,
2408 button, state);
2409 else
2410 location = frame_pointer_button(frame->frame, input,
2411 button, state);
2412
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002413 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002414}
2415
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002416static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002417frame_touch_down_handler(struct widget *widget, struct input *input,
2418 uint32_t serial, uint32_t time, int32_t id,
2419 float x, float y, void *data)
2420{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002421 struct window_frame *frame = data;
2422
Xiong Zhang382de462014-06-12 11:06:26 +08002423 frame->double_click = 0;
2424 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2425 frame->last_id == id) {
2426 frame->double_click = 1;
2427 frame->did_double = 1;
2428 frame->double_id = id;
2429 } else
2430 frame->did_double = 0;
2431
2432 frame->last_time = time;
2433 frame->last_id = id;
2434
2435 if (frame->double_click)
2436 frame_double_touch_down(frame->frame, input, id, x, y);
2437 else
2438 frame_touch_down(frame->frame, input, id, x, y);
2439
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002440 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2441}
2442
2443static void
2444frame_touch_up_handler(struct widget *widget,
2445 struct input *input, uint32_t serial, uint32_t time,
2446 int32_t id, void *data)
2447{
2448 struct window_frame *frame = data;
2449
Xiong Zhang382de462014-06-12 11:06:26 +08002450 if (frame->double_id == id && frame->did_double) {
2451 frame->did_double = 0;
2452 frame->double_id = 0;
2453 frame_double_touch_up(frame->frame, input, id);
2454 } else
2455 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002456 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002457}
2458
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002459struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002460window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002461{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002462 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002463 uint32_t buttons;
2464
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002465 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002466 buttons = FRAME_BUTTON_NONE;
2467 } else {
2468 buttons = FRAME_BUTTON_ALL;
2469 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002470
Peter Huttererf3d62272013-08-08 11:57:05 +10002471 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002472 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002473 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002474
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002475 frame->widget = window_add_widget(window, frame);
2476 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002477
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002478 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2479 widget_set_resize_handler(frame->widget, frame_resize_handler);
2480 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002481 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002482 widget_set_motion_handler(frame->widget, frame_motion_handler);
2483 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002484 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002485 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002486
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002487 window->frame = frame;
2488
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002489 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002490}
2491
Kristian Høgsberga1627922012-06-20 17:30:03 -04002492void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002493window_frame_set_child_size(struct widget *widget, int child_width,
2494 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002495{
2496 struct display *display = widget->window->display;
2497 struct theme *t = display->theme;
2498 int decoration_width, decoration_height;
2499 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002500 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002501
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002502 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002503 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002504 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002505 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002506
2507 width = child_width + decoration_width;
2508 height = child_height + decoration_height;
2509 } else {
2510 width = child_width;
2511 height = child_height;
2512 }
2513
2514 window_schedule_resize(widget->window, width, height);
2515}
2516
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002517static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002518window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002519{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002520 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002521
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002522 /* frame->child must be destroyed by the application */
2523 widget_destroy(frame->widget);
2524 free(frame);
2525}
2526
2527static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002528input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002529 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002530{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002531 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002532 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002533
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002534 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002535 return;
2536
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002537 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002538 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002539 widget = old;
2540 if (input->grab)
2541 widget = input->grab;
2542 if (widget->leave_handler)
2543 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002544 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002545 }
2546
2547 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002548 widget = focus;
2549 if (input->grab)
2550 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002551 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002552 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002553 cursor = widget->enter_handler(focus, input, x, y,
2554 widget->user_data);
2555 else
2556 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002557
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002558 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002559 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002560}
2561
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002562void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002563touch_grab(struct input *input, int32_t touch_id)
2564{
2565 input->touch_grab = 1;
2566 input->touch_grab_id = touch_id;
2567}
2568
2569void
2570touch_ungrab(struct input *input)
2571{
2572 struct touch_point *tp, *tmp;
2573
2574 input->touch_grab = 0;
2575
2576 wl_list_for_each_safe(tp, tmp,
2577 &input->touch_point_list, link) {
2578 if (tp->id != input->touch_grab_id)
2579 continue;
2580 wl_list_remove(&tp->link);
2581 free(tp);
2582
2583 return;
2584 }
2585}
2586
2587void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002588input_grab(struct input *input, struct widget *widget, uint32_t button)
2589{
2590 input->grab = widget;
2591 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002592
2593 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002594}
2595
2596void
2597input_ungrab(struct input *input)
2598{
2599 struct widget *widget;
2600
2601 input->grab = NULL;
2602 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002603 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002604 input->sx, input->sy);
2605 input_set_focus_widget(input, widget, input->sx, input->sy);
2606 }
2607}
2608
2609static void
Derek Foreman118a4292015-04-22 17:23:35 -05002610cursor_delay_timer_reset(struct input *input, uint32_t duration)
2611{
2612 struct itimerspec its;
2613
2614 if (!duration)
2615 input->cursor_timer_running = false;
2616 else
2617 input->cursor_timer_running = true;
2618
2619 its.it_interval.tv_sec = 0;
2620 its.it_interval.tv_nsec = 0;
2621 its.it_value.tv_sec = duration / 1000;
2622 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2623 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2624 fprintf(stderr, "could not set cursor timerfd\n: %m");
2625}
2626
2627static void cancel_pointer_image_update(struct input *input)
2628{
2629 if (input->cursor_timer_running)
2630 cursor_delay_timer_reset(input, 0);
2631}
2632
2633static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002634input_remove_pointer_focus(struct input *input)
2635{
2636 struct window *window = input->pointer_focus;
2637
2638 if (!window)
2639 return;
2640
2641 input_set_focus_widget(input, NULL, 0, 0);
2642
2643 input->pointer_focus = NULL;
2644 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002645 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002646}
2647
2648static void
2649pointer_handle_enter(void *data, struct wl_pointer *pointer,
2650 uint32_t serial, struct wl_surface *surface,
2651 wl_fixed_t sx_w, wl_fixed_t sy_w)
2652{
2653 struct input *input = data;
2654 struct window *window;
2655 struct widget *widget;
2656 float sx = wl_fixed_to_double(sx_w);
2657 float sy = wl_fixed_to_double(sy_w);
2658
2659 if (!surface) {
2660 /* enter event for a window we've just destroyed */
2661 return;
2662 }
2663
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002664 window = wl_surface_get_user_data(surface);
2665 if (surface != window->main_surface->surface) {
2666 DBG("Ignoring input event from subsurface %p\n", surface);
2667 return;
2668 }
2669
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002670 input->display->serial = serial;
2671 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002672 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002673
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002674 input->sx = sx;
2675 input->sy = sy;
2676
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002677 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002678 input_set_focus_widget(input, widget, sx, sy);
2679}
2680
2681static void
2682pointer_handle_leave(void *data, struct wl_pointer *pointer,
2683 uint32_t serial, struct wl_surface *surface)
2684{
2685 struct input *input = data;
2686
2687 input->display->serial = serial;
2688 input_remove_pointer_focus(input);
2689}
2690
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002691static void
Daniel Stone37816df2012-05-16 18:45:18 +01002692pointer_handle_motion(void *data, struct wl_pointer *pointer,
2693 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002694{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002695 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002696 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002697 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002698 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002699 float sx = wl_fixed_to_double(sx_w);
2700 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002701
Paul Winwoodb22bf572013-08-29 10:52:54 +01002702 if (!window)
2703 return;
2704
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002705 input->sx = sx;
2706 input->sy = sy;
2707
Rob Bradford5f087742013-07-11 19:41:27 +01002708 /* when making the window smaller - e.g. after a unmaximise we might
2709 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002710 * based on the old surface dimensions. However, if we have an active
2711 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002712 */
Derek Foreman46812b62015-08-26 17:13:27 -05002713 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002714 sy < window->main_surface->allocation.y ||
2715 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002716 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002717 return;
2718
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002719 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002720 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002721 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002722 }
2723
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002724 if (input->grab)
2725 widget = input->grab;
2726 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002727 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002728 if (widget) {
2729 if (widget->motion_handler)
2730 cursor = widget->motion_handler(input->focus_widget,
2731 input, time, sx, sy,
2732 widget->user_data);
2733 else
2734 cursor = widget->default_cursor;
2735 } else
2736 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002737
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002738 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002739}
2740
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002741static void
Daniel Stone37816df2012-05-16 18:45:18 +01002742pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002743 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002744{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002745 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002746 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002747 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002748
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002749 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002750 if (input->focus_widget && input->grab == NULL &&
2751 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002752 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002753
Neil Roberts6b28aad2012-01-23 19:11:18 +00002754 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002755 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002756 (*widget->button_handler)(widget,
2757 input, time,
2758 button, state,
2759 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002760
Daniel Stone4dbadb12012-05-30 16:31:51 +01002761 if (input->grab && input->grab_button == button &&
2762 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002763 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002764}
2765
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002766static void
Daniel Stone37816df2012-05-16 18:45:18 +01002767pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002768 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002769{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002770 struct input *input = data;
2771 struct widget *widget;
2772
2773 widget = input->focus_widget;
2774 if (input->grab)
2775 widget = input->grab;
2776 if (widget && widget->axis_handler)
2777 (*widget->axis_handler)(widget,
2778 input, time,
2779 axis, value,
2780 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002781}
2782
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002783static const struct wl_pointer_listener pointer_listener = {
2784 pointer_handle_enter,
2785 pointer_handle_leave,
2786 pointer_handle_motion,
2787 pointer_handle_button,
2788 pointer_handle_axis,
2789};
2790
2791static void
2792input_remove_keyboard_focus(struct input *input)
2793{
2794 struct window *window = input->keyboard_focus;
2795 struct itimerspec its;
2796
2797 its.it_interval.tv_sec = 0;
2798 its.it_interval.tv_nsec = 0;
2799 its.it_value.tv_sec = 0;
2800 its.it_value.tv_nsec = 0;
2801 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2802
2803 if (!window)
2804 return;
2805
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002806 if (window->keyboard_focus_handler)
2807 (*window->keyboard_focus_handler)(window, NULL,
2808 window->user_data);
2809
2810 input->keyboard_focus = NULL;
2811}
2812
2813static void
2814keyboard_repeat_func(struct task *task, uint32_t events)
2815{
2816 struct input *input =
2817 container_of(task, struct input, repeat_task);
2818 struct window *window = input->keyboard_focus;
2819 uint64_t exp;
2820
2821 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2822 /* If we change the timer between the fd becoming
2823 * readable and getting here, there'll be nothing to
2824 * read and we get EAGAIN. */
2825 return;
2826
2827 if (window && window->key_handler) {
2828 (*window->key_handler)(window, input, input->repeat_time,
2829 input->repeat_key, input->repeat_sym,
2830 WL_KEYBOARD_KEY_STATE_PRESSED,
2831 window->user_data);
2832 }
2833}
2834
2835static void
2836keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2837 uint32_t format, int fd, uint32_t size)
2838{
2839 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002840 struct xkb_keymap *keymap;
2841 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002842 char *map_str;
2843
2844 if (!data) {
2845 close(fd);
2846 return;
2847 }
2848
2849 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2850 close(fd);
2851 return;
2852 }
2853
2854 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2855 if (map_str == MAP_FAILED) {
2856 close(fd);
2857 return;
2858 }
2859
Ran Benita2e1968f2014-08-19 23:59:51 +03002860 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2861 map_str,
2862 XKB_KEYMAP_FORMAT_TEXT_V1,
2863 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002864 munmap(map_str, size);
2865 close(fd);
2866
Rui Matos3eccb862013-10-10 19:44:22 +02002867 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002868 fprintf(stderr, "failed to compile keymap\n");
2869 return;
2870 }
2871
Rui Matos3eccb862013-10-10 19:44:22 +02002872 state = xkb_state_new(keymap);
2873 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002874 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002875 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002876 return;
2877 }
2878
Rui Matos3eccb862013-10-10 19:44:22 +02002879 xkb_keymap_unref(input->xkb.keymap);
2880 xkb_state_unref(input->xkb.state);
2881 input->xkb.keymap = keymap;
2882 input->xkb.state = state;
2883
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002884 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002885 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002886 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002887 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002888 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002889 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002890}
2891
2892static void
2893keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2894 uint32_t serial, struct wl_surface *surface,
2895 struct wl_array *keys)
2896{
2897 struct input *input = data;
2898 struct window *window;
2899
2900 input->display->serial = serial;
2901 input->keyboard_focus = wl_surface_get_user_data(surface);
2902
2903 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002904 if (window->keyboard_focus_handler)
2905 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002906 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002907}
2908
2909static void
2910keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2911 uint32_t serial, struct wl_surface *surface)
2912{
2913 struct input *input = data;
2914
2915 input->display->serial = serial;
2916 input_remove_keyboard_focus(input);
2917}
2918
Scott Moreau210d0792012-03-22 10:47:01 -06002919static void
Daniel Stone37816df2012-05-16 18:45:18 +01002920keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002921 uint32_t serial, uint32_t time, uint32_t key,
2922 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002923{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002924 struct input *input = data;
2925 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002926 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002927 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002928 const xkb_keysym_t *syms;
2929 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002930 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002931
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002932 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002933 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002934 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002935 return;
2936
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002937 /* We only use input grabs for pointer events for now, so just
2938 * ignore key presses if a grab is active. We expand the key
2939 * event delivery mechanism to route events to widgets to
2940 * properly handle key grabs. In the meantime, this prevents
2941 * key event devlivery while a grab is active. */
2942 if (input->grab && input->grab_button == 0)
2943 return;
2944
Ran Benita2e1968f2014-08-19 23:59:51 +03002945 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002946
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002947 sym = XKB_KEY_NoSymbol;
2948 if (num_syms == 1)
2949 sym = syms[0];
2950
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002951
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002952 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002953 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002954 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002955 } else if (sym == XKB_KEY_F11 &&
2956 window->fullscreen_handler &&
2957 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2958 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002959 } else if (sym == XKB_KEY_F4 &&
2960 input->modifiers == MOD_ALT_MASK &&
2961 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002962 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002963 } else if (window->key_handler) {
2964 (*window->key_handler)(window, input, time, key,
2965 sym, state, window->user_data);
2966 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002967
2968 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2969 key == input->repeat_key) {
2970 its.it_interval.tv_sec = 0;
2971 its.it_interval.tv_nsec = 0;
2972 its.it_value.tv_sec = 0;
2973 its.it_value.tv_nsec = 0;
2974 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002975 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2976 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002977 input->repeat_sym = sym;
2978 input->repeat_key = key;
2979 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002980 its.it_interval.tv_sec = input->repeat_rate_sec;
2981 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2982 its.it_value.tv_sec = input->repeat_delay_sec;
2983 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002984 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2985 }
2986}
2987
2988static void
Daniel Stone351eb612012-05-31 15:27:47 -04002989keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2990 uint32_t serial, uint32_t mods_depressed,
2991 uint32_t mods_latched, uint32_t mods_locked,
2992 uint32_t group)
2993{
2994 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002995 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002996
Matt Ropere61561f2013-06-24 16:52:43 +01002997 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2998 if (!input->xkb.keymap)
2999 return;
3000
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003001 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3002 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003003 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003004 XKB_STATE_MODS_DEPRESSED |
3005 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003006 input->modifiers = 0;
3007 if (mask & input->xkb.control_mask)
3008 input->modifiers |= MOD_CONTROL_MASK;
3009 if (mask & input->xkb.alt_mask)
3010 input->modifiers |= MOD_ALT_MASK;
3011 if (mask & input->xkb.shift_mask)
3012 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003013}
3014
Jonny Lamb06959082014-08-12 14:58:27 +02003015static void
3016set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3017{
3018 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3019 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3020
3021 /* a rate of zero disables any repeating, regardless of the delay's
3022 * value */
3023 if (rate == 0)
3024 return;
3025
3026 if (rate == 1)
3027 input->repeat_rate_sec = 1;
3028 else
3029 input->repeat_rate_nsec = 1000000000 / rate;
3030
3031 input->repeat_delay_sec = delay / 1000;
3032 delay -= (input->repeat_delay_sec * 1000);
3033 input->repeat_delay_nsec = delay * 1000 * 1000;
3034}
3035
3036static void
3037keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3038 int32_t rate, int32_t delay)
3039{
3040 struct input *input = data;
3041
3042 set_repeat_info(input, rate, delay);
3043}
3044
Daniel Stone37816df2012-05-16 18:45:18 +01003045static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003046 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003047 keyboard_handle_enter,
3048 keyboard_handle_leave,
3049 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003050 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003051 keyboard_handle_repeat_info
3052
Daniel Stone37816df2012-05-16 18:45:18 +01003053};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003054
3055static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003056touch_handle_down(void *data, struct wl_touch *wl_touch,
3057 uint32_t serial, uint32_t time, struct wl_surface *surface,
3058 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3059{
3060 struct input *input = data;
3061 struct widget *widget;
3062 float sx = wl_fixed_to_double(x_w);
3063 float sy = wl_fixed_to_double(y_w);
3064
Rusty Lynch1084da52013-08-15 09:10:08 -07003065 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003066 input->touch_focus = wl_surface_get_user_data(surface);
3067 if (!input->touch_focus) {
3068 DBG("Failed to find to touch focus for surface %p\n", surface);
3069 return;
3070 }
3071
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003072 if (surface != input->touch_focus->main_surface->surface) {
3073 DBG("Ignoring input event from subsurface %p\n", surface);
3074 input->touch_focus = NULL;
3075 return;
3076 }
3077
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003078 if (input->grab)
3079 widget = input->grab;
3080 else
3081 widget = window_find_widget(input->touch_focus,
3082 wl_fixed_to_double(x_w),
3083 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003084 if (widget) {
3085 struct touch_point *tp = xmalloc(sizeof *tp);
3086 if (tp) {
3087 tp->id = id;
3088 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003089 tp->x = sx;
3090 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003091 wl_list_insert(&input->touch_point_list, &tp->link);
3092
3093 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003094 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003095 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003096 sx, sy,
3097 widget->user_data);
3098 }
3099 }
3100}
3101
3102static void
3103touch_handle_up(void *data, struct wl_touch *wl_touch,
3104 uint32_t serial, uint32_t time, int32_t id)
3105{
3106 struct input *input = data;
3107 struct touch_point *tp, *tmp;
3108
Rusty Lynch041815a2013-08-08 21:20:38 -07003109 if (!input->touch_focus) {
3110 DBG("No touch focus found for touch up event!\n");
3111 return;
3112 }
3113
3114 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3115 if (tp->id != id)
3116 continue;
3117
3118 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003119 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003120 time, id,
3121 tp->widget->user_data);
3122
3123 wl_list_remove(&tp->link);
3124 free(tp);
3125
3126 return;
3127 }
3128}
3129
3130static void
3131touch_handle_motion(void *data, struct wl_touch *wl_touch,
3132 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3133{
3134 struct input *input = data;
3135 struct touch_point *tp;
3136 float sx = wl_fixed_to_double(x_w);
3137 float sy = wl_fixed_to_double(y_w);
3138
3139 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3140
3141 if (!input->touch_focus) {
3142 DBG("No touch focus found for touch motion event!\n");
3143 return;
3144 }
3145
3146 wl_list_for_each(tp, &input->touch_point_list, link) {
3147 if (tp->id != id)
3148 continue;
3149
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003150 tp->x = sx;
3151 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003152 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003153 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003154 id, sx, sy,
3155 tp->widget->user_data);
3156 return;
3157 }
3158}
3159
3160static void
3161touch_handle_frame(void *data, struct wl_touch *wl_touch)
3162{
3163 struct input *input = data;
3164 struct touch_point *tp, *tmp;
3165
3166 DBG("touch_handle_frame\n");
3167
3168 if (!input->touch_focus) {
3169 DBG("No touch focus found for touch frame event!\n");
3170 return;
3171 }
3172
3173 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3174 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003175 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003176 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003177 }
3178}
3179
3180static void
3181touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3182{
3183 struct input *input = data;
3184 struct touch_point *tp, *tmp;
3185
3186 DBG("touch_handle_cancel\n");
3187
3188 if (!input->touch_focus) {
3189 DBG("No touch focus found for touch cancel event!\n");
3190 return;
3191 }
3192
3193 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3194 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003195 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3196 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003197
3198 wl_list_remove(&tp->link);
3199 free(tp);
3200 }
3201}
3202
3203static const struct wl_touch_listener touch_listener = {
3204 touch_handle_down,
3205 touch_handle_up,
3206 touch_handle_motion,
3207 touch_handle_frame,
3208 touch_handle_cancel,
3209};
3210
3211static void
Daniel Stone37816df2012-05-16 18:45:18 +01003212seat_handle_capabilities(void *data, struct wl_seat *seat,
3213 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003214{
Daniel Stone37816df2012-05-16 18:45:18 +01003215 struct input *input = data;
3216
3217 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3218 input->pointer = wl_seat_get_pointer(seat);
3219 wl_pointer_set_user_data(input->pointer, input);
3220 wl_pointer_add_listener(input->pointer, &pointer_listener,
3221 input);
3222 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003223 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003224 wl_pointer_release(input->pointer);
3225 else
3226 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003227 input->pointer = NULL;
3228 }
3229
3230 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3231 input->keyboard = wl_seat_get_keyboard(seat);
3232 wl_keyboard_set_user_data(input->keyboard, input);
3233 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3234 input);
3235 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003236 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003237 wl_keyboard_release(input->keyboard);
3238 else
3239 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003240 input->keyboard = NULL;
3241 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003242
3243 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3244 input->touch = wl_seat_get_touch(seat);
3245 wl_touch_set_user_data(input->touch, input);
3246 wl_touch_add_listener(input->touch, &touch_listener, input);
3247 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003248 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003249 wl_touch_release(input->touch);
3250 else
3251 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003252 input->touch = NULL;
3253 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003254}
3255
Rob Bradford08031182013-08-13 20:11:03 +01003256static void
3257seat_handle_name(void *data, struct wl_seat *seat,
3258 const char *name)
3259{
3260
3261}
3262
Daniel Stone37816df2012-05-16 18:45:18 +01003263static const struct wl_seat_listener seat_listener = {
3264 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003265 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003266};
3267
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003268void
3269input_get_position(struct input *input, int32_t *x, int32_t *y)
3270{
3271 *x = input->sx;
3272 *y = input->sy;
3273}
3274
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003275int
3276input_get_touch(struct input *input, int32_t id, float *x, float *y)
3277{
3278 struct touch_point *tp;
3279
3280 wl_list_for_each(tp, &input->touch_point_list, link) {
3281 if (tp->id != id)
3282 continue;
3283
3284 *x = tp->x;
3285 *y = tp->y;
3286 return 0;
3287 }
3288
3289 return -1;
3290}
3291
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003292struct display *
3293input_get_display(struct input *input)
3294{
3295 return input->display;
3296}
3297
Daniel Stone37816df2012-05-16 18:45:18 +01003298struct wl_seat *
3299input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003300{
Daniel Stone37816df2012-05-16 18:45:18 +01003301 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003302}
3303
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003304uint32_t
3305input_get_modifiers(struct input *input)
3306{
3307 return input->modifiers;
3308}
3309
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003310struct widget *
3311input_get_focus_widget(struct input *input)
3312{
3313 return input->focus_widget;
3314}
3315
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003316struct data_offer {
3317 struct wl_data_offer *offer;
3318 struct input *input;
3319 struct wl_array types;
3320 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003321
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003322 struct task io_task;
3323 int fd;
3324 data_func_t func;
3325 int32_t x, y;
3326 void *user_data;
3327};
3328
3329static void
3330data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3331{
3332 struct data_offer *offer = data;
3333 char **p;
3334
3335 p = wl_array_add(&offer->types, sizeof *p);
3336 *p = strdup(type);
3337}
3338
3339static const struct wl_data_offer_listener data_offer_listener = {
3340 data_offer_offer,
3341};
3342
3343static void
3344data_offer_destroy(struct data_offer *offer)
3345{
3346 char **p;
3347
3348 offer->refcount--;
3349 if (offer->refcount == 0) {
3350 wl_data_offer_destroy(offer->offer);
3351 for (p = offer->types.data; *p; p++)
3352 free(*p);
3353 wl_array_release(&offer->types);
3354 free(offer);
3355 }
3356}
3357
3358static void
3359data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003360 struct wl_data_device *data_device,
3361 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003362{
3363 struct data_offer *offer;
3364
Brian Lovinbc919262013-08-07 15:34:59 -07003365 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003366
3367 wl_array_init(&offer->types);
3368 offer->refcount = 1;
3369 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003370 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003371 wl_data_offer_add_listener(offer->offer,
3372 &data_offer_listener, offer);
3373}
3374
3375static void
3376data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003377 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003378 wl_fixed_t x_w, wl_fixed_t y_w,
3379 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003380{
3381 struct input *input = data;
3382 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003383 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003384 float x = wl_fixed_to_double(x_w);
3385 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003386 char **p;
3387
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003388 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003389 input->drag_enter_serial = serial;
3390 input->drag_focus = window,
3391 input->drag_x = x;
3392 input->drag_y = y;
3393
3394 if (!input->touch_grab)
3395 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003396
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003397 if (offer) {
3398 input->drag_offer = wl_data_offer_get_user_data(offer);
3399
3400 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3401 *p = NULL;
3402
3403 types_data = input->drag_offer->types.data;
3404 } else {
3405 input->drag_offer = NULL;
3406 types_data = NULL;
3407 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003408
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003409 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003410 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003411 window->user_data);
3412}
3413
3414static void
3415data_device_leave(void *data, struct wl_data_device *data_device)
3416{
3417 struct input *input = data;
3418
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003419 if (input->drag_offer) {
3420 data_offer_destroy(input->drag_offer);
3421 input->drag_offer = NULL;
3422 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003423}
3424
3425static void
3426data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003427 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003428{
3429 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003430 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003431 float x = wl_fixed_to_double(x_w);
3432 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003433 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003434
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003435 input->drag_x = x;
3436 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003437
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003438 if (input->drag_offer)
3439 types_data = input->drag_offer->types.data;
3440 else
3441 types_data = NULL;
3442
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003443 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003444 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003445 window->user_data);
3446}
3447
3448static void
3449data_device_drop(void *data, struct wl_data_device *data_device)
3450{
3451 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003452 struct window *window = input->drag_focus;
3453 float x, y;
3454
3455 x = input->drag_x;
3456 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003457
3458 if (window->drop_handler)
3459 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003460 x, y, window->user_data);
3461
3462 if (input->touch_grab)
3463 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003464}
3465
3466static void
3467data_device_selection(void *data,
3468 struct wl_data_device *wl_data_device,
3469 struct wl_data_offer *offer)
3470{
3471 struct input *input = data;
3472 char **p;
3473
3474 if (input->selection_offer)
3475 data_offer_destroy(input->selection_offer);
3476
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003477 if (offer) {
3478 input->selection_offer = wl_data_offer_get_user_data(offer);
3479 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3480 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003481 } else {
3482 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003483 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003484}
3485
3486static const struct wl_data_device_listener data_device_listener = {
3487 data_device_data_offer,
3488 data_device_enter,
3489 data_device_leave,
3490 data_device_motion,
3491 data_device_drop,
3492 data_device_selection
3493};
3494
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003495static void
3496input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003497{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003498 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003499 struct wl_cursor *cursor;
3500 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003501
Daniel Stone80972742012-11-07 17:51:39 +11003502 if (!input->pointer)
3503 return;
3504
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003505 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003506 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003507 return;
3508
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003509 if (index >= (int) cursor->image_count) {
3510 fprintf(stderr, "cursor index out of range\n");
3511 return;
3512 }
3513
3514 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003515 buffer = wl_cursor_image_get_buffer(image);
3516 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003517 return;
3518
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003519 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3520 wl_surface_damage(input->pointer_surface, 0, 0,
3521 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003522 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003523 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3524 input->pointer_surface,
3525 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003526}
3527
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003528static const struct wl_callback_listener pointer_surface_listener;
3529
Derek Foreman493d9792015-03-04 16:26:25 -06003530static bool
3531input_set_pointer_special(struct input *input)
3532{
3533 if (input->current_cursor == CURSOR_BLANK) {
3534 wl_pointer_set_cursor(input->pointer,
3535 input->pointer_enter_serial,
3536 NULL, 0, 0);
3537 return true;
3538 }
3539
3540 if (input->current_cursor == CURSOR_UNSET)
3541 return true;
3542
3543 return false;
3544}
3545
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003546static void
Derek Foreman118a4292015-04-22 17:23:35 -05003547schedule_pointer_image_update(struct input *input,
3548 struct wl_cursor *cursor,
3549 uint32_t duration,
3550 bool force_frame)
3551{
3552 /* Some silly cursor sets have enormous pauses in them. In these
3553 * cases it's better to use a timer even if it results in less
3554 * accurate presentation, since it will save us having to set the
3555 * same cursor image over and over again.
3556 *
3557 * This is really not the way we're supposed to time any kind of
3558 * animation, but we're pretending it's OK here because we don't
3559 * want animated cursors with long delays to needlessly hog CPU.
3560 *
3561 * We use force_frame to ensure we don't accumulate large timing
3562 * errors by running off the wrong clock.
3563 */
3564 if (!force_frame && duration > 100) {
3565 struct timespec tp;
3566
3567 clock_gettime(CLOCK_MONOTONIC, &tp);
3568 input->cursor_timer_start = tp.tv_sec * 1000
3569 + tp.tv_nsec / 1000000;
3570 cursor_delay_timer_reset(input, duration);
3571 return;
3572 }
3573
3574 /* for short durations we'll just spin on frame callbacks for
3575 * accurate timing - the way any kind of timing sensitive animation
3576 * should really be done. */
3577 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3578 wl_callback_add_listener(input->cursor_frame_cb,
3579 &pointer_surface_listener, input);
3580
3581}
3582
3583static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003584pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3585 uint32_t time)
3586{
3587 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003588 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003589 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003590 uint32_t duration;
3591 bool force_frame = true;
3592
3593 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003594
3595 if (callback) {
3596 assert(callback == input->cursor_frame_cb);
3597 wl_callback_destroy(callback);
3598 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003599 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003600 }
3601
Daniel Stone80972742012-11-07 17:51:39 +11003602 if (!input->pointer)
3603 return;
3604
Derek Foreman493d9792015-03-04 16:26:25 -06003605 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003606 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003607
Daniel Stonea494f1d2012-06-18 19:31:12 +01003608 cursor = input->display->cursors[input->current_cursor];
3609 if (!cursor)
3610 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003611
3612 /* FIXME We don't have the current time on the first call so we set
3613 * the animation start to the time of the first frame callback. */
3614 if (time == 0)
3615 input->cursor_anim_start = 0;
3616 else if (input->cursor_anim_start == 0)
3617 input->cursor_anim_start = time;
3618
Derek Foreman118a4292015-04-22 17:23:35 -05003619 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003620
Derek Foreman118a4292015-04-22 17:23:35 -05003621 if (time == 0 || input->cursor_anim_start == 0) {
3622 duration = 0;
3623 i = 0;
3624 } else
3625 i = wl_cursor_frame_and_duration(
3626 cursor,
3627 time - input->cursor_anim_start,
3628 &duration);
3629
3630 if (cursor->image_count > 1)
3631 schedule_pointer_image_update(input, cursor, duration,
3632 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003633
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003634 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003635}
3636
Derek Foreman118a4292015-04-22 17:23:35 -05003637static void
3638cursor_timer_func(struct task *task, uint32_t events)
3639{
3640 struct input *input = container_of(task, struct input, cursor_task);
3641 struct timespec tp;
3642 struct wl_cursor *cursor;
3643 uint32_t time;
3644 uint64_t exp;
3645
3646 if (!input->cursor_timer_running)
3647 return;
3648
3649 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3650 return;
3651
3652 cursor = input->display->cursors[input->current_cursor];
3653 if (!cursor)
3654 return;
3655
3656 clock_gettime(CLOCK_MONOTONIC, &tp);
3657 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3658 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3659}
3660
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003661static const struct wl_callback_listener pointer_surface_listener = {
3662 pointer_surface_frame_callback
3663};
3664
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003665void
3666input_set_pointer_image(struct input *input, int pointer)
3667{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003668 int force = 0;
3669
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003670 if (!input->pointer)
3671 return;
3672
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003673 if (input->pointer_enter_serial > input->cursor_serial)
3674 force = 1;
3675
3676 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003677 return;
3678
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003679 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003680 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003681 if (!input->cursor_frame_cb)
3682 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003683 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003684 /* The current frame callback may be stuck if, for instance,
3685 * the set cursor request was processed by the server after
3686 * this client lost the focus. In this case the cursor surface
3687 * might not be mapped and the frame callback wouldn't ever
3688 * complete. Send a set_cursor and attach to try to map the
3689 * cursor surface again so that the callback will finish */
3690 input_set_pointer_image_index(input, 0);
3691 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003692}
3693
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003694struct wl_data_device *
3695input_get_data_device(struct input *input)
3696{
3697 return input->data_device;
3698}
3699
3700void
3701input_set_selection(struct input *input,
3702 struct wl_data_source *source, uint32_t time)
3703{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003704 if (input->data_device)
3705 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003706}
3707
3708void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003709input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003710{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003711 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003712 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003713}
3714
3715static void
3716offer_io_func(struct task *task, uint32_t events)
3717{
3718 struct data_offer *offer =
3719 container_of(task, struct data_offer, io_task);
3720 unsigned int len;
3721 char buffer[4096];
3722
3723 len = read(offer->fd, buffer, sizeof buffer);
3724 offer->func(buffer, len,
3725 offer->x, offer->y, offer->user_data);
3726
3727 if (len == 0) {
3728 close(offer->fd);
3729 data_offer_destroy(offer);
3730 }
3731}
3732
3733static void
3734data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3735 data_func_t func, void *user_data)
3736{
3737 int p[2];
3738
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003739 if (pipe2(p, O_CLOEXEC) == -1)
3740 return;
3741
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003742 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3743 close(p[1]);
3744
3745 offer->io_task.run = offer_io_func;
3746 offer->fd = p[0];
3747 offer->func = func;
3748 offer->refcount++;
3749 offer->user_data = user_data;
3750
3751 display_watch_fd(offer->input->display,
3752 offer->fd, EPOLLIN, &offer->io_task);
3753}
3754
3755void
3756input_receive_drag_data(struct input *input, const char *mime_type,
3757 data_func_t func, void *data)
3758{
3759 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003760 input->drag_offer->x = input->drag_x;
3761 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003762}
3763
3764int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003765input_receive_drag_data_to_fd(struct input *input,
3766 const char *mime_type, int fd)
3767{
3768 if (input->drag_offer)
3769 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3770
3771 return 0;
3772}
3773
3774int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003775input_receive_selection_data(struct input *input, const char *mime_type,
3776 data_func_t func, void *data)
3777{
3778 char **p;
3779
3780 if (input->selection_offer == NULL)
3781 return -1;
3782
3783 for (p = input->selection_offer->types.data; *p; p++)
3784 if (strcmp(mime_type, *p) == 0)
3785 break;
3786
3787 if (*p == NULL)
3788 return -1;
3789
3790 data_offer_receive_data(input->selection_offer,
3791 mime_type, func, data);
3792 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003793}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003794
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003795int
3796input_receive_selection_data_to_fd(struct input *input,
3797 const char *mime_type, int fd)
3798{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003799 if (input->selection_offer)
3800 wl_data_offer_receive(input->selection_offer->offer,
3801 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003802
3803 return 0;
3804}
3805
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003806void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003807window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003808{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003809 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003810 return;
3811
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003812 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003813}
3814
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003815static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003816surface_set_synchronized(struct surface *surface)
3817{
3818 if (!surface->subsurface)
3819 return;
3820
3821 if (surface->synchronized)
3822 return;
3823
3824 wl_subsurface_set_sync(surface->subsurface);
3825 surface->synchronized = 1;
3826}
3827
3828static void
3829surface_set_synchronized_default(struct surface *surface)
3830{
3831 if (!surface->subsurface)
3832 return;
3833
3834 if (surface->synchronized == surface->synchronized_default)
3835 return;
3836
3837 if (surface->synchronized_default)
3838 wl_subsurface_set_sync(surface->subsurface);
3839 else
3840 wl_subsurface_set_desync(surface->subsurface);
3841
3842 surface->synchronized = surface->synchronized_default;
3843}
3844
3845static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003846surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003847{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003848 struct widget *widget = surface->widget;
3849 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003850
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003851 if (surface->input_region) {
3852 wl_region_destroy(surface->input_region);
3853 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003854 }
3855
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003856 if (surface->opaque_region)
3857 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003858
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003859 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003860
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003861 if (widget->resize_handler)
3862 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003863 widget->allocation.width,
3864 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003865 widget->user_data);
3866
Pekka Paalanen35e82632013-04-25 13:57:48 +03003867 if (surface->subsurface &&
3868 (surface->allocation.x != widget->allocation.x ||
3869 surface->allocation.y != widget->allocation.y)) {
3870 wl_subsurface_set_position(surface->subsurface,
3871 widget->allocation.x,
3872 widget->allocation.y);
3873 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003874 if (surface->allocation.width != widget->allocation.width ||
3875 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003876 window_schedule_redraw(widget->window);
3877 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003878 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003879
3880 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003881 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003882 widget->allocation.width,
3883 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003884}
3885
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003886static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003887window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003888{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003889 struct surface *surface;
3890
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003891 widget_set_allocation(window->main_surface->widget,
3892 window->pending_allocation.x,
3893 window->pending_allocation.y,
3894 window->pending_allocation.width,
3895 window->pending_allocation.height);
3896
3897 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003898
3899 /* The main surface is in the list, too. Main surface's
3900 * resize_handler is responsible for calling widget_set_allocation()
3901 * on all sub-surface root widgets, so they will be resized
3902 * properly.
3903 */
3904 wl_list_for_each(surface, &window->subsurface_list, link) {
3905 if (surface == window->main_surface)
3906 continue;
3907
3908 surface_set_synchronized(surface);
3909 surface_resize(surface);
3910 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003911
3912 if (!window->fullscreen && !window->maximized)
3913 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003914}
3915
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003916static void
3917idle_resize(struct window *window)
3918{
3919 window->resize_needed = 0;
3920 window->redraw_needed = 1;
3921
3922 DBG("from %dx%d to %dx%d\n",
3923 window->main_surface->server_allocation.width,
3924 window->main_surface->server_allocation.height,
3925 window->pending_allocation.width,
3926 window->pending_allocation.height);
3927
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003928 window_do_resize(window);
3929}
3930
3931static void
3932undo_resize(struct window *window)
3933{
3934 window->pending_allocation.width =
3935 window->main_surface->server_allocation.width;
3936 window->pending_allocation.height =
3937 window->main_surface->server_allocation.height;
3938
3939 DBG("back to %dx%d\n",
3940 window->main_surface->server_allocation.width,
3941 window->main_surface->server_allocation.height);
3942
3943 window_do_resize(window);
3944
3945 if (window->pending_allocation.width == 0 &&
3946 window->pending_allocation.height == 0) {
3947 fprintf(stderr, "Error: Could not draw a surface, "
3948 "most likely due to insufficient disk space in "
3949 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3950 exit(EXIT_FAILURE);
3951 }
3952}
3953
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003954void
3955window_schedule_resize(struct window *window, int width, int height)
3956{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003957 /* We should probably get these numbers from the theme. */
3958 const int min_width = 200, min_height = 200;
3959
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003960 window->pending_allocation.x = 0;
3961 window->pending_allocation.y = 0;
3962 window->pending_allocation.width = width;
3963 window->pending_allocation.height = height;
3964
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003965 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003966 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003967 window->min_allocation.width = min_width;
3968 else
3969 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003970 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003971 window->min_allocation.height = min_height;
3972 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003973 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003974 }
3975
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003976 if (window->pending_allocation.width < window->min_allocation.width)
3977 window->pending_allocation.width = window->min_allocation.width;
3978 if (window->pending_allocation.height < window->min_allocation.height)
3979 window->pending_allocation.height = window->min_allocation.height;
3980
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003981 window->resize_needed = 1;
3982 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003983}
3984
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003985void
3986widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3987{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003988 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003989}
3990
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003991static int
3992window_get_shadow_margin(struct window *window)
3993{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003994 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003995 return frame_get_shadow_margin(window->frame->frame);
3996 else
3997 return 0;
3998}
3999
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004000static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004001handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004002 int32_t width, int32_t height,
4003 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004004{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004005 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004006 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004007
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004008 window->maximized = 0;
4009 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004010 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004011 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004012
4013 wl_array_for_each(p, states) {
4014 uint32_t state = *p;
4015 switch (state) {
4016 case XDG_SURFACE_STATE_MAXIMIZED:
4017 window->maximized = 1;
4018 break;
4019 case XDG_SURFACE_STATE_FULLSCREEN:
4020 window->fullscreen = 1;
4021 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004022 case XDG_SURFACE_STATE_RESIZING:
4023 window->resizing = 1;
4024 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004025 case XDG_SURFACE_STATE_ACTIVATED:
4026 window->focused = 1;
4027 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004028 default:
4029 /* Unknown state */
4030 break;
4031 }
4032 }
4033
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004034 if (window->frame) {
4035 if (window->maximized) {
4036 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4037 } else {
4038 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4039 }
4040
4041 if (window->focused) {
4042 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4043 } else {
4044 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4045 }
4046 }
4047
Jasper St. Pierref184c382014-05-06 08:33:27 -04004048 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004049 /* The width / height params are for window geometry,
4050 * but window_schedule_resize takes allocation. Add
4051 * on the shadow margin to get the difference. */
4052 int margin = window_get_shadow_margin(window);
4053
4054 window_schedule_resize(window,
4055 width + margin * 2,
4056 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004057 } else {
4058 window_schedule_resize(window,
4059 window->saved_allocation.width,
4060 window->saved_allocation.height);
4061 }
4062
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004063 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004064
4065 if (window->state_changed_handler)
4066 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004067}
4068
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004069static void
4070handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4071{
4072 struct window *window = data;
4073 window_close(window);
4074}
4075
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004076static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004077 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004078 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004079};
4080
4081static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004082window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004083{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004084 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004085
4086 if (!window->xdg_surface)
4087 return;
4088
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004089 if (window->parent == window->last_parent)
4090 return;
4091
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004092 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004093 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004094 else
4095 parent_surface = NULL;
4096
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004097 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004098 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004099}
4100
4101static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004102window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004103{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004104 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004105 frame_input_rect(window->frame->frame,
4106 &geometry->x,
4107 &geometry->y,
4108 &geometry->width,
4109 &geometry->height);
4110 else
4111 window_get_allocation(window, geometry);
4112}
4113
4114static void
4115window_sync_geometry(struct window *window)
4116{
4117 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004118
4119 if (!window->xdg_surface)
4120 return;
4121
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004122 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004123 if (geometry.x == window->last_geometry.x &&
4124 geometry.y == window->last_geometry.y &&
4125 geometry.width == window->last_geometry.width &&
4126 geometry.height == window->last_geometry.height)
4127 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004128
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004129 xdg_surface_set_window_geometry(window->xdg_surface,
4130 geometry.x,
4131 geometry.y,
4132 geometry.width,
4133 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004134 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004135}
4136
4137static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004138window_flush(struct window *window)
4139{
4140 struct surface *surface;
4141
4142 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004143 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004144 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004145 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004146 }
4147 }
4148
4149 wl_list_for_each(surface, &window->subsurface_list, link) {
4150 if (surface == window->main_surface)
4151 continue;
4152
4153 surface_flush(surface);
4154 }
4155
4156 surface_flush(window->main_surface);
4157}
4158
4159static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004160menu_destroy(struct menu *menu)
4161{
4162 widget_destroy(menu->widget);
4163 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004164 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004165 free(menu);
4166}
4167
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004168void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004169window_get_allocation(struct window *window,
4170 struct rectangle *allocation)
4171{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004172 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004173}
4174
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004175static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004176widget_redraw(struct widget *widget)
4177{
4178 struct widget *child;
4179
4180 if (widget->redraw_handler)
4181 widget->redraw_handler(widget, widget->user_data);
4182 wl_list_for_each(child, &widget->child_list, link)
4183 widget_redraw(child);
4184}
4185
4186static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004187frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4188{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004189 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004190
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004191 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004192 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004193 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004194 surface->frame_cb = NULL;
4195
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004196 surface->last_time = time;
4197
Pekka Paalanen71233882013-04-25 13:57:53 +03004198 if (surface->redraw_needed || surface->window->redraw_needed) {
4199 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004200 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004201 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004202}
4203
4204static const struct wl_callback_listener listener = {
4205 frame_callback
4206};
4207
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004208static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004209surface_redraw(struct surface *surface)
4210{
Pekka Paalanen71233882013-04-25 13:57:53 +03004211 DBG_OBJ(surface->surface, "begin\n");
4212
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004213 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004214 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004215
4216 /* Whole-window redraw forces a redraw even if the previous has
4217 * not yet hit the screen.
4218 */
4219 if (surface->frame_cb) {
4220 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004221 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004222
Pekka Paalanen71233882013-04-25 13:57:53 +03004223 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004224 wl_callback_destroy(surface->frame_cb);
4225 }
4226
Neil Roberts97b747c2013-12-19 16:17:12 +00004227 if (surface->widget->use_cairo &&
4228 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004229 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004230 return -1;
4231 }
4232
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004233 surface->frame_cb = wl_surface_frame(surface->surface);
4234 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004235 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004236
4237 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004238 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004239 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004240 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004241 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004242}
4243
4244static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004245idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004246{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004247 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004248 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004249 int failed = 0;
4250 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004251
Pekka Paalanen71233882013-04-25 13:57:53 +03004252 DBG(" --------- \n");
4253
Pekka Paalaneneebff542013-04-25 13:57:52 +03004254 wl_list_init(&window->redraw_task.link);
4255 window->redraw_task_scheduled = 0;
4256
4257 if (window->resize_needed) {
4258 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004259 if (window->main_surface->frame_cb) {
4260 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004261 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004262 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004263
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004264 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004265 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004266 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004267
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004268 if (surface_redraw(window->main_surface) < 0) {
4269 /*
4270 * Only main_surface failure will cause us to undo the resize.
4271 * If sub-surfaces fail, they will just be broken with old
4272 * content.
4273 */
4274 failed = 1;
4275 } else {
4276 wl_list_for_each(surface, &window->subsurface_list, link) {
4277 if (surface == window->main_surface)
4278 continue;
4279
4280 surface_redraw(surface);
4281 }
4282 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004283
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004284 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004285 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004286
4287 wl_list_for_each(surface, &window->subsurface_list, link)
4288 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004289
4290 if (resized && failed) {
4291 /* Restore widget tree to correspond to what is on screen. */
4292 undo_resize(window);
4293 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004294}
4295
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004296static void
4297window_schedule_redraw_task(struct window *window)
4298{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004299 if (!window->redraw_task_scheduled) {
4300 window->redraw_task.run = idle_redraw;
4301 display_defer(window->display, &window->redraw_task);
4302 window->redraw_task_scheduled = 1;
4303 }
4304}
4305
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004306void
4307window_schedule_redraw(struct window *window)
4308{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004309 struct surface *surface;
4310
Pekka Paalanen71233882013-04-25 13:57:53 +03004311 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4312
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004313 wl_list_for_each(surface, &window->subsurface_list, link)
4314 surface->redraw_needed = 1;
4315
4316 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004317}
4318
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004319int
4320window_is_fullscreen(struct window *window)
4321{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004322 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004323}
4324
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004325void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004326window_set_fullscreen(struct window *window, int fullscreen)
4327{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004328 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004329 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004330
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004331 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004332 return;
4333
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004334 if (fullscreen)
4335 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4336 else
4337 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004338}
4339
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004340int
4341window_is_maximized(struct window *window)
4342{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004343 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004344}
4345
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004346void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004347window_set_maximized(struct window *window, int maximized)
4348{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004349 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004350 return;
4351
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004352 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004353 return;
4354
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004355 if (maximized)
4356 xdg_surface_set_maximized(window->xdg_surface);
4357 else
4358 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004359}
4360
Jasper St. Pierrede680992014-04-10 17:23:49 -07004361int
4362window_is_resizing(struct window *window)
4363{
4364 return window->resizing;
4365}
4366
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004367void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004368window_set_minimized(struct window *window)
4369{
4370 if (!window->xdg_surface)
4371 return;
4372
4373 xdg_surface_set_minimized(window->xdg_surface);
4374}
4375
4376void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004377window_set_user_data(struct window *window, void *data)
4378{
4379 window->user_data = data;
4380}
4381
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004382void *
4383window_get_user_data(struct window *window)
4384{
4385 return window->user_data;
4386}
4387
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004388void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004389window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004390 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004391{
4392 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004393}
4394
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004395void
4396window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004397 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004398{
4399 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004400}
4401
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004402void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004403window_set_data_handler(struct window *window, window_data_handler_t handler)
4404{
4405 window->data_handler = handler;
4406}
4407
4408void
4409window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4410{
4411 window->drop_handler = handler;
4412}
4413
4414void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004415window_set_close_handler(struct window *window,
4416 window_close_handler_t handler)
4417{
4418 window->close_handler = handler;
4419}
4420
4421void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004422window_set_fullscreen_handler(struct window *window,
4423 window_fullscreen_handler_t handler)
4424{
4425 window->fullscreen_handler = handler;
4426}
4427
4428void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004429window_set_output_handler(struct window *window,
4430 window_output_handler_t handler)
4431{
4432 window->output_handler = handler;
4433}
4434
4435void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004436window_set_state_changed_handler(struct window *window,
4437 window_state_changed_handler_t handler)
4438{
4439 window->state_changed_handler = handler;
4440}
4441
4442void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004443window_set_title(struct window *window, const char *title)
4444{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004445 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004446 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004447 if (window->frame) {
4448 frame_set_title(window->frame->frame, title);
4449 widget_schedule_redraw(window->frame->widget);
4450 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004451 if (window->xdg_surface)
4452 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004453}
4454
4455const char *
4456window_get_title(struct window *window)
4457{
4458 return window->title;
4459}
4460
4461void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004462window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4463{
4464 struct text_cursor_position *text_cursor_position =
4465 window->display->text_cursor_position;
4466
Scott Moreau9295ce02012-06-01 12:46:10 -06004467 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004468 return;
4469
4470 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004471 window->main_surface->surface,
4472 wl_fixed_from_int(x),
4473 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004474}
4475
Casey Dahlin9074db52012-04-19 22:50:09 -04004476static void
4477surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004478 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004479{
Rob Bradford7507b572012-05-15 17:55:34 +01004480 struct window *window = data;
4481 struct output *output;
4482 struct output *output_found = NULL;
4483 struct window_output *window_output;
4484
4485 wl_list_for_each(output, &window->display->output_list, link) {
4486 if (output->output == wl_output) {
4487 output_found = output;
4488 break;
4489 }
4490 }
4491
4492 if (!output_found)
4493 return;
4494
Brian Lovinbc919262013-08-07 15:34:59 -07004495 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004496 window_output->output = output_found;
4497
4498 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004499
4500 if (window->output_handler)
4501 window->output_handler(window, output_found, 1,
4502 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004503}
4504
4505static void
4506surface_leave(void *data,
4507 struct wl_surface *wl_surface, struct wl_output *output)
4508{
Rob Bradford7507b572012-05-15 17:55:34 +01004509 struct window *window = data;
4510 struct window_output *window_output;
4511 struct window_output *window_output_found = NULL;
4512
4513 wl_list_for_each(window_output, &window->window_output_list, link) {
4514 if (window_output->output->output == output) {
4515 window_output_found = window_output;
4516 break;
4517 }
4518 }
4519
4520 if (window_output_found) {
4521 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004522
4523 if (window->output_handler)
4524 window->output_handler(window, window_output->output,
4525 0, window->user_data);
4526
Rob Bradford7507b572012-05-15 17:55:34 +01004527 free(window_output_found);
4528 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004529}
4530
4531static const struct wl_surface_listener surface_listener = {
4532 surface_enter,
4533 surface_leave
4534};
4535
Pekka Paalanen4e373742013-02-13 16:17:13 +02004536static struct surface *
4537surface_create(struct window *window)
4538{
4539 struct display *display = window->display;
4540 struct surface *surface;
4541
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004542 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004543 surface->window = window;
4544 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004545 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004546 wl_surface_add_listener(surface->surface, &surface_listener, window);
4547
Pekka Paalanen35e82632013-04-25 13:57:48 +03004548 wl_list_insert(&window->subsurface_list, &surface->link);
4549
Pekka Paalanen4e373742013-02-13 16:17:13 +02004550 return surface;
4551}
4552
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004553static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004554get_preferred_buffer_type(struct display *display)
4555{
4556#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004557 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004558 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4559#endif
4560
4561 return WINDOW_BUFFER_TYPE_SHM;
4562}
4563
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004564static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004565window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004566{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004567 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004568 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004569
Peter Huttererf3d62272013-08-08 11:57:05 +10004570 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004571 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004572 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004573
4574 surface = surface_create(window);
4575 window->main_surface = surface;
4576
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004577 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004578
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004579 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004580 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004581
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004582 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004583
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004584 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004585 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004586 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004587
Rob Bradford7507b572012-05-15 17:55:34 +01004588 wl_list_init (&window->window_output_list);
4589
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004590 return window;
4591}
4592
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004593struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004594window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004595{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004596 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004597 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004598
4599 window = window_create_internal(display, 0);
4600
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004601 if (window->display->xdg_shell) {
4602 window->xdg_surface =
4603 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4604 window->main_surface->surface);
4605 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004606
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004607 xdg_surface_set_user_data(window->xdg_surface, window);
4608 xdg_surface_add_listener(window->xdg_surface,
4609 &xdg_surface_listener, window);
4610 } else if (display->ivi_application) {
4611 /* auto generation of ivi_id based on process id + basement of id */
4612 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4613 window->ivi_surface =
4614 ivi_application_surface_create(display->ivi_application,
4615 id_ivisurf, window->main_surface->surface);
4616 fail_on_null(window->ivi_surface);
4617
4618 ivi_surface_add_listener(window->ivi_surface,
4619 &ivi_surface_listener, window);
4620 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004621
4622 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004623}
4624
4625struct window *
4626window_create_custom(struct display *display)
4627{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004628 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004629}
4630
Jasper St. Pierre53686042013-12-09 15:26:25 -05004631void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004632window_set_parent(struct window *window,
4633 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004634{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004635 window->parent = parent_window;
4636 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004637}
4638
4639struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004640window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004641{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004642 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004643}
4644
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004645static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004646menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004647{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004648 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004649 int next;
4650
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004651 frame_interior(menu->frame, &x, &y, &width, &height);
4652 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004653 if (menu->current != next) {
4654 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004655 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004656 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004657}
4658
4659static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004660menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004661 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004662 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004663{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004664 struct menu *menu = data;
4665
4666 if (widget == menu->widget)
4667 menu_set_item(data, y);
4668
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004669 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004670}
4671
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004672static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004673menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004674 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004675{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004676 struct menu *menu = data;
4677
4678 if (widget == menu->widget)
4679 menu_set_item(data, y);
4680
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004681 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004682}
4683
4684static void
4685menu_leave_handler(struct widget *widget, struct input *input, void *data)
4686{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004687 struct menu *menu = data;
4688
4689 if (widget == menu->widget)
4690 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004691}
4692
4693static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004694menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004695 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004696 uint32_t button, enum wl_pointer_button_state state,
4697 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004698
4699{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004700 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004701
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004702 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4703 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004704 /* Either relase after press-drag-release or
4705 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004706 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05004707 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004708 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004709 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004710 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004711 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004712}
4713
4714static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004715menu_touch_up_handler(struct widget *widget,
4716 struct input *input,
4717 uint32_t serial,
4718 uint32_t time,
4719 int32_t id,
4720 void *data)
4721{
4722 struct menu *menu = data;
4723
4724 input_ungrab(input);
4725 menu_destroy(menu);
4726}
4727
4728static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004729menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004730{
4731 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004732 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004733 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004734
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004735 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004736
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004737 frame_repaint(menu->frame, cr);
4738 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004739
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004740 theme_set_background_source(menu->window->display->theme,
4741 cr, THEME_FRAME_ACTIVE);
4742 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004743 cairo_fill(cr);
4744
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004745 cairo_select_font_face(cr, "sans",
4746 CAIRO_FONT_SLANT_NORMAL,
4747 CAIRO_FONT_WEIGHT_NORMAL);
4748 cairo_set_font_size(cr, 12);
4749
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004750 for (i = 0; i < menu->count; i++) {
4751 if (i == menu->current) {
4752 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004753 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004754 cairo_fill(cr);
4755 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004756 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004757 cairo_show_text(cr, menu->entries[i]);
4758 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004759 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4760 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004761 cairo_show_text(cr, menu->entries[i]);
4762 }
4763 }
4764
4765 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004766}
4767
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004768static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004769handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004770{
4771 struct window *window = data;
4772 struct menu *menu = window->main_surface->widget->user_data;
4773
4774 input_ungrab(menu->input);
4775 menu_destroy(menu);
4776}
4777
4778static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004779 handle_popup_popup_done,
4780};
4781
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004782static struct menu *
4783create_menu(struct display *display,
4784 struct input *input, uint32_t time,
4785 menu_func_t func, const char **entries, int count,
4786 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004787{
4788 struct window *window;
4789 struct menu *menu;
4790
4791 menu = malloc(sizeof *menu);
4792 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004793 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004794
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004795 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004796 if (!window) {
4797 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004798 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004799 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004800
4801 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004802 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004803 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004804 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004805 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004806 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004807 menu->entries = entries;
4808 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004809 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004810 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004811 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004812 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004813 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004814
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004815 input_ungrab(input);
4816
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004817 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004818 widget_set_enter_handler(menu->widget, menu_enter_handler);
4819 widget_set_leave_handler(menu->widget, menu_leave_handler);
4820 widget_set_motion_handler(menu->widget, menu_motion_handler);
4821 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004822 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004823
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004824 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004825 frame_resize_inside(menu->frame, 200, count * 20);
4826 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4827 window_schedule_resize(window, frame_width(menu->frame),
4828 frame_height(menu->frame));
4829
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004830 return menu;
4831}
4832
4833struct window *
4834window_create_menu(struct display *display,
4835 struct input *input, uint32_t time,
4836 menu_func_t func, const char **entries, int count,
4837 void *user_data)
4838{
4839 struct menu *menu;
4840 menu = create_menu(display, input, time, func, entries, count, user_data);
4841
4842 if (menu == NULL)
4843 return NULL;
4844
4845 return menu->window;
4846}
4847
4848void
4849window_show_menu(struct display *display,
4850 struct input *input, uint32_t time, struct window *parent,
4851 int32_t x, int32_t y,
4852 menu_func_t func, const char **entries, int count)
4853{
4854 struct menu *menu;
4855 struct window *window;
4856 int32_t ix, iy;
4857
4858 menu = create_menu(display, input, time, func, entries, count, parent);
4859
4860 if (menu == NULL)
4861 return;
4862
4863 window = menu->window;
4864
4865 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4866 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4867
4868 window->x = x;
4869 window->y = y;
4870
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004871 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004872
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004873 if (!display->xdg_shell)
4874 return;
4875
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004876 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4877 window->main_surface->surface,
4878 parent->main_surface->surface,
4879 input->seat,
4880 display_get_serial(window->display),
4881 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004882 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004883 fail_on_null(window->xdg_popup);
4884
4885 xdg_popup_set_user_data(window->xdg_popup, window);
4886 xdg_popup_add_listener(window->xdg_popup,
4887 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004888}
4889
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004890void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004891window_set_buffer_type(struct window *window, enum window_buffer_type type)
4892{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004893 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004894}
4895
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004896enum window_buffer_type
4897window_get_buffer_type(struct window *window)
4898{
4899 return window->main_surface->buffer_type;
4900}
4901
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004902void
4903window_set_preferred_format(struct window *window,
4904 enum preferred_format format)
4905{
4906 window->preferred_format = format;
4907}
4908
Pekka Paalanen35e82632013-04-25 13:57:48 +03004909struct widget *
4910window_add_subsurface(struct window *window, void *data,
4911 enum subsurface_mode default_mode)
4912{
4913 struct widget *widget;
4914 struct surface *surface;
4915 struct wl_surface *parent;
4916 struct wl_subcompositor *subcompo = window->display->subcompositor;
4917
Pekka Paalanen35e82632013-04-25 13:57:48 +03004918 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004919 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004920 widget = widget_create(window, surface, data);
4921 wl_list_init(&widget->link);
4922 surface->widget = widget;
4923
4924 parent = window->main_surface->surface;
4925 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4926 surface->surface,
4927 parent);
4928 surface->synchronized = 1;
4929
4930 switch (default_mode) {
4931 case SUBSURFACE_SYNCHRONIZED:
4932 surface->synchronized_default = 1;
4933 break;
4934 case SUBSURFACE_DESYNCHRONIZED:
4935 surface->synchronized_default = 0;
4936 break;
4937 default:
4938 assert(!"bad enum subsurface_mode");
4939 }
4940
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004941 window->resize_needed = 1;
4942 window_schedule_redraw(window);
4943
Pekka Paalanen35e82632013-04-25 13:57:48 +03004944 return widget;
4945}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004946
4947static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004948display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004949 struct wl_output *wl_output,
4950 int x, int y,
4951 int physical_width,
4952 int physical_height,
4953 int subpixel,
4954 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004955 const char *model,
4956 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004957{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004958 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004959
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004960 output->allocation.x = x;
4961 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004962 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004963
4964 if (output->make)
4965 free(output->make);
4966 output->make = strdup(make);
4967
4968 if (output->model)
4969 free(output->model);
4970 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004971}
4972
4973static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004974display_handle_done(void *data,
4975 struct wl_output *wl_output)
4976{
4977}
4978
4979static void
4980display_handle_scale(void *data,
4981 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004982 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004983{
4984 struct output *output = data;
4985
4986 output->scale = scale;
4987}
4988
4989static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004990display_handle_mode(void *data,
4991 struct wl_output *wl_output,
4992 uint32_t flags,
4993 int width,
4994 int height,
4995 int refresh)
4996{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004997 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004998 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004999
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005000 if (flags & WL_OUTPUT_MODE_CURRENT) {
5001 output->allocation.width = width;
5002 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005003 if (display->output_configure_handler)
5004 (*display->output_configure_handler)(
5005 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005006 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005007}
5008
5009static const struct wl_output_listener output_listener = {
5010 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005011 display_handle_mode,
5012 display_handle_done,
5013 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005014};
5015
5016static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005017display_add_output(struct display *d, uint32_t id)
5018{
5019 struct output *output;
5020
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005021 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005022 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005023 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005024 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005025 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005026 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005027 wl_list_insert(d->output_list.prev, &output->link);
5028
5029 wl_output_add_listener(output->output, &output_listener, output);
5030}
5031
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005032static void
5033output_destroy(struct output *output)
5034{
5035 if (output->destroy_handler)
5036 (*output->destroy_handler)(output, output->user_data);
5037
5038 wl_output_destroy(output->output);
5039 wl_list_remove(&output->link);
5040 free(output);
5041}
5042
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005043static void
5044display_destroy_output(struct display *d, uint32_t id)
5045{
5046 struct output *output;
5047
5048 wl_list_for_each(output, &d->output_list, link) {
5049 if (output->server_output_id == id) {
5050 output_destroy(output);
5051 break;
5052 }
5053 }
5054}
5055
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005056void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005057display_set_global_handler(struct display *display,
5058 display_global_handler_t handler)
5059{
5060 struct global *global;
5061
5062 display->global_handler = handler;
5063 if (!handler)
5064 return;
5065
5066 wl_list_for_each(global, &display->global_list, link)
5067 display->global_handler(display,
5068 global->name, global->interface,
5069 global->version, display->user_data);
5070}
5071
5072void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005073display_set_global_handler_remove(struct display *display,
5074 display_global_handler_t remove_handler)
5075{
5076 display->global_handler_remove = remove_handler;
5077 if (!remove_handler)
5078 return;
5079}
5080
5081void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005082display_set_output_configure_handler(struct display *display,
5083 display_output_handler_t handler)
5084{
5085 struct output *output;
5086
5087 display->output_configure_handler = handler;
5088 if (!handler)
5089 return;
5090
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005091 wl_list_for_each(output, &display->output_list, link) {
5092 if (output->allocation.width == 0 &&
5093 output->allocation.height == 0)
5094 continue;
5095
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005096 (*display->output_configure_handler)(output,
5097 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005098 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005099}
5100
5101void
5102output_set_user_data(struct output *output, void *data)
5103{
5104 output->user_data = data;
5105}
5106
5107void *
5108output_get_user_data(struct output *output)
5109{
5110 return output->user_data;
5111}
5112
5113void
5114output_set_destroy_handler(struct output *output,
5115 display_output_handler_t handler)
5116{
5117 output->destroy_handler = handler;
5118 /* FIXME: implement this, once we have way to remove outputs */
5119}
5120
5121void
Scott Moreau4e072362012-09-29 02:03:11 -06005122output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005123{
Scott Moreau4e072362012-09-29 02:03:11 -06005124 struct rectangle allocation = output->allocation;
5125
5126 switch (output->transform) {
5127 case WL_OUTPUT_TRANSFORM_90:
5128 case WL_OUTPUT_TRANSFORM_270:
5129 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5130 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5131 /* Swap width and height */
5132 allocation.width = output->allocation.height;
5133 allocation.height = output->allocation.width;
5134 break;
5135 }
5136
5137 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005138}
5139
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005140struct wl_output *
5141output_get_wl_output(struct output *output)
5142{
5143 return output->output;
5144}
5145
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005146enum wl_output_transform
5147output_get_transform(struct output *output)
5148{
5149 return output->transform;
5150}
5151
Alexander Larssonafd319a2013-05-22 14:41:27 +02005152uint32_t
5153output_get_scale(struct output *output)
5154{
5155 return output->scale;
5156}
5157
Jason Ekstrand738715d2014-04-02 19:53:50 -05005158const char *
5159output_get_make(struct output *output)
5160{
5161 return output->make;
5162}
5163
5164const char *
5165output_get_model(struct output *output)
5166{
5167 return output->model;
5168}
5169
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005170static void
Daniel Stone97f68542012-05-30 16:32:01 +01005171fini_xkb(struct input *input)
5172{
5173 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005174 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005175}
5176
5177static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005178display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005179{
5180 struct input *input;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005181 int seat_version = MIN(display_seat_version, 4);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005182
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005183 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005184 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005185 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005186 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005187 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005188 input->pointer_focus = NULL;
5189 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005190 input->seat_version = seat_version;
5191
Rusty Lynch041815a2013-08-08 21:20:38 -07005192 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005193 wl_list_insert(d->input_list.prev, &input->link);
5194
Daniel Stone37816df2012-05-16 18:45:18 +01005195 wl_seat_add_listener(input->seat, &seat_listener, input);
5196 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005197
Jason Ekstranda669bd52014-04-02 19:53:51 -05005198 if (d->data_device_manager) {
5199 input->data_device =
5200 wl_data_device_manager_get_data_device(d->data_device_manager,
5201 input->seat);
5202 wl_data_device_add_listener(input->data_device,
5203 &data_device_listener,
5204 input);
5205 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005206
5207 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005208 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005209
Derek Foreman118a4292015-04-22 17:23:35 -05005210 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5211 TFD_CLOEXEC | TFD_NONBLOCK);
5212 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5213 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005214 set_repeat_info(input, 40, 400);
5215
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005216 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5217 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005218 input->repeat_task.run = keyboard_repeat_func;
5219 display_watch_fd(d, input->repeat_timer_fd,
5220 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005221}
5222
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005223static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005224input_destroy(struct input *input)
5225{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005226 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005227 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005228
5229 if (input->drag_offer)
5230 data_offer_destroy(input->drag_offer);
5231
5232 if (input->selection_offer)
5233 data_offer_destroy(input->selection_offer);
5234
kabeer khan6ce67ec2014-10-20 11:55:29 +05305235 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005236 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305237 wl_data_device_release(input->data_device);
5238 else
5239 wl_data_device_destroy(input->data_device);
5240 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005241 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005242 if (input->touch)
5243 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005244 if (input->pointer)
5245 wl_pointer_release(input->pointer);
5246 if (input->keyboard)
5247 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005248 } else {
5249 if (input->touch)
5250 wl_touch_destroy(input->touch);
5251 if (input->pointer)
5252 wl_pointer_destroy(input->pointer);
5253 if (input->keyboard)
5254 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005255 }
5256
Daniel Stone97f68542012-05-30 16:32:01 +01005257 fini_xkb(input);
5258
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005259 wl_surface_destroy(input->pointer_surface);
5260
Pekka Paalanene1207c72011-12-16 12:02:09 +02005261 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005262 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005263 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005264 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005265 free(input);
5266}
5267
5268static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005269shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5270{
5271 struct display *d = data;
5272
5273 if (format == WL_SHM_FORMAT_RGB565)
5274 d->has_rgb565 = 1;
5275}
5276
5277struct wl_shm_listener shm_listener = {
5278 shm_format
5279};
5280
5281static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005282xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5283{
5284 xdg_shell_pong(shell, serial);
5285}
5286
5287static const struct xdg_shell_listener xdg_shell_listener = {
5288 xdg_shell_ping,
5289};
5290
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005291#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005292#ifdef static_assert
5293static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5294 "Interface version doesn't match implementation version");
5295#endif
5296
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005297static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005298registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5299 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005300{
5301 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005302 struct global *global;
5303
Brian Lovinbc919262013-08-07 15:34:59 -07005304 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005305 global->name = id;
5306 global->interface = strdup(interface);
5307 global->version = version;
5308 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005309
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005310 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005311 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005312 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005313 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005314 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005315 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005316 display_add_input(d, id, version);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005317 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005318 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005319 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005320 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305321 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005322 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005323 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305324 &wl_data_device_manager_interface,
5325 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005326 } else if (strcmp(interface, "xdg_shell") == 0) {
5327 d->xdg_shell = wl_registry_bind(registry, id,
5328 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005329 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005330 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005331 } else if (strcmp(interface, "text_cursor_position") == 0) {
5332 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005333 wl_registry_bind(registry, id,
5334 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005335 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5336 d->subcompositor =
5337 wl_registry_bind(registry, id,
5338 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005339 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005340 else if (strcmp(interface, "ivi_application") == 0) {
5341 d->ivi_application =
5342 wl_registry_bind(registry, id,
5343 &ivi_application_interface, 1);
5344 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005345
5346 if (d->global_handler)
5347 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005348}
5349
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005350static void
5351registry_handle_global_remove(void *data, struct wl_registry *registry,
5352 uint32_t name)
5353{
5354 struct display *d = data;
5355 struct global *global;
5356 struct global *tmp;
5357
5358 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5359 if (global->name != name)
5360 continue;
5361
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005362 if (strcmp(global->interface, "wl_output") == 0)
5363 display_destroy_output(d, name);
5364
5365 /* XXX: Should destroy remaining bound globals */
5366
5367 if (d->global_handler_remove)
5368 d->global_handler_remove(d, name, global->interface,
5369 global->version, d->user_data);
5370
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005371 wl_list_remove(&global->link);
5372 free(global->interface);
5373 free(global);
5374 }
5375}
5376
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005377void *
5378display_bind(struct display *display, uint32_t name,
5379 const struct wl_interface *interface, uint32_t version)
5380{
5381 return wl_registry_bind(display->registry, name, interface, version);
5382}
5383
5384static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005385 registry_handle_global,
5386 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005387};
5388
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005389#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005390static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005391init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005392{
5393 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005394 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005395
Rob Clark6396ed32012-03-11 19:48:41 -05005396#ifdef USE_CAIRO_GLESV2
5397# define GL_BIT EGL_OPENGL_ES2_BIT
5398#else
5399# define GL_BIT EGL_OPENGL_BIT
5400#endif
5401
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005402 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005403 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005404 EGL_RED_SIZE, 1,
5405 EGL_GREEN_SIZE, 1,
5406 EGL_BLUE_SIZE, 1,
5407 EGL_ALPHA_SIZE, 1,
5408 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005409 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005410 EGL_NONE
5411 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005412
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005413#ifdef USE_CAIRO_GLESV2
5414 static const EGLint context_attribs[] = {
5415 EGL_CONTEXT_CLIENT_VERSION, 2,
5416 EGL_NONE
5417 };
5418 EGLint api = EGL_OPENGL_ES_API;
5419#else
5420 EGLint *context_attribs = NULL;
5421 EGLint api = EGL_OPENGL_API;
5422#endif
5423
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005424 d->dpy =
5425 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5426 d->display, NULL);
5427
Yuval Fledel45568f62010-12-06 09:18:12 -05005428 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005429 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005430 return -1;
5431 }
5432
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005433 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005434 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005435 return -1;
5436 }
5437
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005438 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5439 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005440 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005441 return -1;
5442 }
5443
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005444 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005445 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005446 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005447 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005448 return -1;
5449 }
5450
Benjamin Franzke0c991632011-09-27 21:57:31 +02005451 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5452 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005453 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005454 return -1;
5455 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005456
5457 return 0;
5458}
5459
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005460static void
5461fini_egl(struct display *display)
5462{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005463 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005464
5465 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5466 EGL_NO_CONTEXT);
5467
5468 eglTerminate(display->dpy);
5469 eglReleaseThread();
5470}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005471#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005472
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005473static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005474init_dummy_surface(struct display *display)
5475{
5476 int len;
5477 void *data;
5478
5479 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005480 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005481 display->dummy_surface =
5482 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5483 1, 1, len);
5484 display->dummy_surface_data = data;
5485}
5486
5487static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005488handle_display_data(struct task *task, uint32_t events)
5489{
5490 struct display *display =
5491 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005492 struct epoll_event ep;
5493 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005494
5495 display->display_fd_events = events;
5496
5497 if (events & EPOLLERR || events & EPOLLHUP) {
5498 display_exit(display);
5499 return;
5500 }
5501
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005502 if (events & EPOLLIN) {
5503 ret = wl_display_dispatch(display->display);
5504 if (ret == -1) {
5505 display_exit(display);
5506 return;
5507 }
5508 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005509
5510 if (events & EPOLLOUT) {
5511 ret = wl_display_flush(display->display);
5512 if (ret == 0) {
5513 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5514 ep.data.ptr = &display->display_task;
5515 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5516 display->display_fd, &ep);
5517 } else if (ret == -1 && errno != EAGAIN) {
5518 display_exit(display);
5519 return;
5520 }
5521 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005522}
5523
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005524static void
5525log_handler(const char *format, va_list args)
5526{
5527 vfprintf(stderr, format, args);
5528}
5529
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005530struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005531display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005532{
5533 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005534
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005535 wl_log_set_handler_client(log_handler);
5536
Peter Huttererf3d62272013-08-08 11:57:05 +10005537 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005538 if (d == NULL)
5539 return NULL;
5540
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005541 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005542 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005543 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005544 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005545 return NULL;
5546 }
5547
Rob Bradford5ab9c752013-07-26 16:29:43 +01005548 d->xkb_context = xkb_context_new(0);
5549 if (d->xkb_context == NULL) {
5550 fprintf(stderr, "Failed to create XKB context\n");
5551 free(d);
5552 return NULL;
5553 }
5554
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005555 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005556 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005557 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005558 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5559 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005560
5561 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005562 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005563 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005564 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005565
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005566 d->registry = wl_display_get_registry(d->display);
5567 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005568
Marek Chalupaa519d062014-12-05 13:49:40 +01005569 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005570 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5571 return NULL;
5572 }
5573
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005574#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005575 if (init_egl(d) < 0)
5576 fprintf(stderr, "EGL does not seem to work, "
5577 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005578#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005579
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005580 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005581
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005582 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005583
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005584 wl_list_init(&d->window_list);
5585
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005586 init_dummy_surface(d);
5587
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005588 return d;
5589}
5590
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005591static void
5592display_destroy_outputs(struct display *display)
5593{
5594 struct output *tmp;
5595 struct output *output;
5596
5597 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5598 output_destroy(output);
5599}
5600
Pekka Paalanene1207c72011-12-16 12:02:09 +02005601static void
5602display_destroy_inputs(struct display *display)
5603{
5604 struct input *tmp;
5605 struct input *input;
5606
5607 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5608 input_destroy(input);
5609}
5610
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005611void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005612display_destroy(struct display *display)
5613{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005614 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005615 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5616 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005617
5618 if (!wl_list_empty(&display->deferred_list))
5619 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5620
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005621 cairo_surface_destroy(display->dummy_surface);
5622 free(display->dummy_surface_data);
5623
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005624 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005625 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005626
Daniel Stone97f68542012-05-30 16:32:01 +01005627 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005628
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005629 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005630 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005631
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005632#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005633 if (display->argb_device)
5634 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005635#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005636
Pekka Paalanen35e82632013-04-25 13:57:48 +03005637 if (display->subcompositor)
5638 wl_subcompositor_destroy(display->subcompositor);
5639
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005640 if (display->xdg_shell)
5641 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005642
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005643 if (display->ivi_application)
5644 ivi_application_destroy(display->ivi_application);
5645
Pekka Paalanenc2052982011-12-16 11:41:32 +02005646 if (display->shm)
5647 wl_shm_destroy(display->shm);
5648
5649 if (display->data_device_manager)
5650 wl_data_device_manager_destroy(display->data_device_manager);
5651
5652 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005653 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005654
5655 close(display->epoll_fd);
5656
U. Artie Eoff44874d92012-10-02 21:12:35 -07005657 if (!(display->display_fd_events & EPOLLERR) &&
5658 !(display->display_fd_events & EPOLLHUP))
5659 wl_display_flush(display->display);
5660
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005661 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005662 free(display);
5663}
5664
5665void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005666display_set_user_data(struct display *display, void *data)
5667{
5668 display->user_data = data;
5669}
5670
5671void *
5672display_get_user_data(struct display *display)
5673{
5674 return display->user_data;
5675}
5676
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005677struct wl_display *
5678display_get_display(struct display *display)
5679{
5680 return display->display;
5681}
5682
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005683int
5684display_has_subcompositor(struct display *display)
5685{
5686 if (display->subcompositor)
5687 return 1;
5688
5689 wl_display_roundtrip(display->display);
5690
5691 return display->subcompositor != NULL;
5692}
5693
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005694cairo_device_t *
5695display_get_cairo_device(struct display *display)
5696{
5697 return display->argb_device;
5698}
5699
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005700struct output *
5701display_get_output(struct display *display)
5702{
5703 return container_of(display->output_list.next, struct output, link);
5704}
5705
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005706struct wl_compositor *
5707display_get_compositor(struct display *display)
5708{
5709 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005710}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005711
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005712uint32_t
5713display_get_serial(struct display *display)
5714{
5715 return display->serial;
5716}
5717
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005718EGLDisplay
5719display_get_egl_display(struct display *d)
5720{
5721 return d->dpy;
5722}
5723
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005724struct wl_data_source *
5725display_create_data_source(struct display *display)
5726{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005727 if (display->data_device_manager)
5728 return wl_data_device_manager_create_data_source(display->data_device_manager);
5729 else
5730 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005731}
5732
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005733EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005734display_get_argb_egl_config(struct display *d)
5735{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005736 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005737}
5738
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005739int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005740display_acquire_window_surface(struct display *display,
5741 struct window *window,
5742 EGLContext ctx)
5743{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005744 struct surface *surface = window->main_surface;
5745
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005746 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005747 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005748
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005749 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005750 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005751}
5752
5753void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005754display_release_window_surface(struct display *display,
5755 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005756{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005757 struct surface *surface = window->main_surface;
5758
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005759 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005760 return;
5761
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005762 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005763}
5764
5765void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005766display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005767{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005768 wl_list_insert(&display->deferred_list, &task->link);
5769}
5770
5771void
5772display_watch_fd(struct display *display,
5773 int fd, uint32_t events, struct task *task)
5774{
5775 struct epoll_event ep;
5776
5777 ep.events = events;
5778 ep.data.ptr = task;
5779 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5780}
5781
5782void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005783display_unwatch_fd(struct display *display, int fd)
5784{
5785 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5786}
5787
5788void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005789display_run(struct display *display)
5790{
5791 struct task *task;
5792 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005793 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005794
Pekka Paalanen826d7952011-12-15 10:14:07 +02005795 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005796 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005797 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005798 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005799 struct task, link);
5800 wl_list_remove(&task->link);
5801 task->run(task, 0);
5802 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005803
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005804 wl_display_dispatch_pending(display->display);
5805
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005806 if (!display->running)
5807 break;
5808
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005809 ret = wl_display_flush(display->display);
5810 if (ret < 0 && errno == EAGAIN) {
5811 ep[0].events =
5812 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5813 ep[0].data.ptr = &display->display_task;
5814
5815 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5816 display->display_fd, &ep[0]);
5817 } else if (ret < 0) {
5818 break;
5819 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005820
5821 count = epoll_wait(display->epoll_fd,
5822 ep, ARRAY_LENGTH(ep), -1);
5823 for (i = 0; i < count; i++) {
5824 task = ep[i].data.ptr;
5825 task->run(task, ep[i].events);
5826 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005827 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005828}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005829
5830void
5831display_exit(struct display *display)
5832{
5833 display->running = 0;
5834}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005835
5836void
5837keysym_modifiers_add(struct wl_array *modifiers_map,
5838 const char *name)
5839{
5840 size_t len = strlen(name) + 1;
5841 char *p;
5842
5843 p = wl_array_add(modifiers_map, len);
5844
5845 if (p == NULL)
5846 return;
5847
5848 strncpy(p, name, len);
5849}
5850
5851static xkb_mod_index_t
5852keysym_modifiers_get_index(struct wl_array *modifiers_map,
5853 const char *name)
5854{
5855 xkb_mod_index_t index = 0;
5856 char *p = modifiers_map->data;
5857
5858 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5859 if (strcmp(p, name) == 0)
5860 return index;
5861
5862 index++;
5863 p += strlen(p) + 1;
5864 }
5865
5866 return XKB_MOD_INVALID;
5867}
5868
5869xkb_mod_mask_t
5870keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5871 const char *name)
5872{
5873 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5874
5875 if (index == XKB_MOD_INVALID)
5876 return XKB_MOD_INVALID;
5877
5878 return 1 << index;
5879}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005880
5881void *
5882fail_on_null(void *p)
5883{
5884 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005885 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005886 exit(EXIT_FAILURE);
5887 }
5888
5889 return p;
5890}
5891
5892void *
5893xmalloc(size_t s)
5894{
5895 return fail_on_null(malloc(s));
5896}
5897
Peter Huttererf3d62272013-08-08 11:57:05 +10005898void *
5899xzalloc(size_t s)
5900{
5901 return fail_on_null(zalloc(s));
5902}
5903
Kristian Høgsbergce278412013-07-25 15:20:20 -07005904char *
5905xstrdup(const char *s)
5906{
5907 return fail_on_null(strdup(s));
5908}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005909
5910void *
5911xrealloc(char *p, size_t s)
5912{
5913 return fail_on_null(realloc(p, s));
5914}