blob: c5082bae13a1e7ca264f749f372fc27d6290ad95 [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>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030042#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030055#else /* HAVE_CAIRO_EGL */
56typedef void *EGLDisplay;
57typedef void *EGLConfig;
58typedef void *EGLContext;
59#define EGL_NO_DISPLAY ((EGLDisplay)0)
60#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061
Daniel Stone9d4f0302012-02-15 16:33:21 +000062#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030063#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020066#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040067#include "../shared/cairo-util.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050068#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090075#include <sys/types.h>
76#include "ivi-application-client-protocol.h"
77#define IVI_SURFACE_ID 9000
78
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070079struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030080
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040081struct global {
82 uint32_t name;
83 char *interface;
84 uint32_t version;
85 struct wl_list link;
86};
87
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050088struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050089 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040090 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050091 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030092 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040093 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040094 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060095 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020096 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050097 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090098 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040099 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500100 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200101 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200102 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400103 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400104
105 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700106 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400107 struct task display_task;
108
109 int epoll_fd;
110 struct wl_list deferred_list;
111
Pekka Paalanen826d7952011-12-15 10:14:07 +0200112 int running;
113
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400114 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400115 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400116 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500117 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400118
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400119 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400120
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300121 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300122 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400123
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200124 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400125 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800126 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200127
128 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100129
130 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200131
132 uint32_t workspace;
133 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200134
135 /* A hack to get text extents for tooltips */
136 cairo_surface_t *dummy_surface;
137 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200138
139 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100140 int seat_version;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530141 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500142};
143
Rob Bradford7507b572012-05-15 17:55:34 +0100144struct window_output {
145 struct output *output;
146 struct wl_list link;
147};
148
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200149struct toysurface {
150 /*
151 * Prepare the surface for drawing. Makes sure there is a surface
152 * of the right size available for rendering, and returns it.
153 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200154 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200155 * If flags has SURFACE_HINT_RESIZE set, the user is
156 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157 * Returns the Cairo surface to draw to.
158 */
159 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200160 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200161 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200162
163 /*
164 * Post the surface to the server, returning the server allocation
165 * rectangle. The Cairo surface from prepare() must be destroyed
166 * after calling this.
167 */
168 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200169 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200170 struct rectangle *server_allocation);
171
172 /*
173 * Make the toysurface current with the given EGL context.
174 * Returns 0 on success, and negative of failure.
175 */
176 int (*acquire)(struct toysurface *base, EGLContext ctx);
177
178 /*
179 * Release the toysurface from the EGL context, returning control
180 * to Cairo.
181 */
182 void (*release)(struct toysurface *base);
183
184 /*
185 * Destroy the toysurface, including the Cairo surface, any
186 * backing storage, and the Wayland protocol objects.
187 */
188 void (*destroy)(struct toysurface *base);
189};
190
Pekka Paalanen4e373742013-02-13 16:17:13 +0200191struct surface {
192 struct window *window;
193
194 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300195 struct wl_subsurface *subsurface;
196 int synchronized;
197 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200198 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200199 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300200 int redraw_needed;
201 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300202 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200203
204 struct rectangle allocation;
205 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200206
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200207 struct wl_region *input_region;
208 struct wl_region *opaque_region;
209
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200210 enum window_buffer_type buffer_type;
211 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200212 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200213
214 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300215
216 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200217};
218
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500219struct window {
220 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100221 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500222 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200223 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400224 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500225 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200226 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500227 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400228 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300229 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400230 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400231 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500232 int custom;
233 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400234
Pekka Paalanen99436862012-11-19 17:15:59 +0200235 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400236
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500237 int fullscreen;
238 int maximized;
239
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200240 enum preferred_format preferred_format;
241
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500242 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500243 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400244 window_data_handler_t data_handler;
245 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500246 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400247 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200248 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700249 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400250
Pekka Paalanen4e373742013-02-13 16:17:13 +0200251 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500252 struct xdg_surface *xdg_surface;
253 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300254
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700255 struct window *parent;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200256 struct wl_surface *last_parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500257
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900258 struct ivi_surface *ivi_surface;
259
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500260 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500261
Pekka Paalanen35e82632013-04-25 13:57:48 +0300262 /* struct surface::link, contains also main_surface */
263 struct wl_list subsurface_list;
264
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500265 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400266 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500267};
268
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500269struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500270 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200271 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300272 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500273 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400274 struct wl_list link;
275 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500276 widget_resize_handler_t resize_handler;
277 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500278 widget_enter_handler_t enter_handler;
279 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500280 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500281 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700282 widget_touch_down_handler_t touch_down_handler;
283 widget_touch_up_handler_t touch_up_handler;
284 widget_touch_motion_handler_t touch_motion_handler;
285 widget_touch_frame_handler_t touch_frame_handler;
286 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200287 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400288 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500289 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300290 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500291 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000292 /* If this is set to false then no cairo surface will be
293 * created before redrawing the surface. This is useful if the
294 * redraw handler is going to do completely custom rendering
295 * such as using EGL directly */
296 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400297};
298
Rusty Lynch041815a2013-08-08 21:20:38 -0700299struct touch_point {
300 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800301 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700302 struct widget *widget;
303 struct wl_list link;
304};
305
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400306struct input {
307 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100308 struct wl_seat *seat;
309 struct wl_pointer *pointer;
310 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700311 struct wl_touch *touch;
312 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400313 struct window *pointer_focus;
314 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700315 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300316 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300317 uint32_t cursor_anim_start;
318 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300319 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400320 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400321 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400322 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400323 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400324 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400325
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500326 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500327 struct widget *grab;
328 uint32_t grab_button;
329
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400330 struct wl_data_device *data_device;
331 struct data_offer *drag_offer;
332 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800333 uint32_t touch_grab;
334 int32_t touch_grab_id;
335 float drag_x, drag_y;
336 struct window *drag_focus;
337 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100338
339 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100340 struct xkb_keymap *keymap;
341 struct xkb_state *state;
342 xkb_mod_mask_t control_mask;
343 xkb_mod_mask_t alt_mask;
344 xkb_mod_mask_t shift_mask;
345 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400346
Jonny Lamb06959082014-08-12 14:58:27 +0200347 int32_t repeat_rate_sec;
348 int32_t repeat_rate_nsec;
349 int32_t repeat_delay_sec;
350 int32_t repeat_delay_nsec;
351
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400352 struct task repeat_task;
353 int repeat_timer_fd;
354 uint32_t repeat_sym;
355 uint32_t repeat_key;
356 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400357};
358
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500359struct output {
360 struct display *display;
361 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800362 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500363 struct rectangle allocation;
364 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600365 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200366 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500367 char *make;
368 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200369
370 display_output_handler_t destroy_handler;
371 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500372};
373
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500374struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500375 struct widget *widget;
376 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500377 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800378
379 uint32_t last_time;
380 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800381 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500382};
383
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500384struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400385 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500386 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500387 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500388 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700389 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500390 const char **entries;
391 uint32_t time;
392 int current;
393 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400394 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500395 menu_func_t func;
396};
397
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300398struct tooltip {
399 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300400 struct widget *widget;
401 char *entry;
402 struct task tooltip_task;
403 int tooltip_fd;
404 float x, y;
405};
406
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700407struct shm_pool {
408 struct wl_shm_pool *pool;
409 size_t size;
410 size_t used;
411 void *data;
412};
413
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400414enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300415 CURSOR_DEFAULT = 100,
416 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400417};
418
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200419static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420
Pekka Paalanen97777442013-05-22 10:20:05 +0300421/* #define DEBUG */
422
423#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300424
425static void
426debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
427__attribute__ ((format (printf, 4, 5)));
428
429static void
430debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
431{
432 va_list ap;
433 struct timeval tv;
434
435 gettimeofday(&tv, NULL);
436 fprintf(stderr, "%8ld.%03ld ",
437 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
438
439 if (proxy)
440 fprintf(stderr, "%s@%d ",
441 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
442
443 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
444 fprintf(stderr, "%s ", func);
445
446 va_start(ap, fmt);
447 vfprintf(stderr, fmt, ap);
448 va_end(ap);
449}
450
451#define DBG(fmt, ...) \
452 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
453
454#define DBG_OBJ(obj, fmt, ...) \
455 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
456
457#else
458
459#define DBG(...) do {} while (0)
460#define DBG_OBJ(...) do {} while (0)
461
462#endif
463
Alexander Larsson1818e312013-05-22 14:41:31 +0200464static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200465surface_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 +0200466{
467 int32_t tmp;
468
469 switch (buffer_transform) {
470 case WL_OUTPUT_TRANSFORM_90:
471 case WL_OUTPUT_TRANSFORM_270:
472 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
473 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
474 tmp = *width;
475 *width = *height;
476 *height = tmp;
477 break;
478 default:
479 break;
480 }
481
482 *width *= buffer_scale;
483 *height *= buffer_scale;
484}
485
486static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200487buffer_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 +0200488{
489 int32_t tmp;
490
491 switch (buffer_transform) {
492 case WL_OUTPUT_TRANSFORM_90:
493 case WL_OUTPUT_TRANSFORM_270:
494 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
495 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
496 tmp = *width;
497 *width = *height;
498 *height = tmp;
499 break;
500 default:
501 break;
502 }
503
504 *width /= buffer_scale;
505 *height /= buffer_scale;
506}
507
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500508#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400509
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200510struct egl_window_surface {
511 struct toysurface base;
512 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100513 struct display *display;
514 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515 struct wl_egl_window *egl_window;
516 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100517};
518
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519static struct egl_window_surface *
520to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100523}
524
525static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200527 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200528 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100529{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 struct egl_window_surface *surface = to_egl_window_surface(base);
531
Alexander Larsson1818e312013-05-22 14:41:31 +0200532 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
533
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
535 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
536
537 return cairo_surface_reference(surface->cairo_surface);
538}
539
540static void
541egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200542 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200543 struct rectangle *server_allocation)
544{
545 struct egl_window_surface *surface = to_egl_window_surface(base);
546
547 cairo_gl_surface_swapbuffers(surface->cairo_surface);
548 wl_egl_window_get_attached_size(surface->egl_window,
549 &server_allocation->width,
550 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200551
552 buffer_to_surface_size (buffer_transform, buffer_scale,
553 &server_allocation->width,
554 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200555}
556
557static int
558egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
559{
560 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200561 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100562
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563 device = cairo_surface_get_device(surface->cairo_surface);
564 if (!device)
565 return -1;
566
567 if (!ctx) {
568 if (device == surface->display->argb_device)
569 ctx = surface->display->argb_ctx;
570 else
571 assert(0);
572 }
573
574 cairo_device_flush(device);
575 cairo_device_acquire(device);
576 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
577 surface->egl_surface, ctx))
578 fprintf(stderr, "failed to make surface current\n");
579
580 return 0;
581}
582
583static void
584egl_window_surface_release(struct toysurface *base)
585{
586 struct egl_window_surface *surface = to_egl_window_surface(base);
587 cairo_device_t *device;
588
589 device = cairo_surface_get_device(surface->cairo_surface);
590 if (!device)
591 return;
592
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200593 if (!eglMakeCurrent(surface->display->dpy,
594 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200595 fprintf(stderr, "failed to make context current\n");
596
597 cairo_device_release(device);
598}
599
600static void
601egl_window_surface_destroy(struct toysurface *base)
602{
603 struct egl_window_surface *surface = to_egl_window_surface(base);
604 struct display *d = surface->display;
605
606 cairo_surface_destroy(surface->cairo_surface);
607 eglDestroySurface(d->dpy, surface->egl_surface);
608 wl_egl_window_destroy(surface->egl_window);
609 surface->surface = NULL;
610
611 free(surface);
612}
613
614static struct toysurface *
615egl_window_surface_create(struct display *display,
616 struct wl_surface *wl_surface,
617 uint32_t flags,
618 struct rectangle *rectangle)
619{
620 struct egl_window_surface *surface;
621
Pekka Paalanenb3627362012-11-19 17:16:00 +0200622 if (display->dpy == EGL_NO_DISPLAY)
623 return NULL;
624
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200625 surface = calloc(1, sizeof *surface);
626 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100627 return NULL;
628
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200629 surface->base.prepare = egl_window_surface_prepare;
630 surface->base.swap = egl_window_surface_swap;
631 surface->base.acquire = egl_window_surface_acquire;
632 surface->base.release = egl_window_surface_release;
633 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200635 surface->display = display;
636 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400637
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200638 surface->egl_window = wl_egl_window_create(surface->surface,
639 rectangle->width,
640 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200642 surface->egl_surface = eglCreateWindowSurface(display->dpy,
643 display->argb_config,
644 surface->egl_window,
645 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100646
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200647 surface->cairo_surface =
648 cairo_gl_surface_create_for_egl(display->argb_device,
649 surface->egl_surface,
650 rectangle->width,
651 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100652
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200653 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100654}
655
Pekka Paalanenb3627362012-11-19 17:16:00 +0200656#else
657
658static struct toysurface *
659egl_window_surface_create(struct display *display,
660 struct wl_surface *wl_surface,
661 uint32_t flags,
662 struct rectangle *rectangle)
663{
664 return NULL;
665}
666
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400667#endif
668
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200669struct shm_surface_data {
670 struct wl_buffer *buffer;
671 struct shm_pool *pool;
672};
673
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674struct wl_buffer *
675display_get_buffer_for_surface(struct display *display,
676 cairo_surface_t *surface)
677{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200678 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400679
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200680 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681
682 return data->buffer;
683}
684
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500685static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700686shm_pool_destroy(struct shm_pool *pool);
687
688static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689shm_surface_data_destroy(void *p)
690{
691 struct shm_surface_data *data = p;
692
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200693 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700694 if (data->pool)
695 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300696
697 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400698}
699
Kristian Høgsberg16626282012-04-03 11:21:27 -0400700static struct wl_shm_pool *
701make_shm_pool(struct display *display, int size, void **data)
702{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400703 struct wl_shm_pool *pool;
704 int fd;
705
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300706 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400707 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300708 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
709 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400710 return NULL;
711 }
712
713 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400714 if (*data == MAP_FAILED) {
715 fprintf(stderr, "mmap failed: %m\n");
716 close(fd);
717 return NULL;
718 }
719
720 pool = wl_shm_create_pool(display->shm, fd, size);
721
722 close(fd);
723
724 return pool;
725}
726
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700727static struct shm_pool *
728shm_pool_create(struct display *display, size_t size)
729{
730 struct shm_pool *pool = malloc(sizeof *pool);
731
732 if (!pool)
733 return NULL;
734
735 pool->pool = make_shm_pool(display, size, &pool->data);
736 if (!pool->pool) {
737 free(pool);
738 return NULL;
739 }
740
741 pool->size = size;
742 pool->used = 0;
743
744 return pool;
745}
746
747static void *
748shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
749{
750 if (pool->used + size > pool->size)
751 return NULL;
752
753 *offset = pool->used;
754 pool->used += size;
755
756 return (char *) pool->data + *offset;
757}
758
759/* destroy the pool. this does not unmap the memory though */
760static void
761shm_pool_destroy(struct shm_pool *pool)
762{
763 munmap(pool->data, pool->size);
764 wl_shm_pool_destroy(pool->pool);
765 free(pool);
766}
767
768/* Start allocating from the beginning of the pool again */
769static void
770shm_pool_reset(struct shm_pool *pool)
771{
772 pool->used = 0;
773}
774
775static int
776data_length_for_shm_surface(struct rectangle *rect)
777{
778 int stride;
779
780 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
781 rect->width);
782 return stride * rect->height;
783}
784
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500785static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700786display_create_shm_surface_from_pool(struct display *display,
787 struct rectangle *rectangle,
788 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400789{
790 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400791 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400792 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200793 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700794 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400795 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400796
797 data = malloc(sizeof *data);
798 if (data == NULL)
799 return NULL;
800
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200801 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
802 cairo_format = CAIRO_FORMAT_RGB16_565;
803 else
804 cairo_format = CAIRO_FORMAT_ARGB32;
805
806 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700807 length = stride * rectangle->height;
808 data->pool = NULL;
809 map = shm_pool_allocate(pool, length, &offset);
810
811 if (!map) {
812 free(data);
813 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400814 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400815
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400816 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200817 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400818 rectangle->width,
819 rectangle->height,
820 stride);
821
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200822 cairo_surface_set_user_data(surface, &shm_surface_data_key,
823 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200825 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
826 format = WL_SHM_FORMAT_RGB565;
827 else {
828 if (flags & SURFACE_OPAQUE)
829 format = WL_SHM_FORMAT_XRGB8888;
830 else
831 format = WL_SHM_FORMAT_ARGB8888;
832 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400833
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200834 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
835 rectangle->width,
836 rectangle->height,
837 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400838
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700839 return surface;
840}
841
842static cairo_surface_t *
843display_create_shm_surface(struct display *display,
844 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200845 struct shm_pool *alternate_pool,
846 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700847{
848 struct shm_surface_data *data;
849 struct shm_pool *pool;
850 cairo_surface_t *surface;
851
Pekka Paalanen99436862012-11-19 17:15:59 +0200852 if (alternate_pool) {
853 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700854 surface = display_create_shm_surface_from_pool(display,
855 rectangle,
856 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200857 alternate_pool);
858 if (surface) {
859 data = cairo_surface_get_user_data(surface,
860 &shm_surface_data_key);
861 goto out;
862 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700863 }
864
865 pool = shm_pool_create(display,
866 data_length_for_shm_surface(rectangle));
867 if (!pool)
868 return NULL;
869
870 surface =
871 display_create_shm_surface_from_pool(display, rectangle,
872 flags, pool);
873
874 if (!surface) {
875 shm_pool_destroy(pool);
876 return NULL;
877 }
878
879 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200880 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700881 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400882
Pekka Paalanen99436862012-11-19 17:15:59 +0200883out:
884 if (data_ret)
885 *data_ret = data;
886
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400887 return surface;
888}
889
nobled7b87cb02011-02-01 18:51:47 +0000890static int
891check_size(struct rectangle *rect)
892{
893 if (rect->width && rect->height)
894 return 0;
895
896 fprintf(stderr, "tried to create surface of "
897 "width: %d, height: %d\n", rect->width, rect->height);
898 return -1;
899}
900
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400901cairo_surface_t *
902display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400904 struct rectangle *rectangle,
905 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400906{
nobled7b87cb02011-02-01 18:51:47 +0000907 if (check_size(rectangle) < 0)
908 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200909
910 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200911 return display_create_shm_surface(display, rectangle, flags,
912 NULL, NULL);
913}
914
Pekka Paalanena4eda732012-11-19 17:16:02 +0200915struct shm_surface_leaf {
916 cairo_surface_t *cairo_surface;
917 /* 'data' is automatically destroyed, when 'cairo_surface' is */
918 struct shm_surface_data *data;
919
920 struct shm_pool *resize_pool;
921 int busy;
922};
923
924static void
925shm_surface_leaf_release(struct shm_surface_leaf *leaf)
926{
927 if (leaf->cairo_surface)
928 cairo_surface_destroy(leaf->cairo_surface);
929 /* leaf->data already destroyed via cairo private */
930
931 if (leaf->resize_pool)
932 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200933
934 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200935}
936
Pekka Paalanenaef02542013-04-25 13:57:47 +0300937#define MAX_LEAVES 3
938
Pekka Paalanen99436862012-11-19 17:15:59 +0200939struct shm_surface {
940 struct toysurface base;
941 struct display *display;
942 struct wl_surface *surface;
943 uint32_t flags;
944 int dx, dy;
945
Pekka Paalanenaef02542013-04-25 13:57:47 +0300946 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200947 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200948};
949
950static struct shm_surface *
951to_shm_surface(struct toysurface *base)
952{
953 return container_of(base, struct shm_surface, base);
954}
955
Pekka Paalanena4eda732012-11-19 17:16:02 +0200956static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300957shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
958{
959#ifdef DEBUG
960 struct shm_surface_leaf *leaf;
961 char bufs[MAX_LEAVES + 1];
962 int i;
963
964 for (i = 0; i < MAX_LEAVES; i++) {
965 leaf = &surface->leaf[i];
966
967 if (leaf->busy)
968 bufs[i] = 'b';
969 else if (leaf->cairo_surface)
970 bufs[i] = 'a';
971 else
972 bufs[i] = ' ';
973 }
974
975 bufs[MAX_LEAVES] = '\0';
976 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
977#endif
978}
979
980static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200981shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
982{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200983 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300984 struct shm_surface_leaf *leaf;
985 int i;
986 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300987
988 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200989
Pekka Paalanenaef02542013-04-25 13:57:47 +0300990 for (i = 0; i < MAX_LEAVES; i++) {
991 leaf = &surface->leaf[i];
992 if (leaf->data && leaf->data->buffer == buffer) {
993 leaf->busy = 0;
994 break;
995 }
996 }
997 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200998
Pekka Paalanenaef02542013-04-25 13:57:47 +0300999 /* Leave one free leaf with storage, release others */
1000 free_found = 0;
1001 for (i = 0; i < MAX_LEAVES; i++) {
1002 leaf = &surface->leaf[i];
1003
1004 if (!leaf->cairo_surface || leaf->busy)
1005 continue;
1006
1007 if (!free_found)
1008 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001009 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001010 shm_surface_leaf_release(leaf);
1011 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001012
Pekka Paalanen97777442013-05-22 10:20:05 +03001013 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001014}
1015
1016static const struct wl_buffer_listener shm_surface_buffer_listener = {
1017 shm_surface_buffer_release
1018};
1019
Pekka Paalanen99436862012-11-19 17:15:59 +02001020static cairo_surface_t *
1021shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001022 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001023 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001024{
Pekka Paalanenec076692012-11-30 13:37:27 +02001025 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001026 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001027 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001028 struct shm_surface_leaf *leaf = NULL;
1029 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001030
1031 surface->dx = dx;
1032 surface->dy = dy;
1033
Pekka Paalanenaef02542013-04-25 13:57:47 +03001034 /* pick a free buffer, preferrably one that already has storage */
1035 for (i = 0; i < MAX_LEAVES; i++) {
1036 if (surface->leaf[i].busy)
1037 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001038
Pekka Paalanenaef02542013-04-25 13:57:47 +03001039 if (!leaf || surface->leaf[i].cairo_surface)
1040 leaf = &surface->leaf[i];
1041 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001042 DBG_OBJ(surface->surface, "pick leaf %d\n",
1043 (int)(leaf - &surface->leaf[0]));
1044
Pekka Paalanenaef02542013-04-25 13:57:47 +03001045 if (!leaf) {
1046 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001047 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001048 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001050 }
1051
Pekka Paalanena4eda732012-11-19 17:16:02 +02001052 if (!resize_hint && leaf->resize_pool) {
1053 cairo_surface_destroy(leaf->cairo_surface);
1054 leaf->cairo_surface = NULL;
1055 shm_pool_destroy(leaf->resize_pool);
1056 leaf->resize_pool = NULL;
1057 }
1058
Alexander Larsson1818e312013-05-22 14:41:31 +02001059 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1060
Pekka Paalanena4eda732012-11-19 17:16:02 +02001061 if (leaf->cairo_surface &&
1062 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1063 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001064 goto out;
1065
Pekka Paalanena4eda732012-11-19 17:16:02 +02001066 if (leaf->cairo_surface)
1067 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001068
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001069#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001070 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001071 /* Create a big pool to allocate from, while continuously
1072 * resizing. Mmapping a new pool in the server
1073 * is relatively expensive, so reusing a pool performs
1074 * better, but may temporarily reserve unneeded memory.
1075 */
1076 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001077 leaf->resize_pool = shm_pool_create(surface->display,
1078 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001079 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001080#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001081
Alexander Larsson1818e312013-05-22 14:41:31 +02001082 rect.width = width;
1083 rect.height = height;
1084
Pekka Paalanena4eda732012-11-19 17:16:02 +02001085 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001086 display_create_shm_surface(surface->display, &rect,
1087 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 leaf->resize_pool,
1089 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001090 if (!leaf->cairo_surface)
1091 return NULL;
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001094 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001095
1096out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001097 surface->current = leaf;
1098
1099 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001100}
1101
1102static void
1103shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001104 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001105 struct rectangle *server_allocation)
1106{
1107 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001108 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001109
1110 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001112 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001113 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001114
Alexander Larsson1818e312013-05-22 14:41:31 +02001115 buffer_to_surface_size (buffer_transform, buffer_scale,
1116 &server_allocation->width,
1117 &server_allocation->height);
1118
Pekka Paalanena4eda732012-11-19 17:16:02 +02001119 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001120 surface->dx, surface->dy);
1121 wl_surface_damage(surface->surface, 0, 0,
1122 server_allocation->width, server_allocation->height);
1123 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001124
Pekka Paalanen71233882013-04-25 13:57:53 +03001125 DBG_OBJ(surface->surface, "leaf %d busy\n",
1126 (int)(leaf - &surface->leaf[0]));
1127
Pekka Paalanena4eda732012-11-19 17:16:02 +02001128 leaf->busy = 1;
1129 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001130}
1131
1132static int
1133shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1134{
1135 return -1;
1136}
1137
1138static void
1139shm_surface_release(struct toysurface *base)
1140{
1141}
1142
1143static void
1144shm_surface_destroy(struct toysurface *base)
1145{
1146 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001147 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001148
Pekka Paalanenaef02542013-04-25 13:57:47 +03001149 for (i = 0; i < MAX_LEAVES; i++)
1150 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001151
1152 free(surface);
1153}
1154
1155static struct toysurface *
1156shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1157 uint32_t flags, struct rectangle *rectangle)
1158{
1159 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001160 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001161
Brian Lovinbc919262013-08-07 15:34:59 -07001162 surface = xmalloc(sizeof *surface);
1163 memset(surface, 0, sizeof *surface);
1164
Pekka Paalanen99436862012-11-19 17:15:59 +02001165 if (!surface)
1166 return NULL;
1167
1168 surface->base.prepare = shm_surface_prepare;
1169 surface->base.swap = shm_surface_swap;
1170 surface->base.acquire = shm_surface_acquire;
1171 surface->base.release = shm_surface_release;
1172 surface->base.destroy = shm_surface_destroy;
1173
1174 surface->display = display;
1175 surface->surface = wl_surface;
1176 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001177
1178 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001179}
1180
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001181/*
1182 * The following correspondences between file names and cursors was copied
1183 * from: https://bugs.kde.org/attachment.cgi?id=67313
1184 */
1185
1186static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001187 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001188 "sw-resize",
1189 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001190};
1191
1192static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001193 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001194 "se-resize",
1195 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001196};
1197
1198static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001199 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001200 "s-resize",
1201 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001202};
1203
1204static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001205 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001206 "closedhand",
1207 "208530c400c041818281048008011002"
1208};
1209
1210static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001211 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001212 "default",
1213 "top_left_arrow",
1214 "left-arrow"
1215};
1216
1217static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001218 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001219 "w-resize",
1220 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001221};
1222
1223static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001224 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001225 "e-resize",
1226 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001227};
1228
1229static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001230 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001231 "nw-resize",
1232 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001233};
1234
1235static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001236 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001237 "ne-resize",
1238 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001239};
1240
1241static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001242 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001243 "n-resize",
1244 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001245};
1246
1247static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001248 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001249 "ibeam",
1250 "text"
1251};
1252
1253static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001254 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001255 "pointer",
1256 "pointing_hand",
1257 "e29285e634086352946a0e7090d73106"
1258};
1259
1260static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001261 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001262 "wait",
1263 "0426c94ea35c87780ff01dc239897213"
1264};
1265
1266struct cursor_alternatives {
1267 const char **names;
1268 size_t count;
1269};
1270
1271static const struct cursor_alternatives cursors[] = {
1272 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1273 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1274 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1275 {grabbings, ARRAY_LENGTH(grabbings)},
1276 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1277 {left_sides, ARRAY_LENGTH(left_sides)},
1278 {right_sides, ARRAY_LENGTH(right_sides)},
1279 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1280 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1281 {top_sides, ARRAY_LENGTH(top_sides)},
1282 {xterms, ARRAY_LENGTH(xterms)},
1283 {hand1s, ARRAY_LENGTH(hand1s)},
1284 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001285};
1286
1287static void
1288create_cursors(struct display *display)
1289{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001290 struct weston_config *config;
1291 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001292 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001293 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001294 unsigned int i, j;
1295 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001296
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001297 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001298 s = weston_config_get_section(config, "shell", NULL, NULL);
1299 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1300 weston_config_section_get_int(s, "cursor-size", &size, 32);
1301 weston_config_destroy(config);
1302
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001303 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001304 if (!display->cursor_theme) {
1305 fprintf(stderr, "could not load theme '%s'\n", theme);
1306 return;
1307 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001308 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001309 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001310 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001311
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001312 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001313 cursor = NULL;
1314 for (j = 0; !cursor && j < cursors[i].count; ++j)
1315 cursor = wl_cursor_theme_get_cursor(
1316 display->cursor_theme, cursors[i].names[j]);
1317
1318 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001319 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001320 cursors[i].names[0]);
1321
1322 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001323 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001324}
1325
1326static void
1327destroy_cursors(struct display *display)
1328{
1329 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001330 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001331}
1332
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001333struct wl_cursor_image *
1334display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001335{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001336 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001337
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001338 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001339}
1340
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001341static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001342surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001343{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001344 if (!surface->cairo_surface)
1345 return;
1346
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001347 if (surface->opaque_region) {
1348 wl_surface_set_opaque_region(surface->surface,
1349 surface->opaque_region);
1350 wl_region_destroy(surface->opaque_region);
1351 surface->opaque_region = NULL;
1352 }
1353
1354 if (surface->input_region) {
1355 wl_surface_set_input_region(surface->surface,
1356 surface->input_region);
1357 wl_region_destroy(surface->input_region);
1358 surface->input_region = NULL;
1359 }
1360
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001361 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001362 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001363 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001364
Pekka Paalanen89dee002013-02-13 16:17:20 +02001365 cairo_surface_destroy(surface->cairo_surface);
1366 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001367}
1368
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001369int
1370window_has_focus(struct window *window)
1371{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001372 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001373}
1374
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001375static void
1376window_close(struct window *window)
1377{
1378 if (window->close_handler)
1379 window->close_handler(window->user_data);
1380 else
1381 display_exit(window->display);
1382}
1383
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001384struct display *
1385window_get_display(struct window *window)
1386{
1387 return window->display;
1388}
1389
Pekka Paalanen89dee002013-02-13 16:17:20 +02001390static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001391handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1392 int32_t width, int32_t height)
1393{
1394 struct window *window = data;
1395
1396 window_schedule_resize(window, width, height);
1397}
1398
1399static const struct ivi_surface_listener ivi_surface_listener = {
1400 handle_ivi_surface_configure,
1401};
1402
1403static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001404surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001405{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001406 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001407 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001408
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409 if (!surface->toysurface && display->dpy &&
1410 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001411 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001412 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001413 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001414 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001415 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001416 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001417
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001419 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001420 surface->surface,
1421 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001422
Pekka Paalanen89dee002013-02-13 16:17:20 +02001423 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001424 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001425 allocation.width, allocation.height, flags,
1426 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001427}
1428
1429static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001430window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001431{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001432 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001433 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001434
Pekka Paalanenec076692012-11-30 13:37:27 +02001435 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001436 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001437
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001438 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1439 flags |= SURFACE_HINT_RGB565;
1440
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001441 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001442}
1443
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001444int
1445window_get_buffer_transform(struct window *window)
1446{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001447 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001448}
1449
1450void
1451window_set_buffer_transform(struct window *window,
1452 enum wl_output_transform transform)
1453{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001454 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001455 wl_surface_set_buffer_transform(window->main_surface->surface,
1456 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001457}
1458
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001459void
1460window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001461 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001462{
1463 window->main_surface->buffer_scale = scale;
1464 wl_surface_set_buffer_scale(window->main_surface->surface,
1465 scale);
1466}
1467
1468uint32_t
1469window_get_buffer_scale(struct window *window)
1470{
1471 return window->main_surface->buffer_scale;
1472}
1473
Alexander Larssond68f5232013-05-22 14:41:33 +02001474uint32_t
1475window_get_output_scale(struct window *window)
1476{
1477 struct window_output *window_output;
1478 struct window_output *window_output_tmp;
1479 int scale = 1;
1480
1481 wl_list_for_each_safe(window_output, window_output_tmp,
1482 &window->window_output_list, link) {
1483 if (window_output->output->scale > scale)
1484 scale = window_output->output->scale;
1485 }
1486
1487 return scale;
1488}
1489
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001490static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001491
Pekka Paalanen4e373742013-02-13 16:17:13 +02001492static void
1493surface_destroy(struct surface *surface)
1494{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001495 if (surface->frame_cb)
1496 wl_callback_destroy(surface->frame_cb);
1497
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001498 if (surface->input_region)
1499 wl_region_destroy(surface->input_region);
1500
1501 if (surface->opaque_region)
1502 wl_region_destroy(surface->opaque_region);
1503
Pekka Paalanen35e82632013-04-25 13:57:48 +03001504 if (surface->subsurface)
1505 wl_subsurface_destroy(surface->subsurface);
1506
Pekka Paalanen4e373742013-02-13 16:17:13 +02001507 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001508
1509 if (surface->toysurface)
1510 surface->toysurface->destroy(surface->toysurface);
1511
Pekka Paalanen35e82632013-04-25 13:57:48 +03001512 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001513 free(surface);
1514}
1515
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001516void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001517window_destroy(struct window *window)
1518{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001519 struct display *display = window->display;
1520 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001521 struct window_output *window_output;
1522 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001523
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001524 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001525
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001526 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001527 if (input->touch_focus == window)
1528 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001529 if (input->pointer_focus == window)
1530 input->pointer_focus = NULL;
1531 if (input->keyboard_focus == window)
1532 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001533 if (input->focus_widget &&
1534 input->focus_widget->window == window)
1535 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001536 }
1537
Rob Bradford7507b572012-05-15 17:55:34 +01001538 wl_list_for_each_safe(window_output, window_output_tmp,
1539 &window->window_output_list, link) {
1540 free (window_output);
1541 }
1542
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001543 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001544 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001545
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001546 if (window->xdg_surface)
1547 xdg_surface_destroy(window->xdg_surface);
1548 if (window->xdg_popup)
1549 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001550
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001551 if (window->ivi_surface)
1552 ivi_surface_destroy(window->ivi_surface);
1553
Pekka Paalanen4e373742013-02-13 16:17:13 +02001554 surface_destroy(window->main_surface);
1555
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001556 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001557
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001558 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001559 free(window);
1560}
1561
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001562static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001563widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001564{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001565 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001566
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001567 wl_list_for_each(child, &widget->child_list, link) {
1568 target = widget_find_widget(child, x, y);
1569 if (target)
1570 return target;
1571 }
1572
1573 if (widget->allocation.x <= x &&
1574 x < widget->allocation.x + widget->allocation.width &&
1575 widget->allocation.y <= y &&
1576 y < widget->allocation.y + widget->allocation.height) {
1577 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001578 }
1579
1580 return NULL;
1581}
1582
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001583static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001584window_find_widget(struct window *window, int32_t x, int32_t y)
1585{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001586 struct surface *surface;
1587 struct widget *widget;
1588
1589 wl_list_for_each(surface, &window->subsurface_list, link) {
1590 widget = widget_find_widget(surface->widget, x, y);
1591 if (widget)
1592 return widget;
1593 }
1594
1595 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001596}
1597
1598static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001599widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001600{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001601 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001602
Peter Huttererf3d62272013-08-08 11:57:05 +10001603 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001604 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001605 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001606 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001607 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001608 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001609 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001610 widget->tooltip = NULL;
1611 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001612 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001613 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001614
1615 return widget;
1616}
1617
1618struct widget *
1619window_add_widget(struct window *window, void *data)
1620{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001621 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001622
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001623 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001624 wl_list_init(&widget->link);
1625 window->main_surface->widget = widget;
1626
1627 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001628}
1629
1630struct widget *
1631widget_add_widget(struct widget *parent, void *data)
1632{
1633 struct widget *widget;
1634
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001635 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001636 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001637
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001638 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001639}
1640
1641void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001642widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001643{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001644 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001645 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001646 struct input *input;
1647
Pekka Paalanen35e82632013-04-25 13:57:48 +03001648 /* Destroy the sub-surface along with the root widget */
1649 if (surface->widget == widget && surface->subsurface)
1650 surface_destroy(widget->surface);
1651
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001652 if (widget->tooltip)
1653 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001654
Pekka Paalanene156fb62012-01-19 13:51:38 +02001655 wl_list_for_each(input, &display->input_list, link) {
1656 if (input->focus_widget == widget)
1657 input->focus_widget = NULL;
1658 }
1659
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001660 wl_list_remove(&widget->link);
1661 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001662}
1663
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001665widget_set_default_cursor(struct widget *widget, int cursor)
1666{
1667 widget->default_cursor = cursor;
1668}
1669
1670void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001671widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001672{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001673 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001674}
1675
1676void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001677widget_set_size(struct widget *widget, int32_t width, int32_t height)
1678{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001679 widget->allocation.width = width;
1680 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001681}
1682
1683void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001684widget_set_allocation(struct widget *widget,
1685 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001687 widget->allocation.x = x;
1688 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001689 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690}
1691
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001692void
1693widget_set_transparent(struct widget *widget, int transparent)
1694{
1695 widget->opaque = !transparent;
1696}
1697
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001698void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001699widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001700{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001701 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001702}
1703
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001704static cairo_surface_t *
1705widget_get_cairo_surface(struct widget *widget)
1706{
1707 struct surface *surface = widget->surface;
1708 struct window *window = widget->window;
1709
Neil Roberts97b747c2013-12-19 16:17:12 +00001710 assert(widget->use_cairo);
1711
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001712 if (!surface->cairo_surface) {
1713 if (surface == window->main_surface)
1714 window_create_main_surface(window);
1715 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001716 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001717 }
1718
1719 return surface->cairo_surface;
1720}
1721
Alexander Larsson15901f02013-05-22 14:41:25 +02001722static void
1723widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1724{
1725 struct surface *surface = widget->surface;
1726 double angle;
1727 cairo_matrix_t m;
1728 enum wl_output_transform transform;
1729 int surface_width, surface_height;
1730 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001731 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001732
1733 surface_width = surface->allocation.width;
1734 surface_height = surface->allocation.height;
1735
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001736 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001737 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001738
Alexander Larsson15901f02013-05-22 14:41:25 +02001739 switch (transform) {
1740 case WL_OUTPUT_TRANSFORM_FLIPPED:
1741 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1742 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1743 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1744 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1745 break;
1746 default:
1747 cairo_matrix_init_identity(&m);
1748 break;
1749 }
1750
1751 switch (transform) {
1752 case WL_OUTPUT_TRANSFORM_NORMAL:
1753 default:
1754 angle = 0;
1755 translate_x = 0;
1756 translate_y = 0;
1757 break;
1758 case WL_OUTPUT_TRANSFORM_FLIPPED:
1759 angle = 0;
1760 translate_x = surface_width;
1761 translate_y = 0;
1762 break;
1763 case WL_OUTPUT_TRANSFORM_90:
1764 angle = M_PI_2;
1765 translate_x = surface_height;
1766 translate_y = 0;
1767 break;
1768 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1769 angle = M_PI_2;
1770 translate_x = surface_height;
1771 translate_y = surface_width;
1772 break;
1773 case WL_OUTPUT_TRANSFORM_180:
1774 angle = M_PI;
1775 translate_x = surface_width;
1776 translate_y = surface_height;
1777 break;
1778 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1779 angle = M_PI;
1780 translate_x = 0;
1781 translate_y = surface_height;
1782 break;
1783 case WL_OUTPUT_TRANSFORM_270:
1784 angle = M_PI + M_PI_2;
1785 translate_x = 0;
1786 translate_y = surface_width;
1787 break;
1788 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1789 angle = M_PI + M_PI_2;
1790 translate_x = 0;
1791 translate_y = 0;
1792 break;
1793 }
1794
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001795 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001796 cairo_translate(cr, translate_x, translate_y);
1797 cairo_rotate(cr, angle);
1798 cairo_transform(cr, &m);
1799}
1800
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001801cairo_t *
1802widget_cairo_create(struct widget *widget)
1803{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001804 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001805 cairo_surface_t *cairo_surface;
1806 cairo_t *cr;
1807
1808 cairo_surface = widget_get_cairo_surface(widget);
1809 cr = cairo_create(cairo_surface);
1810
Alexander Larsson15901f02013-05-22 14:41:25 +02001811 widget_cairo_update_transform(widget, cr);
1812
Pekka Paalanen35e82632013-04-25 13:57:48 +03001813 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1814
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001815 return cr;
1816}
1817
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001818struct wl_surface *
1819widget_get_wl_surface(struct widget *widget)
1820{
1821 return widget->surface->surface;
1822}
1823
Neil Roberts5e10a042013-09-09 00:40:17 +01001824struct wl_subsurface *
1825widget_get_wl_subsurface(struct widget *widget)
1826{
1827 return widget->surface->subsurface;
1828}
1829
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001830uint32_t
1831widget_get_last_time(struct widget *widget)
1832{
1833 return widget->surface->last_time;
1834}
1835
1836void
1837widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1838{
1839 struct wl_compositor *comp = widget->window->display->compositor;
1840 struct surface *surface = widget->surface;
1841
1842 if (!surface->input_region)
1843 surface->input_region = wl_compositor_create_region(comp);
1844
1845 if (rect) {
1846 wl_region_add(surface->input_region,
1847 rect->x, rect->y, rect->width, rect->height);
1848 }
1849}
1850
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001851void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001852widget_set_resize_handler(struct widget *widget,
1853 widget_resize_handler_t handler)
1854{
1855 widget->resize_handler = handler;
1856}
1857
1858void
1859widget_set_redraw_handler(struct widget *widget,
1860 widget_redraw_handler_t handler)
1861{
1862 widget->redraw_handler = handler;
1863}
1864
1865void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001866widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001867{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001868 widget->enter_handler = handler;
1869}
1870
1871void
1872widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1873{
1874 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001875}
1876
1877void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001878widget_set_motion_handler(struct widget *widget,
1879 widget_motion_handler_t handler)
1880{
1881 widget->motion_handler = handler;
1882}
1883
1884void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001885widget_set_button_handler(struct widget *widget,
1886 widget_button_handler_t handler)
1887{
1888 widget->button_handler = handler;
1889}
1890
1891void
Rusty Lynch041815a2013-08-08 21:20:38 -07001892widget_set_touch_up_handler(struct widget *widget,
1893 widget_touch_up_handler_t handler)
1894{
1895 widget->touch_up_handler = handler;
1896}
1897
1898void
1899widget_set_touch_down_handler(struct widget *widget,
1900 widget_touch_down_handler_t handler)
1901{
1902 widget->touch_down_handler = handler;
1903}
1904
1905void
1906widget_set_touch_motion_handler(struct widget *widget,
1907 widget_touch_motion_handler_t handler)
1908{
1909 widget->touch_motion_handler = handler;
1910}
1911
1912void
1913widget_set_touch_frame_handler(struct widget *widget,
1914 widget_touch_frame_handler_t handler)
1915{
1916 widget->touch_frame_handler = handler;
1917}
1918
1919void
1920widget_set_touch_cancel_handler(struct widget *widget,
1921 widget_touch_cancel_handler_t handler)
1922{
1923 widget->touch_cancel_handler = handler;
1924}
1925
1926void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001927widget_set_axis_handler(struct widget *widget,
1928 widget_axis_handler_t handler)
1929{
1930 widget->axis_handler = handler;
1931}
1932
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001933static void
1934window_schedule_redraw_task(struct window *window);
1935
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001936void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001937widget_schedule_redraw(struct widget *widget)
1938{
Pekka Paalanen71233882013-04-25 13:57:53 +03001939 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001940 widget->surface->redraw_needed = 1;
1941 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001942}
1943
Neil Roberts97b747c2013-12-19 16:17:12 +00001944void
1945widget_set_use_cairo(struct widget *widget,
1946 int use_cairo)
1947{
1948 widget->use_cairo = use_cairo;
1949}
1950
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001951cairo_surface_t *
1952window_get_surface(struct window *window)
1953{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001954 cairo_surface_t *cairo_surface;
1955
1956 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1957
1958 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001959}
1960
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001961struct wl_surface *
1962window_get_wl_surface(struct window *window)
1963{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001964 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001965}
1966
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001967static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001968tooltip_redraw_handler(struct widget *widget, void *data)
1969{
1970 cairo_t *cr;
1971 const int32_t r = 3;
1972 struct tooltip *tooltip = data;
1973 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001974
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001975 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001976 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001977 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1978 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1979 cairo_paint(cr);
1980
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001981 width = widget->allocation.width;
1982 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001983 rounded_rect(cr, 0, 0, width, height, r);
1984
1985 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1986 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1987 cairo_fill(cr);
1988
1989 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1990 cairo_move_to(cr, 10, 16);
1991 cairo_show_text(cr, tooltip->entry);
1992 cairo_destroy(cr);
1993}
1994
1995static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001996get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001997{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001998 cairo_t *cr;
1999 cairo_text_extents_t extents;
2000
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002001 /* Use the dummy_surface because tooltip's surface was not
2002 * created yet, and parent does not have a valid surface
2003 * outside repaint, either.
2004 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002005 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002006 cairo_text_extents(cr, tooltip->entry, &extents);
2007 cairo_destroy(cr);
2008
2009 return extents;
2010}
2011
2012static int
2013window_create_tooltip(struct tooltip *tooltip)
2014{
2015 struct widget *parent = tooltip->parent;
2016 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002017 const int offset_y = 27;
2018 const int margin = 3;
2019 cairo_text_extents_t extents;
2020
2021 if (tooltip->widget)
2022 return 0;
2023
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002024 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002025
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002026 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002027 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002028 widget_set_allocation(tooltip->widget,
2029 tooltip->x, tooltip->y + offset_y,
2030 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002031
2032 return 0;
2033}
2034
2035void
2036widget_destroy_tooltip(struct widget *parent)
2037{
2038 struct tooltip *tooltip = parent->tooltip;
2039
2040 parent->tooltip_count = 0;
2041 if (!tooltip)
2042 return;
2043
2044 if (tooltip->widget) {
2045 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002046 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002047 }
2048
2049 close(tooltip->tooltip_fd);
2050 free(tooltip->entry);
2051 free(tooltip);
2052 parent->tooltip = NULL;
2053}
2054
2055static void
2056tooltip_func(struct task *task, uint32_t events)
2057{
2058 struct tooltip *tooltip =
2059 container_of(task, struct tooltip, tooltip_task);
2060 uint64_t exp;
2061
Martin Olsson8df662a2012-07-08 03:03:47 +02002062 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2063 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002064 window_create_tooltip(tooltip);
2065}
2066
2067#define TOOLTIP_TIMEOUT 500
2068static int
2069tooltip_timer_reset(struct tooltip *tooltip)
2070{
2071 struct itimerspec its;
2072
2073 its.it_interval.tv_sec = 0;
2074 its.it_interval.tv_nsec = 0;
2075 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2076 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2077 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2078 fprintf(stderr, "could not set timerfd\n: %m");
2079 return -1;
2080 }
2081
2082 return 0;
2083}
2084
2085int
2086widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2087{
2088 struct tooltip *tooltip = parent->tooltip;
2089
2090 parent->tooltip_count++;
2091 if (tooltip) {
2092 tooltip->x = x;
2093 tooltip->y = y;
2094 tooltip_timer_reset(tooltip);
2095 return 0;
2096 }
2097
2098 /* the handler might be triggered too fast via input device motion, so
2099 * we need this check here to make sure tooltip is fully initialized */
2100 if (parent->tooltip_count > 1)
2101 return 0;
2102
2103 tooltip = malloc(sizeof *tooltip);
2104 if (!tooltip)
2105 return -1;
2106
2107 parent->tooltip = tooltip;
2108 tooltip->parent = parent;
2109 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002110 tooltip->x = x;
2111 tooltip->y = y;
2112 tooltip->entry = strdup(entry);
2113 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2114 if (tooltip->tooltip_fd < 0) {
2115 fprintf(stderr, "could not create timerfd\n: %m");
2116 return -1;
2117 }
2118
2119 tooltip->tooltip_task.run = tooltip_func;
2120 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2121 EPOLLIN, &tooltip->tooltip_task);
2122 tooltip_timer_reset(tooltip);
2123
2124 return 0;
2125}
2126
2127static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002128workspace_manager_state(void *data,
2129 struct workspace_manager *workspace_manager,
2130 uint32_t current,
2131 uint32_t count)
2132{
2133 struct display *display = data;
2134
2135 display->workspace = current;
2136 display->workspace_count = count;
2137}
2138
2139static const struct workspace_manager_listener workspace_manager_listener = {
2140 workspace_manager_state
2141};
2142
2143static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002144frame_resize_handler(struct widget *widget,
2145 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002146{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002147 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002148 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002149 struct rectangle interior;
2150 struct rectangle input;
2151 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002152
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002153 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002154 interior.x = 0;
2155 interior.y = 0;
2156 interior.width = width;
2157 interior.height = height;
2158 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002159 frame_resize(frame->frame, width, height);
2160 frame_interior(frame->frame, &interior.x, &interior.y,
2161 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002162 }
2163
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002164 widget_set_allocation(child, interior.x, interior.y,
2165 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002166
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002167 if (child->resize_handler) {
2168 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002169 child->user_data);
2170
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002171 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 width = child->allocation.width;
2173 height = child->allocation.height;
2174 } else {
2175 frame_resize_inside(frame->frame,
2176 child->allocation.width,
2177 child->allocation.height);
2178 width = frame_width(frame->frame);
2179 height = frame_height(frame->frame);
2180 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002181 }
2182
Scott Moreauf7e498c2012-05-14 11:39:29 -06002183 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002184
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002185 widget->surface->input_region =
2186 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002187 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002188 frame_input_rect(frame->frame, &input.x, &input.y,
2189 &input.width, &input.height);
2190 wl_region_add(widget->surface->input_region,
2191 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002192 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002194 }
2195
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002196 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002197
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002199 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002200 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2201 &opaque.width, &opaque.height);
2202
2203 wl_region_add(widget->surface->opaque_region,
2204 opaque.x, opaque.y,
2205 opaque.width, opaque.height);
2206 } else {
2207 wl_region_add(widget->surface->opaque_region,
2208 0, 0, width, height);
2209 }
Martin Minarik1998b152012-05-10 02:04:35 +02002210 }
2211
Martin Minarik1998b152012-05-10 02:04:35 +02002212
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002213 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002214}
2215
2216static void
2217frame_redraw_handler(struct widget *widget, void *data)
2218{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002220 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002221 struct window *window = widget->window;
2222
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002223 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002224 return;
2225
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002226 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002227
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002228 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002229
2230 cairo_destroy(cr);
2231}
2232
2233static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002234frame_get_pointer_image_for_location(struct window_frame *frame,
2235 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002236{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002237 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002238
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002239 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002240 return CURSOR_LEFT_PTR;
2241
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002242 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002243 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002244 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002245 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002246 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002247 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002248 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002249 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002250 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002251 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002252 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002253 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002254 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002255 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002256 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002257 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002258 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002259 case THEME_LOCATION_EXTERIOR:
2260 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002261 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002262 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002263 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002264}
2265
Pekka Paalanen26237862014-09-10 15:10:30 +03002266static void
2267frame_menu_func(void *data, struct input *input, int index)
2268{
2269 struct window *window = data;
2270 struct display *display;
2271
2272 switch (index) {
2273 case 0: /* close */
2274 window_close(window);
2275 break;
2276 case 1: /* move to workspace above */
2277 display = window->display;
2278 if (display->workspace > 0)
2279 workspace_manager_move_surface(
2280 display->workspace_manager,
2281 window->main_surface->surface,
2282 display->workspace - 1);
2283 break;
2284 case 2: /* move to workspace below */
2285 display = window->display;
2286 if (display->workspace < display->workspace_count - 1)
2287 workspace_manager_move_surface(
2288 display->workspace_manager,
2289 window->main_surface->surface,
2290 display->workspace + 1);
2291 break;
2292 case 3: /* fullscreen */
2293 /* we don't have a way to get out of fullscreen for now */
2294 if (window->fullscreen_handler)
2295 window->fullscreen_handler(window, window->user_data);
2296 break;
2297 }
2298}
2299
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002300void
2301window_show_frame_menu(struct window *window,
2302 struct input *input, uint32_t time)
2303{
2304 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002305 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002306
Pekka Paalanen26237862014-09-10 15:10:30 +03002307 static const char *entries[] = {
2308 "Close",
2309 "Move to workspace above", "Move to workspace below",
2310 "Fullscreen"
2311 };
2312
2313 if (window->fullscreen_handler)
2314 count = ARRAY_LENGTH(entries);
2315 else
2316 count = ARRAY_LENGTH(entries) - 1;
2317
2318 input_get_position(input, &x, &y);
2319 window_show_menu(window->display, input, time, window,
2320 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002321}
2322
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002323static int
2324frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002325 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002326{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002327 struct window_frame *frame = data;
2328 enum theme_location location;
2329
2330 location = frame_pointer_enter(frame->frame, input, x, y);
2331 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2332 widget_schedule_redraw(frame->widget);
2333
2334 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002335}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002336
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002337static int
2338frame_motion_handler(struct widget *widget,
2339 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002340 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002342 struct window_frame *frame = data;
2343 enum theme_location location;
2344
2345 location = frame_pointer_motion(frame->frame, input, x, y);
2346 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2347 widget_schedule_redraw(frame->widget);
2348
2349 return frame_get_pointer_image_for_location(data, location);
2350}
2351
2352static void
2353frame_leave_handler(struct widget *widget,
2354 struct input *input, void *data)
2355{
2356 struct window_frame *frame = data;
2357
2358 frame_pointer_leave(frame->frame, input);
2359 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2360 widget_schedule_redraw(frame->widget);
2361}
2362
2363static void
2364frame_handle_status(struct window_frame *frame, struct input *input,
2365 uint32_t time, enum theme_location location)
2366{
2367 struct window *window = frame->widget->window;
2368 uint32_t status;
2369
2370 status = frame_status(frame->frame);
2371 if (status & FRAME_STATUS_REPAINT)
2372 widget_schedule_redraw(frame->widget);
2373
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002374 if (status & FRAME_STATUS_MINIMIZE) {
2375 window_set_minimized(window);
2376 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2377 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002378
2379 if (status & FRAME_STATUS_MENU) {
2380 window_show_frame_menu(window, input, time);
2381 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2382 }
2383
2384 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002385 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002386 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2387 }
2388
2389 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002390 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002391 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002392 }
2393
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002394 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002395 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002396 xdg_surface_move(window->xdg_surface,
2397 input_get_seat(input),
2398 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002399
2400 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2401 }
2402
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002403 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002404 input_ungrab(input);
2405
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002406 xdg_surface_resize(window->xdg_surface,
2407 input_get_seat(input),
2408 window->display->serial,
2409 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002410
2411 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2412 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002413}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002414
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002415#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002416static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002417frame_button_handler(struct widget *widget,
2418 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002419 uint32_t button, enum wl_pointer_button_state state,
2420 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421
2422{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002423 struct window_frame *frame = data;
2424 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002425
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002426 frame->double_click = 0;
2427 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2428 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2429 frame->double_click = 1;
2430 frame->did_double = 1;
2431 } else
2432 frame->did_double = 0;
2433
2434 frame->last_time = time;
2435 } else if (frame->did_double == 1) {
2436 frame->double_click = 1;
2437 frame->did_double = 0;
2438 }
2439
2440 if (frame->double_click)
2441 location = frame_double_click(frame->frame, input,
2442 button, state);
2443 else
2444 location = frame_pointer_button(frame->frame, input,
2445 button, state);
2446
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002447 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002448}
2449
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002450static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002451frame_touch_down_handler(struct widget *widget, struct input *input,
2452 uint32_t serial, uint32_t time, int32_t id,
2453 float x, float y, void *data)
2454{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002455 struct window_frame *frame = data;
2456
Xiong Zhang382de462014-06-12 11:06:26 +08002457 frame->double_click = 0;
2458 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2459 frame->last_id == id) {
2460 frame->double_click = 1;
2461 frame->did_double = 1;
2462 frame->double_id = id;
2463 } else
2464 frame->did_double = 0;
2465
2466 frame->last_time = time;
2467 frame->last_id = id;
2468
2469 if (frame->double_click)
2470 frame_double_touch_down(frame->frame, input, id, x, y);
2471 else
2472 frame_touch_down(frame->frame, input, id, x, y);
2473
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002474 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2475}
2476
2477static void
2478frame_touch_up_handler(struct widget *widget,
2479 struct input *input, uint32_t serial, uint32_t time,
2480 int32_t id, void *data)
2481{
2482 struct window_frame *frame = data;
2483
Xiong Zhang382de462014-06-12 11:06:26 +08002484 if (frame->double_id == id && frame->did_double) {
2485 frame->did_double = 0;
2486 frame->double_id = 0;
2487 frame_double_touch_up(frame->frame, input, id);
2488 } else
2489 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002490 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002491}
2492
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002493struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002494window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002495{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002496 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002497 uint32_t buttons;
2498
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002499 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002500 buttons = FRAME_BUTTON_NONE;
2501 } else {
2502 buttons = FRAME_BUTTON_ALL;
2503 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002504
Peter Huttererf3d62272013-08-08 11:57:05 +10002505 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002506 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002507 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002508
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002509 frame->widget = window_add_widget(window, frame);
2510 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002511
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002512 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2513 widget_set_resize_handler(frame->widget, frame_resize_handler);
2514 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002515 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002516 widget_set_motion_handler(frame->widget, frame_motion_handler);
2517 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002518 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002519 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002520
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002521 window->frame = frame;
2522
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002523 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002524}
2525
Kristian Høgsberga1627922012-06-20 17:30:03 -04002526void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002527window_frame_set_child_size(struct widget *widget, int child_width,
2528 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002529{
2530 struct display *display = widget->window->display;
2531 struct theme *t = display->theme;
2532 int decoration_width, decoration_height;
2533 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002534 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002535
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002536 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002537 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002538 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002539 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002540
2541 width = child_width + decoration_width;
2542 height = child_height + decoration_height;
2543 } else {
2544 width = child_width;
2545 height = child_height;
2546 }
2547
2548 window_schedule_resize(widget->window, width, height);
2549}
2550
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002551static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002552window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002553{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002554 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002555
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002556 /* frame->child must be destroyed by the application */
2557 widget_destroy(frame->widget);
2558 free(frame);
2559}
2560
2561static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002562input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002563 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002564{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002565 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002566 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002567
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002568 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002569 return;
2570
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002571 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002572 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002573 widget = old;
2574 if (input->grab)
2575 widget = input->grab;
2576 if (widget->leave_handler)
2577 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002578 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002579 }
2580
2581 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002582 widget = focus;
2583 if (input->grab)
2584 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002585 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002586 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002587 cursor = widget->enter_handler(focus, input, x, y,
2588 widget->user_data);
2589 else
2590 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002591
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002592 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002593 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002594}
2595
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002596void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002597touch_grab(struct input *input, int32_t touch_id)
2598{
2599 input->touch_grab = 1;
2600 input->touch_grab_id = touch_id;
2601}
2602
2603void
2604touch_ungrab(struct input *input)
2605{
2606 struct touch_point *tp, *tmp;
2607
2608 input->touch_grab = 0;
2609
2610 wl_list_for_each_safe(tp, tmp,
2611 &input->touch_point_list, link) {
2612 if (tp->id != input->touch_grab_id)
2613 continue;
2614 wl_list_remove(&tp->link);
2615 free(tp);
2616
2617 return;
2618 }
2619}
2620
2621void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002622input_grab(struct input *input, struct widget *widget, uint32_t button)
2623{
2624 input->grab = widget;
2625 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002626
2627 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002628}
2629
2630void
2631input_ungrab(struct input *input)
2632{
2633 struct widget *widget;
2634
2635 input->grab = NULL;
2636 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002637 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002638 input->sx, input->sy);
2639 input_set_focus_widget(input, widget, input->sx, input->sy);
2640 }
2641}
2642
2643static void
2644input_remove_pointer_focus(struct input *input)
2645{
2646 struct window *window = input->pointer_focus;
2647
2648 if (!window)
2649 return;
2650
2651 input_set_focus_widget(input, NULL, 0, 0);
2652
2653 input->pointer_focus = NULL;
2654 input->current_cursor = CURSOR_UNSET;
2655}
2656
2657static void
2658pointer_handle_enter(void *data, struct wl_pointer *pointer,
2659 uint32_t serial, struct wl_surface *surface,
2660 wl_fixed_t sx_w, wl_fixed_t sy_w)
2661{
2662 struct input *input = data;
2663 struct window *window;
2664 struct widget *widget;
2665 float sx = wl_fixed_to_double(sx_w);
2666 float sy = wl_fixed_to_double(sy_w);
2667
2668 if (!surface) {
2669 /* enter event for a window we've just destroyed */
2670 return;
2671 }
2672
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002673 window = wl_surface_get_user_data(surface);
2674 if (surface != window->main_surface->surface) {
2675 DBG("Ignoring input event from subsurface %p\n", surface);
2676 return;
2677 }
2678
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002679 input->display->serial = serial;
2680 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002681 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002682
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002683 input->sx = sx;
2684 input->sy = sy;
2685
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002686 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002687 input_set_focus_widget(input, widget, sx, sy);
2688}
2689
2690static void
2691pointer_handle_leave(void *data, struct wl_pointer *pointer,
2692 uint32_t serial, struct wl_surface *surface)
2693{
2694 struct input *input = data;
2695
2696 input->display->serial = serial;
2697 input_remove_pointer_focus(input);
2698}
2699
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002700static void
Daniel Stone37816df2012-05-16 18:45:18 +01002701pointer_handle_motion(void *data, struct wl_pointer *pointer,
2702 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002703{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002704 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002705 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002706 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002707 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002708 float sx = wl_fixed_to_double(sx_w);
2709 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002710
Paul Winwoodb22bf572013-08-29 10:52:54 +01002711 if (!window)
2712 return;
2713
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002714 input->sx = sx;
2715 input->sy = sy;
2716
Rob Bradford5f087742013-07-11 19:41:27 +01002717 /* when making the window smaller - e.g. after a unmaximise we might
2718 * still have a pending motion event that the compositor has picked
2719 * based on the old surface dimensions
2720 */
2721 if (sx > window->main_surface->allocation.width ||
2722 sy > window->main_surface->allocation.height)
2723 return;
2724
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002725 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002726 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002727 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002728 }
2729
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002730 if (input->grab)
2731 widget = input->grab;
2732 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002733 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002734 if (widget) {
2735 if (widget->motion_handler)
2736 cursor = widget->motion_handler(input->focus_widget,
2737 input, time, sx, sy,
2738 widget->user_data);
2739 else
2740 cursor = widget->default_cursor;
2741 } else
2742 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002743
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002744 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002745}
2746
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002747static void
Daniel Stone37816df2012-05-16 18:45:18 +01002748pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002749 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002750{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002751 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002752 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002753 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002754
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002755 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002756 if (input->focus_widget && input->grab == NULL &&
2757 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002758 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002759
Neil Roberts6b28aad2012-01-23 19:11:18 +00002760 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002761 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002762 (*widget->button_handler)(widget,
2763 input, time,
2764 button, state,
2765 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002766
Daniel Stone4dbadb12012-05-30 16:31:51 +01002767 if (input->grab && input->grab_button == button &&
2768 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002769 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002770}
2771
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002772static void
Daniel Stone37816df2012-05-16 18:45:18 +01002773pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002774 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002775{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002776 struct input *input = data;
2777 struct widget *widget;
2778
2779 widget = input->focus_widget;
2780 if (input->grab)
2781 widget = input->grab;
2782 if (widget && widget->axis_handler)
2783 (*widget->axis_handler)(widget,
2784 input, time,
2785 axis, value,
2786 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002787}
2788
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002789static const struct wl_pointer_listener pointer_listener = {
2790 pointer_handle_enter,
2791 pointer_handle_leave,
2792 pointer_handle_motion,
2793 pointer_handle_button,
2794 pointer_handle_axis,
2795};
2796
2797static void
2798input_remove_keyboard_focus(struct input *input)
2799{
2800 struct window *window = input->keyboard_focus;
2801 struct itimerspec its;
2802
2803 its.it_interval.tv_sec = 0;
2804 its.it_interval.tv_nsec = 0;
2805 its.it_value.tv_sec = 0;
2806 its.it_value.tv_nsec = 0;
2807 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2808
2809 if (!window)
2810 return;
2811
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002812 if (window->keyboard_focus_handler)
2813 (*window->keyboard_focus_handler)(window, NULL,
2814 window->user_data);
2815
2816 input->keyboard_focus = NULL;
2817}
2818
2819static void
2820keyboard_repeat_func(struct task *task, uint32_t events)
2821{
2822 struct input *input =
2823 container_of(task, struct input, repeat_task);
2824 struct window *window = input->keyboard_focus;
2825 uint64_t exp;
2826
2827 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2828 /* If we change the timer between the fd becoming
2829 * readable and getting here, there'll be nothing to
2830 * read and we get EAGAIN. */
2831 return;
2832
2833 if (window && window->key_handler) {
2834 (*window->key_handler)(window, input, input->repeat_time,
2835 input->repeat_key, input->repeat_sym,
2836 WL_KEYBOARD_KEY_STATE_PRESSED,
2837 window->user_data);
2838 }
2839}
2840
2841static void
2842keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2843 uint32_t format, int fd, uint32_t size)
2844{
2845 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002846 struct xkb_keymap *keymap;
2847 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002848 char *map_str;
2849
2850 if (!data) {
2851 close(fd);
2852 return;
2853 }
2854
2855 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2856 close(fd);
2857 return;
2858 }
2859
2860 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2861 if (map_str == MAP_FAILED) {
2862 close(fd);
2863 return;
2864 }
2865
Ran Benita2e1968f2014-08-19 23:59:51 +03002866 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2867 map_str,
2868 XKB_KEYMAP_FORMAT_TEXT_V1,
2869 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002870 munmap(map_str, size);
2871 close(fd);
2872
Rui Matos3eccb862013-10-10 19:44:22 +02002873 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002874 fprintf(stderr, "failed to compile keymap\n");
2875 return;
2876 }
2877
Rui Matos3eccb862013-10-10 19:44:22 +02002878 state = xkb_state_new(keymap);
2879 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002880 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002881 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002882 return;
2883 }
2884
Rui Matos3eccb862013-10-10 19:44:22 +02002885 xkb_keymap_unref(input->xkb.keymap);
2886 xkb_state_unref(input->xkb.state);
2887 input->xkb.keymap = keymap;
2888 input->xkb.state = state;
2889
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002890 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002891 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002892 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002893 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002894 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002895 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002896}
2897
2898static void
2899keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2900 uint32_t serial, struct wl_surface *surface,
2901 struct wl_array *keys)
2902{
2903 struct input *input = data;
2904 struct window *window;
2905
2906 input->display->serial = serial;
2907 input->keyboard_focus = wl_surface_get_user_data(surface);
2908
2909 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002910 if (window->keyboard_focus_handler)
2911 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002912 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002913}
2914
2915static void
2916keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2917 uint32_t serial, struct wl_surface *surface)
2918{
2919 struct input *input = data;
2920
2921 input->display->serial = serial;
2922 input_remove_keyboard_focus(input);
2923}
2924
Scott Moreau210d0792012-03-22 10:47:01 -06002925static void
Daniel Stone37816df2012-05-16 18:45:18 +01002926keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002927 uint32_t serial, uint32_t time, uint32_t key,
2928 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002929{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002930 struct input *input = data;
2931 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002932 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002933 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002934 const xkb_keysym_t *syms;
2935 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002936 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002937
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002938 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002939 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002940 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002941 return;
2942
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002943 /* We only use input grabs for pointer events for now, so just
2944 * ignore key presses if a grab is active. We expand the key
2945 * event delivery mechanism to route events to widgets to
2946 * properly handle key grabs. In the meantime, this prevents
2947 * key event devlivery while a grab is active. */
2948 if (input->grab && input->grab_button == 0)
2949 return;
2950
Ran Benita2e1968f2014-08-19 23:59:51 +03002951 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002952
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002953 sym = XKB_KEY_NoSymbol;
2954 if (num_syms == 1)
2955 sym = syms[0];
2956
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002957
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002958 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002959 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002960 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002961 } else if (sym == XKB_KEY_F11 &&
2962 window->fullscreen_handler &&
2963 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2964 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002965 } else if (sym == XKB_KEY_F4 &&
2966 input->modifiers == MOD_ALT_MASK &&
2967 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002968 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002969 } else if (window->key_handler) {
2970 (*window->key_handler)(window, input, time, key,
2971 sym, state, window->user_data);
2972 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002973
2974 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2975 key == input->repeat_key) {
2976 its.it_interval.tv_sec = 0;
2977 its.it_interval.tv_nsec = 0;
2978 its.it_value.tv_sec = 0;
2979 its.it_value.tv_nsec = 0;
2980 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002981 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2982 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002983 input->repeat_sym = sym;
2984 input->repeat_key = key;
2985 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002986 its.it_interval.tv_sec = input->repeat_rate_sec;
2987 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2988 its.it_value.tv_sec = input->repeat_delay_sec;
2989 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002990 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2991 }
2992}
2993
2994static void
Daniel Stone351eb612012-05-31 15:27:47 -04002995keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2996 uint32_t serial, uint32_t mods_depressed,
2997 uint32_t mods_latched, uint32_t mods_locked,
2998 uint32_t group)
2999{
3000 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003001 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003002
Matt Ropere61561f2013-06-24 16:52:43 +01003003 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3004 if (!input->xkb.keymap)
3005 return;
3006
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003007 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3008 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003009 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003010 XKB_STATE_MODS_DEPRESSED |
3011 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003012 input->modifiers = 0;
3013 if (mask & input->xkb.control_mask)
3014 input->modifiers |= MOD_CONTROL_MASK;
3015 if (mask & input->xkb.alt_mask)
3016 input->modifiers |= MOD_ALT_MASK;
3017 if (mask & input->xkb.shift_mask)
3018 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003019}
3020
Jonny Lamb06959082014-08-12 14:58:27 +02003021static void
3022set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3023{
3024 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3025 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3026
3027 /* a rate of zero disables any repeating, regardless of the delay's
3028 * value */
3029 if (rate == 0)
3030 return;
3031
3032 if (rate == 1)
3033 input->repeat_rate_sec = 1;
3034 else
3035 input->repeat_rate_nsec = 1000000000 / rate;
3036
3037 input->repeat_delay_sec = delay / 1000;
3038 delay -= (input->repeat_delay_sec * 1000);
3039 input->repeat_delay_nsec = delay * 1000 * 1000;
3040}
3041
3042static void
3043keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3044 int32_t rate, int32_t delay)
3045{
3046 struct input *input = data;
3047
3048 set_repeat_info(input, rate, delay);
3049}
3050
Daniel Stone37816df2012-05-16 18:45:18 +01003051static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003052 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003053 keyboard_handle_enter,
3054 keyboard_handle_leave,
3055 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003056 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003057 keyboard_handle_repeat_info
3058
Daniel Stone37816df2012-05-16 18:45:18 +01003059};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003060
3061static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003062touch_handle_down(void *data, struct wl_touch *wl_touch,
3063 uint32_t serial, uint32_t time, struct wl_surface *surface,
3064 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3065{
3066 struct input *input = data;
3067 struct widget *widget;
3068 float sx = wl_fixed_to_double(x_w);
3069 float sy = wl_fixed_to_double(y_w);
3070
Rusty Lynch1084da52013-08-15 09:10:08 -07003071 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003072 input->touch_focus = wl_surface_get_user_data(surface);
3073 if (!input->touch_focus) {
3074 DBG("Failed to find to touch focus for surface %p\n", surface);
3075 return;
3076 }
3077
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003078 if (surface != input->touch_focus->main_surface->surface) {
3079 DBG("Ignoring input event from subsurface %p\n", surface);
3080 input->touch_focus = NULL;
3081 return;
3082 }
3083
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003084 if (input->grab)
3085 widget = input->grab;
3086 else
3087 widget = window_find_widget(input->touch_focus,
3088 wl_fixed_to_double(x_w),
3089 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003090 if (widget) {
3091 struct touch_point *tp = xmalloc(sizeof *tp);
3092 if (tp) {
3093 tp->id = id;
3094 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003095 tp->x = sx;
3096 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003097 wl_list_insert(&input->touch_point_list, &tp->link);
3098
3099 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003100 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003101 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003102 sx, sy,
3103 widget->user_data);
3104 }
3105 }
3106}
3107
3108static void
3109touch_handle_up(void *data, struct wl_touch *wl_touch,
3110 uint32_t serial, uint32_t time, int32_t id)
3111{
3112 struct input *input = data;
3113 struct touch_point *tp, *tmp;
3114
Rusty Lynch041815a2013-08-08 21:20:38 -07003115 if (!input->touch_focus) {
3116 DBG("No touch focus found for touch up event!\n");
3117 return;
3118 }
3119
3120 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3121 if (tp->id != id)
3122 continue;
3123
3124 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003125 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003126 time, id,
3127 tp->widget->user_data);
3128
3129 wl_list_remove(&tp->link);
3130 free(tp);
3131
3132 return;
3133 }
3134}
3135
3136static void
3137touch_handle_motion(void *data, struct wl_touch *wl_touch,
3138 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3139{
3140 struct input *input = data;
3141 struct touch_point *tp;
3142 float sx = wl_fixed_to_double(x_w);
3143 float sy = wl_fixed_to_double(y_w);
3144
3145 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3146
3147 if (!input->touch_focus) {
3148 DBG("No touch focus found for touch motion event!\n");
3149 return;
3150 }
3151
3152 wl_list_for_each(tp, &input->touch_point_list, link) {
3153 if (tp->id != id)
3154 continue;
3155
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003156 tp->x = sx;
3157 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003158 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003159 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003160 id, sx, sy,
3161 tp->widget->user_data);
3162 return;
3163 }
3164}
3165
3166static void
3167touch_handle_frame(void *data, struct wl_touch *wl_touch)
3168{
3169 struct input *input = data;
3170 struct touch_point *tp, *tmp;
3171
3172 DBG("touch_handle_frame\n");
3173
3174 if (!input->touch_focus) {
3175 DBG("No touch focus found for touch frame event!\n");
3176 return;
3177 }
3178
3179 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3180 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003181 (*tp->widget->touch_frame_handler)(tp->widget, input,
3182 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003183 }
3184}
3185
3186static void
3187touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3188{
3189 struct input *input = data;
3190 struct touch_point *tp, *tmp;
3191
3192 DBG("touch_handle_cancel\n");
3193
3194 if (!input->touch_focus) {
3195 DBG("No touch focus found for touch cancel event!\n");
3196 return;
3197 }
3198
3199 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3200 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003201 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3202 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003203
3204 wl_list_remove(&tp->link);
3205 free(tp);
3206 }
3207}
3208
3209static const struct wl_touch_listener touch_listener = {
3210 touch_handle_down,
3211 touch_handle_up,
3212 touch_handle_motion,
3213 touch_handle_frame,
3214 touch_handle_cancel,
3215};
3216
3217static void
Daniel Stone37816df2012-05-16 18:45:18 +01003218seat_handle_capabilities(void *data, struct wl_seat *seat,
3219 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003220{
Daniel Stone37816df2012-05-16 18:45:18 +01003221 struct input *input = data;
3222
3223 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3224 input->pointer = wl_seat_get_pointer(seat);
3225 wl_pointer_set_user_data(input->pointer, input);
3226 wl_pointer_add_listener(input->pointer, &pointer_listener,
3227 input);
3228 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3229 wl_pointer_destroy(input->pointer);
3230 input->pointer = NULL;
3231 }
3232
3233 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3234 input->keyboard = wl_seat_get_keyboard(seat);
3235 wl_keyboard_set_user_data(input->keyboard, input);
3236 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3237 input);
3238 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3239 wl_keyboard_destroy(input->keyboard);
3240 input->keyboard = NULL;
3241 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003242
3243 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3244 input->touch = wl_seat_get_touch(seat);
3245 wl_touch_set_user_data(input->touch, input);
3246 wl_touch_add_listener(input->touch, &touch_listener, input);
3247 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3248 wl_touch_destroy(input->touch);
3249 input->touch = NULL;
3250 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003251}
3252
Rob Bradford08031182013-08-13 20:11:03 +01003253static void
3254seat_handle_name(void *data, struct wl_seat *seat,
3255 const char *name)
3256{
3257
3258}
3259
Daniel Stone37816df2012-05-16 18:45:18 +01003260static const struct wl_seat_listener seat_listener = {
3261 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003262 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003263};
3264
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003265void
3266input_get_position(struct input *input, int32_t *x, int32_t *y)
3267{
3268 *x = input->sx;
3269 *y = input->sy;
3270}
3271
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003272int
3273input_get_touch(struct input *input, int32_t id, float *x, float *y)
3274{
3275 struct touch_point *tp;
3276
3277 wl_list_for_each(tp, &input->touch_point_list, link) {
3278 if (tp->id != id)
3279 continue;
3280
3281 *x = tp->x;
3282 *y = tp->y;
3283 return 0;
3284 }
3285
3286 return -1;
3287}
3288
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003289struct display *
3290input_get_display(struct input *input)
3291{
3292 return input->display;
3293}
3294
Daniel Stone37816df2012-05-16 18:45:18 +01003295struct wl_seat *
3296input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003297{
Daniel Stone37816df2012-05-16 18:45:18 +01003298 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003299}
3300
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003301uint32_t
3302input_get_modifiers(struct input *input)
3303{
3304 return input->modifiers;
3305}
3306
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003307struct widget *
3308input_get_focus_widget(struct input *input)
3309{
3310 return input->focus_widget;
3311}
3312
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003313struct data_offer {
3314 struct wl_data_offer *offer;
3315 struct input *input;
3316 struct wl_array types;
3317 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003318
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003319 struct task io_task;
3320 int fd;
3321 data_func_t func;
3322 int32_t x, y;
3323 void *user_data;
3324};
3325
3326static void
3327data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3328{
3329 struct data_offer *offer = data;
3330 char **p;
3331
3332 p = wl_array_add(&offer->types, sizeof *p);
3333 *p = strdup(type);
3334}
3335
3336static const struct wl_data_offer_listener data_offer_listener = {
3337 data_offer_offer,
3338};
3339
3340static void
3341data_offer_destroy(struct data_offer *offer)
3342{
3343 char **p;
3344
3345 offer->refcount--;
3346 if (offer->refcount == 0) {
3347 wl_data_offer_destroy(offer->offer);
3348 for (p = offer->types.data; *p; p++)
3349 free(*p);
3350 wl_array_release(&offer->types);
3351 free(offer);
3352 }
3353}
3354
3355static void
3356data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003357 struct wl_data_device *data_device,
3358 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003359{
3360 struct data_offer *offer;
3361
Brian Lovinbc919262013-08-07 15:34:59 -07003362 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003363
3364 wl_array_init(&offer->types);
3365 offer->refcount = 1;
3366 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003367 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003368 wl_data_offer_add_listener(offer->offer,
3369 &data_offer_listener, offer);
3370}
3371
3372static void
3373data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003374 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003375 wl_fixed_t x_w, wl_fixed_t y_w,
3376 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003377{
3378 struct input *input = data;
3379 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003380 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003381 float x = wl_fixed_to_double(x_w);
3382 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003383 char **p;
3384
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003385 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003386 input->drag_enter_serial = serial;
3387 input->drag_focus = window,
3388 input->drag_x = x;
3389 input->drag_y = y;
3390
3391 if (!input->touch_grab)
3392 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003393
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003394 if (offer) {
3395 input->drag_offer = wl_data_offer_get_user_data(offer);
3396
3397 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3398 *p = NULL;
3399
3400 types_data = input->drag_offer->types.data;
3401 } else {
3402 input->drag_offer = NULL;
3403 types_data = NULL;
3404 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003405
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003406 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003407 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003408 window->user_data);
3409}
3410
3411static void
3412data_device_leave(void *data, struct wl_data_device *data_device)
3413{
3414 struct input *input = data;
3415
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003416 if (input->drag_offer) {
3417 data_offer_destroy(input->drag_offer);
3418 input->drag_offer = NULL;
3419 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003420}
3421
3422static void
3423data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003424 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003425{
3426 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003427 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003428 float x = wl_fixed_to_double(x_w);
3429 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003430 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003431
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003432 input->drag_x = x;
3433 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003434
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003435 if (input->drag_offer)
3436 types_data = input->drag_offer->types.data;
3437 else
3438 types_data = NULL;
3439
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003440 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003441 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003442 window->user_data);
3443}
3444
3445static void
3446data_device_drop(void *data, struct wl_data_device *data_device)
3447{
3448 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003449 struct window *window = input->drag_focus;
3450 float x, y;
3451
3452 x = input->drag_x;
3453 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003454
3455 if (window->drop_handler)
3456 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003457 x, y, window->user_data);
3458
3459 if (input->touch_grab)
3460 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003461}
3462
3463static void
3464data_device_selection(void *data,
3465 struct wl_data_device *wl_data_device,
3466 struct wl_data_offer *offer)
3467{
3468 struct input *input = data;
3469 char **p;
3470
3471 if (input->selection_offer)
3472 data_offer_destroy(input->selection_offer);
3473
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003474 if (offer) {
3475 input->selection_offer = wl_data_offer_get_user_data(offer);
3476 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3477 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003478 } else {
3479 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003480 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003481}
3482
3483static const struct wl_data_device_listener data_device_listener = {
3484 data_device_data_offer,
3485 data_device_enter,
3486 data_device_leave,
3487 data_device_motion,
3488 data_device_drop,
3489 data_device_selection
3490};
3491
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003492static void
3493input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003494{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003495 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003496 struct wl_cursor *cursor;
3497 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003498
Daniel Stone80972742012-11-07 17:51:39 +11003499 if (!input->pointer)
3500 return;
3501
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003502 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003503 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003504 return;
3505
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003506 if (index >= (int) cursor->image_count) {
3507 fprintf(stderr, "cursor index out of range\n");
3508 return;
3509 }
3510
3511 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003512 buffer = wl_cursor_image_get_buffer(image);
3513 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003514 return;
3515
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003516 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3517 wl_surface_damage(input->pointer_surface, 0, 0,
3518 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003519 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003520 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3521 input->pointer_surface,
3522 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003523}
3524
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003525static const struct wl_callback_listener pointer_surface_listener;
3526
3527static void
3528pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3529 uint32_t time)
3530{
3531 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003532 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003533 int i;
3534
3535 if (callback) {
3536 assert(callback == input->cursor_frame_cb);
3537 wl_callback_destroy(callback);
3538 input->cursor_frame_cb = NULL;
3539 }
3540
Daniel Stone80972742012-11-07 17:51:39 +11003541 if (!input->pointer)
3542 return;
3543
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003544 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003545 wl_pointer_set_cursor(input->pointer,
3546 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003547 NULL, 0, 0);
3548 return;
3549 }
3550
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003551 if (input->current_cursor == CURSOR_UNSET)
3552 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003553 cursor = input->display->cursors[input->current_cursor];
3554 if (!cursor)
3555 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003556
3557 /* FIXME We don't have the current time on the first call so we set
3558 * the animation start to the time of the first frame callback. */
3559 if (time == 0)
3560 input->cursor_anim_start = 0;
3561 else if (input->cursor_anim_start == 0)
3562 input->cursor_anim_start = time;
3563
3564 if (time == 0 || input->cursor_anim_start == 0)
3565 i = 0;
3566 else
3567 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3568
Pekka Paalanenbc106382012-10-10 12:49:31 +03003569 if (cursor->image_count > 1) {
3570 input->cursor_frame_cb =
3571 wl_surface_frame(input->pointer_surface);
3572 wl_callback_add_listener(input->cursor_frame_cb,
3573 &pointer_surface_listener, input);
3574 }
3575
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003576 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003577}
3578
3579static const struct wl_callback_listener pointer_surface_listener = {
3580 pointer_surface_frame_callback
3581};
3582
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003583void
3584input_set_pointer_image(struct input *input, int pointer)
3585{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003586 int force = 0;
3587
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003588 if (!input->pointer)
3589 return;
3590
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003591 if (input->pointer_enter_serial > input->cursor_serial)
3592 force = 1;
3593
3594 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003595 return;
3596
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003597 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003598 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003599 if (!input->cursor_frame_cb)
3600 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003601 else if (force) {
3602 /* The current frame callback may be stuck if, for instance,
3603 * the set cursor request was processed by the server after
3604 * this client lost the focus. In this case the cursor surface
3605 * might not be mapped and the frame callback wouldn't ever
3606 * complete. Send a set_cursor and attach to try to map the
3607 * cursor surface again so that the callback will finish */
3608 input_set_pointer_image_index(input, 0);
3609 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003610}
3611
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003612struct wl_data_device *
3613input_get_data_device(struct input *input)
3614{
3615 return input->data_device;
3616}
3617
3618void
3619input_set_selection(struct input *input,
3620 struct wl_data_source *source, uint32_t time)
3621{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003622 if (input->data_device)
3623 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003624}
3625
3626void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003627input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003628{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003629 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003630 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003631}
3632
3633static void
3634offer_io_func(struct task *task, uint32_t events)
3635{
3636 struct data_offer *offer =
3637 container_of(task, struct data_offer, io_task);
3638 unsigned int len;
3639 char buffer[4096];
3640
3641 len = read(offer->fd, buffer, sizeof buffer);
3642 offer->func(buffer, len,
3643 offer->x, offer->y, offer->user_data);
3644
3645 if (len == 0) {
3646 close(offer->fd);
3647 data_offer_destroy(offer);
3648 }
3649}
3650
3651static void
3652data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3653 data_func_t func, void *user_data)
3654{
3655 int p[2];
3656
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003657 if (pipe2(p, O_CLOEXEC) == -1)
3658 return;
3659
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003660 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3661 close(p[1]);
3662
3663 offer->io_task.run = offer_io_func;
3664 offer->fd = p[0];
3665 offer->func = func;
3666 offer->refcount++;
3667 offer->user_data = user_data;
3668
3669 display_watch_fd(offer->input->display,
3670 offer->fd, EPOLLIN, &offer->io_task);
3671}
3672
3673void
3674input_receive_drag_data(struct input *input, const char *mime_type,
3675 data_func_t func, void *data)
3676{
3677 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003678 input->drag_offer->x = input->drag_x;
3679 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003680}
3681
3682int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003683input_receive_drag_data_to_fd(struct input *input,
3684 const char *mime_type, int fd)
3685{
3686 if (input->drag_offer)
3687 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3688
3689 return 0;
3690}
3691
3692int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003693input_receive_selection_data(struct input *input, const char *mime_type,
3694 data_func_t func, void *data)
3695{
3696 char **p;
3697
3698 if (input->selection_offer == NULL)
3699 return -1;
3700
3701 for (p = input->selection_offer->types.data; *p; p++)
3702 if (strcmp(mime_type, *p) == 0)
3703 break;
3704
3705 if (*p == NULL)
3706 return -1;
3707
3708 data_offer_receive_data(input->selection_offer,
3709 mime_type, func, data);
3710 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003711}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003712
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003713int
3714input_receive_selection_data_to_fd(struct input *input,
3715 const char *mime_type, int fd)
3716{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003717 if (input->selection_offer)
3718 wl_data_offer_receive(input->selection_offer->offer,
3719 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003720
3721 return 0;
3722}
3723
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003724void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003725window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003726{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003727 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003728 return;
3729
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003730 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003731}
3732
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003733static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003734surface_set_synchronized(struct surface *surface)
3735{
3736 if (!surface->subsurface)
3737 return;
3738
3739 if (surface->synchronized)
3740 return;
3741
3742 wl_subsurface_set_sync(surface->subsurface);
3743 surface->synchronized = 1;
3744}
3745
3746static void
3747surface_set_synchronized_default(struct surface *surface)
3748{
3749 if (!surface->subsurface)
3750 return;
3751
3752 if (surface->synchronized == surface->synchronized_default)
3753 return;
3754
3755 if (surface->synchronized_default)
3756 wl_subsurface_set_sync(surface->subsurface);
3757 else
3758 wl_subsurface_set_desync(surface->subsurface);
3759
3760 surface->synchronized = surface->synchronized_default;
3761}
3762
3763static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003764surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003765{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003766 struct widget *widget = surface->widget;
3767 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003768
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003769 if (surface->input_region) {
3770 wl_region_destroy(surface->input_region);
3771 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003772 }
3773
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003774 if (surface->opaque_region)
3775 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003776
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003777 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003778
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003779 if (widget->resize_handler)
3780 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003781 widget->allocation.width,
3782 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003783 widget->user_data);
3784
Pekka Paalanen35e82632013-04-25 13:57:48 +03003785 if (surface->subsurface &&
3786 (surface->allocation.x != widget->allocation.x ||
3787 surface->allocation.y != widget->allocation.y)) {
3788 wl_subsurface_set_position(surface->subsurface,
3789 widget->allocation.x,
3790 widget->allocation.y);
3791 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003792 if (surface->allocation.width != widget->allocation.width ||
3793 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003794 window_schedule_redraw(widget->window);
3795 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003796 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003797
3798 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003799 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003800 widget->allocation.width,
3801 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003802}
3803
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003804static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003805hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3806{
3807 /*
3808 * This hack should be removed, when EGL respects
3809 * eglSwapInterval(0).
3810 *
3811 * If this window has sub-surfaces, especially a free-running
3812 * EGL-widget, we need to post the parent surface once with
3813 * all the old state to guarantee, that the EGL-widget will
3814 * receive its frame callback soon. Otherwise, a forced call
3815 * to eglSwapBuffers may end up blocking, waiting for a frame
3816 * event that will never come, because we will commit the parent
3817 * surface with all new state only after eglSwapBuffers returns.
3818 *
3819 * This assumes, that:
3820 * 1. When the EGL widget's resize hook is called, it pauses.
3821 * 2. When the EGL widget's redraw hook is called, it forces a
3822 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3823 * In a single threaded application condition 1 is a no-op.
3824 *
3825 * XXX: This should actually be after the surface_resize() calls,
3826 * but cannot, because then it would commit the incomplete state
3827 * accumulated from the widget resize hooks.
3828 */
3829 if (window->subsurface_list.next != &window->main_surface->link ||
3830 window->subsurface_list.prev != &window->main_surface->link)
3831 wl_surface_commit(window->main_surface->surface);
3832}
3833
3834static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003835window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003836{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003837 struct surface *surface;
3838
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003839 widget_set_allocation(window->main_surface->widget,
3840 window->pending_allocation.x,
3841 window->pending_allocation.y,
3842 window->pending_allocation.width,
3843 window->pending_allocation.height);
3844
3845 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003846
3847 /* The main surface is in the list, too. Main surface's
3848 * resize_handler is responsible for calling widget_set_allocation()
3849 * on all sub-surface root widgets, so they will be resized
3850 * properly.
3851 */
3852 wl_list_for_each(surface, &window->subsurface_list, link) {
3853 if (surface == window->main_surface)
3854 continue;
3855
3856 surface_set_synchronized(surface);
3857 surface_resize(surface);
3858 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003859
3860 if (!window->fullscreen && !window->maximized)
3861 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003862}
3863
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003864static void
3865idle_resize(struct window *window)
3866{
3867 window->resize_needed = 0;
3868 window->redraw_needed = 1;
3869
3870 DBG("from %dx%d to %dx%d\n",
3871 window->main_surface->server_allocation.width,
3872 window->main_surface->server_allocation.height,
3873 window->pending_allocation.width,
3874 window->pending_allocation.height);
3875
3876 hack_prevent_EGL_sub_surface_deadlock(window);
3877
3878 window_do_resize(window);
3879}
3880
3881static void
3882undo_resize(struct window *window)
3883{
3884 window->pending_allocation.width =
3885 window->main_surface->server_allocation.width;
3886 window->pending_allocation.height =
3887 window->main_surface->server_allocation.height;
3888
3889 DBG("back to %dx%d\n",
3890 window->main_surface->server_allocation.width,
3891 window->main_surface->server_allocation.height);
3892
3893 window_do_resize(window);
3894
3895 if (window->pending_allocation.width == 0 &&
3896 window->pending_allocation.height == 0) {
3897 fprintf(stderr, "Error: Could not draw a surface, "
3898 "most likely due to insufficient disk space in "
3899 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3900 exit(EXIT_FAILURE);
3901 }
3902}
3903
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003904void
3905window_schedule_resize(struct window *window, int width, int height)
3906{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003907 /* We should probably get these numbers from the theme. */
3908 const int min_width = 200, min_height = 200;
3909
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003910 window->pending_allocation.x = 0;
3911 window->pending_allocation.y = 0;
3912 window->pending_allocation.width = width;
3913 window->pending_allocation.height = height;
3914
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003915 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003916 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003917 window->min_allocation.width = min_width;
3918 else
3919 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003920 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003921 window->min_allocation.height = min_height;
3922 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003923 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003924 }
3925
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003926 if (window->pending_allocation.width < window->min_allocation.width)
3927 window->pending_allocation.width = window->min_allocation.width;
3928 if (window->pending_allocation.height < window->min_allocation.height)
3929 window->pending_allocation.height = window->min_allocation.height;
3930
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003931 window->resize_needed = 1;
3932 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003933}
3934
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003935void
3936widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3937{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003938 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003939}
3940
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003941static int
3942window_get_shadow_margin(struct window *window)
3943{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003944 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003945 return frame_get_shadow_margin(window->frame->frame);
3946 else
3947 return 0;
3948}
3949
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003950static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003951handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003952 int32_t width, int32_t height,
3953 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003954{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003955 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003956 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003957
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003958 window->maximized = 0;
3959 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003960 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003961 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003962
3963 wl_array_for_each(p, states) {
3964 uint32_t state = *p;
3965 switch (state) {
3966 case XDG_SURFACE_STATE_MAXIMIZED:
3967 window->maximized = 1;
3968 break;
3969 case XDG_SURFACE_STATE_FULLSCREEN:
3970 window->fullscreen = 1;
3971 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003972 case XDG_SURFACE_STATE_RESIZING:
3973 window->resizing = 1;
3974 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003975 case XDG_SURFACE_STATE_ACTIVATED:
3976 window->focused = 1;
3977 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003978 default:
3979 /* Unknown state */
3980 break;
3981 }
3982 }
3983
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003984 if (window->frame) {
3985 if (window->maximized) {
3986 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3987 } else {
3988 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3989 }
3990
3991 if (window->focused) {
3992 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3993 } else {
3994 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3995 }
3996 }
3997
Jasper St. Pierref184c382014-05-06 08:33:27 -04003998 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003999 /* The width / height params are for window geometry,
4000 * but window_schedule_resize takes allocation. Add
4001 * on the shadow margin to get the difference. */
4002 int margin = window_get_shadow_margin(window);
4003
4004 window_schedule_resize(window,
4005 width + margin * 2,
4006 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004007 } else {
4008 window_schedule_resize(window,
4009 window->saved_allocation.width,
4010 window->saved_allocation.height);
4011 }
4012
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004013 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004014
4015 if (window->state_changed_handler)
4016 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004017}
4018
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004019static void
4020handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4021{
4022 struct window *window = data;
4023 window_close(window);
4024}
4025
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004026static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004027 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004028 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004029};
4030
4031static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004032window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004033{
4034 struct wl_surface *parent_surface;
4035
4036 if (!window->xdg_surface)
4037 return;
4038
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004039 if (window->parent)
4040 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004041 else
4042 parent_surface = NULL;
4043
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004044 if (parent_surface == window->last_parent_surface)
4045 return;
4046
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004047 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004048 window->last_parent_surface = parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004049}
4050
4051static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004052window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004053{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004054 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004055 frame_input_rect(window->frame->frame,
4056 &geometry->x,
4057 &geometry->y,
4058 &geometry->width,
4059 &geometry->height);
4060 else
4061 window_get_allocation(window, geometry);
4062}
4063
4064static void
4065window_sync_geometry(struct window *window)
4066{
4067 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004068
4069 if (!window->xdg_surface)
4070 return;
4071
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004072 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004073 if (geometry.x == window->last_geometry.x &&
4074 geometry.y == window->last_geometry.y &&
4075 geometry.width == window->last_geometry.width &&
4076 geometry.height == window->last_geometry.height)
4077 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004078
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004079 xdg_surface_set_window_geometry(window->xdg_surface,
4080 geometry.x,
4081 geometry.y,
4082 geometry.width,
4083 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004084 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004085}
4086
4087static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004088window_flush(struct window *window)
4089{
4090 struct surface *surface;
4091
4092 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004093 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004094 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004095 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004096 }
4097 }
4098
4099 wl_list_for_each(surface, &window->subsurface_list, link) {
4100 if (surface == window->main_surface)
4101 continue;
4102
4103 surface_flush(surface);
4104 }
4105
4106 surface_flush(window->main_surface);
4107}
4108
4109static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004110menu_destroy(struct menu *menu)
4111{
4112 widget_destroy(menu->widget);
4113 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004114 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004115 free(menu);
4116}
4117
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004118void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004119window_get_allocation(struct window *window,
4120 struct rectangle *allocation)
4121{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004122 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004123}
4124
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004125static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004126widget_redraw(struct widget *widget)
4127{
4128 struct widget *child;
4129
4130 if (widget->redraw_handler)
4131 widget->redraw_handler(widget, widget->user_data);
4132 wl_list_for_each(child, &widget->child_list, link)
4133 widget_redraw(child);
4134}
4135
4136static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004137frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4138{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004139 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004140
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004141 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004142 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004143 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004144 surface->frame_cb = NULL;
4145
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004146 surface->last_time = time;
4147
Pekka Paalanen71233882013-04-25 13:57:53 +03004148 if (surface->redraw_needed || surface->window->redraw_needed) {
4149 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004150 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004151 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004152}
4153
4154static const struct wl_callback_listener listener = {
4155 frame_callback
4156};
4157
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004158static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004159surface_redraw(struct surface *surface)
4160{
Pekka Paalanen71233882013-04-25 13:57:53 +03004161 DBG_OBJ(surface->surface, "begin\n");
4162
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004163 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004164 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004165
4166 /* Whole-window redraw forces a redraw even if the previous has
4167 * not yet hit the screen.
4168 */
4169 if (surface->frame_cb) {
4170 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004171 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004172
Pekka Paalanen71233882013-04-25 13:57:53 +03004173 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004174 wl_callback_destroy(surface->frame_cb);
4175 }
4176
Neil Roberts97b747c2013-12-19 16:17:12 +00004177 if (surface->widget->use_cairo &&
4178 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004179 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4180 return -1;
4181 }
4182
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004183 surface->frame_cb = wl_surface_frame(surface->surface);
4184 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004185 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004186
4187 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004188 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004189 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004190 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004191 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004192}
4193
4194static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004195idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004196{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004197 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004198 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004199 int failed = 0;
4200 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004201
Pekka Paalanen71233882013-04-25 13:57:53 +03004202 DBG(" --------- \n");
4203
Pekka Paalaneneebff542013-04-25 13:57:52 +03004204 wl_list_init(&window->redraw_task.link);
4205 window->redraw_task_scheduled = 0;
4206
4207 if (window->resize_needed) {
4208 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004209 if (window->main_surface->frame_cb) {
4210 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004211 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004212 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004213
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004214 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004215 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004216 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004217
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004218 if (surface_redraw(window->main_surface) < 0) {
4219 /*
4220 * Only main_surface failure will cause us to undo the resize.
4221 * If sub-surfaces fail, they will just be broken with old
4222 * content.
4223 */
4224 failed = 1;
4225 } else {
4226 wl_list_for_each(surface, &window->subsurface_list, link) {
4227 if (surface == window->main_surface)
4228 continue;
4229
4230 surface_redraw(surface);
4231 }
4232 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004233
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004234 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004235 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004236
4237 wl_list_for_each(surface, &window->subsurface_list, link)
4238 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004239
4240 if (resized && failed) {
4241 /* Restore widget tree to correspond to what is on screen. */
4242 undo_resize(window);
4243 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004244}
4245
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004246static void
4247window_schedule_redraw_task(struct window *window)
4248{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004249 if (!window->redraw_task_scheduled) {
4250 window->redraw_task.run = idle_redraw;
4251 display_defer(window->display, &window->redraw_task);
4252 window->redraw_task_scheduled = 1;
4253 }
4254}
4255
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004256void
4257window_schedule_redraw(struct window *window)
4258{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004259 struct surface *surface;
4260
Pekka Paalanen71233882013-04-25 13:57:53 +03004261 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4262
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004263 wl_list_for_each(surface, &window->subsurface_list, link)
4264 surface->redraw_needed = 1;
4265
4266 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004267}
4268
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004269int
4270window_is_fullscreen(struct window *window)
4271{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004272 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004273}
4274
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004275void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004276window_set_fullscreen(struct window *window, int fullscreen)
4277{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004278 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004279 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004280
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004281 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004282 return;
4283
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004284 if (fullscreen)
4285 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4286 else
4287 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004288}
4289
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004290int
4291window_is_maximized(struct window *window)
4292{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004293 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004294}
4295
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004296void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004297window_set_maximized(struct window *window, int maximized)
4298{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004299 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004300 return;
4301
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004302 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004303 return;
4304
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004305 if (maximized)
4306 xdg_surface_set_maximized(window->xdg_surface);
4307 else
4308 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004309}
4310
Jasper St. Pierrede680992014-04-10 17:23:49 -07004311int
4312window_is_resizing(struct window *window)
4313{
4314 return window->resizing;
4315}
4316
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004317void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004318window_set_minimized(struct window *window)
4319{
4320 if (!window->xdg_surface)
4321 return;
4322
4323 xdg_surface_set_minimized(window->xdg_surface);
4324}
4325
4326void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004327window_set_user_data(struct window *window, void *data)
4328{
4329 window->user_data = data;
4330}
4331
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004332void *
4333window_get_user_data(struct window *window)
4334{
4335 return window->user_data;
4336}
4337
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004338void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004339window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004340 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004341{
4342 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004343}
4344
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004345void
4346window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004347 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004348{
4349 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004350}
4351
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004352void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004353window_set_data_handler(struct window *window, window_data_handler_t handler)
4354{
4355 window->data_handler = handler;
4356}
4357
4358void
4359window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4360{
4361 window->drop_handler = handler;
4362}
4363
4364void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004365window_set_close_handler(struct window *window,
4366 window_close_handler_t handler)
4367{
4368 window->close_handler = handler;
4369}
4370
4371void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004372window_set_fullscreen_handler(struct window *window,
4373 window_fullscreen_handler_t handler)
4374{
4375 window->fullscreen_handler = handler;
4376}
4377
4378void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004379window_set_output_handler(struct window *window,
4380 window_output_handler_t handler)
4381{
4382 window->output_handler = handler;
4383}
4384
4385void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004386window_set_state_changed_handler(struct window *window,
4387 window_state_changed_handler_t handler)
4388{
4389 window->state_changed_handler = handler;
4390}
4391
4392void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004393window_set_title(struct window *window, const char *title)
4394{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004395 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004396 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004397 if (window->frame) {
4398 frame_set_title(window->frame->frame, title);
4399 widget_schedule_redraw(window->frame->widget);
4400 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004401 if (window->xdg_surface)
4402 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004403}
4404
4405const char *
4406window_get_title(struct window *window)
4407{
4408 return window->title;
4409}
4410
4411void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004412window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4413{
4414 struct text_cursor_position *text_cursor_position =
4415 window->display->text_cursor_position;
4416
Scott Moreau9295ce02012-06-01 12:46:10 -06004417 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004418 return;
4419
4420 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004421 window->main_surface->surface,
4422 wl_fixed_from_int(x),
4423 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004424}
4425
4426void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004427window_damage(struct window *window, int32_t x, int32_t y,
4428 int32_t width, int32_t height)
4429{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004430 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004431}
4432
Casey Dahlin9074db52012-04-19 22:50:09 -04004433static void
4434surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004435 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004436{
Rob Bradford7507b572012-05-15 17:55:34 +01004437 struct window *window = data;
4438 struct output *output;
4439 struct output *output_found = NULL;
4440 struct window_output *window_output;
4441
4442 wl_list_for_each(output, &window->display->output_list, link) {
4443 if (output->output == wl_output) {
4444 output_found = output;
4445 break;
4446 }
4447 }
4448
4449 if (!output_found)
4450 return;
4451
Brian Lovinbc919262013-08-07 15:34:59 -07004452 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004453 window_output->output = output_found;
4454
4455 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004456
4457 if (window->output_handler)
4458 window->output_handler(window, output_found, 1,
4459 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004460}
4461
4462static void
4463surface_leave(void *data,
4464 struct wl_surface *wl_surface, struct wl_output *output)
4465{
Rob Bradford7507b572012-05-15 17:55:34 +01004466 struct window *window = data;
4467 struct window_output *window_output;
4468 struct window_output *window_output_found = NULL;
4469
4470 wl_list_for_each(window_output, &window->window_output_list, link) {
4471 if (window_output->output->output == output) {
4472 window_output_found = window_output;
4473 break;
4474 }
4475 }
4476
4477 if (window_output_found) {
4478 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004479
4480 if (window->output_handler)
4481 window->output_handler(window, window_output->output,
4482 0, window->user_data);
4483
Rob Bradford7507b572012-05-15 17:55:34 +01004484 free(window_output_found);
4485 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004486}
4487
4488static const struct wl_surface_listener surface_listener = {
4489 surface_enter,
4490 surface_leave
4491};
4492
Pekka Paalanen4e373742013-02-13 16:17:13 +02004493static struct surface *
4494surface_create(struct window *window)
4495{
4496 struct display *display = window->display;
4497 struct surface *surface;
4498
Brian Lovinbc919262013-08-07 15:34:59 -07004499 surface = xmalloc(sizeof *surface);
4500 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004501 if (!surface)
4502 return NULL;
4503
4504 surface->window = window;
4505 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004506 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004507 wl_surface_add_listener(surface->surface, &surface_listener, window);
4508
Pekka Paalanen35e82632013-04-25 13:57:48 +03004509 wl_list_insert(&window->subsurface_list, &surface->link);
4510
Pekka Paalanen4e373742013-02-13 16:17:13 +02004511 return surface;
4512}
4513
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004514static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004515get_preferred_buffer_type(struct display *display)
4516{
4517#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004518 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004519 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4520#endif
4521
4522 return WINDOW_BUFFER_TYPE_SHM;
4523}
4524
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004525static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004526window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004527{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004528 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004529 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004530
Peter Huttererf3d62272013-08-08 11:57:05 +10004531 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004532 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004533 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004534
4535 surface = surface_create(window);
4536 window->main_surface = surface;
4537
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004538 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004539
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004540 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004541 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004542
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004543 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004544
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004545 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004546 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004547 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004548
Rob Bradford7507b572012-05-15 17:55:34 +01004549 wl_list_init (&window->window_output_list);
4550
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004551 return window;
4552}
4553
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004554struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004555window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004556{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004557 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004558 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004559
4560 window = window_create_internal(display, 0);
4561
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004562 if (window->display->xdg_shell) {
4563 window->xdg_surface =
4564 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4565 window->main_surface->surface);
4566 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004567
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004568 xdg_surface_set_user_data(window->xdg_surface, window);
4569 xdg_surface_add_listener(window->xdg_surface,
4570 &xdg_surface_listener, window);
4571 } else if (display->ivi_application) {
4572 /* auto generation of ivi_id based on process id + basement of id */
4573 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4574 window->ivi_surface =
4575 ivi_application_surface_create(display->ivi_application,
4576 id_ivisurf, window->main_surface->surface);
4577 fail_on_null(window->ivi_surface);
4578
4579 ivi_surface_add_listener(window->ivi_surface,
4580 &ivi_surface_listener, window);
4581 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004582
4583 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004584}
4585
4586struct window *
4587window_create_custom(struct display *display)
4588{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004589 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004590}
4591
Jasper St. Pierre53686042013-12-09 15:26:25 -05004592void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004593window_set_parent(struct window *window,
4594 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004595{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004596 window->parent = parent_window;
4597 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004598}
4599
4600struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004601window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004602{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004603 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004604}
4605
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004606static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004607menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004608{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004609 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004610 int next;
4611
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004612 frame_interior(menu->frame, &x, &y, &width, &height);
4613 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004614 if (menu->current != next) {
4615 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004616 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004617 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004618}
4619
4620static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004621menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004622 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004623 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004624{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004625 struct menu *menu = data;
4626
4627 if (widget == menu->widget)
4628 menu_set_item(data, y);
4629
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004630 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004631}
4632
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004633static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004634menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004635 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004636{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004637 struct menu *menu = data;
4638
4639 if (widget == menu->widget)
4640 menu_set_item(data, y);
4641
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004642 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004643}
4644
4645static void
4646menu_leave_handler(struct widget *widget, struct input *input, void *data)
4647{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004648 struct menu *menu = data;
4649
4650 if (widget == menu->widget)
4651 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004652}
4653
4654static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004655menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004656 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004657 uint32_t button, enum wl_pointer_button_state state,
4658 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004659
4660{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004661 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004662
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004663 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4664 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004665 /* Either relase after press-drag-release or
4666 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004667 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004668 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004669 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004670 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004671 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004672 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004673}
4674
4675static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004676menu_touch_up_handler(struct widget *widget,
4677 struct input *input,
4678 uint32_t serial,
4679 uint32_t time,
4680 int32_t id,
4681 void *data)
4682{
4683 struct menu *menu = data;
4684
4685 input_ungrab(input);
4686 menu_destroy(menu);
4687}
4688
4689static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004690menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004691{
4692 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004693 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004694 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004695
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004696 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004697
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004698 frame_repaint(menu->frame, cr);
4699 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004700
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004701 theme_set_background_source(menu->window->display->theme,
4702 cr, THEME_FRAME_ACTIVE);
4703 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004704 cairo_fill(cr);
4705
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004706 cairo_select_font_face(cr, "sans",
4707 CAIRO_FONT_SLANT_NORMAL,
4708 CAIRO_FONT_WEIGHT_NORMAL);
4709 cairo_set_font_size(cr, 12);
4710
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004711 for (i = 0; i < menu->count; i++) {
4712 if (i == menu->current) {
4713 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004714 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004715 cairo_fill(cr);
4716 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004717 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004718 cairo_show_text(cr, menu->entries[i]);
4719 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004720 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4721 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004722 cairo_show_text(cr, menu->entries[i]);
4723 }
4724 }
4725
4726 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004727}
4728
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004729static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004730handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4731{
4732 struct window *window = data;
4733 struct menu *menu = window->main_surface->widget->user_data;
4734
4735 input_ungrab(menu->input);
4736 menu_destroy(menu);
4737}
4738
4739static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004740 handle_popup_popup_done,
4741};
4742
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004743static struct menu *
4744create_menu(struct display *display,
4745 struct input *input, uint32_t time,
4746 menu_func_t func, const char **entries, int count,
4747 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004748{
4749 struct window *window;
4750 struct menu *menu;
4751
4752 menu = malloc(sizeof *menu);
4753 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004754 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004755
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004756 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004757 if (!window) {
4758 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004759 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004760 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004761
4762 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004763 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004764 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004765 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004766 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004767 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004768 menu->entries = entries;
4769 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004770 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004771 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004772 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004773 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004774 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004775
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004776 input_ungrab(input);
4777
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004778 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004779 widget_set_enter_handler(menu->widget, menu_enter_handler);
4780 widget_set_leave_handler(menu->widget, menu_leave_handler);
4781 widget_set_motion_handler(menu->widget, menu_motion_handler);
4782 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004783 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004784
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004785 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004786 frame_resize_inside(menu->frame, 200, count * 20);
4787 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4788 window_schedule_resize(window, frame_width(menu->frame),
4789 frame_height(menu->frame));
4790
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004791 return menu;
4792}
4793
4794struct window *
4795window_create_menu(struct display *display,
4796 struct input *input, uint32_t time,
4797 menu_func_t func, const char **entries, int count,
4798 void *user_data)
4799{
4800 struct menu *menu;
4801 menu = create_menu(display, input, time, func, entries, count, user_data);
4802
4803 if (menu == NULL)
4804 return NULL;
4805
4806 return menu->window;
4807}
4808
4809void
4810window_show_menu(struct display *display,
4811 struct input *input, uint32_t time, struct window *parent,
4812 int32_t x, int32_t y,
4813 menu_func_t func, const char **entries, int count)
4814{
4815 struct menu *menu;
4816 struct window *window;
4817 int32_t ix, iy;
4818
4819 menu = create_menu(display, input, time, func, entries, count, parent);
4820
4821 if (menu == NULL)
4822 return;
4823
4824 window = menu->window;
4825
4826 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4827 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4828
4829 window->x = x;
4830 window->y = y;
4831
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004832 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004833
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004834 if (!display->xdg_shell)
4835 return;
4836
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004837 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4838 window->main_surface->surface,
4839 parent->main_surface->surface,
4840 input->seat,
4841 display_get_serial(window->display),
4842 window->x - ix,
4843 window->y - iy,
4844 0);
4845 fail_on_null(window->xdg_popup);
4846
4847 xdg_popup_set_user_data(window->xdg_popup, window);
4848 xdg_popup_add_listener(window->xdg_popup,
4849 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004850}
4851
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004852void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004853window_set_buffer_type(struct window *window, enum window_buffer_type type)
4854{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004855 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004856}
4857
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004858enum window_buffer_type
4859window_get_buffer_type(struct window *window)
4860{
4861 return window->main_surface->buffer_type;
4862}
4863
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004864void
4865window_set_preferred_format(struct window *window,
4866 enum preferred_format format)
4867{
4868 window->preferred_format = format;
4869}
4870
Pekka Paalanen35e82632013-04-25 13:57:48 +03004871struct widget *
4872window_add_subsurface(struct window *window, void *data,
4873 enum subsurface_mode default_mode)
4874{
4875 struct widget *widget;
4876 struct surface *surface;
4877 struct wl_surface *parent;
4878 struct wl_subcompositor *subcompo = window->display->subcompositor;
4879
Pekka Paalanen35e82632013-04-25 13:57:48 +03004880 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004881 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004882 widget = widget_create(window, surface, data);
4883 wl_list_init(&widget->link);
4884 surface->widget = widget;
4885
4886 parent = window->main_surface->surface;
4887 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4888 surface->surface,
4889 parent);
4890 surface->synchronized = 1;
4891
4892 switch (default_mode) {
4893 case SUBSURFACE_SYNCHRONIZED:
4894 surface->synchronized_default = 1;
4895 break;
4896 case SUBSURFACE_DESYNCHRONIZED:
4897 surface->synchronized_default = 0;
4898 break;
4899 default:
4900 assert(!"bad enum subsurface_mode");
4901 }
4902
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004903 window->resize_needed = 1;
4904 window_schedule_redraw(window);
4905
Pekka Paalanen35e82632013-04-25 13:57:48 +03004906 return widget;
4907}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004908
4909static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004910display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004911 struct wl_output *wl_output,
4912 int x, int y,
4913 int physical_width,
4914 int physical_height,
4915 int subpixel,
4916 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004917 const char *model,
4918 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004919{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004920 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004921
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004922 output->allocation.x = x;
4923 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004924 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004925
4926 if (output->make)
4927 free(output->make);
4928 output->make = strdup(make);
4929
4930 if (output->model)
4931 free(output->model);
4932 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004933}
4934
4935static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004936display_handle_done(void *data,
4937 struct wl_output *wl_output)
4938{
4939}
4940
4941static void
4942display_handle_scale(void *data,
4943 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004944 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004945{
4946 struct output *output = data;
4947
4948 output->scale = scale;
4949}
4950
4951static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004952display_handle_mode(void *data,
4953 struct wl_output *wl_output,
4954 uint32_t flags,
4955 int width,
4956 int height,
4957 int refresh)
4958{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004959 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004960 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004961
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004962 if (flags & WL_OUTPUT_MODE_CURRENT) {
4963 output->allocation.width = width;
4964 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004965 if (display->output_configure_handler)
4966 (*display->output_configure_handler)(
4967 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004968 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004969}
4970
4971static const struct wl_output_listener output_listener = {
4972 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004973 display_handle_mode,
4974 display_handle_done,
4975 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004976};
4977
4978static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004979display_add_output(struct display *d, uint32_t id)
4980{
4981 struct output *output;
4982
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004983 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004984 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004985 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004986 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004987 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004988 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004989 wl_list_insert(d->output_list.prev, &output->link);
4990
4991 wl_output_add_listener(output->output, &output_listener, output);
4992}
4993
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004994static void
4995output_destroy(struct output *output)
4996{
4997 if (output->destroy_handler)
4998 (*output->destroy_handler)(output, output->user_data);
4999
5000 wl_output_destroy(output->output);
5001 wl_list_remove(&output->link);
5002 free(output);
5003}
5004
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005005static void
5006display_destroy_output(struct display *d, uint32_t id)
5007{
5008 struct output *output;
5009
5010 wl_list_for_each(output, &d->output_list, link) {
5011 if (output->server_output_id == id) {
5012 output_destroy(output);
5013 break;
5014 }
5015 }
5016}
5017
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005018void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005019display_set_global_handler(struct display *display,
5020 display_global_handler_t handler)
5021{
5022 struct global *global;
5023
5024 display->global_handler = handler;
5025 if (!handler)
5026 return;
5027
5028 wl_list_for_each(global, &display->global_list, link)
5029 display->global_handler(display,
5030 global->name, global->interface,
5031 global->version, display->user_data);
5032}
5033
5034void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005035display_set_global_handler_remove(struct display *display,
5036 display_global_handler_t remove_handler)
5037{
5038 display->global_handler_remove = remove_handler;
5039 if (!remove_handler)
5040 return;
5041}
5042
5043void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005044display_set_output_configure_handler(struct display *display,
5045 display_output_handler_t handler)
5046{
5047 struct output *output;
5048
5049 display->output_configure_handler = handler;
5050 if (!handler)
5051 return;
5052
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005053 wl_list_for_each(output, &display->output_list, link) {
5054 if (output->allocation.width == 0 &&
5055 output->allocation.height == 0)
5056 continue;
5057
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005058 (*display->output_configure_handler)(output,
5059 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005060 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005061}
5062
5063void
5064output_set_user_data(struct output *output, void *data)
5065{
5066 output->user_data = data;
5067}
5068
5069void *
5070output_get_user_data(struct output *output)
5071{
5072 return output->user_data;
5073}
5074
5075void
5076output_set_destroy_handler(struct output *output,
5077 display_output_handler_t handler)
5078{
5079 output->destroy_handler = handler;
5080 /* FIXME: implement this, once we have way to remove outputs */
5081}
5082
5083void
Scott Moreau4e072362012-09-29 02:03:11 -06005084output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005085{
Scott Moreau4e072362012-09-29 02:03:11 -06005086 struct rectangle allocation = output->allocation;
5087
5088 switch (output->transform) {
5089 case WL_OUTPUT_TRANSFORM_90:
5090 case WL_OUTPUT_TRANSFORM_270:
5091 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5092 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5093 /* Swap width and height */
5094 allocation.width = output->allocation.height;
5095 allocation.height = output->allocation.width;
5096 break;
5097 }
5098
5099 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005100}
5101
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005102struct wl_output *
5103output_get_wl_output(struct output *output)
5104{
5105 return output->output;
5106}
5107
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005108enum wl_output_transform
5109output_get_transform(struct output *output)
5110{
5111 return output->transform;
5112}
5113
Alexander Larssonafd319a2013-05-22 14:41:27 +02005114uint32_t
5115output_get_scale(struct output *output)
5116{
5117 return output->scale;
5118}
5119
Jason Ekstrand738715d2014-04-02 19:53:50 -05005120const char *
5121output_get_make(struct output *output)
5122{
5123 return output->make;
5124}
5125
5126const char *
5127output_get_model(struct output *output)
5128{
5129 return output->model;
5130}
5131
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005132static void
Daniel Stone97f68542012-05-30 16:32:01 +01005133fini_xkb(struct input *input)
5134{
5135 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005136 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005137}
5138
Jasper St. Pierre47f10432013-11-12 14:37:20 -05005139#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005140
Daniel Stone97f68542012-05-30 16:32:01 +01005141static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005142display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005143{
5144 struct input *input;
5145
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005146 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005147 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005148 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005149 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005150 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005151 input->pointer_focus = NULL;
5152 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005153 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005154 wl_list_insert(d->input_list.prev, &input->link);
5155
Daniel Stone37816df2012-05-16 18:45:18 +01005156 wl_seat_add_listener(input->seat, &seat_listener, input);
5157 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005158
Jason Ekstranda669bd52014-04-02 19:53:51 -05005159 if (d->data_device_manager) {
5160 input->data_device =
5161 wl_data_device_manager_get_data_device(d->data_device_manager,
5162 input->seat);
5163 wl_data_device_add_listener(input->data_device,
5164 &data_device_listener,
5165 input);
5166 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005167
5168 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005169
Jonny Lamb06959082014-08-12 14:58:27 +02005170 set_repeat_info(input, 40, 400);
5171
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005172 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5173 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005174 input->repeat_task.run = keyboard_repeat_func;
5175 display_watch_fd(d, input->repeat_timer_fd,
5176 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005177}
5178
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005179static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005180input_destroy(struct input *input)
5181{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005182 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005183 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005184
5185 if (input->drag_offer)
5186 data_offer_destroy(input->drag_offer);
5187
5188 if (input->selection_offer)
5189 data_offer_destroy(input->selection_offer);
5190
kabeer khan6ce67ec2014-10-20 11:55:29 +05305191 if (input->data_device) {
5192 if(input->display->data_device_manager_version >= 2)
5193 wl_data_device_release(input->data_device);
5194 else
5195 wl_data_device_destroy(input->data_device);
5196 }
Rob Bradford08031182013-08-13 20:11:03 +01005197 if (input->display->seat_version >= 3) {
5198 if (input->pointer)
5199 wl_pointer_release(input->pointer);
5200 if (input->keyboard)
5201 wl_keyboard_release(input->keyboard);
5202 }
5203
Daniel Stone97f68542012-05-30 16:32:01 +01005204 fini_xkb(input);
5205
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005206 wl_surface_destroy(input->pointer_surface);
5207
Pekka Paalanene1207c72011-12-16 12:02:09 +02005208 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005209 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005210 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005211 free(input);
5212}
5213
5214static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005215init_workspace_manager(struct display *d, uint32_t id)
5216{
5217 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005218 wl_registry_bind(d->registry, id,
5219 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005220 if (d->workspace_manager != NULL)
5221 workspace_manager_add_listener(d->workspace_manager,
5222 &workspace_manager_listener,
5223 d);
5224}
5225
5226static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005227shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5228{
5229 struct display *d = data;
5230
5231 if (format == WL_SHM_FORMAT_RGB565)
5232 d->has_rgb565 = 1;
5233}
5234
5235struct wl_shm_listener shm_listener = {
5236 shm_format
5237};
5238
5239static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005240xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5241{
5242 xdg_shell_pong(shell, serial);
5243}
5244
5245static const struct xdg_shell_listener xdg_shell_listener = {
5246 xdg_shell_ping,
5247};
5248
Pekka Paalanen71182ae2014-08-21 17:47:20 +03005249#define XDG_VERSION 4 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005250#ifdef static_assert
5251static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5252 "Interface version doesn't match implementation version");
5253#endif
5254
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005255static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005256registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5257 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005258{
5259 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005260 struct global *global;
5261
Brian Lovinbc919262013-08-07 15:34:59 -07005262 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005263 global->name = id;
5264 global->interface = strdup(interface);
5265 global->version = version;
5266 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005267
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005268 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005269 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005270 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005271 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005272 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005273 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005274 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005275 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005276 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005277 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005278 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005279 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305280 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005281 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005282 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305283 &wl_data_device_manager_interface,
5284 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005285 } else if (strcmp(interface, "xdg_shell") == 0) {
5286 d->xdg_shell = wl_registry_bind(registry, id,
5287 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005288 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005289 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005290 } else if (strcmp(interface, "text_cursor_position") == 0) {
5291 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005292 wl_registry_bind(registry, id,
5293 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005294 } else if (strcmp(interface, "workspace_manager") == 0) {
5295 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005296 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5297 d->subcompositor =
5298 wl_registry_bind(registry, id,
5299 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005300 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005301 else if (strcmp(interface, "ivi_application") == 0) {
5302 d->ivi_application =
5303 wl_registry_bind(registry, id,
5304 &ivi_application_interface, 1);
5305 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005306
5307 if (d->global_handler)
5308 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005309}
5310
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005311static void
5312registry_handle_global_remove(void *data, struct wl_registry *registry,
5313 uint32_t name)
5314{
5315 struct display *d = data;
5316 struct global *global;
5317 struct global *tmp;
5318
5319 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5320 if (global->name != name)
5321 continue;
5322
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005323 if (strcmp(global->interface, "wl_output") == 0)
5324 display_destroy_output(d, name);
5325
5326 /* XXX: Should destroy remaining bound globals */
5327
5328 if (d->global_handler_remove)
5329 d->global_handler_remove(d, name, global->interface,
5330 global->version, d->user_data);
5331
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005332 wl_list_remove(&global->link);
5333 free(global->interface);
5334 free(global);
5335 }
5336}
5337
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005338void *
5339display_bind(struct display *display, uint32_t name,
5340 const struct wl_interface *interface, uint32_t version)
5341{
5342 return wl_registry_bind(display->registry, name, interface, version);
5343}
5344
5345static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005346 registry_handle_global,
5347 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005348};
5349
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005350#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005351static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005352init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005353{
5354 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005355 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005356
Rob Clark6396ed32012-03-11 19:48:41 -05005357#ifdef USE_CAIRO_GLESV2
5358# define GL_BIT EGL_OPENGL_ES2_BIT
5359#else
5360# define GL_BIT EGL_OPENGL_BIT
5361#endif
5362
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005363 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005364 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005365 EGL_RED_SIZE, 1,
5366 EGL_GREEN_SIZE, 1,
5367 EGL_BLUE_SIZE, 1,
5368 EGL_ALPHA_SIZE, 1,
5369 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005370 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005371 EGL_NONE
5372 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005373
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005374#ifdef USE_CAIRO_GLESV2
5375 static const EGLint context_attribs[] = {
5376 EGL_CONTEXT_CLIENT_VERSION, 2,
5377 EGL_NONE
5378 };
5379 EGLint api = EGL_OPENGL_ES_API;
5380#else
5381 EGLint *context_attribs = NULL;
5382 EGLint api = EGL_OPENGL_API;
5383#endif
5384
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005385 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005386 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005387 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005388 return -1;
5389 }
5390
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005391 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005392 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005393 return -1;
5394 }
5395
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005396 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5397 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005398 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005399 return -1;
5400 }
5401
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005402 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005403 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005404 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005405 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005406 return -1;
5407 }
5408
Benjamin Franzke0c991632011-09-27 21:57:31 +02005409 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5410 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005411 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005412 return -1;
5413 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005414
5415 return 0;
5416}
5417
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005418static void
5419fini_egl(struct display *display)
5420{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005421 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005422
5423 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5424 EGL_NO_CONTEXT);
5425
5426 eglTerminate(display->dpy);
5427 eglReleaseThread();
5428}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005429#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005430
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005431static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005432init_dummy_surface(struct display *display)
5433{
5434 int len;
5435 void *data;
5436
5437 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005438 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005439 display->dummy_surface =
5440 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5441 1, 1, len);
5442 display->dummy_surface_data = data;
5443}
5444
5445static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005446handle_display_data(struct task *task, uint32_t events)
5447{
5448 struct display *display =
5449 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005450 struct epoll_event ep;
5451 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005452
5453 display->display_fd_events = events;
5454
5455 if (events & EPOLLERR || events & EPOLLHUP) {
5456 display_exit(display);
5457 return;
5458 }
5459
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005460 if (events & EPOLLIN) {
5461 ret = wl_display_dispatch(display->display);
5462 if (ret == -1) {
5463 display_exit(display);
5464 return;
5465 }
5466 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005467
5468 if (events & EPOLLOUT) {
5469 ret = wl_display_flush(display->display);
5470 if (ret == 0) {
5471 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5472 ep.data.ptr = &display->display_task;
5473 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5474 display->display_fd, &ep);
5475 } else if (ret == -1 && errno != EAGAIN) {
5476 display_exit(display);
5477 return;
5478 }
5479 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005480}
5481
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005482static void
5483log_handler(const char *format, va_list args)
5484{
5485 vfprintf(stderr, format, args);
5486}
5487
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005488struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005489display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005490{
5491 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005492
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005493 wl_log_set_handler_client(log_handler);
5494
Peter Huttererf3d62272013-08-08 11:57:05 +10005495 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005496 if (d == NULL)
5497 return NULL;
5498
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005499 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005500 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005501 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005502 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005503 return NULL;
5504 }
5505
Rob Bradford5ab9c752013-07-26 16:29:43 +01005506 d->xkb_context = xkb_context_new(0);
5507 if (d->xkb_context == NULL) {
5508 fprintf(stderr, "Failed to create XKB context\n");
5509 free(d);
5510 return NULL;
5511 }
5512
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005513 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005514 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005515 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005516 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5517 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005518
5519 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005520 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005521 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005522 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005523
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005524 d->workspace = 0;
5525 d->workspace_count = 1;
5526
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005527 d->registry = wl_display_get_registry(d->display);
5528 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005529
Marek Chalupaa519d062014-12-05 13:49:40 +01005530 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005531 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5532 return NULL;
5533 }
5534
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005535#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005536 if (init_egl(d) < 0)
5537 fprintf(stderr, "EGL does not seem to work, "
5538 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005539#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005540
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005541 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005542
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005543 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005544
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005545 wl_list_init(&d->window_list);
5546
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005547 init_dummy_surface(d);
5548
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005549 return d;
5550}
5551
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005552static void
5553display_destroy_outputs(struct display *display)
5554{
5555 struct output *tmp;
5556 struct output *output;
5557
5558 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5559 output_destroy(output);
5560}
5561
Pekka Paalanene1207c72011-12-16 12:02:09 +02005562static void
5563display_destroy_inputs(struct display *display)
5564{
5565 struct input *tmp;
5566 struct input *input;
5567
5568 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5569 input_destroy(input);
5570}
5571
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005572void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005573display_destroy(struct display *display)
5574{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005575 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005576 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5577 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005578
5579 if (!wl_list_empty(&display->deferred_list))
5580 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5581
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005582 cairo_surface_destroy(display->dummy_surface);
5583 free(display->dummy_surface_data);
5584
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005585 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005586 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005587
Daniel Stone97f68542012-05-30 16:32:01 +01005588 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005589
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005590 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005591 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005592
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005593#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005594 if (display->argb_device)
5595 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005596#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005597
Pekka Paalanen35e82632013-04-25 13:57:48 +03005598 if (display->subcompositor)
5599 wl_subcompositor_destroy(display->subcompositor);
5600
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005601 if (display->xdg_shell)
5602 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005603
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005604 if (display->ivi_application)
5605 ivi_application_destroy(display->ivi_application);
5606
Pekka Paalanenc2052982011-12-16 11:41:32 +02005607 if (display->shm)
5608 wl_shm_destroy(display->shm);
5609
5610 if (display->data_device_manager)
5611 wl_data_device_manager_destroy(display->data_device_manager);
5612
5613 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005614 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005615
5616 close(display->epoll_fd);
5617
U. Artie Eoff44874d92012-10-02 21:12:35 -07005618 if (!(display->display_fd_events & EPOLLERR) &&
5619 !(display->display_fd_events & EPOLLHUP))
5620 wl_display_flush(display->display);
5621
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005622 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005623 free(display);
5624}
5625
5626void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005627display_set_user_data(struct display *display, void *data)
5628{
5629 display->user_data = data;
5630}
5631
5632void *
5633display_get_user_data(struct display *display)
5634{
5635 return display->user_data;
5636}
5637
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005638struct wl_display *
5639display_get_display(struct display *display)
5640{
5641 return display->display;
5642}
5643
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005644int
5645display_has_subcompositor(struct display *display)
5646{
5647 if (display->subcompositor)
5648 return 1;
5649
5650 wl_display_roundtrip(display->display);
5651
5652 return display->subcompositor != NULL;
5653}
5654
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005655cairo_device_t *
5656display_get_cairo_device(struct display *display)
5657{
5658 return display->argb_device;
5659}
5660
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005661struct output *
5662display_get_output(struct display *display)
5663{
5664 return container_of(display->output_list.next, struct output, link);
5665}
5666
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005667struct wl_compositor *
5668display_get_compositor(struct display *display)
5669{
5670 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005671}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005672
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005673uint32_t
5674display_get_serial(struct display *display)
5675{
5676 return display->serial;
5677}
5678
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005679EGLDisplay
5680display_get_egl_display(struct display *d)
5681{
5682 return d->dpy;
5683}
5684
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005685struct wl_data_source *
5686display_create_data_source(struct display *display)
5687{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005688 if (display->data_device_manager)
5689 return wl_data_device_manager_create_data_source(display->data_device_manager);
5690 else
5691 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005692}
5693
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005694EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005695display_get_argb_egl_config(struct display *d)
5696{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005697 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005698}
5699
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005700int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005701display_acquire_window_surface(struct display *display,
5702 struct window *window,
5703 EGLContext ctx)
5704{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005705 struct surface *surface = window->main_surface;
5706
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005707 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005708 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005709
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005710 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005711 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005712}
5713
5714void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005715display_release_window_surface(struct display *display,
5716 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005717{
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 Franzke0c991632011-09-27 21:57:31 +02005721 return;
5722
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005723 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005724}
5725
5726void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005727display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005728{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005729 wl_list_insert(&display->deferred_list, &task->link);
5730}
5731
5732void
5733display_watch_fd(struct display *display,
5734 int fd, uint32_t events, struct task *task)
5735{
5736 struct epoll_event ep;
5737
5738 ep.events = events;
5739 ep.data.ptr = task;
5740 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5741}
5742
5743void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005744display_unwatch_fd(struct display *display, int fd)
5745{
5746 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5747}
5748
5749void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005750display_run(struct display *display)
5751{
5752 struct task *task;
5753 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005754 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005755
Pekka Paalanen826d7952011-12-15 10:14:07 +02005756 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005757 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005758 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005759 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005760 struct task, link);
5761 wl_list_remove(&task->link);
5762 task->run(task, 0);
5763 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005764
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005765 wl_display_dispatch_pending(display->display);
5766
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005767 if (!display->running)
5768 break;
5769
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005770 ret = wl_display_flush(display->display);
5771 if (ret < 0 && errno == EAGAIN) {
5772 ep[0].events =
5773 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5774 ep[0].data.ptr = &display->display_task;
5775
5776 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5777 display->display_fd, &ep[0]);
5778 } else if (ret < 0) {
5779 break;
5780 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005781
5782 count = epoll_wait(display->epoll_fd,
5783 ep, ARRAY_LENGTH(ep), -1);
5784 for (i = 0; i < count; i++) {
5785 task = ep[i].data.ptr;
5786 task->run(task, ep[i].events);
5787 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005788 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005789}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005790
5791void
5792display_exit(struct display *display)
5793{
5794 display->running = 0;
5795}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005796
5797void
5798keysym_modifiers_add(struct wl_array *modifiers_map,
5799 const char *name)
5800{
5801 size_t len = strlen(name) + 1;
5802 char *p;
5803
5804 p = wl_array_add(modifiers_map, len);
5805
5806 if (p == NULL)
5807 return;
5808
5809 strncpy(p, name, len);
5810}
5811
5812static xkb_mod_index_t
5813keysym_modifiers_get_index(struct wl_array *modifiers_map,
5814 const char *name)
5815{
5816 xkb_mod_index_t index = 0;
5817 char *p = modifiers_map->data;
5818
5819 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5820 if (strcmp(p, name) == 0)
5821 return index;
5822
5823 index++;
5824 p += strlen(p) + 1;
5825 }
5826
5827 return XKB_MOD_INVALID;
5828}
5829
5830xkb_mod_mask_t
5831keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5832 const char *name)
5833{
5834 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5835
5836 if (index == XKB_MOD_INVALID)
5837 return XKB_MOD_INVALID;
5838
5839 return 1 << index;
5840}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005841
5842void *
5843fail_on_null(void *p)
5844{
5845 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005846 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005847 exit(EXIT_FAILURE);
5848 }
5849
5850 return p;
5851}
5852
5853void *
5854xmalloc(size_t s)
5855{
5856 return fail_on_null(malloc(s));
5857}
5858
Peter Huttererf3d62272013-08-08 11:57:05 +10005859void *
5860xzalloc(size_t s)
5861{
5862 return fail_on_null(zalloc(s));
5863}
5864
Kristian Høgsbergce278412013-07-25 15:20:20 -07005865char *
5866xstrdup(const char *s)
5867{
5868 return fail_on_null(strdup(s));
5869}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005870
5871void *
5872xrealloc(char *p, size_t s)
5873{
5874 return fail_on_null(realloc(p, s));
5875}