blob: c5e6f5afad77ce836dbd1d0ff95b765b1ed9fec1 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030030#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Derek Foreman493d9792015-03-04 16:26:25 -060042#include <stdbool.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040043
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070069#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070070#include "shared/helpers.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050071#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060072#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020073#include "workspaces-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070074#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050075
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050076#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050077
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090078#include <sys/types.h>
79#include "ivi-application-client-protocol.h"
80#define IVI_SURFACE_ID 9000
81
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070082struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030083
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040084struct global {
85 uint32_t name;
86 char *interface;
87 uint32_t version;
88 struct wl_list link;
89};
90
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050091struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050092 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040093 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050094 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030095 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040096 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040097 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060098 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020099 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500100 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900101 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500103 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200104 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200105 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400106 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400107
108 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700109 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400110 struct task display_task;
111
112 int epoll_fd;
113 struct wl_list deferred_list;
114
Pekka Paalanen826d7952011-12-15 10:14:07 +0200115 int running;
116
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400117 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400118 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400119 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500120 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400121
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400122 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400123
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300124 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300125 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400126
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200127 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400128 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800129 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200130
131 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100132
133 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200134
135 uint32_t workspace;
136 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200137
138 /* A hack to get text extents for tooltips */
139 cairo_surface_t *dummy_surface;
140 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200141
142 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100143 int seat_version;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530144 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500145};
146
Rob Bradford7507b572012-05-15 17:55:34 +0100147struct window_output {
148 struct output *output;
149 struct wl_list link;
150};
151
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200152struct toysurface {
153 /*
154 * Prepare the surface for drawing. Makes sure there is a surface
155 * of the right size available for rendering, and returns it.
156 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200157 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200158 * If flags has SURFACE_HINT_RESIZE set, the user is
159 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160 * Returns the Cairo surface to draw to.
161 */
162 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200163 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200164 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200165
166 /*
167 * Post the surface to the server, returning the server allocation
168 * rectangle. The Cairo surface from prepare() must be destroyed
169 * after calling this.
170 */
171 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200172 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200173 struct rectangle *server_allocation);
174
175 /*
176 * Make the toysurface current with the given EGL context.
177 * Returns 0 on success, and negative of failure.
178 */
179 int (*acquire)(struct toysurface *base, EGLContext ctx);
180
181 /*
182 * Release the toysurface from the EGL context, returning control
183 * to Cairo.
184 */
185 void (*release)(struct toysurface *base);
186
187 /*
188 * Destroy the toysurface, including the Cairo surface, any
189 * backing storage, and the Wayland protocol objects.
190 */
191 void (*destroy)(struct toysurface *base);
192};
193
Pekka Paalanen4e373742013-02-13 16:17:13 +0200194struct surface {
195 struct window *window;
196
197 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300198 struct wl_subsurface *subsurface;
199 int synchronized;
200 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200201 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200202 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300203 int redraw_needed;
204 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300205 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200206
207 struct rectangle allocation;
208 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200210 struct wl_region *input_region;
211 struct wl_region *opaque_region;
212
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200213 enum window_buffer_type buffer_type;
214 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200215 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200216
217 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300218
219 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200220};
221
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500222struct window {
223 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100224 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500225 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200226 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400227 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500228 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200229 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500230 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400231 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300232 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400233 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400234 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500235 int custom;
236 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400237
Pekka Paalanen99436862012-11-19 17:15:59 +0200238 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400239
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500240 int fullscreen;
241 int maximized;
242
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200243 enum preferred_format preferred_format;
244
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500245 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500246 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500249 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400250 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200251 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700252 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400253
Pekka Paalanen4e373742013-02-13 16:17:13 +0200254 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500255 struct xdg_surface *xdg_surface;
256 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300257
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700258 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800259 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500260
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900261 struct ivi_surface *ivi_surface;
262
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500263 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500264
Pekka Paalanen35e82632013-04-25 13:57:48 +0300265 /* struct surface::link, contains also main_surface */
266 struct wl_list subsurface_list;
267
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500268 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400269 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500270};
271
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500272struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500273 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200274 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300275 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500276 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400277 struct wl_list link;
278 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500279 widget_resize_handler_t resize_handler;
280 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500281 widget_enter_handler_t enter_handler;
282 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500283 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500284 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700285 widget_touch_down_handler_t touch_down_handler;
286 widget_touch_up_handler_t touch_up_handler;
287 widget_touch_motion_handler_t touch_motion_handler;
288 widget_touch_frame_handler_t touch_frame_handler;
289 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200290 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400291 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500292 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300293 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500294 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000295 /* If this is set to false then no cairo surface will be
296 * created before redrawing the surface. This is useful if the
297 * redraw handler is going to do completely custom rendering
298 * such as using EGL directly */
299 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400300};
301
Rusty Lynch041815a2013-08-08 21:20:38 -0700302struct touch_point {
303 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800304 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct widget *widget;
306 struct wl_list link;
307};
308
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400309struct input {
310 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100311 struct wl_seat *seat;
312 struct wl_pointer *pointer;
313 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700314 struct wl_touch *touch;
315 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400316 struct window *pointer_focus;
317 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700318 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300319 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300320 uint32_t cursor_anim_start;
321 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300322 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400323 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400324 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400325 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400326 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400327 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400328
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500329 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500330 struct widget *grab;
331 uint32_t grab_button;
332
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400333 struct wl_data_device *data_device;
334 struct data_offer *drag_offer;
335 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800336 uint32_t touch_grab;
337 int32_t touch_grab_id;
338 float drag_x, drag_y;
339 struct window *drag_focus;
340 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100341
342 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100343 struct xkb_keymap *keymap;
344 struct xkb_state *state;
345 xkb_mod_mask_t control_mask;
346 xkb_mod_mask_t alt_mask;
347 xkb_mod_mask_t shift_mask;
348 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400349
Jonny Lamb06959082014-08-12 14:58:27 +0200350 int32_t repeat_rate_sec;
351 int32_t repeat_rate_nsec;
352 int32_t repeat_delay_sec;
353 int32_t repeat_delay_nsec;
354
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400355 struct task repeat_task;
356 int repeat_timer_fd;
357 uint32_t repeat_sym;
358 uint32_t repeat_key;
359 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400360};
361
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500362struct output {
363 struct display *display;
364 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800365 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500366 struct rectangle allocation;
367 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600368 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200369 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500370 char *make;
371 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200372
373 display_output_handler_t destroy_handler;
374 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500375};
376
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500377struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500378 struct widget *widget;
379 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500380 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800381
382 uint32_t last_time;
383 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800384 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500385};
386
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500387struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400388 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500389 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500390 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500391 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700392 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500393 const char **entries;
394 uint32_t time;
395 int current;
396 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400397 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500398 menu_func_t func;
399};
400
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300401struct tooltip {
402 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300403 struct widget *widget;
404 char *entry;
405 struct task tooltip_task;
406 int tooltip_fd;
407 float x, y;
408};
409
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700410struct shm_pool {
411 struct wl_shm_pool *pool;
412 size_t size;
413 size_t used;
414 void *data;
415};
416
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400417enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300418 CURSOR_DEFAULT = 100,
419 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400420};
421
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200422static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423
Pekka Paalanen97777442013-05-22 10:20:05 +0300424/* #define DEBUG */
425
426#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300427
428static void
429debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
430__attribute__ ((format (printf, 4, 5)));
431
432static void
433debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
434{
435 va_list ap;
436 struct timeval tv;
437
438 gettimeofday(&tv, NULL);
439 fprintf(stderr, "%8ld.%03ld ",
440 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
441
442 if (proxy)
443 fprintf(stderr, "%s@%d ",
444 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
445
446 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
447 fprintf(stderr, "%s ", func);
448
449 va_start(ap, fmt);
450 vfprintf(stderr, fmt, ap);
451 va_end(ap);
452}
453
454#define DBG(fmt, ...) \
455 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
456
457#define DBG_OBJ(obj, fmt, ...) \
458 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
459
460#else
461
462#define DBG(...) do {} while (0)
463#define DBG_OBJ(...) do {} while (0)
464
465#endif
466
Alexander Larsson1818e312013-05-22 14:41:31 +0200467static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200468surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200469{
470 int32_t tmp;
471
472 switch (buffer_transform) {
473 case WL_OUTPUT_TRANSFORM_90:
474 case WL_OUTPUT_TRANSFORM_270:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
476 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
477 tmp = *width;
478 *width = *height;
479 *height = tmp;
480 break;
481 default:
482 break;
483 }
484
485 *width *= buffer_scale;
486 *height *= buffer_scale;
487}
488
489static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200490buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200491{
492 int32_t tmp;
493
494 switch (buffer_transform) {
495 case WL_OUTPUT_TRANSFORM_90:
496 case WL_OUTPUT_TRANSFORM_270:
497 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
498 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
499 tmp = *width;
500 *width = *height;
501 *height = tmp;
502 break;
503 default:
504 break;
505 }
506
507 *width /= buffer_scale;
508 *height /= buffer_scale;
509}
510
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500511#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400512
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200513struct egl_window_surface {
514 struct toysurface base;
515 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100516 struct display *display;
517 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518 struct wl_egl_window *egl_window;
519 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520};
521
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522static struct egl_window_surface *
523to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100524{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200525 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100526}
527
528static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200529egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200530 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200531 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100532{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533 struct egl_window_surface *surface = to_egl_window_surface(base);
534
Alexander Larsson1818e312013-05-22 14:41:31 +0200535 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
536
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200537 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
538 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
539
540 return cairo_surface_reference(surface->cairo_surface);
541}
542
543static void
544egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200545 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200546 struct rectangle *server_allocation)
547{
548 struct egl_window_surface *surface = to_egl_window_surface(base);
549
550 cairo_gl_surface_swapbuffers(surface->cairo_surface);
551 wl_egl_window_get_attached_size(surface->egl_window,
552 &server_allocation->width,
553 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200554
555 buffer_to_surface_size (buffer_transform, buffer_scale,
556 &server_allocation->width,
557 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200558}
559
560static int
561egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
562{
563 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200564 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100565
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200566 device = cairo_surface_get_device(surface->cairo_surface);
567 if (!device)
568 return -1;
569
570 if (!ctx) {
571 if (device == surface->display->argb_device)
572 ctx = surface->display->argb_ctx;
573 else
574 assert(0);
575 }
576
577 cairo_device_flush(device);
578 cairo_device_acquire(device);
579 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
580 surface->egl_surface, ctx))
581 fprintf(stderr, "failed to make surface current\n");
582
583 return 0;
584}
585
586static void
587egl_window_surface_release(struct toysurface *base)
588{
589 struct egl_window_surface *surface = to_egl_window_surface(base);
590 cairo_device_t *device;
591
592 device = cairo_surface_get_device(surface->cairo_surface);
593 if (!device)
594 return;
595
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200596 if (!eglMakeCurrent(surface->display->dpy,
597 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200598 fprintf(stderr, "failed to make context current\n");
599
600 cairo_device_release(device);
601}
602
603static void
604egl_window_surface_destroy(struct toysurface *base)
605{
606 struct egl_window_surface *surface = to_egl_window_surface(base);
607 struct display *d = surface->display;
608
609 cairo_surface_destroy(surface->cairo_surface);
610 eglDestroySurface(d->dpy, surface->egl_surface);
611 wl_egl_window_destroy(surface->egl_window);
612 surface->surface = NULL;
613
614 free(surface);
615}
616
617static struct toysurface *
618egl_window_surface_create(struct display *display,
619 struct wl_surface *wl_surface,
620 uint32_t flags,
621 struct rectangle *rectangle)
622{
623 struct egl_window_surface *surface;
624
Pekka Paalanenb3627362012-11-19 17:16:00 +0200625 if (display->dpy == EGL_NO_DISPLAY)
626 return NULL;
627
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200628 surface = calloc(1, sizeof *surface);
629 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100630 return NULL;
631
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200632 surface->base.prepare = egl_window_surface_prepare;
633 surface->base.swap = egl_window_surface_swap;
634 surface->base.acquire = egl_window_surface_acquire;
635 surface->base.release = egl_window_surface_release;
636 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100637
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200638 surface->display = display;
639 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400640
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200641 surface->egl_window = wl_egl_window_create(surface->surface,
642 rectangle->width,
643 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100644
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100645 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100646 weston_platform_create_egl_surface(display->dpy,
647 display->argb_config,
648 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200650 surface->cairo_surface =
651 cairo_gl_surface_create_for_egl(display->argb_device,
652 surface->egl_surface,
653 rectangle->width,
654 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100655
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200656 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100657}
658
Pekka Paalanenb3627362012-11-19 17:16:00 +0200659#else
660
661static struct toysurface *
662egl_window_surface_create(struct display *display,
663 struct wl_surface *wl_surface,
664 uint32_t flags,
665 struct rectangle *rectangle)
666{
667 return NULL;
668}
669
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670#endif
671
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200672struct shm_surface_data {
673 struct wl_buffer *buffer;
674 struct shm_pool *pool;
675};
676
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400677struct wl_buffer *
678display_get_buffer_for_surface(struct display *display,
679 cairo_surface_t *surface)
680{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200681 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200683 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400684
685 return data->buffer;
686}
687
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500688static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700689shm_pool_destroy(struct shm_pool *pool);
690
691static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400692shm_surface_data_destroy(void *p)
693{
694 struct shm_surface_data *data = p;
695
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200696 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700697 if (data->pool)
698 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300699
700 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400701}
702
Kristian Høgsberg16626282012-04-03 11:21:27 -0400703static struct wl_shm_pool *
704make_shm_pool(struct display *display, int size, void **data)
705{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400706 struct wl_shm_pool *pool;
707 int fd;
708
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300709 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400710 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300711 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
712 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400713 return NULL;
714 }
715
716 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400717 if (*data == MAP_FAILED) {
718 fprintf(stderr, "mmap failed: %m\n");
719 close(fd);
720 return NULL;
721 }
722
723 pool = wl_shm_create_pool(display->shm, fd, size);
724
725 close(fd);
726
727 return pool;
728}
729
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700730static struct shm_pool *
731shm_pool_create(struct display *display, size_t size)
732{
733 struct shm_pool *pool = malloc(sizeof *pool);
734
735 if (!pool)
736 return NULL;
737
738 pool->pool = make_shm_pool(display, size, &pool->data);
739 if (!pool->pool) {
740 free(pool);
741 return NULL;
742 }
743
744 pool->size = size;
745 pool->used = 0;
746
747 return pool;
748}
749
750static void *
751shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
752{
753 if (pool->used + size > pool->size)
754 return NULL;
755
756 *offset = pool->used;
757 pool->used += size;
758
759 return (char *) pool->data + *offset;
760}
761
762/* destroy the pool. this does not unmap the memory though */
763static void
764shm_pool_destroy(struct shm_pool *pool)
765{
766 munmap(pool->data, pool->size);
767 wl_shm_pool_destroy(pool->pool);
768 free(pool);
769}
770
771/* Start allocating from the beginning of the pool again */
772static void
773shm_pool_reset(struct shm_pool *pool)
774{
775 pool->used = 0;
776}
777
778static int
779data_length_for_shm_surface(struct rectangle *rect)
780{
781 int stride;
782
783 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
784 rect->width);
785 return stride * rect->height;
786}
787
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500788static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700789display_create_shm_surface_from_pool(struct display *display,
790 struct rectangle *rectangle,
791 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400792{
793 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400794 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400795 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200796 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700797 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400798 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400799
800 data = malloc(sizeof *data);
801 if (data == NULL)
802 return NULL;
803
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200804 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
805 cairo_format = CAIRO_FORMAT_RGB16_565;
806 else
807 cairo_format = CAIRO_FORMAT_ARGB32;
808
809 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700810 length = stride * rectangle->height;
811 data->pool = NULL;
812 map = shm_pool_allocate(pool, length, &offset);
813
814 if (!map) {
815 free(data);
816 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400817 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400818
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400819 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200820 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821 rectangle->width,
822 rectangle->height,
823 stride);
824
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200825 cairo_surface_set_user_data(surface, &shm_surface_data_key,
826 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400827
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200828 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
829 format = WL_SHM_FORMAT_RGB565;
830 else {
831 if (flags & SURFACE_OPAQUE)
832 format = WL_SHM_FORMAT_XRGB8888;
833 else
834 format = WL_SHM_FORMAT_ARGB8888;
835 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400836
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200837 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
838 rectangle->width,
839 rectangle->height,
840 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400841
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700842 return surface;
843}
844
845static cairo_surface_t *
846display_create_shm_surface(struct display *display,
847 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200848 struct shm_pool *alternate_pool,
849 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700850{
851 struct shm_surface_data *data;
852 struct shm_pool *pool;
853 cairo_surface_t *surface;
854
Pekka Paalanen99436862012-11-19 17:15:59 +0200855 if (alternate_pool) {
856 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700857 surface = display_create_shm_surface_from_pool(display,
858 rectangle,
859 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200860 alternate_pool);
861 if (surface) {
862 data = cairo_surface_get_user_data(surface,
863 &shm_surface_data_key);
864 goto out;
865 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700866 }
867
868 pool = shm_pool_create(display,
869 data_length_for_shm_surface(rectangle));
870 if (!pool)
871 return NULL;
872
873 surface =
874 display_create_shm_surface_from_pool(display, rectangle,
875 flags, pool);
876
877 if (!surface) {
878 shm_pool_destroy(pool);
879 return NULL;
880 }
881
882 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200883 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700884 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400885
Pekka Paalanen99436862012-11-19 17:15:59 +0200886out:
887 if (data_ret)
888 *data_ret = data;
889
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890 return surface;
891}
892
nobled7b87cb02011-02-01 18:51:47 +0000893static int
894check_size(struct rectangle *rect)
895{
896 if (rect->width && rect->height)
897 return 0;
898
899 fprintf(stderr, "tried to create surface of "
900 "width: %d, height: %d\n", rect->width, rect->height);
901 return -1;
902}
903
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400904cairo_surface_t *
905display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400907 struct rectangle *rectangle,
908 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909{
nobled7b87cb02011-02-01 18:51:47 +0000910 if (check_size(rectangle) < 0)
911 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200912
913 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200914 return display_create_shm_surface(display, rectangle, flags,
915 NULL, NULL);
916}
917
Pekka Paalanena4eda732012-11-19 17:16:02 +0200918struct shm_surface_leaf {
919 cairo_surface_t *cairo_surface;
920 /* 'data' is automatically destroyed, when 'cairo_surface' is */
921 struct shm_surface_data *data;
922
923 struct shm_pool *resize_pool;
924 int busy;
925};
926
927static void
928shm_surface_leaf_release(struct shm_surface_leaf *leaf)
929{
930 if (leaf->cairo_surface)
931 cairo_surface_destroy(leaf->cairo_surface);
932 /* leaf->data already destroyed via cairo private */
933
934 if (leaf->resize_pool)
935 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200936
937 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938}
939
Pekka Paalanenaef02542013-04-25 13:57:47 +0300940#define MAX_LEAVES 3
941
Pekka Paalanen99436862012-11-19 17:15:59 +0200942struct shm_surface {
943 struct toysurface base;
944 struct display *display;
945 struct wl_surface *surface;
946 uint32_t flags;
947 int dx, dy;
948
Pekka Paalanenaef02542013-04-25 13:57:47 +0300949 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200950 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200951};
952
953static struct shm_surface *
954to_shm_surface(struct toysurface *base)
955{
956 return container_of(base, struct shm_surface, base);
957}
958
Pekka Paalanena4eda732012-11-19 17:16:02 +0200959static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300960shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
961{
962#ifdef DEBUG
963 struct shm_surface_leaf *leaf;
964 char bufs[MAX_LEAVES + 1];
965 int i;
966
967 for (i = 0; i < MAX_LEAVES; i++) {
968 leaf = &surface->leaf[i];
969
970 if (leaf->busy)
971 bufs[i] = 'b';
972 else if (leaf->cairo_surface)
973 bufs[i] = 'a';
974 else
975 bufs[i] = ' ';
976 }
977
978 bufs[MAX_LEAVES] = '\0';
979 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
980#endif
981}
982
983static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200984shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
985{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200986 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300987 struct shm_surface_leaf *leaf;
988 int i;
989 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300990
991 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200992
Pekka Paalanenaef02542013-04-25 13:57:47 +0300993 for (i = 0; i < MAX_LEAVES; i++) {
994 leaf = &surface->leaf[i];
995 if (leaf->data && leaf->data->buffer == buffer) {
996 leaf->busy = 0;
997 break;
998 }
999 }
1000 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001001
Pekka Paalanenaef02542013-04-25 13:57:47 +03001002 /* Leave one free leaf with storage, release others */
1003 free_found = 0;
1004 for (i = 0; i < MAX_LEAVES; i++) {
1005 leaf = &surface->leaf[i];
1006
1007 if (!leaf->cairo_surface || leaf->busy)
1008 continue;
1009
1010 if (!free_found)
1011 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001012 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001013 shm_surface_leaf_release(leaf);
1014 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001015
Pekka Paalanen97777442013-05-22 10:20:05 +03001016 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001017}
1018
1019static const struct wl_buffer_listener shm_surface_buffer_listener = {
1020 shm_surface_buffer_release
1021};
1022
Pekka Paalanen99436862012-11-19 17:15:59 +02001023static cairo_surface_t *
1024shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001025 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001026 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001027{
Pekka Paalanenec076692012-11-30 13:37:27 +02001028 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001029 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001030 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001031 struct shm_surface_leaf *leaf = NULL;
1032 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001033
1034 surface->dx = dx;
1035 surface->dy = dy;
1036
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001037 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001038 for (i = 0; i < MAX_LEAVES; i++) {
1039 if (surface->leaf[i].busy)
1040 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001041
Pekka Paalanenaef02542013-04-25 13:57:47 +03001042 if (!leaf || surface->leaf[i].cairo_surface)
1043 leaf = &surface->leaf[i];
1044 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001045 DBG_OBJ(surface->surface, "pick leaf %d\n",
1046 (int)(leaf - &surface->leaf[0]));
1047
Pekka Paalanenaef02542013-04-25 13:57:47 +03001048 if (!leaf) {
1049 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001050 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001051 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001052 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001053 }
1054
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055 if (!resize_hint && leaf->resize_pool) {
1056 cairo_surface_destroy(leaf->cairo_surface);
1057 leaf->cairo_surface = NULL;
1058 shm_pool_destroy(leaf->resize_pool);
1059 leaf->resize_pool = NULL;
1060 }
1061
Alexander Larsson1818e312013-05-22 14:41:31 +02001062 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1063
Pekka Paalanena4eda732012-11-19 17:16:02 +02001064 if (leaf->cairo_surface &&
1065 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1066 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001067 goto out;
1068
Pekka Paalanena4eda732012-11-19 17:16:02 +02001069 if (leaf->cairo_surface)
1070 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001071
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001072#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001073 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001074 /* Create a big pool to allocate from, while continuously
1075 * resizing. Mmapping a new pool in the server
1076 * is relatively expensive, so reusing a pool performs
1077 * better, but may temporarily reserve unneeded memory.
1078 */
1079 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001080 leaf->resize_pool = shm_pool_create(surface->display,
1081 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001082 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001083#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001084
Alexander Larsson1818e312013-05-22 14:41:31 +02001085 rect.width = width;
1086 rect.height = height;
1087
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001089 display_create_shm_surface(surface->display, &rect,
1090 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001091 leaf->resize_pool,
1092 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001093 if (!leaf->cairo_surface)
1094 return NULL;
1095
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001097 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001098
1099out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 surface->current = leaf;
1101
1102 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001103}
1104
1105static void
1106shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001107 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001108 struct rectangle *server_allocation)
1109{
1110 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001112
1113 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001114 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001115 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001117
Alexander Larsson1818e312013-05-22 14:41:31 +02001118 buffer_to_surface_size (buffer_transform, buffer_scale,
1119 &server_allocation->width,
1120 &server_allocation->height);
1121
Pekka Paalanena4eda732012-11-19 17:16:02 +02001122 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001123 surface->dx, surface->dy);
1124 wl_surface_damage(surface->surface, 0, 0,
1125 server_allocation->width, server_allocation->height);
1126 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127
Pekka Paalanen71233882013-04-25 13:57:53 +03001128 DBG_OBJ(surface->surface, "leaf %d busy\n",
1129 (int)(leaf - &surface->leaf[0]));
1130
Pekka Paalanena4eda732012-11-19 17:16:02 +02001131 leaf->busy = 1;
1132 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001133}
1134
1135static int
1136shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1137{
1138 return -1;
1139}
1140
1141static void
1142shm_surface_release(struct toysurface *base)
1143{
1144}
1145
1146static void
1147shm_surface_destroy(struct toysurface *base)
1148{
1149 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001150 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001151
Pekka Paalanenaef02542013-04-25 13:57:47 +03001152 for (i = 0; i < MAX_LEAVES; i++)
1153 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001154
1155 free(surface);
1156}
1157
1158static struct toysurface *
1159shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1160 uint32_t flags, struct rectangle *rectangle)
1161{
1162 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001163 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001164
Brian Lovinbc919262013-08-07 15:34:59 -07001165 surface = xmalloc(sizeof *surface);
1166 memset(surface, 0, sizeof *surface);
1167
Pekka Paalanen99436862012-11-19 17:15:59 +02001168 if (!surface)
1169 return NULL;
1170
1171 surface->base.prepare = shm_surface_prepare;
1172 surface->base.swap = shm_surface_swap;
1173 surface->base.acquire = shm_surface_acquire;
1174 surface->base.release = shm_surface_release;
1175 surface->base.destroy = shm_surface_destroy;
1176
1177 surface->display = display;
1178 surface->surface = wl_surface;
1179 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001180
1181 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001182}
1183
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001184/*
1185 * The following correspondences between file names and cursors was copied
1186 * from: https://bugs.kde.org/attachment.cgi?id=67313
1187 */
1188
1189static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001190 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001191 "sw-resize",
1192 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001193};
1194
1195static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001196 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001197 "se-resize",
1198 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001199};
1200
1201static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001202 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001203 "s-resize",
1204 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001205};
1206
1207static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001209 "closedhand",
1210 "208530c400c041818281048008011002"
1211};
1212
1213static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001214 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215 "default",
1216 "top_left_arrow",
1217 "left-arrow"
1218};
1219
1220static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "w-resize",
1223 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "e-resize",
1229 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001234 "nw-resize",
1235 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236};
1237
1238static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001240 "ne-resize",
1241 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242};
1243
1244static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001246 "n-resize",
1247 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248};
1249
1250static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001251 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252 "ibeam",
1253 "text"
1254};
1255
1256static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001257 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258 "pointer",
1259 "pointing_hand",
1260 "e29285e634086352946a0e7090d73106"
1261};
1262
1263static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001264 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001265 "wait",
1266 "0426c94ea35c87780ff01dc239897213"
1267};
1268
1269struct cursor_alternatives {
1270 const char **names;
1271 size_t count;
1272};
1273
1274static const struct cursor_alternatives cursors[] = {
1275 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1276 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1277 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1278 {grabbings, ARRAY_LENGTH(grabbings)},
1279 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1280 {left_sides, ARRAY_LENGTH(left_sides)},
1281 {right_sides, ARRAY_LENGTH(right_sides)},
1282 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1283 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1284 {top_sides, ARRAY_LENGTH(top_sides)},
1285 {xterms, ARRAY_LENGTH(xterms)},
1286 {hand1s, ARRAY_LENGTH(hand1s)},
1287 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001288};
1289
1290static void
1291create_cursors(struct display *display)
1292{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001293 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001294 struct weston_config *config;
1295 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001296 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001297 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001298 unsigned int i, j;
1299 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001300
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001301 config_file = weston_config_get_name_from_env();
1302 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001303 s = weston_config_get_section(config, "shell", NULL, NULL);
1304 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1305 weston_config_section_get_int(s, "cursor-size", &size, 32);
1306 weston_config_destroy(config);
1307
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001308 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001309 if (!display->cursor_theme) {
1310 fprintf(stderr, "could not load theme '%s'\n", theme);
1311 return;
1312 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001313 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001314 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001315 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001316
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001317 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001318 cursor = NULL;
1319 for (j = 0; !cursor && j < cursors[i].count; ++j)
1320 cursor = wl_cursor_theme_get_cursor(
1321 display->cursor_theme, cursors[i].names[j]);
1322
1323 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001324 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001325 cursors[i].names[0]);
1326
1327 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001328 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001329}
1330
1331static void
1332destroy_cursors(struct display *display)
1333{
1334 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001335 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001336}
1337
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001338struct wl_cursor_image *
1339display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001340{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001341 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001342
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001343 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001344}
1345
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001346static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001347surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001348{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001349 if (!surface->cairo_surface)
1350 return;
1351
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001352 if (surface->opaque_region) {
1353 wl_surface_set_opaque_region(surface->surface,
1354 surface->opaque_region);
1355 wl_region_destroy(surface->opaque_region);
1356 surface->opaque_region = NULL;
1357 }
1358
1359 if (surface->input_region) {
1360 wl_surface_set_input_region(surface->surface,
1361 surface->input_region);
1362 wl_region_destroy(surface->input_region);
1363 surface->input_region = NULL;
1364 }
1365
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001366 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001367 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001368 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001369
Pekka Paalanen89dee002013-02-13 16:17:20 +02001370 cairo_surface_destroy(surface->cairo_surface);
1371 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001372}
1373
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001374int
1375window_has_focus(struct window *window)
1376{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001377 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001378}
1379
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001380static void
1381window_close(struct window *window)
1382{
1383 if (window->close_handler)
1384 window->close_handler(window->user_data);
1385 else
1386 display_exit(window->display);
1387}
1388
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001389struct display *
1390window_get_display(struct window *window)
1391{
1392 return window->display;
1393}
1394
Pekka Paalanen89dee002013-02-13 16:17:20 +02001395static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001396handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1397 int32_t width, int32_t height)
1398{
1399 struct window *window = data;
1400
1401 window_schedule_resize(window, width, height);
1402}
1403
1404static const struct ivi_surface_listener ivi_surface_listener = {
1405 handle_ivi_surface_configure,
1406};
1407
1408static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001409surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001410{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001411 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001412 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001413
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001414 if (!surface->toysurface && display->dpy &&
1415 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001416 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001417 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001419 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001420 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001421 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001422
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001423 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001425 surface->surface,
1426 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001427
Pekka Paalanen89dee002013-02-13 16:17:20 +02001428 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001429 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001430 allocation.width, allocation.height, flags,
1431 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001432}
1433
1434static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001435window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001436{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001437 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001438 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001439
Pekka Paalanenec076692012-11-30 13:37:27 +02001440 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001441 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001442
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001443 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1444 flags |= SURFACE_HINT_RGB565;
1445
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001446 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001447}
1448
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001449int
1450window_get_buffer_transform(struct window *window)
1451{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001452 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001453}
1454
1455void
1456window_set_buffer_transform(struct window *window,
1457 enum wl_output_transform transform)
1458{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001459 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001460 wl_surface_set_buffer_transform(window->main_surface->surface,
1461 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001462}
1463
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001464void
1465window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001466 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001467{
1468 window->main_surface->buffer_scale = scale;
1469 wl_surface_set_buffer_scale(window->main_surface->surface,
1470 scale);
1471}
1472
1473uint32_t
1474window_get_buffer_scale(struct window *window)
1475{
1476 return window->main_surface->buffer_scale;
1477}
1478
Alexander Larssond68f5232013-05-22 14:41:33 +02001479uint32_t
1480window_get_output_scale(struct window *window)
1481{
1482 struct window_output *window_output;
1483 struct window_output *window_output_tmp;
1484 int scale = 1;
1485
1486 wl_list_for_each_safe(window_output, window_output_tmp,
1487 &window->window_output_list, link) {
1488 if (window_output->output->scale > scale)
1489 scale = window_output->output->scale;
1490 }
1491
1492 return scale;
1493}
1494
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001495static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001496
Pekka Paalanen4e373742013-02-13 16:17:13 +02001497static void
1498surface_destroy(struct surface *surface)
1499{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001500 if (surface->frame_cb)
1501 wl_callback_destroy(surface->frame_cb);
1502
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001503 if (surface->input_region)
1504 wl_region_destroy(surface->input_region);
1505
1506 if (surface->opaque_region)
1507 wl_region_destroy(surface->opaque_region);
1508
Pekka Paalanen35e82632013-04-25 13:57:48 +03001509 if (surface->subsurface)
1510 wl_subsurface_destroy(surface->subsurface);
1511
Pekka Paalanen4e373742013-02-13 16:17:13 +02001512 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001513
1514 if (surface->toysurface)
1515 surface->toysurface->destroy(surface->toysurface);
1516
Pekka Paalanen35e82632013-04-25 13:57:48 +03001517 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001518 free(surface);
1519}
1520
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001521void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001522window_destroy(struct window *window)
1523{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001524 struct display *display = window->display;
1525 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001526 struct window_output *window_output;
1527 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001528
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001529 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001530
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001531 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001532 if (input->touch_focus == window)
1533 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001534 if (input->pointer_focus == window)
1535 input->pointer_focus = NULL;
1536 if (input->keyboard_focus == window)
1537 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001538 if (input->focus_widget &&
1539 input->focus_widget->window == window)
1540 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001541 }
1542
Rob Bradford7507b572012-05-15 17:55:34 +01001543 wl_list_for_each_safe(window_output, window_output_tmp,
1544 &window->window_output_list, link) {
1545 free (window_output);
1546 }
1547
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001548 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001549 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001550
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001551 if (window->xdg_surface)
1552 xdg_surface_destroy(window->xdg_surface);
1553 if (window->xdg_popup)
1554 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001555
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001556 if (window->ivi_surface)
1557 ivi_surface_destroy(window->ivi_surface);
1558
Pekka Paalanen4e373742013-02-13 16:17:13 +02001559 surface_destroy(window->main_surface);
1560
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001561 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001562
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001563 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001564 free(window);
1565}
1566
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001567static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001568widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001569{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001570 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001571
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001572 wl_list_for_each(child, &widget->child_list, link) {
1573 target = widget_find_widget(child, x, y);
1574 if (target)
1575 return target;
1576 }
1577
1578 if (widget->allocation.x <= x &&
1579 x < widget->allocation.x + widget->allocation.width &&
1580 widget->allocation.y <= y &&
1581 y < widget->allocation.y + widget->allocation.height) {
1582 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001583 }
1584
1585 return NULL;
1586}
1587
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001588static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001589window_find_widget(struct window *window, int32_t x, int32_t y)
1590{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001591 struct surface *surface;
1592 struct widget *widget;
1593
1594 wl_list_for_each(surface, &window->subsurface_list, link) {
1595 widget = widget_find_widget(surface->widget, x, y);
1596 if (widget)
1597 return widget;
1598 }
1599
1600 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001601}
1602
1603static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001604widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001605{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001606 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001607
Peter Huttererf3d62272013-08-08 11:57:05 +10001608 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001609 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001610 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001611 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001612 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001613 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001614 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001615 widget->tooltip = NULL;
1616 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001617 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001618 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619
1620 return widget;
1621}
1622
1623struct widget *
1624window_add_widget(struct window *window, void *data)
1625{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001626 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001627
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001628 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001629 wl_list_init(&widget->link);
1630 window->main_surface->widget = widget;
1631
1632 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001633}
1634
1635struct widget *
1636widget_add_widget(struct widget *parent, void *data)
1637{
1638 struct widget *widget;
1639
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001640 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001641 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001642
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001643 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001644}
1645
1646void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001647widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001648{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001649 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001650 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001651 struct input *input;
1652
Pekka Paalanen35e82632013-04-25 13:57:48 +03001653 /* Destroy the sub-surface along with the root widget */
1654 if (surface->widget == widget && surface->subsurface)
1655 surface_destroy(widget->surface);
1656
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001657 if (widget->tooltip)
1658 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001659
Pekka Paalanene156fb62012-01-19 13:51:38 +02001660 wl_list_for_each(input, &display->input_list, link) {
1661 if (input->focus_widget == widget)
1662 input->focus_widget = NULL;
1663 }
1664
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001665 wl_list_remove(&widget->link);
1666 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001667}
1668
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001669void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001670widget_set_default_cursor(struct widget *widget, int cursor)
1671{
1672 widget->default_cursor = cursor;
1673}
1674
1675void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001676widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001677{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001678 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001679}
1680
1681void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001682widget_set_size(struct widget *widget, int32_t width, int32_t height)
1683{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001684 widget->allocation.width = width;
1685 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001686}
1687
1688void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001689widget_set_allocation(struct widget *widget,
1690 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001691{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001692 widget->allocation.x = x;
1693 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001694 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001695}
1696
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001697void
1698widget_set_transparent(struct widget *widget, int transparent)
1699{
1700 widget->opaque = !transparent;
1701}
1702
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001704widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001705{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001706 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001707}
1708
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001709static cairo_surface_t *
1710widget_get_cairo_surface(struct widget *widget)
1711{
1712 struct surface *surface = widget->surface;
1713 struct window *window = widget->window;
1714
Neil Roberts97b747c2013-12-19 16:17:12 +00001715 assert(widget->use_cairo);
1716
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001717 if (!surface->cairo_surface) {
1718 if (surface == window->main_surface)
1719 window_create_main_surface(window);
1720 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001721 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001722 }
1723
1724 return surface->cairo_surface;
1725}
1726
Alexander Larsson15901f02013-05-22 14:41:25 +02001727static void
1728widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1729{
1730 struct surface *surface = widget->surface;
1731 double angle;
1732 cairo_matrix_t m;
1733 enum wl_output_transform transform;
1734 int surface_width, surface_height;
1735 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001736 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001737
1738 surface_width = surface->allocation.width;
1739 surface_height = surface->allocation.height;
1740
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001741 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001742 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001743
Alexander Larsson15901f02013-05-22 14:41:25 +02001744 switch (transform) {
1745 case WL_OUTPUT_TRANSFORM_FLIPPED:
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1748 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1749 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1750 break;
1751 default:
1752 cairo_matrix_init_identity(&m);
1753 break;
1754 }
1755
1756 switch (transform) {
1757 case WL_OUTPUT_TRANSFORM_NORMAL:
1758 default:
1759 angle = 0;
1760 translate_x = 0;
1761 translate_y = 0;
1762 break;
1763 case WL_OUTPUT_TRANSFORM_FLIPPED:
1764 angle = 0;
1765 translate_x = surface_width;
1766 translate_y = 0;
1767 break;
1768 case WL_OUTPUT_TRANSFORM_90:
1769 angle = M_PI_2;
1770 translate_x = surface_height;
1771 translate_y = 0;
1772 break;
1773 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1774 angle = M_PI_2;
1775 translate_x = surface_height;
1776 translate_y = surface_width;
1777 break;
1778 case WL_OUTPUT_TRANSFORM_180:
1779 angle = M_PI;
1780 translate_x = surface_width;
1781 translate_y = surface_height;
1782 break;
1783 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1784 angle = M_PI;
1785 translate_x = 0;
1786 translate_y = surface_height;
1787 break;
1788 case WL_OUTPUT_TRANSFORM_270:
1789 angle = M_PI + M_PI_2;
1790 translate_x = 0;
1791 translate_y = surface_width;
1792 break;
1793 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1794 angle = M_PI + M_PI_2;
1795 translate_x = 0;
1796 translate_y = 0;
1797 break;
1798 }
1799
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001800 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001801 cairo_translate(cr, translate_x, translate_y);
1802 cairo_rotate(cr, angle);
1803 cairo_transform(cr, &m);
1804}
1805
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001806cairo_t *
1807widget_cairo_create(struct widget *widget)
1808{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001809 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001810 cairo_surface_t *cairo_surface;
1811 cairo_t *cr;
1812
1813 cairo_surface = widget_get_cairo_surface(widget);
1814 cr = cairo_create(cairo_surface);
1815
Alexander Larsson15901f02013-05-22 14:41:25 +02001816 widget_cairo_update_transform(widget, cr);
1817
Pekka Paalanen35e82632013-04-25 13:57:48 +03001818 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1819
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001820 return cr;
1821}
1822
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001823struct wl_surface *
1824widget_get_wl_surface(struct widget *widget)
1825{
1826 return widget->surface->surface;
1827}
1828
Neil Roberts5e10a042013-09-09 00:40:17 +01001829struct wl_subsurface *
1830widget_get_wl_subsurface(struct widget *widget)
1831{
1832 return widget->surface->subsurface;
1833}
1834
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001835uint32_t
1836widget_get_last_time(struct widget *widget)
1837{
1838 return widget->surface->last_time;
1839}
1840
1841void
1842widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1843{
1844 struct wl_compositor *comp = widget->window->display->compositor;
1845 struct surface *surface = widget->surface;
1846
1847 if (!surface->input_region)
1848 surface->input_region = wl_compositor_create_region(comp);
1849
1850 if (rect) {
1851 wl_region_add(surface->input_region,
1852 rect->x, rect->y, rect->width, rect->height);
1853 }
1854}
1855
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001856void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001857widget_set_resize_handler(struct widget *widget,
1858 widget_resize_handler_t handler)
1859{
1860 widget->resize_handler = handler;
1861}
1862
1863void
1864widget_set_redraw_handler(struct widget *widget,
1865 widget_redraw_handler_t handler)
1866{
1867 widget->redraw_handler = handler;
1868}
1869
1870void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001871widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001872{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001873 widget->enter_handler = handler;
1874}
1875
1876void
1877widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1878{
1879 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001880}
1881
1882void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001883widget_set_motion_handler(struct widget *widget,
1884 widget_motion_handler_t handler)
1885{
1886 widget->motion_handler = handler;
1887}
1888
1889void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001890widget_set_button_handler(struct widget *widget,
1891 widget_button_handler_t handler)
1892{
1893 widget->button_handler = handler;
1894}
1895
1896void
Rusty Lynch041815a2013-08-08 21:20:38 -07001897widget_set_touch_up_handler(struct widget *widget,
1898 widget_touch_up_handler_t handler)
1899{
1900 widget->touch_up_handler = handler;
1901}
1902
1903void
1904widget_set_touch_down_handler(struct widget *widget,
1905 widget_touch_down_handler_t handler)
1906{
1907 widget->touch_down_handler = handler;
1908}
1909
1910void
1911widget_set_touch_motion_handler(struct widget *widget,
1912 widget_touch_motion_handler_t handler)
1913{
1914 widget->touch_motion_handler = handler;
1915}
1916
1917void
1918widget_set_touch_frame_handler(struct widget *widget,
1919 widget_touch_frame_handler_t handler)
1920{
1921 widget->touch_frame_handler = handler;
1922}
1923
1924void
1925widget_set_touch_cancel_handler(struct widget *widget,
1926 widget_touch_cancel_handler_t handler)
1927{
1928 widget->touch_cancel_handler = handler;
1929}
1930
1931void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001932widget_set_axis_handler(struct widget *widget,
1933 widget_axis_handler_t handler)
1934{
1935 widget->axis_handler = handler;
1936}
1937
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001938static void
1939window_schedule_redraw_task(struct window *window);
1940
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001941void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001942widget_schedule_redraw(struct widget *widget)
1943{
Pekka Paalanen71233882013-04-25 13:57:53 +03001944 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001945 widget->surface->redraw_needed = 1;
1946 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001947}
1948
Neil Roberts97b747c2013-12-19 16:17:12 +00001949void
1950widget_set_use_cairo(struct widget *widget,
1951 int use_cairo)
1952{
1953 widget->use_cairo = use_cairo;
1954}
1955
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001956cairo_surface_t *
1957window_get_surface(struct window *window)
1958{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001959 cairo_surface_t *cairo_surface;
1960
1961 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1962
1963 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001964}
1965
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001966struct wl_surface *
1967window_get_wl_surface(struct window *window)
1968{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001969 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001970}
1971
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001972static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001973tooltip_redraw_handler(struct widget *widget, void *data)
1974{
1975 cairo_t *cr;
1976 const int32_t r = 3;
1977 struct tooltip *tooltip = data;
1978 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001979
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001980 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001981 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001982 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1983 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1984 cairo_paint(cr);
1985
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001986 width = widget->allocation.width;
1987 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001988 rounded_rect(cr, 0, 0, width, height, r);
1989
1990 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1991 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1992 cairo_fill(cr);
1993
1994 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1995 cairo_move_to(cr, 10, 16);
1996 cairo_show_text(cr, tooltip->entry);
1997 cairo_destroy(cr);
1998}
1999
2000static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002001get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002002{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002003 cairo_t *cr;
2004 cairo_text_extents_t extents;
2005
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002006 /* Use the dummy_surface because tooltip's surface was not
2007 * created yet, and parent does not have a valid surface
2008 * outside repaint, either.
2009 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002010 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002011 cairo_text_extents(cr, tooltip->entry, &extents);
2012 cairo_destroy(cr);
2013
2014 return extents;
2015}
2016
2017static int
2018window_create_tooltip(struct tooltip *tooltip)
2019{
2020 struct widget *parent = tooltip->parent;
2021 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002022 const int offset_y = 27;
2023 const int margin = 3;
2024 cairo_text_extents_t extents;
2025
2026 if (tooltip->widget)
2027 return 0;
2028
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002029 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002030
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002031 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002032 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002033 widget_set_allocation(tooltip->widget,
2034 tooltip->x, tooltip->y + offset_y,
2035 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002036
2037 return 0;
2038}
2039
2040void
2041widget_destroy_tooltip(struct widget *parent)
2042{
2043 struct tooltip *tooltip = parent->tooltip;
2044
2045 parent->tooltip_count = 0;
2046 if (!tooltip)
2047 return;
2048
2049 if (tooltip->widget) {
2050 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002051 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002052 }
2053
2054 close(tooltip->tooltip_fd);
2055 free(tooltip->entry);
2056 free(tooltip);
2057 parent->tooltip = NULL;
2058}
2059
2060static void
2061tooltip_func(struct task *task, uint32_t events)
2062{
2063 struct tooltip *tooltip =
2064 container_of(task, struct tooltip, tooltip_task);
2065 uint64_t exp;
2066
Martin Olsson8df662a2012-07-08 03:03:47 +02002067 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2068 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002069 window_create_tooltip(tooltip);
2070}
2071
2072#define TOOLTIP_TIMEOUT 500
2073static int
2074tooltip_timer_reset(struct tooltip *tooltip)
2075{
2076 struct itimerspec its;
2077
2078 its.it_interval.tv_sec = 0;
2079 its.it_interval.tv_nsec = 0;
2080 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2081 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2082 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2083 fprintf(stderr, "could not set timerfd\n: %m");
2084 return -1;
2085 }
2086
2087 return 0;
2088}
2089
2090int
2091widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2092{
2093 struct tooltip *tooltip = parent->tooltip;
2094
2095 parent->tooltip_count++;
2096 if (tooltip) {
2097 tooltip->x = x;
2098 tooltip->y = y;
2099 tooltip_timer_reset(tooltip);
2100 return 0;
2101 }
2102
2103 /* the handler might be triggered too fast via input device motion, so
2104 * we need this check here to make sure tooltip is fully initialized */
2105 if (parent->tooltip_count > 1)
2106 return 0;
2107
2108 tooltip = malloc(sizeof *tooltip);
2109 if (!tooltip)
2110 return -1;
2111
2112 parent->tooltip = tooltip;
2113 tooltip->parent = parent;
2114 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002115 tooltip->x = x;
2116 tooltip->y = y;
2117 tooltip->entry = strdup(entry);
2118 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2119 if (tooltip->tooltip_fd < 0) {
2120 fprintf(stderr, "could not create timerfd\n: %m");
2121 return -1;
2122 }
2123
2124 tooltip->tooltip_task.run = tooltip_func;
2125 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2126 EPOLLIN, &tooltip->tooltip_task);
2127 tooltip_timer_reset(tooltip);
2128
2129 return 0;
2130}
2131
2132static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002133workspace_manager_state(void *data,
2134 struct workspace_manager *workspace_manager,
2135 uint32_t current,
2136 uint32_t count)
2137{
2138 struct display *display = data;
2139
2140 display->workspace = current;
2141 display->workspace_count = count;
2142}
2143
2144static const struct workspace_manager_listener workspace_manager_listener = {
2145 workspace_manager_state
2146};
2147
2148static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002149frame_resize_handler(struct widget *widget,
2150 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002151{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002152 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002153 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002154 struct rectangle interior;
2155 struct rectangle input;
2156 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002157
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002158 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002159 interior.x = 0;
2160 interior.y = 0;
2161 interior.width = width;
2162 interior.height = height;
2163 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002164 frame_resize(frame->frame, width, height);
2165 frame_interior(frame->frame, &interior.x, &interior.y,
2166 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002167 }
2168
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002169 widget_set_allocation(child, interior.x, interior.y,
2170 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002171
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 if (child->resize_handler) {
2173 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174 child->user_data);
2175
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002176 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002177 width = child->allocation.width;
2178 height = child->allocation.height;
2179 } else {
2180 frame_resize_inside(frame->frame,
2181 child->allocation.width,
2182 child->allocation.height);
2183 width = frame_width(frame->frame);
2184 height = frame_height(frame->frame);
2185 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002186 }
2187
Scott Moreauf7e498c2012-05-14 11:39:29 -06002188 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002189
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 widget->surface->input_region =
2191 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002192 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 frame_input_rect(frame->frame, &input.x, &input.y,
2194 &input.width, &input.height);
2195 wl_region_add(widget->surface->input_region,
2196 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002197 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002199 }
2200
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002201 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002202
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002203 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002204 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002205 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2206 &opaque.width, &opaque.height);
2207
2208 wl_region_add(widget->surface->opaque_region,
2209 opaque.x, opaque.y,
2210 opaque.width, opaque.height);
2211 } else {
2212 wl_region_add(widget->surface->opaque_region,
2213 0, 0, width, height);
2214 }
Martin Minarik1998b152012-05-10 02:04:35 +02002215 }
2216
Martin Minarik1998b152012-05-10 02:04:35 +02002217
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219}
2220
2221static void
2222frame_redraw_handler(struct widget *widget, void *data)
2223{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002225 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226 struct window *window = widget->window;
2227
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002228 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002229 return;
2230
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002231 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002232
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002233 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002234
2235 cairo_destroy(cr);
2236}
2237
2238static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002239frame_get_pointer_image_for_location(struct window_frame *frame,
2240 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002241{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002242 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002243
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002244 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002245 return CURSOR_LEFT_PTR;
2246
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002247 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002248 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002249 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002250 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002251 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002252 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002253 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002254 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002255 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002257 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002258 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002259 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002260 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002261 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002262 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002263 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002264 case THEME_LOCATION_EXTERIOR:
2265 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002266 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002267 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002268 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002269}
2270
Pekka Paalanen26237862014-09-10 15:10:30 +03002271static void
2272frame_menu_func(void *data, struct input *input, int index)
2273{
2274 struct window *window = data;
2275 struct display *display;
2276
2277 switch (index) {
2278 case 0: /* close */
2279 window_close(window);
2280 break;
2281 case 1: /* move to workspace above */
2282 display = window->display;
2283 if (display->workspace > 0)
2284 workspace_manager_move_surface(
2285 display->workspace_manager,
2286 window->main_surface->surface,
2287 display->workspace - 1);
2288 break;
2289 case 2: /* move to workspace below */
2290 display = window->display;
2291 if (display->workspace < display->workspace_count - 1)
2292 workspace_manager_move_surface(
2293 display->workspace_manager,
2294 window->main_surface->surface,
2295 display->workspace + 1);
2296 break;
2297 case 3: /* fullscreen */
2298 /* we don't have a way to get out of fullscreen for now */
2299 if (window->fullscreen_handler)
2300 window->fullscreen_handler(window, window->user_data);
2301 break;
2302 }
2303}
2304
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002305void
2306window_show_frame_menu(struct window *window,
2307 struct input *input, uint32_t time)
2308{
2309 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002310 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002311
Pekka Paalanen26237862014-09-10 15:10:30 +03002312 static const char *entries[] = {
2313 "Close",
2314 "Move to workspace above", "Move to workspace below",
2315 "Fullscreen"
2316 };
2317
2318 if (window->fullscreen_handler)
2319 count = ARRAY_LENGTH(entries);
2320 else
2321 count = ARRAY_LENGTH(entries) - 1;
2322
2323 input_get_position(input, &x, &y);
2324 window_show_menu(window->display, input, time, window,
2325 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002326}
2327
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002328static int
2329frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002330 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002331{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002332 struct window_frame *frame = data;
2333 enum theme_location location;
2334
2335 location = frame_pointer_enter(frame->frame, input, x, y);
2336 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2337 widget_schedule_redraw(frame->widget);
2338
2339 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002340}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002341
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002342static int
2343frame_motion_handler(struct widget *widget,
2344 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002345 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002346{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002347 struct window_frame *frame = data;
2348 enum theme_location location;
2349
2350 location = frame_pointer_motion(frame->frame, input, x, y);
2351 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2352 widget_schedule_redraw(frame->widget);
2353
2354 return frame_get_pointer_image_for_location(data, location);
2355}
2356
2357static void
2358frame_leave_handler(struct widget *widget,
2359 struct input *input, void *data)
2360{
2361 struct window_frame *frame = data;
2362
2363 frame_pointer_leave(frame->frame, input);
2364 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2365 widget_schedule_redraw(frame->widget);
2366}
2367
2368static void
2369frame_handle_status(struct window_frame *frame, struct input *input,
2370 uint32_t time, enum theme_location location)
2371{
2372 struct window *window = frame->widget->window;
2373 uint32_t status;
2374
2375 status = frame_status(frame->frame);
2376 if (status & FRAME_STATUS_REPAINT)
2377 widget_schedule_redraw(frame->widget);
2378
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002379 if (status & FRAME_STATUS_MINIMIZE) {
2380 window_set_minimized(window);
2381 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2382 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002383
2384 if (status & FRAME_STATUS_MENU) {
2385 window_show_frame_menu(window, input, time);
2386 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2387 }
2388
2389 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002390 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002391 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2392 }
2393
2394 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002395 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002396 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002397 }
2398
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002399 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002400 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002401 xdg_surface_move(window->xdg_surface,
2402 input_get_seat(input),
2403 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002404
2405 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2406 }
2407
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002408 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002409 input_ungrab(input);
2410
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002411 xdg_surface_resize(window->xdg_surface,
2412 input_get_seat(input),
2413 window->display->serial,
2414 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002415
2416 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2417 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002418}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002419
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002420#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002422frame_button_handler(struct widget *widget,
2423 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002424 uint32_t button, enum wl_pointer_button_state state,
2425 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002426
2427{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002428 struct window_frame *frame = data;
2429 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002430
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002431 frame->double_click = 0;
2432 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2433 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2434 frame->double_click = 1;
2435 frame->did_double = 1;
2436 } else
2437 frame->did_double = 0;
2438
2439 frame->last_time = time;
2440 } else if (frame->did_double == 1) {
2441 frame->double_click = 1;
2442 frame->did_double = 0;
2443 }
2444
2445 if (frame->double_click)
2446 location = frame_double_click(frame->frame, input,
2447 button, state);
2448 else
2449 location = frame_pointer_button(frame->frame, input,
2450 button, state);
2451
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002452 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002453}
2454
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002455static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002456frame_touch_down_handler(struct widget *widget, struct input *input,
2457 uint32_t serial, uint32_t time, int32_t id,
2458 float x, float y, void *data)
2459{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002460 struct window_frame *frame = data;
2461
Xiong Zhang382de462014-06-12 11:06:26 +08002462 frame->double_click = 0;
2463 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2464 frame->last_id == id) {
2465 frame->double_click = 1;
2466 frame->did_double = 1;
2467 frame->double_id = id;
2468 } else
2469 frame->did_double = 0;
2470
2471 frame->last_time = time;
2472 frame->last_id = id;
2473
2474 if (frame->double_click)
2475 frame_double_touch_down(frame->frame, input, id, x, y);
2476 else
2477 frame_touch_down(frame->frame, input, id, x, y);
2478
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002479 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2480}
2481
2482static void
2483frame_touch_up_handler(struct widget *widget,
2484 struct input *input, uint32_t serial, uint32_t time,
2485 int32_t id, void *data)
2486{
2487 struct window_frame *frame = data;
2488
Xiong Zhang382de462014-06-12 11:06:26 +08002489 if (frame->double_id == id && frame->did_double) {
2490 frame->did_double = 0;
2491 frame->double_id = 0;
2492 frame_double_touch_up(frame->frame, input, id);
2493 } else
2494 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002495 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002496}
2497
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002499window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002500{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002501 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002502 uint32_t buttons;
2503
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002504 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002505 buttons = FRAME_BUTTON_NONE;
2506 } else {
2507 buttons = FRAME_BUTTON_ALL;
2508 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002509
Peter Huttererf3d62272013-08-08 11:57:05 +10002510 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002511 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002512 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002513
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002514 frame->widget = window_add_widget(window, frame);
2515 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002516
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002517 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2518 widget_set_resize_handler(frame->widget, frame_resize_handler);
2519 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002520 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002521 widget_set_motion_handler(frame->widget, frame_motion_handler);
2522 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002523 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002524 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002525
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002526 window->frame = frame;
2527
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002528 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002529}
2530
Kristian Høgsberga1627922012-06-20 17:30:03 -04002531void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002532window_frame_set_child_size(struct widget *widget, int child_width,
2533 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002534{
2535 struct display *display = widget->window->display;
2536 struct theme *t = display->theme;
2537 int decoration_width, decoration_height;
2538 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002539 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002540
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002541 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002542 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002543 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002544 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002545
2546 width = child_width + decoration_width;
2547 height = child_height + decoration_height;
2548 } else {
2549 width = child_width;
2550 height = child_height;
2551 }
2552
2553 window_schedule_resize(widget->window, width, height);
2554}
2555
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002556static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002557window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002558{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002559 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002560
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002561 /* frame->child must be destroyed by the application */
2562 widget_destroy(frame->widget);
2563 free(frame);
2564}
2565
2566static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002567input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002568 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002569{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002570 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002571 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002572
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002573 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002574 return;
2575
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002576 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002577 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002578 widget = old;
2579 if (input->grab)
2580 widget = input->grab;
2581 if (widget->leave_handler)
2582 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002583 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002584 }
2585
2586 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002587 widget = focus;
2588 if (input->grab)
2589 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002590 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002591 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002592 cursor = widget->enter_handler(focus, input, x, y,
2593 widget->user_data);
2594 else
2595 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002596
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002597 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002598 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002599}
2600
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002601void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002602touch_grab(struct input *input, int32_t touch_id)
2603{
2604 input->touch_grab = 1;
2605 input->touch_grab_id = touch_id;
2606}
2607
2608void
2609touch_ungrab(struct input *input)
2610{
2611 struct touch_point *tp, *tmp;
2612
2613 input->touch_grab = 0;
2614
2615 wl_list_for_each_safe(tp, tmp,
2616 &input->touch_point_list, link) {
2617 if (tp->id != input->touch_grab_id)
2618 continue;
2619 wl_list_remove(&tp->link);
2620 free(tp);
2621
2622 return;
2623 }
2624}
2625
2626void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002627input_grab(struct input *input, struct widget *widget, uint32_t button)
2628{
2629 input->grab = widget;
2630 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002631
2632 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002633}
2634
2635void
2636input_ungrab(struct input *input)
2637{
2638 struct widget *widget;
2639
2640 input->grab = NULL;
2641 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002642 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002643 input->sx, input->sy);
2644 input_set_focus_widget(input, widget, input->sx, input->sy);
2645 }
2646}
2647
2648static void
2649input_remove_pointer_focus(struct input *input)
2650{
2651 struct window *window = input->pointer_focus;
2652
2653 if (!window)
2654 return;
2655
2656 input_set_focus_widget(input, NULL, 0, 0);
2657
2658 input->pointer_focus = NULL;
2659 input->current_cursor = CURSOR_UNSET;
2660}
2661
2662static void
2663pointer_handle_enter(void *data, struct wl_pointer *pointer,
2664 uint32_t serial, struct wl_surface *surface,
2665 wl_fixed_t sx_w, wl_fixed_t sy_w)
2666{
2667 struct input *input = data;
2668 struct window *window;
2669 struct widget *widget;
2670 float sx = wl_fixed_to_double(sx_w);
2671 float sy = wl_fixed_to_double(sy_w);
2672
2673 if (!surface) {
2674 /* enter event for a window we've just destroyed */
2675 return;
2676 }
2677
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002678 window = wl_surface_get_user_data(surface);
2679 if (surface != window->main_surface->surface) {
2680 DBG("Ignoring input event from subsurface %p\n", surface);
2681 return;
2682 }
2683
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002684 input->display->serial = serial;
2685 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002686 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002687
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002688 input->sx = sx;
2689 input->sy = sy;
2690
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002691 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002692 input_set_focus_widget(input, widget, sx, sy);
2693}
2694
2695static void
2696pointer_handle_leave(void *data, struct wl_pointer *pointer,
2697 uint32_t serial, struct wl_surface *surface)
2698{
2699 struct input *input = data;
2700
2701 input->display->serial = serial;
2702 input_remove_pointer_focus(input);
2703}
2704
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002705static void
Daniel Stone37816df2012-05-16 18:45:18 +01002706pointer_handle_motion(void *data, struct wl_pointer *pointer,
2707 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002708{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002709 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002710 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002711 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002712 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002713 float sx = wl_fixed_to_double(sx_w);
2714 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002715
Paul Winwoodb22bf572013-08-29 10:52:54 +01002716 if (!window)
2717 return;
2718
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002719 input->sx = sx;
2720 input->sy = sy;
2721
Rob Bradford5f087742013-07-11 19:41:27 +01002722 /* when making the window smaller - e.g. after a unmaximise we might
2723 * still have a pending motion event that the compositor has picked
2724 * based on the old surface dimensions
2725 */
2726 if (sx > window->main_surface->allocation.width ||
2727 sy > window->main_surface->allocation.height)
2728 return;
2729
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002730 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002731 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002732 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002733 }
2734
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002735 if (input->grab)
2736 widget = input->grab;
2737 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002738 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002739 if (widget) {
2740 if (widget->motion_handler)
2741 cursor = widget->motion_handler(input->focus_widget,
2742 input, time, sx, sy,
2743 widget->user_data);
2744 else
2745 cursor = widget->default_cursor;
2746 } else
2747 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002748
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002749 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002750}
2751
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002752static void
Daniel Stone37816df2012-05-16 18:45:18 +01002753pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002754 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002755{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002756 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002757 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002758 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002759
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002760 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002761 if (input->focus_widget && input->grab == NULL &&
2762 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002763 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002764
Neil Roberts6b28aad2012-01-23 19:11:18 +00002765 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002766 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002767 (*widget->button_handler)(widget,
2768 input, time,
2769 button, state,
2770 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002771
Daniel Stone4dbadb12012-05-30 16:31:51 +01002772 if (input->grab && input->grab_button == button &&
2773 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002774 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002775}
2776
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002777static void
Daniel Stone37816df2012-05-16 18:45:18 +01002778pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002779 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002780{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002781 struct input *input = data;
2782 struct widget *widget;
2783
2784 widget = input->focus_widget;
2785 if (input->grab)
2786 widget = input->grab;
2787 if (widget && widget->axis_handler)
2788 (*widget->axis_handler)(widget,
2789 input, time,
2790 axis, value,
2791 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002792}
2793
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002794static const struct wl_pointer_listener pointer_listener = {
2795 pointer_handle_enter,
2796 pointer_handle_leave,
2797 pointer_handle_motion,
2798 pointer_handle_button,
2799 pointer_handle_axis,
2800};
2801
2802static void
2803input_remove_keyboard_focus(struct input *input)
2804{
2805 struct window *window = input->keyboard_focus;
2806 struct itimerspec its;
2807
2808 its.it_interval.tv_sec = 0;
2809 its.it_interval.tv_nsec = 0;
2810 its.it_value.tv_sec = 0;
2811 its.it_value.tv_nsec = 0;
2812 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2813
2814 if (!window)
2815 return;
2816
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002817 if (window->keyboard_focus_handler)
2818 (*window->keyboard_focus_handler)(window, NULL,
2819 window->user_data);
2820
2821 input->keyboard_focus = NULL;
2822}
2823
2824static void
2825keyboard_repeat_func(struct task *task, uint32_t events)
2826{
2827 struct input *input =
2828 container_of(task, struct input, repeat_task);
2829 struct window *window = input->keyboard_focus;
2830 uint64_t exp;
2831
2832 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2833 /* If we change the timer between the fd becoming
2834 * readable and getting here, there'll be nothing to
2835 * read and we get EAGAIN. */
2836 return;
2837
2838 if (window && window->key_handler) {
2839 (*window->key_handler)(window, input, input->repeat_time,
2840 input->repeat_key, input->repeat_sym,
2841 WL_KEYBOARD_KEY_STATE_PRESSED,
2842 window->user_data);
2843 }
2844}
2845
2846static void
2847keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2848 uint32_t format, int fd, uint32_t size)
2849{
2850 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002851 struct xkb_keymap *keymap;
2852 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002853 char *map_str;
2854
2855 if (!data) {
2856 close(fd);
2857 return;
2858 }
2859
2860 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2861 close(fd);
2862 return;
2863 }
2864
2865 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2866 if (map_str == MAP_FAILED) {
2867 close(fd);
2868 return;
2869 }
2870
Ran Benita2e1968f2014-08-19 23:59:51 +03002871 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2872 map_str,
2873 XKB_KEYMAP_FORMAT_TEXT_V1,
2874 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002875 munmap(map_str, size);
2876 close(fd);
2877
Rui Matos3eccb862013-10-10 19:44:22 +02002878 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002879 fprintf(stderr, "failed to compile keymap\n");
2880 return;
2881 }
2882
Rui Matos3eccb862013-10-10 19:44:22 +02002883 state = xkb_state_new(keymap);
2884 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002885 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002886 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002887 return;
2888 }
2889
Rui Matos3eccb862013-10-10 19:44:22 +02002890 xkb_keymap_unref(input->xkb.keymap);
2891 xkb_state_unref(input->xkb.state);
2892 input->xkb.keymap = keymap;
2893 input->xkb.state = state;
2894
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002895 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002896 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002897 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002898 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002899 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002900 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002901}
2902
2903static void
2904keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2905 uint32_t serial, struct wl_surface *surface,
2906 struct wl_array *keys)
2907{
2908 struct input *input = data;
2909 struct window *window;
2910
2911 input->display->serial = serial;
2912 input->keyboard_focus = wl_surface_get_user_data(surface);
2913
2914 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002915 if (window->keyboard_focus_handler)
2916 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002917 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002918}
2919
2920static void
2921keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2922 uint32_t serial, struct wl_surface *surface)
2923{
2924 struct input *input = data;
2925
2926 input->display->serial = serial;
2927 input_remove_keyboard_focus(input);
2928}
2929
Scott Moreau210d0792012-03-22 10:47:01 -06002930static void
Daniel Stone37816df2012-05-16 18:45:18 +01002931keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002932 uint32_t serial, uint32_t time, uint32_t key,
2933 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002934{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002935 struct input *input = data;
2936 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002937 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002938 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002939 const xkb_keysym_t *syms;
2940 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002941 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002942
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002943 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002944 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002945 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002946 return;
2947
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002948 /* We only use input grabs for pointer events for now, so just
2949 * ignore key presses if a grab is active. We expand the key
2950 * event delivery mechanism to route events to widgets to
2951 * properly handle key grabs. In the meantime, this prevents
2952 * key event devlivery while a grab is active. */
2953 if (input->grab && input->grab_button == 0)
2954 return;
2955
Ran Benita2e1968f2014-08-19 23:59:51 +03002956 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002957
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002958 sym = XKB_KEY_NoSymbol;
2959 if (num_syms == 1)
2960 sym = syms[0];
2961
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002962
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002963 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002964 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002965 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002966 } else if (sym == XKB_KEY_F11 &&
2967 window->fullscreen_handler &&
2968 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2969 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002970 } else if (sym == XKB_KEY_F4 &&
2971 input->modifiers == MOD_ALT_MASK &&
2972 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002973 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002974 } else if (window->key_handler) {
2975 (*window->key_handler)(window, input, time, key,
2976 sym, state, window->user_data);
2977 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002978
2979 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2980 key == input->repeat_key) {
2981 its.it_interval.tv_sec = 0;
2982 its.it_interval.tv_nsec = 0;
2983 its.it_value.tv_sec = 0;
2984 its.it_value.tv_nsec = 0;
2985 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002986 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2987 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002988 input->repeat_sym = sym;
2989 input->repeat_key = key;
2990 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002991 its.it_interval.tv_sec = input->repeat_rate_sec;
2992 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2993 its.it_value.tv_sec = input->repeat_delay_sec;
2994 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002995 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2996 }
2997}
2998
2999static void
Daniel Stone351eb612012-05-31 15:27:47 -04003000keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3001 uint32_t serial, uint32_t mods_depressed,
3002 uint32_t mods_latched, uint32_t mods_locked,
3003 uint32_t group)
3004{
3005 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003006 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003007
Matt Ropere61561f2013-06-24 16:52:43 +01003008 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3009 if (!input->xkb.keymap)
3010 return;
3011
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003012 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3013 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003014 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003015 XKB_STATE_MODS_DEPRESSED |
3016 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003017 input->modifiers = 0;
3018 if (mask & input->xkb.control_mask)
3019 input->modifiers |= MOD_CONTROL_MASK;
3020 if (mask & input->xkb.alt_mask)
3021 input->modifiers |= MOD_ALT_MASK;
3022 if (mask & input->xkb.shift_mask)
3023 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003024}
3025
Jonny Lamb06959082014-08-12 14:58:27 +02003026static void
3027set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3028{
3029 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3030 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3031
3032 /* a rate of zero disables any repeating, regardless of the delay's
3033 * value */
3034 if (rate == 0)
3035 return;
3036
3037 if (rate == 1)
3038 input->repeat_rate_sec = 1;
3039 else
3040 input->repeat_rate_nsec = 1000000000 / rate;
3041
3042 input->repeat_delay_sec = delay / 1000;
3043 delay -= (input->repeat_delay_sec * 1000);
3044 input->repeat_delay_nsec = delay * 1000 * 1000;
3045}
3046
3047static void
3048keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3049 int32_t rate, int32_t delay)
3050{
3051 struct input *input = data;
3052
3053 set_repeat_info(input, rate, delay);
3054}
3055
Daniel Stone37816df2012-05-16 18:45:18 +01003056static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003057 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003058 keyboard_handle_enter,
3059 keyboard_handle_leave,
3060 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003061 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003062 keyboard_handle_repeat_info
3063
Daniel Stone37816df2012-05-16 18:45:18 +01003064};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003065
3066static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003067touch_handle_down(void *data, struct wl_touch *wl_touch,
3068 uint32_t serial, uint32_t time, struct wl_surface *surface,
3069 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3070{
3071 struct input *input = data;
3072 struct widget *widget;
3073 float sx = wl_fixed_to_double(x_w);
3074 float sy = wl_fixed_to_double(y_w);
3075
Rusty Lynch1084da52013-08-15 09:10:08 -07003076 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003077 input->touch_focus = wl_surface_get_user_data(surface);
3078 if (!input->touch_focus) {
3079 DBG("Failed to find to touch focus for surface %p\n", surface);
3080 return;
3081 }
3082
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003083 if (surface != input->touch_focus->main_surface->surface) {
3084 DBG("Ignoring input event from subsurface %p\n", surface);
3085 input->touch_focus = NULL;
3086 return;
3087 }
3088
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003089 if (input->grab)
3090 widget = input->grab;
3091 else
3092 widget = window_find_widget(input->touch_focus,
3093 wl_fixed_to_double(x_w),
3094 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003095 if (widget) {
3096 struct touch_point *tp = xmalloc(sizeof *tp);
3097 if (tp) {
3098 tp->id = id;
3099 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003100 tp->x = sx;
3101 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003102 wl_list_insert(&input->touch_point_list, &tp->link);
3103
3104 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003105 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003106 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003107 sx, sy,
3108 widget->user_data);
3109 }
3110 }
3111}
3112
3113static void
3114touch_handle_up(void *data, struct wl_touch *wl_touch,
3115 uint32_t serial, uint32_t time, int32_t id)
3116{
3117 struct input *input = data;
3118 struct touch_point *tp, *tmp;
3119
Rusty Lynch041815a2013-08-08 21:20:38 -07003120 if (!input->touch_focus) {
3121 DBG("No touch focus found for touch up event!\n");
3122 return;
3123 }
3124
3125 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3126 if (tp->id != id)
3127 continue;
3128
3129 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003130 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003131 time, id,
3132 tp->widget->user_data);
3133
3134 wl_list_remove(&tp->link);
3135 free(tp);
3136
3137 return;
3138 }
3139}
3140
3141static void
3142touch_handle_motion(void *data, struct wl_touch *wl_touch,
3143 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3144{
3145 struct input *input = data;
3146 struct touch_point *tp;
3147 float sx = wl_fixed_to_double(x_w);
3148 float sy = wl_fixed_to_double(y_w);
3149
3150 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3151
3152 if (!input->touch_focus) {
3153 DBG("No touch focus found for touch motion event!\n");
3154 return;
3155 }
3156
3157 wl_list_for_each(tp, &input->touch_point_list, link) {
3158 if (tp->id != id)
3159 continue;
3160
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003161 tp->x = sx;
3162 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003163 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003164 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003165 id, sx, sy,
3166 tp->widget->user_data);
3167 return;
3168 }
3169}
3170
3171static void
3172touch_handle_frame(void *data, struct wl_touch *wl_touch)
3173{
3174 struct input *input = data;
3175 struct touch_point *tp, *tmp;
3176
3177 DBG("touch_handle_frame\n");
3178
3179 if (!input->touch_focus) {
3180 DBG("No touch focus found for touch frame event!\n");
3181 return;
3182 }
3183
3184 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3185 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003186 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003187 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003188 }
3189}
3190
3191static void
3192touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3193{
3194 struct input *input = data;
3195 struct touch_point *tp, *tmp;
3196
3197 DBG("touch_handle_cancel\n");
3198
3199 if (!input->touch_focus) {
3200 DBG("No touch focus found for touch cancel event!\n");
3201 return;
3202 }
3203
3204 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3205 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003206 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3207 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003208
3209 wl_list_remove(&tp->link);
3210 free(tp);
3211 }
3212}
3213
3214static const struct wl_touch_listener touch_listener = {
3215 touch_handle_down,
3216 touch_handle_up,
3217 touch_handle_motion,
3218 touch_handle_frame,
3219 touch_handle_cancel,
3220};
3221
3222static void
Daniel Stone37816df2012-05-16 18:45:18 +01003223seat_handle_capabilities(void *data, struct wl_seat *seat,
3224 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003225{
Daniel Stone37816df2012-05-16 18:45:18 +01003226 struct input *input = data;
3227
3228 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3229 input->pointer = wl_seat_get_pointer(seat);
3230 wl_pointer_set_user_data(input->pointer, input);
3231 wl_pointer_add_listener(input->pointer, &pointer_listener,
3232 input);
3233 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3234 wl_pointer_destroy(input->pointer);
3235 input->pointer = NULL;
3236 }
3237
3238 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3239 input->keyboard = wl_seat_get_keyboard(seat);
3240 wl_keyboard_set_user_data(input->keyboard, input);
3241 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3242 input);
3243 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3244 wl_keyboard_destroy(input->keyboard);
3245 input->keyboard = NULL;
3246 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003247
3248 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3249 input->touch = wl_seat_get_touch(seat);
3250 wl_touch_set_user_data(input->touch, input);
3251 wl_touch_add_listener(input->touch, &touch_listener, input);
3252 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3253 wl_touch_destroy(input->touch);
3254 input->touch = NULL;
3255 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003256}
3257
Rob Bradford08031182013-08-13 20:11:03 +01003258static void
3259seat_handle_name(void *data, struct wl_seat *seat,
3260 const char *name)
3261{
3262
3263}
3264
Daniel Stone37816df2012-05-16 18:45:18 +01003265static const struct wl_seat_listener seat_listener = {
3266 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003267 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003268};
3269
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003270void
3271input_get_position(struct input *input, int32_t *x, int32_t *y)
3272{
3273 *x = input->sx;
3274 *y = input->sy;
3275}
3276
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003277int
3278input_get_touch(struct input *input, int32_t id, float *x, float *y)
3279{
3280 struct touch_point *tp;
3281
3282 wl_list_for_each(tp, &input->touch_point_list, link) {
3283 if (tp->id != id)
3284 continue;
3285
3286 *x = tp->x;
3287 *y = tp->y;
3288 return 0;
3289 }
3290
3291 return -1;
3292}
3293
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003294struct display *
3295input_get_display(struct input *input)
3296{
3297 return input->display;
3298}
3299
Daniel Stone37816df2012-05-16 18:45:18 +01003300struct wl_seat *
3301input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003302{
Daniel Stone37816df2012-05-16 18:45:18 +01003303 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003304}
3305
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003306uint32_t
3307input_get_modifiers(struct input *input)
3308{
3309 return input->modifiers;
3310}
3311
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003312struct widget *
3313input_get_focus_widget(struct input *input)
3314{
3315 return input->focus_widget;
3316}
3317
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003318struct data_offer {
3319 struct wl_data_offer *offer;
3320 struct input *input;
3321 struct wl_array types;
3322 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003323
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003324 struct task io_task;
3325 int fd;
3326 data_func_t func;
3327 int32_t x, y;
3328 void *user_data;
3329};
3330
3331static void
3332data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3333{
3334 struct data_offer *offer = data;
3335 char **p;
3336
3337 p = wl_array_add(&offer->types, sizeof *p);
3338 *p = strdup(type);
3339}
3340
3341static const struct wl_data_offer_listener data_offer_listener = {
3342 data_offer_offer,
3343};
3344
3345static void
3346data_offer_destroy(struct data_offer *offer)
3347{
3348 char **p;
3349
3350 offer->refcount--;
3351 if (offer->refcount == 0) {
3352 wl_data_offer_destroy(offer->offer);
3353 for (p = offer->types.data; *p; p++)
3354 free(*p);
3355 wl_array_release(&offer->types);
3356 free(offer);
3357 }
3358}
3359
3360static void
3361data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003362 struct wl_data_device *data_device,
3363 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003364{
3365 struct data_offer *offer;
3366
Brian Lovinbc919262013-08-07 15:34:59 -07003367 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003368
3369 wl_array_init(&offer->types);
3370 offer->refcount = 1;
3371 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003372 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003373 wl_data_offer_add_listener(offer->offer,
3374 &data_offer_listener, offer);
3375}
3376
3377static void
3378data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003379 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003380 wl_fixed_t x_w, wl_fixed_t y_w,
3381 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003382{
3383 struct input *input = data;
3384 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003385 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003386 float x = wl_fixed_to_double(x_w);
3387 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003388 char **p;
3389
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003390 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003391 input->drag_enter_serial = serial;
3392 input->drag_focus = window,
3393 input->drag_x = x;
3394 input->drag_y = y;
3395
3396 if (!input->touch_grab)
3397 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003398
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003399 if (offer) {
3400 input->drag_offer = wl_data_offer_get_user_data(offer);
3401
3402 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3403 *p = NULL;
3404
3405 types_data = input->drag_offer->types.data;
3406 } else {
3407 input->drag_offer = NULL;
3408 types_data = NULL;
3409 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003410
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003411 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003412 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003413 window->user_data);
3414}
3415
3416static void
3417data_device_leave(void *data, struct wl_data_device *data_device)
3418{
3419 struct input *input = data;
3420
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003421 if (input->drag_offer) {
3422 data_offer_destroy(input->drag_offer);
3423 input->drag_offer = NULL;
3424 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003425}
3426
3427static void
3428data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003429 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003430{
3431 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003432 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003433 float x = wl_fixed_to_double(x_w);
3434 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003435 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003436
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003437 input->drag_x = x;
3438 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003439
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003440 if (input->drag_offer)
3441 types_data = input->drag_offer->types.data;
3442 else
3443 types_data = NULL;
3444
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003445 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003446 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003447 window->user_data);
3448}
3449
3450static void
3451data_device_drop(void *data, struct wl_data_device *data_device)
3452{
3453 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003454 struct window *window = input->drag_focus;
3455 float x, y;
3456
3457 x = input->drag_x;
3458 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003459
3460 if (window->drop_handler)
3461 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003462 x, y, window->user_data);
3463
3464 if (input->touch_grab)
3465 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003466}
3467
3468static void
3469data_device_selection(void *data,
3470 struct wl_data_device *wl_data_device,
3471 struct wl_data_offer *offer)
3472{
3473 struct input *input = data;
3474 char **p;
3475
3476 if (input->selection_offer)
3477 data_offer_destroy(input->selection_offer);
3478
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003479 if (offer) {
3480 input->selection_offer = wl_data_offer_get_user_data(offer);
3481 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3482 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003483 } else {
3484 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003485 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003486}
3487
3488static const struct wl_data_device_listener data_device_listener = {
3489 data_device_data_offer,
3490 data_device_enter,
3491 data_device_leave,
3492 data_device_motion,
3493 data_device_drop,
3494 data_device_selection
3495};
3496
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003497static void
3498input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003499{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003500 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003501 struct wl_cursor *cursor;
3502 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003503
Daniel Stone80972742012-11-07 17:51:39 +11003504 if (!input->pointer)
3505 return;
3506
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003507 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003508 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003509 return;
3510
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003511 if (index >= (int) cursor->image_count) {
3512 fprintf(stderr, "cursor index out of range\n");
3513 return;
3514 }
3515
3516 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003517 buffer = wl_cursor_image_get_buffer(image);
3518 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003519 return;
3520
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003521 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3522 wl_surface_damage(input->pointer_surface, 0, 0,
3523 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003524 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003525 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3526 input->pointer_surface,
3527 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003528}
3529
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003530static const struct wl_callback_listener pointer_surface_listener;
3531
Derek Foreman493d9792015-03-04 16:26:25 -06003532static bool
3533input_set_pointer_special(struct input *input)
3534{
3535 if (input->current_cursor == CURSOR_BLANK) {
3536 wl_pointer_set_cursor(input->pointer,
3537 input->pointer_enter_serial,
3538 NULL, 0, 0);
3539 return true;
3540 }
3541
3542 if (input->current_cursor == CURSOR_UNSET)
3543 return true;
3544
3545 return false;
3546}
3547
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003548static void
3549pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3550 uint32_t time)
3551{
3552 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003553 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003554 int i;
3555
3556 if (callback) {
3557 assert(callback == input->cursor_frame_cb);
3558 wl_callback_destroy(callback);
3559 input->cursor_frame_cb = NULL;
3560 }
3561
Daniel Stone80972742012-11-07 17:51:39 +11003562 if (!input->pointer)
3563 return;
3564
Derek Foreman493d9792015-03-04 16:26:25 -06003565 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003566 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003567
Daniel Stonea494f1d2012-06-18 19:31:12 +01003568 cursor = input->display->cursors[input->current_cursor];
3569 if (!cursor)
3570 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003571
3572 /* FIXME We don't have the current time on the first call so we set
3573 * the animation start to the time of the first frame callback. */
3574 if (time == 0)
3575 input->cursor_anim_start = 0;
3576 else if (input->cursor_anim_start == 0)
3577 input->cursor_anim_start = time;
3578
3579 if (time == 0 || input->cursor_anim_start == 0)
3580 i = 0;
3581 else
3582 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3583
Pekka Paalanenbc106382012-10-10 12:49:31 +03003584 if (cursor->image_count > 1) {
3585 input->cursor_frame_cb =
3586 wl_surface_frame(input->pointer_surface);
3587 wl_callback_add_listener(input->cursor_frame_cb,
3588 &pointer_surface_listener, input);
3589 }
3590
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003591 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003592}
3593
3594static const struct wl_callback_listener pointer_surface_listener = {
3595 pointer_surface_frame_callback
3596};
3597
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003598void
3599input_set_pointer_image(struct input *input, int pointer)
3600{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003601 int force = 0;
3602
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003603 if (!input->pointer)
3604 return;
3605
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003606 if (input->pointer_enter_serial > input->cursor_serial)
3607 force = 1;
3608
3609 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003610 return;
3611
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003612 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003613 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003614 if (!input->cursor_frame_cb)
3615 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003616 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003617 /* The current frame callback may be stuck if, for instance,
3618 * the set cursor request was processed by the server after
3619 * this client lost the focus. In this case the cursor surface
3620 * might not be mapped and the frame callback wouldn't ever
3621 * complete. Send a set_cursor and attach to try to map the
3622 * cursor surface again so that the callback will finish */
3623 input_set_pointer_image_index(input, 0);
3624 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003625}
3626
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003627struct wl_data_device *
3628input_get_data_device(struct input *input)
3629{
3630 return input->data_device;
3631}
3632
3633void
3634input_set_selection(struct input *input,
3635 struct wl_data_source *source, uint32_t time)
3636{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003637 if (input->data_device)
3638 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003639}
3640
3641void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003642input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003643{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003644 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003645 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003646}
3647
3648static void
3649offer_io_func(struct task *task, uint32_t events)
3650{
3651 struct data_offer *offer =
3652 container_of(task, struct data_offer, io_task);
3653 unsigned int len;
3654 char buffer[4096];
3655
3656 len = read(offer->fd, buffer, sizeof buffer);
3657 offer->func(buffer, len,
3658 offer->x, offer->y, offer->user_data);
3659
3660 if (len == 0) {
3661 close(offer->fd);
3662 data_offer_destroy(offer);
3663 }
3664}
3665
3666static void
3667data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3668 data_func_t func, void *user_data)
3669{
3670 int p[2];
3671
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003672 if (pipe2(p, O_CLOEXEC) == -1)
3673 return;
3674
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003675 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3676 close(p[1]);
3677
3678 offer->io_task.run = offer_io_func;
3679 offer->fd = p[0];
3680 offer->func = func;
3681 offer->refcount++;
3682 offer->user_data = user_data;
3683
3684 display_watch_fd(offer->input->display,
3685 offer->fd, EPOLLIN, &offer->io_task);
3686}
3687
3688void
3689input_receive_drag_data(struct input *input, const char *mime_type,
3690 data_func_t func, void *data)
3691{
3692 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003693 input->drag_offer->x = input->drag_x;
3694 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003695}
3696
3697int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003698input_receive_drag_data_to_fd(struct input *input,
3699 const char *mime_type, int fd)
3700{
3701 if (input->drag_offer)
3702 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3703
3704 return 0;
3705}
3706
3707int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003708input_receive_selection_data(struct input *input, const char *mime_type,
3709 data_func_t func, void *data)
3710{
3711 char **p;
3712
3713 if (input->selection_offer == NULL)
3714 return -1;
3715
3716 for (p = input->selection_offer->types.data; *p; p++)
3717 if (strcmp(mime_type, *p) == 0)
3718 break;
3719
3720 if (*p == NULL)
3721 return -1;
3722
3723 data_offer_receive_data(input->selection_offer,
3724 mime_type, func, data);
3725 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003726}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003727
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003728int
3729input_receive_selection_data_to_fd(struct input *input,
3730 const char *mime_type, int fd)
3731{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003732 if (input->selection_offer)
3733 wl_data_offer_receive(input->selection_offer->offer,
3734 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003735
3736 return 0;
3737}
3738
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003739void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003740window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003741{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003742 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003743 return;
3744
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003745 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003746}
3747
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003748static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003749surface_set_synchronized(struct surface *surface)
3750{
3751 if (!surface->subsurface)
3752 return;
3753
3754 if (surface->synchronized)
3755 return;
3756
3757 wl_subsurface_set_sync(surface->subsurface);
3758 surface->synchronized = 1;
3759}
3760
3761static void
3762surface_set_synchronized_default(struct surface *surface)
3763{
3764 if (!surface->subsurface)
3765 return;
3766
3767 if (surface->synchronized == surface->synchronized_default)
3768 return;
3769
3770 if (surface->synchronized_default)
3771 wl_subsurface_set_sync(surface->subsurface);
3772 else
3773 wl_subsurface_set_desync(surface->subsurface);
3774
3775 surface->synchronized = surface->synchronized_default;
3776}
3777
3778static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003779surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003780{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003781 struct widget *widget = surface->widget;
3782 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003783
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003784 if (surface->input_region) {
3785 wl_region_destroy(surface->input_region);
3786 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003787 }
3788
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003789 if (surface->opaque_region)
3790 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003791
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003792 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003793
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003794 if (widget->resize_handler)
3795 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003796 widget->allocation.width,
3797 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003798 widget->user_data);
3799
Pekka Paalanen35e82632013-04-25 13:57:48 +03003800 if (surface->subsurface &&
3801 (surface->allocation.x != widget->allocation.x ||
3802 surface->allocation.y != widget->allocation.y)) {
3803 wl_subsurface_set_position(surface->subsurface,
3804 widget->allocation.x,
3805 widget->allocation.y);
3806 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003807 if (surface->allocation.width != widget->allocation.width ||
3808 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003809 window_schedule_redraw(widget->window);
3810 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003811 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003812
3813 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003814 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003815 widget->allocation.width,
3816 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003817}
3818
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003819static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003820hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3821{
3822 /*
3823 * This hack should be removed, when EGL respects
3824 * eglSwapInterval(0).
3825 *
3826 * If this window has sub-surfaces, especially a free-running
3827 * EGL-widget, we need to post the parent surface once with
3828 * all the old state to guarantee, that the EGL-widget will
3829 * receive its frame callback soon. Otherwise, a forced call
3830 * to eglSwapBuffers may end up blocking, waiting for a frame
3831 * event that will never come, because we will commit the parent
3832 * surface with all new state only after eglSwapBuffers returns.
3833 *
3834 * This assumes, that:
3835 * 1. When the EGL widget's resize hook is called, it pauses.
3836 * 2. When the EGL widget's redraw hook is called, it forces a
3837 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3838 * In a single threaded application condition 1 is a no-op.
3839 *
3840 * XXX: This should actually be after the surface_resize() calls,
3841 * but cannot, because then it would commit the incomplete state
3842 * accumulated from the widget resize hooks.
3843 */
3844 if (window->subsurface_list.next != &window->main_surface->link ||
3845 window->subsurface_list.prev != &window->main_surface->link)
3846 wl_surface_commit(window->main_surface->surface);
3847}
3848
3849static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003850window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003851{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003852 struct surface *surface;
3853
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003854 widget_set_allocation(window->main_surface->widget,
3855 window->pending_allocation.x,
3856 window->pending_allocation.y,
3857 window->pending_allocation.width,
3858 window->pending_allocation.height);
3859
3860 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003861
3862 /* The main surface is in the list, too. Main surface's
3863 * resize_handler is responsible for calling widget_set_allocation()
3864 * on all sub-surface root widgets, so they will be resized
3865 * properly.
3866 */
3867 wl_list_for_each(surface, &window->subsurface_list, link) {
3868 if (surface == window->main_surface)
3869 continue;
3870
3871 surface_set_synchronized(surface);
3872 surface_resize(surface);
3873 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003874
3875 if (!window->fullscreen && !window->maximized)
3876 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003877}
3878
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003879static void
3880idle_resize(struct window *window)
3881{
3882 window->resize_needed = 0;
3883 window->redraw_needed = 1;
3884
3885 DBG("from %dx%d to %dx%d\n",
3886 window->main_surface->server_allocation.width,
3887 window->main_surface->server_allocation.height,
3888 window->pending_allocation.width,
3889 window->pending_allocation.height);
3890
3891 hack_prevent_EGL_sub_surface_deadlock(window);
3892
3893 window_do_resize(window);
3894}
3895
3896static void
3897undo_resize(struct window *window)
3898{
3899 window->pending_allocation.width =
3900 window->main_surface->server_allocation.width;
3901 window->pending_allocation.height =
3902 window->main_surface->server_allocation.height;
3903
3904 DBG("back to %dx%d\n",
3905 window->main_surface->server_allocation.width,
3906 window->main_surface->server_allocation.height);
3907
3908 window_do_resize(window);
3909
3910 if (window->pending_allocation.width == 0 &&
3911 window->pending_allocation.height == 0) {
3912 fprintf(stderr, "Error: Could not draw a surface, "
3913 "most likely due to insufficient disk space in "
3914 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3915 exit(EXIT_FAILURE);
3916 }
3917}
3918
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003919void
3920window_schedule_resize(struct window *window, int width, int height)
3921{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003922 /* We should probably get these numbers from the theme. */
3923 const int min_width = 200, min_height = 200;
3924
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003925 window->pending_allocation.x = 0;
3926 window->pending_allocation.y = 0;
3927 window->pending_allocation.width = width;
3928 window->pending_allocation.height = height;
3929
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003930 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003931 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003932 window->min_allocation.width = min_width;
3933 else
3934 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003935 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003936 window->min_allocation.height = min_height;
3937 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003938 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003939 }
3940
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003941 if (window->pending_allocation.width < window->min_allocation.width)
3942 window->pending_allocation.width = window->min_allocation.width;
3943 if (window->pending_allocation.height < window->min_allocation.height)
3944 window->pending_allocation.height = window->min_allocation.height;
3945
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003946 window->resize_needed = 1;
3947 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003948}
3949
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003950void
3951widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3952{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003953 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003954}
3955
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003956static int
3957window_get_shadow_margin(struct window *window)
3958{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003959 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003960 return frame_get_shadow_margin(window->frame->frame);
3961 else
3962 return 0;
3963}
3964
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003965static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003966handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003967 int32_t width, int32_t height,
3968 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003969{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003970 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003971 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003972
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003973 window->maximized = 0;
3974 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003975 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003976 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003977
3978 wl_array_for_each(p, states) {
3979 uint32_t state = *p;
3980 switch (state) {
3981 case XDG_SURFACE_STATE_MAXIMIZED:
3982 window->maximized = 1;
3983 break;
3984 case XDG_SURFACE_STATE_FULLSCREEN:
3985 window->fullscreen = 1;
3986 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003987 case XDG_SURFACE_STATE_RESIZING:
3988 window->resizing = 1;
3989 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003990 case XDG_SURFACE_STATE_ACTIVATED:
3991 window->focused = 1;
3992 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003993 default:
3994 /* Unknown state */
3995 break;
3996 }
3997 }
3998
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003999 if (window->frame) {
4000 if (window->maximized) {
4001 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4002 } else {
4003 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4004 }
4005
4006 if (window->focused) {
4007 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4008 } else {
4009 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4010 }
4011 }
4012
Jasper St. Pierref184c382014-05-06 08:33:27 -04004013 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004014 /* The width / height params are for window geometry,
4015 * but window_schedule_resize takes allocation. Add
4016 * on the shadow margin to get the difference. */
4017 int margin = window_get_shadow_margin(window);
4018
4019 window_schedule_resize(window,
4020 width + margin * 2,
4021 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004022 } else {
4023 window_schedule_resize(window,
4024 window->saved_allocation.width,
4025 window->saved_allocation.height);
4026 }
4027
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004028 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004029
4030 if (window->state_changed_handler)
4031 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004032}
4033
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004034static void
4035handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4036{
4037 struct window *window = data;
4038 window_close(window);
4039}
4040
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004041static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004042 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004043 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004044};
4045
4046static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004047window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004048{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004049 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004050
4051 if (!window->xdg_surface)
4052 return;
4053
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004054 if (window->parent == window->last_parent)
4055 return;
4056
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004057 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004058 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004059 else
4060 parent_surface = NULL;
4061
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004062 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004063 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004064}
4065
4066static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004067window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004068{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004069 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004070 frame_input_rect(window->frame->frame,
4071 &geometry->x,
4072 &geometry->y,
4073 &geometry->width,
4074 &geometry->height);
4075 else
4076 window_get_allocation(window, geometry);
4077}
4078
4079static void
4080window_sync_geometry(struct window *window)
4081{
4082 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004083
4084 if (!window->xdg_surface)
4085 return;
4086
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004087 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004088 if (geometry.x == window->last_geometry.x &&
4089 geometry.y == window->last_geometry.y &&
4090 geometry.width == window->last_geometry.width &&
4091 geometry.height == window->last_geometry.height)
4092 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004093
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004094 xdg_surface_set_window_geometry(window->xdg_surface,
4095 geometry.x,
4096 geometry.y,
4097 geometry.width,
4098 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004099 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004100}
4101
4102static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004103window_flush(struct window *window)
4104{
4105 struct surface *surface;
4106
4107 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004108 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004109 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004110 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004111 }
4112 }
4113
4114 wl_list_for_each(surface, &window->subsurface_list, link) {
4115 if (surface == window->main_surface)
4116 continue;
4117
4118 surface_flush(surface);
4119 }
4120
4121 surface_flush(window->main_surface);
4122}
4123
4124static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004125menu_destroy(struct menu *menu)
4126{
4127 widget_destroy(menu->widget);
4128 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004129 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004130 free(menu);
4131}
4132
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004133void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004134window_get_allocation(struct window *window,
4135 struct rectangle *allocation)
4136{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004137 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004138}
4139
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004140static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004141widget_redraw(struct widget *widget)
4142{
4143 struct widget *child;
4144
4145 if (widget->redraw_handler)
4146 widget->redraw_handler(widget, widget->user_data);
4147 wl_list_for_each(child, &widget->child_list, link)
4148 widget_redraw(child);
4149}
4150
4151static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004152frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4153{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004154 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004155
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004156 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004157 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004158 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004159 surface->frame_cb = NULL;
4160
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004161 surface->last_time = time;
4162
Pekka Paalanen71233882013-04-25 13:57:53 +03004163 if (surface->redraw_needed || surface->window->redraw_needed) {
4164 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004165 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004166 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004167}
4168
4169static const struct wl_callback_listener listener = {
4170 frame_callback
4171};
4172
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004173static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004174surface_redraw(struct surface *surface)
4175{
Pekka Paalanen71233882013-04-25 13:57:53 +03004176 DBG_OBJ(surface->surface, "begin\n");
4177
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004178 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004179 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004180
4181 /* Whole-window redraw forces a redraw even if the previous has
4182 * not yet hit the screen.
4183 */
4184 if (surface->frame_cb) {
4185 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004186 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004187
Pekka Paalanen71233882013-04-25 13:57:53 +03004188 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004189 wl_callback_destroy(surface->frame_cb);
4190 }
4191
Neil Roberts97b747c2013-12-19 16:17:12 +00004192 if (surface->widget->use_cairo &&
4193 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004194 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4195 return -1;
4196 }
4197
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004198 surface->frame_cb = wl_surface_frame(surface->surface);
4199 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004200 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004201
4202 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004203 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004204 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004205 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004206 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004207}
4208
4209static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004210idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004211{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004212 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004213 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004214 int failed = 0;
4215 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004216
Pekka Paalanen71233882013-04-25 13:57:53 +03004217 DBG(" --------- \n");
4218
Pekka Paalaneneebff542013-04-25 13:57:52 +03004219 wl_list_init(&window->redraw_task.link);
4220 window->redraw_task_scheduled = 0;
4221
4222 if (window->resize_needed) {
4223 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004224 if (window->main_surface->frame_cb) {
4225 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004226 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004227 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004228
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004229 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004230 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004231 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004232
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004233 if (surface_redraw(window->main_surface) < 0) {
4234 /*
4235 * Only main_surface failure will cause us to undo the resize.
4236 * If sub-surfaces fail, they will just be broken with old
4237 * content.
4238 */
4239 failed = 1;
4240 } else {
4241 wl_list_for_each(surface, &window->subsurface_list, link) {
4242 if (surface == window->main_surface)
4243 continue;
4244
4245 surface_redraw(surface);
4246 }
4247 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004248
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004249 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004250 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004251
4252 wl_list_for_each(surface, &window->subsurface_list, link)
4253 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004254
4255 if (resized && failed) {
4256 /* Restore widget tree to correspond to what is on screen. */
4257 undo_resize(window);
4258 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004259}
4260
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004261static void
4262window_schedule_redraw_task(struct window *window)
4263{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004264 if (!window->redraw_task_scheduled) {
4265 window->redraw_task.run = idle_redraw;
4266 display_defer(window->display, &window->redraw_task);
4267 window->redraw_task_scheduled = 1;
4268 }
4269}
4270
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004271void
4272window_schedule_redraw(struct window *window)
4273{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004274 struct surface *surface;
4275
Pekka Paalanen71233882013-04-25 13:57:53 +03004276 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4277
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004278 wl_list_for_each(surface, &window->subsurface_list, link)
4279 surface->redraw_needed = 1;
4280
4281 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004282}
4283
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004284int
4285window_is_fullscreen(struct window *window)
4286{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004287 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004288}
4289
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004290void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004291window_set_fullscreen(struct window *window, int fullscreen)
4292{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004293 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004294 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004295
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004296 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004297 return;
4298
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004299 if (fullscreen)
4300 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4301 else
4302 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004303}
4304
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004305int
4306window_is_maximized(struct window *window)
4307{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004308 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004309}
4310
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004311void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004312window_set_maximized(struct window *window, int maximized)
4313{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004314 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004315 return;
4316
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004317 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004318 return;
4319
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004320 if (maximized)
4321 xdg_surface_set_maximized(window->xdg_surface);
4322 else
4323 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004324}
4325
Jasper St. Pierrede680992014-04-10 17:23:49 -07004326int
4327window_is_resizing(struct window *window)
4328{
4329 return window->resizing;
4330}
4331
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004332void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004333window_set_minimized(struct window *window)
4334{
4335 if (!window->xdg_surface)
4336 return;
4337
4338 xdg_surface_set_minimized(window->xdg_surface);
4339}
4340
4341void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004342window_set_user_data(struct window *window, void *data)
4343{
4344 window->user_data = data;
4345}
4346
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004347void *
4348window_get_user_data(struct window *window)
4349{
4350 return window->user_data;
4351}
4352
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004353void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004354window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004355 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004356{
4357 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004358}
4359
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004360void
4361window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004362 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004363{
4364 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004365}
4366
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004367void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004368window_set_data_handler(struct window *window, window_data_handler_t handler)
4369{
4370 window->data_handler = handler;
4371}
4372
4373void
4374window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4375{
4376 window->drop_handler = handler;
4377}
4378
4379void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004380window_set_close_handler(struct window *window,
4381 window_close_handler_t handler)
4382{
4383 window->close_handler = handler;
4384}
4385
4386void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004387window_set_fullscreen_handler(struct window *window,
4388 window_fullscreen_handler_t handler)
4389{
4390 window->fullscreen_handler = handler;
4391}
4392
4393void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004394window_set_output_handler(struct window *window,
4395 window_output_handler_t handler)
4396{
4397 window->output_handler = handler;
4398}
4399
4400void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004401window_set_state_changed_handler(struct window *window,
4402 window_state_changed_handler_t handler)
4403{
4404 window->state_changed_handler = handler;
4405}
4406
4407void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004408window_set_title(struct window *window, const char *title)
4409{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004410 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004411 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004412 if (window->frame) {
4413 frame_set_title(window->frame->frame, title);
4414 widget_schedule_redraw(window->frame->widget);
4415 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004416 if (window->xdg_surface)
4417 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004418}
4419
4420const char *
4421window_get_title(struct window *window)
4422{
4423 return window->title;
4424}
4425
4426void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004427window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4428{
4429 struct text_cursor_position *text_cursor_position =
4430 window->display->text_cursor_position;
4431
Scott Moreau9295ce02012-06-01 12:46:10 -06004432 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004433 return;
4434
4435 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004436 window->main_surface->surface,
4437 wl_fixed_from_int(x),
4438 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004439}
4440
4441void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004442window_damage(struct window *window, int32_t x, int32_t y,
4443 int32_t width, int32_t height)
4444{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004445 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004446}
4447
Casey Dahlin9074db52012-04-19 22:50:09 -04004448static void
4449surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004450 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004451{
Rob Bradford7507b572012-05-15 17:55:34 +01004452 struct window *window = data;
4453 struct output *output;
4454 struct output *output_found = NULL;
4455 struct window_output *window_output;
4456
4457 wl_list_for_each(output, &window->display->output_list, link) {
4458 if (output->output == wl_output) {
4459 output_found = output;
4460 break;
4461 }
4462 }
4463
4464 if (!output_found)
4465 return;
4466
Brian Lovinbc919262013-08-07 15:34:59 -07004467 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004468 window_output->output = output_found;
4469
4470 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004471
4472 if (window->output_handler)
4473 window->output_handler(window, output_found, 1,
4474 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004475}
4476
4477static void
4478surface_leave(void *data,
4479 struct wl_surface *wl_surface, struct wl_output *output)
4480{
Rob Bradford7507b572012-05-15 17:55:34 +01004481 struct window *window = data;
4482 struct window_output *window_output;
4483 struct window_output *window_output_found = NULL;
4484
4485 wl_list_for_each(window_output, &window->window_output_list, link) {
4486 if (window_output->output->output == output) {
4487 window_output_found = window_output;
4488 break;
4489 }
4490 }
4491
4492 if (window_output_found) {
4493 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004494
4495 if (window->output_handler)
4496 window->output_handler(window, window_output->output,
4497 0, window->user_data);
4498
Rob Bradford7507b572012-05-15 17:55:34 +01004499 free(window_output_found);
4500 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004501}
4502
4503static const struct wl_surface_listener surface_listener = {
4504 surface_enter,
4505 surface_leave
4506};
4507
Pekka Paalanen4e373742013-02-13 16:17:13 +02004508static struct surface *
4509surface_create(struct window *window)
4510{
4511 struct display *display = window->display;
4512 struct surface *surface;
4513
Brian Lovinbc919262013-08-07 15:34:59 -07004514 surface = xmalloc(sizeof *surface);
4515 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004516 if (!surface)
4517 return NULL;
4518
4519 surface->window = window;
4520 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004521 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004522 wl_surface_add_listener(surface->surface, &surface_listener, window);
4523
Pekka Paalanen35e82632013-04-25 13:57:48 +03004524 wl_list_insert(&window->subsurface_list, &surface->link);
4525
Pekka Paalanen4e373742013-02-13 16:17:13 +02004526 return surface;
4527}
4528
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004529static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004530get_preferred_buffer_type(struct display *display)
4531{
4532#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004533 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004534 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4535#endif
4536
4537 return WINDOW_BUFFER_TYPE_SHM;
4538}
4539
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004540static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004541window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004542{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004543 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004544 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004545
Peter Huttererf3d62272013-08-08 11:57:05 +10004546 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004547 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004548 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004549
4550 surface = surface_create(window);
4551 window->main_surface = surface;
4552
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004553 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004554
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004555 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004556 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004557
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004558 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004559
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004560 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004561 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004562 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004563
Rob Bradford7507b572012-05-15 17:55:34 +01004564 wl_list_init (&window->window_output_list);
4565
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004566 return window;
4567}
4568
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004569struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004570window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004571{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004572 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004573 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004574
4575 window = window_create_internal(display, 0);
4576
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004577 if (window->display->xdg_shell) {
4578 window->xdg_surface =
4579 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4580 window->main_surface->surface);
4581 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004582
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004583 xdg_surface_set_user_data(window->xdg_surface, window);
4584 xdg_surface_add_listener(window->xdg_surface,
4585 &xdg_surface_listener, window);
4586 } else if (display->ivi_application) {
4587 /* auto generation of ivi_id based on process id + basement of id */
4588 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4589 window->ivi_surface =
4590 ivi_application_surface_create(display->ivi_application,
4591 id_ivisurf, window->main_surface->surface);
4592 fail_on_null(window->ivi_surface);
4593
4594 ivi_surface_add_listener(window->ivi_surface,
4595 &ivi_surface_listener, window);
4596 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004597
4598 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004599}
4600
4601struct window *
4602window_create_custom(struct display *display)
4603{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004604 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004605}
4606
Jasper St. Pierre53686042013-12-09 15:26:25 -05004607void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004608window_set_parent(struct window *window,
4609 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004610{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004611 window->parent = parent_window;
4612 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004613}
4614
4615struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004616window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004617{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004618 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004619}
4620
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004621static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004622menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004623{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004624 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004625 int next;
4626
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004627 frame_interior(menu->frame, &x, &y, &width, &height);
4628 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004629 if (menu->current != next) {
4630 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004631 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004632 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004633}
4634
4635static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004636menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004637 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004638 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004639{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004640 struct menu *menu = data;
4641
4642 if (widget == menu->widget)
4643 menu_set_item(data, y);
4644
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004645 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004646}
4647
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004648static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004649menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004650 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004651{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004652 struct menu *menu = data;
4653
4654 if (widget == menu->widget)
4655 menu_set_item(data, y);
4656
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004657 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004658}
4659
4660static void
4661menu_leave_handler(struct widget *widget, struct input *input, void *data)
4662{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004663 struct menu *menu = data;
4664
4665 if (widget == menu->widget)
4666 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004667}
4668
4669static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004670menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004671 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004672 uint32_t button, enum wl_pointer_button_state state,
4673 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004674
4675{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004676 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004677
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004678 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4679 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004680 /* Either relase after press-drag-release or
4681 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004682 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004683 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004684 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004685 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004686 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004687 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004688}
4689
4690static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004691menu_touch_up_handler(struct widget *widget,
4692 struct input *input,
4693 uint32_t serial,
4694 uint32_t time,
4695 int32_t id,
4696 void *data)
4697{
4698 struct menu *menu = data;
4699
4700 input_ungrab(input);
4701 menu_destroy(menu);
4702}
4703
4704static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004705menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004706{
4707 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004708 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004709 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004710
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004711 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004712
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004713 frame_repaint(menu->frame, cr);
4714 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004715
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004716 theme_set_background_source(menu->window->display->theme,
4717 cr, THEME_FRAME_ACTIVE);
4718 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004719 cairo_fill(cr);
4720
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004721 cairo_select_font_face(cr, "sans",
4722 CAIRO_FONT_SLANT_NORMAL,
4723 CAIRO_FONT_WEIGHT_NORMAL);
4724 cairo_set_font_size(cr, 12);
4725
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004726 for (i = 0; i < menu->count; i++) {
4727 if (i == menu->current) {
4728 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004729 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004730 cairo_fill(cr);
4731 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004732 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004733 cairo_show_text(cr, menu->entries[i]);
4734 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004735 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4736 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004737 cairo_show_text(cr, menu->entries[i]);
4738 }
4739 }
4740
4741 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004742}
4743
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004744static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004745handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004746{
4747 struct window *window = data;
4748 struct menu *menu = window->main_surface->widget->user_data;
4749
4750 input_ungrab(menu->input);
4751 menu_destroy(menu);
4752}
4753
4754static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004755 handle_popup_popup_done,
4756};
4757
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004758static struct menu *
4759create_menu(struct display *display,
4760 struct input *input, uint32_t time,
4761 menu_func_t func, const char **entries, int count,
4762 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004763{
4764 struct window *window;
4765 struct menu *menu;
4766
4767 menu = malloc(sizeof *menu);
4768 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004769 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004770
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004771 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004772 if (!window) {
4773 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004774 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004775 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004776
4777 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004778 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004779 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004780 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004781 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004782 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004783 menu->entries = entries;
4784 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004785 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004786 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004787 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004788 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004789 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004790
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004791 input_ungrab(input);
4792
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004793 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004794 widget_set_enter_handler(menu->widget, menu_enter_handler);
4795 widget_set_leave_handler(menu->widget, menu_leave_handler);
4796 widget_set_motion_handler(menu->widget, menu_motion_handler);
4797 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004798 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004799
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004800 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004801 frame_resize_inside(menu->frame, 200, count * 20);
4802 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4803 window_schedule_resize(window, frame_width(menu->frame),
4804 frame_height(menu->frame));
4805
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004806 return menu;
4807}
4808
4809struct window *
4810window_create_menu(struct display *display,
4811 struct input *input, uint32_t time,
4812 menu_func_t func, const char **entries, int count,
4813 void *user_data)
4814{
4815 struct menu *menu;
4816 menu = create_menu(display, input, time, func, entries, count, user_data);
4817
4818 if (menu == NULL)
4819 return NULL;
4820
4821 return menu->window;
4822}
4823
4824void
4825window_show_menu(struct display *display,
4826 struct input *input, uint32_t time, struct window *parent,
4827 int32_t x, int32_t y,
4828 menu_func_t func, const char **entries, int count)
4829{
4830 struct menu *menu;
4831 struct window *window;
4832 int32_t ix, iy;
4833
4834 menu = create_menu(display, input, time, func, entries, count, parent);
4835
4836 if (menu == NULL)
4837 return;
4838
4839 window = menu->window;
4840
4841 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4842 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4843
4844 window->x = x;
4845 window->y = y;
4846
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004847 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004848
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004849 if (!display->xdg_shell)
4850 return;
4851
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004852 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4853 window->main_surface->surface,
4854 parent->main_surface->surface,
4855 input->seat,
4856 display_get_serial(window->display),
4857 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004858 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004859 fail_on_null(window->xdg_popup);
4860
4861 xdg_popup_set_user_data(window->xdg_popup, window);
4862 xdg_popup_add_listener(window->xdg_popup,
4863 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004864}
4865
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004866void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004867window_set_buffer_type(struct window *window, enum window_buffer_type type)
4868{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004869 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004870}
4871
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004872enum window_buffer_type
4873window_get_buffer_type(struct window *window)
4874{
4875 return window->main_surface->buffer_type;
4876}
4877
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004878void
4879window_set_preferred_format(struct window *window,
4880 enum preferred_format format)
4881{
4882 window->preferred_format = format;
4883}
4884
Pekka Paalanen35e82632013-04-25 13:57:48 +03004885struct widget *
4886window_add_subsurface(struct window *window, void *data,
4887 enum subsurface_mode default_mode)
4888{
4889 struct widget *widget;
4890 struct surface *surface;
4891 struct wl_surface *parent;
4892 struct wl_subcompositor *subcompo = window->display->subcompositor;
4893
Pekka Paalanen35e82632013-04-25 13:57:48 +03004894 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004895 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004896 widget = widget_create(window, surface, data);
4897 wl_list_init(&widget->link);
4898 surface->widget = widget;
4899
4900 parent = window->main_surface->surface;
4901 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4902 surface->surface,
4903 parent);
4904 surface->synchronized = 1;
4905
4906 switch (default_mode) {
4907 case SUBSURFACE_SYNCHRONIZED:
4908 surface->synchronized_default = 1;
4909 break;
4910 case SUBSURFACE_DESYNCHRONIZED:
4911 surface->synchronized_default = 0;
4912 break;
4913 default:
4914 assert(!"bad enum subsurface_mode");
4915 }
4916
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004917 window->resize_needed = 1;
4918 window_schedule_redraw(window);
4919
Pekka Paalanen35e82632013-04-25 13:57:48 +03004920 return widget;
4921}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004922
4923static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004924display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004925 struct wl_output *wl_output,
4926 int x, int y,
4927 int physical_width,
4928 int physical_height,
4929 int subpixel,
4930 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004931 const char *model,
4932 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004933{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004934 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004935
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004936 output->allocation.x = x;
4937 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004938 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004939
4940 if (output->make)
4941 free(output->make);
4942 output->make = strdup(make);
4943
4944 if (output->model)
4945 free(output->model);
4946 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004947}
4948
4949static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004950display_handle_done(void *data,
4951 struct wl_output *wl_output)
4952{
4953}
4954
4955static void
4956display_handle_scale(void *data,
4957 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004958 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004959{
4960 struct output *output = data;
4961
4962 output->scale = scale;
4963}
4964
4965static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004966display_handle_mode(void *data,
4967 struct wl_output *wl_output,
4968 uint32_t flags,
4969 int width,
4970 int height,
4971 int refresh)
4972{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004973 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004974 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004975
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004976 if (flags & WL_OUTPUT_MODE_CURRENT) {
4977 output->allocation.width = width;
4978 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004979 if (display->output_configure_handler)
4980 (*display->output_configure_handler)(
4981 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004982 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004983}
4984
4985static const struct wl_output_listener output_listener = {
4986 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004987 display_handle_mode,
4988 display_handle_done,
4989 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004990};
4991
4992static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004993display_add_output(struct display *d, uint32_t id)
4994{
4995 struct output *output;
4996
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004997 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004998 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004999 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005000 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005001 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005002 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005003 wl_list_insert(d->output_list.prev, &output->link);
5004
5005 wl_output_add_listener(output->output, &output_listener, output);
5006}
5007
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005008static void
5009output_destroy(struct output *output)
5010{
5011 if (output->destroy_handler)
5012 (*output->destroy_handler)(output, output->user_data);
5013
5014 wl_output_destroy(output->output);
5015 wl_list_remove(&output->link);
5016 free(output);
5017}
5018
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005019static void
5020display_destroy_output(struct display *d, uint32_t id)
5021{
5022 struct output *output;
5023
5024 wl_list_for_each(output, &d->output_list, link) {
5025 if (output->server_output_id == id) {
5026 output_destroy(output);
5027 break;
5028 }
5029 }
5030}
5031
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005032void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005033display_set_global_handler(struct display *display,
5034 display_global_handler_t handler)
5035{
5036 struct global *global;
5037
5038 display->global_handler = handler;
5039 if (!handler)
5040 return;
5041
5042 wl_list_for_each(global, &display->global_list, link)
5043 display->global_handler(display,
5044 global->name, global->interface,
5045 global->version, display->user_data);
5046}
5047
5048void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005049display_set_global_handler_remove(struct display *display,
5050 display_global_handler_t remove_handler)
5051{
5052 display->global_handler_remove = remove_handler;
5053 if (!remove_handler)
5054 return;
5055}
5056
5057void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005058display_set_output_configure_handler(struct display *display,
5059 display_output_handler_t handler)
5060{
5061 struct output *output;
5062
5063 display->output_configure_handler = handler;
5064 if (!handler)
5065 return;
5066
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005067 wl_list_for_each(output, &display->output_list, link) {
5068 if (output->allocation.width == 0 &&
5069 output->allocation.height == 0)
5070 continue;
5071
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005072 (*display->output_configure_handler)(output,
5073 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005074 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005075}
5076
5077void
5078output_set_user_data(struct output *output, void *data)
5079{
5080 output->user_data = data;
5081}
5082
5083void *
5084output_get_user_data(struct output *output)
5085{
5086 return output->user_data;
5087}
5088
5089void
5090output_set_destroy_handler(struct output *output,
5091 display_output_handler_t handler)
5092{
5093 output->destroy_handler = handler;
5094 /* FIXME: implement this, once we have way to remove outputs */
5095}
5096
5097void
Scott Moreau4e072362012-09-29 02:03:11 -06005098output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005099{
Scott Moreau4e072362012-09-29 02:03:11 -06005100 struct rectangle allocation = output->allocation;
5101
5102 switch (output->transform) {
5103 case WL_OUTPUT_TRANSFORM_90:
5104 case WL_OUTPUT_TRANSFORM_270:
5105 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5106 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5107 /* Swap width and height */
5108 allocation.width = output->allocation.height;
5109 allocation.height = output->allocation.width;
5110 break;
5111 }
5112
5113 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005114}
5115
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005116struct wl_output *
5117output_get_wl_output(struct output *output)
5118{
5119 return output->output;
5120}
5121
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005122enum wl_output_transform
5123output_get_transform(struct output *output)
5124{
5125 return output->transform;
5126}
5127
Alexander Larssonafd319a2013-05-22 14:41:27 +02005128uint32_t
5129output_get_scale(struct output *output)
5130{
5131 return output->scale;
5132}
5133
Jason Ekstrand738715d2014-04-02 19:53:50 -05005134const char *
5135output_get_make(struct output *output)
5136{
5137 return output->make;
5138}
5139
5140const char *
5141output_get_model(struct output *output)
5142{
5143 return output->model;
5144}
5145
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005146static void
Daniel Stone97f68542012-05-30 16:32:01 +01005147fini_xkb(struct input *input)
5148{
5149 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005150 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005151}
5152
Jasper St. Pierre47f10432013-11-12 14:37:20 -05005153#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005154
Daniel Stone97f68542012-05-30 16:32:01 +01005155static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005156display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005157{
5158 struct input *input;
5159
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005160 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005161 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005162 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005163 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005164 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005165 input->pointer_focus = NULL;
5166 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005167 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005168 wl_list_insert(d->input_list.prev, &input->link);
5169
Daniel Stone37816df2012-05-16 18:45:18 +01005170 wl_seat_add_listener(input->seat, &seat_listener, input);
5171 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005172
Jason Ekstranda669bd52014-04-02 19:53:51 -05005173 if (d->data_device_manager) {
5174 input->data_device =
5175 wl_data_device_manager_get_data_device(d->data_device_manager,
5176 input->seat);
5177 wl_data_device_add_listener(input->data_device,
5178 &data_device_listener,
5179 input);
5180 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005181
5182 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005183
Jonny Lamb06959082014-08-12 14:58:27 +02005184 set_repeat_info(input, 40, 400);
5185
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005186 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5187 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005188 input->repeat_task.run = keyboard_repeat_func;
5189 display_watch_fd(d, input->repeat_timer_fd,
5190 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005191}
5192
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005193static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005194input_destroy(struct input *input)
5195{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005196 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005197 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005198
5199 if (input->drag_offer)
5200 data_offer_destroy(input->drag_offer);
5201
5202 if (input->selection_offer)
5203 data_offer_destroy(input->selection_offer);
5204
kabeer khan6ce67ec2014-10-20 11:55:29 +05305205 if (input->data_device) {
5206 if(input->display->data_device_manager_version >= 2)
5207 wl_data_device_release(input->data_device);
5208 else
5209 wl_data_device_destroy(input->data_device);
5210 }
Rob Bradford08031182013-08-13 20:11:03 +01005211 if (input->display->seat_version >= 3) {
5212 if (input->pointer)
5213 wl_pointer_release(input->pointer);
5214 if (input->keyboard)
5215 wl_keyboard_release(input->keyboard);
5216 }
5217
Daniel Stone97f68542012-05-30 16:32:01 +01005218 fini_xkb(input);
5219
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005220 wl_surface_destroy(input->pointer_surface);
5221
Pekka Paalanene1207c72011-12-16 12:02:09 +02005222 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005223 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005224 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005225 free(input);
5226}
5227
5228static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005229init_workspace_manager(struct display *d, uint32_t id)
5230{
5231 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005232 wl_registry_bind(d->registry, id,
5233 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005234 if (d->workspace_manager != NULL)
5235 workspace_manager_add_listener(d->workspace_manager,
5236 &workspace_manager_listener,
5237 d);
5238}
5239
5240static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005241shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5242{
5243 struct display *d = data;
5244
5245 if (format == WL_SHM_FORMAT_RGB565)
5246 d->has_rgb565 = 1;
5247}
5248
5249struct wl_shm_listener shm_listener = {
5250 shm_format
5251};
5252
5253static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005254xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5255{
5256 xdg_shell_pong(shell, serial);
5257}
5258
5259static const struct xdg_shell_listener xdg_shell_listener = {
5260 xdg_shell_ping,
5261};
5262
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005263#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005264#ifdef static_assert
5265static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5266 "Interface version doesn't match implementation version");
5267#endif
5268
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005269static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005270registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5271 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005272{
5273 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005274 struct global *global;
5275
Brian Lovinbc919262013-08-07 15:34:59 -07005276 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005277 global->name = id;
5278 global->interface = strdup(interface);
5279 global->version = version;
5280 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005281
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005282 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005283 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005284 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005285 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005286 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005287 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005288 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005289 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005290 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005291 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005292 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005293 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305294 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005295 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005296 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305297 &wl_data_device_manager_interface,
5298 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005299 } else if (strcmp(interface, "xdg_shell") == 0) {
5300 d->xdg_shell = wl_registry_bind(registry, id,
5301 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005302 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005303 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005304 } else if (strcmp(interface, "text_cursor_position") == 0) {
5305 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005306 wl_registry_bind(registry, id,
5307 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005308 } else if (strcmp(interface, "workspace_manager") == 0) {
5309 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005310 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5311 d->subcompositor =
5312 wl_registry_bind(registry, id,
5313 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005314 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005315 else if (strcmp(interface, "ivi_application") == 0) {
5316 d->ivi_application =
5317 wl_registry_bind(registry, id,
5318 &ivi_application_interface, 1);
5319 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005320
5321 if (d->global_handler)
5322 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005323}
5324
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005325static void
5326registry_handle_global_remove(void *data, struct wl_registry *registry,
5327 uint32_t name)
5328{
5329 struct display *d = data;
5330 struct global *global;
5331 struct global *tmp;
5332
5333 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5334 if (global->name != name)
5335 continue;
5336
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005337 if (strcmp(global->interface, "wl_output") == 0)
5338 display_destroy_output(d, name);
5339
5340 /* XXX: Should destroy remaining bound globals */
5341
5342 if (d->global_handler_remove)
5343 d->global_handler_remove(d, name, global->interface,
5344 global->version, d->user_data);
5345
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005346 wl_list_remove(&global->link);
5347 free(global->interface);
5348 free(global);
5349 }
5350}
5351
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005352void *
5353display_bind(struct display *display, uint32_t name,
5354 const struct wl_interface *interface, uint32_t version)
5355{
5356 return wl_registry_bind(display->registry, name, interface, version);
5357}
5358
5359static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005360 registry_handle_global,
5361 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005362};
5363
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005364#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005365static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005366init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005367{
5368 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005369 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005370
Rob Clark6396ed32012-03-11 19:48:41 -05005371#ifdef USE_CAIRO_GLESV2
5372# define GL_BIT EGL_OPENGL_ES2_BIT
5373#else
5374# define GL_BIT EGL_OPENGL_BIT
5375#endif
5376
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005377 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005378 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005379 EGL_RED_SIZE, 1,
5380 EGL_GREEN_SIZE, 1,
5381 EGL_BLUE_SIZE, 1,
5382 EGL_ALPHA_SIZE, 1,
5383 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005384 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005385 EGL_NONE
5386 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005387
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005388#ifdef USE_CAIRO_GLESV2
5389 static const EGLint context_attribs[] = {
5390 EGL_CONTEXT_CLIENT_VERSION, 2,
5391 EGL_NONE
5392 };
5393 EGLint api = EGL_OPENGL_ES_API;
5394#else
5395 EGLint *context_attribs = NULL;
5396 EGLint api = EGL_OPENGL_API;
5397#endif
5398
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005399 d->dpy =
5400 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5401 d->display, NULL);
5402
Yuval Fledel45568f62010-12-06 09:18:12 -05005403 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005404 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005405 return -1;
5406 }
5407
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005408 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005409 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005410 return -1;
5411 }
5412
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005413 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5414 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005415 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005416 return -1;
5417 }
5418
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005419 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005420 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005421 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005422 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005423 return -1;
5424 }
5425
Benjamin Franzke0c991632011-09-27 21:57:31 +02005426 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5427 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005428 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005429 return -1;
5430 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005431
5432 return 0;
5433}
5434
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005435static void
5436fini_egl(struct display *display)
5437{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005438 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005439
5440 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5441 EGL_NO_CONTEXT);
5442
5443 eglTerminate(display->dpy);
5444 eglReleaseThread();
5445}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005446#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005447
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005448static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005449init_dummy_surface(struct display *display)
5450{
5451 int len;
5452 void *data;
5453
5454 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005455 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005456 display->dummy_surface =
5457 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5458 1, 1, len);
5459 display->dummy_surface_data = data;
5460}
5461
5462static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005463handle_display_data(struct task *task, uint32_t events)
5464{
5465 struct display *display =
5466 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005467 struct epoll_event ep;
5468 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005469
5470 display->display_fd_events = events;
5471
5472 if (events & EPOLLERR || events & EPOLLHUP) {
5473 display_exit(display);
5474 return;
5475 }
5476
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005477 if (events & EPOLLIN) {
5478 ret = wl_display_dispatch(display->display);
5479 if (ret == -1) {
5480 display_exit(display);
5481 return;
5482 }
5483 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005484
5485 if (events & EPOLLOUT) {
5486 ret = wl_display_flush(display->display);
5487 if (ret == 0) {
5488 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5489 ep.data.ptr = &display->display_task;
5490 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5491 display->display_fd, &ep);
5492 } else if (ret == -1 && errno != EAGAIN) {
5493 display_exit(display);
5494 return;
5495 }
5496 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005497}
5498
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005499static void
5500log_handler(const char *format, va_list args)
5501{
5502 vfprintf(stderr, format, args);
5503}
5504
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005505struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005506display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005507{
5508 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005509
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005510 wl_log_set_handler_client(log_handler);
5511
Peter Huttererf3d62272013-08-08 11:57:05 +10005512 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005513 if (d == NULL)
5514 return NULL;
5515
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005516 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005517 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005518 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005519 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005520 return NULL;
5521 }
5522
Rob Bradford5ab9c752013-07-26 16:29:43 +01005523 d->xkb_context = xkb_context_new(0);
5524 if (d->xkb_context == NULL) {
5525 fprintf(stderr, "Failed to create XKB context\n");
5526 free(d);
5527 return NULL;
5528 }
5529
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005530 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005531 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005532 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005533 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5534 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005535
5536 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005537 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005538 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005539 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005540
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005541 d->workspace = 0;
5542 d->workspace_count = 1;
5543
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005544 d->registry = wl_display_get_registry(d->display);
5545 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005546
Marek Chalupaa519d062014-12-05 13:49:40 +01005547 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005548 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5549 return NULL;
5550 }
5551
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005552#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005553 if (init_egl(d) < 0)
5554 fprintf(stderr, "EGL does not seem to work, "
5555 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005556#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005557
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005558 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005559
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005560 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005561
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005562 wl_list_init(&d->window_list);
5563
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005564 init_dummy_surface(d);
5565
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005566 return d;
5567}
5568
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005569static void
5570display_destroy_outputs(struct display *display)
5571{
5572 struct output *tmp;
5573 struct output *output;
5574
5575 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5576 output_destroy(output);
5577}
5578
Pekka Paalanene1207c72011-12-16 12:02:09 +02005579static void
5580display_destroy_inputs(struct display *display)
5581{
5582 struct input *tmp;
5583 struct input *input;
5584
5585 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5586 input_destroy(input);
5587}
5588
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005589void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005590display_destroy(struct display *display)
5591{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005592 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005593 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5594 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005595
5596 if (!wl_list_empty(&display->deferred_list))
5597 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5598
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005599 cairo_surface_destroy(display->dummy_surface);
5600 free(display->dummy_surface_data);
5601
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005602 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005603 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005604
Daniel Stone97f68542012-05-30 16:32:01 +01005605 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005606
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005607 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005608 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005609
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005610#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005611 if (display->argb_device)
5612 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005613#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005614
Pekka Paalanen35e82632013-04-25 13:57:48 +03005615 if (display->subcompositor)
5616 wl_subcompositor_destroy(display->subcompositor);
5617
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005618 if (display->xdg_shell)
5619 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005620
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005621 if (display->ivi_application)
5622 ivi_application_destroy(display->ivi_application);
5623
Pekka Paalanenc2052982011-12-16 11:41:32 +02005624 if (display->shm)
5625 wl_shm_destroy(display->shm);
5626
5627 if (display->data_device_manager)
5628 wl_data_device_manager_destroy(display->data_device_manager);
5629
5630 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005631 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005632
5633 close(display->epoll_fd);
5634
U. Artie Eoff44874d92012-10-02 21:12:35 -07005635 if (!(display->display_fd_events & EPOLLERR) &&
5636 !(display->display_fd_events & EPOLLHUP))
5637 wl_display_flush(display->display);
5638
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005639 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005640 free(display);
5641}
5642
5643void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005644display_set_user_data(struct display *display, void *data)
5645{
5646 display->user_data = data;
5647}
5648
5649void *
5650display_get_user_data(struct display *display)
5651{
5652 return display->user_data;
5653}
5654
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005655struct wl_display *
5656display_get_display(struct display *display)
5657{
5658 return display->display;
5659}
5660
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005661int
5662display_has_subcompositor(struct display *display)
5663{
5664 if (display->subcompositor)
5665 return 1;
5666
5667 wl_display_roundtrip(display->display);
5668
5669 return display->subcompositor != NULL;
5670}
5671
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005672cairo_device_t *
5673display_get_cairo_device(struct display *display)
5674{
5675 return display->argb_device;
5676}
5677
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005678struct output *
5679display_get_output(struct display *display)
5680{
5681 return container_of(display->output_list.next, struct output, link);
5682}
5683
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005684struct wl_compositor *
5685display_get_compositor(struct display *display)
5686{
5687 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005688}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005689
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005690uint32_t
5691display_get_serial(struct display *display)
5692{
5693 return display->serial;
5694}
5695
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005696EGLDisplay
5697display_get_egl_display(struct display *d)
5698{
5699 return d->dpy;
5700}
5701
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005702struct wl_data_source *
5703display_create_data_source(struct display *display)
5704{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005705 if (display->data_device_manager)
5706 return wl_data_device_manager_create_data_source(display->data_device_manager);
5707 else
5708 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005709}
5710
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005711EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005712display_get_argb_egl_config(struct display *d)
5713{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005714 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005715}
5716
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005717int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005718display_acquire_window_surface(struct display *display,
5719 struct window *window,
5720 EGLContext ctx)
5721{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005722 struct surface *surface = window->main_surface;
5723
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005724 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005725 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005726
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005727 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005728 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005729}
5730
5731void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005732display_release_window_surface(struct display *display,
5733 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005734{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005735 struct surface *surface = window->main_surface;
5736
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005737 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005738 return;
5739
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005740 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005741}
5742
5743void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005744display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005745{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005746 wl_list_insert(&display->deferred_list, &task->link);
5747}
5748
5749void
5750display_watch_fd(struct display *display,
5751 int fd, uint32_t events, struct task *task)
5752{
5753 struct epoll_event ep;
5754
5755 ep.events = events;
5756 ep.data.ptr = task;
5757 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5758}
5759
5760void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005761display_unwatch_fd(struct display *display, int fd)
5762{
5763 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5764}
5765
5766void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005767display_run(struct display *display)
5768{
5769 struct task *task;
5770 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005771 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005772
Pekka Paalanen826d7952011-12-15 10:14:07 +02005773 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005774 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005775 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005776 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005777 struct task, link);
5778 wl_list_remove(&task->link);
5779 task->run(task, 0);
5780 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005781
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005782 wl_display_dispatch_pending(display->display);
5783
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005784 if (!display->running)
5785 break;
5786
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005787 ret = wl_display_flush(display->display);
5788 if (ret < 0 && errno == EAGAIN) {
5789 ep[0].events =
5790 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5791 ep[0].data.ptr = &display->display_task;
5792
5793 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5794 display->display_fd, &ep[0]);
5795 } else if (ret < 0) {
5796 break;
5797 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005798
5799 count = epoll_wait(display->epoll_fd,
5800 ep, ARRAY_LENGTH(ep), -1);
5801 for (i = 0; i < count; i++) {
5802 task = ep[i].data.ptr;
5803 task->run(task, ep[i].events);
5804 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005805 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005806}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005807
5808void
5809display_exit(struct display *display)
5810{
5811 display->running = 0;
5812}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005813
5814void
5815keysym_modifiers_add(struct wl_array *modifiers_map,
5816 const char *name)
5817{
5818 size_t len = strlen(name) + 1;
5819 char *p;
5820
5821 p = wl_array_add(modifiers_map, len);
5822
5823 if (p == NULL)
5824 return;
5825
5826 strncpy(p, name, len);
5827}
5828
5829static xkb_mod_index_t
5830keysym_modifiers_get_index(struct wl_array *modifiers_map,
5831 const char *name)
5832{
5833 xkb_mod_index_t index = 0;
5834 char *p = modifiers_map->data;
5835
5836 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5837 if (strcmp(p, name) == 0)
5838 return index;
5839
5840 index++;
5841 p += strlen(p) + 1;
5842 }
5843
5844 return XKB_MOD_INVALID;
5845}
5846
5847xkb_mod_mask_t
5848keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5849 const char *name)
5850{
5851 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5852
5853 if (index == XKB_MOD_INVALID)
5854 return XKB_MOD_INVALID;
5855
5856 return 1 << index;
5857}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005858
5859void *
5860fail_on_null(void *p)
5861{
5862 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005863 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005864 exit(EXIT_FAILURE);
5865 }
5866
5867 return p;
5868}
5869
5870void *
5871xmalloc(size_t s)
5872{
5873 return fail_on_null(malloc(s));
5874}
5875
Peter Huttererf3d62272013-08-08 11:57:05 +10005876void *
5877xzalloc(size_t s)
5878{
5879 return fail_on_null(zalloc(s));
5880}
5881
Kristian Høgsbergce278412013-07-25 15:20:20 -07005882char *
5883xstrdup(const char *s)
5884{
5885 return fail_on_null(strdup(s));
5886}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005887
5888void *
5889xrealloc(char *p, size_t s)
5890{
5891 return fail_on_null(realloc(p, s));
5892}