blob: 47628debefaab6cceb10bae3c29823b2558cd562 [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"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050071#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060072#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020073#include "workspaces-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070074#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050075
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050076#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050077
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090078#include <sys/types.h>
79#include "ivi-application-client-protocol.h"
80#define IVI_SURFACE_ID 9000
81
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070082struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030083
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040084struct global {
85 uint32_t name;
86 char *interface;
87 uint32_t version;
88 struct wl_list link;
89};
90
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050091struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050092 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040093 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050094 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030095 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040096 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040097 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060098 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020099 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500100 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900101 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500103 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200104 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200105 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400106 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400107
108 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700109 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400110 struct task display_task;
111
112 int epoll_fd;
113 struct wl_list deferred_list;
114
Pekka Paalanen826d7952011-12-15 10:14:07 +0200115 int running;
116
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400117 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400118 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400119 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500120 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400121
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400122 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400123
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300124 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300125 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400126
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200127 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400128 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800129 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200130
131 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100132
133 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200134
135 uint32_t workspace;
136 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200137
138 /* A hack to get text extents for tooltips */
139 cairo_surface_t *dummy_surface;
140 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200141
142 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530143 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500144};
145
Rob Bradford7507b572012-05-15 17:55:34 +0100146struct window_output {
147 struct output *output;
148 struct wl_list link;
149};
150
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200151struct toysurface {
152 /*
153 * Prepare the surface for drawing. Makes sure there is a surface
154 * of the right size available for rendering, and returns it.
155 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200156 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200157 * If flags has SURFACE_HINT_RESIZE set, the user is
158 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200159 * Returns the Cairo surface to draw to.
160 */
161 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200162 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200163 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164
165 /*
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
169 */
170 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200171 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200172 struct rectangle *server_allocation);
173
174 /*
175 * Make the toysurface current with the given EGL context.
176 * Returns 0 on success, and negative of failure.
177 */
178 int (*acquire)(struct toysurface *base, EGLContext ctx);
179
180 /*
181 * Release the toysurface from the EGL context, returning control
182 * to Cairo.
183 */
184 void (*release)(struct toysurface *base);
185
186 /*
187 * Destroy the toysurface, including the Cairo surface, any
188 * backing storage, and the Wayland protocol objects.
189 */
190 void (*destroy)(struct toysurface *base);
191};
192
Pekka Paalanen4e373742013-02-13 16:17:13 +0200193struct surface {
194 struct window *window;
195
196 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300197 struct wl_subsurface *subsurface;
198 int synchronized;
199 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200200 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200201 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300202 int redraw_needed;
203 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300204 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200205
206 struct rectangle allocation;
207 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200208
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200209 struct wl_region *input_region;
210 struct wl_region *opaque_region;
211
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200212 enum window_buffer_type buffer_type;
213 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200214 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200215
216 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300217
218 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200219};
220
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500221struct window {
222 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100223 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500224 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200225 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400226 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500227 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200228 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500229 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400230 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300231 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400232 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400233 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500234 int custom;
235 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400236
Pekka Paalanen99436862012-11-19 17:15:59 +0200237 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400238
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500239 int fullscreen;
240 int maximized;
241
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200242 enum preferred_format preferred_format;
243
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500244 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500245 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400246 window_data_handler_t data_handler;
247 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500248 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400249 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200250 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700251 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400252
Pekka Paalanen4e373742013-02-13 16:17:13 +0200253 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500254 struct xdg_surface *xdg_surface;
255 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300256
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700257 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800258 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500259
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900260 struct ivi_surface *ivi_surface;
261
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500262 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500263
Pekka Paalanen35e82632013-04-25 13:57:48 +0300264 /* struct surface::link, contains also main_surface */
265 struct wl_list subsurface_list;
266
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500267 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400268 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500269};
270
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500271struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500272 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200273 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300274 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500275 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400276 struct wl_list link;
277 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500278 widget_resize_handler_t resize_handler;
279 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500280 widget_enter_handler_t enter_handler;
281 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500282 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500283 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700284 widget_touch_down_handler_t touch_down_handler;
285 widget_touch_up_handler_t touch_up_handler;
286 widget_touch_motion_handler_t touch_motion_handler;
287 widget_touch_frame_handler_t touch_frame_handler;
288 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200289 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400290 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500291 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300292 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500293 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000294 /* If this is set to false then no cairo surface will be
295 * created before redrawing the surface. This is useful if the
296 * redraw handler is going to do completely custom rendering
297 * such as using EGL directly */
298 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400299};
300
Rusty Lynch041815a2013-08-08 21:20:38 -0700301struct touch_point {
302 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800303 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700304 struct widget *widget;
305 struct wl_list link;
306};
307
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400308struct input {
309 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100310 struct wl_seat *seat;
311 struct wl_pointer *pointer;
312 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700313 struct wl_touch *touch;
314 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400315 struct window *pointer_focus;
316 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700317 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300318 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300319 uint32_t cursor_anim_start;
320 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500321 uint32_t cursor_timer_start;
322 uint32_t cursor_anim_current;
323 int cursor_delay_fd;
324 bool cursor_timer_running;
325 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300326 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400327 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400328 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400329 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400330 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400331 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400332
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500333 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500334 struct widget *grab;
335 uint32_t grab_button;
336
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400337 struct wl_data_device *data_device;
338 struct data_offer *drag_offer;
339 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800340 uint32_t touch_grab;
341 int32_t touch_grab_id;
342 float drag_x, drag_y;
343 struct window *drag_focus;
344 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100345
346 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100347 struct xkb_keymap *keymap;
348 struct xkb_state *state;
349 xkb_mod_mask_t control_mask;
350 xkb_mod_mask_t alt_mask;
351 xkb_mod_mask_t shift_mask;
352 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400353
Jonny Lamb06959082014-08-12 14:58:27 +0200354 int32_t repeat_rate_sec;
355 int32_t repeat_rate_nsec;
356 int32_t repeat_delay_sec;
357 int32_t repeat_delay_nsec;
358
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400359 struct task repeat_task;
360 int repeat_timer_fd;
361 uint32_t repeat_sym;
362 uint32_t repeat_key;
363 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500364 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400365};
366
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500367struct output {
368 struct display *display;
369 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800370 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500371 struct rectangle allocation;
372 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600373 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200374 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500375 char *make;
376 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200377
378 display_output_handler_t destroy_handler;
379 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500380};
381
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500382struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500383 struct widget *widget;
384 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500385 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800386
387 uint32_t last_time;
388 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800389 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500390};
391
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500392struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400393 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500394 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500395 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500396 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700397 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500398 const char **entries;
399 uint32_t time;
400 int current;
401 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400402 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500403 menu_func_t func;
404};
405
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300406struct tooltip {
407 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300408 struct widget *widget;
409 char *entry;
410 struct task tooltip_task;
411 int tooltip_fd;
412 float x, y;
413};
414
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700415struct shm_pool {
416 struct wl_shm_pool *pool;
417 size_t size;
418 size_t used;
419 void *data;
420};
421
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400422enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300423 CURSOR_DEFAULT = 100,
424 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400425};
426
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200427static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428
Pekka Paalanen97777442013-05-22 10:20:05 +0300429/* #define DEBUG */
430
431#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300432
433static void
434debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
435__attribute__ ((format (printf, 4, 5)));
436
437static void
438debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
439{
440 va_list ap;
441 struct timeval tv;
442
443 gettimeofday(&tv, NULL);
444 fprintf(stderr, "%8ld.%03ld ",
445 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
446
447 if (proxy)
448 fprintf(stderr, "%s@%d ",
449 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
450
451 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
452 fprintf(stderr, "%s ", func);
453
454 va_start(ap, fmt);
455 vfprintf(stderr, fmt, ap);
456 va_end(ap);
457}
458
459#define DBG(fmt, ...) \
460 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
461
462#define DBG_OBJ(obj, fmt, ...) \
463 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
464
465#else
466
467#define DBG(...) do {} while (0)
468#define DBG_OBJ(...) do {} while (0)
469
470#endif
471
Alexander Larsson1818e312013-05-22 14:41:31 +0200472static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200473surface_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 +0200474{
475 int32_t tmp;
476
477 switch (buffer_transform) {
478 case WL_OUTPUT_TRANSFORM_90:
479 case WL_OUTPUT_TRANSFORM_270:
480 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
481 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
482 tmp = *width;
483 *width = *height;
484 *height = tmp;
485 break;
486 default:
487 break;
488 }
489
490 *width *= buffer_scale;
491 *height *= buffer_scale;
492}
493
494static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200495buffer_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 +0200496{
497 int32_t tmp;
498
499 switch (buffer_transform) {
500 case WL_OUTPUT_TRANSFORM_90:
501 case WL_OUTPUT_TRANSFORM_270:
502 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
503 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
504 tmp = *width;
505 *width = *height;
506 *height = tmp;
507 break;
508 default:
509 break;
510 }
511
512 *width /= buffer_scale;
513 *height /= buffer_scale;
514}
515
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500516#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518struct egl_window_surface {
519 struct toysurface base;
520 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521 struct display *display;
522 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 struct wl_egl_window *egl_window;
524 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525};
526
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527static struct egl_window_surface *
528to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100529{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100531}
532
533static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200535 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200536 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100537{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200538 struct egl_window_surface *surface = to_egl_window_surface(base);
539
Alexander Larsson1818e312013-05-22 14:41:31 +0200540 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
541
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200542 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
543 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
544
545 return cairo_surface_reference(surface->cairo_surface);
546}
547
548static void
549egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200550 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551 struct rectangle *server_allocation)
552{
553 struct egl_window_surface *surface = to_egl_window_surface(base);
554
555 cairo_gl_surface_swapbuffers(surface->cairo_surface);
556 wl_egl_window_get_attached_size(surface->egl_window,
557 &server_allocation->width,
558 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200559
560 buffer_to_surface_size (buffer_transform, buffer_scale,
561 &server_allocation->width,
562 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563}
564
565static int
566egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
567{
568 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200569 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100570
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200571 device = cairo_surface_get_device(surface->cairo_surface);
572 if (!device)
573 return -1;
574
575 if (!ctx) {
576 if (device == surface->display->argb_device)
577 ctx = surface->display->argb_ctx;
578 else
579 assert(0);
580 }
581
582 cairo_device_flush(device);
583 cairo_device_acquire(device);
584 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
585 surface->egl_surface, ctx))
586 fprintf(stderr, "failed to make surface current\n");
587
588 return 0;
589}
590
591static void
592egl_window_surface_release(struct toysurface *base)
593{
594 struct egl_window_surface *surface = to_egl_window_surface(base);
595 cairo_device_t *device;
596
597 device = cairo_surface_get_device(surface->cairo_surface);
598 if (!device)
599 return;
600
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200601 if (!eglMakeCurrent(surface->display->dpy,
602 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200603 fprintf(stderr, "failed to make context current\n");
604
605 cairo_device_release(device);
606}
607
608static void
609egl_window_surface_destroy(struct toysurface *base)
610{
611 struct egl_window_surface *surface = to_egl_window_surface(base);
612 struct display *d = surface->display;
613
614 cairo_surface_destroy(surface->cairo_surface);
615 eglDestroySurface(d->dpy, surface->egl_surface);
616 wl_egl_window_destroy(surface->egl_window);
617 surface->surface = NULL;
618
619 free(surface);
620}
621
622static struct toysurface *
623egl_window_surface_create(struct display *display,
624 struct wl_surface *wl_surface,
625 uint32_t flags,
626 struct rectangle *rectangle)
627{
628 struct egl_window_surface *surface;
629
Pekka Paalanenb3627362012-11-19 17:16:00 +0200630 if (display->dpy == EGL_NO_DISPLAY)
631 return NULL;
632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 surface = calloc(1, sizeof *surface);
634 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 return NULL;
636
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200637 surface->base.prepare = egl_window_surface_prepare;
638 surface->base.swap = egl_window_surface_swap;
639 surface->base.acquire = egl_window_surface_acquire;
640 surface->base.release = egl_window_surface_release;
641 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200643 surface->display = display;
644 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400645
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200646 surface->egl_window = wl_egl_window_create(surface->surface,
647 rectangle->width,
648 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100650 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100651 weston_platform_create_egl_surface(display->dpy,
652 display->argb_config,
653 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100654
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200655 surface->cairo_surface =
656 cairo_gl_surface_create_for_egl(display->argb_device,
657 surface->egl_surface,
658 rectangle->width,
659 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100660
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200661 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100662}
663
Pekka Paalanenb3627362012-11-19 17:16:00 +0200664#else
665
666static struct toysurface *
667egl_window_surface_create(struct display *display,
668 struct wl_surface *wl_surface,
669 uint32_t flags,
670 struct rectangle *rectangle)
671{
672 return NULL;
673}
674
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675#endif
676
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200677struct shm_surface_data {
678 struct wl_buffer *buffer;
679 struct shm_pool *pool;
680};
681
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682struct wl_buffer *
683display_get_buffer_for_surface(struct display *display,
684 cairo_surface_t *surface)
685{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200686 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400687
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200688 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689
690 return data->buffer;
691}
692
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500693static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700694shm_pool_destroy(struct shm_pool *pool);
695
696static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400697shm_surface_data_destroy(void *p)
698{
699 struct shm_surface_data *data = p;
700
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200701 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700702 if (data->pool)
703 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300704
705 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706}
707
Kristian Høgsberg16626282012-04-03 11:21:27 -0400708static struct wl_shm_pool *
709make_shm_pool(struct display *display, int size, void **data)
710{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400711 struct wl_shm_pool *pool;
712 int fd;
713
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300714 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400715 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300716 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
717 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400718 return NULL;
719 }
720
721 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400722 if (*data == MAP_FAILED) {
723 fprintf(stderr, "mmap failed: %m\n");
724 close(fd);
725 return NULL;
726 }
727
728 pool = wl_shm_create_pool(display->shm, fd, size);
729
730 close(fd);
731
732 return pool;
733}
734
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700735static struct shm_pool *
736shm_pool_create(struct display *display, size_t size)
737{
738 struct shm_pool *pool = malloc(sizeof *pool);
739
740 if (!pool)
741 return NULL;
742
743 pool->pool = make_shm_pool(display, size, &pool->data);
744 if (!pool->pool) {
745 free(pool);
746 return NULL;
747 }
748
749 pool->size = size;
750 pool->used = 0;
751
752 return pool;
753}
754
755static void *
756shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
757{
758 if (pool->used + size > pool->size)
759 return NULL;
760
761 *offset = pool->used;
762 pool->used += size;
763
764 return (char *) pool->data + *offset;
765}
766
767/* destroy the pool. this does not unmap the memory though */
768static void
769shm_pool_destroy(struct shm_pool *pool)
770{
771 munmap(pool->data, pool->size);
772 wl_shm_pool_destroy(pool->pool);
773 free(pool);
774}
775
776/* Start allocating from the beginning of the pool again */
777static void
778shm_pool_reset(struct shm_pool *pool)
779{
780 pool->used = 0;
781}
782
783static int
784data_length_for_shm_surface(struct rectangle *rect)
785{
786 int stride;
787
788 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
789 rect->width);
790 return stride * rect->height;
791}
792
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500793static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700794display_create_shm_surface_from_pool(struct display *display,
795 struct rectangle *rectangle,
796 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400797{
798 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400799 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400800 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200801 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700802 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400803 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804
805 data = malloc(sizeof *data);
806 if (data == NULL)
807 return NULL;
808
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200809 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
810 cairo_format = CAIRO_FORMAT_RGB16_565;
811 else
812 cairo_format = CAIRO_FORMAT_ARGB32;
813
814 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700815 length = stride * rectangle->height;
816 data->pool = NULL;
817 map = shm_pool_allocate(pool, length, &offset);
818
819 if (!map) {
820 free(data);
821 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400822 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400824 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200825 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826 rectangle->width,
827 rectangle->height,
828 stride);
829
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200830 cairo_surface_set_user_data(surface, &shm_surface_data_key,
831 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400832
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200833 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
834 format = WL_SHM_FORMAT_RGB565;
835 else {
836 if (flags & SURFACE_OPAQUE)
837 format = WL_SHM_FORMAT_XRGB8888;
838 else
839 format = WL_SHM_FORMAT_ARGB8888;
840 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400841
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200842 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
843 rectangle->width,
844 rectangle->height,
845 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400846
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700847 return surface;
848}
849
850static cairo_surface_t *
851display_create_shm_surface(struct display *display,
852 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200853 struct shm_pool *alternate_pool,
854 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700855{
856 struct shm_surface_data *data;
857 struct shm_pool *pool;
858 cairo_surface_t *surface;
859
Pekka Paalanen99436862012-11-19 17:15:59 +0200860 if (alternate_pool) {
861 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700862 surface = display_create_shm_surface_from_pool(display,
863 rectangle,
864 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200865 alternate_pool);
866 if (surface) {
867 data = cairo_surface_get_user_data(surface,
868 &shm_surface_data_key);
869 goto out;
870 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700871 }
872
873 pool = shm_pool_create(display,
874 data_length_for_shm_surface(rectangle));
875 if (!pool)
876 return NULL;
877
878 surface =
879 display_create_shm_surface_from_pool(display, rectangle,
880 flags, pool);
881
882 if (!surface) {
883 shm_pool_destroy(pool);
884 return NULL;
885 }
886
887 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200888 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700889 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890
Pekka Paalanen99436862012-11-19 17:15:59 +0200891out:
892 if (data_ret)
893 *data_ret = data;
894
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400895 return surface;
896}
897
nobled7b87cb02011-02-01 18:51:47 +0000898static int
899check_size(struct rectangle *rect)
900{
901 if (rect->width && rect->height)
902 return 0;
903
904 fprintf(stderr, "tried to create surface of "
905 "width: %d, height: %d\n", rect->width, rect->height);
906 return -1;
907}
908
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909cairo_surface_t *
910display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400912 struct rectangle *rectangle,
913 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400914{
nobled7b87cb02011-02-01 18:51:47 +0000915 if (check_size(rectangle) < 0)
916 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200917
918 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200919 return display_create_shm_surface(display, rectangle, flags,
920 NULL, NULL);
921}
922
Pekka Paalanena4eda732012-11-19 17:16:02 +0200923struct shm_surface_leaf {
924 cairo_surface_t *cairo_surface;
925 /* 'data' is automatically destroyed, when 'cairo_surface' is */
926 struct shm_surface_data *data;
927
928 struct shm_pool *resize_pool;
929 int busy;
930};
931
932static void
933shm_surface_leaf_release(struct shm_surface_leaf *leaf)
934{
935 if (leaf->cairo_surface)
936 cairo_surface_destroy(leaf->cairo_surface);
937 /* leaf->data already destroyed via cairo private */
938
939 if (leaf->resize_pool)
940 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200941
942 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200943}
944
Pekka Paalanenaef02542013-04-25 13:57:47 +0300945#define MAX_LEAVES 3
946
Pekka Paalanen99436862012-11-19 17:15:59 +0200947struct shm_surface {
948 struct toysurface base;
949 struct display *display;
950 struct wl_surface *surface;
951 uint32_t flags;
952 int dx, dy;
953
Pekka Paalanenaef02542013-04-25 13:57:47 +0300954 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200955 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200956};
957
958static struct shm_surface *
959to_shm_surface(struct toysurface *base)
960{
961 return container_of(base, struct shm_surface, base);
962}
963
Pekka Paalanena4eda732012-11-19 17:16:02 +0200964static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300965shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
966{
967#ifdef DEBUG
968 struct shm_surface_leaf *leaf;
969 char bufs[MAX_LEAVES + 1];
970 int i;
971
972 for (i = 0; i < MAX_LEAVES; i++) {
973 leaf = &surface->leaf[i];
974
975 if (leaf->busy)
976 bufs[i] = 'b';
977 else if (leaf->cairo_surface)
978 bufs[i] = 'a';
979 else
980 bufs[i] = ' ';
981 }
982
983 bufs[MAX_LEAVES] = '\0';
984 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
985#endif
986}
987
988static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200989shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
990{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200991 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300992 struct shm_surface_leaf *leaf;
993 int i;
994 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300995
996 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200997
Pekka Paalanenaef02542013-04-25 13:57:47 +0300998 for (i = 0; i < MAX_LEAVES; i++) {
999 leaf = &surface->leaf[i];
1000 if (leaf->data && leaf->data->buffer == buffer) {
1001 leaf->busy = 0;
1002 break;
1003 }
1004 }
1005 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001006
Pekka Paalanenaef02542013-04-25 13:57:47 +03001007 /* Leave one free leaf with storage, release others */
1008 free_found = 0;
1009 for (i = 0; i < MAX_LEAVES; i++) {
1010 leaf = &surface->leaf[i];
1011
1012 if (!leaf->cairo_surface || leaf->busy)
1013 continue;
1014
1015 if (!free_found)
1016 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001017 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001018 shm_surface_leaf_release(leaf);
1019 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001020
Pekka Paalanen97777442013-05-22 10:20:05 +03001021 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001022}
1023
1024static const struct wl_buffer_listener shm_surface_buffer_listener = {
1025 shm_surface_buffer_release
1026};
1027
Pekka Paalanen99436862012-11-19 17:15:59 +02001028static cairo_surface_t *
1029shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001030 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001031 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001032{
Pekka Paalanenec076692012-11-30 13:37:27 +02001033 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001034 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001035 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 struct shm_surface_leaf *leaf = NULL;
1037 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001038
1039 surface->dx = dx;
1040 surface->dy = dy;
1041
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001042 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001043 for (i = 0; i < MAX_LEAVES; i++) {
1044 if (surface->leaf[i].busy)
1045 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001046
Pekka Paalanenaef02542013-04-25 13:57:47 +03001047 if (!leaf || surface->leaf[i].cairo_surface)
1048 leaf = &surface->leaf[i];
1049 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001050 DBG_OBJ(surface->surface, "pick leaf %d\n",
1051 (int)(leaf - &surface->leaf[0]));
1052
Pekka Paalanenaef02542013-04-25 13:57:47 +03001053 if (!leaf) {
1054 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001056 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001057 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001058 }
1059
Pekka Paalanena4eda732012-11-19 17:16:02 +02001060 if (!resize_hint && leaf->resize_pool) {
1061 cairo_surface_destroy(leaf->cairo_surface);
1062 leaf->cairo_surface = NULL;
1063 shm_pool_destroy(leaf->resize_pool);
1064 leaf->resize_pool = NULL;
1065 }
1066
Alexander Larsson1818e312013-05-22 14:41:31 +02001067 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1068
Pekka Paalanena4eda732012-11-19 17:16:02 +02001069 if (leaf->cairo_surface &&
1070 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1071 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001072 goto out;
1073
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 if (leaf->cairo_surface)
1075 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001077#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001078 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001079 /* Create a big pool to allocate from, while continuously
1080 * resizing. Mmapping a new pool in the server
1081 * is relatively expensive, so reusing a pool performs
1082 * better, but may temporarily reserve unneeded memory.
1083 */
1084 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001085 leaf->resize_pool = shm_pool_create(surface->display,
1086 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001087 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001088#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001089
Alexander Larsson1818e312013-05-22 14:41:31 +02001090 rect.width = width;
1091 rect.height = height;
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001094 display_create_shm_surface(surface->display, &rect,
1095 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 leaf->resize_pool,
1097 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001098 if (!leaf->cairo_surface)
1099 return NULL;
1100
Pekka Paalanena4eda732012-11-19 17:16:02 +02001101 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001102 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001103
1104out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 surface->current = leaf;
1106
1107 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001108}
1109
1110static void
1111shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001112 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001113 struct rectangle *server_allocation)
1114{
1115 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001117
1118 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001119 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001120 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001121 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001122
Alexander Larsson1818e312013-05-22 14:41:31 +02001123 buffer_to_surface_size (buffer_transform, buffer_scale,
1124 &server_allocation->width,
1125 &server_allocation->height);
1126
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001128 surface->dx, surface->dy);
1129 wl_surface_damage(surface->surface, 0, 0,
1130 server_allocation->width, server_allocation->height);
1131 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001132
Pekka Paalanen71233882013-04-25 13:57:53 +03001133 DBG_OBJ(surface->surface, "leaf %d busy\n",
1134 (int)(leaf - &surface->leaf[0]));
1135
Pekka Paalanena4eda732012-11-19 17:16:02 +02001136 leaf->busy = 1;
1137 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001138}
1139
1140static int
1141shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1142{
1143 return -1;
1144}
1145
1146static void
1147shm_surface_release(struct toysurface *base)
1148{
1149}
1150
1151static void
1152shm_surface_destroy(struct toysurface *base)
1153{
1154 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001155 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001156
Pekka Paalanenaef02542013-04-25 13:57:47 +03001157 for (i = 0; i < MAX_LEAVES; i++)
1158 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001159
1160 free(surface);
1161}
1162
1163static struct toysurface *
1164shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1165 uint32_t flags, struct rectangle *rectangle)
1166{
1167 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001168 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001169
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001170 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001171 surface->base.prepare = shm_surface_prepare;
1172 surface->base.swap = shm_surface_swap;
1173 surface->base.acquire = shm_surface_acquire;
1174 surface->base.release = shm_surface_release;
1175 surface->base.destroy = shm_surface_destroy;
1176
1177 surface->display = display;
1178 surface->surface = wl_surface;
1179 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001180
1181 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001182}
1183
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001184/*
1185 * The following correspondences between file names and cursors was copied
1186 * from: https://bugs.kde.org/attachment.cgi?id=67313
1187 */
1188
1189static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001190 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001191 "sw-resize",
1192 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001193};
1194
1195static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001196 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001197 "se-resize",
1198 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001199};
1200
1201static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001202 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001203 "s-resize",
1204 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001205};
1206
1207static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001209 "closedhand",
1210 "208530c400c041818281048008011002"
1211};
1212
1213static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001214 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215 "default",
1216 "top_left_arrow",
1217 "left-arrow"
1218};
1219
1220static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "w-resize",
1223 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "e-resize",
1229 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001234 "nw-resize",
1235 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236};
1237
1238static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001240 "ne-resize",
1241 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242};
1243
1244static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001246 "n-resize",
1247 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248};
1249
1250static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001251 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252 "ibeam",
1253 "text"
1254};
1255
1256static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001257 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258 "pointer",
1259 "pointing_hand",
1260 "e29285e634086352946a0e7090d73106"
1261};
1262
1263static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001264 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001265 "wait",
1266 "0426c94ea35c87780ff01dc239897213"
1267};
1268
1269struct cursor_alternatives {
1270 const char **names;
1271 size_t count;
1272};
1273
1274static const struct cursor_alternatives cursors[] = {
1275 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1276 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1277 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1278 {grabbings, ARRAY_LENGTH(grabbings)},
1279 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1280 {left_sides, ARRAY_LENGTH(left_sides)},
1281 {right_sides, ARRAY_LENGTH(right_sides)},
1282 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1283 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1284 {top_sides, ARRAY_LENGTH(top_sides)},
1285 {xterms, ARRAY_LENGTH(xterms)},
1286 {hand1s, ARRAY_LENGTH(hand1s)},
1287 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001288};
1289
1290static void
1291create_cursors(struct display *display)
1292{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001293 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001294 struct weston_config *config;
1295 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001296 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001297 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001298 unsigned int i, j;
1299 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001300
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001301 config_file = weston_config_get_name_from_env();
1302 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001303 s = weston_config_get_section(config, "shell", NULL, NULL);
1304 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1305 weston_config_section_get_int(s, "cursor-size", &size, 32);
1306 weston_config_destroy(config);
1307
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001308 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001309 if (!display->cursor_theme) {
1310 fprintf(stderr, "could not load theme '%s'\n", theme);
1311 return;
1312 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001313 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001314 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001315 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001316
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001317 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001318 cursor = NULL;
1319 for (j = 0; !cursor && j < cursors[i].count; ++j)
1320 cursor = wl_cursor_theme_get_cursor(
1321 display->cursor_theme, cursors[i].names[j]);
1322
1323 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001324 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001325 cursors[i].names[0]);
1326
1327 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001328 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001329}
1330
1331static void
1332destroy_cursors(struct display *display)
1333{
1334 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001335 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001336}
1337
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001338struct wl_cursor_image *
1339display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001340{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001341 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001342
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001343 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001344}
1345
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001346static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001347surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001348{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001349 if (!surface->cairo_surface)
1350 return;
1351
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001352 if (surface->opaque_region) {
1353 wl_surface_set_opaque_region(surface->surface,
1354 surface->opaque_region);
1355 wl_region_destroy(surface->opaque_region);
1356 surface->opaque_region = NULL;
1357 }
1358
1359 if (surface->input_region) {
1360 wl_surface_set_input_region(surface->surface,
1361 surface->input_region);
1362 wl_region_destroy(surface->input_region);
1363 surface->input_region = NULL;
1364 }
1365
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001366 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001367 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001368 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001369
Pekka Paalanen89dee002013-02-13 16:17:20 +02001370 cairo_surface_destroy(surface->cairo_surface);
1371 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001372}
1373
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001374int
1375window_has_focus(struct window *window)
1376{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001377 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001378}
1379
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001380static void
1381window_close(struct window *window)
1382{
1383 if (window->close_handler)
1384 window->close_handler(window->user_data);
1385 else
1386 display_exit(window->display);
1387}
1388
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001389struct display *
1390window_get_display(struct window *window)
1391{
1392 return window->display;
1393}
1394
Pekka Paalanen89dee002013-02-13 16:17:20 +02001395static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001396handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1397 int32_t width, int32_t height)
1398{
1399 struct window *window = data;
1400
1401 window_schedule_resize(window, width, height);
1402}
1403
1404static const struct ivi_surface_listener ivi_surface_listener = {
1405 handle_ivi_surface_configure,
1406};
1407
1408static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001409surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001410{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001411 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001412 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001413
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001414 if (!surface->toysurface && display->dpy &&
1415 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001416 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001417 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001419 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001420 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001421 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001422
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001423 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001425 surface->surface,
1426 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001427
Pekka Paalanen89dee002013-02-13 16:17:20 +02001428 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001429 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001430 allocation.width, allocation.height, flags,
1431 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001432}
1433
1434static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001435window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001436{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001437 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001438 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001439
Pekka Paalanenec076692012-11-30 13:37:27 +02001440 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001441 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001442
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001443 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1444 flags |= SURFACE_HINT_RGB565;
1445
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001446 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001447}
1448
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001449int
1450window_get_buffer_transform(struct window *window)
1451{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001452 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001453}
1454
1455void
1456window_set_buffer_transform(struct window *window,
1457 enum wl_output_transform transform)
1458{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001459 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001460 wl_surface_set_buffer_transform(window->main_surface->surface,
1461 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001462}
1463
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001464void
1465window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001466 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001467{
1468 window->main_surface->buffer_scale = scale;
1469 wl_surface_set_buffer_scale(window->main_surface->surface,
1470 scale);
1471}
1472
1473uint32_t
1474window_get_buffer_scale(struct window *window)
1475{
1476 return window->main_surface->buffer_scale;
1477}
1478
Alexander Larssond68f5232013-05-22 14:41:33 +02001479uint32_t
1480window_get_output_scale(struct window *window)
1481{
1482 struct window_output *window_output;
1483 struct window_output *window_output_tmp;
1484 int scale = 1;
1485
1486 wl_list_for_each_safe(window_output, window_output_tmp,
1487 &window->window_output_list, link) {
1488 if (window_output->output->scale > scale)
1489 scale = window_output->output->scale;
1490 }
1491
1492 return scale;
1493}
1494
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001495static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001496
Pekka Paalanen4e373742013-02-13 16:17:13 +02001497static void
1498surface_destroy(struct surface *surface)
1499{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001500 if (surface->frame_cb)
1501 wl_callback_destroy(surface->frame_cb);
1502
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001503 if (surface->input_region)
1504 wl_region_destroy(surface->input_region);
1505
1506 if (surface->opaque_region)
1507 wl_region_destroy(surface->opaque_region);
1508
Pekka Paalanen35e82632013-04-25 13:57:48 +03001509 if (surface->subsurface)
1510 wl_subsurface_destroy(surface->subsurface);
1511
Pekka Paalanen4e373742013-02-13 16:17:13 +02001512 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001513
1514 if (surface->toysurface)
1515 surface->toysurface->destroy(surface->toysurface);
1516
Pekka Paalanen35e82632013-04-25 13:57:48 +03001517 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001518 free(surface);
1519}
1520
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001521void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001522window_destroy(struct window *window)
1523{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001524 struct display *display = window->display;
1525 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001526 struct window_output *window_output;
1527 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001528
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001529 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001530
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001531 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001532 if (input->touch_focus == window)
1533 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001534 if (input->pointer_focus == window)
1535 input->pointer_focus = NULL;
1536 if (input->keyboard_focus == window)
1537 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001538 if (input->focus_widget &&
1539 input->focus_widget->window == window)
1540 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001541 }
1542
Rob Bradford7507b572012-05-15 17:55:34 +01001543 wl_list_for_each_safe(window_output, window_output_tmp,
1544 &window->window_output_list, link) {
1545 free (window_output);
1546 }
1547
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001548 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001549 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001550
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001551 if (window->xdg_surface)
1552 xdg_surface_destroy(window->xdg_surface);
1553 if (window->xdg_popup)
1554 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001555
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001556 if (window->ivi_surface)
1557 ivi_surface_destroy(window->ivi_surface);
1558
Pekka Paalanen4e373742013-02-13 16:17:13 +02001559 surface_destroy(window->main_surface);
1560
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001561 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001562
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001563 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001564 free(window);
1565}
1566
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001567static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001568widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001569{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001570 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001571
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001572 wl_list_for_each(child, &widget->child_list, link) {
1573 target = widget_find_widget(child, x, y);
1574 if (target)
1575 return target;
1576 }
1577
1578 if (widget->allocation.x <= x &&
1579 x < widget->allocation.x + widget->allocation.width &&
1580 widget->allocation.y <= y &&
1581 y < widget->allocation.y + widget->allocation.height) {
1582 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001583 }
1584
1585 return NULL;
1586}
1587
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001588static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001589window_find_widget(struct window *window, int32_t x, int32_t y)
1590{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001591 struct surface *surface;
1592 struct widget *widget;
1593
1594 wl_list_for_each(surface, &window->subsurface_list, link) {
1595 widget = widget_find_widget(surface->widget, x, y);
1596 if (widget)
1597 return widget;
1598 }
1599
1600 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001601}
1602
1603static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001604widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001605{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001606 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001607
Peter Huttererf3d62272013-08-08 11:57:05 +10001608 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001609 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001610 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001611 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001612 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001613 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001614 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001615 widget->tooltip = NULL;
1616 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001617 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001618 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619
1620 return widget;
1621}
1622
1623struct widget *
1624window_add_widget(struct window *window, void *data)
1625{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001626 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001627
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001628 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001629 wl_list_init(&widget->link);
1630 window->main_surface->widget = widget;
1631
1632 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001633}
1634
1635struct widget *
1636widget_add_widget(struct widget *parent, void *data)
1637{
1638 struct widget *widget;
1639
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001640 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001641 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001642
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001643 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001644}
1645
1646void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001647widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001648{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001649 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001650 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001651 struct input *input;
1652
Pekka Paalanen35e82632013-04-25 13:57:48 +03001653 /* Destroy the sub-surface along with the root widget */
1654 if (surface->widget == widget && surface->subsurface)
1655 surface_destroy(widget->surface);
1656
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001657 if (widget->tooltip)
1658 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001659
Pekka Paalanene156fb62012-01-19 13:51:38 +02001660 wl_list_for_each(input, &display->input_list, link) {
1661 if (input->focus_widget == widget)
1662 input->focus_widget = NULL;
1663 }
1664
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001665 wl_list_remove(&widget->link);
1666 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001667}
1668
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001669void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001670widget_set_default_cursor(struct widget *widget, int cursor)
1671{
1672 widget->default_cursor = cursor;
1673}
1674
1675void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001676widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001677{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001678 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001679}
1680
1681void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001682widget_set_size(struct widget *widget, int32_t width, int32_t height)
1683{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001684 widget->allocation.width = width;
1685 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001686}
1687
1688void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001689widget_set_allocation(struct widget *widget,
1690 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001691{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001692 widget->allocation.x = x;
1693 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001694 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001695}
1696
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001697void
1698widget_set_transparent(struct widget *widget, int transparent)
1699{
1700 widget->opaque = !transparent;
1701}
1702
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001704widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001705{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001706 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001707}
1708
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001709static cairo_surface_t *
1710widget_get_cairo_surface(struct widget *widget)
1711{
1712 struct surface *surface = widget->surface;
1713 struct window *window = widget->window;
1714
Neil Roberts97b747c2013-12-19 16:17:12 +00001715 assert(widget->use_cairo);
1716
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001717 if (!surface->cairo_surface) {
1718 if (surface == window->main_surface)
1719 window_create_main_surface(window);
1720 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001721 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001722 }
1723
1724 return surface->cairo_surface;
1725}
1726
Alexander Larsson15901f02013-05-22 14:41:25 +02001727static void
1728widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1729{
1730 struct surface *surface = widget->surface;
1731 double angle;
1732 cairo_matrix_t m;
1733 enum wl_output_transform transform;
1734 int surface_width, surface_height;
1735 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001736 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001737
1738 surface_width = surface->allocation.width;
1739 surface_height = surface->allocation.height;
1740
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001741 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001742 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001743
Alexander Larsson15901f02013-05-22 14:41:25 +02001744 switch (transform) {
1745 case WL_OUTPUT_TRANSFORM_FLIPPED:
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1748 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1749 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1750 break;
1751 default:
1752 cairo_matrix_init_identity(&m);
1753 break;
1754 }
1755
1756 switch (transform) {
1757 case WL_OUTPUT_TRANSFORM_NORMAL:
1758 default:
1759 angle = 0;
1760 translate_x = 0;
1761 translate_y = 0;
1762 break;
1763 case WL_OUTPUT_TRANSFORM_FLIPPED:
1764 angle = 0;
1765 translate_x = surface_width;
1766 translate_y = 0;
1767 break;
1768 case WL_OUTPUT_TRANSFORM_90:
1769 angle = M_PI_2;
1770 translate_x = surface_height;
1771 translate_y = 0;
1772 break;
1773 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1774 angle = M_PI_2;
1775 translate_x = surface_height;
1776 translate_y = surface_width;
1777 break;
1778 case WL_OUTPUT_TRANSFORM_180:
1779 angle = M_PI;
1780 translate_x = surface_width;
1781 translate_y = surface_height;
1782 break;
1783 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1784 angle = M_PI;
1785 translate_x = 0;
1786 translate_y = surface_height;
1787 break;
1788 case WL_OUTPUT_TRANSFORM_270:
1789 angle = M_PI + M_PI_2;
1790 translate_x = 0;
1791 translate_y = surface_width;
1792 break;
1793 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1794 angle = M_PI + M_PI_2;
1795 translate_x = 0;
1796 translate_y = 0;
1797 break;
1798 }
1799
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001800 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001801 cairo_translate(cr, translate_x, translate_y);
1802 cairo_rotate(cr, angle);
1803 cairo_transform(cr, &m);
1804}
1805
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001806cairo_t *
1807widget_cairo_create(struct widget *widget)
1808{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001809 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001810 cairo_surface_t *cairo_surface;
1811 cairo_t *cr;
1812
1813 cairo_surface = widget_get_cairo_surface(widget);
1814 cr = cairo_create(cairo_surface);
1815
Alexander Larsson15901f02013-05-22 14:41:25 +02001816 widget_cairo_update_transform(widget, cr);
1817
Pekka Paalanen35e82632013-04-25 13:57:48 +03001818 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1819
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001820 return cr;
1821}
1822
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001823struct wl_surface *
1824widget_get_wl_surface(struct widget *widget)
1825{
1826 return widget->surface->surface;
1827}
1828
Neil Roberts5e10a042013-09-09 00:40:17 +01001829struct wl_subsurface *
1830widget_get_wl_subsurface(struct widget *widget)
1831{
1832 return widget->surface->subsurface;
1833}
1834
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001835uint32_t
1836widget_get_last_time(struct widget *widget)
1837{
1838 return widget->surface->last_time;
1839}
1840
1841void
1842widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1843{
1844 struct wl_compositor *comp = widget->window->display->compositor;
1845 struct surface *surface = widget->surface;
1846
1847 if (!surface->input_region)
1848 surface->input_region = wl_compositor_create_region(comp);
1849
1850 if (rect) {
1851 wl_region_add(surface->input_region,
1852 rect->x, rect->y, rect->width, rect->height);
1853 }
1854}
1855
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001856void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001857widget_set_resize_handler(struct widget *widget,
1858 widget_resize_handler_t handler)
1859{
1860 widget->resize_handler = handler;
1861}
1862
1863void
1864widget_set_redraw_handler(struct widget *widget,
1865 widget_redraw_handler_t handler)
1866{
1867 widget->redraw_handler = handler;
1868}
1869
1870void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001871widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001872{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001873 widget->enter_handler = handler;
1874}
1875
1876void
1877widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1878{
1879 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001880}
1881
1882void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001883widget_set_motion_handler(struct widget *widget,
1884 widget_motion_handler_t handler)
1885{
1886 widget->motion_handler = handler;
1887}
1888
1889void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001890widget_set_button_handler(struct widget *widget,
1891 widget_button_handler_t handler)
1892{
1893 widget->button_handler = handler;
1894}
1895
1896void
Rusty Lynch041815a2013-08-08 21:20:38 -07001897widget_set_touch_up_handler(struct widget *widget,
1898 widget_touch_up_handler_t handler)
1899{
1900 widget->touch_up_handler = handler;
1901}
1902
1903void
1904widget_set_touch_down_handler(struct widget *widget,
1905 widget_touch_down_handler_t handler)
1906{
1907 widget->touch_down_handler = handler;
1908}
1909
1910void
1911widget_set_touch_motion_handler(struct widget *widget,
1912 widget_touch_motion_handler_t handler)
1913{
1914 widget->touch_motion_handler = handler;
1915}
1916
1917void
1918widget_set_touch_frame_handler(struct widget *widget,
1919 widget_touch_frame_handler_t handler)
1920{
1921 widget->touch_frame_handler = handler;
1922}
1923
1924void
1925widget_set_touch_cancel_handler(struct widget *widget,
1926 widget_touch_cancel_handler_t handler)
1927{
1928 widget->touch_cancel_handler = handler;
1929}
1930
1931void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001932widget_set_axis_handler(struct widget *widget,
1933 widget_axis_handler_t handler)
1934{
1935 widget->axis_handler = handler;
1936}
1937
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001938static void
1939window_schedule_redraw_task(struct window *window);
1940
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001941void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001942widget_schedule_redraw(struct widget *widget)
1943{
Pekka Paalanen71233882013-04-25 13:57:53 +03001944 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001945 widget->surface->redraw_needed = 1;
1946 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001947}
1948
Neil Roberts97b747c2013-12-19 16:17:12 +00001949void
1950widget_set_use_cairo(struct widget *widget,
1951 int use_cairo)
1952{
1953 widget->use_cairo = use_cairo;
1954}
1955
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001956cairo_surface_t *
1957window_get_surface(struct window *window)
1958{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001959 cairo_surface_t *cairo_surface;
1960
1961 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1962
1963 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001964}
1965
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001966struct wl_surface *
1967window_get_wl_surface(struct window *window)
1968{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001969 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001970}
1971
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001972static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001973tooltip_redraw_handler(struct widget *widget, void *data)
1974{
1975 cairo_t *cr;
1976 const int32_t r = 3;
1977 struct tooltip *tooltip = data;
1978 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001979
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001980 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001981 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001982 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1983 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1984 cairo_paint(cr);
1985
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001986 width = widget->allocation.width;
1987 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001988 rounded_rect(cr, 0, 0, width, height, r);
1989
1990 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1991 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1992 cairo_fill(cr);
1993
1994 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1995 cairo_move_to(cr, 10, 16);
1996 cairo_show_text(cr, tooltip->entry);
1997 cairo_destroy(cr);
1998}
1999
2000static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002001get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002002{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002003 cairo_t *cr;
2004 cairo_text_extents_t extents;
2005
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002006 /* Use the dummy_surface because tooltip's surface was not
2007 * created yet, and parent does not have a valid surface
2008 * outside repaint, either.
2009 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002010 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002011 cairo_text_extents(cr, tooltip->entry, &extents);
2012 cairo_destroy(cr);
2013
2014 return extents;
2015}
2016
2017static int
2018window_create_tooltip(struct tooltip *tooltip)
2019{
2020 struct widget *parent = tooltip->parent;
2021 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002022 const int offset_y = 27;
2023 const int margin = 3;
2024 cairo_text_extents_t extents;
2025
2026 if (tooltip->widget)
2027 return 0;
2028
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002029 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002030
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002031 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002032 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002033 widget_set_allocation(tooltip->widget,
2034 tooltip->x, tooltip->y + offset_y,
2035 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002036
2037 return 0;
2038}
2039
2040void
2041widget_destroy_tooltip(struct widget *parent)
2042{
2043 struct tooltip *tooltip = parent->tooltip;
2044
2045 parent->tooltip_count = 0;
2046 if (!tooltip)
2047 return;
2048
2049 if (tooltip->widget) {
2050 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002051 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002052 }
2053
2054 close(tooltip->tooltip_fd);
2055 free(tooltip->entry);
2056 free(tooltip);
2057 parent->tooltip = NULL;
2058}
2059
2060static void
2061tooltip_func(struct task *task, uint32_t events)
2062{
2063 struct tooltip *tooltip =
2064 container_of(task, struct tooltip, tooltip_task);
2065 uint64_t exp;
2066
Martin Olsson8df662a2012-07-08 03:03:47 +02002067 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2068 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002069 window_create_tooltip(tooltip);
2070}
2071
2072#define TOOLTIP_TIMEOUT 500
2073static int
2074tooltip_timer_reset(struct tooltip *tooltip)
2075{
2076 struct itimerspec its;
2077
2078 its.it_interval.tv_sec = 0;
2079 its.it_interval.tv_nsec = 0;
2080 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2081 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2082 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2083 fprintf(stderr, "could not set timerfd\n: %m");
2084 return -1;
2085 }
2086
2087 return 0;
2088}
2089
2090int
2091widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2092{
2093 struct tooltip *tooltip = parent->tooltip;
2094
2095 parent->tooltip_count++;
2096 if (tooltip) {
2097 tooltip->x = x;
2098 tooltip->y = y;
2099 tooltip_timer_reset(tooltip);
2100 return 0;
2101 }
2102
2103 /* the handler might be triggered too fast via input device motion, so
2104 * we need this check here to make sure tooltip is fully initialized */
2105 if (parent->tooltip_count > 1)
2106 return 0;
2107
2108 tooltip = malloc(sizeof *tooltip);
2109 if (!tooltip)
2110 return -1;
2111
2112 parent->tooltip = tooltip;
2113 tooltip->parent = parent;
2114 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002115 tooltip->x = x;
2116 tooltip->y = y;
2117 tooltip->entry = strdup(entry);
2118 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2119 if (tooltip->tooltip_fd < 0) {
2120 fprintf(stderr, "could not create timerfd\n: %m");
2121 return -1;
2122 }
2123
2124 tooltip->tooltip_task.run = tooltip_func;
2125 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2126 EPOLLIN, &tooltip->tooltip_task);
2127 tooltip_timer_reset(tooltip);
2128
2129 return 0;
2130}
2131
2132static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002133workspace_manager_state(void *data,
2134 struct workspace_manager *workspace_manager,
2135 uint32_t current,
2136 uint32_t count)
2137{
2138 struct display *display = data;
2139
2140 display->workspace = current;
2141 display->workspace_count = count;
2142}
2143
2144static const struct workspace_manager_listener workspace_manager_listener = {
2145 workspace_manager_state
2146};
2147
2148static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002149frame_resize_handler(struct widget *widget,
2150 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002151{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002152 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002153 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002154 struct rectangle interior;
2155 struct rectangle input;
2156 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002157
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002158 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002159 interior.x = 0;
2160 interior.y = 0;
2161 interior.width = width;
2162 interior.height = height;
2163 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002164 frame_resize(frame->frame, width, height);
2165 frame_interior(frame->frame, &interior.x, &interior.y,
2166 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002167 }
2168
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002169 widget_set_allocation(child, interior.x, interior.y,
2170 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002171
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 if (child->resize_handler) {
2173 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174 child->user_data);
2175
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002176 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002177 width = child->allocation.width;
2178 height = child->allocation.height;
2179 } else {
2180 frame_resize_inside(frame->frame,
2181 child->allocation.width,
2182 child->allocation.height);
2183 width = frame_width(frame->frame);
2184 height = frame_height(frame->frame);
2185 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002186 }
2187
Scott Moreauf7e498c2012-05-14 11:39:29 -06002188 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002189
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 widget->surface->input_region =
2191 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002192 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 frame_input_rect(frame->frame, &input.x, &input.y,
2194 &input.width, &input.height);
2195 wl_region_add(widget->surface->input_region,
2196 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002197 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002199 }
2200
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002201 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002202
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002203 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002204 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002205 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2206 &opaque.width, &opaque.height);
2207
2208 wl_region_add(widget->surface->opaque_region,
2209 opaque.x, opaque.y,
2210 opaque.width, opaque.height);
2211 } else {
2212 wl_region_add(widget->surface->opaque_region,
2213 0, 0, width, height);
2214 }
Martin Minarik1998b152012-05-10 02:04:35 +02002215 }
2216
Martin Minarik1998b152012-05-10 02:04:35 +02002217
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219}
2220
2221static void
2222frame_redraw_handler(struct widget *widget, void *data)
2223{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002225 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226 struct window *window = widget->window;
2227
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002228 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002229 return;
2230
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002231 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002232
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002233 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002234
2235 cairo_destroy(cr);
2236}
2237
2238static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002239frame_get_pointer_image_for_location(struct window_frame *frame,
2240 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002241{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002242 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002243
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002244 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002245 return CURSOR_LEFT_PTR;
2246
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002247 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002248 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002249 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002250 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002251 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002252 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002253 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002254 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002255 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002257 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002258 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002259 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002260 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002261 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002262 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002263 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002264 case THEME_LOCATION_EXTERIOR:
2265 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002266 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002267 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002268 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002269}
2270
Pekka Paalanen26237862014-09-10 15:10:30 +03002271static void
2272frame_menu_func(void *data, struct input *input, int index)
2273{
2274 struct window *window = data;
2275 struct display *display;
2276
2277 switch (index) {
2278 case 0: /* close */
2279 window_close(window);
2280 break;
2281 case 1: /* move to workspace above */
2282 display = window->display;
2283 if (display->workspace > 0)
2284 workspace_manager_move_surface(
2285 display->workspace_manager,
2286 window->main_surface->surface,
2287 display->workspace - 1);
2288 break;
2289 case 2: /* move to workspace below */
2290 display = window->display;
2291 if (display->workspace < display->workspace_count - 1)
2292 workspace_manager_move_surface(
2293 display->workspace_manager,
2294 window->main_surface->surface,
2295 display->workspace + 1);
2296 break;
2297 case 3: /* fullscreen */
2298 /* we don't have a way to get out of fullscreen for now */
2299 if (window->fullscreen_handler)
2300 window->fullscreen_handler(window, window->user_data);
2301 break;
2302 }
2303}
2304
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002305void
2306window_show_frame_menu(struct window *window,
2307 struct input *input, uint32_t time)
2308{
2309 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002310 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002311
Pekka Paalanen26237862014-09-10 15:10:30 +03002312 static const char *entries[] = {
2313 "Close",
2314 "Move to workspace above", "Move to workspace below",
2315 "Fullscreen"
2316 };
2317
2318 if (window->fullscreen_handler)
2319 count = ARRAY_LENGTH(entries);
2320 else
2321 count = ARRAY_LENGTH(entries) - 1;
2322
2323 input_get_position(input, &x, &y);
2324 window_show_menu(window->display, input, time, window,
2325 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002326}
2327
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002328static int
2329frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002330 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002331{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002332 struct window_frame *frame = data;
2333 enum theme_location location;
2334
2335 location = frame_pointer_enter(frame->frame, input, x, y);
2336 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2337 widget_schedule_redraw(frame->widget);
2338
2339 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002340}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002341
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002342static int
2343frame_motion_handler(struct widget *widget,
2344 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002345 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002346{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002347 struct window_frame *frame = data;
2348 enum theme_location location;
2349
2350 location = frame_pointer_motion(frame->frame, input, x, y);
2351 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2352 widget_schedule_redraw(frame->widget);
2353
2354 return frame_get_pointer_image_for_location(data, location);
2355}
2356
2357static void
2358frame_leave_handler(struct widget *widget,
2359 struct input *input, void *data)
2360{
2361 struct window_frame *frame = data;
2362
2363 frame_pointer_leave(frame->frame, input);
2364 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2365 widget_schedule_redraw(frame->widget);
2366}
2367
2368static void
2369frame_handle_status(struct window_frame *frame, struct input *input,
2370 uint32_t time, enum theme_location location)
2371{
2372 struct window *window = frame->widget->window;
2373 uint32_t status;
2374
2375 status = frame_status(frame->frame);
2376 if (status & FRAME_STATUS_REPAINT)
2377 widget_schedule_redraw(frame->widget);
2378
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002379 if (status & FRAME_STATUS_MINIMIZE) {
2380 window_set_minimized(window);
2381 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2382 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002383
2384 if (status & FRAME_STATUS_MENU) {
2385 window_show_frame_menu(window, input, time);
2386 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2387 }
2388
2389 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002390 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002391 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2392 }
2393
2394 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002395 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002396 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002397 }
2398
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002399 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002400 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002401 xdg_surface_move(window->xdg_surface,
2402 input_get_seat(input),
2403 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002404
2405 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2406 }
2407
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002408 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002409 input_ungrab(input);
2410
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002411 xdg_surface_resize(window->xdg_surface,
2412 input_get_seat(input),
2413 window->display->serial,
2414 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002415
2416 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2417 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002418}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002419
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002420#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002422frame_button_handler(struct widget *widget,
2423 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002424 uint32_t button, enum wl_pointer_button_state state,
2425 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002426
2427{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002428 struct window_frame *frame = data;
2429 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002430
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002431 frame->double_click = 0;
2432 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2433 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2434 frame->double_click = 1;
2435 frame->did_double = 1;
2436 } else
2437 frame->did_double = 0;
2438
2439 frame->last_time = time;
2440 } else if (frame->did_double == 1) {
2441 frame->double_click = 1;
2442 frame->did_double = 0;
2443 }
2444
2445 if (frame->double_click)
2446 location = frame_double_click(frame->frame, input,
2447 button, state);
2448 else
2449 location = frame_pointer_button(frame->frame, input,
2450 button, state);
2451
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002452 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002453}
2454
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002455static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002456frame_touch_down_handler(struct widget *widget, struct input *input,
2457 uint32_t serial, uint32_t time, int32_t id,
2458 float x, float y, void *data)
2459{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002460 struct window_frame *frame = data;
2461
Xiong Zhang382de462014-06-12 11:06:26 +08002462 frame->double_click = 0;
2463 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2464 frame->last_id == id) {
2465 frame->double_click = 1;
2466 frame->did_double = 1;
2467 frame->double_id = id;
2468 } else
2469 frame->did_double = 0;
2470
2471 frame->last_time = time;
2472 frame->last_id = id;
2473
2474 if (frame->double_click)
2475 frame_double_touch_down(frame->frame, input, id, x, y);
2476 else
2477 frame_touch_down(frame->frame, input, id, x, y);
2478
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002479 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2480}
2481
2482static void
2483frame_touch_up_handler(struct widget *widget,
2484 struct input *input, uint32_t serial, uint32_t time,
2485 int32_t id, void *data)
2486{
2487 struct window_frame *frame = data;
2488
Xiong Zhang382de462014-06-12 11:06:26 +08002489 if (frame->double_id == id && frame->did_double) {
2490 frame->did_double = 0;
2491 frame->double_id = 0;
2492 frame_double_touch_up(frame->frame, input, id);
2493 } else
2494 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002495 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002496}
2497
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002499window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002500{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002501 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002502 uint32_t buttons;
2503
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002504 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002505 buttons = FRAME_BUTTON_NONE;
2506 } else {
2507 buttons = FRAME_BUTTON_ALL;
2508 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002509
Peter Huttererf3d62272013-08-08 11:57:05 +10002510 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002511 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002512 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002513
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002514 frame->widget = window_add_widget(window, frame);
2515 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002516
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002517 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2518 widget_set_resize_handler(frame->widget, frame_resize_handler);
2519 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002520 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002521 widget_set_motion_handler(frame->widget, frame_motion_handler);
2522 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002523 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002524 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002525
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002526 window->frame = frame;
2527
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002528 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002529}
2530
Kristian Høgsberga1627922012-06-20 17:30:03 -04002531void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002532window_frame_set_child_size(struct widget *widget, int child_width,
2533 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002534{
2535 struct display *display = widget->window->display;
2536 struct theme *t = display->theme;
2537 int decoration_width, decoration_height;
2538 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002539 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002540
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002541 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002542 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002543 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002544 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002545
2546 width = child_width + decoration_width;
2547 height = child_height + decoration_height;
2548 } else {
2549 width = child_width;
2550 height = child_height;
2551 }
2552
2553 window_schedule_resize(widget->window, width, height);
2554}
2555
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002556static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002557window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002558{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002559 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002560
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002561 /* frame->child must be destroyed by the application */
2562 widget_destroy(frame->widget);
2563 free(frame);
2564}
2565
2566static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002567input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002568 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002569{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002570 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002571 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002572
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002573 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002574 return;
2575
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002576 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002577 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002578 widget = old;
2579 if (input->grab)
2580 widget = input->grab;
2581 if (widget->leave_handler)
2582 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002583 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002584 }
2585
2586 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002587 widget = focus;
2588 if (input->grab)
2589 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002590 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002591 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002592 cursor = widget->enter_handler(focus, input, x, y,
2593 widget->user_data);
2594 else
2595 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002596
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002597 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002598 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002599}
2600
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002601void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002602touch_grab(struct input *input, int32_t touch_id)
2603{
2604 input->touch_grab = 1;
2605 input->touch_grab_id = touch_id;
2606}
2607
2608void
2609touch_ungrab(struct input *input)
2610{
2611 struct touch_point *tp, *tmp;
2612
2613 input->touch_grab = 0;
2614
2615 wl_list_for_each_safe(tp, tmp,
2616 &input->touch_point_list, link) {
2617 if (tp->id != input->touch_grab_id)
2618 continue;
2619 wl_list_remove(&tp->link);
2620 free(tp);
2621
2622 return;
2623 }
2624}
2625
2626void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002627input_grab(struct input *input, struct widget *widget, uint32_t button)
2628{
2629 input->grab = widget;
2630 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002631
2632 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002633}
2634
2635void
2636input_ungrab(struct input *input)
2637{
2638 struct widget *widget;
2639
2640 input->grab = NULL;
2641 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002642 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002643 input->sx, input->sy);
2644 input_set_focus_widget(input, widget, input->sx, input->sy);
2645 }
2646}
2647
2648static void
Derek Foreman118a4292015-04-22 17:23:35 -05002649cursor_delay_timer_reset(struct input *input, uint32_t duration)
2650{
2651 struct itimerspec its;
2652
2653 if (!duration)
2654 input->cursor_timer_running = false;
2655 else
2656 input->cursor_timer_running = true;
2657
2658 its.it_interval.tv_sec = 0;
2659 its.it_interval.tv_nsec = 0;
2660 its.it_value.tv_sec = duration / 1000;
2661 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2662 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2663 fprintf(stderr, "could not set cursor timerfd\n: %m");
2664}
2665
2666static void cancel_pointer_image_update(struct input *input)
2667{
2668 if (input->cursor_timer_running)
2669 cursor_delay_timer_reset(input, 0);
2670}
2671
2672static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002673input_remove_pointer_focus(struct input *input)
2674{
2675 struct window *window = input->pointer_focus;
2676
2677 if (!window)
2678 return;
2679
2680 input_set_focus_widget(input, NULL, 0, 0);
2681
2682 input->pointer_focus = NULL;
2683 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002684 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002685}
2686
2687static void
2688pointer_handle_enter(void *data, struct wl_pointer *pointer,
2689 uint32_t serial, struct wl_surface *surface,
2690 wl_fixed_t sx_w, wl_fixed_t sy_w)
2691{
2692 struct input *input = data;
2693 struct window *window;
2694 struct widget *widget;
2695 float sx = wl_fixed_to_double(sx_w);
2696 float sy = wl_fixed_to_double(sy_w);
2697
2698 if (!surface) {
2699 /* enter event for a window we've just destroyed */
2700 return;
2701 }
2702
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002703 window = wl_surface_get_user_data(surface);
2704 if (surface != window->main_surface->surface) {
2705 DBG("Ignoring input event from subsurface %p\n", surface);
2706 return;
2707 }
2708
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002709 input->display->serial = serial;
2710 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002711 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002712
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002713 input->sx = sx;
2714 input->sy = sy;
2715
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002716 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002717 input_set_focus_widget(input, widget, sx, sy);
2718}
2719
2720static void
2721pointer_handle_leave(void *data, struct wl_pointer *pointer,
2722 uint32_t serial, struct wl_surface *surface)
2723{
2724 struct input *input = data;
2725
2726 input->display->serial = serial;
2727 input_remove_pointer_focus(input);
2728}
2729
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002730static void
Daniel Stone37816df2012-05-16 18:45:18 +01002731pointer_handle_motion(void *data, struct wl_pointer *pointer,
2732 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002733{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002734 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002735 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002736 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002737 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002738 float sx = wl_fixed_to_double(sx_w);
2739 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002740
Paul Winwoodb22bf572013-08-29 10:52:54 +01002741 if (!window)
2742 return;
2743
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002744 input->sx = sx;
2745 input->sy = sy;
2746
Rob Bradford5f087742013-07-11 19:41:27 +01002747 /* when making the window smaller - e.g. after a unmaximise we might
2748 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002749 * based on the old surface dimensions. However, if we have an active
2750 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002751 */
Derek Foreman46812b62015-08-26 17:13:27 -05002752 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002753 sy < window->main_surface->allocation.y ||
2754 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002755 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002756 return;
2757
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002758 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002759 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002760 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002761 }
2762
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002763 if (input->grab)
2764 widget = input->grab;
2765 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002766 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002767 if (widget) {
2768 if (widget->motion_handler)
2769 cursor = widget->motion_handler(input->focus_widget,
2770 input, time, sx, sy,
2771 widget->user_data);
2772 else
2773 cursor = widget->default_cursor;
2774 } else
2775 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002776
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002777 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002778}
2779
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002780static void
Daniel Stone37816df2012-05-16 18:45:18 +01002781pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002782 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002783{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002784 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002785 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002786 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002787
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002788 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002789 if (input->focus_widget && input->grab == NULL &&
2790 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002791 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002792
Neil Roberts6b28aad2012-01-23 19:11:18 +00002793 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002794 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002795 (*widget->button_handler)(widget,
2796 input, time,
2797 button, state,
2798 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002799
Daniel Stone4dbadb12012-05-30 16:31:51 +01002800 if (input->grab && input->grab_button == button &&
2801 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002802 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002803}
2804
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002805static void
Daniel Stone37816df2012-05-16 18:45:18 +01002806pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002807 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002808{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002809 struct input *input = data;
2810 struct widget *widget;
2811
2812 widget = input->focus_widget;
2813 if (input->grab)
2814 widget = input->grab;
2815 if (widget && widget->axis_handler)
2816 (*widget->axis_handler)(widget,
2817 input, time,
2818 axis, value,
2819 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002820}
2821
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002822static const struct wl_pointer_listener pointer_listener = {
2823 pointer_handle_enter,
2824 pointer_handle_leave,
2825 pointer_handle_motion,
2826 pointer_handle_button,
2827 pointer_handle_axis,
2828};
2829
2830static void
2831input_remove_keyboard_focus(struct input *input)
2832{
2833 struct window *window = input->keyboard_focus;
2834 struct itimerspec its;
2835
2836 its.it_interval.tv_sec = 0;
2837 its.it_interval.tv_nsec = 0;
2838 its.it_value.tv_sec = 0;
2839 its.it_value.tv_nsec = 0;
2840 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2841
2842 if (!window)
2843 return;
2844
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002845 if (window->keyboard_focus_handler)
2846 (*window->keyboard_focus_handler)(window, NULL,
2847 window->user_data);
2848
2849 input->keyboard_focus = NULL;
2850}
2851
2852static void
2853keyboard_repeat_func(struct task *task, uint32_t events)
2854{
2855 struct input *input =
2856 container_of(task, struct input, repeat_task);
2857 struct window *window = input->keyboard_focus;
2858 uint64_t exp;
2859
2860 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2861 /* If we change the timer between the fd becoming
2862 * readable and getting here, there'll be nothing to
2863 * read and we get EAGAIN. */
2864 return;
2865
2866 if (window && window->key_handler) {
2867 (*window->key_handler)(window, input, input->repeat_time,
2868 input->repeat_key, input->repeat_sym,
2869 WL_KEYBOARD_KEY_STATE_PRESSED,
2870 window->user_data);
2871 }
2872}
2873
2874static void
2875keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2876 uint32_t format, int fd, uint32_t size)
2877{
2878 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002879 struct xkb_keymap *keymap;
2880 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002881 char *map_str;
2882
2883 if (!data) {
2884 close(fd);
2885 return;
2886 }
2887
2888 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2889 close(fd);
2890 return;
2891 }
2892
2893 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2894 if (map_str == MAP_FAILED) {
2895 close(fd);
2896 return;
2897 }
2898
Ran Benita2e1968f2014-08-19 23:59:51 +03002899 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2900 map_str,
2901 XKB_KEYMAP_FORMAT_TEXT_V1,
2902 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002903 munmap(map_str, size);
2904 close(fd);
2905
Rui Matos3eccb862013-10-10 19:44:22 +02002906 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002907 fprintf(stderr, "failed to compile keymap\n");
2908 return;
2909 }
2910
Rui Matos3eccb862013-10-10 19:44:22 +02002911 state = xkb_state_new(keymap);
2912 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002913 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002914 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002915 return;
2916 }
2917
Rui Matos3eccb862013-10-10 19:44:22 +02002918 xkb_keymap_unref(input->xkb.keymap);
2919 xkb_state_unref(input->xkb.state);
2920 input->xkb.keymap = keymap;
2921 input->xkb.state = state;
2922
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002923 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002924 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002925 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002926 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002927 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002928 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002929}
2930
2931static void
2932keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2933 uint32_t serial, struct wl_surface *surface,
2934 struct wl_array *keys)
2935{
2936 struct input *input = data;
2937 struct window *window;
2938
2939 input->display->serial = serial;
2940 input->keyboard_focus = wl_surface_get_user_data(surface);
2941
2942 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002943 if (window->keyboard_focus_handler)
2944 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002945 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002946}
2947
2948static void
2949keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2950 uint32_t serial, struct wl_surface *surface)
2951{
2952 struct input *input = data;
2953
2954 input->display->serial = serial;
2955 input_remove_keyboard_focus(input);
2956}
2957
Scott Moreau210d0792012-03-22 10:47:01 -06002958static void
Daniel Stone37816df2012-05-16 18:45:18 +01002959keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002960 uint32_t serial, uint32_t time, uint32_t key,
2961 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002962{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002963 struct input *input = data;
2964 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002965 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002966 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002967 const xkb_keysym_t *syms;
2968 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002969 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002970
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002971 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002972 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002973 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002974 return;
2975
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002976 /* We only use input grabs for pointer events for now, so just
2977 * ignore key presses if a grab is active. We expand the key
2978 * event delivery mechanism to route events to widgets to
2979 * properly handle key grabs. In the meantime, this prevents
2980 * key event devlivery while a grab is active. */
2981 if (input->grab && input->grab_button == 0)
2982 return;
2983
Ran Benita2e1968f2014-08-19 23:59:51 +03002984 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002985
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002986 sym = XKB_KEY_NoSymbol;
2987 if (num_syms == 1)
2988 sym = syms[0];
2989
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002990
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002991 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002992 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002993 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002994 } else if (sym == XKB_KEY_F11 &&
2995 window->fullscreen_handler &&
2996 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2997 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002998 } else if (sym == XKB_KEY_F4 &&
2999 input->modifiers == MOD_ALT_MASK &&
3000 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003001 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003002 } else if (window->key_handler) {
3003 (*window->key_handler)(window, input, time, key,
3004 sym, state, window->user_data);
3005 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003006
3007 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3008 key == input->repeat_key) {
3009 its.it_interval.tv_sec = 0;
3010 its.it_interval.tv_nsec = 0;
3011 its.it_value.tv_sec = 0;
3012 its.it_value.tv_nsec = 0;
3013 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003014 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3015 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003016 input->repeat_sym = sym;
3017 input->repeat_key = key;
3018 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003019 its.it_interval.tv_sec = input->repeat_rate_sec;
3020 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3021 its.it_value.tv_sec = input->repeat_delay_sec;
3022 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003023 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3024 }
3025}
3026
3027static void
Daniel Stone351eb612012-05-31 15:27:47 -04003028keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3029 uint32_t serial, uint32_t mods_depressed,
3030 uint32_t mods_latched, uint32_t mods_locked,
3031 uint32_t group)
3032{
3033 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003034 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003035
Matt Ropere61561f2013-06-24 16:52:43 +01003036 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3037 if (!input->xkb.keymap)
3038 return;
3039
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003040 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3041 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003042 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003043 XKB_STATE_MODS_DEPRESSED |
3044 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003045 input->modifiers = 0;
3046 if (mask & input->xkb.control_mask)
3047 input->modifiers |= MOD_CONTROL_MASK;
3048 if (mask & input->xkb.alt_mask)
3049 input->modifiers |= MOD_ALT_MASK;
3050 if (mask & input->xkb.shift_mask)
3051 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003052}
3053
Jonny Lamb06959082014-08-12 14:58:27 +02003054static void
3055set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3056{
3057 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3058 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3059
3060 /* a rate of zero disables any repeating, regardless of the delay's
3061 * value */
3062 if (rate == 0)
3063 return;
3064
3065 if (rate == 1)
3066 input->repeat_rate_sec = 1;
3067 else
3068 input->repeat_rate_nsec = 1000000000 / rate;
3069
3070 input->repeat_delay_sec = delay / 1000;
3071 delay -= (input->repeat_delay_sec * 1000);
3072 input->repeat_delay_nsec = delay * 1000 * 1000;
3073}
3074
3075static void
3076keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3077 int32_t rate, int32_t delay)
3078{
3079 struct input *input = data;
3080
3081 set_repeat_info(input, rate, delay);
3082}
3083
Daniel Stone37816df2012-05-16 18:45:18 +01003084static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003085 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003086 keyboard_handle_enter,
3087 keyboard_handle_leave,
3088 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003089 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003090 keyboard_handle_repeat_info
3091
Daniel Stone37816df2012-05-16 18:45:18 +01003092};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003093
3094static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003095touch_handle_down(void *data, struct wl_touch *wl_touch,
3096 uint32_t serial, uint32_t time, struct wl_surface *surface,
3097 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3098{
3099 struct input *input = data;
3100 struct widget *widget;
3101 float sx = wl_fixed_to_double(x_w);
3102 float sy = wl_fixed_to_double(y_w);
3103
Rusty Lynch1084da52013-08-15 09:10:08 -07003104 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003105 input->touch_focus = wl_surface_get_user_data(surface);
3106 if (!input->touch_focus) {
3107 DBG("Failed to find to touch focus for surface %p\n", surface);
3108 return;
3109 }
3110
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003111 if (surface != input->touch_focus->main_surface->surface) {
3112 DBG("Ignoring input event from subsurface %p\n", surface);
3113 input->touch_focus = NULL;
3114 return;
3115 }
3116
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003117 if (input->grab)
3118 widget = input->grab;
3119 else
3120 widget = window_find_widget(input->touch_focus,
3121 wl_fixed_to_double(x_w),
3122 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003123 if (widget) {
3124 struct touch_point *tp = xmalloc(sizeof *tp);
3125 if (tp) {
3126 tp->id = id;
3127 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003128 tp->x = sx;
3129 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003130 wl_list_insert(&input->touch_point_list, &tp->link);
3131
3132 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003133 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003134 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003135 sx, sy,
3136 widget->user_data);
3137 }
3138 }
3139}
3140
3141static void
3142touch_handle_up(void *data, struct wl_touch *wl_touch,
3143 uint32_t serial, uint32_t time, int32_t id)
3144{
3145 struct input *input = data;
3146 struct touch_point *tp, *tmp;
3147
Rusty Lynch041815a2013-08-08 21:20:38 -07003148 if (!input->touch_focus) {
3149 DBG("No touch focus found for touch up event!\n");
3150 return;
3151 }
3152
3153 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3154 if (tp->id != id)
3155 continue;
3156
3157 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003158 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003159 time, id,
3160 tp->widget->user_data);
3161
3162 wl_list_remove(&tp->link);
3163 free(tp);
3164
3165 return;
3166 }
3167}
3168
3169static void
3170touch_handle_motion(void *data, struct wl_touch *wl_touch,
3171 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3172{
3173 struct input *input = data;
3174 struct touch_point *tp;
3175 float sx = wl_fixed_to_double(x_w);
3176 float sy = wl_fixed_to_double(y_w);
3177
3178 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3179
3180 if (!input->touch_focus) {
3181 DBG("No touch focus found for touch motion event!\n");
3182 return;
3183 }
3184
3185 wl_list_for_each(tp, &input->touch_point_list, link) {
3186 if (tp->id != id)
3187 continue;
3188
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003189 tp->x = sx;
3190 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003191 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003192 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003193 id, sx, sy,
3194 tp->widget->user_data);
3195 return;
3196 }
3197}
3198
3199static void
3200touch_handle_frame(void *data, struct wl_touch *wl_touch)
3201{
3202 struct input *input = data;
3203 struct touch_point *tp, *tmp;
3204
3205 DBG("touch_handle_frame\n");
3206
3207 if (!input->touch_focus) {
3208 DBG("No touch focus found for touch frame event!\n");
3209 return;
3210 }
3211
3212 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3213 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003214 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003215 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003216 }
3217}
3218
3219static void
3220touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3221{
3222 struct input *input = data;
3223 struct touch_point *tp, *tmp;
3224
3225 DBG("touch_handle_cancel\n");
3226
3227 if (!input->touch_focus) {
3228 DBG("No touch focus found for touch cancel event!\n");
3229 return;
3230 }
3231
3232 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3233 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003234 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3235 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003236
3237 wl_list_remove(&tp->link);
3238 free(tp);
3239 }
3240}
3241
3242static const struct wl_touch_listener touch_listener = {
3243 touch_handle_down,
3244 touch_handle_up,
3245 touch_handle_motion,
3246 touch_handle_frame,
3247 touch_handle_cancel,
3248};
3249
3250static void
Daniel Stone37816df2012-05-16 18:45:18 +01003251seat_handle_capabilities(void *data, struct wl_seat *seat,
3252 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003253{
Daniel Stone37816df2012-05-16 18:45:18 +01003254 struct input *input = data;
3255
3256 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3257 input->pointer = wl_seat_get_pointer(seat);
3258 wl_pointer_set_user_data(input->pointer, input);
3259 wl_pointer_add_listener(input->pointer, &pointer_listener,
3260 input);
3261 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003262 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003263 wl_pointer_release(input->pointer);
3264 else
3265 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003266 input->pointer = NULL;
3267 }
3268
3269 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3270 input->keyboard = wl_seat_get_keyboard(seat);
3271 wl_keyboard_set_user_data(input->keyboard, input);
3272 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3273 input);
3274 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003275 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003276 wl_keyboard_release(input->keyboard);
3277 else
3278 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003279 input->keyboard = NULL;
3280 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003281
3282 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3283 input->touch = wl_seat_get_touch(seat);
3284 wl_touch_set_user_data(input->touch, input);
3285 wl_touch_add_listener(input->touch, &touch_listener, input);
3286 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003287 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003288 wl_touch_release(input->touch);
3289 else
3290 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003291 input->touch = NULL;
3292 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003293}
3294
Rob Bradford08031182013-08-13 20:11:03 +01003295static void
3296seat_handle_name(void *data, struct wl_seat *seat,
3297 const char *name)
3298{
3299
3300}
3301
Daniel Stone37816df2012-05-16 18:45:18 +01003302static const struct wl_seat_listener seat_listener = {
3303 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003304 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003305};
3306
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003307void
3308input_get_position(struct input *input, int32_t *x, int32_t *y)
3309{
3310 *x = input->sx;
3311 *y = input->sy;
3312}
3313
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003314int
3315input_get_touch(struct input *input, int32_t id, float *x, float *y)
3316{
3317 struct touch_point *tp;
3318
3319 wl_list_for_each(tp, &input->touch_point_list, link) {
3320 if (tp->id != id)
3321 continue;
3322
3323 *x = tp->x;
3324 *y = tp->y;
3325 return 0;
3326 }
3327
3328 return -1;
3329}
3330
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003331struct display *
3332input_get_display(struct input *input)
3333{
3334 return input->display;
3335}
3336
Daniel Stone37816df2012-05-16 18:45:18 +01003337struct wl_seat *
3338input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003339{
Daniel Stone37816df2012-05-16 18:45:18 +01003340 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003341}
3342
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003343uint32_t
3344input_get_modifiers(struct input *input)
3345{
3346 return input->modifiers;
3347}
3348
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003349struct widget *
3350input_get_focus_widget(struct input *input)
3351{
3352 return input->focus_widget;
3353}
3354
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003355struct data_offer {
3356 struct wl_data_offer *offer;
3357 struct input *input;
3358 struct wl_array types;
3359 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003360
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003361 struct task io_task;
3362 int fd;
3363 data_func_t func;
3364 int32_t x, y;
3365 void *user_data;
3366};
3367
3368static void
3369data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3370{
3371 struct data_offer *offer = data;
3372 char **p;
3373
3374 p = wl_array_add(&offer->types, sizeof *p);
3375 *p = strdup(type);
3376}
3377
3378static const struct wl_data_offer_listener data_offer_listener = {
3379 data_offer_offer,
3380};
3381
3382static void
3383data_offer_destroy(struct data_offer *offer)
3384{
3385 char **p;
3386
3387 offer->refcount--;
3388 if (offer->refcount == 0) {
3389 wl_data_offer_destroy(offer->offer);
3390 for (p = offer->types.data; *p; p++)
3391 free(*p);
3392 wl_array_release(&offer->types);
3393 free(offer);
3394 }
3395}
3396
3397static void
3398data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003399 struct wl_data_device *data_device,
3400 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003401{
3402 struct data_offer *offer;
3403
Brian Lovinbc919262013-08-07 15:34:59 -07003404 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003405
3406 wl_array_init(&offer->types);
3407 offer->refcount = 1;
3408 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003409 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003410 wl_data_offer_add_listener(offer->offer,
3411 &data_offer_listener, offer);
3412}
3413
3414static void
3415data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003416 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003417 wl_fixed_t x_w, wl_fixed_t y_w,
3418 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003419{
3420 struct input *input = data;
3421 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003422 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003423 float x = wl_fixed_to_double(x_w);
3424 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003425 char **p;
3426
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003427 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003428 input->drag_enter_serial = serial;
3429 input->drag_focus = window,
3430 input->drag_x = x;
3431 input->drag_y = y;
3432
3433 if (!input->touch_grab)
3434 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003435
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003436 if (offer) {
3437 input->drag_offer = wl_data_offer_get_user_data(offer);
3438
3439 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3440 *p = NULL;
3441
3442 types_data = input->drag_offer->types.data;
3443 } else {
3444 input->drag_offer = NULL;
3445 types_data = NULL;
3446 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003447
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003448 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003449 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003450 window->user_data);
3451}
3452
3453static void
3454data_device_leave(void *data, struct wl_data_device *data_device)
3455{
3456 struct input *input = data;
3457
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003458 if (input->drag_offer) {
3459 data_offer_destroy(input->drag_offer);
3460 input->drag_offer = NULL;
3461 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003462}
3463
3464static void
3465data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003466 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003467{
3468 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003469 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003470 float x = wl_fixed_to_double(x_w);
3471 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003472 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003473
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003474 input->drag_x = x;
3475 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003476
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003477 if (input->drag_offer)
3478 types_data = input->drag_offer->types.data;
3479 else
3480 types_data = NULL;
3481
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003482 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003483 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003484 window->user_data);
3485}
3486
3487static void
3488data_device_drop(void *data, struct wl_data_device *data_device)
3489{
3490 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003491 struct window *window = input->drag_focus;
3492 float x, y;
3493
3494 x = input->drag_x;
3495 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003496
3497 if (window->drop_handler)
3498 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003499 x, y, window->user_data);
3500
3501 if (input->touch_grab)
3502 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003503}
3504
3505static void
3506data_device_selection(void *data,
3507 struct wl_data_device *wl_data_device,
3508 struct wl_data_offer *offer)
3509{
3510 struct input *input = data;
3511 char **p;
3512
3513 if (input->selection_offer)
3514 data_offer_destroy(input->selection_offer);
3515
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003516 if (offer) {
3517 input->selection_offer = wl_data_offer_get_user_data(offer);
3518 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3519 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003520 } else {
3521 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003522 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003523}
3524
3525static const struct wl_data_device_listener data_device_listener = {
3526 data_device_data_offer,
3527 data_device_enter,
3528 data_device_leave,
3529 data_device_motion,
3530 data_device_drop,
3531 data_device_selection
3532};
3533
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003534static void
3535input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003536{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003537 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003538 struct wl_cursor *cursor;
3539 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003540
Daniel Stone80972742012-11-07 17:51:39 +11003541 if (!input->pointer)
3542 return;
3543
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003544 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003545 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003546 return;
3547
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003548 if (index >= (int) cursor->image_count) {
3549 fprintf(stderr, "cursor index out of range\n");
3550 return;
3551 }
3552
3553 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003554 buffer = wl_cursor_image_get_buffer(image);
3555 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003556 return;
3557
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003558 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3559 wl_surface_damage(input->pointer_surface, 0, 0,
3560 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003561 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003562 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3563 input->pointer_surface,
3564 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003565}
3566
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003567static const struct wl_callback_listener pointer_surface_listener;
3568
Derek Foreman493d9792015-03-04 16:26:25 -06003569static bool
3570input_set_pointer_special(struct input *input)
3571{
3572 if (input->current_cursor == CURSOR_BLANK) {
3573 wl_pointer_set_cursor(input->pointer,
3574 input->pointer_enter_serial,
3575 NULL, 0, 0);
3576 return true;
3577 }
3578
3579 if (input->current_cursor == CURSOR_UNSET)
3580 return true;
3581
3582 return false;
3583}
3584
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003585static void
Derek Foreman118a4292015-04-22 17:23:35 -05003586schedule_pointer_image_update(struct input *input,
3587 struct wl_cursor *cursor,
3588 uint32_t duration,
3589 bool force_frame)
3590{
3591 /* Some silly cursor sets have enormous pauses in them. In these
3592 * cases it's better to use a timer even if it results in less
3593 * accurate presentation, since it will save us having to set the
3594 * same cursor image over and over again.
3595 *
3596 * This is really not the way we're supposed to time any kind of
3597 * animation, but we're pretending it's OK here because we don't
3598 * want animated cursors with long delays to needlessly hog CPU.
3599 *
3600 * We use force_frame to ensure we don't accumulate large timing
3601 * errors by running off the wrong clock.
3602 */
3603 if (!force_frame && duration > 100) {
3604 struct timespec tp;
3605
3606 clock_gettime(CLOCK_MONOTONIC, &tp);
3607 input->cursor_timer_start = tp.tv_sec * 1000
3608 + tp.tv_nsec / 1000000;
3609 cursor_delay_timer_reset(input, duration);
3610 return;
3611 }
3612
3613 /* for short durations we'll just spin on frame callbacks for
3614 * accurate timing - the way any kind of timing sensitive animation
3615 * should really be done. */
3616 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3617 wl_callback_add_listener(input->cursor_frame_cb,
3618 &pointer_surface_listener, input);
3619
3620}
3621
3622static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003623pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3624 uint32_t time)
3625{
3626 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003627 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003628 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003629 uint32_t duration;
3630 bool force_frame = true;
3631
3632 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003633
3634 if (callback) {
3635 assert(callback == input->cursor_frame_cb);
3636 wl_callback_destroy(callback);
3637 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003638 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003639 }
3640
Daniel Stone80972742012-11-07 17:51:39 +11003641 if (!input->pointer)
3642 return;
3643
Derek Foreman493d9792015-03-04 16:26:25 -06003644 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003645 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003646
Daniel Stonea494f1d2012-06-18 19:31:12 +01003647 cursor = input->display->cursors[input->current_cursor];
3648 if (!cursor)
3649 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003650
3651 /* FIXME We don't have the current time on the first call so we set
3652 * the animation start to the time of the first frame callback. */
3653 if (time == 0)
3654 input->cursor_anim_start = 0;
3655 else if (input->cursor_anim_start == 0)
3656 input->cursor_anim_start = time;
3657
Derek Foreman118a4292015-04-22 17:23:35 -05003658 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003659
Derek Foreman118a4292015-04-22 17:23:35 -05003660 if (time == 0 || input->cursor_anim_start == 0) {
3661 duration = 0;
3662 i = 0;
3663 } else
3664 i = wl_cursor_frame_and_duration(
3665 cursor,
3666 time - input->cursor_anim_start,
3667 &duration);
3668
3669 if (cursor->image_count > 1)
3670 schedule_pointer_image_update(input, cursor, duration,
3671 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003672
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003673 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003674}
3675
Derek Foreman118a4292015-04-22 17:23:35 -05003676static void
3677cursor_timer_func(struct task *task, uint32_t events)
3678{
3679 struct input *input = container_of(task, struct input, cursor_task);
3680 struct timespec tp;
3681 struct wl_cursor *cursor;
3682 uint32_t time;
3683 uint64_t exp;
3684
3685 if (!input->cursor_timer_running)
3686 return;
3687
3688 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3689 return;
3690
3691 cursor = input->display->cursors[input->current_cursor];
3692 if (!cursor)
3693 return;
3694
3695 clock_gettime(CLOCK_MONOTONIC, &tp);
3696 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3697 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3698}
3699
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003700static const struct wl_callback_listener pointer_surface_listener = {
3701 pointer_surface_frame_callback
3702};
3703
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003704void
3705input_set_pointer_image(struct input *input, int pointer)
3706{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003707 int force = 0;
3708
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003709 if (!input->pointer)
3710 return;
3711
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003712 if (input->pointer_enter_serial > input->cursor_serial)
3713 force = 1;
3714
3715 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003716 return;
3717
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003718 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003719 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003720 if (!input->cursor_frame_cb)
3721 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003722 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003723 /* The current frame callback may be stuck if, for instance,
3724 * the set cursor request was processed by the server after
3725 * this client lost the focus. In this case the cursor surface
3726 * might not be mapped and the frame callback wouldn't ever
3727 * complete. Send a set_cursor and attach to try to map the
3728 * cursor surface again so that the callback will finish */
3729 input_set_pointer_image_index(input, 0);
3730 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003731}
3732
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003733struct wl_data_device *
3734input_get_data_device(struct input *input)
3735{
3736 return input->data_device;
3737}
3738
3739void
3740input_set_selection(struct input *input,
3741 struct wl_data_source *source, uint32_t time)
3742{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003743 if (input->data_device)
3744 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003745}
3746
3747void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003748input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003749{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003750 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003751 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003752}
3753
3754static void
3755offer_io_func(struct task *task, uint32_t events)
3756{
3757 struct data_offer *offer =
3758 container_of(task, struct data_offer, io_task);
3759 unsigned int len;
3760 char buffer[4096];
3761
3762 len = read(offer->fd, buffer, sizeof buffer);
3763 offer->func(buffer, len,
3764 offer->x, offer->y, offer->user_data);
3765
3766 if (len == 0) {
3767 close(offer->fd);
3768 data_offer_destroy(offer);
3769 }
3770}
3771
3772static void
3773data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3774 data_func_t func, void *user_data)
3775{
3776 int p[2];
3777
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003778 if (pipe2(p, O_CLOEXEC) == -1)
3779 return;
3780
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003781 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3782 close(p[1]);
3783
3784 offer->io_task.run = offer_io_func;
3785 offer->fd = p[0];
3786 offer->func = func;
3787 offer->refcount++;
3788 offer->user_data = user_data;
3789
3790 display_watch_fd(offer->input->display,
3791 offer->fd, EPOLLIN, &offer->io_task);
3792}
3793
3794void
3795input_receive_drag_data(struct input *input, const char *mime_type,
3796 data_func_t func, void *data)
3797{
3798 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003799 input->drag_offer->x = input->drag_x;
3800 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003801}
3802
3803int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003804input_receive_drag_data_to_fd(struct input *input,
3805 const char *mime_type, int fd)
3806{
3807 if (input->drag_offer)
3808 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3809
3810 return 0;
3811}
3812
3813int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003814input_receive_selection_data(struct input *input, const char *mime_type,
3815 data_func_t func, void *data)
3816{
3817 char **p;
3818
3819 if (input->selection_offer == NULL)
3820 return -1;
3821
3822 for (p = input->selection_offer->types.data; *p; p++)
3823 if (strcmp(mime_type, *p) == 0)
3824 break;
3825
3826 if (*p == NULL)
3827 return -1;
3828
3829 data_offer_receive_data(input->selection_offer,
3830 mime_type, func, data);
3831 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003832}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003833
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003834int
3835input_receive_selection_data_to_fd(struct input *input,
3836 const char *mime_type, int fd)
3837{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003838 if (input->selection_offer)
3839 wl_data_offer_receive(input->selection_offer->offer,
3840 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003841
3842 return 0;
3843}
3844
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003845void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003846window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003847{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003848 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003849 return;
3850
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003851 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003852}
3853
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003854static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003855surface_set_synchronized(struct surface *surface)
3856{
3857 if (!surface->subsurface)
3858 return;
3859
3860 if (surface->synchronized)
3861 return;
3862
3863 wl_subsurface_set_sync(surface->subsurface);
3864 surface->synchronized = 1;
3865}
3866
3867static void
3868surface_set_synchronized_default(struct surface *surface)
3869{
3870 if (!surface->subsurface)
3871 return;
3872
3873 if (surface->synchronized == surface->synchronized_default)
3874 return;
3875
3876 if (surface->synchronized_default)
3877 wl_subsurface_set_sync(surface->subsurface);
3878 else
3879 wl_subsurface_set_desync(surface->subsurface);
3880
3881 surface->synchronized = surface->synchronized_default;
3882}
3883
3884static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003885surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003886{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003887 struct widget *widget = surface->widget;
3888 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003889
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003890 if (surface->input_region) {
3891 wl_region_destroy(surface->input_region);
3892 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003893 }
3894
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003895 if (surface->opaque_region)
3896 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003897
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003898 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003899
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003900 if (widget->resize_handler)
3901 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003902 widget->allocation.width,
3903 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003904 widget->user_data);
3905
Pekka Paalanen35e82632013-04-25 13:57:48 +03003906 if (surface->subsurface &&
3907 (surface->allocation.x != widget->allocation.x ||
3908 surface->allocation.y != widget->allocation.y)) {
3909 wl_subsurface_set_position(surface->subsurface,
3910 widget->allocation.x,
3911 widget->allocation.y);
3912 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003913 if (surface->allocation.width != widget->allocation.width ||
3914 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003915 window_schedule_redraw(widget->window);
3916 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003917 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003918
3919 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003920 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003921 widget->allocation.width,
3922 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003923}
3924
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003925static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003926window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003927{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003928 struct surface *surface;
3929
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003930 widget_set_allocation(window->main_surface->widget,
3931 window->pending_allocation.x,
3932 window->pending_allocation.y,
3933 window->pending_allocation.width,
3934 window->pending_allocation.height);
3935
3936 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003937
3938 /* The main surface is in the list, too. Main surface's
3939 * resize_handler is responsible for calling widget_set_allocation()
3940 * on all sub-surface root widgets, so they will be resized
3941 * properly.
3942 */
3943 wl_list_for_each(surface, &window->subsurface_list, link) {
3944 if (surface == window->main_surface)
3945 continue;
3946
3947 surface_set_synchronized(surface);
3948 surface_resize(surface);
3949 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003950
3951 if (!window->fullscreen && !window->maximized)
3952 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003953}
3954
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003955static void
3956idle_resize(struct window *window)
3957{
3958 window->resize_needed = 0;
3959 window->redraw_needed = 1;
3960
3961 DBG("from %dx%d to %dx%d\n",
3962 window->main_surface->server_allocation.width,
3963 window->main_surface->server_allocation.height,
3964 window->pending_allocation.width,
3965 window->pending_allocation.height);
3966
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003967 window_do_resize(window);
3968}
3969
3970static void
3971undo_resize(struct window *window)
3972{
3973 window->pending_allocation.width =
3974 window->main_surface->server_allocation.width;
3975 window->pending_allocation.height =
3976 window->main_surface->server_allocation.height;
3977
3978 DBG("back to %dx%d\n",
3979 window->main_surface->server_allocation.width,
3980 window->main_surface->server_allocation.height);
3981
3982 window_do_resize(window);
3983
3984 if (window->pending_allocation.width == 0 &&
3985 window->pending_allocation.height == 0) {
3986 fprintf(stderr, "Error: Could not draw a surface, "
3987 "most likely due to insufficient disk space in "
3988 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3989 exit(EXIT_FAILURE);
3990 }
3991}
3992
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003993void
3994window_schedule_resize(struct window *window, int width, int height)
3995{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003996 /* We should probably get these numbers from the theme. */
3997 const int min_width = 200, min_height = 200;
3998
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003999 window->pending_allocation.x = 0;
4000 window->pending_allocation.y = 0;
4001 window->pending_allocation.width = width;
4002 window->pending_allocation.height = height;
4003
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004004 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004005 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004006 window->min_allocation.width = min_width;
4007 else
4008 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004009 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004010 window->min_allocation.height = min_height;
4011 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004012 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004013 }
4014
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004015 if (window->pending_allocation.width < window->min_allocation.width)
4016 window->pending_allocation.width = window->min_allocation.width;
4017 if (window->pending_allocation.height < window->min_allocation.height)
4018 window->pending_allocation.height = window->min_allocation.height;
4019
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004020 window->resize_needed = 1;
4021 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004022}
4023
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004024void
4025widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4026{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004027 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004028}
4029
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004030static int
4031window_get_shadow_margin(struct window *window)
4032{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004033 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004034 return frame_get_shadow_margin(window->frame->frame);
4035 else
4036 return 0;
4037}
4038
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004039static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004040handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004041 int32_t width, int32_t height,
4042 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004043{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004044 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004045 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004046
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004047 window->maximized = 0;
4048 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004049 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004050 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004051
4052 wl_array_for_each(p, states) {
4053 uint32_t state = *p;
4054 switch (state) {
4055 case XDG_SURFACE_STATE_MAXIMIZED:
4056 window->maximized = 1;
4057 break;
4058 case XDG_SURFACE_STATE_FULLSCREEN:
4059 window->fullscreen = 1;
4060 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004061 case XDG_SURFACE_STATE_RESIZING:
4062 window->resizing = 1;
4063 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004064 case XDG_SURFACE_STATE_ACTIVATED:
4065 window->focused = 1;
4066 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004067 default:
4068 /* Unknown state */
4069 break;
4070 }
4071 }
4072
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004073 if (window->frame) {
4074 if (window->maximized) {
4075 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4076 } else {
4077 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4078 }
4079
4080 if (window->focused) {
4081 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4082 } else {
4083 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4084 }
4085 }
4086
Jasper St. Pierref184c382014-05-06 08:33:27 -04004087 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004088 /* The width / height params are for window geometry,
4089 * but window_schedule_resize takes allocation. Add
4090 * on the shadow margin to get the difference. */
4091 int margin = window_get_shadow_margin(window);
4092
4093 window_schedule_resize(window,
4094 width + margin * 2,
4095 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004096 } else {
4097 window_schedule_resize(window,
4098 window->saved_allocation.width,
4099 window->saved_allocation.height);
4100 }
4101
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004102 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004103
4104 if (window->state_changed_handler)
4105 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004106}
4107
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004108static void
4109handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4110{
4111 struct window *window = data;
4112 window_close(window);
4113}
4114
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004115static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004116 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004117 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004118};
4119
4120static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004121window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004122{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004123 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004124
4125 if (!window->xdg_surface)
4126 return;
4127
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004128 if (window->parent == window->last_parent)
4129 return;
4130
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004131 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004132 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004133 else
4134 parent_surface = NULL;
4135
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004136 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004137 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004138}
4139
4140static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004141window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004142{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004143 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004144 frame_input_rect(window->frame->frame,
4145 &geometry->x,
4146 &geometry->y,
4147 &geometry->width,
4148 &geometry->height);
4149 else
4150 window_get_allocation(window, geometry);
4151}
4152
4153static void
4154window_sync_geometry(struct window *window)
4155{
4156 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004157
4158 if (!window->xdg_surface)
4159 return;
4160
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004161 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004162 if (geometry.x == window->last_geometry.x &&
4163 geometry.y == window->last_geometry.y &&
4164 geometry.width == window->last_geometry.width &&
4165 geometry.height == window->last_geometry.height)
4166 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004167
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004168 xdg_surface_set_window_geometry(window->xdg_surface,
4169 geometry.x,
4170 geometry.y,
4171 geometry.width,
4172 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004173 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004174}
4175
4176static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004177window_flush(struct window *window)
4178{
4179 struct surface *surface;
4180
4181 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004182 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004183 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004184 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004185 }
4186 }
4187
4188 wl_list_for_each(surface, &window->subsurface_list, link) {
4189 if (surface == window->main_surface)
4190 continue;
4191
4192 surface_flush(surface);
4193 }
4194
4195 surface_flush(window->main_surface);
4196}
4197
4198static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004199menu_destroy(struct menu *menu)
4200{
4201 widget_destroy(menu->widget);
4202 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004203 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004204 free(menu);
4205}
4206
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004207void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004208window_get_allocation(struct window *window,
4209 struct rectangle *allocation)
4210{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004211 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004212}
4213
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004214static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004215widget_redraw(struct widget *widget)
4216{
4217 struct widget *child;
4218
4219 if (widget->redraw_handler)
4220 widget->redraw_handler(widget, widget->user_data);
4221 wl_list_for_each(child, &widget->child_list, link)
4222 widget_redraw(child);
4223}
4224
4225static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004226frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4227{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004228 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004229
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004230 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004231 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004232 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004233 surface->frame_cb = NULL;
4234
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004235 surface->last_time = time;
4236
Pekka Paalanen71233882013-04-25 13:57:53 +03004237 if (surface->redraw_needed || surface->window->redraw_needed) {
4238 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004239 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004240 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004241}
4242
4243static const struct wl_callback_listener listener = {
4244 frame_callback
4245};
4246
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004247static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004248surface_redraw(struct surface *surface)
4249{
Pekka Paalanen71233882013-04-25 13:57:53 +03004250 DBG_OBJ(surface->surface, "begin\n");
4251
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004252 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004253 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004254
4255 /* Whole-window redraw forces a redraw even if the previous has
4256 * not yet hit the screen.
4257 */
4258 if (surface->frame_cb) {
4259 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004260 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004261
Pekka Paalanen71233882013-04-25 13:57:53 +03004262 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004263 wl_callback_destroy(surface->frame_cb);
4264 }
4265
Neil Roberts97b747c2013-12-19 16:17:12 +00004266 if (surface->widget->use_cairo &&
4267 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004268 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4269 return -1;
4270 }
4271
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004272 surface->frame_cb = wl_surface_frame(surface->surface);
4273 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004274 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004275
4276 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004277 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004278 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004279 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004280 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004281}
4282
4283static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004284idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004285{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004286 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004287 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004288 int failed = 0;
4289 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004290
Pekka Paalanen71233882013-04-25 13:57:53 +03004291 DBG(" --------- \n");
4292
Pekka Paalaneneebff542013-04-25 13:57:52 +03004293 wl_list_init(&window->redraw_task.link);
4294 window->redraw_task_scheduled = 0;
4295
4296 if (window->resize_needed) {
4297 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004298 if (window->main_surface->frame_cb) {
4299 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004300 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004301 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004302
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004303 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004304 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004305 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004306
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004307 if (surface_redraw(window->main_surface) < 0) {
4308 /*
4309 * Only main_surface failure will cause us to undo the resize.
4310 * If sub-surfaces fail, they will just be broken with old
4311 * content.
4312 */
4313 failed = 1;
4314 } else {
4315 wl_list_for_each(surface, &window->subsurface_list, link) {
4316 if (surface == window->main_surface)
4317 continue;
4318
4319 surface_redraw(surface);
4320 }
4321 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004322
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004323 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004324 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004325
4326 wl_list_for_each(surface, &window->subsurface_list, link)
4327 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004328
4329 if (resized && failed) {
4330 /* Restore widget tree to correspond to what is on screen. */
4331 undo_resize(window);
4332 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004333}
4334
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004335static void
4336window_schedule_redraw_task(struct window *window)
4337{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004338 if (!window->redraw_task_scheduled) {
4339 window->redraw_task.run = idle_redraw;
4340 display_defer(window->display, &window->redraw_task);
4341 window->redraw_task_scheduled = 1;
4342 }
4343}
4344
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004345void
4346window_schedule_redraw(struct window *window)
4347{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004348 struct surface *surface;
4349
Pekka Paalanen71233882013-04-25 13:57:53 +03004350 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4351
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004352 wl_list_for_each(surface, &window->subsurface_list, link)
4353 surface->redraw_needed = 1;
4354
4355 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004356}
4357
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004358int
4359window_is_fullscreen(struct window *window)
4360{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004361 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004362}
4363
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004364void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004365window_set_fullscreen(struct window *window, int fullscreen)
4366{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004367 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004368 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004369
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004370 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004371 return;
4372
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004373 if (fullscreen)
4374 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4375 else
4376 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004377}
4378
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004379int
4380window_is_maximized(struct window *window)
4381{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004382 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004383}
4384
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004385void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004386window_set_maximized(struct window *window, int maximized)
4387{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004388 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004389 return;
4390
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004391 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004392 return;
4393
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004394 if (maximized)
4395 xdg_surface_set_maximized(window->xdg_surface);
4396 else
4397 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004398}
4399
Jasper St. Pierrede680992014-04-10 17:23:49 -07004400int
4401window_is_resizing(struct window *window)
4402{
4403 return window->resizing;
4404}
4405
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004406void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004407window_set_minimized(struct window *window)
4408{
4409 if (!window->xdg_surface)
4410 return;
4411
4412 xdg_surface_set_minimized(window->xdg_surface);
4413}
4414
4415void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004416window_set_user_data(struct window *window, void *data)
4417{
4418 window->user_data = data;
4419}
4420
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004421void *
4422window_get_user_data(struct window *window)
4423{
4424 return window->user_data;
4425}
4426
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004427void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004428window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004429 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004430{
4431 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004432}
4433
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004434void
4435window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004436 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004437{
4438 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004439}
4440
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004441void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004442window_set_data_handler(struct window *window, window_data_handler_t handler)
4443{
4444 window->data_handler = handler;
4445}
4446
4447void
4448window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4449{
4450 window->drop_handler = handler;
4451}
4452
4453void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004454window_set_close_handler(struct window *window,
4455 window_close_handler_t handler)
4456{
4457 window->close_handler = handler;
4458}
4459
4460void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004461window_set_fullscreen_handler(struct window *window,
4462 window_fullscreen_handler_t handler)
4463{
4464 window->fullscreen_handler = handler;
4465}
4466
4467void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004468window_set_output_handler(struct window *window,
4469 window_output_handler_t handler)
4470{
4471 window->output_handler = handler;
4472}
4473
4474void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004475window_set_state_changed_handler(struct window *window,
4476 window_state_changed_handler_t handler)
4477{
4478 window->state_changed_handler = handler;
4479}
4480
4481void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004482window_set_title(struct window *window, const char *title)
4483{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004484 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004485 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004486 if (window->frame) {
4487 frame_set_title(window->frame->frame, title);
4488 widget_schedule_redraw(window->frame->widget);
4489 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004490 if (window->xdg_surface)
4491 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004492}
4493
4494const char *
4495window_get_title(struct window *window)
4496{
4497 return window->title;
4498}
4499
4500void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004501window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4502{
4503 struct text_cursor_position *text_cursor_position =
4504 window->display->text_cursor_position;
4505
Scott Moreau9295ce02012-06-01 12:46:10 -06004506 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004507 return;
4508
4509 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004510 window->main_surface->surface,
4511 wl_fixed_from_int(x),
4512 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004513}
4514
4515void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004516window_damage(struct window *window, int32_t x, int32_t y,
4517 int32_t width, int32_t height)
4518{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004519 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004520}
4521
Casey Dahlin9074db52012-04-19 22:50:09 -04004522static void
4523surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004524 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004525{
Rob Bradford7507b572012-05-15 17:55:34 +01004526 struct window *window = data;
4527 struct output *output;
4528 struct output *output_found = NULL;
4529 struct window_output *window_output;
4530
4531 wl_list_for_each(output, &window->display->output_list, link) {
4532 if (output->output == wl_output) {
4533 output_found = output;
4534 break;
4535 }
4536 }
4537
4538 if (!output_found)
4539 return;
4540
Brian Lovinbc919262013-08-07 15:34:59 -07004541 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004542 window_output->output = output_found;
4543
4544 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004545
4546 if (window->output_handler)
4547 window->output_handler(window, output_found, 1,
4548 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004549}
4550
4551static void
4552surface_leave(void *data,
4553 struct wl_surface *wl_surface, struct wl_output *output)
4554{
Rob Bradford7507b572012-05-15 17:55:34 +01004555 struct window *window = data;
4556 struct window_output *window_output;
4557 struct window_output *window_output_found = NULL;
4558
4559 wl_list_for_each(window_output, &window->window_output_list, link) {
4560 if (window_output->output->output == output) {
4561 window_output_found = window_output;
4562 break;
4563 }
4564 }
4565
4566 if (window_output_found) {
4567 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004568
4569 if (window->output_handler)
4570 window->output_handler(window, window_output->output,
4571 0, window->user_data);
4572
Rob Bradford7507b572012-05-15 17:55:34 +01004573 free(window_output_found);
4574 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004575}
4576
4577static const struct wl_surface_listener surface_listener = {
4578 surface_enter,
4579 surface_leave
4580};
4581
Pekka Paalanen4e373742013-02-13 16:17:13 +02004582static struct surface *
4583surface_create(struct window *window)
4584{
4585 struct display *display = window->display;
4586 struct surface *surface;
4587
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004588 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004589 surface->window = window;
4590 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004591 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004592 wl_surface_add_listener(surface->surface, &surface_listener, window);
4593
Pekka Paalanen35e82632013-04-25 13:57:48 +03004594 wl_list_insert(&window->subsurface_list, &surface->link);
4595
Pekka Paalanen4e373742013-02-13 16:17:13 +02004596 return surface;
4597}
4598
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004599static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004600get_preferred_buffer_type(struct display *display)
4601{
4602#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004603 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004604 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4605#endif
4606
4607 return WINDOW_BUFFER_TYPE_SHM;
4608}
4609
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004610static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004611window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004612{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004613 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004614 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004615
Peter Huttererf3d62272013-08-08 11:57:05 +10004616 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004617 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004618 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004619
4620 surface = surface_create(window);
4621 window->main_surface = surface;
4622
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004623 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004624
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004625 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004626 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004627
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004628 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004629
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004630 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004631 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004632 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004633
Rob Bradford7507b572012-05-15 17:55:34 +01004634 wl_list_init (&window->window_output_list);
4635
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004636 return window;
4637}
4638
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004639struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004640window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004641{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004642 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004643 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004644
4645 window = window_create_internal(display, 0);
4646
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004647 if (window->display->xdg_shell) {
4648 window->xdg_surface =
4649 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4650 window->main_surface->surface);
4651 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004652
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004653 xdg_surface_set_user_data(window->xdg_surface, window);
4654 xdg_surface_add_listener(window->xdg_surface,
4655 &xdg_surface_listener, window);
4656 } else if (display->ivi_application) {
4657 /* auto generation of ivi_id based on process id + basement of id */
4658 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4659 window->ivi_surface =
4660 ivi_application_surface_create(display->ivi_application,
4661 id_ivisurf, window->main_surface->surface);
4662 fail_on_null(window->ivi_surface);
4663
4664 ivi_surface_add_listener(window->ivi_surface,
4665 &ivi_surface_listener, window);
4666 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004667
4668 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004669}
4670
4671struct window *
4672window_create_custom(struct display *display)
4673{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004674 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004675}
4676
Jasper St. Pierre53686042013-12-09 15:26:25 -05004677void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004678window_set_parent(struct window *window,
4679 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004680{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004681 window->parent = parent_window;
4682 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004683}
4684
4685struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004686window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004687{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004688 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004689}
4690
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004691static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004692menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004693{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004694 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004695 int next;
4696
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004697 frame_interior(menu->frame, &x, &y, &width, &height);
4698 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004699 if (menu->current != next) {
4700 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004701 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004702 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004703}
4704
4705static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004706menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004707 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004708 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004709{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004710 struct menu *menu = data;
4711
4712 if (widget == menu->widget)
4713 menu_set_item(data, y);
4714
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004715 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004716}
4717
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004718static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004719menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004720 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004721{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004722 struct menu *menu = data;
4723
4724 if (widget == menu->widget)
4725 menu_set_item(data, y);
4726
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004727 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004728}
4729
4730static void
4731menu_leave_handler(struct widget *widget, struct input *input, void *data)
4732{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004733 struct menu *menu = data;
4734
4735 if (widget == menu->widget)
4736 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004737}
4738
4739static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004740menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004741 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004742 uint32_t button, enum wl_pointer_button_state state,
4743 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004744
4745{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004746 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004747
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004748 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4749 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004750 /* Either relase after press-drag-release or
4751 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004752 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05004753 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004754 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004755 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004756 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004757 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004758}
4759
4760static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004761menu_touch_up_handler(struct widget *widget,
4762 struct input *input,
4763 uint32_t serial,
4764 uint32_t time,
4765 int32_t id,
4766 void *data)
4767{
4768 struct menu *menu = data;
4769
4770 input_ungrab(input);
4771 menu_destroy(menu);
4772}
4773
4774static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004775menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004776{
4777 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004778 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004779 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004780
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004781 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004782
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004783 frame_repaint(menu->frame, cr);
4784 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004785
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004786 theme_set_background_source(menu->window->display->theme,
4787 cr, THEME_FRAME_ACTIVE);
4788 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004789 cairo_fill(cr);
4790
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004791 cairo_select_font_face(cr, "sans",
4792 CAIRO_FONT_SLANT_NORMAL,
4793 CAIRO_FONT_WEIGHT_NORMAL);
4794 cairo_set_font_size(cr, 12);
4795
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004796 for (i = 0; i < menu->count; i++) {
4797 if (i == menu->current) {
4798 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004799 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004800 cairo_fill(cr);
4801 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004802 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004803 cairo_show_text(cr, menu->entries[i]);
4804 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004805 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4806 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004807 cairo_show_text(cr, menu->entries[i]);
4808 }
4809 }
4810
4811 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004812}
4813
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004814static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004815handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004816{
4817 struct window *window = data;
4818 struct menu *menu = window->main_surface->widget->user_data;
4819
4820 input_ungrab(menu->input);
4821 menu_destroy(menu);
4822}
4823
4824static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004825 handle_popup_popup_done,
4826};
4827
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004828static struct menu *
4829create_menu(struct display *display,
4830 struct input *input, uint32_t time,
4831 menu_func_t func, const char **entries, int count,
4832 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004833{
4834 struct window *window;
4835 struct menu *menu;
4836
4837 menu = malloc(sizeof *menu);
4838 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004839 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004840
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004841 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004842 if (!window) {
4843 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004844 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004845 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004846
4847 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004848 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004849 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004850 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004851 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004852 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004853 menu->entries = entries;
4854 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004855 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004856 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004857 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004858 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004859 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004860
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004861 input_ungrab(input);
4862
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004863 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004864 widget_set_enter_handler(menu->widget, menu_enter_handler);
4865 widget_set_leave_handler(menu->widget, menu_leave_handler);
4866 widget_set_motion_handler(menu->widget, menu_motion_handler);
4867 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004868 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004869
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004870 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004871 frame_resize_inside(menu->frame, 200, count * 20);
4872 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4873 window_schedule_resize(window, frame_width(menu->frame),
4874 frame_height(menu->frame));
4875
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004876 return menu;
4877}
4878
4879struct window *
4880window_create_menu(struct display *display,
4881 struct input *input, uint32_t time,
4882 menu_func_t func, const char **entries, int count,
4883 void *user_data)
4884{
4885 struct menu *menu;
4886 menu = create_menu(display, input, time, func, entries, count, user_data);
4887
4888 if (menu == NULL)
4889 return NULL;
4890
4891 return menu->window;
4892}
4893
4894void
4895window_show_menu(struct display *display,
4896 struct input *input, uint32_t time, struct window *parent,
4897 int32_t x, int32_t y,
4898 menu_func_t func, const char **entries, int count)
4899{
4900 struct menu *menu;
4901 struct window *window;
4902 int32_t ix, iy;
4903
4904 menu = create_menu(display, input, time, func, entries, count, parent);
4905
4906 if (menu == NULL)
4907 return;
4908
4909 window = menu->window;
4910
4911 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4912 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4913
4914 window->x = x;
4915 window->y = y;
4916
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004917 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004918
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004919 if (!display->xdg_shell)
4920 return;
4921
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004922 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4923 window->main_surface->surface,
4924 parent->main_surface->surface,
4925 input->seat,
4926 display_get_serial(window->display),
4927 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004928 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004929 fail_on_null(window->xdg_popup);
4930
4931 xdg_popup_set_user_data(window->xdg_popup, window);
4932 xdg_popup_add_listener(window->xdg_popup,
4933 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004934}
4935
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004936void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004937window_set_buffer_type(struct window *window, enum window_buffer_type type)
4938{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004939 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004940}
4941
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004942enum window_buffer_type
4943window_get_buffer_type(struct window *window)
4944{
4945 return window->main_surface->buffer_type;
4946}
4947
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004948void
4949window_set_preferred_format(struct window *window,
4950 enum preferred_format format)
4951{
4952 window->preferred_format = format;
4953}
4954
Pekka Paalanen35e82632013-04-25 13:57:48 +03004955struct widget *
4956window_add_subsurface(struct window *window, void *data,
4957 enum subsurface_mode default_mode)
4958{
4959 struct widget *widget;
4960 struct surface *surface;
4961 struct wl_surface *parent;
4962 struct wl_subcompositor *subcompo = window->display->subcompositor;
4963
Pekka Paalanen35e82632013-04-25 13:57:48 +03004964 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004965 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004966 widget = widget_create(window, surface, data);
4967 wl_list_init(&widget->link);
4968 surface->widget = widget;
4969
4970 parent = window->main_surface->surface;
4971 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4972 surface->surface,
4973 parent);
4974 surface->synchronized = 1;
4975
4976 switch (default_mode) {
4977 case SUBSURFACE_SYNCHRONIZED:
4978 surface->synchronized_default = 1;
4979 break;
4980 case SUBSURFACE_DESYNCHRONIZED:
4981 surface->synchronized_default = 0;
4982 break;
4983 default:
4984 assert(!"bad enum subsurface_mode");
4985 }
4986
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004987 window->resize_needed = 1;
4988 window_schedule_redraw(window);
4989
Pekka Paalanen35e82632013-04-25 13:57:48 +03004990 return widget;
4991}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004992
4993static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004994display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004995 struct wl_output *wl_output,
4996 int x, int y,
4997 int physical_width,
4998 int physical_height,
4999 int subpixel,
5000 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005001 const char *model,
5002 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005003{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005004 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005005
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005006 output->allocation.x = x;
5007 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005008 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005009
5010 if (output->make)
5011 free(output->make);
5012 output->make = strdup(make);
5013
5014 if (output->model)
5015 free(output->model);
5016 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005017}
5018
5019static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005020display_handle_done(void *data,
5021 struct wl_output *wl_output)
5022{
5023}
5024
5025static void
5026display_handle_scale(void *data,
5027 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005028 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005029{
5030 struct output *output = data;
5031
5032 output->scale = scale;
5033}
5034
5035static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005036display_handle_mode(void *data,
5037 struct wl_output *wl_output,
5038 uint32_t flags,
5039 int width,
5040 int height,
5041 int refresh)
5042{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005043 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005044 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005045
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005046 if (flags & WL_OUTPUT_MODE_CURRENT) {
5047 output->allocation.width = width;
5048 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005049 if (display->output_configure_handler)
5050 (*display->output_configure_handler)(
5051 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005052 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005053}
5054
5055static const struct wl_output_listener output_listener = {
5056 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005057 display_handle_mode,
5058 display_handle_done,
5059 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005060};
5061
5062static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005063display_add_output(struct display *d, uint32_t id)
5064{
5065 struct output *output;
5066
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005067 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005068 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005069 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005070 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005071 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005072 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005073 wl_list_insert(d->output_list.prev, &output->link);
5074
5075 wl_output_add_listener(output->output, &output_listener, output);
5076}
5077
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005078static void
5079output_destroy(struct output *output)
5080{
5081 if (output->destroy_handler)
5082 (*output->destroy_handler)(output, output->user_data);
5083
5084 wl_output_destroy(output->output);
5085 wl_list_remove(&output->link);
5086 free(output);
5087}
5088
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005089static void
5090display_destroy_output(struct display *d, uint32_t id)
5091{
5092 struct output *output;
5093
5094 wl_list_for_each(output, &d->output_list, link) {
5095 if (output->server_output_id == id) {
5096 output_destroy(output);
5097 break;
5098 }
5099 }
5100}
5101
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005102void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005103display_set_global_handler(struct display *display,
5104 display_global_handler_t handler)
5105{
5106 struct global *global;
5107
5108 display->global_handler = handler;
5109 if (!handler)
5110 return;
5111
5112 wl_list_for_each(global, &display->global_list, link)
5113 display->global_handler(display,
5114 global->name, global->interface,
5115 global->version, display->user_data);
5116}
5117
5118void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005119display_set_global_handler_remove(struct display *display,
5120 display_global_handler_t remove_handler)
5121{
5122 display->global_handler_remove = remove_handler;
5123 if (!remove_handler)
5124 return;
5125}
5126
5127void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005128display_set_output_configure_handler(struct display *display,
5129 display_output_handler_t handler)
5130{
5131 struct output *output;
5132
5133 display->output_configure_handler = handler;
5134 if (!handler)
5135 return;
5136
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005137 wl_list_for_each(output, &display->output_list, link) {
5138 if (output->allocation.width == 0 &&
5139 output->allocation.height == 0)
5140 continue;
5141
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005142 (*display->output_configure_handler)(output,
5143 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005144 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005145}
5146
5147void
5148output_set_user_data(struct output *output, void *data)
5149{
5150 output->user_data = data;
5151}
5152
5153void *
5154output_get_user_data(struct output *output)
5155{
5156 return output->user_data;
5157}
5158
5159void
5160output_set_destroy_handler(struct output *output,
5161 display_output_handler_t handler)
5162{
5163 output->destroy_handler = handler;
5164 /* FIXME: implement this, once we have way to remove outputs */
5165}
5166
5167void
Scott Moreau4e072362012-09-29 02:03:11 -06005168output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005169{
Scott Moreau4e072362012-09-29 02:03:11 -06005170 struct rectangle allocation = output->allocation;
5171
5172 switch (output->transform) {
5173 case WL_OUTPUT_TRANSFORM_90:
5174 case WL_OUTPUT_TRANSFORM_270:
5175 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5176 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5177 /* Swap width and height */
5178 allocation.width = output->allocation.height;
5179 allocation.height = output->allocation.width;
5180 break;
5181 }
5182
5183 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005184}
5185
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005186struct wl_output *
5187output_get_wl_output(struct output *output)
5188{
5189 return output->output;
5190}
5191
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005192enum wl_output_transform
5193output_get_transform(struct output *output)
5194{
5195 return output->transform;
5196}
5197
Alexander Larssonafd319a2013-05-22 14:41:27 +02005198uint32_t
5199output_get_scale(struct output *output)
5200{
5201 return output->scale;
5202}
5203
Jason Ekstrand738715d2014-04-02 19:53:50 -05005204const char *
5205output_get_make(struct output *output)
5206{
5207 return output->make;
5208}
5209
5210const char *
5211output_get_model(struct output *output)
5212{
5213 return output->model;
5214}
5215
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005216static void
Daniel Stone97f68542012-05-30 16:32:01 +01005217fini_xkb(struct input *input)
5218{
5219 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005220 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005221}
5222
5223static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005224display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005225{
5226 struct input *input;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005227 int seat_version = MIN(display_seat_version, 4);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005228
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005229 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005230 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005231 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005232 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005233 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005234 input->pointer_focus = NULL;
5235 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005236 input->seat_version = seat_version;
5237
Rusty Lynch041815a2013-08-08 21:20:38 -07005238 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005239 wl_list_insert(d->input_list.prev, &input->link);
5240
Daniel Stone37816df2012-05-16 18:45:18 +01005241 wl_seat_add_listener(input->seat, &seat_listener, input);
5242 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005243
Jason Ekstranda669bd52014-04-02 19:53:51 -05005244 if (d->data_device_manager) {
5245 input->data_device =
5246 wl_data_device_manager_get_data_device(d->data_device_manager,
5247 input->seat);
5248 wl_data_device_add_listener(input->data_device,
5249 &data_device_listener,
5250 input);
5251 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005252
5253 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005254 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005255
Derek Foreman118a4292015-04-22 17:23:35 -05005256 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5257 TFD_CLOEXEC | TFD_NONBLOCK);
5258 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5259 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005260 set_repeat_info(input, 40, 400);
5261
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005262 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5263 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005264 input->repeat_task.run = keyboard_repeat_func;
5265 display_watch_fd(d, input->repeat_timer_fd,
5266 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005267}
5268
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005269static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005270input_destroy(struct input *input)
5271{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005272 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005273 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005274
5275 if (input->drag_offer)
5276 data_offer_destroy(input->drag_offer);
5277
5278 if (input->selection_offer)
5279 data_offer_destroy(input->selection_offer);
5280
kabeer khan6ce67ec2014-10-20 11:55:29 +05305281 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005282 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305283 wl_data_device_release(input->data_device);
5284 else
5285 wl_data_device_destroy(input->data_device);
5286 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005287 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005288 if (input->touch)
5289 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005290 if (input->pointer)
5291 wl_pointer_release(input->pointer);
5292 if (input->keyboard)
5293 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005294 } else {
5295 if (input->touch)
5296 wl_touch_destroy(input->touch);
5297 if (input->pointer)
5298 wl_pointer_destroy(input->pointer);
5299 if (input->keyboard)
5300 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005301 }
5302
Daniel Stone97f68542012-05-30 16:32:01 +01005303 fini_xkb(input);
5304
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005305 wl_surface_destroy(input->pointer_surface);
5306
Pekka Paalanene1207c72011-12-16 12:02:09 +02005307 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005308 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005309 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005310 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005311 free(input);
5312}
5313
5314static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005315init_workspace_manager(struct display *d, uint32_t id)
5316{
5317 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005318 wl_registry_bind(d->registry, id,
5319 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005320 if (d->workspace_manager != NULL)
5321 workspace_manager_add_listener(d->workspace_manager,
5322 &workspace_manager_listener,
5323 d);
5324}
5325
5326static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005327shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5328{
5329 struct display *d = data;
5330
5331 if (format == WL_SHM_FORMAT_RGB565)
5332 d->has_rgb565 = 1;
5333}
5334
5335struct wl_shm_listener shm_listener = {
5336 shm_format
5337};
5338
5339static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005340xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5341{
5342 xdg_shell_pong(shell, serial);
5343}
5344
5345static const struct xdg_shell_listener xdg_shell_listener = {
5346 xdg_shell_ping,
5347};
5348
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005349#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005350#ifdef static_assert
5351static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5352 "Interface version doesn't match implementation version");
5353#endif
5354
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005355static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005356registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5357 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005358{
5359 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005360 struct global *global;
5361
Brian Lovinbc919262013-08-07 15:34:59 -07005362 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005363 global->name = id;
5364 global->interface = strdup(interface);
5365 global->version = version;
5366 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005367
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005368 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005369 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005370 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005371 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005372 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005373 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005374 display_add_input(d, id, version);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005375 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005376 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005377 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005378 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305379 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005380 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005381 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305382 &wl_data_device_manager_interface,
5383 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005384 } else if (strcmp(interface, "xdg_shell") == 0) {
5385 d->xdg_shell = wl_registry_bind(registry, id,
5386 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005387 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005388 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005389 } else if (strcmp(interface, "text_cursor_position") == 0) {
5390 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005391 wl_registry_bind(registry, id,
5392 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005393 } else if (strcmp(interface, "workspace_manager") == 0) {
5394 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005395 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5396 d->subcompositor =
5397 wl_registry_bind(registry, id,
5398 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005399 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005400 else if (strcmp(interface, "ivi_application") == 0) {
5401 d->ivi_application =
5402 wl_registry_bind(registry, id,
5403 &ivi_application_interface, 1);
5404 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005405
5406 if (d->global_handler)
5407 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005408}
5409
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005410static void
5411registry_handle_global_remove(void *data, struct wl_registry *registry,
5412 uint32_t name)
5413{
5414 struct display *d = data;
5415 struct global *global;
5416 struct global *tmp;
5417
5418 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5419 if (global->name != name)
5420 continue;
5421
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005422 if (strcmp(global->interface, "wl_output") == 0)
5423 display_destroy_output(d, name);
5424
5425 /* XXX: Should destroy remaining bound globals */
5426
5427 if (d->global_handler_remove)
5428 d->global_handler_remove(d, name, global->interface,
5429 global->version, d->user_data);
5430
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005431 wl_list_remove(&global->link);
5432 free(global->interface);
5433 free(global);
5434 }
5435}
5436
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005437void *
5438display_bind(struct display *display, uint32_t name,
5439 const struct wl_interface *interface, uint32_t version)
5440{
5441 return wl_registry_bind(display->registry, name, interface, version);
5442}
5443
5444static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005445 registry_handle_global,
5446 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005447};
5448
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005449#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005450static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005451init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005452{
5453 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005454 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005455
Rob Clark6396ed32012-03-11 19:48:41 -05005456#ifdef USE_CAIRO_GLESV2
5457# define GL_BIT EGL_OPENGL_ES2_BIT
5458#else
5459# define GL_BIT EGL_OPENGL_BIT
5460#endif
5461
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005462 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005463 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005464 EGL_RED_SIZE, 1,
5465 EGL_GREEN_SIZE, 1,
5466 EGL_BLUE_SIZE, 1,
5467 EGL_ALPHA_SIZE, 1,
5468 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005469 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005470 EGL_NONE
5471 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005472
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005473#ifdef USE_CAIRO_GLESV2
5474 static const EGLint context_attribs[] = {
5475 EGL_CONTEXT_CLIENT_VERSION, 2,
5476 EGL_NONE
5477 };
5478 EGLint api = EGL_OPENGL_ES_API;
5479#else
5480 EGLint *context_attribs = NULL;
5481 EGLint api = EGL_OPENGL_API;
5482#endif
5483
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005484 d->dpy =
5485 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5486 d->display, NULL);
5487
Yuval Fledel45568f62010-12-06 09:18:12 -05005488 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005489 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005490 return -1;
5491 }
5492
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005493 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005494 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005495 return -1;
5496 }
5497
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005498 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5499 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005500 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005501 return -1;
5502 }
5503
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005504 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005505 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005506 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005507 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005508 return -1;
5509 }
5510
Benjamin Franzke0c991632011-09-27 21:57:31 +02005511 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5512 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005513 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005514 return -1;
5515 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005516
5517 return 0;
5518}
5519
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005520static void
5521fini_egl(struct display *display)
5522{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005523 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005524
5525 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5526 EGL_NO_CONTEXT);
5527
5528 eglTerminate(display->dpy);
5529 eglReleaseThread();
5530}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005531#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005532
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005533static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005534init_dummy_surface(struct display *display)
5535{
5536 int len;
5537 void *data;
5538
5539 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005540 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005541 display->dummy_surface =
5542 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5543 1, 1, len);
5544 display->dummy_surface_data = data;
5545}
5546
5547static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005548handle_display_data(struct task *task, uint32_t events)
5549{
5550 struct display *display =
5551 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005552 struct epoll_event ep;
5553 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005554
5555 display->display_fd_events = events;
5556
5557 if (events & EPOLLERR || events & EPOLLHUP) {
5558 display_exit(display);
5559 return;
5560 }
5561
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005562 if (events & EPOLLIN) {
5563 ret = wl_display_dispatch(display->display);
5564 if (ret == -1) {
5565 display_exit(display);
5566 return;
5567 }
5568 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005569
5570 if (events & EPOLLOUT) {
5571 ret = wl_display_flush(display->display);
5572 if (ret == 0) {
5573 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5574 ep.data.ptr = &display->display_task;
5575 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5576 display->display_fd, &ep);
5577 } else if (ret == -1 && errno != EAGAIN) {
5578 display_exit(display);
5579 return;
5580 }
5581 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005582}
5583
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005584static void
5585log_handler(const char *format, va_list args)
5586{
5587 vfprintf(stderr, format, args);
5588}
5589
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005590struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005591display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005592{
5593 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005594
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005595 wl_log_set_handler_client(log_handler);
5596
Peter Huttererf3d62272013-08-08 11:57:05 +10005597 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005598 if (d == NULL)
5599 return NULL;
5600
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005601 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005602 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005603 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005604 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005605 return NULL;
5606 }
5607
Rob Bradford5ab9c752013-07-26 16:29:43 +01005608 d->xkb_context = xkb_context_new(0);
5609 if (d->xkb_context == NULL) {
5610 fprintf(stderr, "Failed to create XKB context\n");
5611 free(d);
5612 return NULL;
5613 }
5614
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005615 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005616 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005617 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005618 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5619 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005620
5621 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005622 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005623 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005624 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005625
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005626 d->workspace = 0;
5627 d->workspace_count = 1;
5628
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005629 d->registry = wl_display_get_registry(d->display);
5630 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005631
Marek Chalupaa519d062014-12-05 13:49:40 +01005632 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005633 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5634 return NULL;
5635 }
5636
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005637#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005638 if (init_egl(d) < 0)
5639 fprintf(stderr, "EGL does not seem to work, "
5640 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005641#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005642
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005643 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005644
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005645 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005646
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005647 wl_list_init(&d->window_list);
5648
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005649 init_dummy_surface(d);
5650
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005651 return d;
5652}
5653
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005654static void
5655display_destroy_outputs(struct display *display)
5656{
5657 struct output *tmp;
5658 struct output *output;
5659
5660 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5661 output_destroy(output);
5662}
5663
Pekka Paalanene1207c72011-12-16 12:02:09 +02005664static void
5665display_destroy_inputs(struct display *display)
5666{
5667 struct input *tmp;
5668 struct input *input;
5669
5670 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5671 input_destroy(input);
5672}
5673
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005674void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005675display_destroy(struct display *display)
5676{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005677 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005678 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5679 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005680
5681 if (!wl_list_empty(&display->deferred_list))
5682 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5683
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005684 cairo_surface_destroy(display->dummy_surface);
5685 free(display->dummy_surface_data);
5686
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005687 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005688 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005689
Daniel Stone97f68542012-05-30 16:32:01 +01005690 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005691
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005692 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005693 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005694
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005695#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005696 if (display->argb_device)
5697 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005698#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005699
Pekka Paalanen35e82632013-04-25 13:57:48 +03005700 if (display->subcompositor)
5701 wl_subcompositor_destroy(display->subcompositor);
5702
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005703 if (display->xdg_shell)
5704 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005705
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005706 if (display->ivi_application)
5707 ivi_application_destroy(display->ivi_application);
5708
Pekka Paalanenc2052982011-12-16 11:41:32 +02005709 if (display->shm)
5710 wl_shm_destroy(display->shm);
5711
5712 if (display->data_device_manager)
5713 wl_data_device_manager_destroy(display->data_device_manager);
5714
5715 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005716 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005717
5718 close(display->epoll_fd);
5719
U. Artie Eoff44874d92012-10-02 21:12:35 -07005720 if (!(display->display_fd_events & EPOLLERR) &&
5721 !(display->display_fd_events & EPOLLHUP))
5722 wl_display_flush(display->display);
5723
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005724 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005725 free(display);
5726}
5727
5728void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005729display_set_user_data(struct display *display, void *data)
5730{
5731 display->user_data = data;
5732}
5733
5734void *
5735display_get_user_data(struct display *display)
5736{
5737 return display->user_data;
5738}
5739
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005740struct wl_display *
5741display_get_display(struct display *display)
5742{
5743 return display->display;
5744}
5745
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005746int
5747display_has_subcompositor(struct display *display)
5748{
5749 if (display->subcompositor)
5750 return 1;
5751
5752 wl_display_roundtrip(display->display);
5753
5754 return display->subcompositor != NULL;
5755}
5756
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005757cairo_device_t *
5758display_get_cairo_device(struct display *display)
5759{
5760 return display->argb_device;
5761}
5762
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005763struct output *
5764display_get_output(struct display *display)
5765{
5766 return container_of(display->output_list.next, struct output, link);
5767}
5768
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005769struct wl_compositor *
5770display_get_compositor(struct display *display)
5771{
5772 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005773}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005774
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005775uint32_t
5776display_get_serial(struct display *display)
5777{
5778 return display->serial;
5779}
5780
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005781EGLDisplay
5782display_get_egl_display(struct display *d)
5783{
5784 return d->dpy;
5785}
5786
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005787struct wl_data_source *
5788display_create_data_source(struct display *display)
5789{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005790 if (display->data_device_manager)
5791 return wl_data_device_manager_create_data_source(display->data_device_manager);
5792 else
5793 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005794}
5795
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005796EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005797display_get_argb_egl_config(struct display *d)
5798{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005799 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005800}
5801
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005802int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005803display_acquire_window_surface(struct display *display,
5804 struct window *window,
5805 EGLContext ctx)
5806{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005807 struct surface *surface = window->main_surface;
5808
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005809 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005810 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005811
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005812 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005813 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005814}
5815
5816void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005817display_release_window_surface(struct display *display,
5818 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005819{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005820 struct surface *surface = window->main_surface;
5821
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005822 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005823 return;
5824
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005825 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005826}
5827
5828void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005829display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005830{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005831 wl_list_insert(&display->deferred_list, &task->link);
5832}
5833
5834void
5835display_watch_fd(struct display *display,
5836 int fd, uint32_t events, struct task *task)
5837{
5838 struct epoll_event ep;
5839
5840 ep.events = events;
5841 ep.data.ptr = task;
5842 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5843}
5844
5845void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005846display_unwatch_fd(struct display *display, int fd)
5847{
5848 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5849}
5850
5851void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005852display_run(struct display *display)
5853{
5854 struct task *task;
5855 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005856 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005857
Pekka Paalanen826d7952011-12-15 10:14:07 +02005858 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005859 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005860 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005861 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005862 struct task, link);
5863 wl_list_remove(&task->link);
5864 task->run(task, 0);
5865 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005866
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005867 wl_display_dispatch_pending(display->display);
5868
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005869 if (!display->running)
5870 break;
5871
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005872 ret = wl_display_flush(display->display);
5873 if (ret < 0 && errno == EAGAIN) {
5874 ep[0].events =
5875 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5876 ep[0].data.ptr = &display->display_task;
5877
5878 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5879 display->display_fd, &ep[0]);
5880 } else if (ret < 0) {
5881 break;
5882 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005883
5884 count = epoll_wait(display->epoll_fd,
5885 ep, ARRAY_LENGTH(ep), -1);
5886 for (i = 0; i < count; i++) {
5887 task = ep[i].data.ptr;
5888 task->run(task, ep[i].events);
5889 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005890 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005891}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005892
5893void
5894display_exit(struct display *display)
5895{
5896 display->running = 0;
5897}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005898
5899void
5900keysym_modifiers_add(struct wl_array *modifiers_map,
5901 const char *name)
5902{
5903 size_t len = strlen(name) + 1;
5904 char *p;
5905
5906 p = wl_array_add(modifiers_map, len);
5907
5908 if (p == NULL)
5909 return;
5910
5911 strncpy(p, name, len);
5912}
5913
5914static xkb_mod_index_t
5915keysym_modifiers_get_index(struct wl_array *modifiers_map,
5916 const char *name)
5917{
5918 xkb_mod_index_t index = 0;
5919 char *p = modifiers_map->data;
5920
5921 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5922 if (strcmp(p, name) == 0)
5923 return index;
5924
5925 index++;
5926 p += strlen(p) + 1;
5927 }
5928
5929 return XKB_MOD_INVALID;
5930}
5931
5932xkb_mod_mask_t
5933keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5934 const char *name)
5935{
5936 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5937
5938 if (index == XKB_MOD_INVALID)
5939 return XKB_MOD_INVALID;
5940
5941 return 1 << index;
5942}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005943
5944void *
5945fail_on_null(void *p)
5946{
5947 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005948 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005949 exit(EXIT_FAILURE);
5950 }
5951
5952 return p;
5953}
5954
5955void *
5956xmalloc(size_t s)
5957{
5958 return fail_on_null(malloc(s));
5959}
5960
Peter Huttererf3d62272013-08-08 11:57:05 +10005961void *
5962xzalloc(size_t s)
5963{
5964 return fail_on_null(zalloc(s));
5965}
5966
Kristian Høgsbergce278412013-07-25 15:20:20 -07005967char *
5968xstrdup(const char *s)
5969{
5970 return fail_on_null(strdup(s));
5971}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005972
5973void *
5974xrealloc(char *p, size_t s)
5975{
5976 return fail_on_null(realloc(p, s));
5977}