blob: f94be4625ab737ed3d98ab5abf5d4981de03bc03 [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>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040069#include "../shared/cairo-util.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050070#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060071#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020072#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030073#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050074
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050075#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050076
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090077#include <sys/types.h>
78#include "ivi-application-client-protocol.h"
79#define IVI_SURFACE_ID 9000
80
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070081struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030082
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040083struct global {
84 uint32_t name;
85 char *interface;
86 uint32_t version;
87 struct wl_list link;
88};
89
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050090struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050091 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040092 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050093 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030094 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040095 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040096 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060097 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020098 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050099 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900100 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400101 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500102 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200103 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200104 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400105 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400106
107 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700108 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400109 struct task display_task;
110
111 int epoll_fd;
112 struct wl_list deferred_list;
113
Pekka Paalanen826d7952011-12-15 10:14:07 +0200114 int running;
115
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400116 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400117 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400118 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500119 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400120
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400121 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400122
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300123 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300124 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400125
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200126 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400127 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800128 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200129
130 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100131
132 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200133
134 uint32_t workspace;
135 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200136
137 /* A hack to get text extents for tooltips */
138 cairo_surface_t *dummy_surface;
139 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200140
141 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100142 int seat_version;
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;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300321 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400322 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400323 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400324 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400325 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400326 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400327
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500328 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500329 struct widget *grab;
330 uint32_t grab_button;
331
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400332 struct wl_data_device *data_device;
333 struct data_offer *drag_offer;
334 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800335 uint32_t touch_grab;
336 int32_t touch_grab_id;
337 float drag_x, drag_y;
338 struct window *drag_focus;
339 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100340
341 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100342 struct xkb_keymap *keymap;
343 struct xkb_state *state;
344 xkb_mod_mask_t control_mask;
345 xkb_mod_mask_t alt_mask;
346 xkb_mod_mask_t shift_mask;
347 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400348
Jonny Lamb06959082014-08-12 14:58:27 +0200349 int32_t repeat_rate_sec;
350 int32_t repeat_rate_nsec;
351 int32_t repeat_delay_sec;
352 int32_t repeat_delay_nsec;
353
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400354 struct task repeat_task;
355 int repeat_timer_fd;
356 uint32_t repeat_sym;
357 uint32_t repeat_key;
358 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400359};
360
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500361struct output {
362 struct display *display;
363 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800364 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500365 struct rectangle allocation;
366 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600367 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200368 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500369 char *make;
370 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200371
372 display_output_handler_t destroy_handler;
373 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500374};
375
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500376struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500377 struct widget *widget;
378 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500379 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800380
381 uint32_t last_time;
382 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800383 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500384};
385
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500386struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400387 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500388 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500389 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500390 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700391 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500392 const char **entries;
393 uint32_t time;
394 int current;
395 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400396 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500397 menu_func_t func;
398};
399
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300400struct tooltip {
401 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300402 struct widget *widget;
403 char *entry;
404 struct task tooltip_task;
405 int tooltip_fd;
406 float x, y;
407};
408
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700409struct shm_pool {
410 struct wl_shm_pool *pool;
411 size_t size;
412 size_t used;
413 void *data;
414};
415
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400416enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300417 CURSOR_DEFAULT = 100,
418 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400419};
420
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200421static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422
Pekka Paalanen97777442013-05-22 10:20:05 +0300423/* #define DEBUG */
424
425#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300426
427static void
428debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
429__attribute__ ((format (printf, 4, 5)));
430
431static void
432debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
433{
434 va_list ap;
435 struct timeval tv;
436
437 gettimeofday(&tv, NULL);
438 fprintf(stderr, "%8ld.%03ld ",
439 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
440
441 if (proxy)
442 fprintf(stderr, "%s@%d ",
443 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
444
445 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
446 fprintf(stderr, "%s ", func);
447
448 va_start(ap, fmt);
449 vfprintf(stderr, fmt, ap);
450 va_end(ap);
451}
452
453#define DBG(fmt, ...) \
454 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
455
456#define DBG_OBJ(obj, fmt, ...) \
457 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
458
459#else
460
461#define DBG(...) do {} while (0)
462#define DBG_OBJ(...) do {} while (0)
463
464#endif
465
Alexander Larsson1818e312013-05-22 14:41:31 +0200466static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200467surface_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 +0200468{
469 int32_t tmp;
470
471 switch (buffer_transform) {
472 case WL_OUTPUT_TRANSFORM_90:
473 case WL_OUTPUT_TRANSFORM_270:
474 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
476 tmp = *width;
477 *width = *height;
478 *height = tmp;
479 break;
480 default:
481 break;
482 }
483
484 *width *= buffer_scale;
485 *height *= buffer_scale;
486}
487
488static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200489buffer_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 +0200490{
491 int32_t tmp;
492
493 switch (buffer_transform) {
494 case WL_OUTPUT_TRANSFORM_90:
495 case WL_OUTPUT_TRANSFORM_270:
496 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
497 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
498 tmp = *width;
499 *width = *height;
500 *height = tmp;
501 break;
502 default:
503 break;
504 }
505
506 *width /= buffer_scale;
507 *height /= buffer_scale;
508}
509
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500510#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400511
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200512struct egl_window_surface {
513 struct toysurface base;
514 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100515 struct display *display;
516 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517 struct wl_egl_window *egl_window;
518 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100519};
520
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521static struct egl_window_surface *
522to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100523{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200524 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525}
526
527static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200528egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200529 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200530 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100531{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200532 struct egl_window_surface *surface = to_egl_window_surface(base);
533
Alexander Larsson1818e312013-05-22 14:41:31 +0200534 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
535
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200536 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
537 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
538
539 return cairo_surface_reference(surface->cairo_surface);
540}
541
542static void
543egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200544 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200545 struct rectangle *server_allocation)
546{
547 struct egl_window_surface *surface = to_egl_window_surface(base);
548
549 cairo_gl_surface_swapbuffers(surface->cairo_surface);
550 wl_egl_window_get_attached_size(surface->egl_window,
551 &server_allocation->width,
552 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200553
554 buffer_to_surface_size (buffer_transform, buffer_scale,
555 &server_allocation->width,
556 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200557}
558
559static int
560egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
561{
562 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200563 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100564
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200565 device = cairo_surface_get_device(surface->cairo_surface);
566 if (!device)
567 return -1;
568
569 if (!ctx) {
570 if (device == surface->display->argb_device)
571 ctx = surface->display->argb_ctx;
572 else
573 assert(0);
574 }
575
576 cairo_device_flush(device);
577 cairo_device_acquire(device);
578 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
579 surface->egl_surface, ctx))
580 fprintf(stderr, "failed to make surface current\n");
581
582 return 0;
583}
584
585static void
586egl_window_surface_release(struct toysurface *base)
587{
588 struct egl_window_surface *surface = to_egl_window_surface(base);
589 cairo_device_t *device;
590
591 device = cairo_surface_get_device(surface->cairo_surface);
592 if (!device)
593 return;
594
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200595 if (!eglMakeCurrent(surface->display->dpy,
596 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200597 fprintf(stderr, "failed to make context current\n");
598
599 cairo_device_release(device);
600}
601
602static void
603egl_window_surface_destroy(struct toysurface *base)
604{
605 struct egl_window_surface *surface = to_egl_window_surface(base);
606 struct display *d = surface->display;
607
608 cairo_surface_destroy(surface->cairo_surface);
609 eglDestroySurface(d->dpy, surface->egl_surface);
610 wl_egl_window_destroy(surface->egl_window);
611 surface->surface = NULL;
612
613 free(surface);
614}
615
616static struct toysurface *
617egl_window_surface_create(struct display *display,
618 struct wl_surface *wl_surface,
619 uint32_t flags,
620 struct rectangle *rectangle)
621{
622 struct egl_window_surface *surface;
623
Pekka Paalanenb3627362012-11-19 17:16:00 +0200624 if (display->dpy == EGL_NO_DISPLAY)
625 return NULL;
626
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200627 surface = calloc(1, sizeof *surface);
628 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100629 return NULL;
630
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200631 surface->base.prepare = egl_window_surface_prepare;
632 surface->base.swap = egl_window_surface_swap;
633 surface->base.acquire = egl_window_surface_acquire;
634 surface->base.release = egl_window_surface_release;
635 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100636
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200637 surface->display = display;
638 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400639
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200640 surface->egl_window = wl_egl_window_create(surface->surface,
641 rectangle->width,
642 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100644 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100645 weston_platform_create_egl_surface(display->dpy,
646 display->argb_config,
647 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200649 surface->cairo_surface =
650 cairo_gl_surface_create_for_egl(display->argb_device,
651 surface->egl_surface,
652 rectangle->width,
653 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100654
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200655 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100656}
657
Pekka Paalanenb3627362012-11-19 17:16:00 +0200658#else
659
660static struct toysurface *
661egl_window_surface_create(struct display *display,
662 struct wl_surface *wl_surface,
663 uint32_t flags,
664 struct rectangle *rectangle)
665{
666 return NULL;
667}
668
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669#endif
670
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200671struct shm_surface_data {
672 struct wl_buffer *buffer;
673 struct shm_pool *pool;
674};
675
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400676struct wl_buffer *
677display_get_buffer_for_surface(struct display *display,
678 cairo_surface_t *surface)
679{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200680 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200682 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400683
684 return data->buffer;
685}
686
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500687static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700688shm_pool_destroy(struct shm_pool *pool);
689
690static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400691shm_surface_data_destroy(void *p)
692{
693 struct shm_surface_data *data = p;
694
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200695 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700696 if (data->pool)
697 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300698
699 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400700}
701
Kristian Høgsberg16626282012-04-03 11:21:27 -0400702static struct wl_shm_pool *
703make_shm_pool(struct display *display, int size, void **data)
704{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400705 struct wl_shm_pool *pool;
706 int fd;
707
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300708 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400709 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300710 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
711 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400712 return NULL;
713 }
714
715 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400716 if (*data == MAP_FAILED) {
717 fprintf(stderr, "mmap failed: %m\n");
718 close(fd);
719 return NULL;
720 }
721
722 pool = wl_shm_create_pool(display->shm, fd, size);
723
724 close(fd);
725
726 return pool;
727}
728
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700729static struct shm_pool *
730shm_pool_create(struct display *display, size_t size)
731{
732 struct shm_pool *pool = malloc(sizeof *pool);
733
734 if (!pool)
735 return NULL;
736
737 pool->pool = make_shm_pool(display, size, &pool->data);
738 if (!pool->pool) {
739 free(pool);
740 return NULL;
741 }
742
743 pool->size = size;
744 pool->used = 0;
745
746 return pool;
747}
748
749static void *
750shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
751{
752 if (pool->used + size > pool->size)
753 return NULL;
754
755 *offset = pool->used;
756 pool->used += size;
757
758 return (char *) pool->data + *offset;
759}
760
761/* destroy the pool. this does not unmap the memory though */
762static void
763shm_pool_destroy(struct shm_pool *pool)
764{
765 munmap(pool->data, pool->size);
766 wl_shm_pool_destroy(pool->pool);
767 free(pool);
768}
769
770/* Start allocating from the beginning of the pool again */
771static void
772shm_pool_reset(struct shm_pool *pool)
773{
774 pool->used = 0;
775}
776
777static int
778data_length_for_shm_surface(struct rectangle *rect)
779{
780 int stride;
781
782 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
783 rect->width);
784 return stride * rect->height;
785}
786
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500787static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700788display_create_shm_surface_from_pool(struct display *display,
789 struct rectangle *rectangle,
790 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400791{
792 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400793 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400794 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200795 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700796 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400797 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400798
799 data = malloc(sizeof *data);
800 if (data == NULL)
801 return NULL;
802
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200803 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
804 cairo_format = CAIRO_FORMAT_RGB16_565;
805 else
806 cairo_format = CAIRO_FORMAT_ARGB32;
807
808 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700809 length = stride * rectangle->height;
810 data->pool = NULL;
811 map = shm_pool_allocate(pool, length, &offset);
812
813 if (!map) {
814 free(data);
815 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400816 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400817
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400818 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200819 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400820 rectangle->width,
821 rectangle->height,
822 stride);
823
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200824 cairo_surface_set_user_data(surface, &shm_surface_data_key,
825 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200827 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
828 format = WL_SHM_FORMAT_RGB565;
829 else {
830 if (flags & SURFACE_OPAQUE)
831 format = WL_SHM_FORMAT_XRGB8888;
832 else
833 format = WL_SHM_FORMAT_ARGB8888;
834 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400835
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200836 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
837 rectangle->width,
838 rectangle->height,
839 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400840
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700841 return surface;
842}
843
844static cairo_surface_t *
845display_create_shm_surface(struct display *display,
846 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200847 struct shm_pool *alternate_pool,
848 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700849{
850 struct shm_surface_data *data;
851 struct shm_pool *pool;
852 cairo_surface_t *surface;
853
Pekka Paalanen99436862012-11-19 17:15:59 +0200854 if (alternate_pool) {
855 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700856 surface = display_create_shm_surface_from_pool(display,
857 rectangle,
858 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200859 alternate_pool);
860 if (surface) {
861 data = cairo_surface_get_user_data(surface,
862 &shm_surface_data_key);
863 goto out;
864 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700865 }
866
867 pool = shm_pool_create(display,
868 data_length_for_shm_surface(rectangle));
869 if (!pool)
870 return NULL;
871
872 surface =
873 display_create_shm_surface_from_pool(display, rectangle,
874 flags, pool);
875
876 if (!surface) {
877 shm_pool_destroy(pool);
878 return NULL;
879 }
880
881 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200882 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700883 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400884
Pekka Paalanen99436862012-11-19 17:15:59 +0200885out:
886 if (data_ret)
887 *data_ret = data;
888
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889 return surface;
890}
891
nobled7b87cb02011-02-01 18:51:47 +0000892static int
893check_size(struct rectangle *rect)
894{
895 if (rect->width && rect->height)
896 return 0;
897
898 fprintf(stderr, "tried to create surface of "
899 "width: %d, height: %d\n", rect->width, rect->height);
900 return -1;
901}
902
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400903cairo_surface_t *
904display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400906 struct rectangle *rectangle,
907 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400908{
nobled7b87cb02011-02-01 18:51:47 +0000909 if (check_size(rectangle) < 0)
910 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200911
912 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200913 return display_create_shm_surface(display, rectangle, flags,
914 NULL, NULL);
915}
916
Pekka Paalanena4eda732012-11-19 17:16:02 +0200917struct shm_surface_leaf {
918 cairo_surface_t *cairo_surface;
919 /* 'data' is automatically destroyed, when 'cairo_surface' is */
920 struct shm_surface_data *data;
921
922 struct shm_pool *resize_pool;
923 int busy;
924};
925
926static void
927shm_surface_leaf_release(struct shm_surface_leaf *leaf)
928{
929 if (leaf->cairo_surface)
930 cairo_surface_destroy(leaf->cairo_surface);
931 /* leaf->data already destroyed via cairo private */
932
933 if (leaf->resize_pool)
934 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200935
936 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200937}
938
Pekka Paalanenaef02542013-04-25 13:57:47 +0300939#define MAX_LEAVES 3
940
Pekka Paalanen99436862012-11-19 17:15:59 +0200941struct shm_surface {
942 struct toysurface base;
943 struct display *display;
944 struct wl_surface *surface;
945 uint32_t flags;
946 int dx, dy;
947
Pekka Paalanenaef02542013-04-25 13:57:47 +0300948 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200949 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200950};
951
952static struct shm_surface *
953to_shm_surface(struct toysurface *base)
954{
955 return container_of(base, struct shm_surface, base);
956}
957
Pekka Paalanena4eda732012-11-19 17:16:02 +0200958static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300959shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
960{
961#ifdef DEBUG
962 struct shm_surface_leaf *leaf;
963 char bufs[MAX_LEAVES + 1];
964 int i;
965
966 for (i = 0; i < MAX_LEAVES; i++) {
967 leaf = &surface->leaf[i];
968
969 if (leaf->busy)
970 bufs[i] = 'b';
971 else if (leaf->cairo_surface)
972 bufs[i] = 'a';
973 else
974 bufs[i] = ' ';
975 }
976
977 bufs[MAX_LEAVES] = '\0';
978 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
979#endif
980}
981
982static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200983shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
984{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200985 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300986 struct shm_surface_leaf *leaf;
987 int i;
988 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300989
990 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200991
Pekka Paalanenaef02542013-04-25 13:57:47 +0300992 for (i = 0; i < MAX_LEAVES; i++) {
993 leaf = &surface->leaf[i];
994 if (leaf->data && leaf->data->buffer == buffer) {
995 leaf->busy = 0;
996 break;
997 }
998 }
999 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001000
Pekka Paalanenaef02542013-04-25 13:57:47 +03001001 /* Leave one free leaf with storage, release others */
1002 free_found = 0;
1003 for (i = 0; i < MAX_LEAVES; i++) {
1004 leaf = &surface->leaf[i];
1005
1006 if (!leaf->cairo_surface || leaf->busy)
1007 continue;
1008
1009 if (!free_found)
1010 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001011 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001012 shm_surface_leaf_release(leaf);
1013 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001014
Pekka Paalanen97777442013-05-22 10:20:05 +03001015 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001016}
1017
1018static const struct wl_buffer_listener shm_surface_buffer_listener = {
1019 shm_surface_buffer_release
1020};
1021
Pekka Paalanen99436862012-11-19 17:15:59 +02001022static cairo_surface_t *
1023shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001024 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001025 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001026{
Pekka Paalanenec076692012-11-30 13:37:27 +02001027 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001028 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001029 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001030 struct shm_surface_leaf *leaf = NULL;
1031 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001032
1033 surface->dx = dx;
1034 surface->dy = dy;
1035
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001036 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001037 for (i = 0; i < MAX_LEAVES; i++) {
1038 if (surface->leaf[i].busy)
1039 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001040
Pekka Paalanenaef02542013-04-25 13:57:47 +03001041 if (!leaf || surface->leaf[i].cairo_surface)
1042 leaf = &surface->leaf[i];
1043 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001044 DBG_OBJ(surface->surface, "pick leaf %d\n",
1045 (int)(leaf - &surface->leaf[0]));
1046
Pekka Paalanenaef02542013-04-25 13:57:47 +03001047 if (!leaf) {
1048 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001050 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001051 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001052 }
1053
Pekka Paalanena4eda732012-11-19 17:16:02 +02001054 if (!resize_hint && leaf->resize_pool) {
1055 cairo_surface_destroy(leaf->cairo_surface);
1056 leaf->cairo_surface = NULL;
1057 shm_pool_destroy(leaf->resize_pool);
1058 leaf->resize_pool = NULL;
1059 }
1060
Alexander Larsson1818e312013-05-22 14:41:31 +02001061 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1062
Pekka Paalanena4eda732012-11-19 17:16:02 +02001063 if (leaf->cairo_surface &&
1064 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1065 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001066 goto out;
1067
Pekka Paalanena4eda732012-11-19 17:16:02 +02001068 if (leaf->cairo_surface)
1069 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001070
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001071#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001072 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001073 /* Create a big pool to allocate from, while continuously
1074 * resizing. Mmapping a new pool in the server
1075 * is relatively expensive, so reusing a pool performs
1076 * better, but may temporarily reserve unneeded memory.
1077 */
1078 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 leaf->resize_pool = shm_pool_create(surface->display,
1080 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001081 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001082#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001083
Alexander Larsson1818e312013-05-22 14:41:31 +02001084 rect.width = width;
1085 rect.height = height;
1086
Pekka Paalanena4eda732012-11-19 17:16:02 +02001087 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001088 display_create_shm_surface(surface->display, &rect,
1089 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001090 leaf->resize_pool,
1091 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001092 if (!leaf->cairo_surface)
1093 return NULL;
1094
Pekka Paalanena4eda732012-11-19 17:16:02 +02001095 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001096 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001097
1098out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001099 surface->current = leaf;
1100
1101 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001102}
1103
1104static void
1105shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001106 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001107 struct rectangle *server_allocation)
1108{
1109 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001110 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001111
1112 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001113 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001114 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001115 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001116
Alexander Larsson1818e312013-05-22 14:41:31 +02001117 buffer_to_surface_size (buffer_transform, buffer_scale,
1118 &server_allocation->width,
1119 &server_allocation->height);
1120
Pekka Paalanena4eda732012-11-19 17:16:02 +02001121 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001122 surface->dx, surface->dy);
1123 wl_surface_damage(surface->surface, 0, 0,
1124 server_allocation->width, server_allocation->height);
1125 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001126
Pekka Paalanen71233882013-04-25 13:57:53 +03001127 DBG_OBJ(surface->surface, "leaf %d busy\n",
1128 (int)(leaf - &surface->leaf[0]));
1129
Pekka Paalanena4eda732012-11-19 17:16:02 +02001130 leaf->busy = 1;
1131 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001132}
1133
1134static int
1135shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1136{
1137 return -1;
1138}
1139
1140static void
1141shm_surface_release(struct toysurface *base)
1142{
1143}
1144
1145static void
1146shm_surface_destroy(struct toysurface *base)
1147{
1148 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001149 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001150
Pekka Paalanenaef02542013-04-25 13:57:47 +03001151 for (i = 0; i < MAX_LEAVES; i++)
1152 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001153
1154 free(surface);
1155}
1156
1157static struct toysurface *
1158shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1159 uint32_t flags, struct rectangle *rectangle)
1160{
1161 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001162 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001163
Brian Lovinbc919262013-08-07 15:34:59 -07001164 surface = xmalloc(sizeof *surface);
1165 memset(surface, 0, sizeof *surface);
1166
Pekka Paalanen99436862012-11-19 17:15:59 +02001167 if (!surface)
1168 return NULL;
1169
1170 surface->base.prepare = shm_surface_prepare;
1171 surface->base.swap = shm_surface_swap;
1172 surface->base.acquire = shm_surface_acquire;
1173 surface->base.release = shm_surface_release;
1174 surface->base.destroy = shm_surface_destroy;
1175
1176 surface->display = display;
1177 surface->surface = wl_surface;
1178 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001179
1180 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001181}
1182
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001183/*
1184 * The following correspondences between file names and cursors was copied
1185 * from: https://bugs.kde.org/attachment.cgi?id=67313
1186 */
1187
1188static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001189 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001190 "sw-resize",
1191 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001192};
1193
1194static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001195 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001196 "se-resize",
1197 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001198};
1199
1200static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001201 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001202 "s-resize",
1203 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001204};
1205
1206static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001207 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001208 "closedhand",
1209 "208530c400c041818281048008011002"
1210};
1211
1212static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001213 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001214 "default",
1215 "top_left_arrow",
1216 "left-arrow"
1217};
1218
1219static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001220 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001221 "w-resize",
1222 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001223};
1224
1225static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001226 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001227 "e-resize",
1228 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001229};
1230
1231static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001233 "nw-resize",
1234 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235};
1236
1237static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001238 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001239 "ne-resize",
1240 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001241};
1242
1243static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001244 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001245 "n-resize",
1246 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001247};
1248
1249static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001250 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001251 "ibeam",
1252 "text"
1253};
1254
1255static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001256 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001257 "pointer",
1258 "pointing_hand",
1259 "e29285e634086352946a0e7090d73106"
1260};
1261
1262static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001263 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001264 "wait",
1265 "0426c94ea35c87780ff01dc239897213"
1266};
1267
1268struct cursor_alternatives {
1269 const char **names;
1270 size_t count;
1271};
1272
1273static const struct cursor_alternatives cursors[] = {
1274 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1275 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1276 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1277 {grabbings, ARRAY_LENGTH(grabbings)},
1278 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1279 {left_sides, ARRAY_LENGTH(left_sides)},
1280 {right_sides, ARRAY_LENGTH(right_sides)},
1281 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1282 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1283 {top_sides, ARRAY_LENGTH(top_sides)},
1284 {xterms, ARRAY_LENGTH(xterms)},
1285 {hand1s, ARRAY_LENGTH(hand1s)},
1286 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001287};
1288
1289static void
1290create_cursors(struct display *display)
1291{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001292 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001293 struct weston_config *config;
1294 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001295 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001296 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001297 unsigned int i, j;
1298 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001299
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001300 config_file = weston_config_get_name_from_env();
1301 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001302 s = weston_config_get_section(config, "shell", NULL, NULL);
1303 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1304 weston_config_section_get_int(s, "cursor-size", &size, 32);
1305 weston_config_destroy(config);
1306
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001307 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001308 if (!display->cursor_theme) {
1309 fprintf(stderr, "could not load theme '%s'\n", theme);
1310 return;
1311 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001312 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001313 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001314 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001315
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001316 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001317 cursor = NULL;
1318 for (j = 0; !cursor && j < cursors[i].count; ++j)
1319 cursor = wl_cursor_theme_get_cursor(
1320 display->cursor_theme, cursors[i].names[j]);
1321
1322 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001323 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001324 cursors[i].names[0]);
1325
1326 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001327 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001328}
1329
1330static void
1331destroy_cursors(struct display *display)
1332{
1333 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001334 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001335}
1336
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001337struct wl_cursor_image *
1338display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001339{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001340 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001341
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001342 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001343}
1344
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001345static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001346surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001347{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001348 if (!surface->cairo_surface)
1349 return;
1350
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001351 if (surface->opaque_region) {
1352 wl_surface_set_opaque_region(surface->surface,
1353 surface->opaque_region);
1354 wl_region_destroy(surface->opaque_region);
1355 surface->opaque_region = NULL;
1356 }
1357
1358 if (surface->input_region) {
1359 wl_surface_set_input_region(surface->surface,
1360 surface->input_region);
1361 wl_region_destroy(surface->input_region);
1362 surface->input_region = NULL;
1363 }
1364
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001365 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001366 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001367 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001368
Pekka Paalanen89dee002013-02-13 16:17:20 +02001369 cairo_surface_destroy(surface->cairo_surface);
1370 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001371}
1372
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001373int
1374window_has_focus(struct window *window)
1375{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001376 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001377}
1378
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001379static void
1380window_close(struct window *window)
1381{
1382 if (window->close_handler)
1383 window->close_handler(window->user_data);
1384 else
1385 display_exit(window->display);
1386}
1387
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001388struct display *
1389window_get_display(struct window *window)
1390{
1391 return window->display;
1392}
1393
Pekka Paalanen89dee002013-02-13 16:17:20 +02001394static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001395handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1396 int32_t width, int32_t height)
1397{
1398 struct window *window = data;
1399
1400 window_schedule_resize(window, width, height);
1401}
1402
1403static const struct ivi_surface_listener ivi_surface_listener = {
1404 handle_ivi_surface_configure,
1405};
1406
1407static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001408surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001409{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001410 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001411 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001412
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001413 if (!surface->toysurface && display->dpy &&
1414 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001415 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001416 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001417 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001418 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001419 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001420 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001421
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001422 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001423 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001424 surface->surface,
1425 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001426
Pekka Paalanen89dee002013-02-13 16:17:20 +02001427 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001428 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001429 allocation.width, allocation.height, flags,
1430 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001431}
1432
1433static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001434window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001435{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001436 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001437 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001438
Pekka Paalanenec076692012-11-30 13:37:27 +02001439 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001440 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001441
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001442 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1443 flags |= SURFACE_HINT_RGB565;
1444
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001445 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001446}
1447
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001448int
1449window_get_buffer_transform(struct window *window)
1450{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001451 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001452}
1453
1454void
1455window_set_buffer_transform(struct window *window,
1456 enum wl_output_transform transform)
1457{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001458 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001459 wl_surface_set_buffer_transform(window->main_surface->surface,
1460 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001461}
1462
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001463void
1464window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001465 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001466{
1467 window->main_surface->buffer_scale = scale;
1468 wl_surface_set_buffer_scale(window->main_surface->surface,
1469 scale);
1470}
1471
1472uint32_t
1473window_get_buffer_scale(struct window *window)
1474{
1475 return window->main_surface->buffer_scale;
1476}
1477
Alexander Larssond68f5232013-05-22 14:41:33 +02001478uint32_t
1479window_get_output_scale(struct window *window)
1480{
1481 struct window_output *window_output;
1482 struct window_output *window_output_tmp;
1483 int scale = 1;
1484
1485 wl_list_for_each_safe(window_output, window_output_tmp,
1486 &window->window_output_list, link) {
1487 if (window_output->output->scale > scale)
1488 scale = window_output->output->scale;
1489 }
1490
1491 return scale;
1492}
1493
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001494static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001495
Pekka Paalanen4e373742013-02-13 16:17:13 +02001496static void
1497surface_destroy(struct surface *surface)
1498{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001499 if (surface->frame_cb)
1500 wl_callback_destroy(surface->frame_cb);
1501
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001502 if (surface->input_region)
1503 wl_region_destroy(surface->input_region);
1504
1505 if (surface->opaque_region)
1506 wl_region_destroy(surface->opaque_region);
1507
Pekka Paalanen35e82632013-04-25 13:57:48 +03001508 if (surface->subsurface)
1509 wl_subsurface_destroy(surface->subsurface);
1510
Pekka Paalanen4e373742013-02-13 16:17:13 +02001511 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001512
1513 if (surface->toysurface)
1514 surface->toysurface->destroy(surface->toysurface);
1515
Pekka Paalanen35e82632013-04-25 13:57:48 +03001516 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001517 free(surface);
1518}
1519
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001520void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001521window_destroy(struct window *window)
1522{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001523 struct display *display = window->display;
1524 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001525 struct window_output *window_output;
1526 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001527
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001528 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001529
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001530 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001531 if (input->touch_focus == window)
1532 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001533 if (input->pointer_focus == window)
1534 input->pointer_focus = NULL;
1535 if (input->keyboard_focus == window)
1536 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001537 if (input->focus_widget &&
1538 input->focus_widget->window == window)
1539 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001540 }
1541
Rob Bradford7507b572012-05-15 17:55:34 +01001542 wl_list_for_each_safe(window_output, window_output_tmp,
1543 &window->window_output_list, link) {
1544 free (window_output);
1545 }
1546
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001547 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001548 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001549
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001550 if (window->xdg_surface)
1551 xdg_surface_destroy(window->xdg_surface);
1552 if (window->xdg_popup)
1553 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001554
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001555 if (window->ivi_surface)
1556 ivi_surface_destroy(window->ivi_surface);
1557
Pekka Paalanen4e373742013-02-13 16:17:13 +02001558 surface_destroy(window->main_surface);
1559
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001560 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001561
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001562 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001563 free(window);
1564}
1565
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001566static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001567widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001569 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001570
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001571 wl_list_for_each(child, &widget->child_list, link) {
1572 target = widget_find_widget(child, x, y);
1573 if (target)
1574 return target;
1575 }
1576
1577 if (widget->allocation.x <= x &&
1578 x < widget->allocation.x + widget->allocation.width &&
1579 widget->allocation.y <= y &&
1580 y < widget->allocation.y + widget->allocation.height) {
1581 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001582 }
1583
1584 return NULL;
1585}
1586
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001588window_find_widget(struct window *window, int32_t x, int32_t y)
1589{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001590 struct surface *surface;
1591 struct widget *widget;
1592
1593 wl_list_for_each(surface, &window->subsurface_list, link) {
1594 widget = widget_find_widget(surface->widget, x, y);
1595 if (widget)
1596 return widget;
1597 }
1598
1599 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001600}
1601
1602static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001603widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001604{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001605 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001606
Peter Huttererf3d62272013-08-08 11:57:05 +10001607 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001608 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001609 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001610 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001611 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001613 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001614 widget->tooltip = NULL;
1615 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001616 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001617 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001618
1619 return widget;
1620}
1621
1622struct widget *
1623window_add_widget(struct window *window, void *data)
1624{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001625 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001626
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001627 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001628 wl_list_init(&widget->link);
1629 window->main_surface->widget = widget;
1630
1631 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001632}
1633
1634struct widget *
1635widget_add_widget(struct widget *parent, void *data)
1636{
1637 struct widget *widget;
1638
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001639 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001640 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001641
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001642 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001643}
1644
1645void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001646widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001648 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001649 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001650 struct input *input;
1651
Pekka Paalanen35e82632013-04-25 13:57:48 +03001652 /* Destroy the sub-surface along with the root widget */
1653 if (surface->widget == widget && surface->subsurface)
1654 surface_destroy(widget->surface);
1655
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001656 if (widget->tooltip)
1657 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001658
Pekka Paalanene156fb62012-01-19 13:51:38 +02001659 wl_list_for_each(input, &display->input_list, link) {
1660 if (input->focus_widget == widget)
1661 input->focus_widget = NULL;
1662 }
1663
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001664 wl_list_remove(&widget->link);
1665 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001666}
1667
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001668void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001669widget_set_default_cursor(struct widget *widget, int cursor)
1670{
1671 widget->default_cursor = cursor;
1672}
1673
1674void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001675widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001676{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001677 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001678}
1679
1680void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001681widget_set_size(struct widget *widget, int32_t width, int32_t height)
1682{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001683 widget->allocation.width = width;
1684 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001685}
1686
1687void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001688widget_set_allocation(struct widget *widget,
1689 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001691 widget->allocation.x = x;
1692 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001693 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694}
1695
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001696void
1697widget_set_transparent(struct widget *widget, int transparent)
1698{
1699 widget->opaque = !transparent;
1700}
1701
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001702void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001703widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001704{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001705 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706}
1707
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001708static cairo_surface_t *
1709widget_get_cairo_surface(struct widget *widget)
1710{
1711 struct surface *surface = widget->surface;
1712 struct window *window = widget->window;
1713
Neil Roberts97b747c2013-12-19 16:17:12 +00001714 assert(widget->use_cairo);
1715
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001716 if (!surface->cairo_surface) {
1717 if (surface == window->main_surface)
1718 window_create_main_surface(window);
1719 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001720 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001721 }
1722
1723 return surface->cairo_surface;
1724}
1725
Alexander Larsson15901f02013-05-22 14:41:25 +02001726static void
1727widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1728{
1729 struct surface *surface = widget->surface;
1730 double angle;
1731 cairo_matrix_t m;
1732 enum wl_output_transform transform;
1733 int surface_width, surface_height;
1734 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001735 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001736
1737 surface_width = surface->allocation.width;
1738 surface_height = surface->allocation.height;
1739
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001740 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001741 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001742
Alexander Larsson15901f02013-05-22 14:41:25 +02001743 switch (transform) {
1744 case WL_OUTPUT_TRANSFORM_FLIPPED:
1745 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1748 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1749 break;
1750 default:
1751 cairo_matrix_init_identity(&m);
1752 break;
1753 }
1754
1755 switch (transform) {
1756 case WL_OUTPUT_TRANSFORM_NORMAL:
1757 default:
1758 angle = 0;
1759 translate_x = 0;
1760 translate_y = 0;
1761 break;
1762 case WL_OUTPUT_TRANSFORM_FLIPPED:
1763 angle = 0;
1764 translate_x = surface_width;
1765 translate_y = 0;
1766 break;
1767 case WL_OUTPUT_TRANSFORM_90:
1768 angle = M_PI_2;
1769 translate_x = surface_height;
1770 translate_y = 0;
1771 break;
1772 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1773 angle = M_PI_2;
1774 translate_x = surface_height;
1775 translate_y = surface_width;
1776 break;
1777 case WL_OUTPUT_TRANSFORM_180:
1778 angle = M_PI;
1779 translate_x = surface_width;
1780 translate_y = surface_height;
1781 break;
1782 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1783 angle = M_PI;
1784 translate_x = 0;
1785 translate_y = surface_height;
1786 break;
1787 case WL_OUTPUT_TRANSFORM_270:
1788 angle = M_PI + M_PI_2;
1789 translate_x = 0;
1790 translate_y = surface_width;
1791 break;
1792 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1793 angle = M_PI + M_PI_2;
1794 translate_x = 0;
1795 translate_y = 0;
1796 break;
1797 }
1798
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001799 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001800 cairo_translate(cr, translate_x, translate_y);
1801 cairo_rotate(cr, angle);
1802 cairo_transform(cr, &m);
1803}
1804
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001805cairo_t *
1806widget_cairo_create(struct widget *widget)
1807{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001808 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001809 cairo_surface_t *cairo_surface;
1810 cairo_t *cr;
1811
1812 cairo_surface = widget_get_cairo_surface(widget);
1813 cr = cairo_create(cairo_surface);
1814
Alexander Larsson15901f02013-05-22 14:41:25 +02001815 widget_cairo_update_transform(widget, cr);
1816
Pekka Paalanen35e82632013-04-25 13:57:48 +03001817 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1818
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001819 return cr;
1820}
1821
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001822struct wl_surface *
1823widget_get_wl_surface(struct widget *widget)
1824{
1825 return widget->surface->surface;
1826}
1827
Neil Roberts5e10a042013-09-09 00:40:17 +01001828struct wl_subsurface *
1829widget_get_wl_subsurface(struct widget *widget)
1830{
1831 return widget->surface->subsurface;
1832}
1833
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001834uint32_t
1835widget_get_last_time(struct widget *widget)
1836{
1837 return widget->surface->last_time;
1838}
1839
1840void
1841widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1842{
1843 struct wl_compositor *comp = widget->window->display->compositor;
1844 struct surface *surface = widget->surface;
1845
1846 if (!surface->input_region)
1847 surface->input_region = wl_compositor_create_region(comp);
1848
1849 if (rect) {
1850 wl_region_add(surface->input_region,
1851 rect->x, rect->y, rect->width, rect->height);
1852 }
1853}
1854
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001855void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001856widget_set_resize_handler(struct widget *widget,
1857 widget_resize_handler_t handler)
1858{
1859 widget->resize_handler = handler;
1860}
1861
1862void
1863widget_set_redraw_handler(struct widget *widget,
1864 widget_redraw_handler_t handler)
1865{
1866 widget->redraw_handler = handler;
1867}
1868
1869void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001870widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001871{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001872 widget->enter_handler = handler;
1873}
1874
1875void
1876widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1877{
1878 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001879}
1880
1881void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001882widget_set_motion_handler(struct widget *widget,
1883 widget_motion_handler_t handler)
1884{
1885 widget->motion_handler = handler;
1886}
1887
1888void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001889widget_set_button_handler(struct widget *widget,
1890 widget_button_handler_t handler)
1891{
1892 widget->button_handler = handler;
1893}
1894
1895void
Rusty Lynch041815a2013-08-08 21:20:38 -07001896widget_set_touch_up_handler(struct widget *widget,
1897 widget_touch_up_handler_t handler)
1898{
1899 widget->touch_up_handler = handler;
1900}
1901
1902void
1903widget_set_touch_down_handler(struct widget *widget,
1904 widget_touch_down_handler_t handler)
1905{
1906 widget->touch_down_handler = handler;
1907}
1908
1909void
1910widget_set_touch_motion_handler(struct widget *widget,
1911 widget_touch_motion_handler_t handler)
1912{
1913 widget->touch_motion_handler = handler;
1914}
1915
1916void
1917widget_set_touch_frame_handler(struct widget *widget,
1918 widget_touch_frame_handler_t handler)
1919{
1920 widget->touch_frame_handler = handler;
1921}
1922
1923void
1924widget_set_touch_cancel_handler(struct widget *widget,
1925 widget_touch_cancel_handler_t handler)
1926{
1927 widget->touch_cancel_handler = handler;
1928}
1929
1930void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001931widget_set_axis_handler(struct widget *widget,
1932 widget_axis_handler_t handler)
1933{
1934 widget->axis_handler = handler;
1935}
1936
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001937static void
1938window_schedule_redraw_task(struct window *window);
1939
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001940void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001941widget_schedule_redraw(struct widget *widget)
1942{
Pekka Paalanen71233882013-04-25 13:57:53 +03001943 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001944 widget->surface->redraw_needed = 1;
1945 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001946}
1947
Neil Roberts97b747c2013-12-19 16:17:12 +00001948void
1949widget_set_use_cairo(struct widget *widget,
1950 int use_cairo)
1951{
1952 widget->use_cairo = use_cairo;
1953}
1954
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001955cairo_surface_t *
1956window_get_surface(struct window *window)
1957{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001958 cairo_surface_t *cairo_surface;
1959
1960 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1961
1962 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001963}
1964
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001965struct wl_surface *
1966window_get_wl_surface(struct window *window)
1967{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001968 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001969}
1970
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001971static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001972tooltip_redraw_handler(struct widget *widget, void *data)
1973{
1974 cairo_t *cr;
1975 const int32_t r = 3;
1976 struct tooltip *tooltip = data;
1977 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001978
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001979 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001980 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001981 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1982 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1983 cairo_paint(cr);
1984
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001985 width = widget->allocation.width;
1986 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001987 rounded_rect(cr, 0, 0, width, height, r);
1988
1989 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1990 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1991 cairo_fill(cr);
1992
1993 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1994 cairo_move_to(cr, 10, 16);
1995 cairo_show_text(cr, tooltip->entry);
1996 cairo_destroy(cr);
1997}
1998
1999static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002000get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002001{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002002 cairo_t *cr;
2003 cairo_text_extents_t extents;
2004
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002005 /* Use the dummy_surface because tooltip's surface was not
2006 * created yet, and parent does not have a valid surface
2007 * outside repaint, either.
2008 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002009 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002010 cairo_text_extents(cr, tooltip->entry, &extents);
2011 cairo_destroy(cr);
2012
2013 return extents;
2014}
2015
2016static int
2017window_create_tooltip(struct tooltip *tooltip)
2018{
2019 struct widget *parent = tooltip->parent;
2020 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002021 const int offset_y = 27;
2022 const int margin = 3;
2023 cairo_text_extents_t extents;
2024
2025 if (tooltip->widget)
2026 return 0;
2027
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002028 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002029
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002030 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002031 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002032 widget_set_allocation(tooltip->widget,
2033 tooltip->x, tooltip->y + offset_y,
2034 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002035
2036 return 0;
2037}
2038
2039void
2040widget_destroy_tooltip(struct widget *parent)
2041{
2042 struct tooltip *tooltip = parent->tooltip;
2043
2044 parent->tooltip_count = 0;
2045 if (!tooltip)
2046 return;
2047
2048 if (tooltip->widget) {
2049 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002050 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002051 }
2052
2053 close(tooltip->tooltip_fd);
2054 free(tooltip->entry);
2055 free(tooltip);
2056 parent->tooltip = NULL;
2057}
2058
2059static void
2060tooltip_func(struct task *task, uint32_t events)
2061{
2062 struct tooltip *tooltip =
2063 container_of(task, struct tooltip, tooltip_task);
2064 uint64_t exp;
2065
Martin Olsson8df662a2012-07-08 03:03:47 +02002066 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2067 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002068 window_create_tooltip(tooltip);
2069}
2070
2071#define TOOLTIP_TIMEOUT 500
2072static int
2073tooltip_timer_reset(struct tooltip *tooltip)
2074{
2075 struct itimerspec its;
2076
2077 its.it_interval.tv_sec = 0;
2078 its.it_interval.tv_nsec = 0;
2079 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2080 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2081 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2082 fprintf(stderr, "could not set timerfd\n: %m");
2083 return -1;
2084 }
2085
2086 return 0;
2087}
2088
2089int
2090widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2091{
2092 struct tooltip *tooltip = parent->tooltip;
2093
2094 parent->tooltip_count++;
2095 if (tooltip) {
2096 tooltip->x = x;
2097 tooltip->y = y;
2098 tooltip_timer_reset(tooltip);
2099 return 0;
2100 }
2101
2102 /* the handler might be triggered too fast via input device motion, so
2103 * we need this check here to make sure tooltip is fully initialized */
2104 if (parent->tooltip_count > 1)
2105 return 0;
2106
2107 tooltip = malloc(sizeof *tooltip);
2108 if (!tooltip)
2109 return -1;
2110
2111 parent->tooltip = tooltip;
2112 tooltip->parent = parent;
2113 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002114 tooltip->x = x;
2115 tooltip->y = y;
2116 tooltip->entry = strdup(entry);
2117 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2118 if (tooltip->tooltip_fd < 0) {
2119 fprintf(stderr, "could not create timerfd\n: %m");
2120 return -1;
2121 }
2122
2123 tooltip->tooltip_task.run = tooltip_func;
2124 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2125 EPOLLIN, &tooltip->tooltip_task);
2126 tooltip_timer_reset(tooltip);
2127
2128 return 0;
2129}
2130
2131static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002132workspace_manager_state(void *data,
2133 struct workspace_manager *workspace_manager,
2134 uint32_t current,
2135 uint32_t count)
2136{
2137 struct display *display = data;
2138
2139 display->workspace = current;
2140 display->workspace_count = count;
2141}
2142
2143static const struct workspace_manager_listener workspace_manager_listener = {
2144 workspace_manager_state
2145};
2146
2147static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002148frame_resize_handler(struct widget *widget,
2149 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002150{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002151 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002152 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002153 struct rectangle interior;
2154 struct rectangle input;
2155 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002156
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002157 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002158 interior.x = 0;
2159 interior.y = 0;
2160 interior.width = width;
2161 interior.height = height;
2162 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002163 frame_resize(frame->frame, width, height);
2164 frame_interior(frame->frame, &interior.x, &interior.y,
2165 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002166 }
2167
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002168 widget_set_allocation(child, interior.x, interior.y,
2169 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002170
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002171 if (child->resize_handler) {
2172 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002173 child->user_data);
2174
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002175 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002176 width = child->allocation.width;
2177 height = child->allocation.height;
2178 } else {
2179 frame_resize_inside(frame->frame,
2180 child->allocation.width,
2181 child->allocation.height);
2182 width = frame_width(frame->frame);
2183 height = frame_height(frame->frame);
2184 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002185 }
2186
Scott Moreauf7e498c2012-05-14 11:39:29 -06002187 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002188
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002189 widget->surface->input_region =
2190 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002191 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002192 frame_input_rect(frame->frame, &input.x, &input.y,
2193 &input.width, &input.height);
2194 wl_region_add(widget->surface->input_region,
2195 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002196 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002198 }
2199
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002200 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002201
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002202 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002203 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002204 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2205 &opaque.width, &opaque.height);
2206
2207 wl_region_add(widget->surface->opaque_region,
2208 opaque.x, opaque.y,
2209 opaque.width, opaque.height);
2210 } else {
2211 wl_region_add(widget->surface->opaque_region,
2212 0, 0, width, height);
2213 }
Martin Minarik1998b152012-05-10 02:04:35 +02002214 }
2215
Martin Minarik1998b152012-05-10 02:04:35 +02002216
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002217 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002218}
2219
2220static void
2221frame_redraw_handler(struct widget *widget, void *data)
2222{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002223 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002224 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002225 struct window *window = widget->window;
2226
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002227 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002228 return;
2229
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002230 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002231
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002232 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002233
2234 cairo_destroy(cr);
2235}
2236
2237static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002238frame_get_pointer_image_for_location(struct window_frame *frame,
2239 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002240{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002241 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002242
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002243 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002244 return CURSOR_LEFT_PTR;
2245
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002246 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002247 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002248 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002249 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002250 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002251 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002252 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002253 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002254 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002255 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002256 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002257 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002258 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002259 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002260 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002261 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002262 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002263 case THEME_LOCATION_EXTERIOR:
2264 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002265 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002266 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002267 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002268}
2269
Pekka Paalanen26237862014-09-10 15:10:30 +03002270static void
2271frame_menu_func(void *data, struct input *input, int index)
2272{
2273 struct window *window = data;
2274 struct display *display;
2275
2276 switch (index) {
2277 case 0: /* close */
2278 window_close(window);
2279 break;
2280 case 1: /* move to workspace above */
2281 display = window->display;
2282 if (display->workspace > 0)
2283 workspace_manager_move_surface(
2284 display->workspace_manager,
2285 window->main_surface->surface,
2286 display->workspace - 1);
2287 break;
2288 case 2: /* move to workspace below */
2289 display = window->display;
2290 if (display->workspace < display->workspace_count - 1)
2291 workspace_manager_move_surface(
2292 display->workspace_manager,
2293 window->main_surface->surface,
2294 display->workspace + 1);
2295 break;
2296 case 3: /* fullscreen */
2297 /* we don't have a way to get out of fullscreen for now */
2298 if (window->fullscreen_handler)
2299 window->fullscreen_handler(window, window->user_data);
2300 break;
2301 }
2302}
2303
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002304void
2305window_show_frame_menu(struct window *window,
2306 struct input *input, uint32_t time)
2307{
2308 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002309 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002310
Pekka Paalanen26237862014-09-10 15:10:30 +03002311 static const char *entries[] = {
2312 "Close",
2313 "Move to workspace above", "Move to workspace below",
2314 "Fullscreen"
2315 };
2316
2317 if (window->fullscreen_handler)
2318 count = ARRAY_LENGTH(entries);
2319 else
2320 count = ARRAY_LENGTH(entries) - 1;
2321
2322 input_get_position(input, &x, &y);
2323 window_show_menu(window->display, input, time, window,
2324 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002325}
2326
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002327static int
2328frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002329 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002330{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002331 struct window_frame *frame = data;
2332 enum theme_location location;
2333
2334 location = frame_pointer_enter(frame->frame, input, x, y);
2335 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2336 widget_schedule_redraw(frame->widget);
2337
2338 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002339}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002340
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341static int
2342frame_motion_handler(struct widget *widget,
2343 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002344 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002345{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002346 struct window_frame *frame = data;
2347 enum theme_location location;
2348
2349 location = frame_pointer_motion(frame->frame, input, x, y);
2350 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2351 widget_schedule_redraw(frame->widget);
2352
2353 return frame_get_pointer_image_for_location(data, location);
2354}
2355
2356static void
2357frame_leave_handler(struct widget *widget,
2358 struct input *input, void *data)
2359{
2360 struct window_frame *frame = data;
2361
2362 frame_pointer_leave(frame->frame, input);
2363 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2364 widget_schedule_redraw(frame->widget);
2365}
2366
2367static void
2368frame_handle_status(struct window_frame *frame, struct input *input,
2369 uint32_t time, enum theme_location location)
2370{
2371 struct window *window = frame->widget->window;
2372 uint32_t status;
2373
2374 status = frame_status(frame->frame);
2375 if (status & FRAME_STATUS_REPAINT)
2376 widget_schedule_redraw(frame->widget);
2377
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002378 if (status & FRAME_STATUS_MINIMIZE) {
2379 window_set_minimized(window);
2380 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2381 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002382
2383 if (status & FRAME_STATUS_MENU) {
2384 window_show_frame_menu(window, input, time);
2385 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2386 }
2387
2388 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002389 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002390 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2391 }
2392
2393 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002394 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002395 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002396 }
2397
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002398 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002399 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002400 xdg_surface_move(window->xdg_surface,
2401 input_get_seat(input),
2402 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002403
2404 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2405 }
2406
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002407 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002408 input_ungrab(input);
2409
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002410 xdg_surface_resize(window->xdg_surface,
2411 input_get_seat(input),
2412 window->display->serial,
2413 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002414
2415 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2416 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002417}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002418
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002419#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002420static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421frame_button_handler(struct widget *widget,
2422 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002423 uint32_t button, enum wl_pointer_button_state state,
2424 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002425
2426{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002427 struct window_frame *frame = data;
2428 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002429
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002430 frame->double_click = 0;
2431 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2432 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2433 frame->double_click = 1;
2434 frame->did_double = 1;
2435 } else
2436 frame->did_double = 0;
2437
2438 frame->last_time = time;
2439 } else if (frame->did_double == 1) {
2440 frame->double_click = 1;
2441 frame->did_double = 0;
2442 }
2443
2444 if (frame->double_click)
2445 location = frame_double_click(frame->frame, input,
2446 button, state);
2447 else
2448 location = frame_pointer_button(frame->frame, input,
2449 button, state);
2450
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002451 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002452}
2453
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002454static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002455frame_touch_down_handler(struct widget *widget, struct input *input,
2456 uint32_t serial, uint32_t time, int32_t id,
2457 float x, float y, void *data)
2458{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002459 struct window_frame *frame = data;
2460
Xiong Zhang382de462014-06-12 11:06:26 +08002461 frame->double_click = 0;
2462 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2463 frame->last_id == id) {
2464 frame->double_click = 1;
2465 frame->did_double = 1;
2466 frame->double_id = id;
2467 } else
2468 frame->did_double = 0;
2469
2470 frame->last_time = time;
2471 frame->last_id = id;
2472
2473 if (frame->double_click)
2474 frame_double_touch_down(frame->frame, input, id, x, y);
2475 else
2476 frame_touch_down(frame->frame, input, id, x, y);
2477
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002478 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2479}
2480
2481static void
2482frame_touch_up_handler(struct widget *widget,
2483 struct input *input, uint32_t serial, uint32_t time,
2484 int32_t id, void *data)
2485{
2486 struct window_frame *frame = data;
2487
Xiong Zhang382de462014-06-12 11:06:26 +08002488 if (frame->double_id == id && frame->did_double) {
2489 frame->did_double = 0;
2490 frame->double_id = 0;
2491 frame_double_touch_up(frame->frame, input, id);
2492 } else
2493 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002494 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002495}
2496
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002497struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002498window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002499{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002500 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002501 uint32_t buttons;
2502
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002503 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002504 buttons = FRAME_BUTTON_NONE;
2505 } else {
2506 buttons = FRAME_BUTTON_ALL;
2507 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002508
Peter Huttererf3d62272013-08-08 11:57:05 +10002509 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002510 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002511 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002512
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002513 frame->widget = window_add_widget(window, frame);
2514 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002515
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002516 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2517 widget_set_resize_handler(frame->widget, frame_resize_handler);
2518 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002519 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002520 widget_set_motion_handler(frame->widget, frame_motion_handler);
2521 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002522 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002523 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002524
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002525 window->frame = frame;
2526
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002527 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002528}
2529
Kristian Høgsberga1627922012-06-20 17:30:03 -04002530void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002531window_frame_set_child_size(struct widget *widget, int child_width,
2532 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002533{
2534 struct display *display = widget->window->display;
2535 struct theme *t = display->theme;
2536 int decoration_width, decoration_height;
2537 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002538 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002539
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002540 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002541 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002542 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002543 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002544
2545 width = child_width + decoration_width;
2546 height = child_height + decoration_height;
2547 } else {
2548 width = child_width;
2549 height = child_height;
2550 }
2551
2552 window_schedule_resize(widget->window, width, height);
2553}
2554
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002555static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002556window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002557{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002558 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002559
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002560 /* frame->child must be destroyed by the application */
2561 widget_destroy(frame->widget);
2562 free(frame);
2563}
2564
2565static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002566input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002567 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002568{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002569 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002570 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002571
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002572 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002573 return;
2574
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002575 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002576 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002577 widget = old;
2578 if (input->grab)
2579 widget = input->grab;
2580 if (widget->leave_handler)
2581 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002582 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002583 }
2584
2585 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002586 widget = focus;
2587 if (input->grab)
2588 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002589 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002590 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002591 cursor = widget->enter_handler(focus, input, x, y,
2592 widget->user_data);
2593 else
2594 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002595
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002596 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002597 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002598}
2599
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002600void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002601touch_grab(struct input *input, int32_t touch_id)
2602{
2603 input->touch_grab = 1;
2604 input->touch_grab_id = touch_id;
2605}
2606
2607void
2608touch_ungrab(struct input *input)
2609{
2610 struct touch_point *tp, *tmp;
2611
2612 input->touch_grab = 0;
2613
2614 wl_list_for_each_safe(tp, tmp,
2615 &input->touch_point_list, link) {
2616 if (tp->id != input->touch_grab_id)
2617 continue;
2618 wl_list_remove(&tp->link);
2619 free(tp);
2620
2621 return;
2622 }
2623}
2624
2625void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002626input_grab(struct input *input, struct widget *widget, uint32_t button)
2627{
2628 input->grab = widget;
2629 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002630
2631 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002632}
2633
2634void
2635input_ungrab(struct input *input)
2636{
2637 struct widget *widget;
2638
2639 input->grab = NULL;
2640 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002641 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002642 input->sx, input->sy);
2643 input_set_focus_widget(input, widget, input->sx, input->sy);
2644 }
2645}
2646
2647static void
2648input_remove_pointer_focus(struct input *input)
2649{
2650 struct window *window = input->pointer_focus;
2651
2652 if (!window)
2653 return;
2654
2655 input_set_focus_widget(input, NULL, 0, 0);
2656
2657 input->pointer_focus = NULL;
2658 input->current_cursor = CURSOR_UNSET;
2659}
2660
2661static void
2662pointer_handle_enter(void *data, struct wl_pointer *pointer,
2663 uint32_t serial, struct wl_surface *surface,
2664 wl_fixed_t sx_w, wl_fixed_t sy_w)
2665{
2666 struct input *input = data;
2667 struct window *window;
2668 struct widget *widget;
2669 float sx = wl_fixed_to_double(sx_w);
2670 float sy = wl_fixed_to_double(sy_w);
2671
2672 if (!surface) {
2673 /* enter event for a window we've just destroyed */
2674 return;
2675 }
2676
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002677 window = wl_surface_get_user_data(surface);
2678 if (surface != window->main_surface->surface) {
2679 DBG("Ignoring input event from subsurface %p\n", surface);
2680 return;
2681 }
2682
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002683 input->display->serial = serial;
2684 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002685 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002686
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002687 input->sx = sx;
2688 input->sy = sy;
2689
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002690 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002691 input_set_focus_widget(input, widget, sx, sy);
2692}
2693
2694static void
2695pointer_handle_leave(void *data, struct wl_pointer *pointer,
2696 uint32_t serial, struct wl_surface *surface)
2697{
2698 struct input *input = data;
2699
2700 input->display->serial = serial;
2701 input_remove_pointer_focus(input);
2702}
2703
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002704static void
Daniel Stone37816df2012-05-16 18:45:18 +01002705pointer_handle_motion(void *data, struct wl_pointer *pointer,
2706 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002707{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002708 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002709 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002710 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002711 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002712 float sx = wl_fixed_to_double(sx_w);
2713 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002714
Paul Winwoodb22bf572013-08-29 10:52:54 +01002715 if (!window)
2716 return;
2717
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002718 input->sx = sx;
2719 input->sy = sy;
2720
Rob Bradford5f087742013-07-11 19:41:27 +01002721 /* when making the window smaller - e.g. after a unmaximise we might
2722 * still have a pending motion event that the compositor has picked
2723 * based on the old surface dimensions
2724 */
2725 if (sx > window->main_surface->allocation.width ||
2726 sy > window->main_surface->allocation.height)
2727 return;
2728
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002729 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002730 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002731 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002732 }
2733
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002734 if (input->grab)
2735 widget = input->grab;
2736 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002737 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002738 if (widget) {
2739 if (widget->motion_handler)
2740 cursor = widget->motion_handler(input->focus_widget,
2741 input, time, sx, sy,
2742 widget->user_data);
2743 else
2744 cursor = widget->default_cursor;
2745 } else
2746 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002747
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002748 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002749}
2750
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002751static void
Daniel Stone37816df2012-05-16 18:45:18 +01002752pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002753 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002754{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002755 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002756 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002757 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002758
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002759 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002760 if (input->focus_widget && input->grab == NULL &&
2761 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002762 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002763
Neil Roberts6b28aad2012-01-23 19:11:18 +00002764 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002765 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002766 (*widget->button_handler)(widget,
2767 input, time,
2768 button, state,
2769 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002770
Daniel Stone4dbadb12012-05-30 16:31:51 +01002771 if (input->grab && input->grab_button == button &&
2772 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002773 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002774}
2775
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002776static void
Daniel Stone37816df2012-05-16 18:45:18 +01002777pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002778 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002779{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002780 struct input *input = data;
2781 struct widget *widget;
2782
2783 widget = input->focus_widget;
2784 if (input->grab)
2785 widget = input->grab;
2786 if (widget && widget->axis_handler)
2787 (*widget->axis_handler)(widget,
2788 input, time,
2789 axis, value,
2790 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002791}
2792
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002793static const struct wl_pointer_listener pointer_listener = {
2794 pointer_handle_enter,
2795 pointer_handle_leave,
2796 pointer_handle_motion,
2797 pointer_handle_button,
2798 pointer_handle_axis,
2799};
2800
2801static void
2802input_remove_keyboard_focus(struct input *input)
2803{
2804 struct window *window = input->keyboard_focus;
2805 struct itimerspec its;
2806
2807 its.it_interval.tv_sec = 0;
2808 its.it_interval.tv_nsec = 0;
2809 its.it_value.tv_sec = 0;
2810 its.it_value.tv_nsec = 0;
2811 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2812
2813 if (!window)
2814 return;
2815
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002816 if (window->keyboard_focus_handler)
2817 (*window->keyboard_focus_handler)(window, NULL,
2818 window->user_data);
2819
2820 input->keyboard_focus = NULL;
2821}
2822
2823static void
2824keyboard_repeat_func(struct task *task, uint32_t events)
2825{
2826 struct input *input =
2827 container_of(task, struct input, repeat_task);
2828 struct window *window = input->keyboard_focus;
2829 uint64_t exp;
2830
2831 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2832 /* If we change the timer between the fd becoming
2833 * readable and getting here, there'll be nothing to
2834 * read and we get EAGAIN. */
2835 return;
2836
2837 if (window && window->key_handler) {
2838 (*window->key_handler)(window, input, input->repeat_time,
2839 input->repeat_key, input->repeat_sym,
2840 WL_KEYBOARD_KEY_STATE_PRESSED,
2841 window->user_data);
2842 }
2843}
2844
2845static void
2846keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2847 uint32_t format, int fd, uint32_t size)
2848{
2849 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002850 struct xkb_keymap *keymap;
2851 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002852 char *map_str;
2853
2854 if (!data) {
2855 close(fd);
2856 return;
2857 }
2858
2859 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2860 close(fd);
2861 return;
2862 }
2863
2864 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2865 if (map_str == MAP_FAILED) {
2866 close(fd);
2867 return;
2868 }
2869
Ran Benita2e1968f2014-08-19 23:59:51 +03002870 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2871 map_str,
2872 XKB_KEYMAP_FORMAT_TEXT_V1,
2873 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002874 munmap(map_str, size);
2875 close(fd);
2876
Rui Matos3eccb862013-10-10 19:44:22 +02002877 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002878 fprintf(stderr, "failed to compile keymap\n");
2879 return;
2880 }
2881
Rui Matos3eccb862013-10-10 19:44:22 +02002882 state = xkb_state_new(keymap);
2883 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002884 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002885 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002886 return;
2887 }
2888
Rui Matos3eccb862013-10-10 19:44:22 +02002889 xkb_keymap_unref(input->xkb.keymap);
2890 xkb_state_unref(input->xkb.state);
2891 input->xkb.keymap = keymap;
2892 input->xkb.state = state;
2893
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002894 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002895 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002896 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002897 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002898 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002899 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002900}
2901
2902static void
2903keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2904 uint32_t serial, struct wl_surface *surface,
2905 struct wl_array *keys)
2906{
2907 struct input *input = data;
2908 struct window *window;
2909
2910 input->display->serial = serial;
2911 input->keyboard_focus = wl_surface_get_user_data(surface);
2912
2913 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002914 if (window->keyboard_focus_handler)
2915 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002916 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002917}
2918
2919static void
2920keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2921 uint32_t serial, struct wl_surface *surface)
2922{
2923 struct input *input = data;
2924
2925 input->display->serial = serial;
2926 input_remove_keyboard_focus(input);
2927}
2928
Scott Moreau210d0792012-03-22 10:47:01 -06002929static void
Daniel Stone37816df2012-05-16 18:45:18 +01002930keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002931 uint32_t serial, uint32_t time, uint32_t key,
2932 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002933{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002934 struct input *input = data;
2935 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002936 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002937 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002938 const xkb_keysym_t *syms;
2939 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002940 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002941
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002942 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002943 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002944 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002945 return;
2946
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002947 /* We only use input grabs for pointer events for now, so just
2948 * ignore key presses if a grab is active. We expand the key
2949 * event delivery mechanism to route events to widgets to
2950 * properly handle key grabs. In the meantime, this prevents
2951 * key event devlivery while a grab is active. */
2952 if (input->grab && input->grab_button == 0)
2953 return;
2954
Ran Benita2e1968f2014-08-19 23:59:51 +03002955 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002956
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002957 sym = XKB_KEY_NoSymbol;
2958 if (num_syms == 1)
2959 sym = syms[0];
2960
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002961
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002962 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002963 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002964 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002965 } else if (sym == XKB_KEY_F11 &&
2966 window->fullscreen_handler &&
2967 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2968 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002969 } else if (sym == XKB_KEY_F4 &&
2970 input->modifiers == MOD_ALT_MASK &&
2971 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002972 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002973 } else if (window->key_handler) {
2974 (*window->key_handler)(window, input, time, key,
2975 sym, state, window->user_data);
2976 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002977
2978 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2979 key == input->repeat_key) {
2980 its.it_interval.tv_sec = 0;
2981 its.it_interval.tv_nsec = 0;
2982 its.it_value.tv_sec = 0;
2983 its.it_value.tv_nsec = 0;
2984 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002985 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2986 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002987 input->repeat_sym = sym;
2988 input->repeat_key = key;
2989 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002990 its.it_interval.tv_sec = input->repeat_rate_sec;
2991 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2992 its.it_value.tv_sec = input->repeat_delay_sec;
2993 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002994 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2995 }
2996}
2997
2998static void
Daniel Stone351eb612012-05-31 15:27:47 -04002999keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3000 uint32_t serial, uint32_t mods_depressed,
3001 uint32_t mods_latched, uint32_t mods_locked,
3002 uint32_t group)
3003{
3004 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003005 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003006
Matt Ropere61561f2013-06-24 16:52:43 +01003007 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3008 if (!input->xkb.keymap)
3009 return;
3010
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003011 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3012 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003013 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003014 XKB_STATE_MODS_DEPRESSED |
3015 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003016 input->modifiers = 0;
3017 if (mask & input->xkb.control_mask)
3018 input->modifiers |= MOD_CONTROL_MASK;
3019 if (mask & input->xkb.alt_mask)
3020 input->modifiers |= MOD_ALT_MASK;
3021 if (mask & input->xkb.shift_mask)
3022 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003023}
3024
Jonny Lamb06959082014-08-12 14:58:27 +02003025static void
3026set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3027{
3028 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3029 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3030
3031 /* a rate of zero disables any repeating, regardless of the delay's
3032 * value */
3033 if (rate == 0)
3034 return;
3035
3036 if (rate == 1)
3037 input->repeat_rate_sec = 1;
3038 else
3039 input->repeat_rate_nsec = 1000000000 / rate;
3040
3041 input->repeat_delay_sec = delay / 1000;
3042 delay -= (input->repeat_delay_sec * 1000);
3043 input->repeat_delay_nsec = delay * 1000 * 1000;
3044}
3045
3046static void
3047keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3048 int32_t rate, int32_t delay)
3049{
3050 struct input *input = data;
3051
3052 set_repeat_info(input, rate, delay);
3053}
3054
Daniel Stone37816df2012-05-16 18:45:18 +01003055static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003056 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003057 keyboard_handle_enter,
3058 keyboard_handle_leave,
3059 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003060 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003061 keyboard_handle_repeat_info
3062
Daniel Stone37816df2012-05-16 18:45:18 +01003063};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003064
3065static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003066touch_handle_down(void *data, struct wl_touch *wl_touch,
3067 uint32_t serial, uint32_t time, struct wl_surface *surface,
3068 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3069{
3070 struct input *input = data;
3071 struct widget *widget;
3072 float sx = wl_fixed_to_double(x_w);
3073 float sy = wl_fixed_to_double(y_w);
3074
Rusty Lynch1084da52013-08-15 09:10:08 -07003075 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003076 input->touch_focus = wl_surface_get_user_data(surface);
3077 if (!input->touch_focus) {
3078 DBG("Failed to find to touch focus for surface %p\n", surface);
3079 return;
3080 }
3081
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003082 if (surface != input->touch_focus->main_surface->surface) {
3083 DBG("Ignoring input event from subsurface %p\n", surface);
3084 input->touch_focus = NULL;
3085 return;
3086 }
3087
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003088 if (input->grab)
3089 widget = input->grab;
3090 else
3091 widget = window_find_widget(input->touch_focus,
3092 wl_fixed_to_double(x_w),
3093 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003094 if (widget) {
3095 struct touch_point *tp = xmalloc(sizeof *tp);
3096 if (tp) {
3097 tp->id = id;
3098 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003099 tp->x = sx;
3100 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003101 wl_list_insert(&input->touch_point_list, &tp->link);
3102
3103 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003104 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003105 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003106 sx, sy,
3107 widget->user_data);
3108 }
3109 }
3110}
3111
3112static void
3113touch_handle_up(void *data, struct wl_touch *wl_touch,
3114 uint32_t serial, uint32_t time, int32_t id)
3115{
3116 struct input *input = data;
3117 struct touch_point *tp, *tmp;
3118
Rusty Lynch041815a2013-08-08 21:20:38 -07003119 if (!input->touch_focus) {
3120 DBG("No touch focus found for touch up event!\n");
3121 return;
3122 }
3123
3124 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3125 if (tp->id != id)
3126 continue;
3127
3128 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003129 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003130 time, id,
3131 tp->widget->user_data);
3132
3133 wl_list_remove(&tp->link);
3134 free(tp);
3135
3136 return;
3137 }
3138}
3139
3140static void
3141touch_handle_motion(void *data, struct wl_touch *wl_touch,
3142 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3143{
3144 struct input *input = data;
3145 struct touch_point *tp;
3146 float sx = wl_fixed_to_double(x_w);
3147 float sy = wl_fixed_to_double(y_w);
3148
3149 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3150
3151 if (!input->touch_focus) {
3152 DBG("No touch focus found for touch motion event!\n");
3153 return;
3154 }
3155
3156 wl_list_for_each(tp, &input->touch_point_list, link) {
3157 if (tp->id != id)
3158 continue;
3159
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003160 tp->x = sx;
3161 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003162 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003163 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003164 id, sx, sy,
3165 tp->widget->user_data);
3166 return;
3167 }
3168}
3169
3170static void
3171touch_handle_frame(void *data, struct wl_touch *wl_touch)
3172{
3173 struct input *input = data;
3174 struct touch_point *tp, *tmp;
3175
3176 DBG("touch_handle_frame\n");
3177
3178 if (!input->touch_focus) {
3179 DBG("No touch focus found for touch frame event!\n");
3180 return;
3181 }
3182
3183 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3184 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003185 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003186 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003187 }
3188}
3189
3190static void
3191touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3192{
3193 struct input *input = data;
3194 struct touch_point *tp, *tmp;
3195
3196 DBG("touch_handle_cancel\n");
3197
3198 if (!input->touch_focus) {
3199 DBG("No touch focus found for touch cancel event!\n");
3200 return;
3201 }
3202
3203 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3204 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003205 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3206 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003207
3208 wl_list_remove(&tp->link);
3209 free(tp);
3210 }
3211}
3212
3213static const struct wl_touch_listener touch_listener = {
3214 touch_handle_down,
3215 touch_handle_up,
3216 touch_handle_motion,
3217 touch_handle_frame,
3218 touch_handle_cancel,
3219};
3220
3221static void
Daniel Stone37816df2012-05-16 18:45:18 +01003222seat_handle_capabilities(void *data, struct wl_seat *seat,
3223 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003224{
Daniel Stone37816df2012-05-16 18:45:18 +01003225 struct input *input = data;
3226
3227 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3228 input->pointer = wl_seat_get_pointer(seat);
3229 wl_pointer_set_user_data(input->pointer, input);
3230 wl_pointer_add_listener(input->pointer, &pointer_listener,
3231 input);
3232 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3233 wl_pointer_destroy(input->pointer);
3234 input->pointer = NULL;
3235 }
3236
3237 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3238 input->keyboard = wl_seat_get_keyboard(seat);
3239 wl_keyboard_set_user_data(input->keyboard, input);
3240 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3241 input);
3242 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3243 wl_keyboard_destroy(input->keyboard);
3244 input->keyboard = NULL;
3245 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003246
3247 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3248 input->touch = wl_seat_get_touch(seat);
3249 wl_touch_set_user_data(input->touch, input);
3250 wl_touch_add_listener(input->touch, &touch_listener, input);
3251 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3252 wl_touch_destroy(input->touch);
3253 input->touch = NULL;
3254 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003255}
3256
Rob Bradford08031182013-08-13 20:11:03 +01003257static void
3258seat_handle_name(void *data, struct wl_seat *seat,
3259 const char *name)
3260{
3261
3262}
3263
Daniel Stone37816df2012-05-16 18:45:18 +01003264static const struct wl_seat_listener seat_listener = {
3265 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003266 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003267};
3268
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003269void
3270input_get_position(struct input *input, int32_t *x, int32_t *y)
3271{
3272 *x = input->sx;
3273 *y = input->sy;
3274}
3275
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003276int
3277input_get_touch(struct input *input, int32_t id, float *x, float *y)
3278{
3279 struct touch_point *tp;
3280
3281 wl_list_for_each(tp, &input->touch_point_list, link) {
3282 if (tp->id != id)
3283 continue;
3284
3285 *x = tp->x;
3286 *y = tp->y;
3287 return 0;
3288 }
3289
3290 return -1;
3291}
3292
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003293struct display *
3294input_get_display(struct input *input)
3295{
3296 return input->display;
3297}
3298
Daniel Stone37816df2012-05-16 18:45:18 +01003299struct wl_seat *
3300input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003301{
Daniel Stone37816df2012-05-16 18:45:18 +01003302 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003303}
3304
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003305uint32_t
3306input_get_modifiers(struct input *input)
3307{
3308 return input->modifiers;
3309}
3310
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003311struct widget *
3312input_get_focus_widget(struct input *input)
3313{
3314 return input->focus_widget;
3315}
3316
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003317struct data_offer {
3318 struct wl_data_offer *offer;
3319 struct input *input;
3320 struct wl_array types;
3321 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003322
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003323 struct task io_task;
3324 int fd;
3325 data_func_t func;
3326 int32_t x, y;
3327 void *user_data;
3328};
3329
3330static void
3331data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3332{
3333 struct data_offer *offer = data;
3334 char **p;
3335
3336 p = wl_array_add(&offer->types, sizeof *p);
3337 *p = strdup(type);
3338}
3339
3340static const struct wl_data_offer_listener data_offer_listener = {
3341 data_offer_offer,
3342};
3343
3344static void
3345data_offer_destroy(struct data_offer *offer)
3346{
3347 char **p;
3348
3349 offer->refcount--;
3350 if (offer->refcount == 0) {
3351 wl_data_offer_destroy(offer->offer);
3352 for (p = offer->types.data; *p; p++)
3353 free(*p);
3354 wl_array_release(&offer->types);
3355 free(offer);
3356 }
3357}
3358
3359static void
3360data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003361 struct wl_data_device *data_device,
3362 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003363{
3364 struct data_offer *offer;
3365
Brian Lovinbc919262013-08-07 15:34:59 -07003366 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003367
3368 wl_array_init(&offer->types);
3369 offer->refcount = 1;
3370 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003371 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003372 wl_data_offer_add_listener(offer->offer,
3373 &data_offer_listener, offer);
3374}
3375
3376static void
3377data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003378 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003379 wl_fixed_t x_w, wl_fixed_t y_w,
3380 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003381{
3382 struct input *input = data;
3383 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003384 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003385 float x = wl_fixed_to_double(x_w);
3386 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003387 char **p;
3388
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003389 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003390 input->drag_enter_serial = serial;
3391 input->drag_focus = window,
3392 input->drag_x = x;
3393 input->drag_y = y;
3394
3395 if (!input->touch_grab)
3396 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003397
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003398 if (offer) {
3399 input->drag_offer = wl_data_offer_get_user_data(offer);
3400
3401 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3402 *p = NULL;
3403
3404 types_data = input->drag_offer->types.data;
3405 } else {
3406 input->drag_offer = NULL;
3407 types_data = NULL;
3408 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003409
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003410 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003411 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003412 window->user_data);
3413}
3414
3415static void
3416data_device_leave(void *data, struct wl_data_device *data_device)
3417{
3418 struct input *input = data;
3419
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003420 if (input->drag_offer) {
3421 data_offer_destroy(input->drag_offer);
3422 input->drag_offer = NULL;
3423 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003424}
3425
3426static void
3427data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003428 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003429{
3430 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003431 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003432 float x = wl_fixed_to_double(x_w);
3433 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003434 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003435
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003436 input->drag_x = x;
3437 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003438
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003439 if (input->drag_offer)
3440 types_data = input->drag_offer->types.data;
3441 else
3442 types_data = NULL;
3443
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003444 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003445 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003446 window->user_data);
3447}
3448
3449static void
3450data_device_drop(void *data, struct wl_data_device *data_device)
3451{
3452 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003453 struct window *window = input->drag_focus;
3454 float x, y;
3455
3456 x = input->drag_x;
3457 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003458
3459 if (window->drop_handler)
3460 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003461 x, y, window->user_data);
3462
3463 if (input->touch_grab)
3464 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003465}
3466
3467static void
3468data_device_selection(void *data,
3469 struct wl_data_device *wl_data_device,
3470 struct wl_data_offer *offer)
3471{
3472 struct input *input = data;
3473 char **p;
3474
3475 if (input->selection_offer)
3476 data_offer_destroy(input->selection_offer);
3477
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003478 if (offer) {
3479 input->selection_offer = wl_data_offer_get_user_data(offer);
3480 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3481 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003482 } else {
3483 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003484 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003485}
3486
3487static const struct wl_data_device_listener data_device_listener = {
3488 data_device_data_offer,
3489 data_device_enter,
3490 data_device_leave,
3491 data_device_motion,
3492 data_device_drop,
3493 data_device_selection
3494};
3495
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003496static void
3497input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003498{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003499 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003500 struct wl_cursor *cursor;
3501 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003502
Daniel Stone80972742012-11-07 17:51:39 +11003503 if (!input->pointer)
3504 return;
3505
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003506 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003507 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003508 return;
3509
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003510 if (index >= (int) cursor->image_count) {
3511 fprintf(stderr, "cursor index out of range\n");
3512 return;
3513 }
3514
3515 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003516 buffer = wl_cursor_image_get_buffer(image);
3517 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003518 return;
3519
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003520 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3521 wl_surface_damage(input->pointer_surface, 0, 0,
3522 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003523 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003524 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3525 input->pointer_surface,
3526 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003527}
3528
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003529static const struct wl_callback_listener pointer_surface_listener;
3530
Derek Foreman493d9792015-03-04 16:26:25 -06003531static bool
3532input_set_pointer_special(struct input *input)
3533{
3534 if (input->current_cursor == CURSOR_BLANK) {
3535 wl_pointer_set_cursor(input->pointer,
3536 input->pointer_enter_serial,
3537 NULL, 0, 0);
3538 return true;
3539 }
3540
3541 if (input->current_cursor == CURSOR_UNSET)
3542 return true;
3543
3544 return false;
3545}
3546
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003547static void
3548pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3549 uint32_t time)
3550{
3551 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003552 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003553 int i;
3554
3555 if (callback) {
3556 assert(callback == input->cursor_frame_cb);
3557 wl_callback_destroy(callback);
3558 input->cursor_frame_cb = NULL;
3559 }
3560
Daniel Stone80972742012-11-07 17:51:39 +11003561 if (!input->pointer)
3562 return;
3563
Derek Foreman493d9792015-03-04 16:26:25 -06003564 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003565 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003566
Daniel Stonea494f1d2012-06-18 19:31:12 +01003567 cursor = input->display->cursors[input->current_cursor];
3568 if (!cursor)
3569 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003570
3571 /* FIXME We don't have the current time on the first call so we set
3572 * the animation start to the time of the first frame callback. */
3573 if (time == 0)
3574 input->cursor_anim_start = 0;
3575 else if (input->cursor_anim_start == 0)
3576 input->cursor_anim_start = time;
3577
3578 if (time == 0 || input->cursor_anim_start == 0)
3579 i = 0;
3580 else
3581 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3582
Pekka Paalanenbc106382012-10-10 12:49:31 +03003583 if (cursor->image_count > 1) {
3584 input->cursor_frame_cb =
3585 wl_surface_frame(input->pointer_surface);
3586 wl_callback_add_listener(input->cursor_frame_cb,
3587 &pointer_surface_listener, input);
3588 }
3589
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003590 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003591}
3592
3593static const struct wl_callback_listener pointer_surface_listener = {
3594 pointer_surface_frame_callback
3595};
3596
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003597void
3598input_set_pointer_image(struct input *input, int pointer)
3599{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003600 int force = 0;
3601
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003602 if (!input->pointer)
3603 return;
3604
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003605 if (input->pointer_enter_serial > input->cursor_serial)
3606 force = 1;
3607
3608 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003609 return;
3610
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003611 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003612 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003613 if (!input->cursor_frame_cb)
3614 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003615 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003616 /* The current frame callback may be stuck if, for instance,
3617 * the set cursor request was processed by the server after
3618 * this client lost the focus. In this case the cursor surface
3619 * might not be mapped and the frame callback wouldn't ever
3620 * complete. Send a set_cursor and attach to try to map the
3621 * cursor surface again so that the callback will finish */
3622 input_set_pointer_image_index(input, 0);
3623 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003624}
3625
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003626struct wl_data_device *
3627input_get_data_device(struct input *input)
3628{
3629 return input->data_device;
3630}
3631
3632void
3633input_set_selection(struct input *input,
3634 struct wl_data_source *source, uint32_t time)
3635{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003636 if (input->data_device)
3637 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003638}
3639
3640void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003641input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003642{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003643 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003644 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003645}
3646
3647static void
3648offer_io_func(struct task *task, uint32_t events)
3649{
3650 struct data_offer *offer =
3651 container_of(task, struct data_offer, io_task);
3652 unsigned int len;
3653 char buffer[4096];
3654
3655 len = read(offer->fd, buffer, sizeof buffer);
3656 offer->func(buffer, len,
3657 offer->x, offer->y, offer->user_data);
3658
3659 if (len == 0) {
3660 close(offer->fd);
3661 data_offer_destroy(offer);
3662 }
3663}
3664
3665static void
3666data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3667 data_func_t func, void *user_data)
3668{
3669 int p[2];
3670
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003671 if (pipe2(p, O_CLOEXEC) == -1)
3672 return;
3673
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003674 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3675 close(p[1]);
3676
3677 offer->io_task.run = offer_io_func;
3678 offer->fd = p[0];
3679 offer->func = func;
3680 offer->refcount++;
3681 offer->user_data = user_data;
3682
3683 display_watch_fd(offer->input->display,
3684 offer->fd, EPOLLIN, &offer->io_task);
3685}
3686
3687void
3688input_receive_drag_data(struct input *input, const char *mime_type,
3689 data_func_t func, void *data)
3690{
3691 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003692 input->drag_offer->x = input->drag_x;
3693 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003694}
3695
3696int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003697input_receive_drag_data_to_fd(struct input *input,
3698 const char *mime_type, int fd)
3699{
3700 if (input->drag_offer)
3701 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3702
3703 return 0;
3704}
3705
3706int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003707input_receive_selection_data(struct input *input, const char *mime_type,
3708 data_func_t func, void *data)
3709{
3710 char **p;
3711
3712 if (input->selection_offer == NULL)
3713 return -1;
3714
3715 for (p = input->selection_offer->types.data; *p; p++)
3716 if (strcmp(mime_type, *p) == 0)
3717 break;
3718
3719 if (*p == NULL)
3720 return -1;
3721
3722 data_offer_receive_data(input->selection_offer,
3723 mime_type, func, data);
3724 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003725}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003726
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003727int
3728input_receive_selection_data_to_fd(struct input *input,
3729 const char *mime_type, int fd)
3730{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003731 if (input->selection_offer)
3732 wl_data_offer_receive(input->selection_offer->offer,
3733 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003734
3735 return 0;
3736}
3737
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003738void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003739window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003740{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003741 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003742 return;
3743
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003744 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003745}
3746
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003747static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003748surface_set_synchronized(struct surface *surface)
3749{
3750 if (!surface->subsurface)
3751 return;
3752
3753 if (surface->synchronized)
3754 return;
3755
3756 wl_subsurface_set_sync(surface->subsurface);
3757 surface->synchronized = 1;
3758}
3759
3760static void
3761surface_set_synchronized_default(struct surface *surface)
3762{
3763 if (!surface->subsurface)
3764 return;
3765
3766 if (surface->synchronized == surface->synchronized_default)
3767 return;
3768
3769 if (surface->synchronized_default)
3770 wl_subsurface_set_sync(surface->subsurface);
3771 else
3772 wl_subsurface_set_desync(surface->subsurface);
3773
3774 surface->synchronized = surface->synchronized_default;
3775}
3776
3777static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003778surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003779{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003780 struct widget *widget = surface->widget;
3781 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003782
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003783 if (surface->input_region) {
3784 wl_region_destroy(surface->input_region);
3785 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003786 }
3787
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003788 if (surface->opaque_region)
3789 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003790
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003791 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003792
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003793 if (widget->resize_handler)
3794 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003795 widget->allocation.width,
3796 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003797 widget->user_data);
3798
Pekka Paalanen35e82632013-04-25 13:57:48 +03003799 if (surface->subsurface &&
3800 (surface->allocation.x != widget->allocation.x ||
3801 surface->allocation.y != widget->allocation.y)) {
3802 wl_subsurface_set_position(surface->subsurface,
3803 widget->allocation.x,
3804 widget->allocation.y);
3805 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003806 if (surface->allocation.width != widget->allocation.width ||
3807 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003808 window_schedule_redraw(widget->window);
3809 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003810 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003811
3812 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003813 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003814 widget->allocation.width,
3815 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003816}
3817
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003818static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003819hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3820{
3821 /*
3822 * This hack should be removed, when EGL respects
3823 * eglSwapInterval(0).
3824 *
3825 * If this window has sub-surfaces, especially a free-running
3826 * EGL-widget, we need to post the parent surface once with
3827 * all the old state to guarantee, that the EGL-widget will
3828 * receive its frame callback soon. Otherwise, a forced call
3829 * to eglSwapBuffers may end up blocking, waiting for a frame
3830 * event that will never come, because we will commit the parent
3831 * surface with all new state only after eglSwapBuffers returns.
3832 *
3833 * This assumes, that:
3834 * 1. When the EGL widget's resize hook is called, it pauses.
3835 * 2. When the EGL widget's redraw hook is called, it forces a
3836 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3837 * In a single threaded application condition 1 is a no-op.
3838 *
3839 * XXX: This should actually be after the surface_resize() calls,
3840 * but cannot, because then it would commit the incomplete state
3841 * accumulated from the widget resize hooks.
3842 */
3843 if (window->subsurface_list.next != &window->main_surface->link ||
3844 window->subsurface_list.prev != &window->main_surface->link)
3845 wl_surface_commit(window->main_surface->surface);
3846}
3847
3848static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003849window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003850{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003851 struct surface *surface;
3852
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003853 widget_set_allocation(window->main_surface->widget,
3854 window->pending_allocation.x,
3855 window->pending_allocation.y,
3856 window->pending_allocation.width,
3857 window->pending_allocation.height);
3858
3859 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003860
3861 /* The main surface is in the list, too. Main surface's
3862 * resize_handler is responsible for calling widget_set_allocation()
3863 * on all sub-surface root widgets, so they will be resized
3864 * properly.
3865 */
3866 wl_list_for_each(surface, &window->subsurface_list, link) {
3867 if (surface == window->main_surface)
3868 continue;
3869
3870 surface_set_synchronized(surface);
3871 surface_resize(surface);
3872 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003873
3874 if (!window->fullscreen && !window->maximized)
3875 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003876}
3877
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003878static void
3879idle_resize(struct window *window)
3880{
3881 window->resize_needed = 0;
3882 window->redraw_needed = 1;
3883
3884 DBG("from %dx%d to %dx%d\n",
3885 window->main_surface->server_allocation.width,
3886 window->main_surface->server_allocation.height,
3887 window->pending_allocation.width,
3888 window->pending_allocation.height);
3889
3890 hack_prevent_EGL_sub_surface_deadlock(window);
3891
3892 window_do_resize(window);
3893}
3894
3895static void
3896undo_resize(struct window *window)
3897{
3898 window->pending_allocation.width =
3899 window->main_surface->server_allocation.width;
3900 window->pending_allocation.height =
3901 window->main_surface->server_allocation.height;
3902
3903 DBG("back to %dx%d\n",
3904 window->main_surface->server_allocation.width,
3905 window->main_surface->server_allocation.height);
3906
3907 window_do_resize(window);
3908
3909 if (window->pending_allocation.width == 0 &&
3910 window->pending_allocation.height == 0) {
3911 fprintf(stderr, "Error: Could not draw a surface, "
3912 "most likely due to insufficient disk space in "
3913 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3914 exit(EXIT_FAILURE);
3915 }
3916}
3917
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003918void
3919window_schedule_resize(struct window *window, int width, int height)
3920{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003921 /* We should probably get these numbers from the theme. */
3922 const int min_width = 200, min_height = 200;
3923
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003924 window->pending_allocation.x = 0;
3925 window->pending_allocation.y = 0;
3926 window->pending_allocation.width = width;
3927 window->pending_allocation.height = height;
3928
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003929 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003930 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003931 window->min_allocation.width = min_width;
3932 else
3933 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003934 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003935 window->min_allocation.height = min_height;
3936 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003937 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003938 }
3939
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003940 if (window->pending_allocation.width < window->min_allocation.width)
3941 window->pending_allocation.width = window->min_allocation.width;
3942 if (window->pending_allocation.height < window->min_allocation.height)
3943 window->pending_allocation.height = window->min_allocation.height;
3944
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003945 window->resize_needed = 1;
3946 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003947}
3948
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003949void
3950widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3951{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003952 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003953}
3954
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003955static int
3956window_get_shadow_margin(struct window *window)
3957{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003958 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003959 return frame_get_shadow_margin(window->frame->frame);
3960 else
3961 return 0;
3962}
3963
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003964static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003965handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003966 int32_t width, int32_t height,
3967 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003968{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003969 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003970 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003971
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003972 window->maximized = 0;
3973 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003974 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003975 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003976
3977 wl_array_for_each(p, states) {
3978 uint32_t state = *p;
3979 switch (state) {
3980 case XDG_SURFACE_STATE_MAXIMIZED:
3981 window->maximized = 1;
3982 break;
3983 case XDG_SURFACE_STATE_FULLSCREEN:
3984 window->fullscreen = 1;
3985 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003986 case XDG_SURFACE_STATE_RESIZING:
3987 window->resizing = 1;
3988 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003989 case XDG_SURFACE_STATE_ACTIVATED:
3990 window->focused = 1;
3991 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003992 default:
3993 /* Unknown state */
3994 break;
3995 }
3996 }
3997
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003998 if (window->frame) {
3999 if (window->maximized) {
4000 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4001 } else {
4002 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4003 }
4004
4005 if (window->focused) {
4006 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4007 } else {
4008 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4009 }
4010 }
4011
Jasper St. Pierref184c382014-05-06 08:33:27 -04004012 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004013 /* The width / height params are for window geometry,
4014 * but window_schedule_resize takes allocation. Add
4015 * on the shadow margin to get the difference. */
4016 int margin = window_get_shadow_margin(window);
4017
4018 window_schedule_resize(window,
4019 width + margin * 2,
4020 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004021 } else {
4022 window_schedule_resize(window,
4023 window->saved_allocation.width,
4024 window->saved_allocation.height);
4025 }
4026
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004027 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004028
4029 if (window->state_changed_handler)
4030 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004031}
4032
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004033static void
4034handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4035{
4036 struct window *window = data;
4037 window_close(window);
4038}
4039
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004040static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004041 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004042 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004043};
4044
4045static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004046window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004047{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004048 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004049
4050 if (!window->xdg_surface)
4051 return;
4052
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004053 if (window->parent == window->last_parent)
4054 return;
4055
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004056 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004057 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004058 else
4059 parent_surface = NULL;
4060
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004061 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004062 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004063}
4064
4065static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004066window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004067{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004068 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004069 frame_input_rect(window->frame->frame,
4070 &geometry->x,
4071 &geometry->y,
4072 &geometry->width,
4073 &geometry->height);
4074 else
4075 window_get_allocation(window, geometry);
4076}
4077
4078static void
4079window_sync_geometry(struct window *window)
4080{
4081 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004082
4083 if (!window->xdg_surface)
4084 return;
4085
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004086 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004087 if (geometry.x == window->last_geometry.x &&
4088 geometry.y == window->last_geometry.y &&
4089 geometry.width == window->last_geometry.width &&
4090 geometry.height == window->last_geometry.height)
4091 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004092
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004093 xdg_surface_set_window_geometry(window->xdg_surface,
4094 geometry.x,
4095 geometry.y,
4096 geometry.width,
4097 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004098 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004099}
4100
4101static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004102window_flush(struct window *window)
4103{
4104 struct surface *surface;
4105
4106 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004107 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004108 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004109 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004110 }
4111 }
4112
4113 wl_list_for_each(surface, &window->subsurface_list, link) {
4114 if (surface == window->main_surface)
4115 continue;
4116
4117 surface_flush(surface);
4118 }
4119
4120 surface_flush(window->main_surface);
4121}
4122
4123static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004124menu_destroy(struct menu *menu)
4125{
4126 widget_destroy(menu->widget);
4127 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004128 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004129 free(menu);
4130}
4131
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004132void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004133window_get_allocation(struct window *window,
4134 struct rectangle *allocation)
4135{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004136 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004137}
4138
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004139static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004140widget_redraw(struct widget *widget)
4141{
4142 struct widget *child;
4143
4144 if (widget->redraw_handler)
4145 widget->redraw_handler(widget, widget->user_data);
4146 wl_list_for_each(child, &widget->child_list, link)
4147 widget_redraw(child);
4148}
4149
4150static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004151frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4152{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004153 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004154
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004155 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004156 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004157 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004158 surface->frame_cb = NULL;
4159
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004160 surface->last_time = time;
4161
Pekka Paalanen71233882013-04-25 13:57:53 +03004162 if (surface->redraw_needed || surface->window->redraw_needed) {
4163 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004164 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004165 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004166}
4167
4168static const struct wl_callback_listener listener = {
4169 frame_callback
4170};
4171
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004172static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004173surface_redraw(struct surface *surface)
4174{
Pekka Paalanen71233882013-04-25 13:57:53 +03004175 DBG_OBJ(surface->surface, "begin\n");
4176
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004177 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004178 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004179
4180 /* Whole-window redraw forces a redraw even if the previous has
4181 * not yet hit the screen.
4182 */
4183 if (surface->frame_cb) {
4184 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004185 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004186
Pekka Paalanen71233882013-04-25 13:57:53 +03004187 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004188 wl_callback_destroy(surface->frame_cb);
4189 }
4190
Neil Roberts97b747c2013-12-19 16:17:12 +00004191 if (surface->widget->use_cairo &&
4192 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004193 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4194 return -1;
4195 }
4196
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004197 surface->frame_cb = wl_surface_frame(surface->surface);
4198 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004199 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004200
4201 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004202 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004203 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004204 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004205 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004206}
4207
4208static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004209idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004210{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004211 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004212 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004213 int failed = 0;
4214 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004215
Pekka Paalanen71233882013-04-25 13:57:53 +03004216 DBG(" --------- \n");
4217
Pekka Paalaneneebff542013-04-25 13:57:52 +03004218 wl_list_init(&window->redraw_task.link);
4219 window->redraw_task_scheduled = 0;
4220
4221 if (window->resize_needed) {
4222 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004223 if (window->main_surface->frame_cb) {
4224 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004225 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004226 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004227
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004228 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004229 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004230 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004231
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004232 if (surface_redraw(window->main_surface) < 0) {
4233 /*
4234 * Only main_surface failure will cause us to undo the resize.
4235 * If sub-surfaces fail, they will just be broken with old
4236 * content.
4237 */
4238 failed = 1;
4239 } else {
4240 wl_list_for_each(surface, &window->subsurface_list, link) {
4241 if (surface == window->main_surface)
4242 continue;
4243
4244 surface_redraw(surface);
4245 }
4246 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004247
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004248 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004249 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004250
4251 wl_list_for_each(surface, &window->subsurface_list, link)
4252 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004253
4254 if (resized && failed) {
4255 /* Restore widget tree to correspond to what is on screen. */
4256 undo_resize(window);
4257 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004258}
4259
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004260static void
4261window_schedule_redraw_task(struct window *window)
4262{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004263 if (!window->redraw_task_scheduled) {
4264 window->redraw_task.run = idle_redraw;
4265 display_defer(window->display, &window->redraw_task);
4266 window->redraw_task_scheduled = 1;
4267 }
4268}
4269
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004270void
4271window_schedule_redraw(struct window *window)
4272{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004273 struct surface *surface;
4274
Pekka Paalanen71233882013-04-25 13:57:53 +03004275 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4276
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004277 wl_list_for_each(surface, &window->subsurface_list, link)
4278 surface->redraw_needed = 1;
4279
4280 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004281}
4282
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004283int
4284window_is_fullscreen(struct window *window)
4285{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004286 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004287}
4288
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004289void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004290window_set_fullscreen(struct window *window, int fullscreen)
4291{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004292 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004293 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004294
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004295 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004296 return;
4297
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004298 if (fullscreen)
4299 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4300 else
4301 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004302}
4303
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004304int
4305window_is_maximized(struct window *window)
4306{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004307 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004308}
4309
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004310void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004311window_set_maximized(struct window *window, int maximized)
4312{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004313 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004314 return;
4315
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004316 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004317 return;
4318
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004319 if (maximized)
4320 xdg_surface_set_maximized(window->xdg_surface);
4321 else
4322 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004323}
4324
Jasper St. Pierrede680992014-04-10 17:23:49 -07004325int
4326window_is_resizing(struct window *window)
4327{
4328 return window->resizing;
4329}
4330
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004331void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004332window_set_minimized(struct window *window)
4333{
4334 if (!window->xdg_surface)
4335 return;
4336
4337 xdg_surface_set_minimized(window->xdg_surface);
4338}
4339
4340void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004341window_set_user_data(struct window *window, void *data)
4342{
4343 window->user_data = data;
4344}
4345
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004346void *
4347window_get_user_data(struct window *window)
4348{
4349 return window->user_data;
4350}
4351
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004352void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004353window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004354 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004355{
4356 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004357}
4358
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004359void
4360window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004361 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004362{
4363 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004364}
4365
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004366void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004367window_set_data_handler(struct window *window, window_data_handler_t handler)
4368{
4369 window->data_handler = handler;
4370}
4371
4372void
4373window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4374{
4375 window->drop_handler = handler;
4376}
4377
4378void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004379window_set_close_handler(struct window *window,
4380 window_close_handler_t handler)
4381{
4382 window->close_handler = handler;
4383}
4384
4385void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004386window_set_fullscreen_handler(struct window *window,
4387 window_fullscreen_handler_t handler)
4388{
4389 window->fullscreen_handler = handler;
4390}
4391
4392void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004393window_set_output_handler(struct window *window,
4394 window_output_handler_t handler)
4395{
4396 window->output_handler = handler;
4397}
4398
4399void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004400window_set_state_changed_handler(struct window *window,
4401 window_state_changed_handler_t handler)
4402{
4403 window->state_changed_handler = handler;
4404}
4405
4406void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004407window_set_title(struct window *window, const char *title)
4408{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004409 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004410 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004411 if (window->frame) {
4412 frame_set_title(window->frame->frame, title);
4413 widget_schedule_redraw(window->frame->widget);
4414 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004415 if (window->xdg_surface)
4416 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004417}
4418
4419const char *
4420window_get_title(struct window *window)
4421{
4422 return window->title;
4423}
4424
4425void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004426window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4427{
4428 struct text_cursor_position *text_cursor_position =
4429 window->display->text_cursor_position;
4430
Scott Moreau9295ce02012-06-01 12:46:10 -06004431 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004432 return;
4433
4434 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004435 window->main_surface->surface,
4436 wl_fixed_from_int(x),
4437 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004438}
4439
4440void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004441window_damage(struct window *window, int32_t x, int32_t y,
4442 int32_t width, int32_t height)
4443{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004444 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004445}
4446
Casey Dahlin9074db52012-04-19 22:50:09 -04004447static void
4448surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004449 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004450{
Rob Bradford7507b572012-05-15 17:55:34 +01004451 struct window *window = data;
4452 struct output *output;
4453 struct output *output_found = NULL;
4454 struct window_output *window_output;
4455
4456 wl_list_for_each(output, &window->display->output_list, link) {
4457 if (output->output == wl_output) {
4458 output_found = output;
4459 break;
4460 }
4461 }
4462
4463 if (!output_found)
4464 return;
4465
Brian Lovinbc919262013-08-07 15:34:59 -07004466 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004467 window_output->output = output_found;
4468
4469 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004470
4471 if (window->output_handler)
4472 window->output_handler(window, output_found, 1,
4473 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004474}
4475
4476static void
4477surface_leave(void *data,
4478 struct wl_surface *wl_surface, struct wl_output *output)
4479{
Rob Bradford7507b572012-05-15 17:55:34 +01004480 struct window *window = data;
4481 struct window_output *window_output;
4482 struct window_output *window_output_found = NULL;
4483
4484 wl_list_for_each(window_output, &window->window_output_list, link) {
4485 if (window_output->output->output == output) {
4486 window_output_found = window_output;
4487 break;
4488 }
4489 }
4490
4491 if (window_output_found) {
4492 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004493
4494 if (window->output_handler)
4495 window->output_handler(window, window_output->output,
4496 0, window->user_data);
4497
Rob Bradford7507b572012-05-15 17:55:34 +01004498 free(window_output_found);
4499 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004500}
4501
4502static const struct wl_surface_listener surface_listener = {
4503 surface_enter,
4504 surface_leave
4505};
4506
Pekka Paalanen4e373742013-02-13 16:17:13 +02004507static struct surface *
4508surface_create(struct window *window)
4509{
4510 struct display *display = window->display;
4511 struct surface *surface;
4512
Brian Lovinbc919262013-08-07 15:34:59 -07004513 surface = xmalloc(sizeof *surface);
4514 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004515 if (!surface)
4516 return NULL;
4517
4518 surface->window = window;
4519 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004520 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004521 wl_surface_add_listener(surface->surface, &surface_listener, window);
4522
Pekka Paalanen35e82632013-04-25 13:57:48 +03004523 wl_list_insert(&window->subsurface_list, &surface->link);
4524
Pekka Paalanen4e373742013-02-13 16:17:13 +02004525 return surface;
4526}
4527
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004528static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004529get_preferred_buffer_type(struct display *display)
4530{
4531#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004532 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004533 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4534#endif
4535
4536 return WINDOW_BUFFER_TYPE_SHM;
4537}
4538
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004539static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004540window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004541{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004542 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004543 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004544
Peter Huttererf3d62272013-08-08 11:57:05 +10004545 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004546 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004547 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004548
4549 surface = surface_create(window);
4550 window->main_surface = surface;
4551
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004552 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004553
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004554 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004555 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004556
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004557 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004558
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004559 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004560 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004561 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004562
Rob Bradford7507b572012-05-15 17:55:34 +01004563 wl_list_init (&window->window_output_list);
4564
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004565 return window;
4566}
4567
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004568struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004569window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004570{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004571 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004572 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004573
4574 window = window_create_internal(display, 0);
4575
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004576 if (window->display->xdg_shell) {
4577 window->xdg_surface =
4578 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4579 window->main_surface->surface);
4580 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004581
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004582 xdg_surface_set_user_data(window->xdg_surface, window);
4583 xdg_surface_add_listener(window->xdg_surface,
4584 &xdg_surface_listener, window);
4585 } else if (display->ivi_application) {
4586 /* auto generation of ivi_id based on process id + basement of id */
4587 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4588 window->ivi_surface =
4589 ivi_application_surface_create(display->ivi_application,
4590 id_ivisurf, window->main_surface->surface);
4591 fail_on_null(window->ivi_surface);
4592
4593 ivi_surface_add_listener(window->ivi_surface,
4594 &ivi_surface_listener, window);
4595 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004596
4597 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004598}
4599
4600struct window *
4601window_create_custom(struct display *display)
4602{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004603 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004604}
4605
Jasper St. Pierre53686042013-12-09 15:26:25 -05004606void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004607window_set_parent(struct window *window,
4608 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004609{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004610 window->parent = parent_window;
4611 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004612}
4613
4614struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004615window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004616{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004617 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004618}
4619
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004620static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004621menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004622{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004623 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004624 int next;
4625
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004626 frame_interior(menu->frame, &x, &y, &width, &height);
4627 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004628 if (menu->current != next) {
4629 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004630 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004631 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004632}
4633
4634static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004635menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004636 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004637 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004638{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004639 struct menu *menu = data;
4640
4641 if (widget == menu->widget)
4642 menu_set_item(data, y);
4643
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004644 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004645}
4646
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004647static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004648menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004649 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004650{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004651 struct menu *menu = data;
4652
4653 if (widget == menu->widget)
4654 menu_set_item(data, y);
4655
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004656 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004657}
4658
4659static void
4660menu_leave_handler(struct widget *widget, struct input *input, void *data)
4661{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004662 struct menu *menu = data;
4663
4664 if (widget == menu->widget)
4665 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004666}
4667
4668static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004669menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004670 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004671 uint32_t button, enum wl_pointer_button_state state,
4672 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004673
4674{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004675 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004676
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004677 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4678 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004679 /* Either relase after press-drag-release or
4680 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004681 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004682 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004683 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004684 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004685 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004686 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004687}
4688
4689static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004690menu_touch_up_handler(struct widget *widget,
4691 struct input *input,
4692 uint32_t serial,
4693 uint32_t time,
4694 int32_t id,
4695 void *data)
4696{
4697 struct menu *menu = data;
4698
4699 input_ungrab(input);
4700 menu_destroy(menu);
4701}
4702
4703static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004704menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004705{
4706 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004707 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004708 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004709
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004710 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004711
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004712 frame_repaint(menu->frame, cr);
4713 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004714
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004715 theme_set_background_source(menu->window->display->theme,
4716 cr, THEME_FRAME_ACTIVE);
4717 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004718 cairo_fill(cr);
4719
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004720 cairo_select_font_face(cr, "sans",
4721 CAIRO_FONT_SLANT_NORMAL,
4722 CAIRO_FONT_WEIGHT_NORMAL);
4723 cairo_set_font_size(cr, 12);
4724
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004725 for (i = 0; i < menu->count; i++) {
4726 if (i == menu->current) {
4727 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004728 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004729 cairo_fill(cr);
4730 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004731 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004732 cairo_show_text(cr, menu->entries[i]);
4733 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004734 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4735 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004736 cairo_show_text(cr, menu->entries[i]);
4737 }
4738 }
4739
4740 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004741}
4742
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004743static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004744handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004745{
4746 struct window *window = data;
4747 struct menu *menu = window->main_surface->widget->user_data;
4748
4749 input_ungrab(menu->input);
4750 menu_destroy(menu);
4751}
4752
4753static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004754 handle_popup_popup_done,
4755};
4756
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004757static struct menu *
4758create_menu(struct display *display,
4759 struct input *input, uint32_t time,
4760 menu_func_t func, const char **entries, int count,
4761 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004762{
4763 struct window *window;
4764 struct menu *menu;
4765
4766 menu = malloc(sizeof *menu);
4767 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004768 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004769
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004770 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004771 if (!window) {
4772 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004773 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004774 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004775
4776 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004777 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004778 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004779 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004780 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004781 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004782 menu->entries = entries;
4783 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004784 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004785 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004786 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004787 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004788 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004789
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004790 input_ungrab(input);
4791
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004792 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004793 widget_set_enter_handler(menu->widget, menu_enter_handler);
4794 widget_set_leave_handler(menu->widget, menu_leave_handler);
4795 widget_set_motion_handler(menu->widget, menu_motion_handler);
4796 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004797 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004798
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004799 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004800 frame_resize_inside(menu->frame, 200, count * 20);
4801 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4802 window_schedule_resize(window, frame_width(menu->frame),
4803 frame_height(menu->frame));
4804
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004805 return menu;
4806}
4807
4808struct window *
4809window_create_menu(struct display *display,
4810 struct input *input, uint32_t time,
4811 menu_func_t func, const char **entries, int count,
4812 void *user_data)
4813{
4814 struct menu *menu;
4815 menu = create_menu(display, input, time, func, entries, count, user_data);
4816
4817 if (menu == NULL)
4818 return NULL;
4819
4820 return menu->window;
4821}
4822
4823void
4824window_show_menu(struct display *display,
4825 struct input *input, uint32_t time, struct window *parent,
4826 int32_t x, int32_t y,
4827 menu_func_t func, const char **entries, int count)
4828{
4829 struct menu *menu;
4830 struct window *window;
4831 int32_t ix, iy;
4832
4833 menu = create_menu(display, input, time, func, entries, count, parent);
4834
4835 if (menu == NULL)
4836 return;
4837
4838 window = menu->window;
4839
4840 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4841 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4842
4843 window->x = x;
4844 window->y = y;
4845
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004846 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004847
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004848 if (!display->xdg_shell)
4849 return;
4850
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004851 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4852 window->main_surface->surface,
4853 parent->main_surface->surface,
4854 input->seat,
4855 display_get_serial(window->display),
4856 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004857 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004858 fail_on_null(window->xdg_popup);
4859
4860 xdg_popup_set_user_data(window->xdg_popup, window);
4861 xdg_popup_add_listener(window->xdg_popup,
4862 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004863}
4864
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004865void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004866window_set_buffer_type(struct window *window, enum window_buffer_type type)
4867{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004868 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004869}
4870
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004871enum window_buffer_type
4872window_get_buffer_type(struct window *window)
4873{
4874 return window->main_surface->buffer_type;
4875}
4876
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004877void
4878window_set_preferred_format(struct window *window,
4879 enum preferred_format format)
4880{
4881 window->preferred_format = format;
4882}
4883
Pekka Paalanen35e82632013-04-25 13:57:48 +03004884struct widget *
4885window_add_subsurface(struct window *window, void *data,
4886 enum subsurface_mode default_mode)
4887{
4888 struct widget *widget;
4889 struct surface *surface;
4890 struct wl_surface *parent;
4891 struct wl_subcompositor *subcompo = window->display->subcompositor;
4892
Pekka Paalanen35e82632013-04-25 13:57:48 +03004893 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004894 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004895 widget = widget_create(window, surface, data);
4896 wl_list_init(&widget->link);
4897 surface->widget = widget;
4898
4899 parent = window->main_surface->surface;
4900 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4901 surface->surface,
4902 parent);
4903 surface->synchronized = 1;
4904
4905 switch (default_mode) {
4906 case SUBSURFACE_SYNCHRONIZED:
4907 surface->synchronized_default = 1;
4908 break;
4909 case SUBSURFACE_DESYNCHRONIZED:
4910 surface->synchronized_default = 0;
4911 break;
4912 default:
4913 assert(!"bad enum subsurface_mode");
4914 }
4915
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004916 window->resize_needed = 1;
4917 window_schedule_redraw(window);
4918
Pekka Paalanen35e82632013-04-25 13:57:48 +03004919 return widget;
4920}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004921
4922static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004923display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004924 struct wl_output *wl_output,
4925 int x, int y,
4926 int physical_width,
4927 int physical_height,
4928 int subpixel,
4929 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004930 const char *model,
4931 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004932{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004933 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004934
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004935 output->allocation.x = x;
4936 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004937 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004938
4939 if (output->make)
4940 free(output->make);
4941 output->make = strdup(make);
4942
4943 if (output->model)
4944 free(output->model);
4945 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004946}
4947
4948static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004949display_handle_done(void *data,
4950 struct wl_output *wl_output)
4951{
4952}
4953
4954static void
4955display_handle_scale(void *data,
4956 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004957 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004958{
4959 struct output *output = data;
4960
4961 output->scale = scale;
4962}
4963
4964static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004965display_handle_mode(void *data,
4966 struct wl_output *wl_output,
4967 uint32_t flags,
4968 int width,
4969 int height,
4970 int refresh)
4971{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004972 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004973 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004974
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004975 if (flags & WL_OUTPUT_MODE_CURRENT) {
4976 output->allocation.width = width;
4977 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004978 if (display->output_configure_handler)
4979 (*display->output_configure_handler)(
4980 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004981 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004982}
4983
4984static const struct wl_output_listener output_listener = {
4985 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004986 display_handle_mode,
4987 display_handle_done,
4988 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004989};
4990
4991static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004992display_add_output(struct display *d, uint32_t id)
4993{
4994 struct output *output;
4995
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004996 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004997 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004998 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004999 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005000 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005001 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005002 wl_list_insert(d->output_list.prev, &output->link);
5003
5004 wl_output_add_listener(output->output, &output_listener, output);
5005}
5006
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005007static void
5008output_destroy(struct output *output)
5009{
5010 if (output->destroy_handler)
5011 (*output->destroy_handler)(output, output->user_data);
5012
5013 wl_output_destroy(output->output);
5014 wl_list_remove(&output->link);
5015 free(output);
5016}
5017
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005018static void
5019display_destroy_output(struct display *d, uint32_t id)
5020{
5021 struct output *output;
5022
5023 wl_list_for_each(output, &d->output_list, link) {
5024 if (output->server_output_id == id) {
5025 output_destroy(output);
5026 break;
5027 }
5028 }
5029}
5030
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005031void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005032display_set_global_handler(struct display *display,
5033 display_global_handler_t handler)
5034{
5035 struct global *global;
5036
5037 display->global_handler = handler;
5038 if (!handler)
5039 return;
5040
5041 wl_list_for_each(global, &display->global_list, link)
5042 display->global_handler(display,
5043 global->name, global->interface,
5044 global->version, display->user_data);
5045}
5046
5047void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005048display_set_global_handler_remove(struct display *display,
5049 display_global_handler_t remove_handler)
5050{
5051 display->global_handler_remove = remove_handler;
5052 if (!remove_handler)
5053 return;
5054}
5055
5056void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005057display_set_output_configure_handler(struct display *display,
5058 display_output_handler_t handler)
5059{
5060 struct output *output;
5061
5062 display->output_configure_handler = handler;
5063 if (!handler)
5064 return;
5065
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005066 wl_list_for_each(output, &display->output_list, link) {
5067 if (output->allocation.width == 0 &&
5068 output->allocation.height == 0)
5069 continue;
5070
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005071 (*display->output_configure_handler)(output,
5072 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005073 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005074}
5075
5076void
5077output_set_user_data(struct output *output, void *data)
5078{
5079 output->user_data = data;
5080}
5081
5082void *
5083output_get_user_data(struct output *output)
5084{
5085 return output->user_data;
5086}
5087
5088void
5089output_set_destroy_handler(struct output *output,
5090 display_output_handler_t handler)
5091{
5092 output->destroy_handler = handler;
5093 /* FIXME: implement this, once we have way to remove outputs */
5094}
5095
5096void
Scott Moreau4e072362012-09-29 02:03:11 -06005097output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005098{
Scott Moreau4e072362012-09-29 02:03:11 -06005099 struct rectangle allocation = output->allocation;
5100
5101 switch (output->transform) {
5102 case WL_OUTPUT_TRANSFORM_90:
5103 case WL_OUTPUT_TRANSFORM_270:
5104 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5105 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5106 /* Swap width and height */
5107 allocation.width = output->allocation.height;
5108 allocation.height = output->allocation.width;
5109 break;
5110 }
5111
5112 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005113}
5114
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005115struct wl_output *
5116output_get_wl_output(struct output *output)
5117{
5118 return output->output;
5119}
5120
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005121enum wl_output_transform
5122output_get_transform(struct output *output)
5123{
5124 return output->transform;
5125}
5126
Alexander Larssonafd319a2013-05-22 14:41:27 +02005127uint32_t
5128output_get_scale(struct output *output)
5129{
5130 return output->scale;
5131}
5132
Jason Ekstrand738715d2014-04-02 19:53:50 -05005133const char *
5134output_get_make(struct output *output)
5135{
5136 return output->make;
5137}
5138
5139const char *
5140output_get_model(struct output *output)
5141{
5142 return output->model;
5143}
5144
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005145static void
Daniel Stone97f68542012-05-30 16:32:01 +01005146fini_xkb(struct input *input)
5147{
5148 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005149 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005150}
5151
Jasper St. Pierre47f10432013-11-12 14:37:20 -05005152#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005153
Daniel Stone97f68542012-05-30 16:32:01 +01005154static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005155display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005156{
5157 struct input *input;
5158
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005159 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005160 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005161 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005162 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005163 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005164 input->pointer_focus = NULL;
5165 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005166 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005167 wl_list_insert(d->input_list.prev, &input->link);
5168
Daniel Stone37816df2012-05-16 18:45:18 +01005169 wl_seat_add_listener(input->seat, &seat_listener, input);
5170 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005171
Jason Ekstranda669bd52014-04-02 19:53:51 -05005172 if (d->data_device_manager) {
5173 input->data_device =
5174 wl_data_device_manager_get_data_device(d->data_device_manager,
5175 input->seat);
5176 wl_data_device_add_listener(input->data_device,
5177 &data_device_listener,
5178 input);
5179 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005180
5181 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005182
Jonny Lamb06959082014-08-12 14:58:27 +02005183 set_repeat_info(input, 40, 400);
5184
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005185 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5186 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005187 input->repeat_task.run = keyboard_repeat_func;
5188 display_watch_fd(d, input->repeat_timer_fd,
5189 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005190}
5191
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005192static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005193input_destroy(struct input *input)
5194{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005195 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005196 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005197
5198 if (input->drag_offer)
5199 data_offer_destroy(input->drag_offer);
5200
5201 if (input->selection_offer)
5202 data_offer_destroy(input->selection_offer);
5203
kabeer khan6ce67ec2014-10-20 11:55:29 +05305204 if (input->data_device) {
5205 if(input->display->data_device_manager_version >= 2)
5206 wl_data_device_release(input->data_device);
5207 else
5208 wl_data_device_destroy(input->data_device);
5209 }
Rob Bradford08031182013-08-13 20:11:03 +01005210 if (input->display->seat_version >= 3) {
5211 if (input->pointer)
5212 wl_pointer_release(input->pointer);
5213 if (input->keyboard)
5214 wl_keyboard_release(input->keyboard);
5215 }
5216
Daniel Stone97f68542012-05-30 16:32:01 +01005217 fini_xkb(input);
5218
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005219 wl_surface_destroy(input->pointer_surface);
5220
Pekka Paalanene1207c72011-12-16 12:02:09 +02005221 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005222 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005223 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005224 free(input);
5225}
5226
5227static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005228init_workspace_manager(struct display *d, uint32_t id)
5229{
5230 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005231 wl_registry_bind(d->registry, id,
5232 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005233 if (d->workspace_manager != NULL)
5234 workspace_manager_add_listener(d->workspace_manager,
5235 &workspace_manager_listener,
5236 d);
5237}
5238
5239static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005240shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5241{
5242 struct display *d = data;
5243
5244 if (format == WL_SHM_FORMAT_RGB565)
5245 d->has_rgb565 = 1;
5246}
5247
5248struct wl_shm_listener shm_listener = {
5249 shm_format
5250};
5251
5252static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005253xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5254{
5255 xdg_shell_pong(shell, serial);
5256}
5257
5258static const struct xdg_shell_listener xdg_shell_listener = {
5259 xdg_shell_ping,
5260};
5261
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005262#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005263#ifdef static_assert
5264static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5265 "Interface version doesn't match implementation version");
5266#endif
5267
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005268static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005269registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5270 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005271{
5272 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005273 struct global *global;
5274
Brian Lovinbc919262013-08-07 15:34:59 -07005275 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005276 global->name = id;
5277 global->interface = strdup(interface);
5278 global->version = version;
5279 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005280
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005281 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005282 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005283 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005284 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005285 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005286 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005287 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005288 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005289 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005290 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005291 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005292 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305293 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005294 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005295 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305296 &wl_data_device_manager_interface,
5297 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005298 } else if (strcmp(interface, "xdg_shell") == 0) {
5299 d->xdg_shell = wl_registry_bind(registry, id,
5300 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005301 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005302 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005303 } else if (strcmp(interface, "text_cursor_position") == 0) {
5304 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005305 wl_registry_bind(registry, id,
5306 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005307 } else if (strcmp(interface, "workspace_manager") == 0) {
5308 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005309 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5310 d->subcompositor =
5311 wl_registry_bind(registry, id,
5312 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005313 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005314 else if (strcmp(interface, "ivi_application") == 0) {
5315 d->ivi_application =
5316 wl_registry_bind(registry, id,
5317 &ivi_application_interface, 1);
5318 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005319
5320 if (d->global_handler)
5321 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005322}
5323
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005324static void
5325registry_handle_global_remove(void *data, struct wl_registry *registry,
5326 uint32_t name)
5327{
5328 struct display *d = data;
5329 struct global *global;
5330 struct global *tmp;
5331
5332 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5333 if (global->name != name)
5334 continue;
5335
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005336 if (strcmp(global->interface, "wl_output") == 0)
5337 display_destroy_output(d, name);
5338
5339 /* XXX: Should destroy remaining bound globals */
5340
5341 if (d->global_handler_remove)
5342 d->global_handler_remove(d, name, global->interface,
5343 global->version, d->user_data);
5344
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005345 wl_list_remove(&global->link);
5346 free(global->interface);
5347 free(global);
5348 }
5349}
5350
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005351void *
5352display_bind(struct display *display, uint32_t name,
5353 const struct wl_interface *interface, uint32_t version)
5354{
5355 return wl_registry_bind(display->registry, name, interface, version);
5356}
5357
5358static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005359 registry_handle_global,
5360 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005361};
5362
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005363#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005364static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005365init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005366{
5367 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005368 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005369
Rob Clark6396ed32012-03-11 19:48:41 -05005370#ifdef USE_CAIRO_GLESV2
5371# define GL_BIT EGL_OPENGL_ES2_BIT
5372#else
5373# define GL_BIT EGL_OPENGL_BIT
5374#endif
5375
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005376 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005377 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005378 EGL_RED_SIZE, 1,
5379 EGL_GREEN_SIZE, 1,
5380 EGL_BLUE_SIZE, 1,
5381 EGL_ALPHA_SIZE, 1,
5382 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005383 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005384 EGL_NONE
5385 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005386
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005387#ifdef USE_CAIRO_GLESV2
5388 static const EGLint context_attribs[] = {
5389 EGL_CONTEXT_CLIENT_VERSION, 2,
5390 EGL_NONE
5391 };
5392 EGLint api = EGL_OPENGL_ES_API;
5393#else
5394 EGLint *context_attribs = NULL;
5395 EGLint api = EGL_OPENGL_API;
5396#endif
5397
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005398 d->dpy =
5399 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5400 d->display, NULL);
5401
Yuval Fledel45568f62010-12-06 09:18:12 -05005402 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005403 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005404 return -1;
5405 }
5406
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005407 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005408 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005409 return -1;
5410 }
5411
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005412 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5413 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005414 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005415 return -1;
5416 }
5417
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005418 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005419 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005420 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005421 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005422 return -1;
5423 }
5424
Benjamin Franzke0c991632011-09-27 21:57:31 +02005425 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5426 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005427 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005428 return -1;
5429 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005430
5431 return 0;
5432}
5433
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005434static void
5435fini_egl(struct display *display)
5436{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005437 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005438
5439 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5440 EGL_NO_CONTEXT);
5441
5442 eglTerminate(display->dpy);
5443 eglReleaseThread();
5444}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005445#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005446
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005447static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005448init_dummy_surface(struct display *display)
5449{
5450 int len;
5451 void *data;
5452
5453 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005454 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005455 display->dummy_surface =
5456 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5457 1, 1, len);
5458 display->dummy_surface_data = data;
5459}
5460
5461static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005462handle_display_data(struct task *task, uint32_t events)
5463{
5464 struct display *display =
5465 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005466 struct epoll_event ep;
5467 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005468
5469 display->display_fd_events = events;
5470
5471 if (events & EPOLLERR || events & EPOLLHUP) {
5472 display_exit(display);
5473 return;
5474 }
5475
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005476 if (events & EPOLLIN) {
5477 ret = wl_display_dispatch(display->display);
5478 if (ret == -1) {
5479 display_exit(display);
5480 return;
5481 }
5482 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005483
5484 if (events & EPOLLOUT) {
5485 ret = wl_display_flush(display->display);
5486 if (ret == 0) {
5487 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5488 ep.data.ptr = &display->display_task;
5489 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5490 display->display_fd, &ep);
5491 } else if (ret == -1 && errno != EAGAIN) {
5492 display_exit(display);
5493 return;
5494 }
5495 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005496}
5497
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005498static void
5499log_handler(const char *format, va_list args)
5500{
5501 vfprintf(stderr, format, args);
5502}
5503
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005504struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005505display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005506{
5507 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005508
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005509 wl_log_set_handler_client(log_handler);
5510
Peter Huttererf3d62272013-08-08 11:57:05 +10005511 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005512 if (d == NULL)
5513 return NULL;
5514
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005515 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005516 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005517 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005518 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005519 return NULL;
5520 }
5521
Rob Bradford5ab9c752013-07-26 16:29:43 +01005522 d->xkb_context = xkb_context_new(0);
5523 if (d->xkb_context == NULL) {
5524 fprintf(stderr, "Failed to create XKB context\n");
5525 free(d);
5526 return NULL;
5527 }
5528
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005529 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005530 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005531 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005532 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5533 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005534
5535 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005536 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005537 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005538 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005539
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005540 d->workspace = 0;
5541 d->workspace_count = 1;
5542
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005543 d->registry = wl_display_get_registry(d->display);
5544 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005545
Marek Chalupaa519d062014-12-05 13:49:40 +01005546 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005547 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5548 return NULL;
5549 }
5550
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005551#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005552 if (init_egl(d) < 0)
5553 fprintf(stderr, "EGL does not seem to work, "
5554 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005555#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005556
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005557 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005558
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005559 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005560
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005561 wl_list_init(&d->window_list);
5562
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005563 init_dummy_surface(d);
5564
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005565 return d;
5566}
5567
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005568static void
5569display_destroy_outputs(struct display *display)
5570{
5571 struct output *tmp;
5572 struct output *output;
5573
5574 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5575 output_destroy(output);
5576}
5577
Pekka Paalanene1207c72011-12-16 12:02:09 +02005578static void
5579display_destroy_inputs(struct display *display)
5580{
5581 struct input *tmp;
5582 struct input *input;
5583
5584 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5585 input_destroy(input);
5586}
5587
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005588void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005589display_destroy(struct display *display)
5590{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005591 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005592 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5593 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005594
5595 if (!wl_list_empty(&display->deferred_list))
5596 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5597
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005598 cairo_surface_destroy(display->dummy_surface);
5599 free(display->dummy_surface_data);
5600
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005601 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005602 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005603
Daniel Stone97f68542012-05-30 16:32:01 +01005604 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005605
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005606 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005607 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005608
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005609#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005610 if (display->argb_device)
5611 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005612#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005613
Pekka Paalanen35e82632013-04-25 13:57:48 +03005614 if (display->subcompositor)
5615 wl_subcompositor_destroy(display->subcompositor);
5616
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005617 if (display->xdg_shell)
5618 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005619
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005620 if (display->ivi_application)
5621 ivi_application_destroy(display->ivi_application);
5622
Pekka Paalanenc2052982011-12-16 11:41:32 +02005623 if (display->shm)
5624 wl_shm_destroy(display->shm);
5625
5626 if (display->data_device_manager)
5627 wl_data_device_manager_destroy(display->data_device_manager);
5628
5629 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005630 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005631
5632 close(display->epoll_fd);
5633
U. Artie Eoff44874d92012-10-02 21:12:35 -07005634 if (!(display->display_fd_events & EPOLLERR) &&
5635 !(display->display_fd_events & EPOLLHUP))
5636 wl_display_flush(display->display);
5637
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005638 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005639 free(display);
5640}
5641
5642void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005643display_set_user_data(struct display *display, void *data)
5644{
5645 display->user_data = data;
5646}
5647
5648void *
5649display_get_user_data(struct display *display)
5650{
5651 return display->user_data;
5652}
5653
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005654struct wl_display *
5655display_get_display(struct display *display)
5656{
5657 return display->display;
5658}
5659
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005660int
5661display_has_subcompositor(struct display *display)
5662{
5663 if (display->subcompositor)
5664 return 1;
5665
5666 wl_display_roundtrip(display->display);
5667
5668 return display->subcompositor != NULL;
5669}
5670
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005671cairo_device_t *
5672display_get_cairo_device(struct display *display)
5673{
5674 return display->argb_device;
5675}
5676
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005677struct output *
5678display_get_output(struct display *display)
5679{
5680 return container_of(display->output_list.next, struct output, link);
5681}
5682
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005683struct wl_compositor *
5684display_get_compositor(struct display *display)
5685{
5686 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005687}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005688
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005689uint32_t
5690display_get_serial(struct display *display)
5691{
5692 return display->serial;
5693}
5694
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005695EGLDisplay
5696display_get_egl_display(struct display *d)
5697{
5698 return d->dpy;
5699}
5700
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005701struct wl_data_source *
5702display_create_data_source(struct display *display)
5703{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005704 if (display->data_device_manager)
5705 return wl_data_device_manager_create_data_source(display->data_device_manager);
5706 else
5707 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005708}
5709
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005710EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005711display_get_argb_egl_config(struct display *d)
5712{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005713 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005714}
5715
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005716int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005717display_acquire_window_surface(struct display *display,
5718 struct window *window,
5719 EGLContext ctx)
5720{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005721 struct surface *surface = window->main_surface;
5722
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005723 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005724 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005725
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005726 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005727 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005728}
5729
5730void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005731display_release_window_surface(struct display *display,
5732 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005733{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005734 struct surface *surface = window->main_surface;
5735
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005736 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005737 return;
5738
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005739 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005740}
5741
5742void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005743display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005744{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005745 wl_list_insert(&display->deferred_list, &task->link);
5746}
5747
5748void
5749display_watch_fd(struct display *display,
5750 int fd, uint32_t events, struct task *task)
5751{
5752 struct epoll_event ep;
5753
5754 ep.events = events;
5755 ep.data.ptr = task;
5756 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5757}
5758
5759void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005760display_unwatch_fd(struct display *display, int fd)
5761{
5762 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5763}
5764
5765void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005766display_run(struct display *display)
5767{
5768 struct task *task;
5769 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005770 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005771
Pekka Paalanen826d7952011-12-15 10:14:07 +02005772 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005773 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005774 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005775 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005776 struct task, link);
5777 wl_list_remove(&task->link);
5778 task->run(task, 0);
5779 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005780
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005781 wl_display_dispatch_pending(display->display);
5782
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005783 if (!display->running)
5784 break;
5785
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005786 ret = wl_display_flush(display->display);
5787 if (ret < 0 && errno == EAGAIN) {
5788 ep[0].events =
5789 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5790 ep[0].data.ptr = &display->display_task;
5791
5792 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5793 display->display_fd, &ep[0]);
5794 } else if (ret < 0) {
5795 break;
5796 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005797
5798 count = epoll_wait(display->epoll_fd,
5799 ep, ARRAY_LENGTH(ep), -1);
5800 for (i = 0; i < count; i++) {
5801 task = ep[i].data.ptr;
5802 task->run(task, ep[i].events);
5803 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005804 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005805}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005806
5807void
5808display_exit(struct display *display)
5809{
5810 display->running = 0;
5811}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005812
5813void
5814keysym_modifiers_add(struct wl_array *modifiers_map,
5815 const char *name)
5816{
5817 size_t len = strlen(name) + 1;
5818 char *p;
5819
5820 p = wl_array_add(modifiers_map, len);
5821
5822 if (p == NULL)
5823 return;
5824
5825 strncpy(p, name, len);
5826}
5827
5828static xkb_mod_index_t
5829keysym_modifiers_get_index(struct wl_array *modifiers_map,
5830 const char *name)
5831{
5832 xkb_mod_index_t index = 0;
5833 char *p = modifiers_map->data;
5834
5835 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5836 if (strcmp(p, name) == 0)
5837 return index;
5838
5839 index++;
5840 p += strlen(p) + 1;
5841 }
5842
5843 return XKB_MOD_INVALID;
5844}
5845
5846xkb_mod_mask_t
5847keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5848 const char *name)
5849{
5850 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5851
5852 if (index == XKB_MOD_INVALID)
5853 return XKB_MOD_INVALID;
5854
5855 return 1 << index;
5856}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005857
5858void *
5859fail_on_null(void *p)
5860{
5861 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005862 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005863 exit(EXIT_FAILURE);
5864 }
5865
5866 return p;
5867}
5868
5869void *
5870xmalloc(size_t s)
5871{
5872 return fail_on_null(malloc(s));
5873}
5874
Peter Huttererf3d62272013-08-08 11:57:05 +10005875void *
5876xzalloc(size_t s)
5877{
5878 return fail_on_null(zalloc(s));
5879}
5880
Kristian Høgsbergce278412013-07-25 15:20:20 -07005881char *
5882xstrdup(const char *s)
5883{
5884 return fail_on_null(strdup(s));
5885}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005886
5887void *
5888xrealloc(char *p, size_t s)
5889{
5890 return fail_on_null(realloc(p, s));
5891}