blob: 6941201ebc1620fe243b8f666e7f9ad0a967184b [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
2749 * based on the old surface dimensions
2750 */
Derek Foreman5d135482015-08-26 17:13:26 -05002751 if (sx < window->main_surface->allocation.x ||
2752 sy < window->main_surface->allocation.y ||
2753 sx > window->main_surface->allocation.width ||
Rob Bradford5f087742013-07-11 19:41:27 +01002754 sy > window->main_surface->allocation.height)
2755 return;
2756
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002757 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002758 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002759 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002760 }
2761
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002762 if (input->grab)
2763 widget = input->grab;
2764 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002765 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002766 if (widget) {
2767 if (widget->motion_handler)
2768 cursor = widget->motion_handler(input->focus_widget,
2769 input, time, sx, sy,
2770 widget->user_data);
2771 else
2772 cursor = widget->default_cursor;
2773 } else
2774 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002775
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002776 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002777}
2778
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002779static void
Daniel Stone37816df2012-05-16 18:45:18 +01002780pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002781 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002782{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002783 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002784 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002785 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002786
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002787 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002788 if (input->focus_widget && input->grab == NULL &&
2789 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002790 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002791
Neil Roberts6b28aad2012-01-23 19:11:18 +00002792 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002793 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002794 (*widget->button_handler)(widget,
2795 input, time,
2796 button, state,
2797 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002798
Daniel Stone4dbadb12012-05-30 16:31:51 +01002799 if (input->grab && input->grab_button == button &&
2800 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002801 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002802}
2803
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002804static void
Daniel Stone37816df2012-05-16 18:45:18 +01002805pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002806 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002807{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002808 struct input *input = data;
2809 struct widget *widget;
2810
2811 widget = input->focus_widget;
2812 if (input->grab)
2813 widget = input->grab;
2814 if (widget && widget->axis_handler)
2815 (*widget->axis_handler)(widget,
2816 input, time,
2817 axis, value,
2818 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002819}
2820
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002821static const struct wl_pointer_listener pointer_listener = {
2822 pointer_handle_enter,
2823 pointer_handle_leave,
2824 pointer_handle_motion,
2825 pointer_handle_button,
2826 pointer_handle_axis,
2827};
2828
2829static void
2830input_remove_keyboard_focus(struct input *input)
2831{
2832 struct window *window = input->keyboard_focus;
2833 struct itimerspec its;
2834
2835 its.it_interval.tv_sec = 0;
2836 its.it_interval.tv_nsec = 0;
2837 its.it_value.tv_sec = 0;
2838 its.it_value.tv_nsec = 0;
2839 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2840
2841 if (!window)
2842 return;
2843
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002844 if (window->keyboard_focus_handler)
2845 (*window->keyboard_focus_handler)(window, NULL,
2846 window->user_data);
2847
2848 input->keyboard_focus = NULL;
2849}
2850
2851static void
2852keyboard_repeat_func(struct task *task, uint32_t events)
2853{
2854 struct input *input =
2855 container_of(task, struct input, repeat_task);
2856 struct window *window = input->keyboard_focus;
2857 uint64_t exp;
2858
2859 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2860 /* If we change the timer between the fd becoming
2861 * readable and getting here, there'll be nothing to
2862 * read and we get EAGAIN. */
2863 return;
2864
2865 if (window && window->key_handler) {
2866 (*window->key_handler)(window, input, input->repeat_time,
2867 input->repeat_key, input->repeat_sym,
2868 WL_KEYBOARD_KEY_STATE_PRESSED,
2869 window->user_data);
2870 }
2871}
2872
2873static void
2874keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2875 uint32_t format, int fd, uint32_t size)
2876{
2877 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002878 struct xkb_keymap *keymap;
2879 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002880 char *map_str;
2881
2882 if (!data) {
2883 close(fd);
2884 return;
2885 }
2886
2887 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2888 close(fd);
2889 return;
2890 }
2891
2892 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2893 if (map_str == MAP_FAILED) {
2894 close(fd);
2895 return;
2896 }
2897
Ran Benita2e1968f2014-08-19 23:59:51 +03002898 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2899 map_str,
2900 XKB_KEYMAP_FORMAT_TEXT_V1,
2901 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002902 munmap(map_str, size);
2903 close(fd);
2904
Rui Matos3eccb862013-10-10 19:44:22 +02002905 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002906 fprintf(stderr, "failed to compile keymap\n");
2907 return;
2908 }
2909
Rui Matos3eccb862013-10-10 19:44:22 +02002910 state = xkb_state_new(keymap);
2911 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002912 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002913 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002914 return;
2915 }
2916
Rui Matos3eccb862013-10-10 19:44:22 +02002917 xkb_keymap_unref(input->xkb.keymap);
2918 xkb_state_unref(input->xkb.state);
2919 input->xkb.keymap = keymap;
2920 input->xkb.state = state;
2921
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002922 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002923 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002924 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002925 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002926 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002927 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002928}
2929
2930static void
2931keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2932 uint32_t serial, struct wl_surface *surface,
2933 struct wl_array *keys)
2934{
2935 struct input *input = data;
2936 struct window *window;
2937
2938 input->display->serial = serial;
2939 input->keyboard_focus = wl_surface_get_user_data(surface);
2940
2941 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002942 if (window->keyboard_focus_handler)
2943 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002944 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002945}
2946
2947static void
2948keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2949 uint32_t serial, struct wl_surface *surface)
2950{
2951 struct input *input = data;
2952
2953 input->display->serial = serial;
2954 input_remove_keyboard_focus(input);
2955}
2956
Scott Moreau210d0792012-03-22 10:47:01 -06002957static void
Daniel Stone37816df2012-05-16 18:45:18 +01002958keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002959 uint32_t serial, uint32_t time, uint32_t key,
2960 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002961{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002962 struct input *input = data;
2963 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002964 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002965 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002966 const xkb_keysym_t *syms;
2967 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002968 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002969
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002970 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002971 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002972 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002973 return;
2974
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002975 /* We only use input grabs for pointer events for now, so just
2976 * ignore key presses if a grab is active. We expand the key
2977 * event delivery mechanism to route events to widgets to
2978 * properly handle key grabs. In the meantime, this prevents
2979 * key event devlivery while a grab is active. */
2980 if (input->grab && input->grab_button == 0)
2981 return;
2982
Ran Benita2e1968f2014-08-19 23:59:51 +03002983 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002984
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002985 sym = XKB_KEY_NoSymbol;
2986 if (num_syms == 1)
2987 sym = syms[0];
2988
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002989
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002990 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002991 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002992 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002993 } else if (sym == XKB_KEY_F11 &&
2994 window->fullscreen_handler &&
2995 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2996 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002997 } else if (sym == XKB_KEY_F4 &&
2998 input->modifiers == MOD_ALT_MASK &&
2999 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003000 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003001 } else if (window->key_handler) {
3002 (*window->key_handler)(window, input, time, key,
3003 sym, state, window->user_data);
3004 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003005
3006 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3007 key == input->repeat_key) {
3008 its.it_interval.tv_sec = 0;
3009 its.it_interval.tv_nsec = 0;
3010 its.it_value.tv_sec = 0;
3011 its.it_value.tv_nsec = 0;
3012 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003013 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3014 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003015 input->repeat_sym = sym;
3016 input->repeat_key = key;
3017 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003018 its.it_interval.tv_sec = input->repeat_rate_sec;
3019 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3020 its.it_value.tv_sec = input->repeat_delay_sec;
3021 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003022 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3023 }
3024}
3025
3026static void
Daniel Stone351eb612012-05-31 15:27:47 -04003027keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3028 uint32_t serial, uint32_t mods_depressed,
3029 uint32_t mods_latched, uint32_t mods_locked,
3030 uint32_t group)
3031{
3032 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003033 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003034
Matt Ropere61561f2013-06-24 16:52:43 +01003035 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3036 if (!input->xkb.keymap)
3037 return;
3038
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003039 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3040 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003041 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003042 XKB_STATE_MODS_DEPRESSED |
3043 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003044 input->modifiers = 0;
3045 if (mask & input->xkb.control_mask)
3046 input->modifiers |= MOD_CONTROL_MASK;
3047 if (mask & input->xkb.alt_mask)
3048 input->modifiers |= MOD_ALT_MASK;
3049 if (mask & input->xkb.shift_mask)
3050 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003051}
3052
Jonny Lamb06959082014-08-12 14:58:27 +02003053static void
3054set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3055{
3056 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3057 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3058
3059 /* a rate of zero disables any repeating, regardless of the delay's
3060 * value */
3061 if (rate == 0)
3062 return;
3063
3064 if (rate == 1)
3065 input->repeat_rate_sec = 1;
3066 else
3067 input->repeat_rate_nsec = 1000000000 / rate;
3068
3069 input->repeat_delay_sec = delay / 1000;
3070 delay -= (input->repeat_delay_sec * 1000);
3071 input->repeat_delay_nsec = delay * 1000 * 1000;
3072}
3073
3074static void
3075keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3076 int32_t rate, int32_t delay)
3077{
3078 struct input *input = data;
3079
3080 set_repeat_info(input, rate, delay);
3081}
3082
Daniel Stone37816df2012-05-16 18:45:18 +01003083static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003084 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003085 keyboard_handle_enter,
3086 keyboard_handle_leave,
3087 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003088 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003089 keyboard_handle_repeat_info
3090
Daniel Stone37816df2012-05-16 18:45:18 +01003091};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003092
3093static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003094touch_handle_down(void *data, struct wl_touch *wl_touch,
3095 uint32_t serial, uint32_t time, struct wl_surface *surface,
3096 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3097{
3098 struct input *input = data;
3099 struct widget *widget;
3100 float sx = wl_fixed_to_double(x_w);
3101 float sy = wl_fixed_to_double(y_w);
3102
Rusty Lynch1084da52013-08-15 09:10:08 -07003103 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003104 input->touch_focus = wl_surface_get_user_data(surface);
3105 if (!input->touch_focus) {
3106 DBG("Failed to find to touch focus for surface %p\n", surface);
3107 return;
3108 }
3109
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003110 if (surface != input->touch_focus->main_surface->surface) {
3111 DBG("Ignoring input event from subsurface %p\n", surface);
3112 input->touch_focus = NULL;
3113 return;
3114 }
3115
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003116 if (input->grab)
3117 widget = input->grab;
3118 else
3119 widget = window_find_widget(input->touch_focus,
3120 wl_fixed_to_double(x_w),
3121 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003122 if (widget) {
3123 struct touch_point *tp = xmalloc(sizeof *tp);
3124 if (tp) {
3125 tp->id = id;
3126 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003127 tp->x = sx;
3128 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003129 wl_list_insert(&input->touch_point_list, &tp->link);
3130
3131 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003132 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003133 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003134 sx, sy,
3135 widget->user_data);
3136 }
3137 }
3138}
3139
3140static void
3141touch_handle_up(void *data, struct wl_touch *wl_touch,
3142 uint32_t serial, uint32_t time, int32_t id)
3143{
3144 struct input *input = data;
3145 struct touch_point *tp, *tmp;
3146
Rusty Lynch041815a2013-08-08 21:20:38 -07003147 if (!input->touch_focus) {
3148 DBG("No touch focus found for touch up event!\n");
3149 return;
3150 }
3151
3152 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3153 if (tp->id != id)
3154 continue;
3155
3156 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003157 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003158 time, id,
3159 tp->widget->user_data);
3160
3161 wl_list_remove(&tp->link);
3162 free(tp);
3163
3164 return;
3165 }
3166}
3167
3168static void
3169touch_handle_motion(void *data, struct wl_touch *wl_touch,
3170 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3171{
3172 struct input *input = data;
3173 struct touch_point *tp;
3174 float sx = wl_fixed_to_double(x_w);
3175 float sy = wl_fixed_to_double(y_w);
3176
3177 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3178
3179 if (!input->touch_focus) {
3180 DBG("No touch focus found for touch motion event!\n");
3181 return;
3182 }
3183
3184 wl_list_for_each(tp, &input->touch_point_list, link) {
3185 if (tp->id != id)
3186 continue;
3187
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003188 tp->x = sx;
3189 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003190 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003191 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003192 id, sx, sy,
3193 tp->widget->user_data);
3194 return;
3195 }
3196}
3197
3198static void
3199touch_handle_frame(void *data, struct wl_touch *wl_touch)
3200{
3201 struct input *input = data;
3202 struct touch_point *tp, *tmp;
3203
3204 DBG("touch_handle_frame\n");
3205
3206 if (!input->touch_focus) {
3207 DBG("No touch focus found for touch frame event!\n");
3208 return;
3209 }
3210
3211 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3212 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003213 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003214 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003215 }
3216}
3217
3218static void
3219touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3220{
3221 struct input *input = data;
3222 struct touch_point *tp, *tmp;
3223
3224 DBG("touch_handle_cancel\n");
3225
3226 if (!input->touch_focus) {
3227 DBG("No touch focus found for touch cancel event!\n");
3228 return;
3229 }
3230
3231 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3232 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003233 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3234 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003235
3236 wl_list_remove(&tp->link);
3237 free(tp);
3238 }
3239}
3240
3241static const struct wl_touch_listener touch_listener = {
3242 touch_handle_down,
3243 touch_handle_up,
3244 touch_handle_motion,
3245 touch_handle_frame,
3246 touch_handle_cancel,
3247};
3248
3249static void
Daniel Stone37816df2012-05-16 18:45:18 +01003250seat_handle_capabilities(void *data, struct wl_seat *seat,
3251 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003252{
Daniel Stone37816df2012-05-16 18:45:18 +01003253 struct input *input = data;
3254
3255 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3256 input->pointer = wl_seat_get_pointer(seat);
3257 wl_pointer_set_user_data(input->pointer, input);
3258 wl_pointer_add_listener(input->pointer, &pointer_listener,
3259 input);
3260 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003261 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003262 wl_pointer_release(input->pointer);
3263 else
3264 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003265 input->pointer = NULL;
3266 }
3267
3268 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3269 input->keyboard = wl_seat_get_keyboard(seat);
3270 wl_keyboard_set_user_data(input->keyboard, input);
3271 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3272 input);
3273 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003274 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003275 wl_keyboard_release(input->keyboard);
3276 else
3277 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003278 input->keyboard = NULL;
3279 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003280
3281 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3282 input->touch = wl_seat_get_touch(seat);
3283 wl_touch_set_user_data(input->touch, input);
3284 wl_touch_add_listener(input->touch, &touch_listener, input);
3285 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003286 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003287 wl_touch_release(input->touch);
3288 else
3289 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003290 input->touch = NULL;
3291 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003292}
3293
Rob Bradford08031182013-08-13 20:11:03 +01003294static void
3295seat_handle_name(void *data, struct wl_seat *seat,
3296 const char *name)
3297{
3298
3299}
3300
Daniel Stone37816df2012-05-16 18:45:18 +01003301static const struct wl_seat_listener seat_listener = {
3302 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003303 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003304};
3305
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003306void
3307input_get_position(struct input *input, int32_t *x, int32_t *y)
3308{
3309 *x = input->sx;
3310 *y = input->sy;
3311}
3312
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003313int
3314input_get_touch(struct input *input, int32_t id, float *x, float *y)
3315{
3316 struct touch_point *tp;
3317
3318 wl_list_for_each(tp, &input->touch_point_list, link) {
3319 if (tp->id != id)
3320 continue;
3321
3322 *x = tp->x;
3323 *y = tp->y;
3324 return 0;
3325 }
3326
3327 return -1;
3328}
3329
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003330struct display *
3331input_get_display(struct input *input)
3332{
3333 return input->display;
3334}
3335
Daniel Stone37816df2012-05-16 18:45:18 +01003336struct wl_seat *
3337input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003338{
Daniel Stone37816df2012-05-16 18:45:18 +01003339 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003340}
3341
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003342uint32_t
3343input_get_modifiers(struct input *input)
3344{
3345 return input->modifiers;
3346}
3347
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003348struct widget *
3349input_get_focus_widget(struct input *input)
3350{
3351 return input->focus_widget;
3352}
3353
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003354struct data_offer {
3355 struct wl_data_offer *offer;
3356 struct input *input;
3357 struct wl_array types;
3358 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003359
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003360 struct task io_task;
3361 int fd;
3362 data_func_t func;
3363 int32_t x, y;
3364 void *user_data;
3365};
3366
3367static void
3368data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3369{
3370 struct data_offer *offer = data;
3371 char **p;
3372
3373 p = wl_array_add(&offer->types, sizeof *p);
3374 *p = strdup(type);
3375}
3376
3377static const struct wl_data_offer_listener data_offer_listener = {
3378 data_offer_offer,
3379};
3380
3381static void
3382data_offer_destroy(struct data_offer *offer)
3383{
3384 char **p;
3385
3386 offer->refcount--;
3387 if (offer->refcount == 0) {
3388 wl_data_offer_destroy(offer->offer);
3389 for (p = offer->types.data; *p; p++)
3390 free(*p);
3391 wl_array_release(&offer->types);
3392 free(offer);
3393 }
3394}
3395
3396static void
3397data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003398 struct wl_data_device *data_device,
3399 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003400{
3401 struct data_offer *offer;
3402
Brian Lovinbc919262013-08-07 15:34:59 -07003403 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003404
3405 wl_array_init(&offer->types);
3406 offer->refcount = 1;
3407 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003408 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003409 wl_data_offer_add_listener(offer->offer,
3410 &data_offer_listener, offer);
3411}
3412
3413static void
3414data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003415 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003416 wl_fixed_t x_w, wl_fixed_t y_w,
3417 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003418{
3419 struct input *input = data;
3420 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003421 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003422 float x = wl_fixed_to_double(x_w);
3423 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003424 char **p;
3425
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003426 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003427 input->drag_enter_serial = serial;
3428 input->drag_focus = window,
3429 input->drag_x = x;
3430 input->drag_y = y;
3431
3432 if (!input->touch_grab)
3433 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003434
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003435 if (offer) {
3436 input->drag_offer = wl_data_offer_get_user_data(offer);
3437
3438 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3439 *p = NULL;
3440
3441 types_data = input->drag_offer->types.data;
3442 } else {
3443 input->drag_offer = NULL;
3444 types_data = NULL;
3445 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003446
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003447 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003448 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003449 window->user_data);
3450}
3451
3452static void
3453data_device_leave(void *data, struct wl_data_device *data_device)
3454{
3455 struct input *input = data;
3456
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003457 if (input->drag_offer) {
3458 data_offer_destroy(input->drag_offer);
3459 input->drag_offer = NULL;
3460 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003461}
3462
3463static void
3464data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003465 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003466{
3467 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003468 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003469 float x = wl_fixed_to_double(x_w);
3470 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003471 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003472
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003473 input->drag_x = x;
3474 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003475
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003476 if (input->drag_offer)
3477 types_data = input->drag_offer->types.data;
3478 else
3479 types_data = NULL;
3480
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003481 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003482 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003483 window->user_data);
3484}
3485
3486static void
3487data_device_drop(void *data, struct wl_data_device *data_device)
3488{
3489 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003490 struct window *window = input->drag_focus;
3491 float x, y;
3492
3493 x = input->drag_x;
3494 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003495
3496 if (window->drop_handler)
3497 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003498 x, y, window->user_data);
3499
3500 if (input->touch_grab)
3501 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003502}
3503
3504static void
3505data_device_selection(void *data,
3506 struct wl_data_device *wl_data_device,
3507 struct wl_data_offer *offer)
3508{
3509 struct input *input = data;
3510 char **p;
3511
3512 if (input->selection_offer)
3513 data_offer_destroy(input->selection_offer);
3514
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003515 if (offer) {
3516 input->selection_offer = wl_data_offer_get_user_data(offer);
3517 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3518 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003519 } else {
3520 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003521 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003522}
3523
3524static const struct wl_data_device_listener data_device_listener = {
3525 data_device_data_offer,
3526 data_device_enter,
3527 data_device_leave,
3528 data_device_motion,
3529 data_device_drop,
3530 data_device_selection
3531};
3532
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003533static void
3534input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003535{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003536 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003537 struct wl_cursor *cursor;
3538 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003539
Daniel Stone80972742012-11-07 17:51:39 +11003540 if (!input->pointer)
3541 return;
3542
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003543 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003544 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003545 return;
3546
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003547 if (index >= (int) cursor->image_count) {
3548 fprintf(stderr, "cursor index out of range\n");
3549 return;
3550 }
3551
3552 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003553 buffer = wl_cursor_image_get_buffer(image);
3554 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003555 return;
3556
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003557 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3558 wl_surface_damage(input->pointer_surface, 0, 0,
3559 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003560 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003561 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3562 input->pointer_surface,
3563 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003564}
3565
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003566static const struct wl_callback_listener pointer_surface_listener;
3567
Derek Foreman493d9792015-03-04 16:26:25 -06003568static bool
3569input_set_pointer_special(struct input *input)
3570{
3571 if (input->current_cursor == CURSOR_BLANK) {
3572 wl_pointer_set_cursor(input->pointer,
3573 input->pointer_enter_serial,
3574 NULL, 0, 0);
3575 return true;
3576 }
3577
3578 if (input->current_cursor == CURSOR_UNSET)
3579 return true;
3580
3581 return false;
3582}
3583
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003584static void
Derek Foreman118a4292015-04-22 17:23:35 -05003585schedule_pointer_image_update(struct input *input,
3586 struct wl_cursor *cursor,
3587 uint32_t duration,
3588 bool force_frame)
3589{
3590 /* Some silly cursor sets have enormous pauses in them. In these
3591 * cases it's better to use a timer even if it results in less
3592 * accurate presentation, since it will save us having to set the
3593 * same cursor image over and over again.
3594 *
3595 * This is really not the way we're supposed to time any kind of
3596 * animation, but we're pretending it's OK here because we don't
3597 * want animated cursors with long delays to needlessly hog CPU.
3598 *
3599 * We use force_frame to ensure we don't accumulate large timing
3600 * errors by running off the wrong clock.
3601 */
3602 if (!force_frame && duration > 100) {
3603 struct timespec tp;
3604
3605 clock_gettime(CLOCK_MONOTONIC, &tp);
3606 input->cursor_timer_start = tp.tv_sec * 1000
3607 + tp.tv_nsec / 1000000;
3608 cursor_delay_timer_reset(input, duration);
3609 return;
3610 }
3611
3612 /* for short durations we'll just spin on frame callbacks for
3613 * accurate timing - the way any kind of timing sensitive animation
3614 * should really be done. */
3615 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3616 wl_callback_add_listener(input->cursor_frame_cb,
3617 &pointer_surface_listener, input);
3618
3619}
3620
3621static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003622pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3623 uint32_t time)
3624{
3625 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003626 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003627 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003628 uint32_t duration;
3629 bool force_frame = true;
3630
3631 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003632
3633 if (callback) {
3634 assert(callback == input->cursor_frame_cb);
3635 wl_callback_destroy(callback);
3636 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003637 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003638 }
3639
Daniel Stone80972742012-11-07 17:51:39 +11003640 if (!input->pointer)
3641 return;
3642
Derek Foreman493d9792015-03-04 16:26:25 -06003643 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003644 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003645
Daniel Stonea494f1d2012-06-18 19:31:12 +01003646 cursor = input->display->cursors[input->current_cursor];
3647 if (!cursor)
3648 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003649
3650 /* FIXME We don't have the current time on the first call so we set
3651 * the animation start to the time of the first frame callback. */
3652 if (time == 0)
3653 input->cursor_anim_start = 0;
3654 else if (input->cursor_anim_start == 0)
3655 input->cursor_anim_start = time;
3656
Derek Foreman118a4292015-04-22 17:23:35 -05003657 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003658
Derek Foreman118a4292015-04-22 17:23:35 -05003659 if (time == 0 || input->cursor_anim_start == 0) {
3660 duration = 0;
3661 i = 0;
3662 } else
3663 i = wl_cursor_frame_and_duration(
3664 cursor,
3665 time - input->cursor_anim_start,
3666 &duration);
3667
3668 if (cursor->image_count > 1)
3669 schedule_pointer_image_update(input, cursor, duration,
3670 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003671
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003672 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003673}
3674
Derek Foreman118a4292015-04-22 17:23:35 -05003675static void
3676cursor_timer_func(struct task *task, uint32_t events)
3677{
3678 struct input *input = container_of(task, struct input, cursor_task);
3679 struct timespec tp;
3680 struct wl_cursor *cursor;
3681 uint32_t time;
3682 uint64_t exp;
3683
3684 if (!input->cursor_timer_running)
3685 return;
3686
3687 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3688 return;
3689
3690 cursor = input->display->cursors[input->current_cursor];
3691 if (!cursor)
3692 return;
3693
3694 clock_gettime(CLOCK_MONOTONIC, &tp);
3695 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3696 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3697}
3698
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003699static const struct wl_callback_listener pointer_surface_listener = {
3700 pointer_surface_frame_callback
3701};
3702
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003703void
3704input_set_pointer_image(struct input *input, int pointer)
3705{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003706 int force = 0;
3707
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003708 if (!input->pointer)
3709 return;
3710
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003711 if (input->pointer_enter_serial > input->cursor_serial)
3712 force = 1;
3713
3714 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003715 return;
3716
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003717 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003718 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003719 if (!input->cursor_frame_cb)
3720 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003721 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003722 /* The current frame callback may be stuck if, for instance,
3723 * the set cursor request was processed by the server after
3724 * this client lost the focus. In this case the cursor surface
3725 * might not be mapped and the frame callback wouldn't ever
3726 * complete. Send a set_cursor and attach to try to map the
3727 * cursor surface again so that the callback will finish */
3728 input_set_pointer_image_index(input, 0);
3729 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003730}
3731
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003732struct wl_data_device *
3733input_get_data_device(struct input *input)
3734{
3735 return input->data_device;
3736}
3737
3738void
3739input_set_selection(struct input *input,
3740 struct wl_data_source *source, uint32_t time)
3741{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003742 if (input->data_device)
3743 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003744}
3745
3746void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003747input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003748{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003749 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003750 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003751}
3752
3753static void
3754offer_io_func(struct task *task, uint32_t events)
3755{
3756 struct data_offer *offer =
3757 container_of(task, struct data_offer, io_task);
3758 unsigned int len;
3759 char buffer[4096];
3760
3761 len = read(offer->fd, buffer, sizeof buffer);
3762 offer->func(buffer, len,
3763 offer->x, offer->y, offer->user_data);
3764
3765 if (len == 0) {
3766 close(offer->fd);
3767 data_offer_destroy(offer);
3768 }
3769}
3770
3771static void
3772data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3773 data_func_t func, void *user_data)
3774{
3775 int p[2];
3776
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003777 if (pipe2(p, O_CLOEXEC) == -1)
3778 return;
3779
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003780 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3781 close(p[1]);
3782
3783 offer->io_task.run = offer_io_func;
3784 offer->fd = p[0];
3785 offer->func = func;
3786 offer->refcount++;
3787 offer->user_data = user_data;
3788
3789 display_watch_fd(offer->input->display,
3790 offer->fd, EPOLLIN, &offer->io_task);
3791}
3792
3793void
3794input_receive_drag_data(struct input *input, const char *mime_type,
3795 data_func_t func, void *data)
3796{
3797 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003798 input->drag_offer->x = input->drag_x;
3799 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003800}
3801
3802int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003803input_receive_drag_data_to_fd(struct input *input,
3804 const char *mime_type, int fd)
3805{
3806 if (input->drag_offer)
3807 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3808
3809 return 0;
3810}
3811
3812int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003813input_receive_selection_data(struct input *input, const char *mime_type,
3814 data_func_t func, void *data)
3815{
3816 char **p;
3817
3818 if (input->selection_offer == NULL)
3819 return -1;
3820
3821 for (p = input->selection_offer->types.data; *p; p++)
3822 if (strcmp(mime_type, *p) == 0)
3823 break;
3824
3825 if (*p == NULL)
3826 return -1;
3827
3828 data_offer_receive_data(input->selection_offer,
3829 mime_type, func, data);
3830 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003831}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003832
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003833int
3834input_receive_selection_data_to_fd(struct input *input,
3835 const char *mime_type, int fd)
3836{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003837 if (input->selection_offer)
3838 wl_data_offer_receive(input->selection_offer->offer,
3839 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003840
3841 return 0;
3842}
3843
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003844void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003845window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003846{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003847 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003848 return;
3849
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003850 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003851}
3852
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003853static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003854surface_set_synchronized(struct surface *surface)
3855{
3856 if (!surface->subsurface)
3857 return;
3858
3859 if (surface->synchronized)
3860 return;
3861
3862 wl_subsurface_set_sync(surface->subsurface);
3863 surface->synchronized = 1;
3864}
3865
3866static void
3867surface_set_synchronized_default(struct surface *surface)
3868{
3869 if (!surface->subsurface)
3870 return;
3871
3872 if (surface->synchronized == surface->synchronized_default)
3873 return;
3874
3875 if (surface->synchronized_default)
3876 wl_subsurface_set_sync(surface->subsurface);
3877 else
3878 wl_subsurface_set_desync(surface->subsurface);
3879
3880 surface->synchronized = surface->synchronized_default;
3881}
3882
3883static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003884surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003885{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003886 struct widget *widget = surface->widget;
3887 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003888
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003889 if (surface->input_region) {
3890 wl_region_destroy(surface->input_region);
3891 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003892 }
3893
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003894 if (surface->opaque_region)
3895 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003896
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003897 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003898
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003899 if (widget->resize_handler)
3900 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003901 widget->allocation.width,
3902 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003903 widget->user_data);
3904
Pekka Paalanen35e82632013-04-25 13:57:48 +03003905 if (surface->subsurface &&
3906 (surface->allocation.x != widget->allocation.x ||
3907 surface->allocation.y != widget->allocation.y)) {
3908 wl_subsurface_set_position(surface->subsurface,
3909 widget->allocation.x,
3910 widget->allocation.y);
3911 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003912 if (surface->allocation.width != widget->allocation.width ||
3913 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003914 window_schedule_redraw(widget->window);
3915 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003916 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003917
3918 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003919 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003920 widget->allocation.width,
3921 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003922}
3923
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003924static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003925window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003926{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003927 struct surface *surface;
3928
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003929 widget_set_allocation(window->main_surface->widget,
3930 window->pending_allocation.x,
3931 window->pending_allocation.y,
3932 window->pending_allocation.width,
3933 window->pending_allocation.height);
3934
3935 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003936
3937 /* The main surface is in the list, too. Main surface's
3938 * resize_handler is responsible for calling widget_set_allocation()
3939 * on all sub-surface root widgets, so they will be resized
3940 * properly.
3941 */
3942 wl_list_for_each(surface, &window->subsurface_list, link) {
3943 if (surface == window->main_surface)
3944 continue;
3945
3946 surface_set_synchronized(surface);
3947 surface_resize(surface);
3948 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003949
3950 if (!window->fullscreen && !window->maximized)
3951 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003952}
3953
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003954static void
3955idle_resize(struct window *window)
3956{
3957 window->resize_needed = 0;
3958 window->redraw_needed = 1;
3959
3960 DBG("from %dx%d to %dx%d\n",
3961 window->main_surface->server_allocation.width,
3962 window->main_surface->server_allocation.height,
3963 window->pending_allocation.width,
3964 window->pending_allocation.height);
3965
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003966 window_do_resize(window);
3967}
3968
3969static void
3970undo_resize(struct window *window)
3971{
3972 window->pending_allocation.width =
3973 window->main_surface->server_allocation.width;
3974 window->pending_allocation.height =
3975 window->main_surface->server_allocation.height;
3976
3977 DBG("back to %dx%d\n",
3978 window->main_surface->server_allocation.width,
3979 window->main_surface->server_allocation.height);
3980
3981 window_do_resize(window);
3982
3983 if (window->pending_allocation.width == 0 &&
3984 window->pending_allocation.height == 0) {
3985 fprintf(stderr, "Error: Could not draw a surface, "
3986 "most likely due to insufficient disk space in "
3987 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3988 exit(EXIT_FAILURE);
3989 }
3990}
3991
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003992void
3993window_schedule_resize(struct window *window, int width, int height)
3994{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003995 /* We should probably get these numbers from the theme. */
3996 const int min_width = 200, min_height = 200;
3997
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003998 window->pending_allocation.x = 0;
3999 window->pending_allocation.y = 0;
4000 window->pending_allocation.width = width;
4001 window->pending_allocation.height = height;
4002
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004003 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004004 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004005 window->min_allocation.width = min_width;
4006 else
4007 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004008 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004009 window->min_allocation.height = min_height;
4010 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004011 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004012 }
4013
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004014 if (window->pending_allocation.width < window->min_allocation.width)
4015 window->pending_allocation.width = window->min_allocation.width;
4016 if (window->pending_allocation.height < window->min_allocation.height)
4017 window->pending_allocation.height = window->min_allocation.height;
4018
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004019 window->resize_needed = 1;
4020 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004021}
4022
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004023void
4024widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4025{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004026 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004027}
4028
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004029static int
4030window_get_shadow_margin(struct window *window)
4031{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004032 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004033 return frame_get_shadow_margin(window->frame->frame);
4034 else
4035 return 0;
4036}
4037
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004038static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004039handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004040 int32_t width, int32_t height,
4041 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004042{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004043 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004044 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004045
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004046 window->maximized = 0;
4047 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004048 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004049 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004050
4051 wl_array_for_each(p, states) {
4052 uint32_t state = *p;
4053 switch (state) {
4054 case XDG_SURFACE_STATE_MAXIMIZED:
4055 window->maximized = 1;
4056 break;
4057 case XDG_SURFACE_STATE_FULLSCREEN:
4058 window->fullscreen = 1;
4059 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004060 case XDG_SURFACE_STATE_RESIZING:
4061 window->resizing = 1;
4062 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004063 case XDG_SURFACE_STATE_ACTIVATED:
4064 window->focused = 1;
4065 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004066 default:
4067 /* Unknown state */
4068 break;
4069 }
4070 }
4071
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004072 if (window->frame) {
4073 if (window->maximized) {
4074 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4075 } else {
4076 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4077 }
4078
4079 if (window->focused) {
4080 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4081 } else {
4082 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4083 }
4084 }
4085
Jasper St. Pierref184c382014-05-06 08:33:27 -04004086 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004087 /* The width / height params are for window geometry,
4088 * but window_schedule_resize takes allocation. Add
4089 * on the shadow margin to get the difference. */
4090 int margin = window_get_shadow_margin(window);
4091
4092 window_schedule_resize(window,
4093 width + margin * 2,
4094 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004095 } else {
4096 window_schedule_resize(window,
4097 window->saved_allocation.width,
4098 window->saved_allocation.height);
4099 }
4100
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004101 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004102
4103 if (window->state_changed_handler)
4104 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004105}
4106
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004107static void
4108handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4109{
4110 struct window *window = data;
4111 window_close(window);
4112}
4113
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004114static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004115 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004116 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004117};
4118
4119static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004120window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004121{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004122 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004123
4124 if (!window->xdg_surface)
4125 return;
4126
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004127 if (window->parent == window->last_parent)
4128 return;
4129
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004130 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004131 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004132 else
4133 parent_surface = NULL;
4134
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004135 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004136 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004137}
4138
4139static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004140window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004141{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004142 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004143 frame_input_rect(window->frame->frame,
4144 &geometry->x,
4145 &geometry->y,
4146 &geometry->width,
4147 &geometry->height);
4148 else
4149 window_get_allocation(window, geometry);
4150}
4151
4152static void
4153window_sync_geometry(struct window *window)
4154{
4155 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004156
4157 if (!window->xdg_surface)
4158 return;
4159
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004160 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004161 if (geometry.x == window->last_geometry.x &&
4162 geometry.y == window->last_geometry.y &&
4163 geometry.width == window->last_geometry.width &&
4164 geometry.height == window->last_geometry.height)
4165 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004166
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004167 xdg_surface_set_window_geometry(window->xdg_surface,
4168 geometry.x,
4169 geometry.y,
4170 geometry.width,
4171 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004172 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004173}
4174
4175static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004176window_flush(struct window *window)
4177{
4178 struct surface *surface;
4179
4180 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004181 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004182 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004183 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004184 }
4185 }
4186
4187 wl_list_for_each(surface, &window->subsurface_list, link) {
4188 if (surface == window->main_surface)
4189 continue;
4190
4191 surface_flush(surface);
4192 }
4193
4194 surface_flush(window->main_surface);
4195}
4196
4197static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004198menu_destroy(struct menu *menu)
4199{
4200 widget_destroy(menu->widget);
4201 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004202 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004203 free(menu);
4204}
4205
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004206void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004207window_get_allocation(struct window *window,
4208 struct rectangle *allocation)
4209{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004210 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004211}
4212
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004213static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004214widget_redraw(struct widget *widget)
4215{
4216 struct widget *child;
4217
4218 if (widget->redraw_handler)
4219 widget->redraw_handler(widget, widget->user_data);
4220 wl_list_for_each(child, &widget->child_list, link)
4221 widget_redraw(child);
4222}
4223
4224static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004225frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4226{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004227 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004228
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004229 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004230 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004231 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004232 surface->frame_cb = NULL;
4233
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004234 surface->last_time = time;
4235
Pekka Paalanen71233882013-04-25 13:57:53 +03004236 if (surface->redraw_needed || surface->window->redraw_needed) {
4237 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004238 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004239 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004240}
4241
4242static const struct wl_callback_listener listener = {
4243 frame_callback
4244};
4245
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004246static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004247surface_redraw(struct surface *surface)
4248{
Pekka Paalanen71233882013-04-25 13:57:53 +03004249 DBG_OBJ(surface->surface, "begin\n");
4250
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004251 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004252 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004253
4254 /* Whole-window redraw forces a redraw even if the previous has
4255 * not yet hit the screen.
4256 */
4257 if (surface->frame_cb) {
4258 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004259 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004260
Pekka Paalanen71233882013-04-25 13:57:53 +03004261 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004262 wl_callback_destroy(surface->frame_cb);
4263 }
4264
Neil Roberts97b747c2013-12-19 16:17:12 +00004265 if (surface->widget->use_cairo &&
4266 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004267 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4268 return -1;
4269 }
4270
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004271 surface->frame_cb = wl_surface_frame(surface->surface);
4272 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004273 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004274
4275 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004276 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004277 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004278 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004279 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004280}
4281
4282static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004283idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004284{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004285 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004286 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004287 int failed = 0;
4288 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004289
Pekka Paalanen71233882013-04-25 13:57:53 +03004290 DBG(" --------- \n");
4291
Pekka Paalaneneebff542013-04-25 13:57:52 +03004292 wl_list_init(&window->redraw_task.link);
4293 window->redraw_task_scheduled = 0;
4294
4295 if (window->resize_needed) {
4296 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004297 if (window->main_surface->frame_cb) {
4298 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004299 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004300 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004301
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004302 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004303 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004304 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004305
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004306 if (surface_redraw(window->main_surface) < 0) {
4307 /*
4308 * Only main_surface failure will cause us to undo the resize.
4309 * If sub-surfaces fail, they will just be broken with old
4310 * content.
4311 */
4312 failed = 1;
4313 } else {
4314 wl_list_for_each(surface, &window->subsurface_list, link) {
4315 if (surface == window->main_surface)
4316 continue;
4317
4318 surface_redraw(surface);
4319 }
4320 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004321
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004322 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004323 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004324
4325 wl_list_for_each(surface, &window->subsurface_list, link)
4326 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004327
4328 if (resized && failed) {
4329 /* Restore widget tree to correspond to what is on screen. */
4330 undo_resize(window);
4331 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004332}
4333
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004334static void
4335window_schedule_redraw_task(struct window *window)
4336{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004337 if (!window->redraw_task_scheduled) {
4338 window->redraw_task.run = idle_redraw;
4339 display_defer(window->display, &window->redraw_task);
4340 window->redraw_task_scheduled = 1;
4341 }
4342}
4343
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004344void
4345window_schedule_redraw(struct window *window)
4346{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004347 struct surface *surface;
4348
Pekka Paalanen71233882013-04-25 13:57:53 +03004349 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4350
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004351 wl_list_for_each(surface, &window->subsurface_list, link)
4352 surface->redraw_needed = 1;
4353
4354 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004355}
4356
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004357int
4358window_is_fullscreen(struct window *window)
4359{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004360 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004361}
4362
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004363void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004364window_set_fullscreen(struct window *window, int fullscreen)
4365{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004366 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004367 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004368
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004369 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004370 return;
4371
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004372 if (fullscreen)
4373 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4374 else
4375 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004376}
4377
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004378int
4379window_is_maximized(struct window *window)
4380{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004381 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004382}
4383
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004384void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004385window_set_maximized(struct window *window, int maximized)
4386{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004387 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004388 return;
4389
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004390 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004391 return;
4392
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004393 if (maximized)
4394 xdg_surface_set_maximized(window->xdg_surface);
4395 else
4396 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004397}
4398
Jasper St. Pierrede680992014-04-10 17:23:49 -07004399int
4400window_is_resizing(struct window *window)
4401{
4402 return window->resizing;
4403}
4404
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004405void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004406window_set_minimized(struct window *window)
4407{
4408 if (!window->xdg_surface)
4409 return;
4410
4411 xdg_surface_set_minimized(window->xdg_surface);
4412}
4413
4414void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004415window_set_user_data(struct window *window, void *data)
4416{
4417 window->user_data = data;
4418}
4419
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004420void *
4421window_get_user_data(struct window *window)
4422{
4423 return window->user_data;
4424}
4425
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004426void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004427window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004428 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004429{
4430 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004431}
4432
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004433void
4434window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004435 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004436{
4437 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004438}
4439
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004440void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004441window_set_data_handler(struct window *window, window_data_handler_t handler)
4442{
4443 window->data_handler = handler;
4444}
4445
4446void
4447window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4448{
4449 window->drop_handler = handler;
4450}
4451
4452void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004453window_set_close_handler(struct window *window,
4454 window_close_handler_t handler)
4455{
4456 window->close_handler = handler;
4457}
4458
4459void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004460window_set_fullscreen_handler(struct window *window,
4461 window_fullscreen_handler_t handler)
4462{
4463 window->fullscreen_handler = handler;
4464}
4465
4466void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004467window_set_output_handler(struct window *window,
4468 window_output_handler_t handler)
4469{
4470 window->output_handler = handler;
4471}
4472
4473void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004474window_set_state_changed_handler(struct window *window,
4475 window_state_changed_handler_t handler)
4476{
4477 window->state_changed_handler = handler;
4478}
4479
4480void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004481window_set_title(struct window *window, const char *title)
4482{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004483 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004484 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004485 if (window->frame) {
4486 frame_set_title(window->frame->frame, title);
4487 widget_schedule_redraw(window->frame->widget);
4488 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004489 if (window->xdg_surface)
4490 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004491}
4492
4493const char *
4494window_get_title(struct window *window)
4495{
4496 return window->title;
4497}
4498
4499void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004500window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4501{
4502 struct text_cursor_position *text_cursor_position =
4503 window->display->text_cursor_position;
4504
Scott Moreau9295ce02012-06-01 12:46:10 -06004505 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004506 return;
4507
4508 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004509 window->main_surface->surface,
4510 wl_fixed_from_int(x),
4511 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004512}
4513
4514void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004515window_damage(struct window *window, int32_t x, int32_t y,
4516 int32_t width, int32_t height)
4517{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004518 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004519}
4520
Casey Dahlin9074db52012-04-19 22:50:09 -04004521static void
4522surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004523 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004524{
Rob Bradford7507b572012-05-15 17:55:34 +01004525 struct window *window = data;
4526 struct output *output;
4527 struct output *output_found = NULL;
4528 struct window_output *window_output;
4529
4530 wl_list_for_each(output, &window->display->output_list, link) {
4531 if (output->output == wl_output) {
4532 output_found = output;
4533 break;
4534 }
4535 }
4536
4537 if (!output_found)
4538 return;
4539
Brian Lovinbc919262013-08-07 15:34:59 -07004540 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004541 window_output->output = output_found;
4542
4543 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004544
4545 if (window->output_handler)
4546 window->output_handler(window, output_found, 1,
4547 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004548}
4549
4550static void
4551surface_leave(void *data,
4552 struct wl_surface *wl_surface, struct wl_output *output)
4553{
Rob Bradford7507b572012-05-15 17:55:34 +01004554 struct window *window = data;
4555 struct window_output *window_output;
4556 struct window_output *window_output_found = NULL;
4557
4558 wl_list_for_each(window_output, &window->window_output_list, link) {
4559 if (window_output->output->output == output) {
4560 window_output_found = window_output;
4561 break;
4562 }
4563 }
4564
4565 if (window_output_found) {
4566 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004567
4568 if (window->output_handler)
4569 window->output_handler(window, window_output->output,
4570 0, window->user_data);
4571
Rob Bradford7507b572012-05-15 17:55:34 +01004572 free(window_output_found);
4573 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004574}
4575
4576static const struct wl_surface_listener surface_listener = {
4577 surface_enter,
4578 surface_leave
4579};
4580
Pekka Paalanen4e373742013-02-13 16:17:13 +02004581static struct surface *
4582surface_create(struct window *window)
4583{
4584 struct display *display = window->display;
4585 struct surface *surface;
4586
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004587 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004588 surface->window = window;
4589 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004590 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004591 wl_surface_add_listener(surface->surface, &surface_listener, window);
4592
Pekka Paalanen35e82632013-04-25 13:57:48 +03004593 wl_list_insert(&window->subsurface_list, &surface->link);
4594
Pekka Paalanen4e373742013-02-13 16:17:13 +02004595 return surface;
4596}
4597
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004598static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004599get_preferred_buffer_type(struct display *display)
4600{
4601#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004602 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004603 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4604#endif
4605
4606 return WINDOW_BUFFER_TYPE_SHM;
4607}
4608
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004609static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004610window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004611{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004612 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004613 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004614
Peter Huttererf3d62272013-08-08 11:57:05 +10004615 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004616 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004617 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004618
4619 surface = surface_create(window);
4620 window->main_surface = surface;
4621
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004622 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004623
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004624 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004625 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004626
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004627 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004628
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004629 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004630 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004631 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004632
Rob Bradford7507b572012-05-15 17:55:34 +01004633 wl_list_init (&window->window_output_list);
4634
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004635 return window;
4636}
4637
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004638struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004639window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004640{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004641 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004642 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004643
4644 window = window_create_internal(display, 0);
4645
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004646 if (window->display->xdg_shell) {
4647 window->xdg_surface =
4648 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4649 window->main_surface->surface);
4650 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004651
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004652 xdg_surface_set_user_data(window->xdg_surface, window);
4653 xdg_surface_add_listener(window->xdg_surface,
4654 &xdg_surface_listener, window);
4655 } else if (display->ivi_application) {
4656 /* auto generation of ivi_id based on process id + basement of id */
4657 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4658 window->ivi_surface =
4659 ivi_application_surface_create(display->ivi_application,
4660 id_ivisurf, window->main_surface->surface);
4661 fail_on_null(window->ivi_surface);
4662
4663 ivi_surface_add_listener(window->ivi_surface,
4664 &ivi_surface_listener, window);
4665 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004666
4667 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004668}
4669
4670struct window *
4671window_create_custom(struct display *display)
4672{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004673 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004674}
4675
Jasper St. Pierre53686042013-12-09 15:26:25 -05004676void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004677window_set_parent(struct window *window,
4678 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004679{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004680 window->parent = parent_window;
4681 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004682}
4683
4684struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004685window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004686{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004687 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004688}
4689
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004690static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004691menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004692{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004693 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004694 int next;
4695
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004696 frame_interior(menu->frame, &x, &y, &width, &height);
4697 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004698 if (menu->current != next) {
4699 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004700 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004701 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004702}
4703
4704static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004705menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004706 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004707 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004708{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004709 struct menu *menu = data;
4710
4711 if (widget == menu->widget)
4712 menu_set_item(data, y);
4713
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004714 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004715}
4716
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004717static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004718menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004719 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004720{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004721 struct menu *menu = data;
4722
4723 if (widget == menu->widget)
4724 menu_set_item(data, y);
4725
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004726 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004727}
4728
4729static void
4730menu_leave_handler(struct widget *widget, struct input *input, void *data)
4731{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004732 struct menu *menu = data;
4733
4734 if (widget == menu->widget)
4735 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004736}
4737
4738static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004739menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004740 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004741 uint32_t button, enum wl_pointer_button_state state,
4742 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004743
4744{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004745 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004746
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004747 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4748 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004749 /* Either relase after press-drag-release or
4750 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004751 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05004752 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004753 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004754 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004755 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004756 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004757}
4758
4759static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004760menu_touch_up_handler(struct widget *widget,
4761 struct input *input,
4762 uint32_t serial,
4763 uint32_t time,
4764 int32_t id,
4765 void *data)
4766{
4767 struct menu *menu = data;
4768
4769 input_ungrab(input);
4770 menu_destroy(menu);
4771}
4772
4773static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004774menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004775{
4776 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004777 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004778 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004779
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004780 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004781
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004782 frame_repaint(menu->frame, cr);
4783 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004784
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004785 theme_set_background_source(menu->window->display->theme,
4786 cr, THEME_FRAME_ACTIVE);
4787 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004788 cairo_fill(cr);
4789
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004790 cairo_select_font_face(cr, "sans",
4791 CAIRO_FONT_SLANT_NORMAL,
4792 CAIRO_FONT_WEIGHT_NORMAL);
4793 cairo_set_font_size(cr, 12);
4794
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004795 for (i = 0; i < menu->count; i++) {
4796 if (i == menu->current) {
4797 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004798 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004799 cairo_fill(cr);
4800 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004801 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004802 cairo_show_text(cr, menu->entries[i]);
4803 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004804 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4805 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004806 cairo_show_text(cr, menu->entries[i]);
4807 }
4808 }
4809
4810 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004811}
4812
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004813static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004814handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004815{
4816 struct window *window = data;
4817 struct menu *menu = window->main_surface->widget->user_data;
4818
4819 input_ungrab(menu->input);
4820 menu_destroy(menu);
4821}
4822
4823static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004824 handle_popup_popup_done,
4825};
4826
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004827static struct menu *
4828create_menu(struct display *display,
4829 struct input *input, uint32_t time,
4830 menu_func_t func, const char **entries, int count,
4831 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004832{
4833 struct window *window;
4834 struct menu *menu;
4835
4836 menu = malloc(sizeof *menu);
4837 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004838 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004839
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004840 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004841 if (!window) {
4842 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004843 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004844 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004845
4846 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004847 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004848 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004849 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004850 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004851 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004852 menu->entries = entries;
4853 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004854 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004855 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004856 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004857 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004858 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004859
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004860 input_ungrab(input);
4861
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004862 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004863 widget_set_enter_handler(menu->widget, menu_enter_handler);
4864 widget_set_leave_handler(menu->widget, menu_leave_handler);
4865 widget_set_motion_handler(menu->widget, menu_motion_handler);
4866 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004867 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004868
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004869 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004870 frame_resize_inside(menu->frame, 200, count * 20);
4871 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4872 window_schedule_resize(window, frame_width(menu->frame),
4873 frame_height(menu->frame));
4874
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004875 return menu;
4876}
4877
4878struct window *
4879window_create_menu(struct display *display,
4880 struct input *input, uint32_t time,
4881 menu_func_t func, const char **entries, int count,
4882 void *user_data)
4883{
4884 struct menu *menu;
4885 menu = create_menu(display, input, time, func, entries, count, user_data);
4886
4887 if (menu == NULL)
4888 return NULL;
4889
4890 return menu->window;
4891}
4892
4893void
4894window_show_menu(struct display *display,
4895 struct input *input, uint32_t time, struct window *parent,
4896 int32_t x, int32_t y,
4897 menu_func_t func, const char **entries, int count)
4898{
4899 struct menu *menu;
4900 struct window *window;
4901 int32_t ix, iy;
4902
4903 menu = create_menu(display, input, time, func, entries, count, parent);
4904
4905 if (menu == NULL)
4906 return;
4907
4908 window = menu->window;
4909
4910 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4911 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4912
4913 window->x = x;
4914 window->y = y;
4915
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004916 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004917
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004918 if (!display->xdg_shell)
4919 return;
4920
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004921 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4922 window->main_surface->surface,
4923 parent->main_surface->surface,
4924 input->seat,
4925 display_get_serial(window->display),
4926 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004927 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004928 fail_on_null(window->xdg_popup);
4929
4930 xdg_popup_set_user_data(window->xdg_popup, window);
4931 xdg_popup_add_listener(window->xdg_popup,
4932 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004933}
4934
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004935void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004936window_set_buffer_type(struct window *window, enum window_buffer_type type)
4937{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004938 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004939}
4940
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004941enum window_buffer_type
4942window_get_buffer_type(struct window *window)
4943{
4944 return window->main_surface->buffer_type;
4945}
4946
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004947void
4948window_set_preferred_format(struct window *window,
4949 enum preferred_format format)
4950{
4951 window->preferred_format = format;
4952}
4953
Pekka Paalanen35e82632013-04-25 13:57:48 +03004954struct widget *
4955window_add_subsurface(struct window *window, void *data,
4956 enum subsurface_mode default_mode)
4957{
4958 struct widget *widget;
4959 struct surface *surface;
4960 struct wl_surface *parent;
4961 struct wl_subcompositor *subcompo = window->display->subcompositor;
4962
Pekka Paalanen35e82632013-04-25 13:57:48 +03004963 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004964 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004965 widget = widget_create(window, surface, data);
4966 wl_list_init(&widget->link);
4967 surface->widget = widget;
4968
4969 parent = window->main_surface->surface;
4970 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4971 surface->surface,
4972 parent);
4973 surface->synchronized = 1;
4974
4975 switch (default_mode) {
4976 case SUBSURFACE_SYNCHRONIZED:
4977 surface->synchronized_default = 1;
4978 break;
4979 case SUBSURFACE_DESYNCHRONIZED:
4980 surface->synchronized_default = 0;
4981 break;
4982 default:
4983 assert(!"bad enum subsurface_mode");
4984 }
4985
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004986 window->resize_needed = 1;
4987 window_schedule_redraw(window);
4988
Pekka Paalanen35e82632013-04-25 13:57:48 +03004989 return widget;
4990}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004991
4992static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004993display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004994 struct wl_output *wl_output,
4995 int x, int y,
4996 int physical_width,
4997 int physical_height,
4998 int subpixel,
4999 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005000 const char *model,
5001 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005002{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005003 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005004
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005005 output->allocation.x = x;
5006 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005007 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005008
5009 if (output->make)
5010 free(output->make);
5011 output->make = strdup(make);
5012
5013 if (output->model)
5014 free(output->model);
5015 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005016}
5017
5018static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005019display_handle_done(void *data,
5020 struct wl_output *wl_output)
5021{
5022}
5023
5024static void
5025display_handle_scale(void *data,
5026 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005027 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005028{
5029 struct output *output = data;
5030
5031 output->scale = scale;
5032}
5033
5034static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005035display_handle_mode(void *data,
5036 struct wl_output *wl_output,
5037 uint32_t flags,
5038 int width,
5039 int height,
5040 int refresh)
5041{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005042 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005043 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005044
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005045 if (flags & WL_OUTPUT_MODE_CURRENT) {
5046 output->allocation.width = width;
5047 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005048 if (display->output_configure_handler)
5049 (*display->output_configure_handler)(
5050 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005051 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005052}
5053
5054static const struct wl_output_listener output_listener = {
5055 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005056 display_handle_mode,
5057 display_handle_done,
5058 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005059};
5060
5061static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005062display_add_output(struct display *d, uint32_t id)
5063{
5064 struct output *output;
5065
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005066 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005067 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005068 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005069 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005070 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005071 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005072 wl_list_insert(d->output_list.prev, &output->link);
5073
5074 wl_output_add_listener(output->output, &output_listener, output);
5075}
5076
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005077static void
5078output_destroy(struct output *output)
5079{
5080 if (output->destroy_handler)
5081 (*output->destroy_handler)(output, output->user_data);
5082
5083 wl_output_destroy(output->output);
5084 wl_list_remove(&output->link);
5085 free(output);
5086}
5087
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005088static void
5089display_destroy_output(struct display *d, uint32_t id)
5090{
5091 struct output *output;
5092
5093 wl_list_for_each(output, &d->output_list, link) {
5094 if (output->server_output_id == id) {
5095 output_destroy(output);
5096 break;
5097 }
5098 }
5099}
5100
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005101void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005102display_set_global_handler(struct display *display,
5103 display_global_handler_t handler)
5104{
5105 struct global *global;
5106
5107 display->global_handler = handler;
5108 if (!handler)
5109 return;
5110
5111 wl_list_for_each(global, &display->global_list, link)
5112 display->global_handler(display,
5113 global->name, global->interface,
5114 global->version, display->user_data);
5115}
5116
5117void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005118display_set_global_handler_remove(struct display *display,
5119 display_global_handler_t remove_handler)
5120{
5121 display->global_handler_remove = remove_handler;
5122 if (!remove_handler)
5123 return;
5124}
5125
5126void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005127display_set_output_configure_handler(struct display *display,
5128 display_output_handler_t handler)
5129{
5130 struct output *output;
5131
5132 display->output_configure_handler = handler;
5133 if (!handler)
5134 return;
5135
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005136 wl_list_for_each(output, &display->output_list, link) {
5137 if (output->allocation.width == 0 &&
5138 output->allocation.height == 0)
5139 continue;
5140
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005141 (*display->output_configure_handler)(output,
5142 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005143 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005144}
5145
5146void
5147output_set_user_data(struct output *output, void *data)
5148{
5149 output->user_data = data;
5150}
5151
5152void *
5153output_get_user_data(struct output *output)
5154{
5155 return output->user_data;
5156}
5157
5158void
5159output_set_destroy_handler(struct output *output,
5160 display_output_handler_t handler)
5161{
5162 output->destroy_handler = handler;
5163 /* FIXME: implement this, once we have way to remove outputs */
5164}
5165
5166void
Scott Moreau4e072362012-09-29 02:03:11 -06005167output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005168{
Scott Moreau4e072362012-09-29 02:03:11 -06005169 struct rectangle allocation = output->allocation;
5170
5171 switch (output->transform) {
5172 case WL_OUTPUT_TRANSFORM_90:
5173 case WL_OUTPUT_TRANSFORM_270:
5174 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5175 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5176 /* Swap width and height */
5177 allocation.width = output->allocation.height;
5178 allocation.height = output->allocation.width;
5179 break;
5180 }
5181
5182 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005183}
5184
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005185struct wl_output *
5186output_get_wl_output(struct output *output)
5187{
5188 return output->output;
5189}
5190
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005191enum wl_output_transform
5192output_get_transform(struct output *output)
5193{
5194 return output->transform;
5195}
5196
Alexander Larssonafd319a2013-05-22 14:41:27 +02005197uint32_t
5198output_get_scale(struct output *output)
5199{
5200 return output->scale;
5201}
5202
Jason Ekstrand738715d2014-04-02 19:53:50 -05005203const char *
5204output_get_make(struct output *output)
5205{
5206 return output->make;
5207}
5208
5209const char *
5210output_get_model(struct output *output)
5211{
5212 return output->model;
5213}
5214
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005215static void
Daniel Stone97f68542012-05-30 16:32:01 +01005216fini_xkb(struct input *input)
5217{
5218 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005219 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005220}
5221
5222static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005223display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005224{
5225 struct input *input;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005226 int seat_version = MIN(display_seat_version, 4);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005227
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005228 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005229 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005230 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005231 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005232 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005233 input->pointer_focus = NULL;
5234 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005235 input->seat_version = seat_version;
5236
Rusty Lynch041815a2013-08-08 21:20:38 -07005237 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005238 wl_list_insert(d->input_list.prev, &input->link);
5239
Daniel Stone37816df2012-05-16 18:45:18 +01005240 wl_seat_add_listener(input->seat, &seat_listener, input);
5241 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005242
Jason Ekstranda669bd52014-04-02 19:53:51 -05005243 if (d->data_device_manager) {
5244 input->data_device =
5245 wl_data_device_manager_get_data_device(d->data_device_manager,
5246 input->seat);
5247 wl_data_device_add_listener(input->data_device,
5248 &data_device_listener,
5249 input);
5250 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005251
5252 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005253 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005254
Derek Foreman118a4292015-04-22 17:23:35 -05005255 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5256 TFD_CLOEXEC | TFD_NONBLOCK);
5257 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5258 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005259 set_repeat_info(input, 40, 400);
5260
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005261 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5262 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005263 input->repeat_task.run = keyboard_repeat_func;
5264 display_watch_fd(d, input->repeat_timer_fd,
5265 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005266}
5267
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005268static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005269input_destroy(struct input *input)
5270{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005271 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005272 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005273
5274 if (input->drag_offer)
5275 data_offer_destroy(input->drag_offer);
5276
5277 if (input->selection_offer)
5278 data_offer_destroy(input->selection_offer);
5279
kabeer khan6ce67ec2014-10-20 11:55:29 +05305280 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005281 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305282 wl_data_device_release(input->data_device);
5283 else
5284 wl_data_device_destroy(input->data_device);
5285 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005286 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005287 if (input->touch)
5288 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005289 if (input->pointer)
5290 wl_pointer_release(input->pointer);
5291 if (input->keyboard)
5292 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005293 } else {
5294 if (input->touch)
5295 wl_touch_destroy(input->touch);
5296 if (input->pointer)
5297 wl_pointer_destroy(input->pointer);
5298 if (input->keyboard)
5299 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005300 }
5301
Daniel Stone97f68542012-05-30 16:32:01 +01005302 fini_xkb(input);
5303
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005304 wl_surface_destroy(input->pointer_surface);
5305
Pekka Paalanene1207c72011-12-16 12:02:09 +02005306 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005307 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005308 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005309 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005310 free(input);
5311}
5312
5313static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005314init_workspace_manager(struct display *d, uint32_t id)
5315{
5316 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005317 wl_registry_bind(d->registry, id,
5318 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005319 if (d->workspace_manager != NULL)
5320 workspace_manager_add_listener(d->workspace_manager,
5321 &workspace_manager_listener,
5322 d);
5323}
5324
5325static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005326shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5327{
5328 struct display *d = data;
5329
5330 if (format == WL_SHM_FORMAT_RGB565)
5331 d->has_rgb565 = 1;
5332}
5333
5334struct wl_shm_listener shm_listener = {
5335 shm_format
5336};
5337
5338static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005339xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5340{
5341 xdg_shell_pong(shell, serial);
5342}
5343
5344static const struct xdg_shell_listener xdg_shell_listener = {
5345 xdg_shell_ping,
5346};
5347
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005348#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005349#ifdef static_assert
5350static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5351 "Interface version doesn't match implementation version");
5352#endif
5353
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005354static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005355registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5356 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005357{
5358 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005359 struct global *global;
5360
Brian Lovinbc919262013-08-07 15:34:59 -07005361 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005362 global->name = id;
5363 global->interface = strdup(interface);
5364 global->version = version;
5365 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005366
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005367 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005368 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005369 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005370 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005371 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005372 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005373 display_add_input(d, id, version);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005374 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005375 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005376 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005377 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305378 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005379 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005380 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305381 &wl_data_device_manager_interface,
5382 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005383 } else if (strcmp(interface, "xdg_shell") == 0) {
5384 d->xdg_shell = wl_registry_bind(registry, id,
5385 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005386 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005387 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005388 } else if (strcmp(interface, "text_cursor_position") == 0) {
5389 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005390 wl_registry_bind(registry, id,
5391 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005392 } else if (strcmp(interface, "workspace_manager") == 0) {
5393 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005394 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5395 d->subcompositor =
5396 wl_registry_bind(registry, id,
5397 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005398 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005399 else if (strcmp(interface, "ivi_application") == 0) {
5400 d->ivi_application =
5401 wl_registry_bind(registry, id,
5402 &ivi_application_interface, 1);
5403 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005404
5405 if (d->global_handler)
5406 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005407}
5408
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005409static void
5410registry_handle_global_remove(void *data, struct wl_registry *registry,
5411 uint32_t name)
5412{
5413 struct display *d = data;
5414 struct global *global;
5415 struct global *tmp;
5416
5417 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5418 if (global->name != name)
5419 continue;
5420
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005421 if (strcmp(global->interface, "wl_output") == 0)
5422 display_destroy_output(d, name);
5423
5424 /* XXX: Should destroy remaining bound globals */
5425
5426 if (d->global_handler_remove)
5427 d->global_handler_remove(d, name, global->interface,
5428 global->version, d->user_data);
5429
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005430 wl_list_remove(&global->link);
5431 free(global->interface);
5432 free(global);
5433 }
5434}
5435
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005436void *
5437display_bind(struct display *display, uint32_t name,
5438 const struct wl_interface *interface, uint32_t version)
5439{
5440 return wl_registry_bind(display->registry, name, interface, version);
5441}
5442
5443static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005444 registry_handle_global,
5445 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005446};
5447
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005448#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005449static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005450init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005451{
5452 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005453 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005454
Rob Clark6396ed32012-03-11 19:48:41 -05005455#ifdef USE_CAIRO_GLESV2
5456# define GL_BIT EGL_OPENGL_ES2_BIT
5457#else
5458# define GL_BIT EGL_OPENGL_BIT
5459#endif
5460
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005461 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005462 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005463 EGL_RED_SIZE, 1,
5464 EGL_GREEN_SIZE, 1,
5465 EGL_BLUE_SIZE, 1,
5466 EGL_ALPHA_SIZE, 1,
5467 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005468 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005469 EGL_NONE
5470 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005471
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005472#ifdef USE_CAIRO_GLESV2
5473 static const EGLint context_attribs[] = {
5474 EGL_CONTEXT_CLIENT_VERSION, 2,
5475 EGL_NONE
5476 };
5477 EGLint api = EGL_OPENGL_ES_API;
5478#else
5479 EGLint *context_attribs = NULL;
5480 EGLint api = EGL_OPENGL_API;
5481#endif
5482
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005483 d->dpy =
5484 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5485 d->display, NULL);
5486
Yuval Fledel45568f62010-12-06 09:18:12 -05005487 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005488 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005489 return -1;
5490 }
5491
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005492 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005493 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005494 return -1;
5495 }
5496
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005497 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5498 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005499 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005500 return -1;
5501 }
5502
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005503 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005504 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005505 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005506 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005507 return -1;
5508 }
5509
Benjamin Franzke0c991632011-09-27 21:57:31 +02005510 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5511 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005512 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005513 return -1;
5514 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005515
5516 return 0;
5517}
5518
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005519static void
5520fini_egl(struct display *display)
5521{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005522 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005523
5524 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5525 EGL_NO_CONTEXT);
5526
5527 eglTerminate(display->dpy);
5528 eglReleaseThread();
5529}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005530#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005531
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005532static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005533init_dummy_surface(struct display *display)
5534{
5535 int len;
5536 void *data;
5537
5538 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005539 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005540 display->dummy_surface =
5541 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5542 1, 1, len);
5543 display->dummy_surface_data = data;
5544}
5545
5546static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005547handle_display_data(struct task *task, uint32_t events)
5548{
5549 struct display *display =
5550 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005551 struct epoll_event ep;
5552 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005553
5554 display->display_fd_events = events;
5555
5556 if (events & EPOLLERR || events & EPOLLHUP) {
5557 display_exit(display);
5558 return;
5559 }
5560
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005561 if (events & EPOLLIN) {
5562 ret = wl_display_dispatch(display->display);
5563 if (ret == -1) {
5564 display_exit(display);
5565 return;
5566 }
5567 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005568
5569 if (events & EPOLLOUT) {
5570 ret = wl_display_flush(display->display);
5571 if (ret == 0) {
5572 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5573 ep.data.ptr = &display->display_task;
5574 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5575 display->display_fd, &ep);
5576 } else if (ret == -1 && errno != EAGAIN) {
5577 display_exit(display);
5578 return;
5579 }
5580 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005581}
5582
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005583static void
5584log_handler(const char *format, va_list args)
5585{
5586 vfprintf(stderr, format, args);
5587}
5588
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005589struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005590display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005591{
5592 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005593
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005594 wl_log_set_handler_client(log_handler);
5595
Peter Huttererf3d62272013-08-08 11:57:05 +10005596 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005597 if (d == NULL)
5598 return NULL;
5599
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005600 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005601 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005602 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005603 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005604 return NULL;
5605 }
5606
Rob Bradford5ab9c752013-07-26 16:29:43 +01005607 d->xkb_context = xkb_context_new(0);
5608 if (d->xkb_context == NULL) {
5609 fprintf(stderr, "Failed to create XKB context\n");
5610 free(d);
5611 return NULL;
5612 }
5613
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005614 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005615 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005616 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005617 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5618 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005619
5620 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005621 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005622 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005623 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005624
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005625 d->workspace = 0;
5626 d->workspace_count = 1;
5627
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005628 d->registry = wl_display_get_registry(d->display);
5629 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005630
Marek Chalupaa519d062014-12-05 13:49:40 +01005631 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005632 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5633 return NULL;
5634 }
5635
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005636#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005637 if (init_egl(d) < 0)
5638 fprintf(stderr, "EGL does not seem to work, "
5639 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005640#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005641
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005642 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005643
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005644 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005645
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005646 wl_list_init(&d->window_list);
5647
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005648 init_dummy_surface(d);
5649
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005650 return d;
5651}
5652
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005653static void
5654display_destroy_outputs(struct display *display)
5655{
5656 struct output *tmp;
5657 struct output *output;
5658
5659 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5660 output_destroy(output);
5661}
5662
Pekka Paalanene1207c72011-12-16 12:02:09 +02005663static void
5664display_destroy_inputs(struct display *display)
5665{
5666 struct input *tmp;
5667 struct input *input;
5668
5669 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5670 input_destroy(input);
5671}
5672
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005673void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005674display_destroy(struct display *display)
5675{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005676 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005677 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5678 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005679
5680 if (!wl_list_empty(&display->deferred_list))
5681 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5682
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005683 cairo_surface_destroy(display->dummy_surface);
5684 free(display->dummy_surface_data);
5685
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005686 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005687 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005688
Daniel Stone97f68542012-05-30 16:32:01 +01005689 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005690
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005691 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005692 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005693
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005694#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005695 if (display->argb_device)
5696 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005697#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005698
Pekka Paalanen35e82632013-04-25 13:57:48 +03005699 if (display->subcompositor)
5700 wl_subcompositor_destroy(display->subcompositor);
5701
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005702 if (display->xdg_shell)
5703 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005704
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005705 if (display->ivi_application)
5706 ivi_application_destroy(display->ivi_application);
5707
Pekka Paalanenc2052982011-12-16 11:41:32 +02005708 if (display->shm)
5709 wl_shm_destroy(display->shm);
5710
5711 if (display->data_device_manager)
5712 wl_data_device_manager_destroy(display->data_device_manager);
5713
5714 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005715 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005716
5717 close(display->epoll_fd);
5718
U. Artie Eoff44874d92012-10-02 21:12:35 -07005719 if (!(display->display_fd_events & EPOLLERR) &&
5720 !(display->display_fd_events & EPOLLHUP))
5721 wl_display_flush(display->display);
5722
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005723 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005724 free(display);
5725}
5726
5727void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005728display_set_user_data(struct display *display, void *data)
5729{
5730 display->user_data = data;
5731}
5732
5733void *
5734display_get_user_data(struct display *display)
5735{
5736 return display->user_data;
5737}
5738
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005739struct wl_display *
5740display_get_display(struct display *display)
5741{
5742 return display->display;
5743}
5744
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005745int
5746display_has_subcompositor(struct display *display)
5747{
5748 if (display->subcompositor)
5749 return 1;
5750
5751 wl_display_roundtrip(display->display);
5752
5753 return display->subcompositor != NULL;
5754}
5755
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005756cairo_device_t *
5757display_get_cairo_device(struct display *display)
5758{
5759 return display->argb_device;
5760}
5761
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005762struct output *
5763display_get_output(struct display *display)
5764{
5765 return container_of(display->output_list.next, struct output, link);
5766}
5767
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005768struct wl_compositor *
5769display_get_compositor(struct display *display)
5770{
5771 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005772}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005773
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005774uint32_t
5775display_get_serial(struct display *display)
5776{
5777 return display->serial;
5778}
5779
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005780EGLDisplay
5781display_get_egl_display(struct display *d)
5782{
5783 return d->dpy;
5784}
5785
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005786struct wl_data_source *
5787display_create_data_source(struct display *display)
5788{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005789 if (display->data_device_manager)
5790 return wl_data_device_manager_create_data_source(display->data_device_manager);
5791 else
5792 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005793}
5794
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005795EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005796display_get_argb_egl_config(struct display *d)
5797{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005798 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005799}
5800
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005801int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005802display_acquire_window_surface(struct display *display,
5803 struct window *window,
5804 EGLContext ctx)
5805{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005806 struct surface *surface = window->main_surface;
5807
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005808 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005809 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005810
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005811 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005812 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005813}
5814
5815void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005816display_release_window_surface(struct display *display,
5817 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005818{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005819 struct surface *surface = window->main_surface;
5820
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005821 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005822 return;
5823
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005824 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005825}
5826
5827void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005828display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005829{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005830 wl_list_insert(&display->deferred_list, &task->link);
5831}
5832
5833void
5834display_watch_fd(struct display *display,
5835 int fd, uint32_t events, struct task *task)
5836{
5837 struct epoll_event ep;
5838
5839 ep.events = events;
5840 ep.data.ptr = task;
5841 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5842}
5843
5844void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005845display_unwatch_fd(struct display *display, int fd)
5846{
5847 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5848}
5849
5850void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005851display_run(struct display *display)
5852{
5853 struct task *task;
5854 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005855 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005856
Pekka Paalanen826d7952011-12-15 10:14:07 +02005857 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005858 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005859 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005860 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005861 struct task, link);
5862 wl_list_remove(&task->link);
5863 task->run(task, 0);
5864 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005865
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005866 wl_display_dispatch_pending(display->display);
5867
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005868 if (!display->running)
5869 break;
5870
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005871 ret = wl_display_flush(display->display);
5872 if (ret < 0 && errno == EAGAIN) {
5873 ep[0].events =
5874 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5875 ep[0].data.ptr = &display->display_task;
5876
5877 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5878 display->display_fd, &ep[0]);
5879 } else if (ret < 0) {
5880 break;
5881 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005882
5883 count = epoll_wait(display->epoll_fd,
5884 ep, ARRAY_LENGTH(ep), -1);
5885 for (i = 0; i < count; i++) {
5886 task = ep[i].data.ptr;
5887 task->run(task, ep[i].events);
5888 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005889 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005890}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005891
5892void
5893display_exit(struct display *display)
5894{
5895 display->running = 0;
5896}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005897
5898void
5899keysym_modifiers_add(struct wl_array *modifiers_map,
5900 const char *name)
5901{
5902 size_t len = strlen(name) + 1;
5903 char *p;
5904
5905 p = wl_array_add(modifiers_map, len);
5906
5907 if (p == NULL)
5908 return;
5909
5910 strncpy(p, name, len);
5911}
5912
5913static xkb_mod_index_t
5914keysym_modifiers_get_index(struct wl_array *modifiers_map,
5915 const char *name)
5916{
5917 xkb_mod_index_t index = 0;
5918 char *p = modifiers_map->data;
5919
5920 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5921 if (strcmp(p, name) == 0)
5922 return index;
5923
5924 index++;
5925 p += strlen(p) + 1;
5926 }
5927
5928 return XKB_MOD_INVALID;
5929}
5930
5931xkb_mod_mask_t
5932keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5933 const char *name)
5934{
5935 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5936
5937 if (index == XKB_MOD_INVALID)
5938 return XKB_MOD_INVALID;
5939
5940 return 1 << index;
5941}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005942
5943void *
5944fail_on_null(void *p)
5945{
5946 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005947 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005948 exit(EXIT_FAILURE);
5949 }
5950
5951 return p;
5952}
5953
5954void *
5955xmalloc(size_t s)
5956{
5957 return fail_on_null(malloc(s));
5958}
5959
Peter Huttererf3d62272013-08-08 11:57:05 +10005960void *
5961xzalloc(size_t s)
5962{
5963 return fail_on_null(zalloc(s));
5964}
5965
Kristian Høgsbergce278412013-07-25 15:20:20 -07005966char *
5967xstrdup(const char *s)
5968{
5969 return fail_on_null(strdup(s));
5970}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005971
5972void *
5973xrealloc(char *p, size_t s)
5974{
5975 return fail_on_null(realloc(p, s));
5976}