blob: 81e007bed8aacf6babc2e4343f07977c68ff3f53 [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 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025
Kristian Høgsberg61017b12008-11-02 18:51:48 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030029#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Derek Foreman493d9792015-03-04 16:26:25 -060041#include <stdbool.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040042
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030043#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050044#include <wayland-egl.h>
45
Rob Clark6396ed32012-03-11 19:48:41 -050046#ifdef USE_CAIRO_GLESV2
47#include <GLES2/gl2.h>
48#include <GLES2/gl2ext.h>
49#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050051#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <EGL/egl.h>
53#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040054
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030056#else /* HAVE_CAIRO_EGL */
57typedef void *EGLDisplay;
58typedef void *EGLConfig;
59typedef void *EGLContext;
60#define EGL_NO_DISPLAY ((EGLDisplay)0)
61#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050062
Daniel Stone9d4f0302012-02-15 16:33:21 +000063#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030064#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040065
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020067#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040068#include "../shared/cairo-util.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050069#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060070#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020071#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030072#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050073
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050074#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050075
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090076#include <sys/types.h>
77#include "ivi-application-client-protocol.h"
78#define IVI_SURFACE_ID 9000
79
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070080struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030081
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040082struct global {
83 uint32_t name;
84 char *interface;
85 uint32_t version;
86 struct wl_list link;
87};
88
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050089struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050090 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040091 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050092 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030093 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040094 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040095 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060096 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020097 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050098 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090099 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400100 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500101 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200102 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200103 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400104 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400105
106 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700107 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400108 struct task display_task;
109
110 int epoll_fd;
111 struct wl_list deferred_list;
112
Pekka Paalanen826d7952011-12-15 10:14:07 +0200113 int running;
114
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400115 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400116 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400117 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500118 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400119
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400120 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400121
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300122 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300123 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400124
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200125 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400126 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800127 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200128
129 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100130
131 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200132
133 uint32_t workspace;
134 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200135
136 /* A hack to get text extents for tooltips */
137 cairo_surface_t *dummy_surface;
138 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200139
140 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100141 int seat_version;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530142 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500143};
144
Rob Bradford7507b572012-05-15 17:55:34 +0100145struct window_output {
146 struct output *output;
147 struct wl_list link;
148};
149
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150struct toysurface {
151 /*
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200155 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200156 * If flags has SURFACE_HINT_RESIZE set, the user is
157 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200158 * Returns the Cairo surface to draw to.
159 */
160 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200161 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200162 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200163
164 /*
165 * Post the surface to the server, returning the server allocation
166 * rectangle. The Cairo surface from prepare() must be destroyed
167 * after calling this.
168 */
169 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200170 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200171 struct rectangle *server_allocation);
172
173 /*
174 * Make the toysurface current with the given EGL context.
175 * Returns 0 on success, and negative of failure.
176 */
177 int (*acquire)(struct toysurface *base, EGLContext ctx);
178
179 /*
180 * Release the toysurface from the EGL context, returning control
181 * to Cairo.
182 */
183 void (*release)(struct toysurface *base);
184
185 /*
186 * Destroy the toysurface, including the Cairo surface, any
187 * backing storage, and the Wayland protocol objects.
188 */
189 void (*destroy)(struct toysurface *base);
190};
191
Pekka Paalanen4e373742013-02-13 16:17:13 +0200192struct surface {
193 struct window *window;
194
195 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300196 struct wl_subsurface *subsurface;
197 int synchronized;
198 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200199 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200200 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300201 int redraw_needed;
202 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300203 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200204
205 struct rectangle allocation;
206 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200207
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200208 struct wl_region *input_region;
209 struct wl_region *opaque_region;
210
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200211 enum window_buffer_type buffer_type;
212 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200213 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200214
215 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300216
217 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200218};
219
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500220struct window {
221 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100222 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500223 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200224 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400225 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500226 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200227 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500228 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400229 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300230 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400231 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400232 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500233 int custom;
234 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400235
Pekka Paalanen99436862012-11-19 17:15:59 +0200236 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400237
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500238 int fullscreen;
239 int maximized;
240
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200241 enum preferred_format preferred_format;
242
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500243 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500244 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400245 window_data_handler_t data_handler;
246 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500247 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400248 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200249 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700250 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400251
Pekka Paalanen4e373742013-02-13 16:17:13 +0200252 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500253 struct xdg_surface *xdg_surface;
254 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300255
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700256 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800257 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500258
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900259 struct ivi_surface *ivi_surface;
260
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500261 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500262
Pekka Paalanen35e82632013-04-25 13:57:48 +0300263 /* struct surface::link, contains also main_surface */
264 struct wl_list subsurface_list;
265
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500266 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400267 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500268};
269
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500270struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500271 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200272 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300273 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500274 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400275 struct wl_list link;
276 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500277 widget_resize_handler_t resize_handler;
278 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500279 widget_enter_handler_t enter_handler;
280 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500281 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500282 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700283 widget_touch_down_handler_t touch_down_handler;
284 widget_touch_up_handler_t touch_up_handler;
285 widget_touch_motion_handler_t touch_motion_handler;
286 widget_touch_frame_handler_t touch_frame_handler;
287 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200288 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400289 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500290 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300291 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500292 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000293 /* If this is set to false then no cairo surface will be
294 * created before redrawing the surface. This is useful if the
295 * redraw handler is going to do completely custom rendering
296 * such as using EGL directly */
297 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400298};
299
Rusty Lynch041815a2013-08-08 21:20:38 -0700300struct touch_point {
301 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800302 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700303 struct widget *widget;
304 struct wl_list link;
305};
306
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400307struct input {
308 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100309 struct wl_seat *seat;
310 struct wl_pointer *pointer;
311 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700312 struct wl_touch *touch;
313 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct window *pointer_focus;
315 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700316 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300317 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300318 uint32_t cursor_anim_start;
319 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300320 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400321 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400322 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400323 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400324 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400325 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400326
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500327 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500328 struct widget *grab;
329 uint32_t grab_button;
330
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400331 struct wl_data_device *data_device;
332 struct data_offer *drag_offer;
333 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800334 uint32_t touch_grab;
335 int32_t touch_grab_id;
336 float drag_x, drag_y;
337 struct window *drag_focus;
338 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100339
340 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100341 struct xkb_keymap *keymap;
342 struct xkb_state *state;
343 xkb_mod_mask_t control_mask;
344 xkb_mod_mask_t alt_mask;
345 xkb_mod_mask_t shift_mask;
346 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400347
Jonny Lamb06959082014-08-12 14:58:27 +0200348 int32_t repeat_rate_sec;
349 int32_t repeat_rate_nsec;
350 int32_t repeat_delay_sec;
351 int32_t repeat_delay_nsec;
352
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400353 struct task repeat_task;
354 int repeat_timer_fd;
355 uint32_t repeat_sym;
356 uint32_t repeat_key;
357 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400358};
359
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500360struct output {
361 struct display *display;
362 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800363 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500364 struct rectangle allocation;
365 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600366 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200367 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500368 char *make;
369 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200370
371 display_output_handler_t destroy_handler;
372 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500373};
374
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500375struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500376 struct widget *widget;
377 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500378 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800379
380 uint32_t last_time;
381 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800382 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500383};
384
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500385struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400386 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500387 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500388 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500389 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700390 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500391 const char **entries;
392 uint32_t time;
393 int current;
394 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400395 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500396 menu_func_t func;
397};
398
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300399struct tooltip {
400 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300401 struct widget *widget;
402 char *entry;
403 struct task tooltip_task;
404 int tooltip_fd;
405 float x, y;
406};
407
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700408struct shm_pool {
409 struct wl_shm_pool *pool;
410 size_t size;
411 size_t used;
412 void *data;
413};
414
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400415enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300416 CURSOR_DEFAULT = 100,
417 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400418};
419
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200420static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421
Pekka Paalanen97777442013-05-22 10:20:05 +0300422/* #define DEBUG */
423
424#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300425
426static void
427debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
428__attribute__ ((format (printf, 4, 5)));
429
430static void
431debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
432{
433 va_list ap;
434 struct timeval tv;
435
436 gettimeofday(&tv, NULL);
437 fprintf(stderr, "%8ld.%03ld ",
438 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
439
440 if (proxy)
441 fprintf(stderr, "%s@%d ",
442 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
443
444 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
445 fprintf(stderr, "%s ", func);
446
447 va_start(ap, fmt);
448 vfprintf(stderr, fmt, ap);
449 va_end(ap);
450}
451
452#define DBG(fmt, ...) \
453 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
454
455#define DBG_OBJ(obj, fmt, ...) \
456 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
457
458#else
459
460#define DBG(...) do {} while (0)
461#define DBG_OBJ(...) do {} while (0)
462
463#endif
464
Alexander Larsson1818e312013-05-22 14:41:31 +0200465static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200466surface_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 +0200467{
468 int32_t tmp;
469
470 switch (buffer_transform) {
471 case WL_OUTPUT_TRANSFORM_90:
472 case WL_OUTPUT_TRANSFORM_270:
473 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
474 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
475 tmp = *width;
476 *width = *height;
477 *height = tmp;
478 break;
479 default:
480 break;
481 }
482
483 *width *= buffer_scale;
484 *height *= buffer_scale;
485}
486
487static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200488buffer_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 +0200489{
490 int32_t tmp;
491
492 switch (buffer_transform) {
493 case WL_OUTPUT_TRANSFORM_90:
494 case WL_OUTPUT_TRANSFORM_270:
495 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
496 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
497 tmp = *width;
498 *width = *height;
499 *height = tmp;
500 break;
501 default:
502 break;
503 }
504
505 *width /= buffer_scale;
506 *height /= buffer_scale;
507}
508
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500509#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400510
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511struct egl_window_surface {
512 struct toysurface base;
513 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100514 struct display *display;
515 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200516 struct wl_egl_window *egl_window;
517 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100518};
519
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200520static struct egl_window_surface *
521to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100522{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100524}
525
526static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200528 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200529 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100530{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200531 struct egl_window_surface *surface = to_egl_window_surface(base);
532
Alexander Larsson1818e312013-05-22 14:41:31 +0200533 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
534
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200535 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
536 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
537
538 return cairo_surface_reference(surface->cairo_surface);
539}
540
541static void
542egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200543 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200544 struct rectangle *server_allocation)
545{
546 struct egl_window_surface *surface = to_egl_window_surface(base);
547
548 cairo_gl_surface_swapbuffers(surface->cairo_surface);
549 wl_egl_window_get_attached_size(surface->egl_window,
550 &server_allocation->width,
551 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200552
553 buffer_to_surface_size (buffer_transform, buffer_scale,
554 &server_allocation->width,
555 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200556}
557
558static int
559egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
560{
561 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200562 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100563
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200564 device = cairo_surface_get_device(surface->cairo_surface);
565 if (!device)
566 return -1;
567
568 if (!ctx) {
569 if (device == surface->display->argb_device)
570 ctx = surface->display->argb_ctx;
571 else
572 assert(0);
573 }
574
575 cairo_device_flush(device);
576 cairo_device_acquire(device);
577 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
578 surface->egl_surface, ctx))
579 fprintf(stderr, "failed to make surface current\n");
580
581 return 0;
582}
583
584static void
585egl_window_surface_release(struct toysurface *base)
586{
587 struct egl_window_surface *surface = to_egl_window_surface(base);
588 cairo_device_t *device;
589
590 device = cairo_surface_get_device(surface->cairo_surface);
591 if (!device)
592 return;
593
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200594 if (!eglMakeCurrent(surface->display->dpy,
595 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200596 fprintf(stderr, "failed to make context current\n");
597
598 cairo_device_release(device);
599}
600
601static void
602egl_window_surface_destroy(struct toysurface *base)
603{
604 struct egl_window_surface *surface = to_egl_window_surface(base);
605 struct display *d = surface->display;
606
607 cairo_surface_destroy(surface->cairo_surface);
608 eglDestroySurface(d->dpy, surface->egl_surface);
609 wl_egl_window_destroy(surface->egl_window);
610 surface->surface = NULL;
611
612 free(surface);
613}
614
615static struct toysurface *
616egl_window_surface_create(struct display *display,
617 struct wl_surface *wl_surface,
618 uint32_t flags,
619 struct rectangle *rectangle)
620{
621 struct egl_window_surface *surface;
622
Pekka Paalanenb3627362012-11-19 17:16:00 +0200623 if (display->dpy == EGL_NO_DISPLAY)
624 return NULL;
625
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200626 surface = calloc(1, sizeof *surface);
627 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100628 return NULL;
629
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200630 surface->base.prepare = egl_window_surface_prepare;
631 surface->base.swap = egl_window_surface_swap;
632 surface->base.acquire = egl_window_surface_acquire;
633 surface->base.release = egl_window_surface_release;
634 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200636 surface->display = display;
637 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400638
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200639 surface->egl_window = wl_egl_window_create(surface->surface,
640 rectangle->width,
641 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100643 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100644 weston_platform_create_egl_surface(display->dpy,
645 display->argb_config,
646 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200648 surface->cairo_surface =
649 cairo_gl_surface_create_for_egl(display->argb_device,
650 surface->egl_surface,
651 rectangle->width,
652 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100653
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200654 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100655}
656
Pekka Paalanenb3627362012-11-19 17:16:00 +0200657#else
658
659static struct toysurface *
660egl_window_surface_create(struct display *display,
661 struct wl_surface *wl_surface,
662 uint32_t flags,
663 struct rectangle *rectangle)
664{
665 return NULL;
666}
667
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400668#endif
669
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200670struct shm_surface_data {
671 struct wl_buffer *buffer;
672 struct shm_pool *pool;
673};
674
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675struct wl_buffer *
676display_get_buffer_for_surface(struct display *display,
677 cairo_surface_t *surface)
678{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200679 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400680
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200681 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682
683 return data->buffer;
684}
685
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500686static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700687shm_pool_destroy(struct shm_pool *pool);
688
689static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690shm_surface_data_destroy(void *p)
691{
692 struct shm_surface_data *data = p;
693
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200694 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700695 if (data->pool)
696 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300697
698 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400699}
700
Kristian Høgsberg16626282012-04-03 11:21:27 -0400701static struct wl_shm_pool *
702make_shm_pool(struct display *display, int size, void **data)
703{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400704 struct wl_shm_pool *pool;
705 int fd;
706
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300707 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400708 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300709 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
710 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400711 return NULL;
712 }
713
714 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400715 if (*data == MAP_FAILED) {
716 fprintf(stderr, "mmap failed: %m\n");
717 close(fd);
718 return NULL;
719 }
720
721 pool = wl_shm_create_pool(display->shm, fd, size);
722
723 close(fd);
724
725 return pool;
726}
727
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700728static struct shm_pool *
729shm_pool_create(struct display *display, size_t size)
730{
731 struct shm_pool *pool = malloc(sizeof *pool);
732
733 if (!pool)
734 return NULL;
735
736 pool->pool = make_shm_pool(display, size, &pool->data);
737 if (!pool->pool) {
738 free(pool);
739 return NULL;
740 }
741
742 pool->size = size;
743 pool->used = 0;
744
745 return pool;
746}
747
748static void *
749shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
750{
751 if (pool->used + size > pool->size)
752 return NULL;
753
754 *offset = pool->used;
755 pool->used += size;
756
757 return (char *) pool->data + *offset;
758}
759
760/* destroy the pool. this does not unmap the memory though */
761static void
762shm_pool_destroy(struct shm_pool *pool)
763{
764 munmap(pool->data, pool->size);
765 wl_shm_pool_destroy(pool->pool);
766 free(pool);
767}
768
769/* Start allocating from the beginning of the pool again */
770static void
771shm_pool_reset(struct shm_pool *pool)
772{
773 pool->used = 0;
774}
775
776static int
777data_length_for_shm_surface(struct rectangle *rect)
778{
779 int stride;
780
781 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
782 rect->width);
783 return stride * rect->height;
784}
785
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500786static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700787display_create_shm_surface_from_pool(struct display *display,
788 struct rectangle *rectangle,
789 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400790{
791 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400792 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400793 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200794 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700795 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400796 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400797
798 data = malloc(sizeof *data);
799 if (data == NULL)
800 return NULL;
801
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200802 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
803 cairo_format = CAIRO_FORMAT_RGB16_565;
804 else
805 cairo_format = CAIRO_FORMAT_ARGB32;
806
807 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700808 length = stride * rectangle->height;
809 data->pool = NULL;
810 map = shm_pool_allocate(pool, length, &offset);
811
812 if (!map) {
813 free(data);
814 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400815 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400816
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400817 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200818 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400819 rectangle->width,
820 rectangle->height,
821 stride);
822
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200823 cairo_surface_set_user_data(surface, &shm_surface_data_key,
824 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400825
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200826 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
827 format = WL_SHM_FORMAT_RGB565;
828 else {
829 if (flags & SURFACE_OPAQUE)
830 format = WL_SHM_FORMAT_XRGB8888;
831 else
832 format = WL_SHM_FORMAT_ARGB8888;
833 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400834
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200835 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
836 rectangle->width,
837 rectangle->height,
838 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400839
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700840 return surface;
841}
842
843static cairo_surface_t *
844display_create_shm_surface(struct display *display,
845 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200846 struct shm_pool *alternate_pool,
847 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700848{
849 struct shm_surface_data *data;
850 struct shm_pool *pool;
851 cairo_surface_t *surface;
852
Pekka Paalanen99436862012-11-19 17:15:59 +0200853 if (alternate_pool) {
854 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700855 surface = display_create_shm_surface_from_pool(display,
856 rectangle,
857 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200858 alternate_pool);
859 if (surface) {
860 data = cairo_surface_get_user_data(surface,
861 &shm_surface_data_key);
862 goto out;
863 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700864 }
865
866 pool = shm_pool_create(display,
867 data_length_for_shm_surface(rectangle));
868 if (!pool)
869 return NULL;
870
871 surface =
872 display_create_shm_surface_from_pool(display, rectangle,
873 flags, pool);
874
875 if (!surface) {
876 shm_pool_destroy(pool);
877 return NULL;
878 }
879
880 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200881 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700882 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400883
Pekka Paalanen99436862012-11-19 17:15:59 +0200884out:
885 if (data_ret)
886 *data_ret = data;
887
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400888 return surface;
889}
890
nobled7b87cb02011-02-01 18:51:47 +0000891static int
892check_size(struct rectangle *rect)
893{
894 if (rect->width && rect->height)
895 return 0;
896
897 fprintf(stderr, "tried to create surface of "
898 "width: %d, height: %d\n", rect->width, rect->height);
899 return -1;
900}
901
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400902cairo_surface_t *
903display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400905 struct rectangle *rectangle,
906 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400907{
nobled7b87cb02011-02-01 18:51:47 +0000908 if (check_size(rectangle) < 0)
909 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200910
911 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200912 return display_create_shm_surface(display, rectangle, flags,
913 NULL, NULL);
914}
915
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916struct shm_surface_leaf {
917 cairo_surface_t *cairo_surface;
918 /* 'data' is automatically destroyed, when 'cairo_surface' is */
919 struct shm_surface_data *data;
920
921 struct shm_pool *resize_pool;
922 int busy;
923};
924
925static void
926shm_surface_leaf_release(struct shm_surface_leaf *leaf)
927{
928 if (leaf->cairo_surface)
929 cairo_surface_destroy(leaf->cairo_surface);
930 /* leaf->data already destroyed via cairo private */
931
932 if (leaf->resize_pool)
933 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200934
935 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200936}
937
Pekka Paalanenaef02542013-04-25 13:57:47 +0300938#define MAX_LEAVES 3
939
Pekka Paalanen99436862012-11-19 17:15:59 +0200940struct shm_surface {
941 struct toysurface base;
942 struct display *display;
943 struct wl_surface *surface;
944 uint32_t flags;
945 int dx, dy;
946
Pekka Paalanenaef02542013-04-25 13:57:47 +0300947 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200948 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200949};
950
951static struct shm_surface *
952to_shm_surface(struct toysurface *base)
953{
954 return container_of(base, struct shm_surface, base);
955}
956
Pekka Paalanena4eda732012-11-19 17:16:02 +0200957static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300958shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
959{
960#ifdef DEBUG
961 struct shm_surface_leaf *leaf;
962 char bufs[MAX_LEAVES + 1];
963 int i;
964
965 for (i = 0; i < MAX_LEAVES; i++) {
966 leaf = &surface->leaf[i];
967
968 if (leaf->busy)
969 bufs[i] = 'b';
970 else if (leaf->cairo_surface)
971 bufs[i] = 'a';
972 else
973 bufs[i] = ' ';
974 }
975
976 bufs[MAX_LEAVES] = '\0';
977 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
978#endif
979}
980
981static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200982shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
983{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200984 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300985 struct shm_surface_leaf *leaf;
986 int i;
987 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300988
989 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200990
Pekka Paalanenaef02542013-04-25 13:57:47 +0300991 for (i = 0; i < MAX_LEAVES; i++) {
992 leaf = &surface->leaf[i];
993 if (leaf->data && leaf->data->buffer == buffer) {
994 leaf->busy = 0;
995 break;
996 }
997 }
998 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200999
Pekka Paalanenaef02542013-04-25 13:57:47 +03001000 /* Leave one free leaf with storage, release others */
1001 free_found = 0;
1002 for (i = 0; i < MAX_LEAVES; i++) {
1003 leaf = &surface->leaf[i];
1004
1005 if (!leaf->cairo_surface || leaf->busy)
1006 continue;
1007
1008 if (!free_found)
1009 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001010 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001011 shm_surface_leaf_release(leaf);
1012 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001013
Pekka Paalanen97777442013-05-22 10:20:05 +03001014 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001015}
1016
1017static const struct wl_buffer_listener shm_surface_buffer_listener = {
1018 shm_surface_buffer_release
1019};
1020
Pekka Paalanen99436862012-11-19 17:15:59 +02001021static cairo_surface_t *
1022shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001023 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001024 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001025{
Pekka Paalanenec076692012-11-30 13:37:27 +02001026 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001027 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001028 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001029 struct shm_surface_leaf *leaf = NULL;
1030 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001031
1032 surface->dx = dx;
1033 surface->dy = dy;
1034
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001035 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 for (i = 0; i < MAX_LEAVES; i++) {
1037 if (surface->leaf[i].busy)
1038 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001039
Pekka Paalanenaef02542013-04-25 13:57:47 +03001040 if (!leaf || surface->leaf[i].cairo_surface)
1041 leaf = &surface->leaf[i];
1042 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001043 DBG_OBJ(surface->surface, "pick leaf %d\n",
1044 (int)(leaf - &surface->leaf[0]));
1045
Pekka Paalanenaef02542013-04-25 13:57:47 +03001046 if (!leaf) {
1047 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001048 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001049 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001050 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001051 }
1052
Pekka Paalanena4eda732012-11-19 17:16:02 +02001053 if (!resize_hint && leaf->resize_pool) {
1054 cairo_surface_destroy(leaf->cairo_surface);
1055 leaf->cairo_surface = NULL;
1056 shm_pool_destroy(leaf->resize_pool);
1057 leaf->resize_pool = NULL;
1058 }
1059
Alexander Larsson1818e312013-05-22 14:41:31 +02001060 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1061
Pekka Paalanena4eda732012-11-19 17:16:02 +02001062 if (leaf->cairo_surface &&
1063 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1064 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001065 goto out;
1066
Pekka Paalanena4eda732012-11-19 17:16:02 +02001067 if (leaf->cairo_surface)
1068 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001069
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001070#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001071 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001072 /* Create a big pool to allocate from, while continuously
1073 * resizing. Mmapping a new pool in the server
1074 * is relatively expensive, so reusing a pool performs
1075 * better, but may temporarily reserve unneeded memory.
1076 */
1077 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001078 leaf->resize_pool = shm_pool_create(surface->display,
1079 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001080 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001081#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001082
Alexander Larsson1818e312013-05-22 14:41:31 +02001083 rect.width = width;
1084 rect.height = height;
1085
Pekka Paalanena4eda732012-11-19 17:16:02 +02001086 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001087 display_create_shm_surface(surface->display, &rect,
1088 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001089 leaf->resize_pool,
1090 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001091 if (!leaf->cairo_surface)
1092 return NULL;
1093
Pekka Paalanena4eda732012-11-19 17:16:02 +02001094 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001095 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001096
1097out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001098 surface->current = leaf;
1099
1100 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001101}
1102
1103static void
1104shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001105 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001106 struct rectangle *server_allocation)
1107{
1108 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001109 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001110
1111 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001112 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001113 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001114 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001115
Alexander Larsson1818e312013-05-22 14:41:31 +02001116 buffer_to_surface_size (buffer_transform, buffer_scale,
1117 &server_allocation->width,
1118 &server_allocation->height);
1119
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001121 surface->dx, surface->dy);
1122 wl_surface_damage(surface->surface, 0, 0,
1123 server_allocation->width, server_allocation->height);
1124 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001125
Pekka Paalanen71233882013-04-25 13:57:53 +03001126 DBG_OBJ(surface->surface, "leaf %d busy\n",
1127 (int)(leaf - &surface->leaf[0]));
1128
Pekka Paalanena4eda732012-11-19 17:16:02 +02001129 leaf->busy = 1;
1130 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001131}
1132
1133static int
1134shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1135{
1136 return -1;
1137}
1138
1139static void
1140shm_surface_release(struct toysurface *base)
1141{
1142}
1143
1144static void
1145shm_surface_destroy(struct toysurface *base)
1146{
1147 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001148 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001149
Pekka Paalanenaef02542013-04-25 13:57:47 +03001150 for (i = 0; i < MAX_LEAVES; i++)
1151 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001152
1153 free(surface);
1154}
1155
1156static struct toysurface *
1157shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1158 uint32_t flags, struct rectangle *rectangle)
1159{
1160 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001161 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001162
Brian Lovinbc919262013-08-07 15:34:59 -07001163 surface = xmalloc(sizeof *surface);
1164 memset(surface, 0, sizeof *surface);
1165
Pekka Paalanen99436862012-11-19 17:15:59 +02001166 if (!surface)
1167 return NULL;
1168
1169 surface->base.prepare = shm_surface_prepare;
1170 surface->base.swap = shm_surface_swap;
1171 surface->base.acquire = shm_surface_acquire;
1172 surface->base.release = shm_surface_release;
1173 surface->base.destroy = shm_surface_destroy;
1174
1175 surface->display = display;
1176 surface->surface = wl_surface;
1177 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001178
1179 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001180}
1181
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001182/*
1183 * The following correspondences between file names and cursors was copied
1184 * from: https://bugs.kde.org/attachment.cgi?id=67313
1185 */
1186
1187static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001188 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001189 "sw-resize",
1190 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001191};
1192
1193static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001194 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001195 "se-resize",
1196 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001197};
1198
1199static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001200 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001201 "s-resize",
1202 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001203};
1204
1205static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001206 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001207 "closedhand",
1208 "208530c400c041818281048008011002"
1209};
1210
1211static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001212 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001213 "default",
1214 "top_left_arrow",
1215 "left-arrow"
1216};
1217
1218static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001219 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001220 "w-resize",
1221 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001222};
1223
1224static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001225 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001226 "e-resize",
1227 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001228};
1229
1230static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001231 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001232 "nw-resize",
1233 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001234};
1235
1236static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001237 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001238 "ne-resize",
1239 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001240};
1241
1242static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001243 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001244 "n-resize",
1245 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001246};
1247
1248static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001249 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001250 "ibeam",
1251 "text"
1252};
1253
1254static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001255 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001256 "pointer",
1257 "pointing_hand",
1258 "e29285e634086352946a0e7090d73106"
1259};
1260
1261static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001262 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001263 "wait",
1264 "0426c94ea35c87780ff01dc239897213"
1265};
1266
1267struct cursor_alternatives {
1268 const char **names;
1269 size_t count;
1270};
1271
1272static const struct cursor_alternatives cursors[] = {
1273 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1274 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1275 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1276 {grabbings, ARRAY_LENGTH(grabbings)},
1277 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1278 {left_sides, ARRAY_LENGTH(left_sides)},
1279 {right_sides, ARRAY_LENGTH(right_sides)},
1280 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1281 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1282 {top_sides, ARRAY_LENGTH(top_sides)},
1283 {xterms, ARRAY_LENGTH(xterms)},
1284 {hand1s, ARRAY_LENGTH(hand1s)},
1285 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001286};
1287
1288static void
1289create_cursors(struct display *display)
1290{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001291 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001292 struct weston_config *config;
1293 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001294 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001295 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001296 unsigned int i, j;
1297 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001298
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001299 config_file = weston_config_get_name_from_env();
1300 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001301 s = weston_config_get_section(config, "shell", NULL, NULL);
1302 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1303 weston_config_section_get_int(s, "cursor-size", &size, 32);
1304 weston_config_destroy(config);
1305
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001306 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001307 if (!display->cursor_theme) {
1308 fprintf(stderr, "could not load theme '%s'\n", theme);
1309 return;
1310 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001311 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001312 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001313 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001314
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001315 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001316 cursor = NULL;
1317 for (j = 0; !cursor && j < cursors[i].count; ++j)
1318 cursor = wl_cursor_theme_get_cursor(
1319 display->cursor_theme, cursors[i].names[j]);
1320
1321 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001322 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001323 cursors[i].names[0]);
1324
1325 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001326 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001327}
1328
1329static void
1330destroy_cursors(struct display *display)
1331{
1332 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001333 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001334}
1335
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001336struct wl_cursor_image *
1337display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001338{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001339 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001340
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001341 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001342}
1343
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001344static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001345surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001346{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001347 if (!surface->cairo_surface)
1348 return;
1349
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001350 if (surface->opaque_region) {
1351 wl_surface_set_opaque_region(surface->surface,
1352 surface->opaque_region);
1353 wl_region_destroy(surface->opaque_region);
1354 surface->opaque_region = NULL;
1355 }
1356
1357 if (surface->input_region) {
1358 wl_surface_set_input_region(surface->surface,
1359 surface->input_region);
1360 wl_region_destroy(surface->input_region);
1361 surface->input_region = NULL;
1362 }
1363
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001364 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001365 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001366 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001367
Pekka Paalanen89dee002013-02-13 16:17:20 +02001368 cairo_surface_destroy(surface->cairo_surface);
1369 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001370}
1371
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001372int
1373window_has_focus(struct window *window)
1374{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001375 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001376}
1377
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001378static void
1379window_close(struct window *window)
1380{
1381 if (window->close_handler)
1382 window->close_handler(window->user_data);
1383 else
1384 display_exit(window->display);
1385}
1386
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001387struct display *
1388window_get_display(struct window *window)
1389{
1390 return window->display;
1391}
1392
Pekka Paalanen89dee002013-02-13 16:17:20 +02001393static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001394handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1395 int32_t width, int32_t height)
1396{
1397 struct window *window = data;
1398
1399 window_schedule_resize(window, width, height);
1400}
1401
1402static const struct ivi_surface_listener ivi_surface_listener = {
1403 handle_ivi_surface_configure,
1404};
1405
1406static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001407surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001408{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001410 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001411
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001412 if (!surface->toysurface && display->dpy &&
1413 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001414 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001415 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001416 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001417 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001418 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001419 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001420
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001421 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001422 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001423 surface->surface,
1424 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001425
Pekka Paalanen89dee002013-02-13 16:17:20 +02001426 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001427 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001428 allocation.width, allocation.height, flags,
1429 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001430}
1431
1432static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001433window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001434{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001435 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001436 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001437
Pekka Paalanenec076692012-11-30 13:37:27 +02001438 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001439 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001440
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001441 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1442 flags |= SURFACE_HINT_RGB565;
1443
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001444 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001445}
1446
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001447int
1448window_get_buffer_transform(struct window *window)
1449{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001450 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001451}
1452
1453void
1454window_set_buffer_transform(struct window *window,
1455 enum wl_output_transform transform)
1456{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001457 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001458 wl_surface_set_buffer_transform(window->main_surface->surface,
1459 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001460}
1461
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001462void
1463window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001464 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001465{
1466 window->main_surface->buffer_scale = scale;
1467 wl_surface_set_buffer_scale(window->main_surface->surface,
1468 scale);
1469}
1470
1471uint32_t
1472window_get_buffer_scale(struct window *window)
1473{
1474 return window->main_surface->buffer_scale;
1475}
1476
Alexander Larssond68f5232013-05-22 14:41:33 +02001477uint32_t
1478window_get_output_scale(struct window *window)
1479{
1480 struct window_output *window_output;
1481 struct window_output *window_output_tmp;
1482 int scale = 1;
1483
1484 wl_list_for_each_safe(window_output, window_output_tmp,
1485 &window->window_output_list, link) {
1486 if (window_output->output->scale > scale)
1487 scale = window_output->output->scale;
1488 }
1489
1490 return scale;
1491}
1492
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001493static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001494
Pekka Paalanen4e373742013-02-13 16:17:13 +02001495static void
1496surface_destroy(struct surface *surface)
1497{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001498 if (surface->frame_cb)
1499 wl_callback_destroy(surface->frame_cb);
1500
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001501 if (surface->input_region)
1502 wl_region_destroy(surface->input_region);
1503
1504 if (surface->opaque_region)
1505 wl_region_destroy(surface->opaque_region);
1506
Pekka Paalanen35e82632013-04-25 13:57:48 +03001507 if (surface->subsurface)
1508 wl_subsurface_destroy(surface->subsurface);
1509
Pekka Paalanen4e373742013-02-13 16:17:13 +02001510 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001511
1512 if (surface->toysurface)
1513 surface->toysurface->destroy(surface->toysurface);
1514
Pekka Paalanen35e82632013-04-25 13:57:48 +03001515 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001516 free(surface);
1517}
1518
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001519void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001520window_destroy(struct window *window)
1521{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001522 struct display *display = window->display;
1523 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001524 struct window_output *window_output;
1525 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001526
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001527 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001528
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001529 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001530 if (input->touch_focus == window)
1531 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001532 if (input->pointer_focus == window)
1533 input->pointer_focus = NULL;
1534 if (input->keyboard_focus == window)
1535 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001536 if (input->focus_widget &&
1537 input->focus_widget->window == window)
1538 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001539 }
1540
Rob Bradford7507b572012-05-15 17:55:34 +01001541 wl_list_for_each_safe(window_output, window_output_tmp,
1542 &window->window_output_list, link) {
1543 free (window_output);
1544 }
1545
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001546 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001547 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001548
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001549 if (window->xdg_surface)
1550 xdg_surface_destroy(window->xdg_surface);
1551 if (window->xdg_popup)
1552 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001553
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001554 if (window->ivi_surface)
1555 ivi_surface_destroy(window->ivi_surface);
1556
Pekka Paalanen4e373742013-02-13 16:17:13 +02001557 surface_destroy(window->main_surface);
1558
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001559 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001560
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001561 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001562 free(window);
1563}
1564
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001565static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001566widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001567{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001568 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001569
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001570 wl_list_for_each(child, &widget->child_list, link) {
1571 target = widget_find_widget(child, x, y);
1572 if (target)
1573 return target;
1574 }
1575
1576 if (widget->allocation.x <= x &&
1577 x < widget->allocation.x + widget->allocation.width &&
1578 widget->allocation.y <= y &&
1579 y < widget->allocation.y + widget->allocation.height) {
1580 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001581 }
1582
1583 return NULL;
1584}
1585
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001586static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001587window_find_widget(struct window *window, int32_t x, int32_t y)
1588{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001589 struct surface *surface;
1590 struct widget *widget;
1591
1592 wl_list_for_each(surface, &window->subsurface_list, link) {
1593 widget = widget_find_widget(surface->widget, x, y);
1594 if (widget)
1595 return widget;
1596 }
1597
1598 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001599}
1600
1601static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001602widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001603{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001604 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001605
Peter Huttererf3d62272013-08-08 11:57:05 +10001606 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001607 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001608 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001609 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001610 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001611 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001612 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001613 widget->tooltip = NULL;
1614 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001615 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001616 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001617
1618 return widget;
1619}
1620
1621struct widget *
1622window_add_widget(struct window *window, void *data)
1623{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001624 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001625
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001626 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001627 wl_list_init(&widget->link);
1628 window->main_surface->widget = widget;
1629
1630 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001631}
1632
1633struct widget *
1634widget_add_widget(struct widget *parent, void *data)
1635{
1636 struct widget *widget;
1637
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001638 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001639 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001640
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001641 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001642}
1643
1644void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001645widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001646{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001647 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001648 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001649 struct input *input;
1650
Pekka Paalanen35e82632013-04-25 13:57:48 +03001651 /* Destroy the sub-surface along with the root widget */
1652 if (surface->widget == widget && surface->subsurface)
1653 surface_destroy(widget->surface);
1654
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001655 if (widget->tooltip)
1656 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001657
Pekka Paalanene156fb62012-01-19 13:51:38 +02001658 wl_list_for_each(input, &display->input_list, link) {
1659 if (input->focus_widget == widget)
1660 input->focus_widget = NULL;
1661 }
1662
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001663 wl_list_remove(&widget->link);
1664 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001665}
1666
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001667void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001668widget_set_default_cursor(struct widget *widget, int cursor)
1669{
1670 widget->default_cursor = cursor;
1671}
1672
1673void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001674widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001675{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001676 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001677}
1678
1679void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001680widget_set_size(struct widget *widget, int32_t width, int32_t height)
1681{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001682 widget->allocation.width = width;
1683 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001684}
1685
1686void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001687widget_set_allocation(struct widget *widget,
1688 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001689{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001690 widget->allocation.x = x;
1691 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001692 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001693}
1694
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001695void
1696widget_set_transparent(struct widget *widget, int transparent)
1697{
1698 widget->opaque = !transparent;
1699}
1700
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001701void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001702widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001704 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001705}
1706
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001707static cairo_surface_t *
1708widget_get_cairo_surface(struct widget *widget)
1709{
1710 struct surface *surface = widget->surface;
1711 struct window *window = widget->window;
1712
Neil Roberts97b747c2013-12-19 16:17:12 +00001713 assert(widget->use_cairo);
1714
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001715 if (!surface->cairo_surface) {
1716 if (surface == window->main_surface)
1717 window_create_main_surface(window);
1718 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001719 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001720 }
1721
1722 return surface->cairo_surface;
1723}
1724
Alexander Larsson15901f02013-05-22 14:41:25 +02001725static void
1726widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1727{
1728 struct surface *surface = widget->surface;
1729 double angle;
1730 cairo_matrix_t m;
1731 enum wl_output_transform transform;
1732 int surface_width, surface_height;
1733 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001734 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001735
1736 surface_width = surface->allocation.width;
1737 surface_height = surface->allocation.height;
1738
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001739 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001740 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001741
Alexander Larsson15901f02013-05-22 14:41:25 +02001742 switch (transform) {
1743 case WL_OUTPUT_TRANSFORM_FLIPPED:
1744 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1745 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1747 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1748 break;
1749 default:
1750 cairo_matrix_init_identity(&m);
1751 break;
1752 }
1753
1754 switch (transform) {
1755 case WL_OUTPUT_TRANSFORM_NORMAL:
1756 default:
1757 angle = 0;
1758 translate_x = 0;
1759 translate_y = 0;
1760 break;
1761 case WL_OUTPUT_TRANSFORM_FLIPPED:
1762 angle = 0;
1763 translate_x = surface_width;
1764 translate_y = 0;
1765 break;
1766 case WL_OUTPUT_TRANSFORM_90:
1767 angle = M_PI_2;
1768 translate_x = surface_height;
1769 translate_y = 0;
1770 break;
1771 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1772 angle = M_PI_2;
1773 translate_x = surface_height;
1774 translate_y = surface_width;
1775 break;
1776 case WL_OUTPUT_TRANSFORM_180:
1777 angle = M_PI;
1778 translate_x = surface_width;
1779 translate_y = surface_height;
1780 break;
1781 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1782 angle = M_PI;
1783 translate_x = 0;
1784 translate_y = surface_height;
1785 break;
1786 case WL_OUTPUT_TRANSFORM_270:
1787 angle = M_PI + M_PI_2;
1788 translate_x = 0;
1789 translate_y = surface_width;
1790 break;
1791 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1792 angle = M_PI + M_PI_2;
1793 translate_x = 0;
1794 translate_y = 0;
1795 break;
1796 }
1797
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001798 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001799 cairo_translate(cr, translate_x, translate_y);
1800 cairo_rotate(cr, angle);
1801 cairo_transform(cr, &m);
1802}
1803
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001804cairo_t *
1805widget_cairo_create(struct widget *widget)
1806{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001807 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001808 cairo_surface_t *cairo_surface;
1809 cairo_t *cr;
1810
1811 cairo_surface = widget_get_cairo_surface(widget);
1812 cr = cairo_create(cairo_surface);
1813
Alexander Larsson15901f02013-05-22 14:41:25 +02001814 widget_cairo_update_transform(widget, cr);
1815
Pekka Paalanen35e82632013-04-25 13:57:48 +03001816 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1817
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001818 return cr;
1819}
1820
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001821struct wl_surface *
1822widget_get_wl_surface(struct widget *widget)
1823{
1824 return widget->surface->surface;
1825}
1826
Neil Roberts5e10a042013-09-09 00:40:17 +01001827struct wl_subsurface *
1828widget_get_wl_subsurface(struct widget *widget)
1829{
1830 return widget->surface->subsurface;
1831}
1832
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001833uint32_t
1834widget_get_last_time(struct widget *widget)
1835{
1836 return widget->surface->last_time;
1837}
1838
1839void
1840widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1841{
1842 struct wl_compositor *comp = widget->window->display->compositor;
1843 struct surface *surface = widget->surface;
1844
1845 if (!surface->input_region)
1846 surface->input_region = wl_compositor_create_region(comp);
1847
1848 if (rect) {
1849 wl_region_add(surface->input_region,
1850 rect->x, rect->y, rect->width, rect->height);
1851 }
1852}
1853
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001854void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001855widget_set_resize_handler(struct widget *widget,
1856 widget_resize_handler_t handler)
1857{
1858 widget->resize_handler = handler;
1859}
1860
1861void
1862widget_set_redraw_handler(struct widget *widget,
1863 widget_redraw_handler_t handler)
1864{
1865 widget->redraw_handler = handler;
1866}
1867
1868void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001869widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001870{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001871 widget->enter_handler = handler;
1872}
1873
1874void
1875widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1876{
1877 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001878}
1879
1880void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001881widget_set_motion_handler(struct widget *widget,
1882 widget_motion_handler_t handler)
1883{
1884 widget->motion_handler = handler;
1885}
1886
1887void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001888widget_set_button_handler(struct widget *widget,
1889 widget_button_handler_t handler)
1890{
1891 widget->button_handler = handler;
1892}
1893
1894void
Rusty Lynch041815a2013-08-08 21:20:38 -07001895widget_set_touch_up_handler(struct widget *widget,
1896 widget_touch_up_handler_t handler)
1897{
1898 widget->touch_up_handler = handler;
1899}
1900
1901void
1902widget_set_touch_down_handler(struct widget *widget,
1903 widget_touch_down_handler_t handler)
1904{
1905 widget->touch_down_handler = handler;
1906}
1907
1908void
1909widget_set_touch_motion_handler(struct widget *widget,
1910 widget_touch_motion_handler_t handler)
1911{
1912 widget->touch_motion_handler = handler;
1913}
1914
1915void
1916widget_set_touch_frame_handler(struct widget *widget,
1917 widget_touch_frame_handler_t handler)
1918{
1919 widget->touch_frame_handler = handler;
1920}
1921
1922void
1923widget_set_touch_cancel_handler(struct widget *widget,
1924 widget_touch_cancel_handler_t handler)
1925{
1926 widget->touch_cancel_handler = handler;
1927}
1928
1929void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001930widget_set_axis_handler(struct widget *widget,
1931 widget_axis_handler_t handler)
1932{
1933 widget->axis_handler = handler;
1934}
1935
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001936static void
1937window_schedule_redraw_task(struct window *window);
1938
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001939void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001940widget_schedule_redraw(struct widget *widget)
1941{
Pekka Paalanen71233882013-04-25 13:57:53 +03001942 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001943 widget->surface->redraw_needed = 1;
1944 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001945}
1946
Neil Roberts97b747c2013-12-19 16:17:12 +00001947void
1948widget_set_use_cairo(struct widget *widget,
1949 int use_cairo)
1950{
1951 widget->use_cairo = use_cairo;
1952}
1953
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001954cairo_surface_t *
1955window_get_surface(struct window *window)
1956{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001957 cairo_surface_t *cairo_surface;
1958
1959 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1960
1961 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001962}
1963
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001964struct wl_surface *
1965window_get_wl_surface(struct window *window)
1966{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001967 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001968}
1969
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001970static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001971tooltip_redraw_handler(struct widget *widget, void *data)
1972{
1973 cairo_t *cr;
1974 const int32_t r = 3;
1975 struct tooltip *tooltip = data;
1976 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001977
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001978 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001979 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001980 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1981 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1982 cairo_paint(cr);
1983
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001984 width = widget->allocation.width;
1985 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001986 rounded_rect(cr, 0, 0, width, height, r);
1987
1988 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1989 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1990 cairo_fill(cr);
1991
1992 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1993 cairo_move_to(cr, 10, 16);
1994 cairo_show_text(cr, tooltip->entry);
1995 cairo_destroy(cr);
1996}
1997
1998static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001999get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002000{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002001 cairo_t *cr;
2002 cairo_text_extents_t extents;
2003
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002004 /* Use the dummy_surface because tooltip's surface was not
2005 * created yet, and parent does not have a valid surface
2006 * outside repaint, either.
2007 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002008 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002009 cairo_text_extents(cr, tooltip->entry, &extents);
2010 cairo_destroy(cr);
2011
2012 return extents;
2013}
2014
2015static int
2016window_create_tooltip(struct tooltip *tooltip)
2017{
2018 struct widget *parent = tooltip->parent;
2019 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002020 const int offset_y = 27;
2021 const int margin = 3;
2022 cairo_text_extents_t extents;
2023
2024 if (tooltip->widget)
2025 return 0;
2026
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002027 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002028
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002029 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002030 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002031 widget_set_allocation(tooltip->widget,
2032 tooltip->x, tooltip->y + offset_y,
2033 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002034
2035 return 0;
2036}
2037
2038void
2039widget_destroy_tooltip(struct widget *parent)
2040{
2041 struct tooltip *tooltip = parent->tooltip;
2042
2043 parent->tooltip_count = 0;
2044 if (!tooltip)
2045 return;
2046
2047 if (tooltip->widget) {
2048 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002049 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002050 }
2051
2052 close(tooltip->tooltip_fd);
2053 free(tooltip->entry);
2054 free(tooltip);
2055 parent->tooltip = NULL;
2056}
2057
2058static void
2059tooltip_func(struct task *task, uint32_t events)
2060{
2061 struct tooltip *tooltip =
2062 container_of(task, struct tooltip, tooltip_task);
2063 uint64_t exp;
2064
Martin Olsson8df662a2012-07-08 03:03:47 +02002065 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2066 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002067 window_create_tooltip(tooltip);
2068}
2069
2070#define TOOLTIP_TIMEOUT 500
2071static int
2072tooltip_timer_reset(struct tooltip *tooltip)
2073{
2074 struct itimerspec its;
2075
2076 its.it_interval.tv_sec = 0;
2077 its.it_interval.tv_nsec = 0;
2078 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2079 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2080 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2081 fprintf(stderr, "could not set timerfd\n: %m");
2082 return -1;
2083 }
2084
2085 return 0;
2086}
2087
2088int
2089widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2090{
2091 struct tooltip *tooltip = parent->tooltip;
2092
2093 parent->tooltip_count++;
2094 if (tooltip) {
2095 tooltip->x = x;
2096 tooltip->y = y;
2097 tooltip_timer_reset(tooltip);
2098 return 0;
2099 }
2100
2101 /* the handler might be triggered too fast via input device motion, so
2102 * we need this check here to make sure tooltip is fully initialized */
2103 if (parent->tooltip_count > 1)
2104 return 0;
2105
2106 tooltip = malloc(sizeof *tooltip);
2107 if (!tooltip)
2108 return -1;
2109
2110 parent->tooltip = tooltip;
2111 tooltip->parent = parent;
2112 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002113 tooltip->x = x;
2114 tooltip->y = y;
2115 tooltip->entry = strdup(entry);
2116 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2117 if (tooltip->tooltip_fd < 0) {
2118 fprintf(stderr, "could not create timerfd\n: %m");
2119 return -1;
2120 }
2121
2122 tooltip->tooltip_task.run = tooltip_func;
2123 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2124 EPOLLIN, &tooltip->tooltip_task);
2125 tooltip_timer_reset(tooltip);
2126
2127 return 0;
2128}
2129
2130static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002131workspace_manager_state(void *data,
2132 struct workspace_manager *workspace_manager,
2133 uint32_t current,
2134 uint32_t count)
2135{
2136 struct display *display = data;
2137
2138 display->workspace = current;
2139 display->workspace_count = count;
2140}
2141
2142static const struct workspace_manager_listener workspace_manager_listener = {
2143 workspace_manager_state
2144};
2145
2146static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002147frame_resize_handler(struct widget *widget,
2148 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002149{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002150 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002151 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002152 struct rectangle interior;
2153 struct rectangle input;
2154 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002155
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002156 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002157 interior.x = 0;
2158 interior.y = 0;
2159 interior.width = width;
2160 interior.height = height;
2161 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002162 frame_resize(frame->frame, width, height);
2163 frame_interior(frame->frame, &interior.x, &interior.y,
2164 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002165 }
2166
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002167 widget_set_allocation(child, interior.x, interior.y,
2168 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002169
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002170 if (child->resize_handler) {
2171 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002172 child->user_data);
2173
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002174 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002175 width = child->allocation.width;
2176 height = child->allocation.height;
2177 } else {
2178 frame_resize_inside(frame->frame,
2179 child->allocation.width,
2180 child->allocation.height);
2181 width = frame_width(frame->frame);
2182 height = frame_height(frame->frame);
2183 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002184 }
2185
Scott Moreauf7e498c2012-05-14 11:39:29 -06002186 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002187
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002188 widget->surface->input_region =
2189 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002190 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002191 frame_input_rect(frame->frame, &input.x, &input.y,
2192 &input.width, &input.height);
2193 wl_region_add(widget->surface->input_region,
2194 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002195 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002196 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002197 }
2198
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002199 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002200
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002201 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002202 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002203 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2204 &opaque.width, &opaque.height);
2205
2206 wl_region_add(widget->surface->opaque_region,
2207 opaque.x, opaque.y,
2208 opaque.width, opaque.height);
2209 } else {
2210 wl_region_add(widget->surface->opaque_region,
2211 0, 0, width, height);
2212 }
Martin Minarik1998b152012-05-10 02:04:35 +02002213 }
2214
Martin Minarik1998b152012-05-10 02:04:35 +02002215
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002216 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002217}
2218
2219static void
2220frame_redraw_handler(struct widget *widget, void *data)
2221{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002223 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 struct window *window = widget->window;
2225
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002226 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002227 return;
2228
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002229 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002230
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002231 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002232
2233 cairo_destroy(cr);
2234}
2235
2236static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002237frame_get_pointer_image_for_location(struct window_frame *frame,
2238 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002239{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002240 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002241
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002242 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002243 return CURSOR_LEFT_PTR;
2244
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002245 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002246 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002247 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002248 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002249 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002250 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002251 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002252 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002253 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002254 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002255 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002257 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002258 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002259 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002260 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002261 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002262 case THEME_LOCATION_EXTERIOR:
2263 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002264 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002265 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002266 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002267}
2268
Pekka Paalanen26237862014-09-10 15:10:30 +03002269static void
2270frame_menu_func(void *data, struct input *input, int index)
2271{
2272 struct window *window = data;
2273 struct display *display;
2274
2275 switch (index) {
2276 case 0: /* close */
2277 window_close(window);
2278 break;
2279 case 1: /* move to workspace above */
2280 display = window->display;
2281 if (display->workspace > 0)
2282 workspace_manager_move_surface(
2283 display->workspace_manager,
2284 window->main_surface->surface,
2285 display->workspace - 1);
2286 break;
2287 case 2: /* move to workspace below */
2288 display = window->display;
2289 if (display->workspace < display->workspace_count - 1)
2290 workspace_manager_move_surface(
2291 display->workspace_manager,
2292 window->main_surface->surface,
2293 display->workspace + 1);
2294 break;
2295 case 3: /* fullscreen */
2296 /* we don't have a way to get out of fullscreen for now */
2297 if (window->fullscreen_handler)
2298 window->fullscreen_handler(window, window->user_data);
2299 break;
2300 }
2301}
2302
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002303void
2304window_show_frame_menu(struct window *window,
2305 struct input *input, uint32_t time)
2306{
2307 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002308 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002309
Pekka Paalanen26237862014-09-10 15:10:30 +03002310 static const char *entries[] = {
2311 "Close",
2312 "Move to workspace above", "Move to workspace below",
2313 "Fullscreen"
2314 };
2315
2316 if (window->fullscreen_handler)
2317 count = ARRAY_LENGTH(entries);
2318 else
2319 count = ARRAY_LENGTH(entries) - 1;
2320
2321 input_get_position(input, &x, &y);
2322 window_show_menu(window->display, input, time, window,
2323 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002324}
2325
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002326static int
2327frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002328 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002329{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002330 struct window_frame *frame = data;
2331 enum theme_location location;
2332
2333 location = frame_pointer_enter(frame->frame, input, x, y);
2334 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2335 widget_schedule_redraw(frame->widget);
2336
2337 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002338}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002339
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002340static int
2341frame_motion_handler(struct widget *widget,
2342 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002343 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002344{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002345 struct window_frame *frame = data;
2346 enum theme_location location;
2347
2348 location = frame_pointer_motion(frame->frame, input, x, y);
2349 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2350 widget_schedule_redraw(frame->widget);
2351
2352 return frame_get_pointer_image_for_location(data, location);
2353}
2354
2355static void
2356frame_leave_handler(struct widget *widget,
2357 struct input *input, void *data)
2358{
2359 struct window_frame *frame = data;
2360
2361 frame_pointer_leave(frame->frame, input);
2362 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2363 widget_schedule_redraw(frame->widget);
2364}
2365
2366static void
2367frame_handle_status(struct window_frame *frame, struct input *input,
2368 uint32_t time, enum theme_location location)
2369{
2370 struct window *window = frame->widget->window;
2371 uint32_t status;
2372
2373 status = frame_status(frame->frame);
2374 if (status & FRAME_STATUS_REPAINT)
2375 widget_schedule_redraw(frame->widget);
2376
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002377 if (status & FRAME_STATUS_MINIMIZE) {
2378 window_set_minimized(window);
2379 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2380 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002381
2382 if (status & FRAME_STATUS_MENU) {
2383 window_show_frame_menu(window, input, time);
2384 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2385 }
2386
2387 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002388 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002389 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2390 }
2391
2392 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002393 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002394 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002395 }
2396
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002397 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002398 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002399 xdg_surface_move(window->xdg_surface,
2400 input_get_seat(input),
2401 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002402
2403 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2404 }
2405
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002406 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002407 input_ungrab(input);
2408
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002409 xdg_surface_resize(window->xdg_surface,
2410 input_get_seat(input),
2411 window->display->serial,
2412 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002413
2414 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2415 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002416}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002417
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002418#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002419static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002420frame_button_handler(struct widget *widget,
2421 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002422 uint32_t button, enum wl_pointer_button_state state,
2423 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002424
2425{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002426 struct window_frame *frame = data;
2427 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002428
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002429 frame->double_click = 0;
2430 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2431 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2432 frame->double_click = 1;
2433 frame->did_double = 1;
2434 } else
2435 frame->did_double = 0;
2436
2437 frame->last_time = time;
2438 } else if (frame->did_double == 1) {
2439 frame->double_click = 1;
2440 frame->did_double = 0;
2441 }
2442
2443 if (frame->double_click)
2444 location = frame_double_click(frame->frame, input,
2445 button, state);
2446 else
2447 location = frame_pointer_button(frame->frame, input,
2448 button, state);
2449
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002450 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002451}
2452
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002453static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002454frame_touch_down_handler(struct widget *widget, struct input *input,
2455 uint32_t serial, uint32_t time, int32_t id,
2456 float x, float y, void *data)
2457{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002458 struct window_frame *frame = data;
2459
Xiong Zhang382de462014-06-12 11:06:26 +08002460 frame->double_click = 0;
2461 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2462 frame->last_id == id) {
2463 frame->double_click = 1;
2464 frame->did_double = 1;
2465 frame->double_id = id;
2466 } else
2467 frame->did_double = 0;
2468
2469 frame->last_time = time;
2470 frame->last_id = id;
2471
2472 if (frame->double_click)
2473 frame_double_touch_down(frame->frame, input, id, x, y);
2474 else
2475 frame_touch_down(frame->frame, input, id, x, y);
2476
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002477 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2478}
2479
2480static void
2481frame_touch_up_handler(struct widget *widget,
2482 struct input *input, uint32_t serial, uint32_t time,
2483 int32_t id, void *data)
2484{
2485 struct window_frame *frame = data;
2486
Xiong Zhang382de462014-06-12 11:06:26 +08002487 if (frame->double_id == id && frame->did_double) {
2488 frame->did_double = 0;
2489 frame->double_id = 0;
2490 frame_double_touch_up(frame->frame, input, id);
2491 } else
2492 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002493 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002494}
2495
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002496struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002497window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002499 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002500 uint32_t buttons;
2501
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002502 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002503 buttons = FRAME_BUTTON_NONE;
2504 } else {
2505 buttons = FRAME_BUTTON_ALL;
2506 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002507
Peter Huttererf3d62272013-08-08 11:57:05 +10002508 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002509 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002510 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002511
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002512 frame->widget = window_add_widget(window, frame);
2513 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002514
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002515 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2516 widget_set_resize_handler(frame->widget, frame_resize_handler);
2517 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002518 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002519 widget_set_motion_handler(frame->widget, frame_motion_handler);
2520 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002521 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002522 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002523
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002524 window->frame = frame;
2525
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002526 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002527}
2528
Kristian Høgsberga1627922012-06-20 17:30:03 -04002529void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002530window_frame_set_child_size(struct widget *widget, int child_width,
2531 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002532{
2533 struct display *display = widget->window->display;
2534 struct theme *t = display->theme;
2535 int decoration_width, decoration_height;
2536 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002537 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002538
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002539 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002540 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002541 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002542 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002543
2544 width = child_width + decoration_width;
2545 height = child_height + decoration_height;
2546 } else {
2547 width = child_width;
2548 height = child_height;
2549 }
2550
2551 window_schedule_resize(widget->window, width, height);
2552}
2553
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002554static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002555window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002556{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002557 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002558
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002559 /* frame->child must be destroyed by the application */
2560 widget_destroy(frame->widget);
2561 free(frame);
2562}
2563
2564static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002565input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002566 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002567{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002568 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002569 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002570
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002571 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002572 return;
2573
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002574 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002575 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002576 widget = old;
2577 if (input->grab)
2578 widget = input->grab;
2579 if (widget->leave_handler)
2580 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002581 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002582 }
2583
2584 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002585 widget = focus;
2586 if (input->grab)
2587 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002588 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002589 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002590 cursor = widget->enter_handler(focus, input, x, y,
2591 widget->user_data);
2592 else
2593 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002594
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002595 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002596 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002597}
2598
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002599void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002600touch_grab(struct input *input, int32_t touch_id)
2601{
2602 input->touch_grab = 1;
2603 input->touch_grab_id = touch_id;
2604}
2605
2606void
2607touch_ungrab(struct input *input)
2608{
2609 struct touch_point *tp, *tmp;
2610
2611 input->touch_grab = 0;
2612
2613 wl_list_for_each_safe(tp, tmp,
2614 &input->touch_point_list, link) {
2615 if (tp->id != input->touch_grab_id)
2616 continue;
2617 wl_list_remove(&tp->link);
2618 free(tp);
2619
2620 return;
2621 }
2622}
2623
2624void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002625input_grab(struct input *input, struct widget *widget, uint32_t button)
2626{
2627 input->grab = widget;
2628 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002629
2630 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002631}
2632
2633void
2634input_ungrab(struct input *input)
2635{
2636 struct widget *widget;
2637
2638 input->grab = NULL;
2639 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002640 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002641 input->sx, input->sy);
2642 input_set_focus_widget(input, widget, input->sx, input->sy);
2643 }
2644}
2645
2646static void
2647input_remove_pointer_focus(struct input *input)
2648{
2649 struct window *window = input->pointer_focus;
2650
2651 if (!window)
2652 return;
2653
2654 input_set_focus_widget(input, NULL, 0, 0);
2655
2656 input->pointer_focus = NULL;
2657 input->current_cursor = CURSOR_UNSET;
2658}
2659
2660static void
2661pointer_handle_enter(void *data, struct wl_pointer *pointer,
2662 uint32_t serial, struct wl_surface *surface,
2663 wl_fixed_t sx_w, wl_fixed_t sy_w)
2664{
2665 struct input *input = data;
2666 struct window *window;
2667 struct widget *widget;
2668 float sx = wl_fixed_to_double(sx_w);
2669 float sy = wl_fixed_to_double(sy_w);
2670
2671 if (!surface) {
2672 /* enter event for a window we've just destroyed */
2673 return;
2674 }
2675
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002676 window = wl_surface_get_user_data(surface);
2677 if (surface != window->main_surface->surface) {
2678 DBG("Ignoring input event from subsurface %p\n", surface);
2679 return;
2680 }
2681
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002682 input->display->serial = serial;
2683 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002684 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002685
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002686 input->sx = sx;
2687 input->sy = sy;
2688
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002689 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002690 input_set_focus_widget(input, widget, sx, sy);
2691}
2692
2693static void
2694pointer_handle_leave(void *data, struct wl_pointer *pointer,
2695 uint32_t serial, struct wl_surface *surface)
2696{
2697 struct input *input = data;
2698
2699 input->display->serial = serial;
2700 input_remove_pointer_focus(input);
2701}
2702
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002703static void
Daniel Stone37816df2012-05-16 18:45:18 +01002704pointer_handle_motion(void *data, struct wl_pointer *pointer,
2705 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002706{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002707 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002708 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002709 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002710 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002711 float sx = wl_fixed_to_double(sx_w);
2712 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002713
Paul Winwoodb22bf572013-08-29 10:52:54 +01002714 if (!window)
2715 return;
2716
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002717 input->sx = sx;
2718 input->sy = sy;
2719
Rob Bradford5f087742013-07-11 19:41:27 +01002720 /* when making the window smaller - e.g. after a unmaximise we might
2721 * still have a pending motion event that the compositor has picked
2722 * based on the old surface dimensions
2723 */
2724 if (sx > window->main_surface->allocation.width ||
2725 sy > window->main_surface->allocation.height)
2726 return;
2727
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002728 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002729 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002730 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002731 }
2732
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002733 if (input->grab)
2734 widget = input->grab;
2735 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002736 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002737 if (widget) {
2738 if (widget->motion_handler)
2739 cursor = widget->motion_handler(input->focus_widget,
2740 input, time, sx, sy,
2741 widget->user_data);
2742 else
2743 cursor = widget->default_cursor;
2744 } else
2745 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002746
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002747 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002748}
2749
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002750static void
Daniel Stone37816df2012-05-16 18:45:18 +01002751pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002752 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002753{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002754 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002755 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002756 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002757
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002758 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002759 if (input->focus_widget && input->grab == NULL &&
2760 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002761 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002762
Neil Roberts6b28aad2012-01-23 19:11:18 +00002763 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002764 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002765 (*widget->button_handler)(widget,
2766 input, time,
2767 button, state,
2768 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002769
Daniel Stone4dbadb12012-05-30 16:31:51 +01002770 if (input->grab && input->grab_button == button &&
2771 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002772 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002773}
2774
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002775static void
Daniel Stone37816df2012-05-16 18:45:18 +01002776pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002777 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002778{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002779 struct input *input = data;
2780 struct widget *widget;
2781
2782 widget = input->focus_widget;
2783 if (input->grab)
2784 widget = input->grab;
2785 if (widget && widget->axis_handler)
2786 (*widget->axis_handler)(widget,
2787 input, time,
2788 axis, value,
2789 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002790}
2791
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002792static const struct wl_pointer_listener pointer_listener = {
2793 pointer_handle_enter,
2794 pointer_handle_leave,
2795 pointer_handle_motion,
2796 pointer_handle_button,
2797 pointer_handle_axis,
2798};
2799
2800static void
2801input_remove_keyboard_focus(struct input *input)
2802{
2803 struct window *window = input->keyboard_focus;
2804 struct itimerspec its;
2805
2806 its.it_interval.tv_sec = 0;
2807 its.it_interval.tv_nsec = 0;
2808 its.it_value.tv_sec = 0;
2809 its.it_value.tv_nsec = 0;
2810 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2811
2812 if (!window)
2813 return;
2814
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002815 if (window->keyboard_focus_handler)
2816 (*window->keyboard_focus_handler)(window, NULL,
2817 window->user_data);
2818
2819 input->keyboard_focus = NULL;
2820}
2821
2822static void
2823keyboard_repeat_func(struct task *task, uint32_t events)
2824{
2825 struct input *input =
2826 container_of(task, struct input, repeat_task);
2827 struct window *window = input->keyboard_focus;
2828 uint64_t exp;
2829
2830 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2831 /* If we change the timer between the fd becoming
2832 * readable and getting here, there'll be nothing to
2833 * read and we get EAGAIN. */
2834 return;
2835
2836 if (window && window->key_handler) {
2837 (*window->key_handler)(window, input, input->repeat_time,
2838 input->repeat_key, input->repeat_sym,
2839 WL_KEYBOARD_KEY_STATE_PRESSED,
2840 window->user_data);
2841 }
2842}
2843
2844static void
2845keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2846 uint32_t format, int fd, uint32_t size)
2847{
2848 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002849 struct xkb_keymap *keymap;
2850 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002851 char *map_str;
2852
2853 if (!data) {
2854 close(fd);
2855 return;
2856 }
2857
2858 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2859 close(fd);
2860 return;
2861 }
2862
2863 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2864 if (map_str == MAP_FAILED) {
2865 close(fd);
2866 return;
2867 }
2868
Ran Benita2e1968f2014-08-19 23:59:51 +03002869 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2870 map_str,
2871 XKB_KEYMAP_FORMAT_TEXT_V1,
2872 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002873 munmap(map_str, size);
2874 close(fd);
2875
Rui Matos3eccb862013-10-10 19:44:22 +02002876 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002877 fprintf(stderr, "failed to compile keymap\n");
2878 return;
2879 }
2880
Rui Matos3eccb862013-10-10 19:44:22 +02002881 state = xkb_state_new(keymap);
2882 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002883 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002884 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002885 return;
2886 }
2887
Rui Matos3eccb862013-10-10 19:44:22 +02002888 xkb_keymap_unref(input->xkb.keymap);
2889 xkb_state_unref(input->xkb.state);
2890 input->xkb.keymap = keymap;
2891 input->xkb.state = state;
2892
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002893 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002894 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002895 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002896 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002897 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002898 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002899}
2900
2901static void
2902keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2903 uint32_t serial, struct wl_surface *surface,
2904 struct wl_array *keys)
2905{
2906 struct input *input = data;
2907 struct window *window;
2908
2909 input->display->serial = serial;
2910 input->keyboard_focus = wl_surface_get_user_data(surface);
2911
2912 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002913 if (window->keyboard_focus_handler)
2914 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002915 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002916}
2917
2918static void
2919keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2920 uint32_t serial, struct wl_surface *surface)
2921{
2922 struct input *input = data;
2923
2924 input->display->serial = serial;
2925 input_remove_keyboard_focus(input);
2926}
2927
Scott Moreau210d0792012-03-22 10:47:01 -06002928static void
Daniel Stone37816df2012-05-16 18:45:18 +01002929keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002930 uint32_t serial, uint32_t time, uint32_t key,
2931 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002932{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002933 struct input *input = data;
2934 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002935 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002936 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002937 const xkb_keysym_t *syms;
2938 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002939 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002940
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002941 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002942 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002943 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002944 return;
2945
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002946 /* We only use input grabs for pointer events for now, so just
2947 * ignore key presses if a grab is active. We expand the key
2948 * event delivery mechanism to route events to widgets to
2949 * properly handle key grabs. In the meantime, this prevents
2950 * key event devlivery while a grab is active. */
2951 if (input->grab && input->grab_button == 0)
2952 return;
2953
Ran Benita2e1968f2014-08-19 23:59:51 +03002954 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002955
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002956 sym = XKB_KEY_NoSymbol;
2957 if (num_syms == 1)
2958 sym = syms[0];
2959
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002960
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002961 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002962 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002963 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002964 } else if (sym == XKB_KEY_F11 &&
2965 window->fullscreen_handler &&
2966 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2967 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002968 } else if (sym == XKB_KEY_F4 &&
2969 input->modifiers == MOD_ALT_MASK &&
2970 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002971 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002972 } else if (window->key_handler) {
2973 (*window->key_handler)(window, input, time, key,
2974 sym, state, window->user_data);
2975 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002976
2977 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2978 key == input->repeat_key) {
2979 its.it_interval.tv_sec = 0;
2980 its.it_interval.tv_nsec = 0;
2981 its.it_value.tv_sec = 0;
2982 its.it_value.tv_nsec = 0;
2983 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002984 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2985 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002986 input->repeat_sym = sym;
2987 input->repeat_key = key;
2988 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002989 its.it_interval.tv_sec = input->repeat_rate_sec;
2990 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2991 its.it_value.tv_sec = input->repeat_delay_sec;
2992 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002993 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2994 }
2995}
2996
2997static void
Daniel Stone351eb612012-05-31 15:27:47 -04002998keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2999 uint32_t serial, uint32_t mods_depressed,
3000 uint32_t mods_latched, uint32_t mods_locked,
3001 uint32_t group)
3002{
3003 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003004 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003005
Matt Ropere61561f2013-06-24 16:52:43 +01003006 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3007 if (!input->xkb.keymap)
3008 return;
3009
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003010 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3011 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003012 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003013 XKB_STATE_MODS_DEPRESSED |
3014 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003015 input->modifiers = 0;
3016 if (mask & input->xkb.control_mask)
3017 input->modifiers |= MOD_CONTROL_MASK;
3018 if (mask & input->xkb.alt_mask)
3019 input->modifiers |= MOD_ALT_MASK;
3020 if (mask & input->xkb.shift_mask)
3021 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003022}
3023
Jonny Lamb06959082014-08-12 14:58:27 +02003024static void
3025set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3026{
3027 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3028 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3029
3030 /* a rate of zero disables any repeating, regardless of the delay's
3031 * value */
3032 if (rate == 0)
3033 return;
3034
3035 if (rate == 1)
3036 input->repeat_rate_sec = 1;
3037 else
3038 input->repeat_rate_nsec = 1000000000 / rate;
3039
3040 input->repeat_delay_sec = delay / 1000;
3041 delay -= (input->repeat_delay_sec * 1000);
3042 input->repeat_delay_nsec = delay * 1000 * 1000;
3043}
3044
3045static void
3046keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3047 int32_t rate, int32_t delay)
3048{
3049 struct input *input = data;
3050
3051 set_repeat_info(input, rate, delay);
3052}
3053
Daniel Stone37816df2012-05-16 18:45:18 +01003054static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003055 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003056 keyboard_handle_enter,
3057 keyboard_handle_leave,
3058 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003059 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003060 keyboard_handle_repeat_info
3061
Daniel Stone37816df2012-05-16 18:45:18 +01003062};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003063
3064static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003065touch_handle_down(void *data, struct wl_touch *wl_touch,
3066 uint32_t serial, uint32_t time, struct wl_surface *surface,
3067 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3068{
3069 struct input *input = data;
3070 struct widget *widget;
3071 float sx = wl_fixed_to_double(x_w);
3072 float sy = wl_fixed_to_double(y_w);
3073
Rusty Lynch1084da52013-08-15 09:10:08 -07003074 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003075 input->touch_focus = wl_surface_get_user_data(surface);
3076 if (!input->touch_focus) {
3077 DBG("Failed to find to touch focus for surface %p\n", surface);
3078 return;
3079 }
3080
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003081 if (surface != input->touch_focus->main_surface->surface) {
3082 DBG("Ignoring input event from subsurface %p\n", surface);
3083 input->touch_focus = NULL;
3084 return;
3085 }
3086
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003087 if (input->grab)
3088 widget = input->grab;
3089 else
3090 widget = window_find_widget(input->touch_focus,
3091 wl_fixed_to_double(x_w),
3092 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003093 if (widget) {
3094 struct touch_point *tp = xmalloc(sizeof *tp);
3095 if (tp) {
3096 tp->id = id;
3097 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003098 tp->x = sx;
3099 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003100 wl_list_insert(&input->touch_point_list, &tp->link);
3101
3102 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003103 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003104 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003105 sx, sy,
3106 widget->user_data);
3107 }
3108 }
3109}
3110
3111static void
3112touch_handle_up(void *data, struct wl_touch *wl_touch,
3113 uint32_t serial, uint32_t time, int32_t id)
3114{
3115 struct input *input = data;
3116 struct touch_point *tp, *tmp;
3117
Rusty Lynch041815a2013-08-08 21:20:38 -07003118 if (!input->touch_focus) {
3119 DBG("No touch focus found for touch up event!\n");
3120 return;
3121 }
3122
3123 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3124 if (tp->id != id)
3125 continue;
3126
3127 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003128 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003129 time, id,
3130 tp->widget->user_data);
3131
3132 wl_list_remove(&tp->link);
3133 free(tp);
3134
3135 return;
3136 }
3137}
3138
3139static void
3140touch_handle_motion(void *data, struct wl_touch *wl_touch,
3141 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3142{
3143 struct input *input = data;
3144 struct touch_point *tp;
3145 float sx = wl_fixed_to_double(x_w);
3146 float sy = wl_fixed_to_double(y_w);
3147
3148 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3149
3150 if (!input->touch_focus) {
3151 DBG("No touch focus found for touch motion event!\n");
3152 return;
3153 }
3154
3155 wl_list_for_each(tp, &input->touch_point_list, link) {
3156 if (tp->id != id)
3157 continue;
3158
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003159 tp->x = sx;
3160 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003161 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003162 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003163 id, sx, sy,
3164 tp->widget->user_data);
3165 return;
3166 }
3167}
3168
3169static void
3170touch_handle_frame(void *data, struct wl_touch *wl_touch)
3171{
3172 struct input *input = data;
3173 struct touch_point *tp, *tmp;
3174
3175 DBG("touch_handle_frame\n");
3176
3177 if (!input->touch_focus) {
3178 DBG("No touch focus found for touch frame event!\n");
3179 return;
3180 }
3181
3182 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3183 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003184 (*tp->widget->touch_frame_handler)(tp->widget, input,
3185 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003186 }
3187}
3188
3189static void
3190touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3191{
3192 struct input *input = data;
3193 struct touch_point *tp, *tmp;
3194
3195 DBG("touch_handle_cancel\n");
3196
3197 if (!input->touch_focus) {
3198 DBG("No touch focus found for touch cancel event!\n");
3199 return;
3200 }
3201
3202 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3203 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003204 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3205 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003206
3207 wl_list_remove(&tp->link);
3208 free(tp);
3209 }
3210}
3211
3212static const struct wl_touch_listener touch_listener = {
3213 touch_handle_down,
3214 touch_handle_up,
3215 touch_handle_motion,
3216 touch_handle_frame,
3217 touch_handle_cancel,
3218};
3219
3220static void
Daniel Stone37816df2012-05-16 18:45:18 +01003221seat_handle_capabilities(void *data, struct wl_seat *seat,
3222 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003223{
Daniel Stone37816df2012-05-16 18:45:18 +01003224 struct input *input = data;
3225
3226 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3227 input->pointer = wl_seat_get_pointer(seat);
3228 wl_pointer_set_user_data(input->pointer, input);
3229 wl_pointer_add_listener(input->pointer, &pointer_listener,
3230 input);
3231 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3232 wl_pointer_destroy(input->pointer);
3233 input->pointer = NULL;
3234 }
3235
3236 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3237 input->keyboard = wl_seat_get_keyboard(seat);
3238 wl_keyboard_set_user_data(input->keyboard, input);
3239 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3240 input);
3241 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3242 wl_keyboard_destroy(input->keyboard);
3243 input->keyboard = NULL;
3244 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003245
3246 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3247 input->touch = wl_seat_get_touch(seat);
3248 wl_touch_set_user_data(input->touch, input);
3249 wl_touch_add_listener(input->touch, &touch_listener, input);
3250 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3251 wl_touch_destroy(input->touch);
3252 input->touch = NULL;
3253 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003254}
3255
Rob Bradford08031182013-08-13 20:11:03 +01003256static void
3257seat_handle_name(void *data, struct wl_seat *seat,
3258 const char *name)
3259{
3260
3261}
3262
Daniel Stone37816df2012-05-16 18:45:18 +01003263static const struct wl_seat_listener seat_listener = {
3264 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003265 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003266};
3267
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003268void
3269input_get_position(struct input *input, int32_t *x, int32_t *y)
3270{
3271 *x = input->sx;
3272 *y = input->sy;
3273}
3274
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003275int
3276input_get_touch(struct input *input, int32_t id, float *x, float *y)
3277{
3278 struct touch_point *tp;
3279
3280 wl_list_for_each(tp, &input->touch_point_list, link) {
3281 if (tp->id != id)
3282 continue;
3283
3284 *x = tp->x;
3285 *y = tp->y;
3286 return 0;
3287 }
3288
3289 return -1;
3290}
3291
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003292struct display *
3293input_get_display(struct input *input)
3294{
3295 return input->display;
3296}
3297
Daniel Stone37816df2012-05-16 18:45:18 +01003298struct wl_seat *
3299input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003300{
Daniel Stone37816df2012-05-16 18:45:18 +01003301 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003302}
3303
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003304uint32_t
3305input_get_modifiers(struct input *input)
3306{
3307 return input->modifiers;
3308}
3309
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003310struct widget *
3311input_get_focus_widget(struct input *input)
3312{
3313 return input->focus_widget;
3314}
3315
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003316struct data_offer {
3317 struct wl_data_offer *offer;
3318 struct input *input;
3319 struct wl_array types;
3320 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003321
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003322 struct task io_task;
3323 int fd;
3324 data_func_t func;
3325 int32_t x, y;
3326 void *user_data;
3327};
3328
3329static void
3330data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3331{
3332 struct data_offer *offer = data;
3333 char **p;
3334
3335 p = wl_array_add(&offer->types, sizeof *p);
3336 *p = strdup(type);
3337}
3338
3339static const struct wl_data_offer_listener data_offer_listener = {
3340 data_offer_offer,
3341};
3342
3343static void
3344data_offer_destroy(struct data_offer *offer)
3345{
3346 char **p;
3347
3348 offer->refcount--;
3349 if (offer->refcount == 0) {
3350 wl_data_offer_destroy(offer->offer);
3351 for (p = offer->types.data; *p; p++)
3352 free(*p);
3353 wl_array_release(&offer->types);
3354 free(offer);
3355 }
3356}
3357
3358static void
3359data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003360 struct wl_data_device *data_device,
3361 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003362{
3363 struct data_offer *offer;
3364
Brian Lovinbc919262013-08-07 15:34:59 -07003365 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003366
3367 wl_array_init(&offer->types);
3368 offer->refcount = 1;
3369 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003370 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003371 wl_data_offer_add_listener(offer->offer,
3372 &data_offer_listener, offer);
3373}
3374
3375static void
3376data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003377 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003378 wl_fixed_t x_w, wl_fixed_t y_w,
3379 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003380{
3381 struct input *input = data;
3382 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003383 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003384 float x = wl_fixed_to_double(x_w);
3385 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003386 char **p;
3387
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003388 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003389 input->drag_enter_serial = serial;
3390 input->drag_focus = window,
3391 input->drag_x = x;
3392 input->drag_y = y;
3393
3394 if (!input->touch_grab)
3395 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003396
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003397 if (offer) {
3398 input->drag_offer = wl_data_offer_get_user_data(offer);
3399
3400 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3401 *p = NULL;
3402
3403 types_data = input->drag_offer->types.data;
3404 } else {
3405 input->drag_offer = NULL;
3406 types_data = NULL;
3407 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003408
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003409 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003410 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003411 window->user_data);
3412}
3413
3414static void
3415data_device_leave(void *data, struct wl_data_device *data_device)
3416{
3417 struct input *input = data;
3418
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003419 if (input->drag_offer) {
3420 data_offer_destroy(input->drag_offer);
3421 input->drag_offer = NULL;
3422 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003423}
3424
3425static void
3426data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003427 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003428{
3429 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003430 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003431 float x = wl_fixed_to_double(x_w);
3432 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003433 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003434
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003435 input->drag_x = x;
3436 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003437
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003438 if (input->drag_offer)
3439 types_data = input->drag_offer->types.data;
3440 else
3441 types_data = NULL;
3442
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003443 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003444 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003445 window->user_data);
3446}
3447
3448static void
3449data_device_drop(void *data, struct wl_data_device *data_device)
3450{
3451 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003452 struct window *window = input->drag_focus;
3453 float x, y;
3454
3455 x = input->drag_x;
3456 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003457
3458 if (window->drop_handler)
3459 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003460 x, y, window->user_data);
3461
3462 if (input->touch_grab)
3463 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003464}
3465
3466static void
3467data_device_selection(void *data,
3468 struct wl_data_device *wl_data_device,
3469 struct wl_data_offer *offer)
3470{
3471 struct input *input = data;
3472 char **p;
3473
3474 if (input->selection_offer)
3475 data_offer_destroy(input->selection_offer);
3476
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003477 if (offer) {
3478 input->selection_offer = wl_data_offer_get_user_data(offer);
3479 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3480 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003481 } else {
3482 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003483 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003484}
3485
3486static const struct wl_data_device_listener data_device_listener = {
3487 data_device_data_offer,
3488 data_device_enter,
3489 data_device_leave,
3490 data_device_motion,
3491 data_device_drop,
3492 data_device_selection
3493};
3494
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003495static void
3496input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003497{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003498 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003499 struct wl_cursor *cursor;
3500 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003501
Daniel Stone80972742012-11-07 17:51:39 +11003502 if (!input->pointer)
3503 return;
3504
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003505 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003506 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003507 return;
3508
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003509 if (index >= (int) cursor->image_count) {
3510 fprintf(stderr, "cursor index out of range\n");
3511 return;
3512 }
3513
3514 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003515 buffer = wl_cursor_image_get_buffer(image);
3516 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003517 return;
3518
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003519 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3520 wl_surface_damage(input->pointer_surface, 0, 0,
3521 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003522 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003523 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3524 input->pointer_surface,
3525 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003526}
3527
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003528static const struct wl_callback_listener pointer_surface_listener;
3529
Derek Foreman493d9792015-03-04 16:26:25 -06003530static bool
3531input_set_pointer_special(struct input *input)
3532{
3533 if (input->current_cursor == CURSOR_BLANK) {
3534 wl_pointer_set_cursor(input->pointer,
3535 input->pointer_enter_serial,
3536 NULL, 0, 0);
3537 return true;
3538 }
3539
3540 if (input->current_cursor == CURSOR_UNSET)
3541 return true;
3542
3543 return false;
3544}
3545
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003546static void
3547pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3548 uint32_t time)
3549{
3550 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003551 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003552 int i;
3553
3554 if (callback) {
3555 assert(callback == input->cursor_frame_cb);
3556 wl_callback_destroy(callback);
3557 input->cursor_frame_cb = NULL;
3558 }
3559
Daniel Stone80972742012-11-07 17:51:39 +11003560 if (!input->pointer)
3561 return;
3562
Derek Foreman493d9792015-03-04 16:26:25 -06003563 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003564 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003565
Daniel Stonea494f1d2012-06-18 19:31:12 +01003566 cursor = input->display->cursors[input->current_cursor];
3567 if (!cursor)
3568 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003569
3570 /* FIXME We don't have the current time on the first call so we set
3571 * the animation start to the time of the first frame callback. */
3572 if (time == 0)
3573 input->cursor_anim_start = 0;
3574 else if (input->cursor_anim_start == 0)
3575 input->cursor_anim_start = time;
3576
3577 if (time == 0 || input->cursor_anim_start == 0)
3578 i = 0;
3579 else
3580 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3581
Pekka Paalanenbc106382012-10-10 12:49:31 +03003582 if (cursor->image_count > 1) {
3583 input->cursor_frame_cb =
3584 wl_surface_frame(input->pointer_surface);
3585 wl_callback_add_listener(input->cursor_frame_cb,
3586 &pointer_surface_listener, input);
3587 }
3588
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003589 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003590}
3591
3592static const struct wl_callback_listener pointer_surface_listener = {
3593 pointer_surface_frame_callback
3594};
3595
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003596void
3597input_set_pointer_image(struct input *input, int pointer)
3598{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003599 int force = 0;
3600
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003601 if (!input->pointer)
3602 return;
3603
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003604 if (input->pointer_enter_serial > input->cursor_serial)
3605 force = 1;
3606
3607 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003608 return;
3609
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003610 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003611 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003612 if (!input->cursor_frame_cb)
3613 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003614 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003615 /* The current frame callback may be stuck if, for instance,
3616 * the set cursor request was processed by the server after
3617 * this client lost the focus. In this case the cursor surface
3618 * might not be mapped and the frame callback wouldn't ever
3619 * complete. Send a set_cursor and attach to try to map the
3620 * cursor surface again so that the callback will finish */
3621 input_set_pointer_image_index(input, 0);
3622 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003623}
3624
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003625struct wl_data_device *
3626input_get_data_device(struct input *input)
3627{
3628 return input->data_device;
3629}
3630
3631void
3632input_set_selection(struct input *input,
3633 struct wl_data_source *source, uint32_t time)
3634{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003635 if (input->data_device)
3636 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003637}
3638
3639void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003640input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003641{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003642 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003643 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003644}
3645
3646static void
3647offer_io_func(struct task *task, uint32_t events)
3648{
3649 struct data_offer *offer =
3650 container_of(task, struct data_offer, io_task);
3651 unsigned int len;
3652 char buffer[4096];
3653
3654 len = read(offer->fd, buffer, sizeof buffer);
3655 offer->func(buffer, len,
3656 offer->x, offer->y, offer->user_data);
3657
3658 if (len == 0) {
3659 close(offer->fd);
3660 data_offer_destroy(offer);
3661 }
3662}
3663
3664static void
3665data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3666 data_func_t func, void *user_data)
3667{
3668 int p[2];
3669
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003670 if (pipe2(p, O_CLOEXEC) == -1)
3671 return;
3672
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003673 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3674 close(p[1]);
3675
3676 offer->io_task.run = offer_io_func;
3677 offer->fd = p[0];
3678 offer->func = func;
3679 offer->refcount++;
3680 offer->user_data = user_data;
3681
3682 display_watch_fd(offer->input->display,
3683 offer->fd, EPOLLIN, &offer->io_task);
3684}
3685
3686void
3687input_receive_drag_data(struct input *input, const char *mime_type,
3688 data_func_t func, void *data)
3689{
3690 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003691 input->drag_offer->x = input->drag_x;
3692 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003693}
3694
3695int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003696input_receive_drag_data_to_fd(struct input *input,
3697 const char *mime_type, int fd)
3698{
3699 if (input->drag_offer)
3700 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3701
3702 return 0;
3703}
3704
3705int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003706input_receive_selection_data(struct input *input, const char *mime_type,
3707 data_func_t func, void *data)
3708{
3709 char **p;
3710
3711 if (input->selection_offer == NULL)
3712 return -1;
3713
3714 for (p = input->selection_offer->types.data; *p; p++)
3715 if (strcmp(mime_type, *p) == 0)
3716 break;
3717
3718 if (*p == NULL)
3719 return -1;
3720
3721 data_offer_receive_data(input->selection_offer,
3722 mime_type, func, data);
3723 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003724}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003725
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003726int
3727input_receive_selection_data_to_fd(struct input *input,
3728 const char *mime_type, int fd)
3729{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003730 if (input->selection_offer)
3731 wl_data_offer_receive(input->selection_offer->offer,
3732 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003733
3734 return 0;
3735}
3736
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003737void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003738window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003739{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003740 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003741 return;
3742
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003743 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003744}
3745
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003746static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003747surface_set_synchronized(struct surface *surface)
3748{
3749 if (!surface->subsurface)
3750 return;
3751
3752 if (surface->synchronized)
3753 return;
3754
3755 wl_subsurface_set_sync(surface->subsurface);
3756 surface->synchronized = 1;
3757}
3758
3759static void
3760surface_set_synchronized_default(struct surface *surface)
3761{
3762 if (!surface->subsurface)
3763 return;
3764
3765 if (surface->synchronized == surface->synchronized_default)
3766 return;
3767
3768 if (surface->synchronized_default)
3769 wl_subsurface_set_sync(surface->subsurface);
3770 else
3771 wl_subsurface_set_desync(surface->subsurface);
3772
3773 surface->synchronized = surface->synchronized_default;
3774}
3775
3776static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003777surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003778{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003779 struct widget *widget = surface->widget;
3780 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003781
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003782 if (surface->input_region) {
3783 wl_region_destroy(surface->input_region);
3784 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003785 }
3786
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003787 if (surface->opaque_region)
3788 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003789
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003790 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003791
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003792 if (widget->resize_handler)
3793 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003794 widget->allocation.width,
3795 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003796 widget->user_data);
3797
Pekka Paalanen35e82632013-04-25 13:57:48 +03003798 if (surface->subsurface &&
3799 (surface->allocation.x != widget->allocation.x ||
3800 surface->allocation.y != widget->allocation.y)) {
3801 wl_subsurface_set_position(surface->subsurface,
3802 widget->allocation.x,
3803 widget->allocation.y);
3804 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003805 if (surface->allocation.width != widget->allocation.width ||
3806 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003807 window_schedule_redraw(widget->window);
3808 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003809 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003810
3811 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003812 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003813 widget->allocation.width,
3814 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003815}
3816
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003817static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003818hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3819{
3820 /*
3821 * This hack should be removed, when EGL respects
3822 * eglSwapInterval(0).
3823 *
3824 * If this window has sub-surfaces, especially a free-running
3825 * EGL-widget, we need to post the parent surface once with
3826 * all the old state to guarantee, that the EGL-widget will
3827 * receive its frame callback soon. Otherwise, a forced call
3828 * to eglSwapBuffers may end up blocking, waiting for a frame
3829 * event that will never come, because we will commit the parent
3830 * surface with all new state only after eglSwapBuffers returns.
3831 *
3832 * This assumes, that:
3833 * 1. When the EGL widget's resize hook is called, it pauses.
3834 * 2. When the EGL widget's redraw hook is called, it forces a
3835 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3836 * In a single threaded application condition 1 is a no-op.
3837 *
3838 * XXX: This should actually be after the surface_resize() calls,
3839 * but cannot, because then it would commit the incomplete state
3840 * accumulated from the widget resize hooks.
3841 */
3842 if (window->subsurface_list.next != &window->main_surface->link ||
3843 window->subsurface_list.prev != &window->main_surface->link)
3844 wl_surface_commit(window->main_surface->surface);
3845}
3846
3847static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003848window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003849{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003850 struct surface *surface;
3851
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003852 widget_set_allocation(window->main_surface->widget,
3853 window->pending_allocation.x,
3854 window->pending_allocation.y,
3855 window->pending_allocation.width,
3856 window->pending_allocation.height);
3857
3858 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003859
3860 /* The main surface is in the list, too. Main surface's
3861 * resize_handler is responsible for calling widget_set_allocation()
3862 * on all sub-surface root widgets, so they will be resized
3863 * properly.
3864 */
3865 wl_list_for_each(surface, &window->subsurface_list, link) {
3866 if (surface == window->main_surface)
3867 continue;
3868
3869 surface_set_synchronized(surface);
3870 surface_resize(surface);
3871 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003872
3873 if (!window->fullscreen && !window->maximized)
3874 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003875}
3876
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003877static void
3878idle_resize(struct window *window)
3879{
3880 window->resize_needed = 0;
3881 window->redraw_needed = 1;
3882
3883 DBG("from %dx%d to %dx%d\n",
3884 window->main_surface->server_allocation.width,
3885 window->main_surface->server_allocation.height,
3886 window->pending_allocation.width,
3887 window->pending_allocation.height);
3888
3889 hack_prevent_EGL_sub_surface_deadlock(window);
3890
3891 window_do_resize(window);
3892}
3893
3894static void
3895undo_resize(struct window *window)
3896{
3897 window->pending_allocation.width =
3898 window->main_surface->server_allocation.width;
3899 window->pending_allocation.height =
3900 window->main_surface->server_allocation.height;
3901
3902 DBG("back to %dx%d\n",
3903 window->main_surface->server_allocation.width,
3904 window->main_surface->server_allocation.height);
3905
3906 window_do_resize(window);
3907
3908 if (window->pending_allocation.width == 0 &&
3909 window->pending_allocation.height == 0) {
3910 fprintf(stderr, "Error: Could not draw a surface, "
3911 "most likely due to insufficient disk space in "
3912 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3913 exit(EXIT_FAILURE);
3914 }
3915}
3916
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003917void
3918window_schedule_resize(struct window *window, int width, int height)
3919{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003920 /* We should probably get these numbers from the theme. */
3921 const int min_width = 200, min_height = 200;
3922
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003923 window->pending_allocation.x = 0;
3924 window->pending_allocation.y = 0;
3925 window->pending_allocation.width = width;
3926 window->pending_allocation.height = height;
3927
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003928 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003929 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003930 window->min_allocation.width = min_width;
3931 else
3932 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003933 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003934 window->min_allocation.height = min_height;
3935 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003936 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003937 }
3938
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003939 if (window->pending_allocation.width < window->min_allocation.width)
3940 window->pending_allocation.width = window->min_allocation.width;
3941 if (window->pending_allocation.height < window->min_allocation.height)
3942 window->pending_allocation.height = window->min_allocation.height;
3943
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003944 window->resize_needed = 1;
3945 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003946}
3947
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003948void
3949widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3950{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003951 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003952}
3953
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003954static int
3955window_get_shadow_margin(struct window *window)
3956{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003957 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003958 return frame_get_shadow_margin(window->frame->frame);
3959 else
3960 return 0;
3961}
3962
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003963static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003964handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003965 int32_t width, int32_t height,
3966 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003967{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003968 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003969 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003970
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003971 window->maximized = 0;
3972 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003973 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003974 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003975
3976 wl_array_for_each(p, states) {
3977 uint32_t state = *p;
3978 switch (state) {
3979 case XDG_SURFACE_STATE_MAXIMIZED:
3980 window->maximized = 1;
3981 break;
3982 case XDG_SURFACE_STATE_FULLSCREEN:
3983 window->fullscreen = 1;
3984 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003985 case XDG_SURFACE_STATE_RESIZING:
3986 window->resizing = 1;
3987 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003988 case XDG_SURFACE_STATE_ACTIVATED:
3989 window->focused = 1;
3990 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003991 default:
3992 /* Unknown state */
3993 break;
3994 }
3995 }
3996
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003997 if (window->frame) {
3998 if (window->maximized) {
3999 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4000 } else {
4001 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4002 }
4003
4004 if (window->focused) {
4005 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4006 } else {
4007 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4008 }
4009 }
4010
Jasper St. Pierref184c382014-05-06 08:33:27 -04004011 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004012 /* The width / height params are for window geometry,
4013 * but window_schedule_resize takes allocation. Add
4014 * on the shadow margin to get the difference. */
4015 int margin = window_get_shadow_margin(window);
4016
4017 window_schedule_resize(window,
4018 width + margin * 2,
4019 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004020 } else {
4021 window_schedule_resize(window,
4022 window->saved_allocation.width,
4023 window->saved_allocation.height);
4024 }
4025
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004026 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004027
4028 if (window->state_changed_handler)
4029 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004030}
4031
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004032static void
4033handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4034{
4035 struct window *window = data;
4036 window_close(window);
4037}
4038
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004039static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004040 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004041 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004042};
4043
4044static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004045window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004046{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004047 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004048
4049 if (!window->xdg_surface)
4050 return;
4051
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004052 if (window->parent == window->last_parent)
4053 return;
4054
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004055 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004056 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004057 else
4058 parent_surface = NULL;
4059
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004060 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004061 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004062}
4063
4064static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004065window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004066{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004067 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004068 frame_input_rect(window->frame->frame,
4069 &geometry->x,
4070 &geometry->y,
4071 &geometry->width,
4072 &geometry->height);
4073 else
4074 window_get_allocation(window, geometry);
4075}
4076
4077static void
4078window_sync_geometry(struct window *window)
4079{
4080 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004081
4082 if (!window->xdg_surface)
4083 return;
4084
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004085 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004086 if (geometry.x == window->last_geometry.x &&
4087 geometry.y == window->last_geometry.y &&
4088 geometry.width == window->last_geometry.width &&
4089 geometry.height == window->last_geometry.height)
4090 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004091
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004092 xdg_surface_set_window_geometry(window->xdg_surface,
4093 geometry.x,
4094 geometry.y,
4095 geometry.width,
4096 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004097 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004098}
4099
4100static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004101window_flush(struct window *window)
4102{
4103 struct surface *surface;
4104
4105 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004106 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004107 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004108 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004109 }
4110 }
4111
4112 wl_list_for_each(surface, &window->subsurface_list, link) {
4113 if (surface == window->main_surface)
4114 continue;
4115
4116 surface_flush(surface);
4117 }
4118
4119 surface_flush(window->main_surface);
4120}
4121
4122static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004123menu_destroy(struct menu *menu)
4124{
4125 widget_destroy(menu->widget);
4126 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004127 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004128 free(menu);
4129}
4130
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004131void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004132window_get_allocation(struct window *window,
4133 struct rectangle *allocation)
4134{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004135 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004136}
4137
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004138static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004139widget_redraw(struct widget *widget)
4140{
4141 struct widget *child;
4142
4143 if (widget->redraw_handler)
4144 widget->redraw_handler(widget, widget->user_data);
4145 wl_list_for_each(child, &widget->child_list, link)
4146 widget_redraw(child);
4147}
4148
4149static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004150frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4151{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004152 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004153
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004154 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004155 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004156 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004157 surface->frame_cb = NULL;
4158
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004159 surface->last_time = time;
4160
Pekka Paalanen71233882013-04-25 13:57:53 +03004161 if (surface->redraw_needed || surface->window->redraw_needed) {
4162 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004163 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004164 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004165}
4166
4167static const struct wl_callback_listener listener = {
4168 frame_callback
4169};
4170
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004171static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004172surface_redraw(struct surface *surface)
4173{
Pekka Paalanen71233882013-04-25 13:57:53 +03004174 DBG_OBJ(surface->surface, "begin\n");
4175
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004176 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004177 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004178
4179 /* Whole-window redraw forces a redraw even if the previous has
4180 * not yet hit the screen.
4181 */
4182 if (surface->frame_cb) {
4183 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004184 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004185
Pekka Paalanen71233882013-04-25 13:57:53 +03004186 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004187 wl_callback_destroy(surface->frame_cb);
4188 }
4189
Neil Roberts97b747c2013-12-19 16:17:12 +00004190 if (surface->widget->use_cairo &&
4191 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004192 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4193 return -1;
4194 }
4195
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004196 surface->frame_cb = wl_surface_frame(surface->surface);
4197 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004198 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004199
4200 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004201 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004202 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004203 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004204 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004205}
4206
4207static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004208idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004209{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004210 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004211 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004212 int failed = 0;
4213 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004214
Pekka Paalanen71233882013-04-25 13:57:53 +03004215 DBG(" --------- \n");
4216
Pekka Paalaneneebff542013-04-25 13:57:52 +03004217 wl_list_init(&window->redraw_task.link);
4218 window->redraw_task_scheduled = 0;
4219
4220 if (window->resize_needed) {
4221 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004222 if (window->main_surface->frame_cb) {
4223 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004224 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004225 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004226
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004227 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004228 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004229 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004230
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004231 if (surface_redraw(window->main_surface) < 0) {
4232 /*
4233 * Only main_surface failure will cause us to undo the resize.
4234 * If sub-surfaces fail, they will just be broken with old
4235 * content.
4236 */
4237 failed = 1;
4238 } else {
4239 wl_list_for_each(surface, &window->subsurface_list, link) {
4240 if (surface == window->main_surface)
4241 continue;
4242
4243 surface_redraw(surface);
4244 }
4245 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004246
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004247 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004248 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004249
4250 wl_list_for_each(surface, &window->subsurface_list, link)
4251 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004252
4253 if (resized && failed) {
4254 /* Restore widget tree to correspond to what is on screen. */
4255 undo_resize(window);
4256 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004257}
4258
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004259static void
4260window_schedule_redraw_task(struct window *window)
4261{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004262 if (!window->redraw_task_scheduled) {
4263 window->redraw_task.run = idle_redraw;
4264 display_defer(window->display, &window->redraw_task);
4265 window->redraw_task_scheduled = 1;
4266 }
4267}
4268
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004269void
4270window_schedule_redraw(struct window *window)
4271{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004272 struct surface *surface;
4273
Pekka Paalanen71233882013-04-25 13:57:53 +03004274 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4275
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004276 wl_list_for_each(surface, &window->subsurface_list, link)
4277 surface->redraw_needed = 1;
4278
4279 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004280}
4281
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004282int
4283window_is_fullscreen(struct window *window)
4284{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004285 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004286}
4287
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004288void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004289window_set_fullscreen(struct window *window, int fullscreen)
4290{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004291 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004292 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004293
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004294 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004295 return;
4296
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004297 if (fullscreen)
4298 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4299 else
4300 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004301}
4302
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004303int
4304window_is_maximized(struct window *window)
4305{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004306 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004307}
4308
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004309void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004310window_set_maximized(struct window *window, int maximized)
4311{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004312 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004313 return;
4314
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004315 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004316 return;
4317
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004318 if (maximized)
4319 xdg_surface_set_maximized(window->xdg_surface);
4320 else
4321 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004322}
4323
Jasper St. Pierrede680992014-04-10 17:23:49 -07004324int
4325window_is_resizing(struct window *window)
4326{
4327 return window->resizing;
4328}
4329
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004330void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004331window_set_minimized(struct window *window)
4332{
4333 if (!window->xdg_surface)
4334 return;
4335
4336 xdg_surface_set_minimized(window->xdg_surface);
4337}
4338
4339void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004340window_set_user_data(struct window *window, void *data)
4341{
4342 window->user_data = data;
4343}
4344
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004345void *
4346window_get_user_data(struct window *window)
4347{
4348 return window->user_data;
4349}
4350
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004351void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004352window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004353 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004354{
4355 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004356}
4357
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004358void
4359window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004360 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004361{
4362 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004363}
4364
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004365void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004366window_set_data_handler(struct window *window, window_data_handler_t handler)
4367{
4368 window->data_handler = handler;
4369}
4370
4371void
4372window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4373{
4374 window->drop_handler = handler;
4375}
4376
4377void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004378window_set_close_handler(struct window *window,
4379 window_close_handler_t handler)
4380{
4381 window->close_handler = handler;
4382}
4383
4384void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004385window_set_fullscreen_handler(struct window *window,
4386 window_fullscreen_handler_t handler)
4387{
4388 window->fullscreen_handler = handler;
4389}
4390
4391void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004392window_set_output_handler(struct window *window,
4393 window_output_handler_t handler)
4394{
4395 window->output_handler = handler;
4396}
4397
4398void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004399window_set_state_changed_handler(struct window *window,
4400 window_state_changed_handler_t handler)
4401{
4402 window->state_changed_handler = handler;
4403}
4404
4405void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004406window_set_title(struct window *window, const char *title)
4407{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004408 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004409 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004410 if (window->frame) {
4411 frame_set_title(window->frame->frame, title);
4412 widget_schedule_redraw(window->frame->widget);
4413 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004414 if (window->xdg_surface)
4415 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004416}
4417
4418const char *
4419window_get_title(struct window *window)
4420{
4421 return window->title;
4422}
4423
4424void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004425window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4426{
4427 struct text_cursor_position *text_cursor_position =
4428 window->display->text_cursor_position;
4429
Scott Moreau9295ce02012-06-01 12:46:10 -06004430 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004431 return;
4432
4433 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004434 window->main_surface->surface,
4435 wl_fixed_from_int(x),
4436 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004437}
4438
4439void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004440window_damage(struct window *window, int32_t x, int32_t y,
4441 int32_t width, int32_t height)
4442{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004443 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004444}
4445
Casey Dahlin9074db52012-04-19 22:50:09 -04004446static void
4447surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004448 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004449{
Rob Bradford7507b572012-05-15 17:55:34 +01004450 struct window *window = data;
4451 struct output *output;
4452 struct output *output_found = NULL;
4453 struct window_output *window_output;
4454
4455 wl_list_for_each(output, &window->display->output_list, link) {
4456 if (output->output == wl_output) {
4457 output_found = output;
4458 break;
4459 }
4460 }
4461
4462 if (!output_found)
4463 return;
4464
Brian Lovinbc919262013-08-07 15:34:59 -07004465 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004466 window_output->output = output_found;
4467
4468 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004469
4470 if (window->output_handler)
4471 window->output_handler(window, output_found, 1,
4472 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004473}
4474
4475static void
4476surface_leave(void *data,
4477 struct wl_surface *wl_surface, struct wl_output *output)
4478{
Rob Bradford7507b572012-05-15 17:55:34 +01004479 struct window *window = data;
4480 struct window_output *window_output;
4481 struct window_output *window_output_found = NULL;
4482
4483 wl_list_for_each(window_output, &window->window_output_list, link) {
4484 if (window_output->output->output == output) {
4485 window_output_found = window_output;
4486 break;
4487 }
4488 }
4489
4490 if (window_output_found) {
4491 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004492
4493 if (window->output_handler)
4494 window->output_handler(window, window_output->output,
4495 0, window->user_data);
4496
Rob Bradford7507b572012-05-15 17:55:34 +01004497 free(window_output_found);
4498 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004499}
4500
4501static const struct wl_surface_listener surface_listener = {
4502 surface_enter,
4503 surface_leave
4504};
4505
Pekka Paalanen4e373742013-02-13 16:17:13 +02004506static struct surface *
4507surface_create(struct window *window)
4508{
4509 struct display *display = window->display;
4510 struct surface *surface;
4511
Brian Lovinbc919262013-08-07 15:34:59 -07004512 surface = xmalloc(sizeof *surface);
4513 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004514 if (!surface)
4515 return NULL;
4516
4517 surface->window = window;
4518 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004519 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004520 wl_surface_add_listener(surface->surface, &surface_listener, window);
4521
Pekka Paalanen35e82632013-04-25 13:57:48 +03004522 wl_list_insert(&window->subsurface_list, &surface->link);
4523
Pekka Paalanen4e373742013-02-13 16:17:13 +02004524 return surface;
4525}
4526
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004527static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004528get_preferred_buffer_type(struct display *display)
4529{
4530#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004531 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004532 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4533#endif
4534
4535 return WINDOW_BUFFER_TYPE_SHM;
4536}
4537
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004538static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004539window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004540{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004541 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004542 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004543
Peter Huttererf3d62272013-08-08 11:57:05 +10004544 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004545 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004546 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004547
4548 surface = surface_create(window);
4549 window->main_surface = surface;
4550
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004551 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004552
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004553 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004554 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004555
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004556 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004557
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004558 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004559 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004560 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004561
Rob Bradford7507b572012-05-15 17:55:34 +01004562 wl_list_init (&window->window_output_list);
4563
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004564 return window;
4565}
4566
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004567struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004568window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004569{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004570 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004571 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004572
4573 window = window_create_internal(display, 0);
4574
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004575 if (window->display->xdg_shell) {
4576 window->xdg_surface =
4577 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4578 window->main_surface->surface);
4579 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004580
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004581 xdg_surface_set_user_data(window->xdg_surface, window);
4582 xdg_surface_add_listener(window->xdg_surface,
4583 &xdg_surface_listener, window);
4584 } else if (display->ivi_application) {
4585 /* auto generation of ivi_id based on process id + basement of id */
4586 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4587 window->ivi_surface =
4588 ivi_application_surface_create(display->ivi_application,
4589 id_ivisurf, window->main_surface->surface);
4590 fail_on_null(window->ivi_surface);
4591
4592 ivi_surface_add_listener(window->ivi_surface,
4593 &ivi_surface_listener, window);
4594 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004595
4596 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004597}
4598
4599struct window *
4600window_create_custom(struct display *display)
4601{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004602 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004603}
4604
Jasper St. Pierre53686042013-12-09 15:26:25 -05004605void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004606window_set_parent(struct window *window,
4607 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004608{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004609 window->parent = parent_window;
4610 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004611}
4612
4613struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004614window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004615{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004616 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004617}
4618
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004619static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004620menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004621{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004622 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004623 int next;
4624
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004625 frame_interior(menu->frame, &x, &y, &width, &height);
4626 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004627 if (menu->current != next) {
4628 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004629 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004630 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004631}
4632
4633static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004634menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004635 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004636 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004637{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004638 struct menu *menu = data;
4639
4640 if (widget == menu->widget)
4641 menu_set_item(data, y);
4642
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004643 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004644}
4645
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004646static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004647menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004648 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004649{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004650 struct menu *menu = data;
4651
4652 if (widget == menu->widget)
4653 menu_set_item(data, y);
4654
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004655 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004656}
4657
4658static void
4659menu_leave_handler(struct widget *widget, struct input *input, void *data)
4660{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004661 struct menu *menu = data;
4662
4663 if (widget == menu->widget)
4664 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004665}
4666
4667static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004668menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004669 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004670 uint32_t button, enum wl_pointer_button_state state,
4671 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004672
4673{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004674 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004675
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004676 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4677 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004678 /* Either relase after press-drag-release or
4679 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004680 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004681 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004682 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004683 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004684 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004685 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004686}
4687
4688static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004689menu_touch_up_handler(struct widget *widget,
4690 struct input *input,
4691 uint32_t serial,
4692 uint32_t time,
4693 int32_t id,
4694 void *data)
4695{
4696 struct menu *menu = data;
4697
4698 input_ungrab(input);
4699 menu_destroy(menu);
4700}
4701
4702static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004703menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004704{
4705 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004706 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004707 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004708
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004709 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004710
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004711 frame_repaint(menu->frame, cr);
4712 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004713
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004714 theme_set_background_source(menu->window->display->theme,
4715 cr, THEME_FRAME_ACTIVE);
4716 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004717 cairo_fill(cr);
4718
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004719 cairo_select_font_face(cr, "sans",
4720 CAIRO_FONT_SLANT_NORMAL,
4721 CAIRO_FONT_WEIGHT_NORMAL);
4722 cairo_set_font_size(cr, 12);
4723
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004724 for (i = 0; i < menu->count; i++) {
4725 if (i == menu->current) {
4726 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004727 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004728 cairo_fill(cr);
4729 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004730 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004731 cairo_show_text(cr, menu->entries[i]);
4732 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004733 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4734 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004735 cairo_show_text(cr, menu->entries[i]);
4736 }
4737 }
4738
4739 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004740}
4741
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004742static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004743handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004744{
4745 struct window *window = data;
4746 struct menu *menu = window->main_surface->widget->user_data;
4747
4748 input_ungrab(menu->input);
4749 menu_destroy(menu);
4750}
4751
4752static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004753 handle_popup_popup_done,
4754};
4755
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004756static struct menu *
4757create_menu(struct display *display,
4758 struct input *input, uint32_t time,
4759 menu_func_t func, const char **entries, int count,
4760 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004761{
4762 struct window *window;
4763 struct menu *menu;
4764
4765 menu = malloc(sizeof *menu);
4766 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004767 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004768
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004769 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004770 if (!window) {
4771 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004772 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004773 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004774
4775 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004776 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004777 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004778 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004779 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004780 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004781 menu->entries = entries;
4782 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004783 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004784 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004785 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004786 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004787 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004788
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004789 input_ungrab(input);
4790
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004791 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004792 widget_set_enter_handler(menu->widget, menu_enter_handler);
4793 widget_set_leave_handler(menu->widget, menu_leave_handler);
4794 widget_set_motion_handler(menu->widget, menu_motion_handler);
4795 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004796 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004797
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004798 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004799 frame_resize_inside(menu->frame, 200, count * 20);
4800 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4801 window_schedule_resize(window, frame_width(menu->frame),
4802 frame_height(menu->frame));
4803
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004804 return menu;
4805}
4806
4807struct window *
4808window_create_menu(struct display *display,
4809 struct input *input, uint32_t time,
4810 menu_func_t func, const char **entries, int count,
4811 void *user_data)
4812{
4813 struct menu *menu;
4814 menu = create_menu(display, input, time, func, entries, count, user_data);
4815
4816 if (menu == NULL)
4817 return NULL;
4818
4819 return menu->window;
4820}
4821
4822void
4823window_show_menu(struct display *display,
4824 struct input *input, uint32_t time, struct window *parent,
4825 int32_t x, int32_t y,
4826 menu_func_t func, const char **entries, int count)
4827{
4828 struct menu *menu;
4829 struct window *window;
4830 int32_t ix, iy;
4831
4832 menu = create_menu(display, input, time, func, entries, count, parent);
4833
4834 if (menu == NULL)
4835 return;
4836
4837 window = menu->window;
4838
4839 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4840 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4841
4842 window->x = x;
4843 window->y = y;
4844
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004845 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004846
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004847 if (!display->xdg_shell)
4848 return;
4849
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004850 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4851 window->main_surface->surface,
4852 parent->main_surface->surface,
4853 input->seat,
4854 display_get_serial(window->display),
4855 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004856 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004857 fail_on_null(window->xdg_popup);
4858
4859 xdg_popup_set_user_data(window->xdg_popup, window);
4860 xdg_popup_add_listener(window->xdg_popup,
4861 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004862}
4863
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004864void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004865window_set_buffer_type(struct window *window, enum window_buffer_type type)
4866{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004867 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004868}
4869
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004870enum window_buffer_type
4871window_get_buffer_type(struct window *window)
4872{
4873 return window->main_surface->buffer_type;
4874}
4875
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004876void
4877window_set_preferred_format(struct window *window,
4878 enum preferred_format format)
4879{
4880 window->preferred_format = format;
4881}
4882
Pekka Paalanen35e82632013-04-25 13:57:48 +03004883struct widget *
4884window_add_subsurface(struct window *window, void *data,
4885 enum subsurface_mode default_mode)
4886{
4887 struct widget *widget;
4888 struct surface *surface;
4889 struct wl_surface *parent;
4890 struct wl_subcompositor *subcompo = window->display->subcompositor;
4891
Pekka Paalanen35e82632013-04-25 13:57:48 +03004892 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004893 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004894 widget = widget_create(window, surface, data);
4895 wl_list_init(&widget->link);
4896 surface->widget = widget;
4897
4898 parent = window->main_surface->surface;
4899 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4900 surface->surface,
4901 parent);
4902 surface->synchronized = 1;
4903
4904 switch (default_mode) {
4905 case SUBSURFACE_SYNCHRONIZED:
4906 surface->synchronized_default = 1;
4907 break;
4908 case SUBSURFACE_DESYNCHRONIZED:
4909 surface->synchronized_default = 0;
4910 break;
4911 default:
4912 assert(!"bad enum subsurface_mode");
4913 }
4914
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004915 window->resize_needed = 1;
4916 window_schedule_redraw(window);
4917
Pekka Paalanen35e82632013-04-25 13:57:48 +03004918 return widget;
4919}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004920
4921static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004922display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004923 struct wl_output *wl_output,
4924 int x, int y,
4925 int physical_width,
4926 int physical_height,
4927 int subpixel,
4928 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004929 const char *model,
4930 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004931{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004932 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004933
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004934 output->allocation.x = x;
4935 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004936 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004937
4938 if (output->make)
4939 free(output->make);
4940 output->make = strdup(make);
4941
4942 if (output->model)
4943 free(output->model);
4944 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004945}
4946
4947static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004948display_handle_done(void *data,
4949 struct wl_output *wl_output)
4950{
4951}
4952
4953static void
4954display_handle_scale(void *data,
4955 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004956 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004957{
4958 struct output *output = data;
4959
4960 output->scale = scale;
4961}
4962
4963static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004964display_handle_mode(void *data,
4965 struct wl_output *wl_output,
4966 uint32_t flags,
4967 int width,
4968 int height,
4969 int refresh)
4970{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004971 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004972 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004973
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004974 if (flags & WL_OUTPUT_MODE_CURRENT) {
4975 output->allocation.width = width;
4976 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004977 if (display->output_configure_handler)
4978 (*display->output_configure_handler)(
4979 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004980 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004981}
4982
4983static const struct wl_output_listener output_listener = {
4984 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004985 display_handle_mode,
4986 display_handle_done,
4987 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004988};
4989
4990static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004991display_add_output(struct display *d, uint32_t id)
4992{
4993 struct output *output;
4994
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004995 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004996 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004997 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004998 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004999 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005000 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005001 wl_list_insert(d->output_list.prev, &output->link);
5002
5003 wl_output_add_listener(output->output, &output_listener, output);
5004}
5005
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005006static void
5007output_destroy(struct output *output)
5008{
5009 if (output->destroy_handler)
5010 (*output->destroy_handler)(output, output->user_data);
5011
5012 wl_output_destroy(output->output);
5013 wl_list_remove(&output->link);
5014 free(output);
5015}
5016
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005017static void
5018display_destroy_output(struct display *d, uint32_t id)
5019{
5020 struct output *output;
5021
5022 wl_list_for_each(output, &d->output_list, link) {
5023 if (output->server_output_id == id) {
5024 output_destroy(output);
5025 break;
5026 }
5027 }
5028}
5029
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005030void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005031display_set_global_handler(struct display *display,
5032 display_global_handler_t handler)
5033{
5034 struct global *global;
5035
5036 display->global_handler = handler;
5037 if (!handler)
5038 return;
5039
5040 wl_list_for_each(global, &display->global_list, link)
5041 display->global_handler(display,
5042 global->name, global->interface,
5043 global->version, display->user_data);
5044}
5045
5046void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005047display_set_global_handler_remove(struct display *display,
5048 display_global_handler_t remove_handler)
5049{
5050 display->global_handler_remove = remove_handler;
5051 if (!remove_handler)
5052 return;
5053}
5054
5055void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005056display_set_output_configure_handler(struct display *display,
5057 display_output_handler_t handler)
5058{
5059 struct output *output;
5060
5061 display->output_configure_handler = handler;
5062 if (!handler)
5063 return;
5064
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005065 wl_list_for_each(output, &display->output_list, link) {
5066 if (output->allocation.width == 0 &&
5067 output->allocation.height == 0)
5068 continue;
5069
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005070 (*display->output_configure_handler)(output,
5071 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005072 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005073}
5074
5075void
5076output_set_user_data(struct output *output, void *data)
5077{
5078 output->user_data = data;
5079}
5080
5081void *
5082output_get_user_data(struct output *output)
5083{
5084 return output->user_data;
5085}
5086
5087void
5088output_set_destroy_handler(struct output *output,
5089 display_output_handler_t handler)
5090{
5091 output->destroy_handler = handler;
5092 /* FIXME: implement this, once we have way to remove outputs */
5093}
5094
5095void
Scott Moreau4e072362012-09-29 02:03:11 -06005096output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005097{
Scott Moreau4e072362012-09-29 02:03:11 -06005098 struct rectangle allocation = output->allocation;
5099
5100 switch (output->transform) {
5101 case WL_OUTPUT_TRANSFORM_90:
5102 case WL_OUTPUT_TRANSFORM_270:
5103 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5104 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5105 /* Swap width and height */
5106 allocation.width = output->allocation.height;
5107 allocation.height = output->allocation.width;
5108 break;
5109 }
5110
5111 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005112}
5113
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005114struct wl_output *
5115output_get_wl_output(struct output *output)
5116{
5117 return output->output;
5118}
5119
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005120enum wl_output_transform
5121output_get_transform(struct output *output)
5122{
5123 return output->transform;
5124}
5125
Alexander Larssonafd319a2013-05-22 14:41:27 +02005126uint32_t
5127output_get_scale(struct output *output)
5128{
5129 return output->scale;
5130}
5131
Jason Ekstrand738715d2014-04-02 19:53:50 -05005132const char *
5133output_get_make(struct output *output)
5134{
5135 return output->make;
5136}
5137
5138const char *
5139output_get_model(struct output *output)
5140{
5141 return output->model;
5142}
5143
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005144static void
Daniel Stone97f68542012-05-30 16:32:01 +01005145fini_xkb(struct input *input)
5146{
5147 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005148 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005149}
5150
Jasper St. Pierre47f10432013-11-12 14:37:20 -05005151#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005152
Daniel Stone97f68542012-05-30 16:32:01 +01005153static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005154display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005155{
5156 struct input *input;
5157
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005158 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005159 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005160 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005161 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005162 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005163 input->pointer_focus = NULL;
5164 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005165 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005166 wl_list_insert(d->input_list.prev, &input->link);
5167
Daniel Stone37816df2012-05-16 18:45:18 +01005168 wl_seat_add_listener(input->seat, &seat_listener, input);
5169 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005170
Jason Ekstranda669bd52014-04-02 19:53:51 -05005171 if (d->data_device_manager) {
5172 input->data_device =
5173 wl_data_device_manager_get_data_device(d->data_device_manager,
5174 input->seat);
5175 wl_data_device_add_listener(input->data_device,
5176 &data_device_listener,
5177 input);
5178 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005179
5180 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005181
Jonny Lamb06959082014-08-12 14:58:27 +02005182 set_repeat_info(input, 40, 400);
5183
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005184 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5185 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005186 input->repeat_task.run = keyboard_repeat_func;
5187 display_watch_fd(d, input->repeat_timer_fd,
5188 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005189}
5190
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005191static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005192input_destroy(struct input *input)
5193{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005194 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005195 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005196
5197 if (input->drag_offer)
5198 data_offer_destroy(input->drag_offer);
5199
5200 if (input->selection_offer)
5201 data_offer_destroy(input->selection_offer);
5202
kabeer khan6ce67ec2014-10-20 11:55:29 +05305203 if (input->data_device) {
5204 if(input->display->data_device_manager_version >= 2)
5205 wl_data_device_release(input->data_device);
5206 else
5207 wl_data_device_destroy(input->data_device);
5208 }
Rob Bradford08031182013-08-13 20:11:03 +01005209 if (input->display->seat_version >= 3) {
5210 if (input->pointer)
5211 wl_pointer_release(input->pointer);
5212 if (input->keyboard)
5213 wl_keyboard_release(input->keyboard);
5214 }
5215
Daniel Stone97f68542012-05-30 16:32:01 +01005216 fini_xkb(input);
5217
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005218 wl_surface_destroy(input->pointer_surface);
5219
Pekka Paalanene1207c72011-12-16 12:02:09 +02005220 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005221 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005222 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005223 free(input);
5224}
5225
5226static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005227init_workspace_manager(struct display *d, uint32_t id)
5228{
5229 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005230 wl_registry_bind(d->registry, id,
5231 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005232 if (d->workspace_manager != NULL)
5233 workspace_manager_add_listener(d->workspace_manager,
5234 &workspace_manager_listener,
5235 d);
5236}
5237
5238static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005239shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5240{
5241 struct display *d = data;
5242
5243 if (format == WL_SHM_FORMAT_RGB565)
5244 d->has_rgb565 = 1;
5245}
5246
5247struct wl_shm_listener shm_listener = {
5248 shm_format
5249};
5250
5251static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005252xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5253{
5254 xdg_shell_pong(shell, serial);
5255}
5256
5257static const struct xdg_shell_listener xdg_shell_listener = {
5258 xdg_shell_ping,
5259};
5260
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005261#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005262#ifdef static_assert
5263static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5264 "Interface version doesn't match implementation version");
5265#endif
5266
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005267static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005268registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5269 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005270{
5271 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005272 struct global *global;
5273
Brian Lovinbc919262013-08-07 15:34:59 -07005274 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005275 global->name = id;
5276 global->interface = strdup(interface);
5277 global->version = version;
5278 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005279
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005280 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005281 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005282 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005283 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005284 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005285 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005286 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005287 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005288 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005289 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005290 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005291 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305292 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005293 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005294 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305295 &wl_data_device_manager_interface,
5296 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005297 } else if (strcmp(interface, "xdg_shell") == 0) {
5298 d->xdg_shell = wl_registry_bind(registry, id,
5299 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005300 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005301 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005302 } else if (strcmp(interface, "text_cursor_position") == 0) {
5303 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005304 wl_registry_bind(registry, id,
5305 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005306 } else if (strcmp(interface, "workspace_manager") == 0) {
5307 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005308 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5309 d->subcompositor =
5310 wl_registry_bind(registry, id,
5311 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005312 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005313 else if (strcmp(interface, "ivi_application") == 0) {
5314 d->ivi_application =
5315 wl_registry_bind(registry, id,
5316 &ivi_application_interface, 1);
5317 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005318
5319 if (d->global_handler)
5320 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005321}
5322
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005323static void
5324registry_handle_global_remove(void *data, struct wl_registry *registry,
5325 uint32_t name)
5326{
5327 struct display *d = data;
5328 struct global *global;
5329 struct global *tmp;
5330
5331 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5332 if (global->name != name)
5333 continue;
5334
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005335 if (strcmp(global->interface, "wl_output") == 0)
5336 display_destroy_output(d, name);
5337
5338 /* XXX: Should destroy remaining bound globals */
5339
5340 if (d->global_handler_remove)
5341 d->global_handler_remove(d, name, global->interface,
5342 global->version, d->user_data);
5343
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005344 wl_list_remove(&global->link);
5345 free(global->interface);
5346 free(global);
5347 }
5348}
5349
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005350void *
5351display_bind(struct display *display, uint32_t name,
5352 const struct wl_interface *interface, uint32_t version)
5353{
5354 return wl_registry_bind(display->registry, name, interface, version);
5355}
5356
5357static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005358 registry_handle_global,
5359 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005360};
5361
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005362#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005363static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005364init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005365{
5366 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005367 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005368
Rob Clark6396ed32012-03-11 19:48:41 -05005369#ifdef USE_CAIRO_GLESV2
5370# define GL_BIT EGL_OPENGL_ES2_BIT
5371#else
5372# define GL_BIT EGL_OPENGL_BIT
5373#endif
5374
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005375 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005376 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005377 EGL_RED_SIZE, 1,
5378 EGL_GREEN_SIZE, 1,
5379 EGL_BLUE_SIZE, 1,
5380 EGL_ALPHA_SIZE, 1,
5381 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005382 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005383 EGL_NONE
5384 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005385
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005386#ifdef USE_CAIRO_GLESV2
5387 static const EGLint context_attribs[] = {
5388 EGL_CONTEXT_CLIENT_VERSION, 2,
5389 EGL_NONE
5390 };
5391 EGLint api = EGL_OPENGL_ES_API;
5392#else
5393 EGLint *context_attribs = NULL;
5394 EGLint api = EGL_OPENGL_API;
5395#endif
5396
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005397 d->dpy =
5398 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5399 d->display, NULL);
5400
Yuval Fledel45568f62010-12-06 09:18:12 -05005401 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005402 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005403 return -1;
5404 }
5405
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005406 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005407 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005408 return -1;
5409 }
5410
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005411 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5412 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005413 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005414 return -1;
5415 }
5416
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005417 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005418 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005419 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005420 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005421 return -1;
5422 }
5423
Benjamin Franzke0c991632011-09-27 21:57:31 +02005424 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5425 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005426 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005427 return -1;
5428 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005429
5430 return 0;
5431}
5432
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005433static void
5434fini_egl(struct display *display)
5435{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005436 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005437
5438 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5439 EGL_NO_CONTEXT);
5440
5441 eglTerminate(display->dpy);
5442 eglReleaseThread();
5443}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005444#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005445
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005446static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005447init_dummy_surface(struct display *display)
5448{
5449 int len;
5450 void *data;
5451
5452 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005453 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005454 display->dummy_surface =
5455 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5456 1, 1, len);
5457 display->dummy_surface_data = data;
5458}
5459
5460static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005461handle_display_data(struct task *task, uint32_t events)
5462{
5463 struct display *display =
5464 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005465 struct epoll_event ep;
5466 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005467
5468 display->display_fd_events = events;
5469
5470 if (events & EPOLLERR || events & EPOLLHUP) {
5471 display_exit(display);
5472 return;
5473 }
5474
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005475 if (events & EPOLLIN) {
5476 ret = wl_display_dispatch(display->display);
5477 if (ret == -1) {
5478 display_exit(display);
5479 return;
5480 }
5481 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005482
5483 if (events & EPOLLOUT) {
5484 ret = wl_display_flush(display->display);
5485 if (ret == 0) {
5486 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5487 ep.data.ptr = &display->display_task;
5488 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5489 display->display_fd, &ep);
5490 } else if (ret == -1 && errno != EAGAIN) {
5491 display_exit(display);
5492 return;
5493 }
5494 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005495}
5496
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005497static void
5498log_handler(const char *format, va_list args)
5499{
5500 vfprintf(stderr, format, args);
5501}
5502
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005503struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005504display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005505{
5506 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005507
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005508 wl_log_set_handler_client(log_handler);
5509
Peter Huttererf3d62272013-08-08 11:57:05 +10005510 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005511 if (d == NULL)
5512 return NULL;
5513
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005514 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005515 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005516 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005517 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005518 return NULL;
5519 }
5520
Rob Bradford5ab9c752013-07-26 16:29:43 +01005521 d->xkb_context = xkb_context_new(0);
5522 if (d->xkb_context == NULL) {
5523 fprintf(stderr, "Failed to create XKB context\n");
5524 free(d);
5525 return NULL;
5526 }
5527
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005528 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005529 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005530 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005531 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5532 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005533
5534 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005535 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005536 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005537 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005538
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005539 d->workspace = 0;
5540 d->workspace_count = 1;
5541
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005542 d->registry = wl_display_get_registry(d->display);
5543 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005544
Marek Chalupaa519d062014-12-05 13:49:40 +01005545 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005546 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5547 return NULL;
5548 }
5549
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005550#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005551 if (init_egl(d) < 0)
5552 fprintf(stderr, "EGL does not seem to work, "
5553 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005554#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005555
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005556 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005557
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005558 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005559
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005560 wl_list_init(&d->window_list);
5561
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005562 init_dummy_surface(d);
5563
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005564 return d;
5565}
5566
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005567static void
5568display_destroy_outputs(struct display *display)
5569{
5570 struct output *tmp;
5571 struct output *output;
5572
5573 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5574 output_destroy(output);
5575}
5576
Pekka Paalanene1207c72011-12-16 12:02:09 +02005577static void
5578display_destroy_inputs(struct display *display)
5579{
5580 struct input *tmp;
5581 struct input *input;
5582
5583 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5584 input_destroy(input);
5585}
5586
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005587void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005588display_destroy(struct display *display)
5589{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005590 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005591 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5592 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005593
5594 if (!wl_list_empty(&display->deferred_list))
5595 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5596
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005597 cairo_surface_destroy(display->dummy_surface);
5598 free(display->dummy_surface_data);
5599
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005600 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005601 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005602
Daniel Stone97f68542012-05-30 16:32:01 +01005603 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005604
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005605 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005606 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005607
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005608#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005609 if (display->argb_device)
5610 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005611#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005612
Pekka Paalanen35e82632013-04-25 13:57:48 +03005613 if (display->subcompositor)
5614 wl_subcompositor_destroy(display->subcompositor);
5615
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005616 if (display->xdg_shell)
5617 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005618
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005619 if (display->ivi_application)
5620 ivi_application_destroy(display->ivi_application);
5621
Pekka Paalanenc2052982011-12-16 11:41:32 +02005622 if (display->shm)
5623 wl_shm_destroy(display->shm);
5624
5625 if (display->data_device_manager)
5626 wl_data_device_manager_destroy(display->data_device_manager);
5627
5628 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005629 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005630
5631 close(display->epoll_fd);
5632
U. Artie Eoff44874d92012-10-02 21:12:35 -07005633 if (!(display->display_fd_events & EPOLLERR) &&
5634 !(display->display_fd_events & EPOLLHUP))
5635 wl_display_flush(display->display);
5636
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005637 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005638 free(display);
5639}
5640
5641void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005642display_set_user_data(struct display *display, void *data)
5643{
5644 display->user_data = data;
5645}
5646
5647void *
5648display_get_user_data(struct display *display)
5649{
5650 return display->user_data;
5651}
5652
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005653struct wl_display *
5654display_get_display(struct display *display)
5655{
5656 return display->display;
5657}
5658
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005659int
5660display_has_subcompositor(struct display *display)
5661{
5662 if (display->subcompositor)
5663 return 1;
5664
5665 wl_display_roundtrip(display->display);
5666
5667 return display->subcompositor != NULL;
5668}
5669
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005670cairo_device_t *
5671display_get_cairo_device(struct display *display)
5672{
5673 return display->argb_device;
5674}
5675
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005676struct output *
5677display_get_output(struct display *display)
5678{
5679 return container_of(display->output_list.next, struct output, link);
5680}
5681
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005682struct wl_compositor *
5683display_get_compositor(struct display *display)
5684{
5685 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005686}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005687
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005688uint32_t
5689display_get_serial(struct display *display)
5690{
5691 return display->serial;
5692}
5693
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005694EGLDisplay
5695display_get_egl_display(struct display *d)
5696{
5697 return d->dpy;
5698}
5699
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005700struct wl_data_source *
5701display_create_data_source(struct display *display)
5702{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005703 if (display->data_device_manager)
5704 return wl_data_device_manager_create_data_source(display->data_device_manager);
5705 else
5706 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005707}
5708
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005709EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005710display_get_argb_egl_config(struct display *d)
5711{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005712 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005713}
5714
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005715int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005716display_acquire_window_surface(struct display *display,
5717 struct window *window,
5718 EGLContext ctx)
5719{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005720 struct surface *surface = window->main_surface;
5721
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005722 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005723 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005724
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005725 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005726 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005727}
5728
5729void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005730display_release_window_surface(struct display *display,
5731 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005732{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005733 struct surface *surface = window->main_surface;
5734
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005735 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005736 return;
5737
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005738 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005739}
5740
5741void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005742display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005743{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005744 wl_list_insert(&display->deferred_list, &task->link);
5745}
5746
5747void
5748display_watch_fd(struct display *display,
5749 int fd, uint32_t events, struct task *task)
5750{
5751 struct epoll_event ep;
5752
5753 ep.events = events;
5754 ep.data.ptr = task;
5755 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5756}
5757
5758void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005759display_unwatch_fd(struct display *display, int fd)
5760{
5761 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5762}
5763
5764void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005765display_run(struct display *display)
5766{
5767 struct task *task;
5768 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005769 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005770
Pekka Paalanen826d7952011-12-15 10:14:07 +02005771 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005772 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005773 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005774 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005775 struct task, link);
5776 wl_list_remove(&task->link);
5777 task->run(task, 0);
5778 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005779
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005780 wl_display_dispatch_pending(display->display);
5781
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005782 if (!display->running)
5783 break;
5784
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005785 ret = wl_display_flush(display->display);
5786 if (ret < 0 && errno == EAGAIN) {
5787 ep[0].events =
5788 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5789 ep[0].data.ptr = &display->display_task;
5790
5791 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5792 display->display_fd, &ep[0]);
5793 } else if (ret < 0) {
5794 break;
5795 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005796
5797 count = epoll_wait(display->epoll_fd,
5798 ep, ARRAY_LENGTH(ep), -1);
5799 for (i = 0; i < count; i++) {
5800 task = ep[i].data.ptr;
5801 task->run(task, ep[i].events);
5802 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005803 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005804}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005805
5806void
5807display_exit(struct display *display)
5808{
5809 display->running = 0;
5810}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005811
5812void
5813keysym_modifiers_add(struct wl_array *modifiers_map,
5814 const char *name)
5815{
5816 size_t len = strlen(name) + 1;
5817 char *p;
5818
5819 p = wl_array_add(modifiers_map, len);
5820
5821 if (p == NULL)
5822 return;
5823
5824 strncpy(p, name, len);
5825}
5826
5827static xkb_mod_index_t
5828keysym_modifiers_get_index(struct wl_array *modifiers_map,
5829 const char *name)
5830{
5831 xkb_mod_index_t index = 0;
5832 char *p = modifiers_map->data;
5833
5834 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5835 if (strcmp(p, name) == 0)
5836 return index;
5837
5838 index++;
5839 p += strlen(p) + 1;
5840 }
5841
5842 return XKB_MOD_INVALID;
5843}
5844
5845xkb_mod_mask_t
5846keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5847 const char *name)
5848{
5849 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5850
5851 if (index == XKB_MOD_INVALID)
5852 return XKB_MOD_INVALID;
5853
5854 return 1 << index;
5855}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005856
5857void *
5858fail_on_null(void *p)
5859{
5860 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005861 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005862 exit(EXIT_FAILURE);
5863 }
5864
5865 return p;
5866}
5867
5868void *
5869xmalloc(size_t s)
5870{
5871 return fail_on_null(malloc(s));
5872}
5873
Peter Huttererf3d62272013-08-08 11:57:05 +10005874void *
5875xzalloc(size_t s)
5876{
5877 return fail_on_null(zalloc(s));
5878}
5879
Kristian Høgsbergce278412013-07-25 15:20:20 -07005880char *
5881xstrdup(const char *s)
5882{
5883 return fail_on_null(strdup(s));
5884}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005885
5886void *
5887xrealloc(char *p, size_t s)
5888{
5889 return fail_on_null(realloc(p, s));
5890}