blob: 62b51852c465e6905d9f4ff0b922e7b26812382d [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 =
644 weston_platform_create_egl_window(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{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001291 struct weston_config *config;
1292 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001293 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001294 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001295 unsigned int i, j;
1296 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001297
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001298 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001299 s = weston_config_get_section(config, "shell", NULL, NULL);
1300 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1301 weston_config_section_get_int(s, "cursor-size", &size, 32);
1302 weston_config_destroy(config);
1303
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001304 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001305 if (!display->cursor_theme) {
1306 fprintf(stderr, "could not load theme '%s'\n", theme);
1307 return;
1308 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001309 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001310 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001311 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001312
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001313 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001314 cursor = NULL;
1315 for (j = 0; !cursor && j < cursors[i].count; ++j)
1316 cursor = wl_cursor_theme_get_cursor(
1317 display->cursor_theme, cursors[i].names[j]);
1318
1319 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001320 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001321 cursors[i].names[0]);
1322
1323 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001324 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001325}
1326
1327static void
1328destroy_cursors(struct display *display)
1329{
1330 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001331 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001332}
1333
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001334struct wl_cursor_image *
1335display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001336{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001337 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001338
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001339 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001340}
1341
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001342static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001343surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001344{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001345 if (!surface->cairo_surface)
1346 return;
1347
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001348 if (surface->opaque_region) {
1349 wl_surface_set_opaque_region(surface->surface,
1350 surface->opaque_region);
1351 wl_region_destroy(surface->opaque_region);
1352 surface->opaque_region = NULL;
1353 }
1354
1355 if (surface->input_region) {
1356 wl_surface_set_input_region(surface->surface,
1357 surface->input_region);
1358 wl_region_destroy(surface->input_region);
1359 surface->input_region = NULL;
1360 }
1361
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001362 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001363 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001364 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001365
Pekka Paalanen89dee002013-02-13 16:17:20 +02001366 cairo_surface_destroy(surface->cairo_surface);
1367 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001368}
1369
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001370int
1371window_has_focus(struct window *window)
1372{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001373 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001374}
1375
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001376static void
1377window_close(struct window *window)
1378{
1379 if (window->close_handler)
1380 window->close_handler(window->user_data);
1381 else
1382 display_exit(window->display);
1383}
1384
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001385struct display *
1386window_get_display(struct window *window)
1387{
1388 return window->display;
1389}
1390
Pekka Paalanen89dee002013-02-13 16:17:20 +02001391static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001392handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1393 int32_t width, int32_t height)
1394{
1395 struct window *window = data;
1396
1397 window_schedule_resize(window, width, height);
1398}
1399
1400static const struct ivi_surface_listener ivi_surface_listener = {
1401 handle_ivi_surface_configure,
1402};
1403
1404static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001405surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001406{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001408 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001409
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001410 if (!surface->toysurface && display->dpy &&
1411 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001412 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001413 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001414 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001415 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001416 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001417 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001418
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001419 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001420 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001421 surface->surface,
1422 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001423
Pekka Paalanen89dee002013-02-13 16:17:20 +02001424 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001425 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001426 allocation.width, allocation.height, flags,
1427 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001428}
1429
1430static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001431window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001432{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001433 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001434 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001435
Pekka Paalanenec076692012-11-30 13:37:27 +02001436 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001437 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001438
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001439 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1440 flags |= SURFACE_HINT_RGB565;
1441
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001442 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001443}
1444
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001445int
1446window_get_buffer_transform(struct window *window)
1447{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001448 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001449}
1450
1451void
1452window_set_buffer_transform(struct window *window,
1453 enum wl_output_transform transform)
1454{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001455 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001456 wl_surface_set_buffer_transform(window->main_surface->surface,
1457 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001458}
1459
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001460void
1461window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001462 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001463{
1464 window->main_surface->buffer_scale = scale;
1465 wl_surface_set_buffer_scale(window->main_surface->surface,
1466 scale);
1467}
1468
1469uint32_t
1470window_get_buffer_scale(struct window *window)
1471{
1472 return window->main_surface->buffer_scale;
1473}
1474
Alexander Larssond68f5232013-05-22 14:41:33 +02001475uint32_t
1476window_get_output_scale(struct window *window)
1477{
1478 struct window_output *window_output;
1479 struct window_output *window_output_tmp;
1480 int scale = 1;
1481
1482 wl_list_for_each_safe(window_output, window_output_tmp,
1483 &window->window_output_list, link) {
1484 if (window_output->output->scale > scale)
1485 scale = window_output->output->scale;
1486 }
1487
1488 return scale;
1489}
1490
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001491static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001492
Pekka Paalanen4e373742013-02-13 16:17:13 +02001493static void
1494surface_destroy(struct surface *surface)
1495{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001496 if (surface->frame_cb)
1497 wl_callback_destroy(surface->frame_cb);
1498
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001499 if (surface->input_region)
1500 wl_region_destroy(surface->input_region);
1501
1502 if (surface->opaque_region)
1503 wl_region_destroy(surface->opaque_region);
1504
Pekka Paalanen35e82632013-04-25 13:57:48 +03001505 if (surface->subsurface)
1506 wl_subsurface_destroy(surface->subsurface);
1507
Pekka Paalanen4e373742013-02-13 16:17:13 +02001508 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001509
1510 if (surface->toysurface)
1511 surface->toysurface->destroy(surface->toysurface);
1512
Pekka Paalanen35e82632013-04-25 13:57:48 +03001513 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001514 free(surface);
1515}
1516
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001517void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001518window_destroy(struct window *window)
1519{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001520 struct display *display = window->display;
1521 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001522 struct window_output *window_output;
1523 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001524
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001525 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001526
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001527 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001528 if (input->touch_focus == window)
1529 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001530 if (input->pointer_focus == window)
1531 input->pointer_focus = NULL;
1532 if (input->keyboard_focus == window)
1533 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001534 if (input->focus_widget &&
1535 input->focus_widget->window == window)
1536 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001537 }
1538
Rob Bradford7507b572012-05-15 17:55:34 +01001539 wl_list_for_each_safe(window_output, window_output_tmp,
1540 &window->window_output_list, link) {
1541 free (window_output);
1542 }
1543
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001544 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001545 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001546
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001547 if (window->xdg_surface)
1548 xdg_surface_destroy(window->xdg_surface);
1549 if (window->xdg_popup)
1550 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001551
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001552 if (window->ivi_surface)
1553 ivi_surface_destroy(window->ivi_surface);
1554
Pekka Paalanen4e373742013-02-13 16:17:13 +02001555 surface_destroy(window->main_surface);
1556
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001557 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001558
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001559 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001560 free(window);
1561}
1562
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001563static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001564widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001565{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001566 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001567
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001568 wl_list_for_each(child, &widget->child_list, link) {
1569 target = widget_find_widget(child, x, y);
1570 if (target)
1571 return target;
1572 }
1573
1574 if (widget->allocation.x <= x &&
1575 x < widget->allocation.x + widget->allocation.width &&
1576 widget->allocation.y <= y &&
1577 y < widget->allocation.y + widget->allocation.height) {
1578 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001579 }
1580
1581 return NULL;
1582}
1583
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001584static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001585window_find_widget(struct window *window, int32_t x, int32_t y)
1586{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001587 struct surface *surface;
1588 struct widget *widget;
1589
1590 wl_list_for_each(surface, &window->subsurface_list, link) {
1591 widget = widget_find_widget(surface->widget, x, y);
1592 if (widget)
1593 return widget;
1594 }
1595
1596 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001597}
1598
1599static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001600widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001601{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001602 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001603
Peter Huttererf3d62272013-08-08 11:57:05 +10001604 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001605 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001606 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001607 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001608 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001609 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001610 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001611 widget->tooltip = NULL;
1612 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001613 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001614 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001615
1616 return widget;
1617}
1618
1619struct widget *
1620window_add_widget(struct window *window, void *data)
1621{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001622 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001623
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001624 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001625 wl_list_init(&widget->link);
1626 window->main_surface->widget = widget;
1627
1628 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001629}
1630
1631struct widget *
1632widget_add_widget(struct widget *parent, void *data)
1633{
1634 struct widget *widget;
1635
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001636 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001637 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001638
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001639 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001640}
1641
1642void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001643widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001644{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001645 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001646 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001647 struct input *input;
1648
Pekka Paalanen35e82632013-04-25 13:57:48 +03001649 /* Destroy the sub-surface along with the root widget */
1650 if (surface->widget == widget && surface->subsurface)
1651 surface_destroy(widget->surface);
1652
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001653 if (widget->tooltip)
1654 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001655
Pekka Paalanene156fb62012-01-19 13:51:38 +02001656 wl_list_for_each(input, &display->input_list, link) {
1657 if (input->focus_widget == widget)
1658 input->focus_widget = NULL;
1659 }
1660
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001661 wl_list_remove(&widget->link);
1662 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663}
1664
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001665void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001666widget_set_default_cursor(struct widget *widget, int cursor)
1667{
1668 widget->default_cursor = cursor;
1669}
1670
1671void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001672widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001673{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001674 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001675}
1676
1677void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001678widget_set_size(struct widget *widget, int32_t width, int32_t height)
1679{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001680 widget->allocation.width = width;
1681 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001682}
1683
1684void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001685widget_set_allocation(struct widget *widget,
1686 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001687{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001688 widget->allocation.x = x;
1689 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001690 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001691}
1692
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001693void
1694widget_set_transparent(struct widget *widget, int transparent)
1695{
1696 widget->opaque = !transparent;
1697}
1698
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001699void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001700widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001701{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001702 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703}
1704
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001705static cairo_surface_t *
1706widget_get_cairo_surface(struct widget *widget)
1707{
1708 struct surface *surface = widget->surface;
1709 struct window *window = widget->window;
1710
Neil Roberts97b747c2013-12-19 16:17:12 +00001711 assert(widget->use_cairo);
1712
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001713 if (!surface->cairo_surface) {
1714 if (surface == window->main_surface)
1715 window_create_main_surface(window);
1716 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001717 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001718 }
1719
1720 return surface->cairo_surface;
1721}
1722
Alexander Larsson15901f02013-05-22 14:41:25 +02001723static void
1724widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1725{
1726 struct surface *surface = widget->surface;
1727 double angle;
1728 cairo_matrix_t m;
1729 enum wl_output_transform transform;
1730 int surface_width, surface_height;
1731 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001732 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001733
1734 surface_width = surface->allocation.width;
1735 surface_height = surface->allocation.height;
1736
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001737 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001738 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001739
Alexander Larsson15901f02013-05-22 14:41:25 +02001740 switch (transform) {
1741 case WL_OUTPUT_TRANSFORM_FLIPPED:
1742 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1743 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1744 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1745 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1746 break;
1747 default:
1748 cairo_matrix_init_identity(&m);
1749 break;
1750 }
1751
1752 switch (transform) {
1753 case WL_OUTPUT_TRANSFORM_NORMAL:
1754 default:
1755 angle = 0;
1756 translate_x = 0;
1757 translate_y = 0;
1758 break;
1759 case WL_OUTPUT_TRANSFORM_FLIPPED:
1760 angle = 0;
1761 translate_x = surface_width;
1762 translate_y = 0;
1763 break;
1764 case WL_OUTPUT_TRANSFORM_90:
1765 angle = M_PI_2;
1766 translate_x = surface_height;
1767 translate_y = 0;
1768 break;
1769 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1770 angle = M_PI_2;
1771 translate_x = surface_height;
1772 translate_y = surface_width;
1773 break;
1774 case WL_OUTPUT_TRANSFORM_180:
1775 angle = M_PI;
1776 translate_x = surface_width;
1777 translate_y = surface_height;
1778 break;
1779 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1780 angle = M_PI;
1781 translate_x = 0;
1782 translate_y = surface_height;
1783 break;
1784 case WL_OUTPUT_TRANSFORM_270:
1785 angle = M_PI + M_PI_2;
1786 translate_x = 0;
1787 translate_y = surface_width;
1788 break;
1789 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1790 angle = M_PI + M_PI_2;
1791 translate_x = 0;
1792 translate_y = 0;
1793 break;
1794 }
1795
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001796 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001797 cairo_translate(cr, translate_x, translate_y);
1798 cairo_rotate(cr, angle);
1799 cairo_transform(cr, &m);
1800}
1801
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001802cairo_t *
1803widget_cairo_create(struct widget *widget)
1804{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001805 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001806 cairo_surface_t *cairo_surface;
1807 cairo_t *cr;
1808
1809 cairo_surface = widget_get_cairo_surface(widget);
1810 cr = cairo_create(cairo_surface);
1811
Alexander Larsson15901f02013-05-22 14:41:25 +02001812 widget_cairo_update_transform(widget, cr);
1813
Pekka Paalanen35e82632013-04-25 13:57:48 +03001814 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1815
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001816 return cr;
1817}
1818
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001819struct wl_surface *
1820widget_get_wl_surface(struct widget *widget)
1821{
1822 return widget->surface->surface;
1823}
1824
Neil Roberts5e10a042013-09-09 00:40:17 +01001825struct wl_subsurface *
1826widget_get_wl_subsurface(struct widget *widget)
1827{
1828 return widget->surface->subsurface;
1829}
1830
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001831uint32_t
1832widget_get_last_time(struct widget *widget)
1833{
1834 return widget->surface->last_time;
1835}
1836
1837void
1838widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1839{
1840 struct wl_compositor *comp = widget->window->display->compositor;
1841 struct surface *surface = widget->surface;
1842
1843 if (!surface->input_region)
1844 surface->input_region = wl_compositor_create_region(comp);
1845
1846 if (rect) {
1847 wl_region_add(surface->input_region,
1848 rect->x, rect->y, rect->width, rect->height);
1849 }
1850}
1851
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001852void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001853widget_set_resize_handler(struct widget *widget,
1854 widget_resize_handler_t handler)
1855{
1856 widget->resize_handler = handler;
1857}
1858
1859void
1860widget_set_redraw_handler(struct widget *widget,
1861 widget_redraw_handler_t handler)
1862{
1863 widget->redraw_handler = handler;
1864}
1865
1866void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001867widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001868{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001869 widget->enter_handler = handler;
1870}
1871
1872void
1873widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1874{
1875 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001876}
1877
1878void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001879widget_set_motion_handler(struct widget *widget,
1880 widget_motion_handler_t handler)
1881{
1882 widget->motion_handler = handler;
1883}
1884
1885void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001886widget_set_button_handler(struct widget *widget,
1887 widget_button_handler_t handler)
1888{
1889 widget->button_handler = handler;
1890}
1891
1892void
Rusty Lynch041815a2013-08-08 21:20:38 -07001893widget_set_touch_up_handler(struct widget *widget,
1894 widget_touch_up_handler_t handler)
1895{
1896 widget->touch_up_handler = handler;
1897}
1898
1899void
1900widget_set_touch_down_handler(struct widget *widget,
1901 widget_touch_down_handler_t handler)
1902{
1903 widget->touch_down_handler = handler;
1904}
1905
1906void
1907widget_set_touch_motion_handler(struct widget *widget,
1908 widget_touch_motion_handler_t handler)
1909{
1910 widget->touch_motion_handler = handler;
1911}
1912
1913void
1914widget_set_touch_frame_handler(struct widget *widget,
1915 widget_touch_frame_handler_t handler)
1916{
1917 widget->touch_frame_handler = handler;
1918}
1919
1920void
1921widget_set_touch_cancel_handler(struct widget *widget,
1922 widget_touch_cancel_handler_t handler)
1923{
1924 widget->touch_cancel_handler = handler;
1925}
1926
1927void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001928widget_set_axis_handler(struct widget *widget,
1929 widget_axis_handler_t handler)
1930{
1931 widget->axis_handler = handler;
1932}
1933
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001934static void
1935window_schedule_redraw_task(struct window *window);
1936
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001937void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001938widget_schedule_redraw(struct widget *widget)
1939{
Pekka Paalanen71233882013-04-25 13:57:53 +03001940 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001941 widget->surface->redraw_needed = 1;
1942 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001943}
1944
Neil Roberts97b747c2013-12-19 16:17:12 +00001945void
1946widget_set_use_cairo(struct widget *widget,
1947 int use_cairo)
1948{
1949 widget->use_cairo = use_cairo;
1950}
1951
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001952cairo_surface_t *
1953window_get_surface(struct window *window)
1954{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001955 cairo_surface_t *cairo_surface;
1956
1957 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1958
1959 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001960}
1961
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001962struct wl_surface *
1963window_get_wl_surface(struct window *window)
1964{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001965 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001966}
1967
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001968static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001969tooltip_redraw_handler(struct widget *widget, void *data)
1970{
1971 cairo_t *cr;
1972 const int32_t r = 3;
1973 struct tooltip *tooltip = data;
1974 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001975
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001976 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001977 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001978 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1979 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1980 cairo_paint(cr);
1981
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001982 width = widget->allocation.width;
1983 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001984 rounded_rect(cr, 0, 0, width, height, r);
1985
1986 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1987 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1988 cairo_fill(cr);
1989
1990 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1991 cairo_move_to(cr, 10, 16);
1992 cairo_show_text(cr, tooltip->entry);
1993 cairo_destroy(cr);
1994}
1995
1996static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001997get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001998{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001999 cairo_t *cr;
2000 cairo_text_extents_t extents;
2001
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002002 /* Use the dummy_surface because tooltip's surface was not
2003 * created yet, and parent does not have a valid surface
2004 * outside repaint, either.
2005 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002006 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002007 cairo_text_extents(cr, tooltip->entry, &extents);
2008 cairo_destroy(cr);
2009
2010 return extents;
2011}
2012
2013static int
2014window_create_tooltip(struct tooltip *tooltip)
2015{
2016 struct widget *parent = tooltip->parent;
2017 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002018 const int offset_y = 27;
2019 const int margin = 3;
2020 cairo_text_extents_t extents;
2021
2022 if (tooltip->widget)
2023 return 0;
2024
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002025 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002026
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002027 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002028 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002029 widget_set_allocation(tooltip->widget,
2030 tooltip->x, tooltip->y + offset_y,
2031 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002032
2033 return 0;
2034}
2035
2036void
2037widget_destroy_tooltip(struct widget *parent)
2038{
2039 struct tooltip *tooltip = parent->tooltip;
2040
2041 parent->tooltip_count = 0;
2042 if (!tooltip)
2043 return;
2044
2045 if (tooltip->widget) {
2046 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002047 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002048 }
2049
2050 close(tooltip->tooltip_fd);
2051 free(tooltip->entry);
2052 free(tooltip);
2053 parent->tooltip = NULL;
2054}
2055
2056static void
2057tooltip_func(struct task *task, uint32_t events)
2058{
2059 struct tooltip *tooltip =
2060 container_of(task, struct tooltip, tooltip_task);
2061 uint64_t exp;
2062
Martin Olsson8df662a2012-07-08 03:03:47 +02002063 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2064 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002065 window_create_tooltip(tooltip);
2066}
2067
2068#define TOOLTIP_TIMEOUT 500
2069static int
2070tooltip_timer_reset(struct tooltip *tooltip)
2071{
2072 struct itimerspec its;
2073
2074 its.it_interval.tv_sec = 0;
2075 its.it_interval.tv_nsec = 0;
2076 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2077 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2078 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2079 fprintf(stderr, "could not set timerfd\n: %m");
2080 return -1;
2081 }
2082
2083 return 0;
2084}
2085
2086int
2087widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2088{
2089 struct tooltip *tooltip = parent->tooltip;
2090
2091 parent->tooltip_count++;
2092 if (tooltip) {
2093 tooltip->x = x;
2094 tooltip->y = y;
2095 tooltip_timer_reset(tooltip);
2096 return 0;
2097 }
2098
2099 /* the handler might be triggered too fast via input device motion, so
2100 * we need this check here to make sure tooltip is fully initialized */
2101 if (parent->tooltip_count > 1)
2102 return 0;
2103
2104 tooltip = malloc(sizeof *tooltip);
2105 if (!tooltip)
2106 return -1;
2107
2108 parent->tooltip = tooltip;
2109 tooltip->parent = parent;
2110 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002111 tooltip->x = x;
2112 tooltip->y = y;
2113 tooltip->entry = strdup(entry);
2114 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2115 if (tooltip->tooltip_fd < 0) {
2116 fprintf(stderr, "could not create timerfd\n: %m");
2117 return -1;
2118 }
2119
2120 tooltip->tooltip_task.run = tooltip_func;
2121 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2122 EPOLLIN, &tooltip->tooltip_task);
2123 tooltip_timer_reset(tooltip);
2124
2125 return 0;
2126}
2127
2128static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002129workspace_manager_state(void *data,
2130 struct workspace_manager *workspace_manager,
2131 uint32_t current,
2132 uint32_t count)
2133{
2134 struct display *display = data;
2135
2136 display->workspace = current;
2137 display->workspace_count = count;
2138}
2139
2140static const struct workspace_manager_listener workspace_manager_listener = {
2141 workspace_manager_state
2142};
2143
2144static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002145frame_resize_handler(struct widget *widget,
2146 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002147{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002148 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002149 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002150 struct rectangle interior;
2151 struct rectangle input;
2152 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002153
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002154 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002155 interior.x = 0;
2156 interior.y = 0;
2157 interior.width = width;
2158 interior.height = height;
2159 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002160 frame_resize(frame->frame, width, height);
2161 frame_interior(frame->frame, &interior.x, &interior.y,
2162 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002163 }
2164
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002165 widget_set_allocation(child, interior.x, interior.y,
2166 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002167
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002168 if (child->resize_handler) {
2169 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002170 child->user_data);
2171
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002172 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002173 width = child->allocation.width;
2174 height = child->allocation.height;
2175 } else {
2176 frame_resize_inside(frame->frame,
2177 child->allocation.width,
2178 child->allocation.height);
2179 width = frame_width(frame->frame);
2180 height = frame_height(frame->frame);
2181 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002182 }
2183
Scott Moreauf7e498c2012-05-14 11:39:29 -06002184 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002185
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002186 widget->surface->input_region =
2187 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002188 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002189 frame_input_rect(frame->frame, &input.x, &input.y,
2190 &input.width, &input.height);
2191 wl_region_add(widget->surface->input_region,
2192 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002193 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002194 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002195 }
2196
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002198
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002199 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002200 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002201 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2202 &opaque.width, &opaque.height);
2203
2204 wl_region_add(widget->surface->opaque_region,
2205 opaque.x, opaque.y,
2206 opaque.width, opaque.height);
2207 } else {
2208 wl_region_add(widget->surface->opaque_region,
2209 0, 0, width, height);
2210 }
Martin Minarik1998b152012-05-10 02:04:35 +02002211 }
2212
Martin Minarik1998b152012-05-10 02:04:35 +02002213
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002214 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002215}
2216
2217static void
2218frame_redraw_handler(struct widget *widget, void *data)
2219{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002220 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002221 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222 struct window *window = widget->window;
2223
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002224 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002225 return;
2226
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002227 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002228
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002229 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002230
2231 cairo_destroy(cr);
2232}
2233
2234static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002235frame_get_pointer_image_for_location(struct window_frame *frame,
2236 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002237{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002238 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002239
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002240 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002241 return CURSOR_LEFT_PTR;
2242
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002243 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002244 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002245 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002246 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002247 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002248 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002249 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002250 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002251 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002252 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002253 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002254 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002255 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002257 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002258 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002259 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002260 case THEME_LOCATION_EXTERIOR:
2261 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002263 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002264 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002265}
2266
Pekka Paalanen26237862014-09-10 15:10:30 +03002267static void
2268frame_menu_func(void *data, struct input *input, int index)
2269{
2270 struct window *window = data;
2271 struct display *display;
2272
2273 switch (index) {
2274 case 0: /* close */
2275 window_close(window);
2276 break;
2277 case 1: /* move to workspace above */
2278 display = window->display;
2279 if (display->workspace > 0)
2280 workspace_manager_move_surface(
2281 display->workspace_manager,
2282 window->main_surface->surface,
2283 display->workspace - 1);
2284 break;
2285 case 2: /* move to workspace below */
2286 display = window->display;
2287 if (display->workspace < display->workspace_count - 1)
2288 workspace_manager_move_surface(
2289 display->workspace_manager,
2290 window->main_surface->surface,
2291 display->workspace + 1);
2292 break;
2293 case 3: /* fullscreen */
2294 /* we don't have a way to get out of fullscreen for now */
2295 if (window->fullscreen_handler)
2296 window->fullscreen_handler(window, window->user_data);
2297 break;
2298 }
2299}
2300
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002301void
2302window_show_frame_menu(struct window *window,
2303 struct input *input, uint32_t time)
2304{
2305 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002306 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002307
Pekka Paalanen26237862014-09-10 15:10:30 +03002308 static const char *entries[] = {
2309 "Close",
2310 "Move to workspace above", "Move to workspace below",
2311 "Fullscreen"
2312 };
2313
2314 if (window->fullscreen_handler)
2315 count = ARRAY_LENGTH(entries);
2316 else
2317 count = ARRAY_LENGTH(entries) - 1;
2318
2319 input_get_position(input, &x, &y);
2320 window_show_menu(window->display, input, time, window,
2321 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002322}
2323
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002324static int
2325frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002326 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002327{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002328 struct window_frame *frame = data;
2329 enum theme_location location;
2330
2331 location = frame_pointer_enter(frame->frame, input, x, y);
2332 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2333 widget_schedule_redraw(frame->widget);
2334
2335 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002336}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002337
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002338static int
2339frame_motion_handler(struct widget *widget,
2340 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002341 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002342{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002343 struct window_frame *frame = data;
2344 enum theme_location location;
2345
2346 location = frame_pointer_motion(frame->frame, input, x, y);
2347 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2348 widget_schedule_redraw(frame->widget);
2349
2350 return frame_get_pointer_image_for_location(data, location);
2351}
2352
2353static void
2354frame_leave_handler(struct widget *widget,
2355 struct input *input, void *data)
2356{
2357 struct window_frame *frame = data;
2358
2359 frame_pointer_leave(frame->frame, input);
2360 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2361 widget_schedule_redraw(frame->widget);
2362}
2363
2364static void
2365frame_handle_status(struct window_frame *frame, struct input *input,
2366 uint32_t time, enum theme_location location)
2367{
2368 struct window *window = frame->widget->window;
2369 uint32_t status;
2370
2371 status = frame_status(frame->frame);
2372 if (status & FRAME_STATUS_REPAINT)
2373 widget_schedule_redraw(frame->widget);
2374
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002375 if (status & FRAME_STATUS_MINIMIZE) {
2376 window_set_minimized(window);
2377 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2378 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002379
2380 if (status & FRAME_STATUS_MENU) {
2381 window_show_frame_menu(window, input, time);
2382 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2383 }
2384
2385 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002386 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002387 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2388 }
2389
2390 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002391 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002392 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002393 }
2394
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002395 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002396 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002397 xdg_surface_move(window->xdg_surface,
2398 input_get_seat(input),
2399 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002400
2401 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2402 }
2403
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002404 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002405 input_ungrab(input);
2406
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002407 xdg_surface_resize(window->xdg_surface,
2408 input_get_seat(input),
2409 window->display->serial,
2410 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002411
2412 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2413 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002414}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002415
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002416#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002417static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002418frame_button_handler(struct widget *widget,
2419 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002420 uint32_t button, enum wl_pointer_button_state state,
2421 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002422
2423{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002424 struct window_frame *frame = data;
2425 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002426
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002427 frame->double_click = 0;
2428 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2429 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2430 frame->double_click = 1;
2431 frame->did_double = 1;
2432 } else
2433 frame->did_double = 0;
2434
2435 frame->last_time = time;
2436 } else if (frame->did_double == 1) {
2437 frame->double_click = 1;
2438 frame->did_double = 0;
2439 }
2440
2441 if (frame->double_click)
2442 location = frame_double_click(frame->frame, input,
2443 button, state);
2444 else
2445 location = frame_pointer_button(frame->frame, input,
2446 button, state);
2447
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002448 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002449}
2450
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002451static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002452frame_touch_down_handler(struct widget *widget, struct input *input,
2453 uint32_t serial, uint32_t time, int32_t id,
2454 float x, float y, void *data)
2455{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002456 struct window_frame *frame = data;
2457
Xiong Zhang382de462014-06-12 11:06:26 +08002458 frame->double_click = 0;
2459 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2460 frame->last_id == id) {
2461 frame->double_click = 1;
2462 frame->did_double = 1;
2463 frame->double_id = id;
2464 } else
2465 frame->did_double = 0;
2466
2467 frame->last_time = time;
2468 frame->last_id = id;
2469
2470 if (frame->double_click)
2471 frame_double_touch_down(frame->frame, input, id, x, y);
2472 else
2473 frame_touch_down(frame->frame, input, id, x, y);
2474
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002475 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2476}
2477
2478static void
2479frame_touch_up_handler(struct widget *widget,
2480 struct input *input, uint32_t serial, uint32_t time,
2481 int32_t id, void *data)
2482{
2483 struct window_frame *frame = data;
2484
Xiong Zhang382de462014-06-12 11:06:26 +08002485 if (frame->double_id == id && frame->did_double) {
2486 frame->did_double = 0;
2487 frame->double_id = 0;
2488 frame_double_touch_up(frame->frame, input, id);
2489 } else
2490 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002491 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002492}
2493
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002494struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002495window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002496{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002497 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002498 uint32_t buttons;
2499
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002500 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002501 buttons = FRAME_BUTTON_NONE;
2502 } else {
2503 buttons = FRAME_BUTTON_ALL;
2504 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002505
Peter Huttererf3d62272013-08-08 11:57:05 +10002506 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002507 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002508 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002509
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002510 frame->widget = window_add_widget(window, frame);
2511 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002512
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002513 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2514 widget_set_resize_handler(frame->widget, frame_resize_handler);
2515 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002516 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002517 widget_set_motion_handler(frame->widget, frame_motion_handler);
2518 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002519 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002520 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002521
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002522 window->frame = frame;
2523
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002524 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002525}
2526
Kristian Høgsberga1627922012-06-20 17:30:03 -04002527void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002528window_frame_set_child_size(struct widget *widget, int child_width,
2529 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002530{
2531 struct display *display = widget->window->display;
2532 struct theme *t = display->theme;
2533 int decoration_width, decoration_height;
2534 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002535 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002536
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002537 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002538 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002539 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002540 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002541
2542 width = child_width + decoration_width;
2543 height = child_height + decoration_height;
2544 } else {
2545 width = child_width;
2546 height = child_height;
2547 }
2548
2549 window_schedule_resize(widget->window, width, height);
2550}
2551
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002552static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002553window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002554{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002555 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002556
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002557 /* frame->child must be destroyed by the application */
2558 widget_destroy(frame->widget);
2559 free(frame);
2560}
2561
2562static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002563input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002564 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002565{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002566 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002567 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002568
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002569 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002570 return;
2571
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002572 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002573 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002574 widget = old;
2575 if (input->grab)
2576 widget = input->grab;
2577 if (widget->leave_handler)
2578 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002579 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002580 }
2581
2582 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002583 widget = focus;
2584 if (input->grab)
2585 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002586 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002587 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002588 cursor = widget->enter_handler(focus, input, x, y,
2589 widget->user_data);
2590 else
2591 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002592
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002593 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002594 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002595}
2596
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002597void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002598touch_grab(struct input *input, int32_t touch_id)
2599{
2600 input->touch_grab = 1;
2601 input->touch_grab_id = touch_id;
2602}
2603
2604void
2605touch_ungrab(struct input *input)
2606{
2607 struct touch_point *tp, *tmp;
2608
2609 input->touch_grab = 0;
2610
2611 wl_list_for_each_safe(tp, tmp,
2612 &input->touch_point_list, link) {
2613 if (tp->id != input->touch_grab_id)
2614 continue;
2615 wl_list_remove(&tp->link);
2616 free(tp);
2617
2618 return;
2619 }
2620}
2621
2622void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002623input_grab(struct input *input, struct widget *widget, uint32_t button)
2624{
2625 input->grab = widget;
2626 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002627
2628 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002629}
2630
2631void
2632input_ungrab(struct input *input)
2633{
2634 struct widget *widget;
2635
2636 input->grab = NULL;
2637 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002638 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002639 input->sx, input->sy);
2640 input_set_focus_widget(input, widget, input->sx, input->sy);
2641 }
2642}
2643
2644static void
2645input_remove_pointer_focus(struct input *input)
2646{
2647 struct window *window = input->pointer_focus;
2648
2649 if (!window)
2650 return;
2651
2652 input_set_focus_widget(input, NULL, 0, 0);
2653
2654 input->pointer_focus = NULL;
2655 input->current_cursor = CURSOR_UNSET;
2656}
2657
2658static void
2659pointer_handle_enter(void *data, struct wl_pointer *pointer,
2660 uint32_t serial, struct wl_surface *surface,
2661 wl_fixed_t sx_w, wl_fixed_t sy_w)
2662{
2663 struct input *input = data;
2664 struct window *window;
2665 struct widget *widget;
2666 float sx = wl_fixed_to_double(sx_w);
2667 float sy = wl_fixed_to_double(sy_w);
2668
2669 if (!surface) {
2670 /* enter event for a window we've just destroyed */
2671 return;
2672 }
2673
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002674 window = wl_surface_get_user_data(surface);
2675 if (surface != window->main_surface->surface) {
2676 DBG("Ignoring input event from subsurface %p\n", surface);
2677 return;
2678 }
2679
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002680 input->display->serial = serial;
2681 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002682 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002683
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002684 input->sx = sx;
2685 input->sy = sy;
2686
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002687 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002688 input_set_focus_widget(input, widget, sx, sy);
2689}
2690
2691static void
2692pointer_handle_leave(void *data, struct wl_pointer *pointer,
2693 uint32_t serial, struct wl_surface *surface)
2694{
2695 struct input *input = data;
2696
2697 input->display->serial = serial;
2698 input_remove_pointer_focus(input);
2699}
2700
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002701static void
Daniel Stone37816df2012-05-16 18:45:18 +01002702pointer_handle_motion(void *data, struct wl_pointer *pointer,
2703 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002704{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002705 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002706 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002707 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002708 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002709 float sx = wl_fixed_to_double(sx_w);
2710 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002711
Paul Winwoodb22bf572013-08-29 10:52:54 +01002712 if (!window)
2713 return;
2714
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002715 input->sx = sx;
2716 input->sy = sy;
2717
Rob Bradford5f087742013-07-11 19:41:27 +01002718 /* when making the window smaller - e.g. after a unmaximise we might
2719 * still have a pending motion event that the compositor has picked
2720 * based on the old surface dimensions
2721 */
2722 if (sx > window->main_surface->allocation.width ||
2723 sy > window->main_surface->allocation.height)
2724 return;
2725
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002726 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002727 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002728 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002729 }
2730
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002731 if (input->grab)
2732 widget = input->grab;
2733 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002734 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002735 if (widget) {
2736 if (widget->motion_handler)
2737 cursor = widget->motion_handler(input->focus_widget,
2738 input, time, sx, sy,
2739 widget->user_data);
2740 else
2741 cursor = widget->default_cursor;
2742 } else
2743 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002744
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002745 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002746}
2747
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002748static void
Daniel Stone37816df2012-05-16 18:45:18 +01002749pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002750 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002751{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002752 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002753 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002754 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002755
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002756 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002757 if (input->focus_widget && input->grab == NULL &&
2758 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002759 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002760
Neil Roberts6b28aad2012-01-23 19:11:18 +00002761 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002762 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002763 (*widget->button_handler)(widget,
2764 input, time,
2765 button, state,
2766 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002767
Daniel Stone4dbadb12012-05-30 16:31:51 +01002768 if (input->grab && input->grab_button == button &&
2769 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002770 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002771}
2772
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002773static void
Daniel Stone37816df2012-05-16 18:45:18 +01002774pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002775 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002776{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002777 struct input *input = data;
2778 struct widget *widget;
2779
2780 widget = input->focus_widget;
2781 if (input->grab)
2782 widget = input->grab;
2783 if (widget && widget->axis_handler)
2784 (*widget->axis_handler)(widget,
2785 input, time,
2786 axis, value,
2787 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002788}
2789
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002790static const struct wl_pointer_listener pointer_listener = {
2791 pointer_handle_enter,
2792 pointer_handle_leave,
2793 pointer_handle_motion,
2794 pointer_handle_button,
2795 pointer_handle_axis,
2796};
2797
2798static void
2799input_remove_keyboard_focus(struct input *input)
2800{
2801 struct window *window = input->keyboard_focus;
2802 struct itimerspec its;
2803
2804 its.it_interval.tv_sec = 0;
2805 its.it_interval.tv_nsec = 0;
2806 its.it_value.tv_sec = 0;
2807 its.it_value.tv_nsec = 0;
2808 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2809
2810 if (!window)
2811 return;
2812
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002813 if (window->keyboard_focus_handler)
2814 (*window->keyboard_focus_handler)(window, NULL,
2815 window->user_data);
2816
2817 input->keyboard_focus = NULL;
2818}
2819
2820static void
2821keyboard_repeat_func(struct task *task, uint32_t events)
2822{
2823 struct input *input =
2824 container_of(task, struct input, repeat_task);
2825 struct window *window = input->keyboard_focus;
2826 uint64_t exp;
2827
2828 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2829 /* If we change the timer between the fd becoming
2830 * readable and getting here, there'll be nothing to
2831 * read and we get EAGAIN. */
2832 return;
2833
2834 if (window && window->key_handler) {
2835 (*window->key_handler)(window, input, input->repeat_time,
2836 input->repeat_key, input->repeat_sym,
2837 WL_KEYBOARD_KEY_STATE_PRESSED,
2838 window->user_data);
2839 }
2840}
2841
2842static void
2843keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2844 uint32_t format, int fd, uint32_t size)
2845{
2846 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002847 struct xkb_keymap *keymap;
2848 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002849 char *map_str;
2850
2851 if (!data) {
2852 close(fd);
2853 return;
2854 }
2855
2856 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2857 close(fd);
2858 return;
2859 }
2860
2861 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2862 if (map_str == MAP_FAILED) {
2863 close(fd);
2864 return;
2865 }
2866
Ran Benita2e1968f2014-08-19 23:59:51 +03002867 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2868 map_str,
2869 XKB_KEYMAP_FORMAT_TEXT_V1,
2870 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002871 munmap(map_str, size);
2872 close(fd);
2873
Rui Matos3eccb862013-10-10 19:44:22 +02002874 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002875 fprintf(stderr, "failed to compile keymap\n");
2876 return;
2877 }
2878
Rui Matos3eccb862013-10-10 19:44:22 +02002879 state = xkb_state_new(keymap);
2880 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002881 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002882 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002883 return;
2884 }
2885
Rui Matos3eccb862013-10-10 19:44:22 +02002886 xkb_keymap_unref(input->xkb.keymap);
2887 xkb_state_unref(input->xkb.state);
2888 input->xkb.keymap = keymap;
2889 input->xkb.state = state;
2890
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002891 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002892 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002893 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002894 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002895 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002896 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002897}
2898
2899static void
2900keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2901 uint32_t serial, struct wl_surface *surface,
2902 struct wl_array *keys)
2903{
2904 struct input *input = data;
2905 struct window *window;
2906
2907 input->display->serial = serial;
2908 input->keyboard_focus = wl_surface_get_user_data(surface);
2909
2910 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002911 if (window->keyboard_focus_handler)
2912 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002913 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002914}
2915
2916static void
2917keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2918 uint32_t serial, struct wl_surface *surface)
2919{
2920 struct input *input = data;
2921
2922 input->display->serial = serial;
2923 input_remove_keyboard_focus(input);
2924}
2925
Scott Moreau210d0792012-03-22 10:47:01 -06002926static void
Daniel Stone37816df2012-05-16 18:45:18 +01002927keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002928 uint32_t serial, uint32_t time, uint32_t key,
2929 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002930{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002931 struct input *input = data;
2932 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002933 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002934 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002935 const xkb_keysym_t *syms;
2936 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002937 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002938
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002939 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002940 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002941 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002942 return;
2943
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002944 /* We only use input grabs for pointer events for now, so just
2945 * ignore key presses if a grab is active. We expand the key
2946 * event delivery mechanism to route events to widgets to
2947 * properly handle key grabs. In the meantime, this prevents
2948 * key event devlivery while a grab is active. */
2949 if (input->grab && input->grab_button == 0)
2950 return;
2951
Ran Benita2e1968f2014-08-19 23:59:51 +03002952 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002953
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002954 sym = XKB_KEY_NoSymbol;
2955 if (num_syms == 1)
2956 sym = syms[0];
2957
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002958
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002959 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002960 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002961 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002962 } else if (sym == XKB_KEY_F11 &&
2963 window->fullscreen_handler &&
2964 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2965 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002966 } else if (sym == XKB_KEY_F4 &&
2967 input->modifiers == MOD_ALT_MASK &&
2968 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002969 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002970 } else if (window->key_handler) {
2971 (*window->key_handler)(window, input, time, key,
2972 sym, state, window->user_data);
2973 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002974
2975 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2976 key == input->repeat_key) {
2977 its.it_interval.tv_sec = 0;
2978 its.it_interval.tv_nsec = 0;
2979 its.it_value.tv_sec = 0;
2980 its.it_value.tv_nsec = 0;
2981 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002982 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2983 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002984 input->repeat_sym = sym;
2985 input->repeat_key = key;
2986 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002987 its.it_interval.tv_sec = input->repeat_rate_sec;
2988 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2989 its.it_value.tv_sec = input->repeat_delay_sec;
2990 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002991 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2992 }
2993}
2994
2995static void
Daniel Stone351eb612012-05-31 15:27:47 -04002996keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2997 uint32_t serial, uint32_t mods_depressed,
2998 uint32_t mods_latched, uint32_t mods_locked,
2999 uint32_t group)
3000{
3001 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003002 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003003
Matt Ropere61561f2013-06-24 16:52:43 +01003004 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3005 if (!input->xkb.keymap)
3006 return;
3007
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003008 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3009 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003010 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003011 XKB_STATE_MODS_DEPRESSED |
3012 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003013 input->modifiers = 0;
3014 if (mask & input->xkb.control_mask)
3015 input->modifiers |= MOD_CONTROL_MASK;
3016 if (mask & input->xkb.alt_mask)
3017 input->modifiers |= MOD_ALT_MASK;
3018 if (mask & input->xkb.shift_mask)
3019 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003020}
3021
Jonny Lamb06959082014-08-12 14:58:27 +02003022static void
3023set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3024{
3025 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3026 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3027
3028 /* a rate of zero disables any repeating, regardless of the delay's
3029 * value */
3030 if (rate == 0)
3031 return;
3032
3033 if (rate == 1)
3034 input->repeat_rate_sec = 1;
3035 else
3036 input->repeat_rate_nsec = 1000000000 / rate;
3037
3038 input->repeat_delay_sec = delay / 1000;
3039 delay -= (input->repeat_delay_sec * 1000);
3040 input->repeat_delay_nsec = delay * 1000 * 1000;
3041}
3042
3043static void
3044keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3045 int32_t rate, int32_t delay)
3046{
3047 struct input *input = data;
3048
3049 set_repeat_info(input, rate, delay);
3050}
3051
Daniel Stone37816df2012-05-16 18:45:18 +01003052static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003053 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003054 keyboard_handle_enter,
3055 keyboard_handle_leave,
3056 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003057 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003058 keyboard_handle_repeat_info
3059
Daniel Stone37816df2012-05-16 18:45:18 +01003060};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003061
3062static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003063touch_handle_down(void *data, struct wl_touch *wl_touch,
3064 uint32_t serial, uint32_t time, struct wl_surface *surface,
3065 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3066{
3067 struct input *input = data;
3068 struct widget *widget;
3069 float sx = wl_fixed_to_double(x_w);
3070 float sy = wl_fixed_to_double(y_w);
3071
Rusty Lynch1084da52013-08-15 09:10:08 -07003072 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003073 input->touch_focus = wl_surface_get_user_data(surface);
3074 if (!input->touch_focus) {
3075 DBG("Failed to find to touch focus for surface %p\n", surface);
3076 return;
3077 }
3078
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003079 if (surface != input->touch_focus->main_surface->surface) {
3080 DBG("Ignoring input event from subsurface %p\n", surface);
3081 input->touch_focus = NULL;
3082 return;
3083 }
3084
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003085 if (input->grab)
3086 widget = input->grab;
3087 else
3088 widget = window_find_widget(input->touch_focus,
3089 wl_fixed_to_double(x_w),
3090 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003091 if (widget) {
3092 struct touch_point *tp = xmalloc(sizeof *tp);
3093 if (tp) {
3094 tp->id = id;
3095 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003096 tp->x = sx;
3097 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003098 wl_list_insert(&input->touch_point_list, &tp->link);
3099
3100 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003101 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003102 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003103 sx, sy,
3104 widget->user_data);
3105 }
3106 }
3107}
3108
3109static void
3110touch_handle_up(void *data, struct wl_touch *wl_touch,
3111 uint32_t serial, uint32_t time, int32_t id)
3112{
3113 struct input *input = data;
3114 struct touch_point *tp, *tmp;
3115
Rusty Lynch041815a2013-08-08 21:20:38 -07003116 if (!input->touch_focus) {
3117 DBG("No touch focus found for touch up event!\n");
3118 return;
3119 }
3120
3121 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3122 if (tp->id != id)
3123 continue;
3124
3125 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003126 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003127 time, id,
3128 tp->widget->user_data);
3129
3130 wl_list_remove(&tp->link);
3131 free(tp);
3132
3133 return;
3134 }
3135}
3136
3137static void
3138touch_handle_motion(void *data, struct wl_touch *wl_touch,
3139 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3140{
3141 struct input *input = data;
3142 struct touch_point *tp;
3143 float sx = wl_fixed_to_double(x_w);
3144 float sy = wl_fixed_to_double(y_w);
3145
3146 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3147
3148 if (!input->touch_focus) {
3149 DBG("No touch focus found for touch motion event!\n");
3150 return;
3151 }
3152
3153 wl_list_for_each(tp, &input->touch_point_list, link) {
3154 if (tp->id != id)
3155 continue;
3156
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003157 tp->x = sx;
3158 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003159 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003160 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003161 id, sx, sy,
3162 tp->widget->user_data);
3163 return;
3164 }
3165}
3166
3167static void
3168touch_handle_frame(void *data, struct wl_touch *wl_touch)
3169{
3170 struct input *input = data;
3171 struct touch_point *tp, *tmp;
3172
3173 DBG("touch_handle_frame\n");
3174
3175 if (!input->touch_focus) {
3176 DBG("No touch focus found for touch frame event!\n");
3177 return;
3178 }
3179
3180 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3181 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003182 (*tp->widget->touch_frame_handler)(tp->widget, input,
3183 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003184 }
3185}
3186
3187static void
3188touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3189{
3190 struct input *input = data;
3191 struct touch_point *tp, *tmp;
3192
3193 DBG("touch_handle_cancel\n");
3194
3195 if (!input->touch_focus) {
3196 DBG("No touch focus found for touch cancel event!\n");
3197 return;
3198 }
3199
3200 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3201 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003202 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3203 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003204
3205 wl_list_remove(&tp->link);
3206 free(tp);
3207 }
3208}
3209
3210static const struct wl_touch_listener touch_listener = {
3211 touch_handle_down,
3212 touch_handle_up,
3213 touch_handle_motion,
3214 touch_handle_frame,
3215 touch_handle_cancel,
3216};
3217
3218static void
Daniel Stone37816df2012-05-16 18:45:18 +01003219seat_handle_capabilities(void *data, struct wl_seat *seat,
3220 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003221{
Daniel Stone37816df2012-05-16 18:45:18 +01003222 struct input *input = data;
3223
3224 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3225 input->pointer = wl_seat_get_pointer(seat);
3226 wl_pointer_set_user_data(input->pointer, input);
3227 wl_pointer_add_listener(input->pointer, &pointer_listener,
3228 input);
3229 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3230 wl_pointer_destroy(input->pointer);
3231 input->pointer = NULL;
3232 }
3233
3234 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3235 input->keyboard = wl_seat_get_keyboard(seat);
3236 wl_keyboard_set_user_data(input->keyboard, input);
3237 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3238 input);
3239 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3240 wl_keyboard_destroy(input->keyboard);
3241 input->keyboard = NULL;
3242 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003243
3244 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3245 input->touch = wl_seat_get_touch(seat);
3246 wl_touch_set_user_data(input->touch, input);
3247 wl_touch_add_listener(input->touch, &touch_listener, input);
3248 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3249 wl_touch_destroy(input->touch);
3250 input->touch = NULL;
3251 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003252}
3253
Rob Bradford08031182013-08-13 20:11:03 +01003254static void
3255seat_handle_name(void *data, struct wl_seat *seat,
3256 const char *name)
3257{
3258
3259}
3260
Daniel Stone37816df2012-05-16 18:45:18 +01003261static const struct wl_seat_listener seat_listener = {
3262 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003263 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003264};
3265
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003266void
3267input_get_position(struct input *input, int32_t *x, int32_t *y)
3268{
3269 *x = input->sx;
3270 *y = input->sy;
3271}
3272
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003273int
3274input_get_touch(struct input *input, int32_t id, float *x, float *y)
3275{
3276 struct touch_point *tp;
3277
3278 wl_list_for_each(tp, &input->touch_point_list, link) {
3279 if (tp->id != id)
3280 continue;
3281
3282 *x = tp->x;
3283 *y = tp->y;
3284 return 0;
3285 }
3286
3287 return -1;
3288}
3289
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003290struct display *
3291input_get_display(struct input *input)
3292{
3293 return input->display;
3294}
3295
Daniel Stone37816df2012-05-16 18:45:18 +01003296struct wl_seat *
3297input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003298{
Daniel Stone37816df2012-05-16 18:45:18 +01003299 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003300}
3301
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003302uint32_t
3303input_get_modifiers(struct input *input)
3304{
3305 return input->modifiers;
3306}
3307
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003308struct widget *
3309input_get_focus_widget(struct input *input)
3310{
3311 return input->focus_widget;
3312}
3313
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003314struct data_offer {
3315 struct wl_data_offer *offer;
3316 struct input *input;
3317 struct wl_array types;
3318 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003320 struct task io_task;
3321 int fd;
3322 data_func_t func;
3323 int32_t x, y;
3324 void *user_data;
3325};
3326
3327static void
3328data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3329{
3330 struct data_offer *offer = data;
3331 char **p;
3332
3333 p = wl_array_add(&offer->types, sizeof *p);
3334 *p = strdup(type);
3335}
3336
3337static const struct wl_data_offer_listener data_offer_listener = {
3338 data_offer_offer,
3339};
3340
3341static void
3342data_offer_destroy(struct data_offer *offer)
3343{
3344 char **p;
3345
3346 offer->refcount--;
3347 if (offer->refcount == 0) {
3348 wl_data_offer_destroy(offer->offer);
3349 for (p = offer->types.data; *p; p++)
3350 free(*p);
3351 wl_array_release(&offer->types);
3352 free(offer);
3353 }
3354}
3355
3356static void
3357data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003358 struct wl_data_device *data_device,
3359 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003360{
3361 struct data_offer *offer;
3362
Brian Lovinbc919262013-08-07 15:34:59 -07003363 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003364
3365 wl_array_init(&offer->types);
3366 offer->refcount = 1;
3367 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003368 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003369 wl_data_offer_add_listener(offer->offer,
3370 &data_offer_listener, offer);
3371}
3372
3373static void
3374data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003375 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003376 wl_fixed_t x_w, wl_fixed_t y_w,
3377 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003378{
3379 struct input *input = data;
3380 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003381 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003382 float x = wl_fixed_to_double(x_w);
3383 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003384 char **p;
3385
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003386 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003387 input->drag_enter_serial = serial;
3388 input->drag_focus = window,
3389 input->drag_x = x;
3390 input->drag_y = y;
3391
3392 if (!input->touch_grab)
3393 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003394
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003395 if (offer) {
3396 input->drag_offer = wl_data_offer_get_user_data(offer);
3397
3398 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3399 *p = NULL;
3400
3401 types_data = input->drag_offer->types.data;
3402 } else {
3403 input->drag_offer = NULL;
3404 types_data = NULL;
3405 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003406
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003407 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003408 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003409 window->user_data);
3410}
3411
3412static void
3413data_device_leave(void *data, struct wl_data_device *data_device)
3414{
3415 struct input *input = data;
3416
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003417 if (input->drag_offer) {
3418 data_offer_destroy(input->drag_offer);
3419 input->drag_offer = NULL;
3420 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003421}
3422
3423static void
3424data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003425 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003426{
3427 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003428 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003429 float x = wl_fixed_to_double(x_w);
3430 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003431 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003432
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003433 input->drag_x = x;
3434 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003435
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003436 if (input->drag_offer)
3437 types_data = input->drag_offer->types.data;
3438 else
3439 types_data = NULL;
3440
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003441 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003442 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003443 window->user_data);
3444}
3445
3446static void
3447data_device_drop(void *data, struct wl_data_device *data_device)
3448{
3449 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003450 struct window *window = input->drag_focus;
3451 float x, y;
3452
3453 x = input->drag_x;
3454 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003455
3456 if (window->drop_handler)
3457 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003458 x, y, window->user_data);
3459
3460 if (input->touch_grab)
3461 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003462}
3463
3464static void
3465data_device_selection(void *data,
3466 struct wl_data_device *wl_data_device,
3467 struct wl_data_offer *offer)
3468{
3469 struct input *input = data;
3470 char **p;
3471
3472 if (input->selection_offer)
3473 data_offer_destroy(input->selection_offer);
3474
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003475 if (offer) {
3476 input->selection_offer = wl_data_offer_get_user_data(offer);
3477 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3478 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003479 } else {
3480 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003481 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003482}
3483
3484static const struct wl_data_device_listener data_device_listener = {
3485 data_device_data_offer,
3486 data_device_enter,
3487 data_device_leave,
3488 data_device_motion,
3489 data_device_drop,
3490 data_device_selection
3491};
3492
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003493static void
3494input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003495{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003496 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003497 struct wl_cursor *cursor;
3498 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003499
Daniel Stone80972742012-11-07 17:51:39 +11003500 if (!input->pointer)
3501 return;
3502
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003503 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003504 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003505 return;
3506
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003507 if (index >= (int) cursor->image_count) {
3508 fprintf(stderr, "cursor index out of range\n");
3509 return;
3510 }
3511
3512 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003513 buffer = wl_cursor_image_get_buffer(image);
3514 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003515 return;
3516
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003517 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3518 wl_surface_damage(input->pointer_surface, 0, 0,
3519 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003520 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003521 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3522 input->pointer_surface,
3523 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003524}
3525
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003526static const struct wl_callback_listener pointer_surface_listener;
3527
Derek Foreman493d9792015-03-04 16:26:25 -06003528static bool
3529input_set_pointer_special(struct input *input)
3530{
3531 if (input->current_cursor == CURSOR_BLANK) {
3532 wl_pointer_set_cursor(input->pointer,
3533 input->pointer_enter_serial,
3534 NULL, 0, 0);
3535 return true;
3536 }
3537
3538 if (input->current_cursor == CURSOR_UNSET)
3539 return true;
3540
3541 return false;
3542}
3543
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003544static void
3545pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3546 uint32_t time)
3547{
3548 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003549 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003550 int i;
3551
3552 if (callback) {
3553 assert(callback == input->cursor_frame_cb);
3554 wl_callback_destroy(callback);
3555 input->cursor_frame_cb = NULL;
3556 }
3557
Daniel Stone80972742012-11-07 17:51:39 +11003558 if (!input->pointer)
3559 return;
3560
Derek Foreman493d9792015-03-04 16:26:25 -06003561 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003562 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003563
Daniel Stonea494f1d2012-06-18 19:31:12 +01003564 cursor = input->display->cursors[input->current_cursor];
3565 if (!cursor)
3566 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003567
3568 /* FIXME We don't have the current time on the first call so we set
3569 * the animation start to the time of the first frame callback. */
3570 if (time == 0)
3571 input->cursor_anim_start = 0;
3572 else if (input->cursor_anim_start == 0)
3573 input->cursor_anim_start = time;
3574
3575 if (time == 0 || input->cursor_anim_start == 0)
3576 i = 0;
3577 else
3578 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3579
Pekka Paalanenbc106382012-10-10 12:49:31 +03003580 if (cursor->image_count > 1) {
3581 input->cursor_frame_cb =
3582 wl_surface_frame(input->pointer_surface);
3583 wl_callback_add_listener(input->cursor_frame_cb,
3584 &pointer_surface_listener, input);
3585 }
3586
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003587 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003588}
3589
3590static const struct wl_callback_listener pointer_surface_listener = {
3591 pointer_surface_frame_callback
3592};
3593
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003594void
3595input_set_pointer_image(struct input *input, int pointer)
3596{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003597 int force = 0;
3598
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003599 if (!input->pointer)
3600 return;
3601
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003602 if (input->pointer_enter_serial > input->cursor_serial)
3603 force = 1;
3604
3605 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003606 return;
3607
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003608 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003609 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003610 if (!input->cursor_frame_cb)
3611 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003612 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003613 /* The current frame callback may be stuck if, for instance,
3614 * the set cursor request was processed by the server after
3615 * this client lost the focus. In this case the cursor surface
3616 * might not be mapped and the frame callback wouldn't ever
3617 * complete. Send a set_cursor and attach to try to map the
3618 * cursor surface again so that the callback will finish */
3619 input_set_pointer_image_index(input, 0);
3620 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003621}
3622
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003623struct wl_data_device *
3624input_get_data_device(struct input *input)
3625{
3626 return input->data_device;
3627}
3628
3629void
3630input_set_selection(struct input *input,
3631 struct wl_data_source *source, uint32_t time)
3632{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003633 if (input->data_device)
3634 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003635}
3636
3637void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003638input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003639{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003640 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003641 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003642}
3643
3644static void
3645offer_io_func(struct task *task, uint32_t events)
3646{
3647 struct data_offer *offer =
3648 container_of(task, struct data_offer, io_task);
3649 unsigned int len;
3650 char buffer[4096];
3651
3652 len = read(offer->fd, buffer, sizeof buffer);
3653 offer->func(buffer, len,
3654 offer->x, offer->y, offer->user_data);
3655
3656 if (len == 0) {
3657 close(offer->fd);
3658 data_offer_destroy(offer);
3659 }
3660}
3661
3662static void
3663data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3664 data_func_t func, void *user_data)
3665{
3666 int p[2];
3667
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003668 if (pipe2(p, O_CLOEXEC) == -1)
3669 return;
3670
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003671 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3672 close(p[1]);
3673
3674 offer->io_task.run = offer_io_func;
3675 offer->fd = p[0];
3676 offer->func = func;
3677 offer->refcount++;
3678 offer->user_data = user_data;
3679
3680 display_watch_fd(offer->input->display,
3681 offer->fd, EPOLLIN, &offer->io_task);
3682}
3683
3684void
3685input_receive_drag_data(struct input *input, const char *mime_type,
3686 data_func_t func, void *data)
3687{
3688 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003689 input->drag_offer->x = input->drag_x;
3690 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003691}
3692
3693int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003694input_receive_drag_data_to_fd(struct input *input,
3695 const char *mime_type, int fd)
3696{
3697 if (input->drag_offer)
3698 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3699
3700 return 0;
3701}
3702
3703int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003704input_receive_selection_data(struct input *input, const char *mime_type,
3705 data_func_t func, void *data)
3706{
3707 char **p;
3708
3709 if (input->selection_offer == NULL)
3710 return -1;
3711
3712 for (p = input->selection_offer->types.data; *p; p++)
3713 if (strcmp(mime_type, *p) == 0)
3714 break;
3715
3716 if (*p == NULL)
3717 return -1;
3718
3719 data_offer_receive_data(input->selection_offer,
3720 mime_type, func, data);
3721 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003722}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003723
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003724int
3725input_receive_selection_data_to_fd(struct input *input,
3726 const char *mime_type, int fd)
3727{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003728 if (input->selection_offer)
3729 wl_data_offer_receive(input->selection_offer->offer,
3730 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003731
3732 return 0;
3733}
3734
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003735void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003736window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003737{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003738 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003739 return;
3740
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003741 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003742}
3743
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003744static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003745surface_set_synchronized(struct surface *surface)
3746{
3747 if (!surface->subsurface)
3748 return;
3749
3750 if (surface->synchronized)
3751 return;
3752
3753 wl_subsurface_set_sync(surface->subsurface);
3754 surface->synchronized = 1;
3755}
3756
3757static void
3758surface_set_synchronized_default(struct surface *surface)
3759{
3760 if (!surface->subsurface)
3761 return;
3762
3763 if (surface->synchronized == surface->synchronized_default)
3764 return;
3765
3766 if (surface->synchronized_default)
3767 wl_subsurface_set_sync(surface->subsurface);
3768 else
3769 wl_subsurface_set_desync(surface->subsurface);
3770
3771 surface->synchronized = surface->synchronized_default;
3772}
3773
3774static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003775surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003776{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003777 struct widget *widget = surface->widget;
3778 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003779
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003780 if (surface->input_region) {
3781 wl_region_destroy(surface->input_region);
3782 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003783 }
3784
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003785 if (surface->opaque_region)
3786 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003787
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003788 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003789
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003790 if (widget->resize_handler)
3791 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003792 widget->allocation.width,
3793 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003794 widget->user_data);
3795
Pekka Paalanen35e82632013-04-25 13:57:48 +03003796 if (surface->subsurface &&
3797 (surface->allocation.x != widget->allocation.x ||
3798 surface->allocation.y != widget->allocation.y)) {
3799 wl_subsurface_set_position(surface->subsurface,
3800 widget->allocation.x,
3801 widget->allocation.y);
3802 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003803 if (surface->allocation.width != widget->allocation.width ||
3804 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003805 window_schedule_redraw(widget->window);
3806 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003807 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003808
3809 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003810 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003811 widget->allocation.width,
3812 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003813}
3814
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003815static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003816hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3817{
3818 /*
3819 * This hack should be removed, when EGL respects
3820 * eglSwapInterval(0).
3821 *
3822 * If this window has sub-surfaces, especially a free-running
3823 * EGL-widget, we need to post the parent surface once with
3824 * all the old state to guarantee, that the EGL-widget will
3825 * receive its frame callback soon. Otherwise, a forced call
3826 * to eglSwapBuffers may end up blocking, waiting for a frame
3827 * event that will never come, because we will commit the parent
3828 * surface with all new state only after eglSwapBuffers returns.
3829 *
3830 * This assumes, that:
3831 * 1. When the EGL widget's resize hook is called, it pauses.
3832 * 2. When the EGL widget's redraw hook is called, it forces a
3833 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3834 * In a single threaded application condition 1 is a no-op.
3835 *
3836 * XXX: This should actually be after the surface_resize() calls,
3837 * but cannot, because then it would commit the incomplete state
3838 * accumulated from the widget resize hooks.
3839 */
3840 if (window->subsurface_list.next != &window->main_surface->link ||
3841 window->subsurface_list.prev != &window->main_surface->link)
3842 wl_surface_commit(window->main_surface->surface);
3843}
3844
3845static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003846window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003847{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003848 struct surface *surface;
3849
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003850 widget_set_allocation(window->main_surface->widget,
3851 window->pending_allocation.x,
3852 window->pending_allocation.y,
3853 window->pending_allocation.width,
3854 window->pending_allocation.height);
3855
3856 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003857
3858 /* The main surface is in the list, too. Main surface's
3859 * resize_handler is responsible for calling widget_set_allocation()
3860 * on all sub-surface root widgets, so they will be resized
3861 * properly.
3862 */
3863 wl_list_for_each(surface, &window->subsurface_list, link) {
3864 if (surface == window->main_surface)
3865 continue;
3866
3867 surface_set_synchronized(surface);
3868 surface_resize(surface);
3869 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003870
3871 if (!window->fullscreen && !window->maximized)
3872 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003873}
3874
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003875static void
3876idle_resize(struct window *window)
3877{
3878 window->resize_needed = 0;
3879 window->redraw_needed = 1;
3880
3881 DBG("from %dx%d to %dx%d\n",
3882 window->main_surface->server_allocation.width,
3883 window->main_surface->server_allocation.height,
3884 window->pending_allocation.width,
3885 window->pending_allocation.height);
3886
3887 hack_prevent_EGL_sub_surface_deadlock(window);
3888
3889 window_do_resize(window);
3890}
3891
3892static void
3893undo_resize(struct window *window)
3894{
3895 window->pending_allocation.width =
3896 window->main_surface->server_allocation.width;
3897 window->pending_allocation.height =
3898 window->main_surface->server_allocation.height;
3899
3900 DBG("back to %dx%d\n",
3901 window->main_surface->server_allocation.width,
3902 window->main_surface->server_allocation.height);
3903
3904 window_do_resize(window);
3905
3906 if (window->pending_allocation.width == 0 &&
3907 window->pending_allocation.height == 0) {
3908 fprintf(stderr, "Error: Could not draw a surface, "
3909 "most likely due to insufficient disk space in "
3910 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3911 exit(EXIT_FAILURE);
3912 }
3913}
3914
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003915void
3916window_schedule_resize(struct window *window, int width, int height)
3917{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003918 /* We should probably get these numbers from the theme. */
3919 const int min_width = 200, min_height = 200;
3920
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003921 window->pending_allocation.x = 0;
3922 window->pending_allocation.y = 0;
3923 window->pending_allocation.width = width;
3924 window->pending_allocation.height = height;
3925
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003926 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003927 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003928 window->min_allocation.width = min_width;
3929 else
3930 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003931 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003932 window->min_allocation.height = min_height;
3933 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003934 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003935 }
3936
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003937 if (window->pending_allocation.width < window->min_allocation.width)
3938 window->pending_allocation.width = window->min_allocation.width;
3939 if (window->pending_allocation.height < window->min_allocation.height)
3940 window->pending_allocation.height = window->min_allocation.height;
3941
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003942 window->resize_needed = 1;
3943 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003944}
3945
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003946void
3947widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3948{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003949 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003950}
3951
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003952static int
3953window_get_shadow_margin(struct window *window)
3954{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003955 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003956 return frame_get_shadow_margin(window->frame->frame);
3957 else
3958 return 0;
3959}
3960
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003961static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003962handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003963 int32_t width, int32_t height,
3964 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003965{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003966 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003967 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003968
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003969 window->maximized = 0;
3970 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003971 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003972 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003973
3974 wl_array_for_each(p, states) {
3975 uint32_t state = *p;
3976 switch (state) {
3977 case XDG_SURFACE_STATE_MAXIMIZED:
3978 window->maximized = 1;
3979 break;
3980 case XDG_SURFACE_STATE_FULLSCREEN:
3981 window->fullscreen = 1;
3982 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003983 case XDG_SURFACE_STATE_RESIZING:
3984 window->resizing = 1;
3985 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003986 case XDG_SURFACE_STATE_ACTIVATED:
3987 window->focused = 1;
3988 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003989 default:
3990 /* Unknown state */
3991 break;
3992 }
3993 }
3994
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003995 if (window->frame) {
3996 if (window->maximized) {
3997 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3998 } else {
3999 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4000 }
4001
4002 if (window->focused) {
4003 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4004 } else {
4005 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4006 }
4007 }
4008
Jasper St. Pierref184c382014-05-06 08:33:27 -04004009 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004010 /* The width / height params are for window geometry,
4011 * but window_schedule_resize takes allocation. Add
4012 * on the shadow margin to get the difference. */
4013 int margin = window_get_shadow_margin(window);
4014
4015 window_schedule_resize(window,
4016 width + margin * 2,
4017 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004018 } else {
4019 window_schedule_resize(window,
4020 window->saved_allocation.width,
4021 window->saved_allocation.height);
4022 }
4023
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004024 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004025
4026 if (window->state_changed_handler)
4027 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004028}
4029
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004030static void
4031handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4032{
4033 struct window *window = data;
4034 window_close(window);
4035}
4036
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004037static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004038 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004039 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004040};
4041
4042static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004043window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004044{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004045 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004046
4047 if (!window->xdg_surface)
4048 return;
4049
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004050 if (window->parent == window->last_parent)
4051 return;
4052
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004053 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004054 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004055 else
4056 parent_surface = NULL;
4057
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004058 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004059 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004060}
4061
4062static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004063window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004064{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004065 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004066 frame_input_rect(window->frame->frame,
4067 &geometry->x,
4068 &geometry->y,
4069 &geometry->width,
4070 &geometry->height);
4071 else
4072 window_get_allocation(window, geometry);
4073}
4074
4075static void
4076window_sync_geometry(struct window *window)
4077{
4078 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004079
4080 if (!window->xdg_surface)
4081 return;
4082
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004083 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004084 if (geometry.x == window->last_geometry.x &&
4085 geometry.y == window->last_geometry.y &&
4086 geometry.width == window->last_geometry.width &&
4087 geometry.height == window->last_geometry.height)
4088 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004089
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004090 xdg_surface_set_window_geometry(window->xdg_surface,
4091 geometry.x,
4092 geometry.y,
4093 geometry.width,
4094 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004095 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004096}
4097
4098static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004099window_flush(struct window *window)
4100{
4101 struct surface *surface;
4102
4103 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004104 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004105 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004106 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004107 }
4108 }
4109
4110 wl_list_for_each(surface, &window->subsurface_list, link) {
4111 if (surface == window->main_surface)
4112 continue;
4113
4114 surface_flush(surface);
4115 }
4116
4117 surface_flush(window->main_surface);
4118}
4119
4120static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004121menu_destroy(struct menu *menu)
4122{
4123 widget_destroy(menu->widget);
4124 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004125 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004126 free(menu);
4127}
4128
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004129void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004130window_get_allocation(struct window *window,
4131 struct rectangle *allocation)
4132{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004133 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004134}
4135
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004136static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004137widget_redraw(struct widget *widget)
4138{
4139 struct widget *child;
4140
4141 if (widget->redraw_handler)
4142 widget->redraw_handler(widget, widget->user_data);
4143 wl_list_for_each(child, &widget->child_list, link)
4144 widget_redraw(child);
4145}
4146
4147static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004148frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4149{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004150 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004151
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004152 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004153 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004154 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004155 surface->frame_cb = NULL;
4156
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004157 surface->last_time = time;
4158
Pekka Paalanen71233882013-04-25 13:57:53 +03004159 if (surface->redraw_needed || surface->window->redraw_needed) {
4160 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004161 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004162 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004163}
4164
4165static const struct wl_callback_listener listener = {
4166 frame_callback
4167};
4168
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004169static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004170surface_redraw(struct surface *surface)
4171{
Pekka Paalanen71233882013-04-25 13:57:53 +03004172 DBG_OBJ(surface->surface, "begin\n");
4173
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004174 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004175 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004176
4177 /* Whole-window redraw forces a redraw even if the previous has
4178 * not yet hit the screen.
4179 */
4180 if (surface->frame_cb) {
4181 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004182 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004183
Pekka Paalanen71233882013-04-25 13:57:53 +03004184 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004185 wl_callback_destroy(surface->frame_cb);
4186 }
4187
Neil Roberts97b747c2013-12-19 16:17:12 +00004188 if (surface->widget->use_cairo &&
4189 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004190 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4191 return -1;
4192 }
4193
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004194 surface->frame_cb = wl_surface_frame(surface->surface);
4195 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004196 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004197
4198 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004199 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004200 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004201 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004202 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004203}
4204
4205static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004206idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004207{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004208 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004209 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004210 int failed = 0;
4211 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004212
Pekka Paalanen71233882013-04-25 13:57:53 +03004213 DBG(" --------- \n");
4214
Pekka Paalaneneebff542013-04-25 13:57:52 +03004215 wl_list_init(&window->redraw_task.link);
4216 window->redraw_task_scheduled = 0;
4217
4218 if (window->resize_needed) {
4219 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004220 if (window->main_surface->frame_cb) {
4221 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004222 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004223 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004224
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004225 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004226 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004227 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004228
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004229 if (surface_redraw(window->main_surface) < 0) {
4230 /*
4231 * Only main_surface failure will cause us to undo the resize.
4232 * If sub-surfaces fail, they will just be broken with old
4233 * content.
4234 */
4235 failed = 1;
4236 } else {
4237 wl_list_for_each(surface, &window->subsurface_list, link) {
4238 if (surface == window->main_surface)
4239 continue;
4240
4241 surface_redraw(surface);
4242 }
4243 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004244
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004245 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004246 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004247
4248 wl_list_for_each(surface, &window->subsurface_list, link)
4249 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004250
4251 if (resized && failed) {
4252 /* Restore widget tree to correspond to what is on screen. */
4253 undo_resize(window);
4254 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004255}
4256
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004257static void
4258window_schedule_redraw_task(struct window *window)
4259{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004260 if (!window->redraw_task_scheduled) {
4261 window->redraw_task.run = idle_redraw;
4262 display_defer(window->display, &window->redraw_task);
4263 window->redraw_task_scheduled = 1;
4264 }
4265}
4266
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004267void
4268window_schedule_redraw(struct window *window)
4269{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004270 struct surface *surface;
4271
Pekka Paalanen71233882013-04-25 13:57:53 +03004272 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4273
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004274 wl_list_for_each(surface, &window->subsurface_list, link)
4275 surface->redraw_needed = 1;
4276
4277 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004278}
4279
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004280int
4281window_is_fullscreen(struct window *window)
4282{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004283 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004284}
4285
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004286void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004287window_set_fullscreen(struct window *window, int fullscreen)
4288{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004289 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004290 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004291
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004292 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004293 return;
4294
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004295 if (fullscreen)
4296 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4297 else
4298 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004299}
4300
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004301int
4302window_is_maximized(struct window *window)
4303{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004304 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004305}
4306
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004307void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004308window_set_maximized(struct window *window, int maximized)
4309{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004310 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004311 return;
4312
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004313 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004314 return;
4315
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004316 if (maximized)
4317 xdg_surface_set_maximized(window->xdg_surface);
4318 else
4319 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004320}
4321
Jasper St. Pierrede680992014-04-10 17:23:49 -07004322int
4323window_is_resizing(struct window *window)
4324{
4325 return window->resizing;
4326}
4327
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004328void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004329window_set_minimized(struct window *window)
4330{
4331 if (!window->xdg_surface)
4332 return;
4333
4334 xdg_surface_set_minimized(window->xdg_surface);
4335}
4336
4337void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004338window_set_user_data(struct window *window, void *data)
4339{
4340 window->user_data = data;
4341}
4342
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004343void *
4344window_get_user_data(struct window *window)
4345{
4346 return window->user_data;
4347}
4348
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004349void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004350window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004351 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004352{
4353 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004354}
4355
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004356void
4357window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004358 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004359{
4360 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004361}
4362
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004363void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004364window_set_data_handler(struct window *window, window_data_handler_t handler)
4365{
4366 window->data_handler = handler;
4367}
4368
4369void
4370window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4371{
4372 window->drop_handler = handler;
4373}
4374
4375void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004376window_set_close_handler(struct window *window,
4377 window_close_handler_t handler)
4378{
4379 window->close_handler = handler;
4380}
4381
4382void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004383window_set_fullscreen_handler(struct window *window,
4384 window_fullscreen_handler_t handler)
4385{
4386 window->fullscreen_handler = handler;
4387}
4388
4389void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004390window_set_output_handler(struct window *window,
4391 window_output_handler_t handler)
4392{
4393 window->output_handler = handler;
4394}
4395
4396void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004397window_set_state_changed_handler(struct window *window,
4398 window_state_changed_handler_t handler)
4399{
4400 window->state_changed_handler = handler;
4401}
4402
4403void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004404window_set_title(struct window *window, const char *title)
4405{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004406 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004407 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004408 if (window->frame) {
4409 frame_set_title(window->frame->frame, title);
4410 widget_schedule_redraw(window->frame->widget);
4411 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004412 if (window->xdg_surface)
4413 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004414}
4415
4416const char *
4417window_get_title(struct window *window)
4418{
4419 return window->title;
4420}
4421
4422void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004423window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4424{
4425 struct text_cursor_position *text_cursor_position =
4426 window->display->text_cursor_position;
4427
Scott Moreau9295ce02012-06-01 12:46:10 -06004428 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004429 return;
4430
4431 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004432 window->main_surface->surface,
4433 wl_fixed_from_int(x),
4434 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004435}
4436
4437void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004438window_damage(struct window *window, int32_t x, int32_t y,
4439 int32_t width, int32_t height)
4440{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004441 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004442}
4443
Casey Dahlin9074db52012-04-19 22:50:09 -04004444static void
4445surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004446 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004447{
Rob Bradford7507b572012-05-15 17:55:34 +01004448 struct window *window = data;
4449 struct output *output;
4450 struct output *output_found = NULL;
4451 struct window_output *window_output;
4452
4453 wl_list_for_each(output, &window->display->output_list, link) {
4454 if (output->output == wl_output) {
4455 output_found = output;
4456 break;
4457 }
4458 }
4459
4460 if (!output_found)
4461 return;
4462
Brian Lovinbc919262013-08-07 15:34:59 -07004463 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004464 window_output->output = output_found;
4465
4466 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004467
4468 if (window->output_handler)
4469 window->output_handler(window, output_found, 1,
4470 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004471}
4472
4473static void
4474surface_leave(void *data,
4475 struct wl_surface *wl_surface, struct wl_output *output)
4476{
Rob Bradford7507b572012-05-15 17:55:34 +01004477 struct window *window = data;
4478 struct window_output *window_output;
4479 struct window_output *window_output_found = NULL;
4480
4481 wl_list_for_each(window_output, &window->window_output_list, link) {
4482 if (window_output->output->output == output) {
4483 window_output_found = window_output;
4484 break;
4485 }
4486 }
4487
4488 if (window_output_found) {
4489 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004490
4491 if (window->output_handler)
4492 window->output_handler(window, window_output->output,
4493 0, window->user_data);
4494
Rob Bradford7507b572012-05-15 17:55:34 +01004495 free(window_output_found);
4496 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004497}
4498
4499static const struct wl_surface_listener surface_listener = {
4500 surface_enter,
4501 surface_leave
4502};
4503
Pekka Paalanen4e373742013-02-13 16:17:13 +02004504static struct surface *
4505surface_create(struct window *window)
4506{
4507 struct display *display = window->display;
4508 struct surface *surface;
4509
Brian Lovinbc919262013-08-07 15:34:59 -07004510 surface = xmalloc(sizeof *surface);
4511 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004512 if (!surface)
4513 return NULL;
4514
4515 surface->window = window;
4516 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004517 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004518 wl_surface_add_listener(surface->surface, &surface_listener, window);
4519
Pekka Paalanen35e82632013-04-25 13:57:48 +03004520 wl_list_insert(&window->subsurface_list, &surface->link);
4521
Pekka Paalanen4e373742013-02-13 16:17:13 +02004522 return surface;
4523}
4524
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004525static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004526get_preferred_buffer_type(struct display *display)
4527{
4528#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004529 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004530 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4531#endif
4532
4533 return WINDOW_BUFFER_TYPE_SHM;
4534}
4535
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004536static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004537window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004538{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004539 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004540 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004541
Peter Huttererf3d62272013-08-08 11:57:05 +10004542 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004543 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004544 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004545
4546 surface = surface_create(window);
4547 window->main_surface = surface;
4548
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004549 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004550
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004551 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004552 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004553
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004554 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004555
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004556 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004557 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004558 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004559
Rob Bradford7507b572012-05-15 17:55:34 +01004560 wl_list_init (&window->window_output_list);
4561
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004562 return window;
4563}
4564
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004565struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004566window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004567{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004568 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004569 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004570
4571 window = window_create_internal(display, 0);
4572
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004573 if (window->display->xdg_shell) {
4574 window->xdg_surface =
4575 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4576 window->main_surface->surface);
4577 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004578
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004579 xdg_surface_set_user_data(window->xdg_surface, window);
4580 xdg_surface_add_listener(window->xdg_surface,
4581 &xdg_surface_listener, window);
4582 } else if (display->ivi_application) {
4583 /* auto generation of ivi_id based on process id + basement of id */
4584 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4585 window->ivi_surface =
4586 ivi_application_surface_create(display->ivi_application,
4587 id_ivisurf, window->main_surface->surface);
4588 fail_on_null(window->ivi_surface);
4589
4590 ivi_surface_add_listener(window->ivi_surface,
4591 &ivi_surface_listener, window);
4592 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004593
4594 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004595}
4596
4597struct window *
4598window_create_custom(struct display *display)
4599{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004600 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004601}
4602
Jasper St. Pierre53686042013-12-09 15:26:25 -05004603void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004604window_set_parent(struct window *window,
4605 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004606{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004607 window->parent = parent_window;
4608 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004609}
4610
4611struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004612window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004613{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004614 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004615}
4616
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004617static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004618menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004619{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004620 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004621 int next;
4622
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004623 frame_interior(menu->frame, &x, &y, &width, &height);
4624 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004625 if (menu->current != next) {
4626 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004627 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004628 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004629}
4630
4631static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004632menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004633 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004634 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004635{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004636 struct menu *menu = data;
4637
4638 if (widget == menu->widget)
4639 menu_set_item(data, y);
4640
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004641 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004642}
4643
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004644static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004645menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004646 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004647{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004648 struct menu *menu = data;
4649
4650 if (widget == menu->widget)
4651 menu_set_item(data, y);
4652
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004653 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004654}
4655
4656static void
4657menu_leave_handler(struct widget *widget, struct input *input, void *data)
4658{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004659 struct menu *menu = data;
4660
4661 if (widget == menu->widget)
4662 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004663}
4664
4665static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004666menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004667 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004668 uint32_t button, enum wl_pointer_button_state state,
4669 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004670
4671{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004672 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004673
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004674 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4675 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004676 /* Either relase after press-drag-release or
4677 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004678 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004679 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004680 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004681 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004682 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004683 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004684}
4685
4686static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004687menu_touch_up_handler(struct widget *widget,
4688 struct input *input,
4689 uint32_t serial,
4690 uint32_t time,
4691 int32_t id,
4692 void *data)
4693{
4694 struct menu *menu = data;
4695
4696 input_ungrab(input);
4697 menu_destroy(menu);
4698}
4699
4700static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004701menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004702{
4703 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004704 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004705 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004706
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004707 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004708
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004709 frame_repaint(menu->frame, cr);
4710 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004711
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004712 theme_set_background_source(menu->window->display->theme,
4713 cr, THEME_FRAME_ACTIVE);
4714 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004715 cairo_fill(cr);
4716
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004717 cairo_select_font_face(cr, "sans",
4718 CAIRO_FONT_SLANT_NORMAL,
4719 CAIRO_FONT_WEIGHT_NORMAL);
4720 cairo_set_font_size(cr, 12);
4721
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004722 for (i = 0; i < menu->count; i++) {
4723 if (i == menu->current) {
4724 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004725 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004726 cairo_fill(cr);
4727 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004728 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004729 cairo_show_text(cr, menu->entries[i]);
4730 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004731 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4732 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004733 cairo_show_text(cr, menu->entries[i]);
4734 }
4735 }
4736
4737 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004738}
4739
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004740static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004741handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004742{
4743 struct window *window = data;
4744 struct menu *menu = window->main_surface->widget->user_data;
4745
4746 input_ungrab(menu->input);
4747 menu_destroy(menu);
4748}
4749
4750static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004751 handle_popup_popup_done,
4752};
4753
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004754static struct menu *
4755create_menu(struct display *display,
4756 struct input *input, uint32_t time,
4757 menu_func_t func, const char **entries, int count,
4758 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004759{
4760 struct window *window;
4761 struct menu *menu;
4762
4763 menu = malloc(sizeof *menu);
4764 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004765 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004766
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004767 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004768 if (!window) {
4769 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004770 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004771 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004772
4773 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004774 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004775 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004776 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004777 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004778 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004779 menu->entries = entries;
4780 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004781 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004782 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004783 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004784 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004785 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004786
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004787 input_ungrab(input);
4788
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004789 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004790 widget_set_enter_handler(menu->widget, menu_enter_handler);
4791 widget_set_leave_handler(menu->widget, menu_leave_handler);
4792 widget_set_motion_handler(menu->widget, menu_motion_handler);
4793 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004794 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004795
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004796 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004797 frame_resize_inside(menu->frame, 200, count * 20);
4798 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4799 window_schedule_resize(window, frame_width(menu->frame),
4800 frame_height(menu->frame));
4801
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004802 return menu;
4803}
4804
4805struct window *
4806window_create_menu(struct display *display,
4807 struct input *input, uint32_t time,
4808 menu_func_t func, const char **entries, int count,
4809 void *user_data)
4810{
4811 struct menu *menu;
4812 menu = create_menu(display, input, time, func, entries, count, user_data);
4813
4814 if (menu == NULL)
4815 return NULL;
4816
4817 return menu->window;
4818}
4819
4820void
4821window_show_menu(struct display *display,
4822 struct input *input, uint32_t time, struct window *parent,
4823 int32_t x, int32_t y,
4824 menu_func_t func, const char **entries, int count)
4825{
4826 struct menu *menu;
4827 struct window *window;
4828 int32_t ix, iy;
4829
4830 menu = create_menu(display, input, time, func, entries, count, parent);
4831
4832 if (menu == NULL)
4833 return;
4834
4835 window = menu->window;
4836
4837 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4838 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4839
4840 window->x = x;
4841 window->y = y;
4842
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004843 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004844
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004845 if (!display->xdg_shell)
4846 return;
4847
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004848 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4849 window->main_surface->surface,
4850 parent->main_surface->surface,
4851 input->seat,
4852 display_get_serial(window->display),
4853 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004854 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004855 fail_on_null(window->xdg_popup);
4856
4857 xdg_popup_set_user_data(window->xdg_popup, window);
4858 xdg_popup_add_listener(window->xdg_popup,
4859 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004860}
4861
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004862void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004863window_set_buffer_type(struct window *window, enum window_buffer_type type)
4864{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004865 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004866}
4867
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004868enum window_buffer_type
4869window_get_buffer_type(struct window *window)
4870{
4871 return window->main_surface->buffer_type;
4872}
4873
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004874void
4875window_set_preferred_format(struct window *window,
4876 enum preferred_format format)
4877{
4878 window->preferred_format = format;
4879}
4880
Pekka Paalanen35e82632013-04-25 13:57:48 +03004881struct widget *
4882window_add_subsurface(struct window *window, void *data,
4883 enum subsurface_mode default_mode)
4884{
4885 struct widget *widget;
4886 struct surface *surface;
4887 struct wl_surface *parent;
4888 struct wl_subcompositor *subcompo = window->display->subcompositor;
4889
Pekka Paalanen35e82632013-04-25 13:57:48 +03004890 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004891 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004892 widget = widget_create(window, surface, data);
4893 wl_list_init(&widget->link);
4894 surface->widget = widget;
4895
4896 parent = window->main_surface->surface;
4897 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4898 surface->surface,
4899 parent);
4900 surface->synchronized = 1;
4901
4902 switch (default_mode) {
4903 case SUBSURFACE_SYNCHRONIZED:
4904 surface->synchronized_default = 1;
4905 break;
4906 case SUBSURFACE_DESYNCHRONIZED:
4907 surface->synchronized_default = 0;
4908 break;
4909 default:
4910 assert(!"bad enum subsurface_mode");
4911 }
4912
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004913 window->resize_needed = 1;
4914 window_schedule_redraw(window);
4915
Pekka Paalanen35e82632013-04-25 13:57:48 +03004916 return widget;
4917}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004918
4919static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004920display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004921 struct wl_output *wl_output,
4922 int x, int y,
4923 int physical_width,
4924 int physical_height,
4925 int subpixel,
4926 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004927 const char *model,
4928 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004929{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004930 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004931
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004932 output->allocation.x = x;
4933 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004934 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004935
4936 if (output->make)
4937 free(output->make);
4938 output->make = strdup(make);
4939
4940 if (output->model)
4941 free(output->model);
4942 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004943}
4944
4945static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004946display_handle_done(void *data,
4947 struct wl_output *wl_output)
4948{
4949}
4950
4951static void
4952display_handle_scale(void *data,
4953 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004954 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004955{
4956 struct output *output = data;
4957
4958 output->scale = scale;
4959}
4960
4961static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004962display_handle_mode(void *data,
4963 struct wl_output *wl_output,
4964 uint32_t flags,
4965 int width,
4966 int height,
4967 int refresh)
4968{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004969 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004970 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004971
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004972 if (flags & WL_OUTPUT_MODE_CURRENT) {
4973 output->allocation.width = width;
4974 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004975 if (display->output_configure_handler)
4976 (*display->output_configure_handler)(
4977 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004978 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004979}
4980
4981static const struct wl_output_listener output_listener = {
4982 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004983 display_handle_mode,
4984 display_handle_done,
4985 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004986};
4987
4988static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004989display_add_output(struct display *d, uint32_t id)
4990{
4991 struct output *output;
4992
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004993 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004994 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004995 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004996 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004997 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004998 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004999 wl_list_insert(d->output_list.prev, &output->link);
5000
5001 wl_output_add_listener(output->output, &output_listener, output);
5002}
5003
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005004static void
5005output_destroy(struct output *output)
5006{
5007 if (output->destroy_handler)
5008 (*output->destroy_handler)(output, output->user_data);
5009
5010 wl_output_destroy(output->output);
5011 wl_list_remove(&output->link);
5012 free(output);
5013}
5014
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005015static void
5016display_destroy_output(struct display *d, uint32_t id)
5017{
5018 struct output *output;
5019
5020 wl_list_for_each(output, &d->output_list, link) {
5021 if (output->server_output_id == id) {
5022 output_destroy(output);
5023 break;
5024 }
5025 }
5026}
5027
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005028void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005029display_set_global_handler(struct display *display,
5030 display_global_handler_t handler)
5031{
5032 struct global *global;
5033
5034 display->global_handler = handler;
5035 if (!handler)
5036 return;
5037
5038 wl_list_for_each(global, &display->global_list, link)
5039 display->global_handler(display,
5040 global->name, global->interface,
5041 global->version, display->user_data);
5042}
5043
5044void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005045display_set_global_handler_remove(struct display *display,
5046 display_global_handler_t remove_handler)
5047{
5048 display->global_handler_remove = remove_handler;
5049 if (!remove_handler)
5050 return;
5051}
5052
5053void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005054display_set_output_configure_handler(struct display *display,
5055 display_output_handler_t handler)
5056{
5057 struct output *output;
5058
5059 display->output_configure_handler = handler;
5060 if (!handler)
5061 return;
5062
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005063 wl_list_for_each(output, &display->output_list, link) {
5064 if (output->allocation.width == 0 &&
5065 output->allocation.height == 0)
5066 continue;
5067
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005068 (*display->output_configure_handler)(output,
5069 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005070 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005071}
5072
5073void
5074output_set_user_data(struct output *output, void *data)
5075{
5076 output->user_data = data;
5077}
5078
5079void *
5080output_get_user_data(struct output *output)
5081{
5082 return output->user_data;
5083}
5084
5085void
5086output_set_destroy_handler(struct output *output,
5087 display_output_handler_t handler)
5088{
5089 output->destroy_handler = handler;
5090 /* FIXME: implement this, once we have way to remove outputs */
5091}
5092
5093void
Scott Moreau4e072362012-09-29 02:03:11 -06005094output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005095{
Scott Moreau4e072362012-09-29 02:03:11 -06005096 struct rectangle allocation = output->allocation;
5097
5098 switch (output->transform) {
5099 case WL_OUTPUT_TRANSFORM_90:
5100 case WL_OUTPUT_TRANSFORM_270:
5101 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5102 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5103 /* Swap width and height */
5104 allocation.width = output->allocation.height;
5105 allocation.height = output->allocation.width;
5106 break;
5107 }
5108
5109 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005110}
5111
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005112struct wl_output *
5113output_get_wl_output(struct output *output)
5114{
5115 return output->output;
5116}
5117
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005118enum wl_output_transform
5119output_get_transform(struct output *output)
5120{
5121 return output->transform;
5122}
5123
Alexander Larssonafd319a2013-05-22 14:41:27 +02005124uint32_t
5125output_get_scale(struct output *output)
5126{
5127 return output->scale;
5128}
5129
Jason Ekstrand738715d2014-04-02 19:53:50 -05005130const char *
5131output_get_make(struct output *output)
5132{
5133 return output->make;
5134}
5135
5136const char *
5137output_get_model(struct output *output)
5138{
5139 return output->model;
5140}
5141
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005142static void
Daniel Stone97f68542012-05-30 16:32:01 +01005143fini_xkb(struct input *input)
5144{
5145 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005146 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005147}
5148
Jasper St. Pierre47f10432013-11-12 14:37:20 -05005149#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005150
Daniel Stone97f68542012-05-30 16:32:01 +01005151static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005152display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005153{
5154 struct input *input;
5155
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005156 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005157 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005158 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005159 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005160 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005161 input->pointer_focus = NULL;
5162 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005163 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005164 wl_list_insert(d->input_list.prev, &input->link);
5165
Daniel Stone37816df2012-05-16 18:45:18 +01005166 wl_seat_add_listener(input->seat, &seat_listener, input);
5167 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005168
Jason Ekstranda669bd52014-04-02 19:53:51 -05005169 if (d->data_device_manager) {
5170 input->data_device =
5171 wl_data_device_manager_get_data_device(d->data_device_manager,
5172 input->seat);
5173 wl_data_device_add_listener(input->data_device,
5174 &data_device_listener,
5175 input);
5176 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005177
5178 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005179
Jonny Lamb06959082014-08-12 14:58:27 +02005180 set_repeat_info(input, 40, 400);
5181
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005182 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5183 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005184 input->repeat_task.run = keyboard_repeat_func;
5185 display_watch_fd(d, input->repeat_timer_fd,
5186 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005187}
5188
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005189static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005190input_destroy(struct input *input)
5191{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005192 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005193 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005194
5195 if (input->drag_offer)
5196 data_offer_destroy(input->drag_offer);
5197
5198 if (input->selection_offer)
5199 data_offer_destroy(input->selection_offer);
5200
kabeer khan6ce67ec2014-10-20 11:55:29 +05305201 if (input->data_device) {
5202 if(input->display->data_device_manager_version >= 2)
5203 wl_data_device_release(input->data_device);
5204 else
5205 wl_data_device_destroy(input->data_device);
5206 }
Rob Bradford08031182013-08-13 20:11:03 +01005207 if (input->display->seat_version >= 3) {
5208 if (input->pointer)
5209 wl_pointer_release(input->pointer);
5210 if (input->keyboard)
5211 wl_keyboard_release(input->keyboard);
5212 }
5213
Daniel Stone97f68542012-05-30 16:32:01 +01005214 fini_xkb(input);
5215
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005216 wl_surface_destroy(input->pointer_surface);
5217
Pekka Paalanene1207c72011-12-16 12:02:09 +02005218 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005219 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005220 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005221 free(input);
5222}
5223
5224static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005225init_workspace_manager(struct display *d, uint32_t id)
5226{
5227 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005228 wl_registry_bind(d->registry, id,
5229 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005230 if (d->workspace_manager != NULL)
5231 workspace_manager_add_listener(d->workspace_manager,
5232 &workspace_manager_listener,
5233 d);
5234}
5235
5236static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005237shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5238{
5239 struct display *d = data;
5240
5241 if (format == WL_SHM_FORMAT_RGB565)
5242 d->has_rgb565 = 1;
5243}
5244
5245struct wl_shm_listener shm_listener = {
5246 shm_format
5247};
5248
5249static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005250xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5251{
5252 xdg_shell_pong(shell, serial);
5253}
5254
5255static const struct xdg_shell_listener xdg_shell_listener = {
5256 xdg_shell_ping,
5257};
5258
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005259#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005260#ifdef static_assert
5261static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5262 "Interface version doesn't match implementation version");
5263#endif
5264
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005265static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005266registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5267 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005268{
5269 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005270 struct global *global;
5271
Brian Lovinbc919262013-08-07 15:34:59 -07005272 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005273 global->name = id;
5274 global->interface = strdup(interface);
5275 global->version = version;
5276 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005277
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005278 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005279 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005280 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005281 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005282 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005283 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005284 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005285 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005286 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005287 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005288 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005289 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305290 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005291 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005292 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305293 &wl_data_device_manager_interface,
5294 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005295 } else if (strcmp(interface, "xdg_shell") == 0) {
5296 d->xdg_shell = wl_registry_bind(registry, id,
5297 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005298 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005299 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005300 } else if (strcmp(interface, "text_cursor_position") == 0) {
5301 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005302 wl_registry_bind(registry, id,
5303 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005304 } else if (strcmp(interface, "workspace_manager") == 0) {
5305 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005306 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5307 d->subcompositor =
5308 wl_registry_bind(registry, id,
5309 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005310 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005311 else if (strcmp(interface, "ivi_application") == 0) {
5312 d->ivi_application =
5313 wl_registry_bind(registry, id,
5314 &ivi_application_interface, 1);
5315 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005316
5317 if (d->global_handler)
5318 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005319}
5320
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005321static void
5322registry_handle_global_remove(void *data, struct wl_registry *registry,
5323 uint32_t name)
5324{
5325 struct display *d = data;
5326 struct global *global;
5327 struct global *tmp;
5328
5329 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5330 if (global->name != name)
5331 continue;
5332
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005333 if (strcmp(global->interface, "wl_output") == 0)
5334 display_destroy_output(d, name);
5335
5336 /* XXX: Should destroy remaining bound globals */
5337
5338 if (d->global_handler_remove)
5339 d->global_handler_remove(d, name, global->interface,
5340 global->version, d->user_data);
5341
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005342 wl_list_remove(&global->link);
5343 free(global->interface);
5344 free(global);
5345 }
5346}
5347
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005348void *
5349display_bind(struct display *display, uint32_t name,
5350 const struct wl_interface *interface, uint32_t version)
5351{
5352 return wl_registry_bind(display->registry, name, interface, version);
5353}
5354
5355static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005356 registry_handle_global,
5357 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005358};
5359
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005360#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005361static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005362init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005363{
5364 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005365 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005366
Rob Clark6396ed32012-03-11 19:48:41 -05005367#ifdef USE_CAIRO_GLESV2
5368# define GL_BIT EGL_OPENGL_ES2_BIT
5369#else
5370# define GL_BIT EGL_OPENGL_BIT
5371#endif
5372
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005373 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005374 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005375 EGL_RED_SIZE, 1,
5376 EGL_GREEN_SIZE, 1,
5377 EGL_BLUE_SIZE, 1,
5378 EGL_ALPHA_SIZE, 1,
5379 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005380 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005381 EGL_NONE
5382 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005383
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005384#ifdef USE_CAIRO_GLESV2
5385 static const EGLint context_attribs[] = {
5386 EGL_CONTEXT_CLIENT_VERSION, 2,
5387 EGL_NONE
5388 };
5389 EGLint api = EGL_OPENGL_ES_API;
5390#else
5391 EGLint *context_attribs = NULL;
5392 EGLint api = EGL_OPENGL_API;
5393#endif
5394
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005395 d->dpy =
5396 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5397 d->display, NULL);
5398
Yuval Fledel45568f62010-12-06 09:18:12 -05005399 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005400 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005401 return -1;
5402 }
5403
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005404 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005405 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005406 return -1;
5407 }
5408
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005409 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5410 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005411 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005412 return -1;
5413 }
5414
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005415 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005416 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005417 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005418 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005419 return -1;
5420 }
5421
Benjamin Franzke0c991632011-09-27 21:57:31 +02005422 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5423 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005424 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005425 return -1;
5426 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005427
5428 return 0;
5429}
5430
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005431static void
5432fini_egl(struct display *display)
5433{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005434 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005435
5436 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5437 EGL_NO_CONTEXT);
5438
5439 eglTerminate(display->dpy);
5440 eglReleaseThread();
5441}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005442#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005443
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005444static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005445init_dummy_surface(struct display *display)
5446{
5447 int len;
5448 void *data;
5449
5450 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005451 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005452 display->dummy_surface =
5453 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5454 1, 1, len);
5455 display->dummy_surface_data = data;
5456}
5457
5458static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005459handle_display_data(struct task *task, uint32_t events)
5460{
5461 struct display *display =
5462 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005463 struct epoll_event ep;
5464 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005465
5466 display->display_fd_events = events;
5467
5468 if (events & EPOLLERR || events & EPOLLHUP) {
5469 display_exit(display);
5470 return;
5471 }
5472
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005473 if (events & EPOLLIN) {
5474 ret = wl_display_dispatch(display->display);
5475 if (ret == -1) {
5476 display_exit(display);
5477 return;
5478 }
5479 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005480
5481 if (events & EPOLLOUT) {
5482 ret = wl_display_flush(display->display);
5483 if (ret == 0) {
5484 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5485 ep.data.ptr = &display->display_task;
5486 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5487 display->display_fd, &ep);
5488 } else if (ret == -1 && errno != EAGAIN) {
5489 display_exit(display);
5490 return;
5491 }
5492 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005493}
5494
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005495static void
5496log_handler(const char *format, va_list args)
5497{
5498 vfprintf(stderr, format, args);
5499}
5500
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005501struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005502display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005503{
5504 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005505
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005506 wl_log_set_handler_client(log_handler);
5507
Peter Huttererf3d62272013-08-08 11:57:05 +10005508 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005509 if (d == NULL)
5510 return NULL;
5511
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005512 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005513 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005514 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005515 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005516 return NULL;
5517 }
5518
Rob Bradford5ab9c752013-07-26 16:29:43 +01005519 d->xkb_context = xkb_context_new(0);
5520 if (d->xkb_context == NULL) {
5521 fprintf(stderr, "Failed to create XKB context\n");
5522 free(d);
5523 return NULL;
5524 }
5525
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005526 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005527 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005528 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005529 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5530 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005531
5532 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005533 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005534 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005535 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005536
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005537 d->workspace = 0;
5538 d->workspace_count = 1;
5539
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005540 d->registry = wl_display_get_registry(d->display);
5541 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005542
Marek Chalupaa519d062014-12-05 13:49:40 +01005543 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005544 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5545 return NULL;
5546 }
5547
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005548#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005549 if (init_egl(d) < 0)
5550 fprintf(stderr, "EGL does not seem to work, "
5551 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005552#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005553
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005554 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005555
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005556 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005557
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005558 wl_list_init(&d->window_list);
5559
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005560 init_dummy_surface(d);
5561
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005562 return d;
5563}
5564
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005565static void
5566display_destroy_outputs(struct display *display)
5567{
5568 struct output *tmp;
5569 struct output *output;
5570
5571 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5572 output_destroy(output);
5573}
5574
Pekka Paalanene1207c72011-12-16 12:02:09 +02005575static void
5576display_destroy_inputs(struct display *display)
5577{
5578 struct input *tmp;
5579 struct input *input;
5580
5581 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5582 input_destroy(input);
5583}
5584
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005585void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005586display_destroy(struct display *display)
5587{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005588 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005589 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5590 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005591
5592 if (!wl_list_empty(&display->deferred_list))
5593 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5594
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005595 cairo_surface_destroy(display->dummy_surface);
5596 free(display->dummy_surface_data);
5597
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005598 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005599 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005600
Daniel Stone97f68542012-05-30 16:32:01 +01005601 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005602
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005603 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005604 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005605
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005606#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005607 if (display->argb_device)
5608 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005609#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005610
Pekka Paalanen35e82632013-04-25 13:57:48 +03005611 if (display->subcompositor)
5612 wl_subcompositor_destroy(display->subcompositor);
5613
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005614 if (display->xdg_shell)
5615 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005616
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005617 if (display->ivi_application)
5618 ivi_application_destroy(display->ivi_application);
5619
Pekka Paalanenc2052982011-12-16 11:41:32 +02005620 if (display->shm)
5621 wl_shm_destroy(display->shm);
5622
5623 if (display->data_device_manager)
5624 wl_data_device_manager_destroy(display->data_device_manager);
5625
5626 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005627 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005628
5629 close(display->epoll_fd);
5630
U. Artie Eoff44874d92012-10-02 21:12:35 -07005631 if (!(display->display_fd_events & EPOLLERR) &&
5632 !(display->display_fd_events & EPOLLHUP))
5633 wl_display_flush(display->display);
5634
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005635 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005636 free(display);
5637}
5638
5639void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005640display_set_user_data(struct display *display, void *data)
5641{
5642 display->user_data = data;
5643}
5644
5645void *
5646display_get_user_data(struct display *display)
5647{
5648 return display->user_data;
5649}
5650
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005651struct wl_display *
5652display_get_display(struct display *display)
5653{
5654 return display->display;
5655}
5656
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005657int
5658display_has_subcompositor(struct display *display)
5659{
5660 if (display->subcompositor)
5661 return 1;
5662
5663 wl_display_roundtrip(display->display);
5664
5665 return display->subcompositor != NULL;
5666}
5667
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005668cairo_device_t *
5669display_get_cairo_device(struct display *display)
5670{
5671 return display->argb_device;
5672}
5673
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005674struct output *
5675display_get_output(struct display *display)
5676{
5677 return container_of(display->output_list.next, struct output, link);
5678}
5679
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005680struct wl_compositor *
5681display_get_compositor(struct display *display)
5682{
5683 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005684}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005685
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005686uint32_t
5687display_get_serial(struct display *display)
5688{
5689 return display->serial;
5690}
5691
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005692EGLDisplay
5693display_get_egl_display(struct display *d)
5694{
5695 return d->dpy;
5696}
5697
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005698struct wl_data_source *
5699display_create_data_source(struct display *display)
5700{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005701 if (display->data_device_manager)
5702 return wl_data_device_manager_create_data_source(display->data_device_manager);
5703 else
5704 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005705}
5706
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005707EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005708display_get_argb_egl_config(struct display *d)
5709{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005710 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005711}
5712
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005713int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005714display_acquire_window_surface(struct display *display,
5715 struct window *window,
5716 EGLContext ctx)
5717{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005718 struct surface *surface = window->main_surface;
5719
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005720 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005721 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005722
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005723 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005724 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005725}
5726
5727void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005728display_release_window_surface(struct display *display,
5729 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005730{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005731 struct surface *surface = window->main_surface;
5732
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005733 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005734 return;
5735
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005736 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005737}
5738
5739void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005740display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005741{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005742 wl_list_insert(&display->deferred_list, &task->link);
5743}
5744
5745void
5746display_watch_fd(struct display *display,
5747 int fd, uint32_t events, struct task *task)
5748{
5749 struct epoll_event ep;
5750
5751 ep.events = events;
5752 ep.data.ptr = task;
5753 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5754}
5755
5756void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005757display_unwatch_fd(struct display *display, int fd)
5758{
5759 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5760}
5761
5762void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005763display_run(struct display *display)
5764{
5765 struct task *task;
5766 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005767 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005768
Pekka Paalanen826d7952011-12-15 10:14:07 +02005769 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005770 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005771 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005772 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005773 struct task, link);
5774 wl_list_remove(&task->link);
5775 task->run(task, 0);
5776 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005777
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005778 wl_display_dispatch_pending(display->display);
5779
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005780 if (!display->running)
5781 break;
5782
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005783 ret = wl_display_flush(display->display);
5784 if (ret < 0 && errno == EAGAIN) {
5785 ep[0].events =
5786 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5787 ep[0].data.ptr = &display->display_task;
5788
5789 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5790 display->display_fd, &ep[0]);
5791 } else if (ret < 0) {
5792 break;
5793 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005794
5795 count = epoll_wait(display->epoll_fd,
5796 ep, ARRAY_LENGTH(ep), -1);
5797 for (i = 0; i < count; i++) {
5798 task = ep[i].data.ptr;
5799 task->run(task, ep[i].events);
5800 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005801 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005802}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005803
5804void
5805display_exit(struct display *display)
5806{
5807 display->running = 0;
5808}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005809
5810void
5811keysym_modifiers_add(struct wl_array *modifiers_map,
5812 const char *name)
5813{
5814 size_t len = strlen(name) + 1;
5815 char *p;
5816
5817 p = wl_array_add(modifiers_map, len);
5818
5819 if (p == NULL)
5820 return;
5821
5822 strncpy(p, name, len);
5823}
5824
5825static xkb_mod_index_t
5826keysym_modifiers_get_index(struct wl_array *modifiers_map,
5827 const char *name)
5828{
5829 xkb_mod_index_t index = 0;
5830 char *p = modifiers_map->data;
5831
5832 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5833 if (strcmp(p, name) == 0)
5834 return index;
5835
5836 index++;
5837 p += strlen(p) + 1;
5838 }
5839
5840 return XKB_MOD_INVALID;
5841}
5842
5843xkb_mod_mask_t
5844keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5845 const char *name)
5846{
5847 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5848
5849 if (index == XKB_MOD_INVALID)
5850 return XKB_MOD_INVALID;
5851
5852 return 1 << index;
5853}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005854
5855void *
5856fail_on_null(void *p)
5857{
5858 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005859 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005860 exit(EXIT_FAILURE);
5861 }
5862
5863 return p;
5864}
5865
5866void *
5867xmalloc(size_t s)
5868{
5869 return fail_on_null(malloc(s));
5870}
5871
Peter Huttererf3d62272013-08-08 11:57:05 +10005872void *
5873xzalloc(size_t s)
5874{
5875 return fail_on_null(zalloc(s));
5876}
5877
Kristian Høgsbergce278412013-07-25 15:20:20 -07005878char *
5879xstrdup(const char *s)
5880{
5881 return fail_on_null(strdup(s));
5882}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005883
5884void *
5885xrealloc(char *p, size_t s)
5886{
5887 return fail_on_null(realloc(p, s));
5888}