blob: 289f3c548302e326526810a4e0e1f55ffedf7906 [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"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060068#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020069#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030070#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050071
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050072#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050073
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070074struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030075
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040076struct global {
77 uint32_t name;
78 char *interface;
79 uint32_t version;
80 struct wl_list link;
81};
82
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050083struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050084 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040085 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050086 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030087 struct wl_subcompositor *subcompositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040088 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050094 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020095 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040097 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040098
99 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700100 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400101 struct task display_task;
102
103 int epoll_fd;
104 struct wl_list deferred_list;
105
Pekka Paalanen826d7952011-12-15 10:14:07 +0200106 int running;
107
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400108 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400109 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400110 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500111 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400112
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400113 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400114
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300115 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300116 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400117
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200118 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800120 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200121
122 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100123
124 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200125
126 uint32_t workspace;
127 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200128
129 /* A hack to get text extents for tooltips */
130 cairo_surface_t *dummy_surface;
131 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200132
133 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100134 int seat_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500135};
136
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400138 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400139 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400140 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500141 TYPE_MAXIMIZED,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500142 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400143 TYPE_CUSTOM
144};
Rob Bradford7507b572012-05-15 17:55:34 +0100145
146struct window_output {
147 struct output *output;
148 struct wl_list link;
149};
150
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200151struct toysurface {
152 /*
153 * Prepare the surface for drawing. Makes sure there is a surface
154 * of the right size available for rendering, and returns it.
155 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200156 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200157 * If flags has SURFACE_HINT_RESIZE set, the user is
158 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200159 * Returns the Cairo surface to draw to.
160 */
161 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200162 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200163 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164
165 /*
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
169 */
170 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200171 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200172 struct rectangle *server_allocation);
173
174 /*
175 * Make the toysurface current with the given EGL context.
176 * Returns 0 on success, and negative of failure.
177 */
178 int (*acquire)(struct toysurface *base, EGLContext ctx);
179
180 /*
181 * Release the toysurface from the EGL context, returning control
182 * to Cairo.
183 */
184 void (*release)(struct toysurface *base);
185
186 /*
187 * Destroy the toysurface, including the Cairo surface, any
188 * backing storage, and the Wayland protocol objects.
189 */
190 void (*destroy)(struct toysurface *base);
191};
192
Pekka Paalanen4e373742013-02-13 16:17:13 +0200193struct surface {
194 struct window *window;
195
196 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300197 struct wl_subsurface *subsurface;
198 int synchronized;
199 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200200 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200201 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300202 int redraw_needed;
203 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300204 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200205
206 struct rectangle allocation;
207 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200208
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200209 struct wl_region *input_region;
210 struct wl_region *opaque_region;
211
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200212 enum window_buffer_type buffer_type;
213 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200214 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200215
216 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300217
218 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200219};
220
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500221struct window {
222 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100223 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500224 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200225 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400226 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500227 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500228 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400229 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400230 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300231 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400232 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400233 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100234 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400235 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400236 int focus_count;
237
Pekka Paalanen99436862012-11-19 17:15:59 +0200238 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200239 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000240 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400241
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200242 enum preferred_format preferred_format;
243
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500244 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500245 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400246 window_data_handler_t data_handler;
247 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500248 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400249 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200250 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400251
Pekka Paalanen4e373742013-02-13 16:17:13 +0200252 struct surface *main_surface;
253 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300254
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500255 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500256
Pekka Paalanen35e82632013-04-25 13:57:48 +0300257 /* struct surface::link, contains also main_surface */
258 struct wl_list subsurface_list;
259
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500260 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400261 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500262};
263
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500264struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500265 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200266 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300267 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500268 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400269 struct wl_list link;
270 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500271 widget_resize_handler_t resize_handler;
272 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500273 widget_enter_handler_t enter_handler;
274 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500275 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500276 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700277 widget_touch_down_handler_t touch_down_handler;
278 widget_touch_up_handler_t touch_up_handler;
279 widget_touch_motion_handler_t touch_motion_handler;
280 widget_touch_frame_handler_t touch_frame_handler;
281 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200282 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400283 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500284 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300285 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500286 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000287 /* If this is set to false then no cairo surface will be
288 * created before redrawing the surface. This is useful if the
289 * redraw handler is going to do completely custom rendering
290 * such as using EGL directly */
291 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400292};
293
Rusty Lynch041815a2013-08-08 21:20:38 -0700294struct touch_point {
295 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800296 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700297 struct widget *widget;
298 struct wl_list link;
299};
300
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400301struct input {
302 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100303 struct wl_seat *seat;
304 struct wl_pointer *pointer;
305 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700306 struct wl_touch *touch;
307 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400308 struct window *pointer_focus;
309 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700310 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300311 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300312 uint32_t cursor_anim_start;
313 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300314 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400315 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400316 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400317 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400318 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400319 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400320
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500321 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500322 struct widget *grab;
323 uint32_t grab_button;
324
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400325 struct wl_data_device *data_device;
326 struct data_offer *drag_offer;
327 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800328 uint32_t touch_grab;
329 int32_t touch_grab_id;
330 float drag_x, drag_y;
331 struct window *drag_focus;
332 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100333
334 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100335 struct xkb_keymap *keymap;
336 struct xkb_state *state;
337 xkb_mod_mask_t control_mask;
338 xkb_mod_mask_t alt_mask;
339 xkb_mod_mask_t shift_mask;
340 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400341
342 struct task repeat_task;
343 int repeat_timer_fd;
344 uint32_t repeat_sym;
345 uint32_t repeat_key;
346 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400347};
348
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500349struct output {
350 struct display *display;
351 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800352 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500353 struct rectangle allocation;
354 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600355 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200356 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200357
358 display_output_handler_t destroy_handler;
359 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500360};
361
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500362struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500363 struct widget *widget;
364 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500365 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500366};
367
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500368struct menu {
369 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500370 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500371 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500372 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700373 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500374 const char **entries;
375 uint32_t time;
376 int current;
377 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400378 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500379 menu_func_t func;
380};
381
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300382struct tooltip {
383 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300384 struct widget *widget;
385 char *entry;
386 struct task tooltip_task;
387 int tooltip_fd;
388 float x, y;
389};
390
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700391struct shm_pool {
392 struct wl_shm_pool *pool;
393 size_t size;
394 size_t used;
395 void *data;
396};
397
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400398enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300399 CURSOR_DEFAULT = 100,
400 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400401};
402
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500403enum window_location {
404 WINDOW_INTERIOR = 0,
405 WINDOW_RESIZING_TOP = 1,
406 WINDOW_RESIZING_BOTTOM = 2,
407 WINDOW_RESIZING_LEFT = 4,
408 WINDOW_RESIZING_TOP_LEFT = 5,
409 WINDOW_RESIZING_BOTTOM_LEFT = 6,
410 WINDOW_RESIZING_RIGHT = 8,
411 WINDOW_RESIZING_TOP_RIGHT = 9,
412 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
413 WINDOW_RESIZING_MASK = 15,
414 WINDOW_EXTERIOR = 16,
415 WINDOW_TITLEBAR = 17,
416 WINDOW_CLIENT_AREA = 18,
417};
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
593 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
594 surface->display->argb_ctx))
595 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);
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001304 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001305 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001306 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001307
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001308 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001309 cursor = NULL;
1310 for (j = 0; !cursor && j < cursors[i].count; ++j)
1311 cursor = wl_cursor_theme_get_cursor(
1312 display->cursor_theme, cursors[i].names[j]);
1313
1314 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001315 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001316 cursors[i].names[0]);
1317
1318 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001319 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001320}
1321
1322static void
1323destroy_cursors(struct display *display)
1324{
1325 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001326 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001327}
1328
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001329struct wl_cursor_image *
1330display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001331{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001332 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001333
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001334 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001335}
1336
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001337static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001338surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001339{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001340 if (!surface->cairo_surface)
1341 return;
1342
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001343 if (surface->opaque_region) {
1344 wl_surface_set_opaque_region(surface->surface,
1345 surface->opaque_region);
1346 wl_region_destroy(surface->opaque_region);
1347 surface->opaque_region = NULL;
1348 }
1349
1350 if (surface->input_region) {
1351 wl_surface_set_input_region(surface->surface,
1352 surface->input_region);
1353 wl_region_destroy(surface->input_region);
1354 surface->input_region = NULL;
1355 }
1356
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001357 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001358 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001359 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001360
Pekka Paalanen89dee002013-02-13 16:17:20 +02001361 cairo_surface_destroy(surface->cairo_surface);
1362 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001363}
1364
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001365int
1366window_has_focus(struct window *window)
1367{
1368 return window->focus_count > 0;
1369}
1370
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001371static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001372window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001373{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001374 struct surface *surface;
1375
Pekka Paalanen89dee002013-02-13 16:17:20 +02001376 if (window->type == TYPE_NONE) {
1377 window->type = TYPE_TOPLEVEL;
1378 if (window->shell_surface)
1379 wl_shell_surface_set_toplevel(window->shell_surface);
1380 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001381
Pekka Paalanen35e82632013-04-25 13:57:48 +03001382 wl_list_for_each(surface, &window->subsurface_list, link) {
1383 if (surface == window->main_surface)
1384 continue;
1385
1386 surface_flush(surface);
1387 }
1388
Pekka Paalanen89dee002013-02-13 16:17:20 +02001389 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001390}
1391
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001392struct display *
1393window_get_display(struct window *window)
1394{
1395 return window->display;
1396}
1397
Pekka Paalanen89dee002013-02-13 16:17:20 +02001398static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001399surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001400{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001401 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001402 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001403
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001404 if (!surface->toysurface && display->dpy &&
1405 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001406 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001408 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001409 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001410 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001411 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001412
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001413 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001414 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001415 surface->surface,
1416 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001417
Pekka Paalanen89dee002013-02-13 16:17:20 +02001418 surface->cairo_surface = surface->toysurface->prepare(
1419 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001420 allocation.width, allocation.height, flags,
1421 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001422}
1423
1424static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001425window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001426{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001427 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001428 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001429 int dx = 0;
1430 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001431
Pekka Paalanenec076692012-11-30 13:37:27 +02001432 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001433 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001434
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001435 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1436 flags |= SURFACE_HINT_RGB565;
1437
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001438 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1439 dx = surface->server_allocation.width -
1440 surface->allocation.width;
1441
1442 if (window->resize_edges & WINDOW_RESIZING_TOP)
1443 dy = surface->server_allocation.height -
1444 surface->allocation.height;
1445
1446 window->resize_edges = 0;
1447
Pekka Paalanen89dee002013-02-13 16:17:20 +02001448 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001449}
1450
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001451int
1452window_get_buffer_transform(struct window *window)
1453{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001454 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001455}
1456
1457void
1458window_set_buffer_transform(struct window *window,
1459 enum wl_output_transform transform)
1460{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001461 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001462 wl_surface_set_buffer_transform(window->main_surface->surface,
1463 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001464}
1465
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001466void
1467window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001468 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001469{
1470 window->main_surface->buffer_scale = scale;
1471 wl_surface_set_buffer_scale(window->main_surface->surface,
1472 scale);
1473}
1474
1475uint32_t
1476window_get_buffer_scale(struct window *window)
1477{
1478 return window->main_surface->buffer_scale;
1479}
1480
Alexander Larssond68f5232013-05-22 14:41:33 +02001481uint32_t
1482window_get_output_scale(struct window *window)
1483{
1484 struct window_output *window_output;
1485 struct window_output *window_output_tmp;
1486 int scale = 1;
1487
1488 wl_list_for_each_safe(window_output, window_output_tmp,
1489 &window->window_output_list, link) {
1490 if (window_output->output->scale > scale)
1491 scale = window_output->output->scale;
1492 }
1493
1494 return scale;
1495}
1496
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001497static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001498
Pekka Paalanen4e373742013-02-13 16:17:13 +02001499static void
1500surface_destroy(struct surface *surface)
1501{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001502 if (surface->frame_cb)
1503 wl_callback_destroy(surface->frame_cb);
1504
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001505 if (surface->input_region)
1506 wl_region_destroy(surface->input_region);
1507
1508 if (surface->opaque_region)
1509 wl_region_destroy(surface->opaque_region);
1510
Pekka Paalanen35e82632013-04-25 13:57:48 +03001511 if (surface->subsurface)
1512 wl_subsurface_destroy(surface->subsurface);
1513
Pekka Paalanen4e373742013-02-13 16:17:13 +02001514 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001515
1516 if (surface->toysurface)
1517 surface->toysurface->destroy(surface->toysurface);
1518
Pekka Paalanen35e82632013-04-25 13:57:48 +03001519 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001520 free(surface);
1521}
1522
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001523void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001524window_destroy(struct window *window)
1525{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001526 struct display *display = window->display;
1527 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001528 struct window_output *window_output;
1529 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001530
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001531 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001532
Rusty Lynch1084da52013-08-15 09:10:08 -07001533 wl_list_for_each(input, &display->input_list, link) {
1534 if (input->touch_focus == window)
1535 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001536 if (input->pointer_focus == window)
1537 input->pointer_focus = NULL;
1538 if (input->keyboard_focus == window)
1539 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001540 if (input->focus_widget &&
1541 input->focus_widget->window == window)
1542 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001543 }
1544
Rob Bradford7507b572012-05-15 17:55:34 +01001545 wl_list_for_each_safe(window_output, window_output_tmp,
1546 &window->window_output_list, link) {
1547 free (window_output);
1548 }
1549
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001550 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001551 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001552
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001553 if (window->shell_surface)
1554 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001555
1556 surface_destroy(window->main_surface);
1557
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001558 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001559
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001560 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001561 free(window);
1562}
1563
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001564static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001565widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001566{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001567 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001569 wl_list_for_each(child, &widget->child_list, link) {
1570 target = widget_find_widget(child, x, y);
1571 if (target)
1572 return target;
1573 }
1574
1575 if (widget->allocation.x <= x &&
1576 x < widget->allocation.x + widget->allocation.width &&
1577 widget->allocation.y <= y &&
1578 y < widget->allocation.y + widget->allocation.height) {
1579 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001580 }
1581
1582 return NULL;
1583}
1584
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001585static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001586window_find_widget(struct window *window, int32_t x, int32_t y)
1587{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001588 struct surface *surface;
1589 struct widget *widget;
1590
1591 wl_list_for_each(surface, &window->subsurface_list, link) {
1592 widget = widget_find_widget(surface->widget, x, y);
1593 if (widget)
1594 return widget;
1595 }
1596
1597 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001598}
1599
1600static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001601widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001602{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001603 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001604
Peter Huttererf3d62272013-08-08 11:57:05 +10001605 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001606 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001607 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001608 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001609 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001610 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001611 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001612 widget->tooltip = NULL;
1613 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001614 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001615 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001616
1617 return widget;
1618}
1619
1620struct widget *
1621window_add_widget(struct window *window, void *data)
1622{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001623 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001624
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001625 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001626 wl_list_init(&widget->link);
1627 window->main_surface->widget = widget;
1628
1629 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001630}
1631
1632struct widget *
1633widget_add_widget(struct widget *parent, void *data)
1634{
1635 struct widget *widget;
1636
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001637 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001638 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001639
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001640 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001641}
1642
1643void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001644widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001645{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001646 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001647 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001648 struct input *input;
1649
Pekka Paalanen35e82632013-04-25 13:57:48 +03001650 /* Destroy the sub-surface along with the root widget */
1651 if (surface->widget == widget && surface->subsurface)
1652 surface_destroy(widget->surface);
1653
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001654 if (widget->tooltip)
1655 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001656
Pekka Paalanene156fb62012-01-19 13:51:38 +02001657 wl_list_for_each(input, &display->input_list, link) {
1658 if (input->focus_widget == widget)
1659 input->focus_widget = NULL;
1660 }
1661
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001662 wl_list_remove(&widget->link);
1663 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664}
1665
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001666void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001667widget_set_default_cursor(struct widget *widget, int cursor)
1668{
1669 widget->default_cursor = cursor;
1670}
1671
1672void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001673widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001674{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001675 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001676}
1677
1678void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001679widget_set_size(struct widget *widget, int32_t width, int32_t height)
1680{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001681 widget->allocation.width = width;
1682 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001683}
1684
1685void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001686widget_set_allocation(struct widget *widget,
1687 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001688{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001689 widget->allocation.x = x;
1690 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001691 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001692}
1693
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001694void
1695widget_set_transparent(struct widget *widget, int transparent)
1696{
1697 widget->opaque = !transparent;
1698}
1699
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001700void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001701widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001702{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001703 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001704}
1705
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001706static cairo_surface_t *
1707widget_get_cairo_surface(struct widget *widget)
1708{
1709 struct surface *surface = widget->surface;
1710 struct window *window = widget->window;
1711
Neil Roberts97b747c2013-12-19 16:17:12 +00001712 assert(widget->use_cairo);
1713
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001714 if (!surface->cairo_surface) {
1715 if (surface == window->main_surface)
1716 window_create_main_surface(window);
1717 else
1718 surface_create_surface(surface, 0, 0, 0);
1719 }
1720
1721 return surface->cairo_surface;
1722}
1723
Alexander Larsson15901f02013-05-22 14:41:25 +02001724static void
1725widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1726{
1727 struct surface *surface = widget->surface;
1728 double angle;
1729 cairo_matrix_t m;
1730 enum wl_output_transform transform;
1731 int surface_width, surface_height;
1732 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001733 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001734
1735 surface_width = surface->allocation.width;
1736 surface_height = surface->allocation.height;
1737
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001738 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001739 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001740
Alexander Larsson15901f02013-05-22 14:41:25 +02001741 switch (transform) {
1742 case WL_OUTPUT_TRANSFORM_FLIPPED:
1743 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1744 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1745 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1746 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1747 break;
1748 default:
1749 cairo_matrix_init_identity(&m);
1750 break;
1751 }
1752
1753 switch (transform) {
1754 case WL_OUTPUT_TRANSFORM_NORMAL:
1755 default:
1756 angle = 0;
1757 translate_x = 0;
1758 translate_y = 0;
1759 break;
1760 case WL_OUTPUT_TRANSFORM_FLIPPED:
1761 angle = 0;
1762 translate_x = surface_width;
1763 translate_y = 0;
1764 break;
1765 case WL_OUTPUT_TRANSFORM_90:
1766 angle = M_PI_2;
1767 translate_x = surface_height;
1768 translate_y = 0;
1769 break;
1770 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1771 angle = M_PI_2;
1772 translate_x = surface_height;
1773 translate_y = surface_width;
1774 break;
1775 case WL_OUTPUT_TRANSFORM_180:
1776 angle = M_PI;
1777 translate_x = surface_width;
1778 translate_y = surface_height;
1779 break;
1780 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1781 angle = M_PI;
1782 translate_x = 0;
1783 translate_y = surface_height;
1784 break;
1785 case WL_OUTPUT_TRANSFORM_270:
1786 angle = M_PI + M_PI_2;
1787 translate_x = 0;
1788 translate_y = surface_width;
1789 break;
1790 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1791 angle = M_PI + M_PI_2;
1792 translate_x = 0;
1793 translate_y = 0;
1794 break;
1795 }
1796
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001797 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001798 cairo_translate(cr, translate_x, translate_y);
1799 cairo_rotate(cr, angle);
1800 cairo_transform(cr, &m);
1801}
1802
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001803cairo_t *
1804widget_cairo_create(struct widget *widget)
1805{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001806 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001807 cairo_surface_t *cairo_surface;
1808 cairo_t *cr;
1809
1810 cairo_surface = widget_get_cairo_surface(widget);
1811 cr = cairo_create(cairo_surface);
1812
Alexander Larsson15901f02013-05-22 14:41:25 +02001813 widget_cairo_update_transform(widget, cr);
1814
Pekka Paalanen35e82632013-04-25 13:57:48 +03001815 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1816
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001817 return cr;
1818}
1819
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001820struct wl_surface *
1821widget_get_wl_surface(struct widget *widget)
1822{
1823 return widget->surface->surface;
1824}
1825
Neil Roberts5e10a042013-09-09 00:40:17 +01001826struct wl_subsurface *
1827widget_get_wl_subsurface(struct widget *widget)
1828{
1829 return widget->surface->subsurface;
1830}
1831
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001832uint32_t
1833widget_get_last_time(struct widget *widget)
1834{
1835 return widget->surface->last_time;
1836}
1837
1838void
1839widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1840{
1841 struct wl_compositor *comp = widget->window->display->compositor;
1842 struct surface *surface = widget->surface;
1843
1844 if (!surface->input_region)
1845 surface->input_region = wl_compositor_create_region(comp);
1846
1847 if (rect) {
1848 wl_region_add(surface->input_region,
1849 rect->x, rect->y, rect->width, rect->height);
1850 }
1851}
1852
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001853void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001854widget_set_resize_handler(struct widget *widget,
1855 widget_resize_handler_t handler)
1856{
1857 widget->resize_handler = handler;
1858}
1859
1860void
1861widget_set_redraw_handler(struct widget *widget,
1862 widget_redraw_handler_t handler)
1863{
1864 widget->redraw_handler = handler;
1865}
1866
1867void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001868widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001869{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001870 widget->enter_handler = handler;
1871}
1872
1873void
1874widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1875{
1876 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001877}
1878
1879void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001880widget_set_motion_handler(struct widget *widget,
1881 widget_motion_handler_t handler)
1882{
1883 widget->motion_handler = handler;
1884}
1885
1886void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001887widget_set_button_handler(struct widget *widget,
1888 widget_button_handler_t handler)
1889{
1890 widget->button_handler = handler;
1891}
1892
1893void
Rusty Lynch041815a2013-08-08 21:20:38 -07001894widget_set_touch_up_handler(struct widget *widget,
1895 widget_touch_up_handler_t handler)
1896{
1897 widget->touch_up_handler = handler;
1898}
1899
1900void
1901widget_set_touch_down_handler(struct widget *widget,
1902 widget_touch_down_handler_t handler)
1903{
1904 widget->touch_down_handler = handler;
1905}
1906
1907void
1908widget_set_touch_motion_handler(struct widget *widget,
1909 widget_touch_motion_handler_t handler)
1910{
1911 widget->touch_motion_handler = handler;
1912}
1913
1914void
1915widget_set_touch_frame_handler(struct widget *widget,
1916 widget_touch_frame_handler_t handler)
1917{
1918 widget->touch_frame_handler = handler;
1919}
1920
1921void
1922widget_set_touch_cancel_handler(struct widget *widget,
1923 widget_touch_cancel_handler_t handler)
1924{
1925 widget->touch_cancel_handler = handler;
1926}
1927
1928void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001929widget_set_axis_handler(struct widget *widget,
1930 widget_axis_handler_t handler)
1931{
1932 widget->axis_handler = handler;
1933}
1934
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001935static void
1936window_schedule_redraw_task(struct window *window);
1937
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001938void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001939widget_schedule_redraw(struct widget *widget)
1940{
Pekka Paalanen71233882013-04-25 13:57:53 +03001941 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001942 widget->surface->redraw_needed = 1;
1943 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001944}
1945
Neil Roberts97b747c2013-12-19 16:17:12 +00001946void
1947widget_set_use_cairo(struct widget *widget,
1948 int use_cairo)
1949{
1950 widget->use_cairo = use_cairo;
1951}
1952
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001953cairo_surface_t *
1954window_get_surface(struct window *window)
1955{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001956 cairo_surface_t *cairo_surface;
1957
1958 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1959
1960 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001961}
1962
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001963struct wl_surface *
1964window_get_wl_surface(struct window *window)
1965{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001966 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001967}
1968
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001969static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001970tooltip_redraw_handler(struct widget *widget, void *data)
1971{
1972 cairo_t *cr;
1973 const int32_t r = 3;
1974 struct tooltip *tooltip = data;
1975 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001976
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001977 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001978 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001979 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1980 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1981 cairo_paint(cr);
1982
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001983 width = widget->allocation.width;
1984 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001985 rounded_rect(cr, 0, 0, width, height, r);
1986
1987 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1988 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1989 cairo_fill(cr);
1990
1991 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1992 cairo_move_to(cr, 10, 16);
1993 cairo_show_text(cr, tooltip->entry);
1994 cairo_destroy(cr);
1995}
1996
1997static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001998get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001999{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002000 cairo_t *cr;
2001 cairo_text_extents_t extents;
2002
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002003 /* Use the dummy_surface because tooltip's surface was not
2004 * created yet, and parent does not have a valid surface
2005 * outside repaint, either.
2006 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002007 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002008 cairo_text_extents(cr, tooltip->entry, &extents);
2009 cairo_destroy(cr);
2010
2011 return extents;
2012}
2013
2014static int
2015window_create_tooltip(struct tooltip *tooltip)
2016{
2017 struct widget *parent = tooltip->parent;
2018 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002019 const int offset_y = 27;
2020 const int margin = 3;
2021 cairo_text_extents_t extents;
2022
2023 if (tooltip->widget)
2024 return 0;
2025
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002026 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002027
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002028 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002029 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002030 widget_set_allocation(tooltip->widget,
2031 tooltip->x, tooltip->y + offset_y,
2032 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002033
2034 return 0;
2035}
2036
2037void
2038widget_destroy_tooltip(struct widget *parent)
2039{
2040 struct tooltip *tooltip = parent->tooltip;
2041
2042 parent->tooltip_count = 0;
2043 if (!tooltip)
2044 return;
2045
2046 if (tooltip->widget) {
2047 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002048 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002049 }
2050
2051 close(tooltip->tooltip_fd);
2052 free(tooltip->entry);
2053 free(tooltip);
2054 parent->tooltip = NULL;
2055}
2056
2057static void
2058tooltip_func(struct task *task, uint32_t events)
2059{
2060 struct tooltip *tooltip =
2061 container_of(task, struct tooltip, tooltip_task);
2062 uint64_t exp;
2063
Martin Olsson8df662a2012-07-08 03:03:47 +02002064 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2065 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002066 window_create_tooltip(tooltip);
2067}
2068
2069#define TOOLTIP_TIMEOUT 500
2070static int
2071tooltip_timer_reset(struct tooltip *tooltip)
2072{
2073 struct itimerspec its;
2074
2075 its.it_interval.tv_sec = 0;
2076 its.it_interval.tv_nsec = 0;
2077 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2078 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2079 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2080 fprintf(stderr, "could not set timerfd\n: %m");
2081 return -1;
2082 }
2083
2084 return 0;
2085}
2086
2087int
2088widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2089{
2090 struct tooltip *tooltip = parent->tooltip;
2091
2092 parent->tooltip_count++;
2093 if (tooltip) {
2094 tooltip->x = x;
2095 tooltip->y = y;
2096 tooltip_timer_reset(tooltip);
2097 return 0;
2098 }
2099
2100 /* the handler might be triggered too fast via input device motion, so
2101 * we need this check here to make sure tooltip is fully initialized */
2102 if (parent->tooltip_count > 1)
2103 return 0;
2104
2105 tooltip = malloc(sizeof *tooltip);
2106 if (!tooltip)
2107 return -1;
2108
2109 parent->tooltip = tooltip;
2110 tooltip->parent = parent;
2111 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002112 tooltip->x = x;
2113 tooltip->y = y;
2114 tooltip->entry = strdup(entry);
2115 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2116 if (tooltip->tooltip_fd < 0) {
2117 fprintf(stderr, "could not create timerfd\n: %m");
2118 return -1;
2119 }
2120
2121 tooltip->tooltip_task.run = tooltip_func;
2122 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2123 EPOLLIN, &tooltip->tooltip_task);
2124 tooltip_timer_reset(tooltip);
2125
2126 return 0;
2127}
2128
2129static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002130workspace_manager_state(void *data,
2131 struct workspace_manager *workspace_manager,
2132 uint32_t current,
2133 uint32_t count)
2134{
2135 struct display *display = data;
2136
2137 display->workspace = current;
2138 display->workspace_count = count;
2139}
2140
2141static const struct workspace_manager_listener workspace_manager_listener = {
2142 workspace_manager_state
2143};
2144
2145static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002146frame_resize_handler(struct widget *widget,
2147 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002148{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002149 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002150 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002151 struct rectangle interior;
2152 struct rectangle input;
2153 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002154
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002155 if (widget->window->type == TYPE_FULLSCREEN) {
2156 interior.x = 0;
2157 interior.y = 0;
2158 interior.width = width;
2159 interior.height = height;
2160 } else {
2161 if (widget->window->type == TYPE_MAXIMIZED) {
2162 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2163 } else {
2164 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2165 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002166
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002167 frame_resize(frame->frame, width, height);
2168 frame_interior(frame->frame, &interior.x, &interior.y,
2169 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002170 }
2171
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 widget_set_allocation(child, interior.x, interior.y,
2173 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002175 if (child->resize_handler) {
2176 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177 child->user_data);
2178
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002179 if (widget->window->type == TYPE_FULLSCREEN) {
2180 width = child->allocation.width;
2181 height = child->allocation.height;
2182 } else {
2183 frame_resize_inside(frame->frame,
2184 child->allocation.width,
2185 child->allocation.height);
2186 width = frame_width(frame->frame);
2187 height = frame_height(frame->frame);
2188 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002189 }
2190
Scott Moreauf7e498c2012-05-14 11:39:29 -06002191 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002192
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 widget->surface->input_region =
2194 wl_compositor_create_region(widget->window->display->compositor);
2195 if (widget->window->type != TYPE_FULLSCREEN) {
2196 frame_input_rect(frame->frame, &input.x, &input.y,
2197 &input.width, &input.height);
2198 wl_region_add(widget->surface->input_region,
2199 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002200 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002201 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002202 }
2203
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002204 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002205
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002206 if (child->opaque) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002207 if (widget->window->type != TYPE_FULLSCREEN) {
2208 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2209 &opaque.width, &opaque.height);
2210
2211 wl_region_add(widget->surface->opaque_region,
2212 opaque.x, opaque.y,
2213 opaque.width, opaque.height);
2214 } else {
2215 wl_region_add(widget->surface->opaque_region,
2216 0, 0, width, height);
2217 }
Martin Minarik1998b152012-05-10 02:04:35 +02002218 }
2219
Martin Minarik1998b152012-05-10 02:04:35 +02002220
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002221 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222}
2223
2224static void
2225frame_redraw_handler(struct widget *widget, void *data)
2226{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002227 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002228 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002229 struct window *window = widget->window;
2230
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002231 if (window->type == TYPE_FULLSCREEN)
2232 return;
2233
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002234 if (window->focus_count) {
2235 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2236 } else {
2237 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2238 }
2239
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002240 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002241
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002242 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002243
2244 cairo_destroy(cr);
2245}
2246
2247static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002248frame_get_pointer_image_for_location(struct window_frame *frame,
2249 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002250{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002251 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002252
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002253 if (window->type != TYPE_TOPLEVEL)
2254 return CURSOR_LEFT_PTR;
2255
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002256 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002257 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002258 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002259 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002260 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002261 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002262 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002263 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002264 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002265 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002266 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002267 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002268 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002269 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002270 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002271 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002272 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002273 case THEME_LOCATION_EXTERIOR:
2274 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002275 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002276 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002277 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002278}
2279
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002280static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002281frame_menu_func(struct window *window,
2282 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002283{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002284 struct display *display;
2285
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002286 switch (index) {
2287 case 0: /* close */
2288 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002289 window->close_handler(window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002290 else
2291 display_exit(window->display);
2292 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002293 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002294 display = window->display;
2295 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002296 workspace_manager_move_surface(
2297 display->workspace_manager,
2298 window->main_surface->surface,
2299 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002300 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002301 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002302 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002303 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002304 workspace_manager_move_surface(
2305 display->workspace_manager,
2306 window->main_surface->surface,
2307 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002308 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002309 case 3: /* fullscreen */
2310 /* we don't have a way to get out of fullscreen for now */
2311 if (window->fullscreen_handler)
2312 window->fullscreen_handler(window, window->user_data);
2313 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002314 }
2315}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002316
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002317void
2318window_show_frame_menu(struct window *window,
2319 struct input *input, uint32_t time)
2320{
2321 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002322 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002323
2324 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002325 "Close",
2326 "Move to workspace above", "Move to workspace below",
2327 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002328 };
2329
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002330 if (window->fullscreen_handler)
2331 count = ARRAY_LENGTH(entries);
2332 else
2333 count = ARRAY_LENGTH(entries) - 1;
2334
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002335 input_get_position(input, &x, &y);
2336 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002337 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002338}
2339
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002340static int
2341frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002342 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002343{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002344 struct window_frame *frame = data;
2345 enum theme_location location;
2346
2347 location = frame_pointer_enter(frame->frame, input, x, y);
2348 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2349 widget_schedule_redraw(frame->widget);
2350
2351 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002352}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002353
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002354static int
2355frame_motion_handler(struct widget *widget,
2356 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002357 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002358{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002359 struct window_frame *frame = data;
2360 enum theme_location location;
2361
2362 location = frame_pointer_motion(frame->frame, input, x, y);
2363 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2364 widget_schedule_redraw(frame->widget);
2365
2366 return frame_get_pointer_image_for_location(data, location);
2367}
2368
2369static void
2370frame_leave_handler(struct widget *widget,
2371 struct input *input, void *data)
2372{
2373 struct window_frame *frame = data;
2374
2375 frame_pointer_leave(frame->frame, input);
2376 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2377 widget_schedule_redraw(frame->widget);
2378}
2379
2380static void
2381frame_handle_status(struct window_frame *frame, struct input *input,
2382 uint32_t time, enum theme_location location)
2383{
2384 struct window *window = frame->widget->window;
2385 uint32_t status;
2386
2387 status = frame_status(frame->frame);
2388 if (status & FRAME_STATUS_REPAINT)
2389 widget_schedule_redraw(frame->widget);
2390
2391 if (status & FRAME_STATUS_MINIMIZE)
2392 fprintf(stderr,"Minimize stub\n");
2393
2394 if (status & FRAME_STATUS_MENU) {
2395 window_show_frame_menu(window, input, time);
2396 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2397 }
2398
2399 if (status & FRAME_STATUS_MAXIMIZE) {
2400 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2401 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2402 }
2403
2404 if (status & FRAME_STATUS_CLOSE) {
2405 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002406 window->close_handler(window->user_data);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002407 else
2408 display_exit(window->display);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002409 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002410 }
2411
2412 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2413 input_ungrab(input);
2414 wl_shell_surface_move(window->shell_surface,
2415 input_get_seat(input),
2416 window->display->serial);
2417
2418 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2419 }
2420
2421 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2422 input_ungrab(input);
2423
2424 window->resizing = 1;
2425 wl_shell_surface_resize(window->shell_surface,
2426 input_get_seat(input),
2427 window->display->serial,
2428 location);
2429
2430 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2431 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002432}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002433
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002434static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002435frame_button_handler(struct widget *widget,
2436 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002437 uint32_t button, enum wl_pointer_button_state state,
2438 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002439
2440{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002441 struct window_frame *frame = data;
2442 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002443
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002444 location = frame_pointer_button(frame->frame, input, button, state);
2445 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002446}
2447
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002448static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002449frame_touch_down_handler(struct widget *widget, struct input *input,
2450 uint32_t serial, uint32_t time, int32_t id,
2451 float x, float y, void *data)
2452{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002453 struct window_frame *frame = data;
2454
2455 frame_touch_down(frame->frame, input, id, x, y);
2456 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2457}
2458
2459static void
2460frame_touch_up_handler(struct widget *widget,
2461 struct input *input, uint32_t serial, uint32_t time,
2462 int32_t id, void *data)
2463{
2464 struct window_frame *frame = data;
2465
2466 frame_touch_up(frame->frame, input, id);
2467 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002468}
2469
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002470struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002471window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002472{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002473 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002474 uint32_t buttons;
2475
2476 if (window->type == TYPE_CUSTOM) {
2477 buttons = FRAME_BUTTON_NONE;
2478 } else {
2479 buttons = FRAME_BUTTON_ALL;
2480 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002481
Peter Huttererf3d62272013-08-08 11:57:05 +10002482 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002483 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002484 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002485
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002486 frame->widget = window_add_widget(window, frame);
2487 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002488
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002489 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2490 widget_set_resize_handler(frame->widget, frame_resize_handler);
2491 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002492 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002493 widget_set_motion_handler(frame->widget, frame_motion_handler);
2494 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002495 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002496 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002497
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002498 window->frame = frame;
2499
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002500 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002501}
2502
Kristian Høgsberga1627922012-06-20 17:30:03 -04002503void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002504window_frame_set_child_size(struct widget *widget, int child_width,
2505 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002506{
2507 struct display *display = widget->window->display;
2508 struct theme *t = display->theme;
2509 int decoration_width, decoration_height;
2510 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002511 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002512
2513 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002514 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002515 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002516 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002517
2518 width = child_width + decoration_width;
2519 height = child_height + decoration_height;
2520 } else {
2521 width = child_width;
2522 height = child_height;
2523 }
2524
2525 window_schedule_resize(widget->window, width, height);
2526}
2527
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002528static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002529window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002530{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002531 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002532
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002533 /* frame->child must be destroyed by the application */
2534 widget_destroy(frame->widget);
2535 free(frame);
2536}
2537
2538static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002539input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002540 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002541{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002542 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002543 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002544
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002545 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002546 return;
2547
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002548 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002549 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002550 widget = old;
2551 if (input->grab)
2552 widget = input->grab;
2553 if (widget->leave_handler)
2554 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002555 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002556 }
2557
2558 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002559 widget = focus;
2560 if (input->grab)
2561 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002562 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002563 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002564 cursor = widget->enter_handler(focus, input, x, y,
2565 widget->user_data);
2566 else
2567 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002568
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002569 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002570 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002571}
2572
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002573void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002574touch_grab(struct input *input, int32_t touch_id)
2575{
2576 input->touch_grab = 1;
2577 input->touch_grab_id = touch_id;
2578}
2579
2580void
2581touch_ungrab(struct input *input)
2582{
2583 struct touch_point *tp, *tmp;
2584
2585 input->touch_grab = 0;
2586
2587 wl_list_for_each_safe(tp, tmp,
2588 &input->touch_point_list, link) {
2589 if (tp->id != input->touch_grab_id)
2590 continue;
2591 wl_list_remove(&tp->link);
2592 free(tp);
2593
2594 return;
2595 }
2596}
2597
2598void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002599input_grab(struct input *input, struct widget *widget, uint32_t button)
2600{
2601 input->grab = widget;
2602 input->grab_button = button;
2603}
2604
2605void
2606input_ungrab(struct input *input)
2607{
2608 struct widget *widget;
2609
2610 input->grab = NULL;
2611 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002612 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002613 input->sx, input->sy);
2614 input_set_focus_widget(input, widget, input->sx, input->sy);
2615 }
2616}
2617
2618static void
2619input_remove_pointer_focus(struct input *input)
2620{
2621 struct window *window = input->pointer_focus;
2622
2623 if (!window)
2624 return;
2625
2626 input_set_focus_widget(input, NULL, 0, 0);
2627
2628 input->pointer_focus = NULL;
2629 input->current_cursor = CURSOR_UNSET;
2630}
2631
2632static void
2633pointer_handle_enter(void *data, struct wl_pointer *pointer,
2634 uint32_t serial, struct wl_surface *surface,
2635 wl_fixed_t sx_w, wl_fixed_t sy_w)
2636{
2637 struct input *input = data;
2638 struct window *window;
2639 struct widget *widget;
2640 float sx = wl_fixed_to_double(sx_w);
2641 float sy = wl_fixed_to_double(sy_w);
2642
2643 if (!surface) {
2644 /* enter event for a window we've just destroyed */
2645 return;
2646 }
2647
2648 input->display->serial = serial;
2649 input->pointer_enter_serial = serial;
2650 input->pointer_focus = wl_surface_get_user_data(surface);
2651 window = input->pointer_focus;
2652
Pekka Paalanen99436862012-11-19 17:15:59 +02002653 if (window->resizing) {
2654 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002655 /* Schedule a redraw to free the pool */
2656 window_schedule_redraw(window);
2657 }
2658
2659 input->sx = sx;
2660 input->sy = sy;
2661
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002662 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002663 input_set_focus_widget(input, widget, sx, sy);
2664}
2665
2666static void
2667pointer_handle_leave(void *data, struct wl_pointer *pointer,
2668 uint32_t serial, struct wl_surface *surface)
2669{
2670 struct input *input = data;
2671
2672 input->display->serial = serial;
2673 input_remove_pointer_focus(input);
2674}
2675
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002676static void
Daniel Stone37816df2012-05-16 18:45:18 +01002677pointer_handle_motion(void *data, struct wl_pointer *pointer,
2678 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002679{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002680 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002681 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002682 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002683 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002684 float sx = wl_fixed_to_double(sx_w);
2685 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002686
Paul Winwoodb22bf572013-08-29 10:52:54 +01002687 input->sx = sx;
2688 input->sy = sy;
2689
2690 if (!window)
2691 return;
2692
Rob Bradford5f087742013-07-11 19:41:27 +01002693 /* when making the window smaller - e.g. after a unmaximise we might
2694 * still have a pending motion event that the compositor has picked
2695 * based on the old surface dimensions
2696 */
2697 if (sx > window->main_surface->allocation.width ||
2698 sy > window->main_surface->allocation.height)
2699 return;
2700
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002701 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002702 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002703 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002704 }
2705
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002706 if (input->grab)
2707 widget = input->grab;
2708 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002709 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002710 if (widget) {
2711 if (widget->motion_handler)
2712 cursor = widget->motion_handler(input->focus_widget,
2713 input, time, sx, sy,
2714 widget->user_data);
2715 else
2716 cursor = widget->default_cursor;
2717 } else
2718 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002719
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002720 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002721}
2722
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002723static void
Daniel Stone37816df2012-05-16 18:45:18 +01002724pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002725 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002726{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002727 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002728 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002729 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002730
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002731 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002732 if (input->focus_widget && input->grab == NULL &&
2733 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002734 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002735
Neil Roberts6b28aad2012-01-23 19:11:18 +00002736 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002737 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002738 (*widget->button_handler)(widget,
2739 input, time,
2740 button, state,
2741 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002742
Daniel Stone4dbadb12012-05-30 16:31:51 +01002743 if (input->grab && input->grab_button == button &&
2744 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002745 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002746}
2747
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002748static void
Daniel Stone37816df2012-05-16 18:45:18 +01002749pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002750 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002751{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002752 struct input *input = data;
2753 struct widget *widget;
2754
2755 widget = input->focus_widget;
2756 if (input->grab)
2757 widget = input->grab;
2758 if (widget && widget->axis_handler)
2759 (*widget->axis_handler)(widget,
2760 input, time,
2761 axis, value,
2762 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002763}
2764
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002765static const struct wl_pointer_listener pointer_listener = {
2766 pointer_handle_enter,
2767 pointer_handle_leave,
2768 pointer_handle_motion,
2769 pointer_handle_button,
2770 pointer_handle_axis,
2771};
2772
2773static void
2774input_remove_keyboard_focus(struct input *input)
2775{
2776 struct window *window = input->keyboard_focus;
2777 struct itimerspec its;
2778
2779 its.it_interval.tv_sec = 0;
2780 its.it_interval.tv_nsec = 0;
2781 its.it_value.tv_sec = 0;
2782 its.it_value.tv_nsec = 0;
2783 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2784
2785 if (!window)
2786 return;
2787
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002788 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002789 if (window->keyboard_focus_handler)
2790 (*window->keyboard_focus_handler)(window, NULL,
2791 window->user_data);
2792
2793 input->keyboard_focus = NULL;
2794}
2795
2796static void
2797keyboard_repeat_func(struct task *task, uint32_t events)
2798{
2799 struct input *input =
2800 container_of(task, struct input, repeat_task);
2801 struct window *window = input->keyboard_focus;
2802 uint64_t exp;
2803
2804 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2805 /* If we change the timer between the fd becoming
2806 * readable and getting here, there'll be nothing to
2807 * read and we get EAGAIN. */
2808 return;
2809
2810 if (window && window->key_handler) {
2811 (*window->key_handler)(window, input, input->repeat_time,
2812 input->repeat_key, input->repeat_sym,
2813 WL_KEYBOARD_KEY_STATE_PRESSED,
2814 window->user_data);
2815 }
2816}
2817
2818static void
2819keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2820 uint32_t format, int fd, uint32_t size)
2821{
2822 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002823 struct xkb_keymap *keymap;
2824 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002825 char *map_str;
2826
2827 if (!data) {
2828 close(fd);
2829 return;
2830 }
2831
2832 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2833 close(fd);
2834 return;
2835 }
2836
2837 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2838 if (map_str == MAP_FAILED) {
2839 close(fd);
2840 return;
2841 }
2842
Rui Matos3eccb862013-10-10 19:44:22 +02002843 keymap = xkb_map_new_from_string(input->display->xkb_context,
2844 map_str,
2845 XKB_KEYMAP_FORMAT_TEXT_V1,
2846 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002847 munmap(map_str, size);
2848 close(fd);
2849
Rui Matos3eccb862013-10-10 19:44:22 +02002850 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002851 fprintf(stderr, "failed to compile keymap\n");
2852 return;
2853 }
2854
Rui Matos3eccb862013-10-10 19:44:22 +02002855 state = xkb_state_new(keymap);
2856 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002857 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002858 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002859 return;
2860 }
2861
Rui Matos3eccb862013-10-10 19:44:22 +02002862 xkb_keymap_unref(input->xkb.keymap);
2863 xkb_state_unref(input->xkb.state);
2864 input->xkb.keymap = keymap;
2865 input->xkb.state = state;
2866
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002867 input->xkb.control_mask =
2868 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2869 input->xkb.alt_mask =
2870 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2871 input->xkb.shift_mask =
2872 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2873}
2874
2875static void
2876keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2877 uint32_t serial, struct wl_surface *surface,
2878 struct wl_array *keys)
2879{
2880 struct input *input = data;
2881 struct window *window;
2882
2883 input->display->serial = serial;
2884 input->keyboard_focus = wl_surface_get_user_data(surface);
2885
2886 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002887 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002888 if (window->keyboard_focus_handler)
2889 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002890 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002891}
2892
2893static void
2894keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2895 uint32_t serial, struct wl_surface *surface)
2896{
2897 struct input *input = data;
2898
2899 input->display->serial = serial;
2900 input_remove_keyboard_focus(input);
2901}
2902
Scott Moreau210d0792012-03-22 10:47:01 -06002903static void
Daniel Stone37816df2012-05-16 18:45:18 +01002904keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002905 uint32_t serial, uint32_t time, uint32_t key,
2906 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002907{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002908 struct input *input = data;
2909 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002910 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002911 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002912 const xkb_keysym_t *syms;
2913 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002914 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002915
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002916 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002917 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002918 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002919 return;
2920
Daniel Stone97f68542012-05-30 16:32:01 +01002921 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002922
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002923 sym = XKB_KEY_NoSymbol;
2924 if (num_syms == 1)
2925 sym = syms[0];
2926
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002927
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002928 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002929 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002930 window_set_maximized(window,
2931 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002932 } else if (sym == XKB_KEY_F11 &&
2933 window->fullscreen_handler &&
2934 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2935 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002936 } else if (sym == XKB_KEY_F4 &&
2937 input->modifiers == MOD_ALT_MASK &&
2938 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2939 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002940 window->close_handler(window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002941 else
2942 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002943 } else if (window->key_handler) {
2944 (*window->key_handler)(window, input, time, key,
2945 sym, state, window->user_data);
2946 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002947
2948 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2949 key == input->repeat_key) {
2950 its.it_interval.tv_sec = 0;
2951 its.it_interval.tv_nsec = 0;
2952 its.it_value.tv_sec = 0;
2953 its.it_value.tv_nsec = 0;
2954 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2955 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2956 input->repeat_sym = sym;
2957 input->repeat_key = key;
2958 input->repeat_time = time;
2959 its.it_interval.tv_sec = 0;
2960 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2961 its.it_value.tv_sec = 0;
2962 its.it_value.tv_nsec = 400 * 1000 * 1000;
2963 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2964 }
2965}
2966
2967static void
Daniel Stone351eb612012-05-31 15:27:47 -04002968keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2969 uint32_t serial, uint32_t mods_depressed,
2970 uint32_t mods_latched, uint32_t mods_locked,
2971 uint32_t group)
2972{
2973 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002974 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002975
Matt Ropere61561f2013-06-24 16:52:43 +01002976 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2977 if (!input->xkb.keymap)
2978 return;
2979
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002980 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2981 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002982 mask = xkb_state_serialize_mods(input->xkb.state,
2983 XKB_STATE_DEPRESSED |
2984 XKB_STATE_LATCHED);
2985 input->modifiers = 0;
2986 if (mask & input->xkb.control_mask)
2987 input->modifiers |= MOD_CONTROL_MASK;
2988 if (mask & input->xkb.alt_mask)
2989 input->modifiers |= MOD_ALT_MASK;
2990 if (mask & input->xkb.shift_mask)
2991 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002992}
2993
Daniel Stone37816df2012-05-16 18:45:18 +01002994static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002995 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002996 keyboard_handle_enter,
2997 keyboard_handle_leave,
2998 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002999 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003000};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003001
3002static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003003touch_handle_down(void *data, struct wl_touch *wl_touch,
3004 uint32_t serial, uint32_t time, struct wl_surface *surface,
3005 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3006{
3007 struct input *input = data;
3008 struct widget *widget;
3009 float sx = wl_fixed_to_double(x_w);
3010 float sy = wl_fixed_to_double(y_w);
3011
Rusty Lynch1084da52013-08-15 09:10:08 -07003012 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003013 input->touch_focus = wl_surface_get_user_data(surface);
3014 if (!input->touch_focus) {
3015 DBG("Failed to find to touch focus for surface %p\n", surface);
3016 return;
3017 }
3018
3019 widget = window_find_widget(input->touch_focus,
3020 wl_fixed_to_double(x_w),
3021 wl_fixed_to_double(y_w));
3022 if (widget) {
3023 struct touch_point *tp = xmalloc(sizeof *tp);
3024 if (tp) {
3025 tp->id = id;
3026 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003027 tp->x = sx;
3028 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003029 wl_list_insert(&input->touch_point_list, &tp->link);
3030
3031 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003032 (*widget->touch_down_handler)(widget, input,
3033 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003034 sx, sy,
3035 widget->user_data);
3036 }
3037 }
3038}
3039
3040static void
3041touch_handle_up(void *data, struct wl_touch *wl_touch,
3042 uint32_t serial, uint32_t time, int32_t id)
3043{
3044 struct input *input = data;
3045 struct touch_point *tp, *tmp;
3046
Rusty Lynch041815a2013-08-08 21:20:38 -07003047 if (!input->touch_focus) {
3048 DBG("No touch focus found for touch up event!\n");
3049 return;
3050 }
3051
3052 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3053 if (tp->id != id)
3054 continue;
3055
3056 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003057 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003058 time, id,
3059 tp->widget->user_data);
3060
3061 wl_list_remove(&tp->link);
3062 free(tp);
3063
3064 return;
3065 }
3066}
3067
3068static void
3069touch_handle_motion(void *data, struct wl_touch *wl_touch,
3070 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3071{
3072 struct input *input = data;
3073 struct touch_point *tp;
3074 float sx = wl_fixed_to_double(x_w);
3075 float sy = wl_fixed_to_double(y_w);
3076
3077 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3078
3079 if (!input->touch_focus) {
3080 DBG("No touch focus found for touch motion event!\n");
3081 return;
3082 }
3083
3084 wl_list_for_each(tp, &input->touch_point_list, link) {
3085 if (tp->id != id)
3086 continue;
3087
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003088 tp->x = sx;
3089 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003090 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003091 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003092 id, sx, sy,
3093 tp->widget->user_data);
3094 return;
3095 }
3096}
3097
3098static void
3099touch_handle_frame(void *data, struct wl_touch *wl_touch)
3100{
3101 struct input *input = data;
3102 struct touch_point *tp, *tmp;
3103
3104 DBG("touch_handle_frame\n");
3105
3106 if (!input->touch_focus) {
3107 DBG("No touch focus found for touch frame event!\n");
3108 return;
3109 }
3110
3111 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3112 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003113 (*tp->widget->touch_frame_handler)(tp->widget, input,
3114 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003115
3116 wl_list_remove(&tp->link);
3117 free(tp);
3118 }
3119}
3120
3121static void
3122touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3123{
3124 struct input *input = data;
3125 struct touch_point *tp, *tmp;
3126
3127 DBG("touch_handle_cancel\n");
3128
3129 if (!input->touch_focus) {
3130 DBG("No touch focus found for touch cancel event!\n");
3131 return;
3132 }
3133
3134 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3135 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003136 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3137 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003138
3139 wl_list_remove(&tp->link);
3140 free(tp);
3141 }
3142}
3143
3144static const struct wl_touch_listener touch_listener = {
3145 touch_handle_down,
3146 touch_handle_up,
3147 touch_handle_motion,
3148 touch_handle_frame,
3149 touch_handle_cancel,
3150};
3151
3152static void
Daniel Stone37816df2012-05-16 18:45:18 +01003153seat_handle_capabilities(void *data, struct wl_seat *seat,
3154 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003155{
Daniel Stone37816df2012-05-16 18:45:18 +01003156 struct input *input = data;
3157
3158 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3159 input->pointer = wl_seat_get_pointer(seat);
3160 wl_pointer_set_user_data(input->pointer, input);
3161 wl_pointer_add_listener(input->pointer, &pointer_listener,
3162 input);
3163 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3164 wl_pointer_destroy(input->pointer);
3165 input->pointer = NULL;
3166 }
3167
3168 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3169 input->keyboard = wl_seat_get_keyboard(seat);
3170 wl_keyboard_set_user_data(input->keyboard, input);
3171 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3172 input);
3173 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3174 wl_keyboard_destroy(input->keyboard);
3175 input->keyboard = NULL;
3176 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003177
3178 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3179 input->touch = wl_seat_get_touch(seat);
3180 wl_touch_set_user_data(input->touch, input);
3181 wl_touch_add_listener(input->touch, &touch_listener, input);
3182 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3183 wl_touch_destroy(input->touch);
3184 input->touch = NULL;
3185 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003186}
3187
Rob Bradford08031182013-08-13 20:11:03 +01003188static void
3189seat_handle_name(void *data, struct wl_seat *seat,
3190 const char *name)
3191{
3192
3193}
3194
Daniel Stone37816df2012-05-16 18:45:18 +01003195static const struct wl_seat_listener seat_listener = {
3196 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003197 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003198};
3199
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003200void
3201input_get_position(struct input *input, int32_t *x, int32_t *y)
3202{
3203 *x = input->sx;
3204 *y = input->sy;
3205}
3206
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003207int
3208input_get_touch(struct input *input, int32_t id, float *x, float *y)
3209{
3210 struct touch_point *tp;
3211
3212 wl_list_for_each(tp, &input->touch_point_list, link) {
3213 if (tp->id != id)
3214 continue;
3215
3216 *x = tp->x;
3217 *y = tp->y;
3218 return 0;
3219 }
3220
3221 return -1;
3222}
3223
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003224struct display *
3225input_get_display(struct input *input)
3226{
3227 return input->display;
3228}
3229
Daniel Stone37816df2012-05-16 18:45:18 +01003230struct wl_seat *
3231input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003232{
Daniel Stone37816df2012-05-16 18:45:18 +01003233 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003234}
3235
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003236uint32_t
3237input_get_modifiers(struct input *input)
3238{
3239 return input->modifiers;
3240}
3241
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003242struct widget *
3243input_get_focus_widget(struct input *input)
3244{
3245 return input->focus_widget;
3246}
3247
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003248struct data_offer {
3249 struct wl_data_offer *offer;
3250 struct input *input;
3251 struct wl_array types;
3252 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003253
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003254 struct task io_task;
3255 int fd;
3256 data_func_t func;
3257 int32_t x, y;
3258 void *user_data;
3259};
3260
3261static void
3262data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3263{
3264 struct data_offer *offer = data;
3265 char **p;
3266
3267 p = wl_array_add(&offer->types, sizeof *p);
3268 *p = strdup(type);
3269}
3270
3271static const struct wl_data_offer_listener data_offer_listener = {
3272 data_offer_offer,
3273};
3274
3275static void
3276data_offer_destroy(struct data_offer *offer)
3277{
3278 char **p;
3279
3280 offer->refcount--;
3281 if (offer->refcount == 0) {
3282 wl_data_offer_destroy(offer->offer);
3283 for (p = offer->types.data; *p; p++)
3284 free(*p);
3285 wl_array_release(&offer->types);
3286 free(offer);
3287 }
3288}
3289
3290static void
3291data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003292 struct wl_data_device *data_device,
3293 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003294{
3295 struct data_offer *offer;
3296
Brian Lovinbc919262013-08-07 15:34:59 -07003297 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003298
3299 wl_array_init(&offer->types);
3300 offer->refcount = 1;
3301 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003302 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003303 wl_data_offer_add_listener(offer->offer,
3304 &data_offer_listener, offer);
3305}
3306
3307static void
3308data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003309 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003310 wl_fixed_t x_w, wl_fixed_t y_w,
3311 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003312{
3313 struct input *input = data;
3314 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003315 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003316 float x = wl_fixed_to_double(x_w);
3317 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003318 char **p;
3319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003320 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003321 input->drag_enter_serial = serial;
3322 input->drag_focus = window,
3323 input->drag_x = x;
3324 input->drag_y = y;
3325
3326 if (!input->touch_grab)
3327 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003328
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003329 if (offer) {
3330 input->drag_offer = wl_data_offer_get_user_data(offer);
3331
3332 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3333 *p = NULL;
3334
3335 types_data = input->drag_offer->types.data;
3336 } else {
3337 input->drag_offer = NULL;
3338 types_data = NULL;
3339 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003340
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003341 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003342 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003343 window->user_data);
3344}
3345
3346static void
3347data_device_leave(void *data, struct wl_data_device *data_device)
3348{
3349 struct input *input = data;
3350
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003351 if (input->drag_offer) {
3352 data_offer_destroy(input->drag_offer);
3353 input->drag_offer = NULL;
3354 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003355}
3356
3357static void
3358data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003359 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003360{
3361 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003362 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003363 float x = wl_fixed_to_double(x_w);
3364 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003365 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003366
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003367 input->drag_x = x;
3368 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003369
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003370 if (input->drag_offer)
3371 types_data = input->drag_offer->types.data;
3372 else
3373 types_data = NULL;
3374
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003375 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003376 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003377 window->user_data);
3378}
3379
3380static void
3381data_device_drop(void *data, struct wl_data_device *data_device)
3382{
3383 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003384 struct window *window = input->drag_focus;
3385 float x, y;
3386
3387 x = input->drag_x;
3388 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003389
3390 if (window->drop_handler)
3391 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003392 x, y, window->user_data);
3393
3394 if (input->touch_grab)
3395 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003396}
3397
3398static void
3399data_device_selection(void *data,
3400 struct wl_data_device *wl_data_device,
3401 struct wl_data_offer *offer)
3402{
3403 struct input *input = data;
3404 char **p;
3405
3406 if (input->selection_offer)
3407 data_offer_destroy(input->selection_offer);
3408
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003409 if (offer) {
3410 input->selection_offer = wl_data_offer_get_user_data(offer);
3411 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3412 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003413 } else {
3414 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003415 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003416}
3417
3418static const struct wl_data_device_listener data_device_listener = {
3419 data_device_data_offer,
3420 data_device_enter,
3421 data_device_leave,
3422 data_device_motion,
3423 data_device_drop,
3424 data_device_selection
3425};
3426
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003427static void
3428input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003429{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003430 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003431 struct wl_cursor *cursor;
3432 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003433
Daniel Stone80972742012-11-07 17:51:39 +11003434 if (!input->pointer)
3435 return;
3436
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003437 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003438 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003439 return;
3440
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003441 if (index >= (int) cursor->image_count) {
3442 fprintf(stderr, "cursor index out of range\n");
3443 return;
3444 }
3445
3446 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003447 buffer = wl_cursor_image_get_buffer(image);
3448 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003449 return;
3450
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003451 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3452 wl_surface_damage(input->pointer_surface, 0, 0,
3453 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003454 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003455 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3456 input->pointer_surface,
3457 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003458}
3459
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003460static const struct wl_callback_listener pointer_surface_listener;
3461
3462static void
3463pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3464 uint32_t time)
3465{
3466 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003467 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003468 int i;
3469
3470 if (callback) {
3471 assert(callback == input->cursor_frame_cb);
3472 wl_callback_destroy(callback);
3473 input->cursor_frame_cb = NULL;
3474 }
3475
Daniel Stone80972742012-11-07 17:51:39 +11003476 if (!input->pointer)
3477 return;
3478
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003479 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003480 wl_pointer_set_cursor(input->pointer,
3481 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003482 NULL, 0, 0);
3483 return;
3484 }
3485
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003486 if (input->current_cursor == CURSOR_UNSET)
3487 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003488 cursor = input->display->cursors[input->current_cursor];
3489 if (!cursor)
3490 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003491
3492 /* FIXME We don't have the current time on the first call so we set
3493 * the animation start to the time of the first frame callback. */
3494 if (time == 0)
3495 input->cursor_anim_start = 0;
3496 else if (input->cursor_anim_start == 0)
3497 input->cursor_anim_start = time;
3498
3499 if (time == 0 || input->cursor_anim_start == 0)
3500 i = 0;
3501 else
3502 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3503
Pekka Paalanenbc106382012-10-10 12:49:31 +03003504 if (cursor->image_count > 1) {
3505 input->cursor_frame_cb =
3506 wl_surface_frame(input->pointer_surface);
3507 wl_callback_add_listener(input->cursor_frame_cb,
3508 &pointer_surface_listener, input);
3509 }
3510
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003511 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003512}
3513
3514static const struct wl_callback_listener pointer_surface_listener = {
3515 pointer_surface_frame_callback
3516};
3517
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003518void
3519input_set_pointer_image(struct input *input, int pointer)
3520{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003521 int force = 0;
3522
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003523 if (!input->pointer)
3524 return;
3525
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003526 if (input->pointer_enter_serial > input->cursor_serial)
3527 force = 1;
3528
3529 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003530 return;
3531
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003532 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003533 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003534 if (!input->cursor_frame_cb)
3535 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003536 else if (force) {
3537 /* The current frame callback may be stuck if, for instance,
3538 * the set cursor request was processed by the server after
3539 * this client lost the focus. In this case the cursor surface
3540 * might not be mapped and the frame callback wouldn't ever
3541 * complete. Send a set_cursor and attach to try to map the
3542 * cursor surface again so that the callback will finish */
3543 input_set_pointer_image_index(input, 0);
3544 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003545}
3546
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003547struct wl_data_device *
3548input_get_data_device(struct input *input)
3549{
3550 return input->data_device;
3551}
3552
3553void
3554input_set_selection(struct input *input,
3555 struct wl_data_source *source, uint32_t time)
3556{
3557 wl_data_device_set_selection(input->data_device, source, time);
3558}
3559
3560void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003561input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003562{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003563 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003564 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003565}
3566
3567static void
3568offer_io_func(struct task *task, uint32_t events)
3569{
3570 struct data_offer *offer =
3571 container_of(task, struct data_offer, io_task);
3572 unsigned int len;
3573 char buffer[4096];
3574
3575 len = read(offer->fd, buffer, sizeof buffer);
3576 offer->func(buffer, len,
3577 offer->x, offer->y, offer->user_data);
3578
3579 if (len == 0) {
3580 close(offer->fd);
3581 data_offer_destroy(offer);
3582 }
3583}
3584
3585static void
3586data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3587 data_func_t func, void *user_data)
3588{
3589 int p[2];
3590
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003591 if (pipe2(p, O_CLOEXEC) == -1)
3592 return;
3593
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003594 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3595 close(p[1]);
3596
3597 offer->io_task.run = offer_io_func;
3598 offer->fd = p[0];
3599 offer->func = func;
3600 offer->refcount++;
3601 offer->user_data = user_data;
3602
3603 display_watch_fd(offer->input->display,
3604 offer->fd, EPOLLIN, &offer->io_task);
3605}
3606
3607void
3608input_receive_drag_data(struct input *input, const char *mime_type,
3609 data_func_t func, void *data)
3610{
3611 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003612 input->drag_offer->x = input->drag_x;
3613 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003614}
3615
3616int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003617input_receive_drag_data_to_fd(struct input *input,
3618 const char *mime_type, int fd)
3619{
3620 if (input->drag_offer)
3621 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3622
3623 return 0;
3624}
3625
3626int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003627input_receive_selection_data(struct input *input, const char *mime_type,
3628 data_func_t func, void *data)
3629{
3630 char **p;
3631
3632 if (input->selection_offer == NULL)
3633 return -1;
3634
3635 for (p = input->selection_offer->types.data; *p; p++)
3636 if (strcmp(mime_type, *p) == 0)
3637 break;
3638
3639 if (*p == NULL)
3640 return -1;
3641
3642 data_offer_receive_data(input->selection_offer,
3643 mime_type, func, data);
3644 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003645}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003646
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003647int
3648input_receive_selection_data_to_fd(struct input *input,
3649 const char *mime_type, int fd)
3650{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003651 if (input->selection_offer)
3652 wl_data_offer_receive(input->selection_offer->offer,
3653 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003654
3655 return 0;
3656}
3657
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003658void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003659window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003660{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003661 if (!window->shell_surface)
3662 return;
3663
Daniel Stone37816df2012-05-16 18:45:18 +01003664 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003665}
3666
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003667static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003668surface_set_synchronized(struct surface *surface)
3669{
3670 if (!surface->subsurface)
3671 return;
3672
3673 if (surface->synchronized)
3674 return;
3675
3676 wl_subsurface_set_sync(surface->subsurface);
3677 surface->synchronized = 1;
3678}
3679
3680static void
3681surface_set_synchronized_default(struct surface *surface)
3682{
3683 if (!surface->subsurface)
3684 return;
3685
3686 if (surface->synchronized == surface->synchronized_default)
3687 return;
3688
3689 if (surface->synchronized_default)
3690 wl_subsurface_set_sync(surface->subsurface);
3691 else
3692 wl_subsurface_set_desync(surface->subsurface);
3693
3694 surface->synchronized = surface->synchronized_default;
3695}
3696
3697static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003698surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003699{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003700 struct widget *widget = surface->widget;
3701 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003702
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003703 if (surface->input_region) {
3704 wl_region_destroy(surface->input_region);
3705 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003706 }
3707
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003708 if (surface->opaque_region)
3709 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003710
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003711 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003712
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003713 if (widget->resize_handler)
3714 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003715 widget->allocation.width,
3716 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003717 widget->user_data);
3718
Pekka Paalanen35e82632013-04-25 13:57:48 +03003719 if (surface->subsurface &&
3720 (surface->allocation.x != widget->allocation.x ||
3721 surface->allocation.y != widget->allocation.y)) {
3722 wl_subsurface_set_position(surface->subsurface,
3723 widget->allocation.x,
3724 widget->allocation.y);
3725 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003726 if (surface->allocation.width != widget->allocation.width ||
3727 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003728 window_schedule_redraw(widget->window);
3729 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003730 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003731
3732 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003733 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003734 widget->allocation.width,
3735 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003736}
3737
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003738static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003739hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3740{
3741 /*
3742 * This hack should be removed, when EGL respects
3743 * eglSwapInterval(0).
3744 *
3745 * If this window has sub-surfaces, especially a free-running
3746 * EGL-widget, we need to post the parent surface once with
3747 * all the old state to guarantee, that the EGL-widget will
3748 * receive its frame callback soon. Otherwise, a forced call
3749 * to eglSwapBuffers may end up blocking, waiting for a frame
3750 * event that will never come, because we will commit the parent
3751 * surface with all new state only after eglSwapBuffers returns.
3752 *
3753 * This assumes, that:
3754 * 1. When the EGL widget's resize hook is called, it pauses.
3755 * 2. When the EGL widget's redraw hook is called, it forces a
3756 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3757 * In a single threaded application condition 1 is a no-op.
3758 *
3759 * XXX: This should actually be after the surface_resize() calls,
3760 * but cannot, because then it would commit the incomplete state
3761 * accumulated from the widget resize hooks.
3762 */
3763 if (window->subsurface_list.next != &window->main_surface->link ||
3764 window->subsurface_list.prev != &window->main_surface->link)
3765 wl_surface_commit(window->main_surface->surface);
3766}
3767
3768static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003769window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003770{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003771 struct surface *surface;
3772
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003773 widget_set_allocation(window->main_surface->widget,
3774 window->pending_allocation.x,
3775 window->pending_allocation.y,
3776 window->pending_allocation.width,
3777 window->pending_allocation.height);
3778
3779 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003780
3781 /* The main surface is in the list, too. Main surface's
3782 * resize_handler is responsible for calling widget_set_allocation()
3783 * on all sub-surface root widgets, so they will be resized
3784 * properly.
3785 */
3786 wl_list_for_each(surface, &window->subsurface_list, link) {
3787 if (surface == window->main_surface)
3788 continue;
3789
3790 surface_set_synchronized(surface);
3791 surface_resize(surface);
3792 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003793}
3794
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003795static void
3796idle_resize(struct window *window)
3797{
3798 window->resize_needed = 0;
3799 window->redraw_needed = 1;
3800
3801 DBG("from %dx%d to %dx%d\n",
3802 window->main_surface->server_allocation.width,
3803 window->main_surface->server_allocation.height,
3804 window->pending_allocation.width,
3805 window->pending_allocation.height);
3806
3807 hack_prevent_EGL_sub_surface_deadlock(window);
3808
3809 window_do_resize(window);
3810}
3811
3812static void
3813undo_resize(struct window *window)
3814{
3815 window->pending_allocation.width =
3816 window->main_surface->server_allocation.width;
3817 window->pending_allocation.height =
3818 window->main_surface->server_allocation.height;
3819
3820 DBG("back to %dx%d\n",
3821 window->main_surface->server_allocation.width,
3822 window->main_surface->server_allocation.height);
3823
3824 window_do_resize(window);
3825
3826 if (window->pending_allocation.width == 0 &&
3827 window->pending_allocation.height == 0) {
3828 fprintf(stderr, "Error: Could not draw a surface, "
3829 "most likely due to insufficient disk space in "
3830 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3831 exit(EXIT_FAILURE);
3832 }
3833}
3834
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003835void
3836window_schedule_resize(struct window *window, int width, int height)
3837{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003838 /* We should probably get these numbers from the theme. */
3839 const int min_width = 200, min_height = 200;
3840
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003841 window->pending_allocation.x = 0;
3842 window->pending_allocation.y = 0;
3843 window->pending_allocation.width = width;
3844 window->pending_allocation.height = height;
3845
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003846 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003847 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003848 window->min_allocation.width = min_width;
3849 else
3850 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003851 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003852 window->min_allocation.height = min_height;
3853 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003854 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003855 }
3856
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003857 if (window->pending_allocation.width < window->min_allocation.width)
3858 window->pending_allocation.width = window->min_allocation.width;
3859 if (window->pending_allocation.height < window->min_allocation.height)
3860 window->pending_allocation.height = window->min_allocation.height;
3861
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003862 window->resize_needed = 1;
3863 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003864}
3865
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003866void
3867widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3868{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003869 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003870}
3871
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003872static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003873handle_ping(void *data, struct wl_shell_surface *shell_surface,
3874 uint32_t serial)
3875{
3876 wl_shell_surface_pong(shell_surface, serial);
3877}
3878
3879static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003880handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003881 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003882{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003883 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003884
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003885 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003886 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003887}
3888
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003889static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003890menu_destroy(struct menu *menu)
3891{
3892 widget_destroy(menu->widget);
3893 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003894 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003895 free(menu);
3896}
3897
3898static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003899handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3900{
3901 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003902 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003903
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003904 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003905 * device. Or just use wl_callback. And this really needs to
3906 * be a window vfunc that the menu can set. And we need the
3907 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003908
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003909 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003910 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003911}
3912
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003913static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003914 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003915 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003916 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003917};
3918
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003919void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003920window_get_allocation(struct window *window,
3921 struct rectangle *allocation)
3922{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003923 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003924}
3925
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003926static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003927widget_redraw(struct widget *widget)
3928{
3929 struct widget *child;
3930
3931 if (widget->redraw_handler)
3932 widget->redraw_handler(widget, widget->user_data);
3933 wl_list_for_each(child, &widget->child_list, link)
3934 widget_redraw(child);
3935}
3936
3937static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003938frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3939{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003940 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003941
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003942 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003943 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003944 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003945 surface->frame_cb = NULL;
3946
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003947 surface->last_time = time;
3948
Pekka Paalanen71233882013-04-25 13:57:53 +03003949 if (surface->redraw_needed || surface->window->redraw_needed) {
3950 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003951 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003952 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003953}
3954
3955static const struct wl_callback_listener listener = {
3956 frame_callback
3957};
3958
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003959static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003960surface_redraw(struct surface *surface)
3961{
Pekka Paalanen71233882013-04-25 13:57:53 +03003962 DBG_OBJ(surface->surface, "begin\n");
3963
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003964 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003965 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003966
3967 /* Whole-window redraw forces a redraw even if the previous has
3968 * not yet hit the screen.
3969 */
3970 if (surface->frame_cb) {
3971 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003972 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003973
Pekka Paalanen71233882013-04-25 13:57:53 +03003974 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003975 wl_callback_destroy(surface->frame_cb);
3976 }
3977
Neil Roberts97b747c2013-12-19 16:17:12 +00003978 if (surface->widget->use_cairo &&
3979 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003980 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3981 return -1;
3982 }
3983
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003984 surface->frame_cb = wl_surface_frame(surface->surface);
3985 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003986 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003987
3988 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003989 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003990 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003991 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003992 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003993}
3994
3995static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003996idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003997{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003998 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003999 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004000 int failed = 0;
4001 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004002
Pekka Paalanen71233882013-04-25 13:57:53 +03004003 DBG(" --------- \n");
4004
Pekka Paalaneneebff542013-04-25 13:57:52 +03004005 wl_list_init(&window->redraw_task.link);
4006 window->redraw_task_scheduled = 0;
4007
4008 if (window->resize_needed) {
4009 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004010 if (window->main_surface->frame_cb) {
4011 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004012 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004013 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004014
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004015 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004016 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004017 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004018
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004019 if (surface_redraw(window->main_surface) < 0) {
4020 /*
4021 * Only main_surface failure will cause us to undo the resize.
4022 * If sub-surfaces fail, they will just be broken with old
4023 * content.
4024 */
4025 failed = 1;
4026 } else {
4027 wl_list_for_each(surface, &window->subsurface_list, link) {
4028 if (surface == window->main_surface)
4029 continue;
4030
4031 surface_redraw(surface);
4032 }
4033 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004034
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004035 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004036 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004037
4038 wl_list_for_each(surface, &window->subsurface_list, link)
4039 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004040
4041 if (resized && failed) {
4042 /* Restore widget tree to correspond to what is on screen. */
4043 undo_resize(window);
4044 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004045}
4046
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004047static void
4048window_schedule_redraw_task(struct window *window)
4049{
4050 if (window->configure_requests)
4051 return;
4052 if (!window->redraw_task_scheduled) {
4053 window->redraw_task.run = idle_redraw;
4054 display_defer(window->display, &window->redraw_task);
4055 window->redraw_task_scheduled = 1;
4056 }
4057}
4058
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004059void
4060window_schedule_redraw(struct window *window)
4061{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004062 struct surface *surface;
4063
Pekka Paalanen71233882013-04-25 13:57:53 +03004064 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4065
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004066 wl_list_for_each(surface, &window->subsurface_list, link)
4067 surface->redraw_needed = 1;
4068
4069 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004070}
4071
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004072int
4073window_is_fullscreen(struct window *window)
4074{
4075 return window->type == TYPE_FULLSCREEN;
4076}
4077
MoD063a8822013-03-02 23:43:57 +00004078static void
4079configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4080{
4081 struct window *window = data;
4082
4083 wl_callback_destroy(callback);
4084 window->configure_requests--;
4085
4086 if (!window->configure_requests)
4087 window_schedule_redraw(window);
4088}
4089
4090static struct wl_callback_listener configure_request_listener = {
4091 configure_request_completed,
4092};
4093
4094static void
4095window_defer_redraw_until_configure(struct window* window)
4096{
4097 struct wl_callback *callback;
4098
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004099 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004100 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004101 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004102 }
4103
4104 callback = wl_display_sync(window->display->display);
4105 wl_callback_add_listener(callback, &configure_request_listener, window);
4106 window->configure_requests++;
4107}
4108
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004109void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004110window_set_fullscreen(struct window *window, int fullscreen)
4111{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004112 if (!window->display->shell)
4113 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004114
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004115 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004116 return;
4117
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004118 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004119 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004120 if (window->type == TYPE_TOPLEVEL) {
4121 window->saved_allocation = window->main_surface->allocation;
4122 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004123 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004124 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004125 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004126 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004127 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004128 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004129 if (window->saved_type == TYPE_MAXIMIZED) {
4130 window_set_maximized(window, 1);
4131 } else {
4132 window->type = TYPE_TOPLEVEL;
4133 wl_shell_surface_set_toplevel(window->shell_surface);
4134 window_schedule_resize(window,
4135 window->saved_allocation.width,
4136 window->saved_allocation.height);
4137 }
4138
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004139 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004140}
4141
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004142void
4143window_set_fullscreen_method(struct window *window,
4144 enum wl_shell_surface_fullscreen_method method)
4145{
4146 window->fullscreen_method = method;
4147}
4148
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004149int
4150window_is_maximized(struct window *window)
4151{
4152 return window->type == TYPE_MAXIMIZED;
4153}
4154
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004155void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004156window_set_maximized(struct window *window, int maximized)
4157{
4158 if (!window->display->shell)
4159 return;
4160
4161 if ((window->type == TYPE_MAXIMIZED) == maximized)
4162 return;
4163
4164 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004165 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004166 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4167 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004168 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004169 } else if (window->type == TYPE_FULLSCREEN) {
4170 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4171 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004172 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004173 } else {
4174 wl_shell_surface_set_toplevel(window->shell_surface);
4175 window->type = TYPE_TOPLEVEL;
4176 window_schedule_resize(window,
4177 window->saved_allocation.width,
4178 window->saved_allocation.height);
4179 }
4180}
4181
4182void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004183window_set_user_data(struct window *window, void *data)
4184{
4185 window->user_data = data;
4186}
4187
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004188void *
4189window_get_user_data(struct window *window)
4190{
4191 return window->user_data;
4192}
4193
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004194void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004195window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004196 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004197{
4198 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004199}
4200
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004201void
4202window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004203 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004204{
4205 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004206}
4207
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004208void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004209window_set_data_handler(struct window *window, window_data_handler_t handler)
4210{
4211 window->data_handler = handler;
4212}
4213
4214void
4215window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4216{
4217 window->drop_handler = handler;
4218}
4219
4220void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004221window_set_close_handler(struct window *window,
4222 window_close_handler_t handler)
4223{
4224 window->close_handler = handler;
4225}
4226
4227void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004228window_set_fullscreen_handler(struct window *window,
4229 window_fullscreen_handler_t handler)
4230{
4231 window->fullscreen_handler = handler;
4232}
4233
4234void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004235window_set_output_handler(struct window *window,
4236 window_output_handler_t handler)
4237{
4238 window->output_handler = handler;
4239}
4240
4241void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004242window_set_title(struct window *window, const char *title)
4243{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004244 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004245 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004246 if (window->frame) {
4247 frame_set_title(window->frame->frame, title);
4248 widget_schedule_redraw(window->frame->widget);
4249 }
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004250 if (window->shell_surface)
4251 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004252}
4253
4254const char *
4255window_get_title(struct window *window)
4256{
4257 return window->title;
4258}
4259
4260void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004261window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4262{
4263 struct text_cursor_position *text_cursor_position =
4264 window->display->text_cursor_position;
4265
Scott Moreau9295ce02012-06-01 12:46:10 -06004266 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004267 return;
4268
4269 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004270 window->main_surface->surface,
4271 wl_fixed_from_int(x),
4272 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004273}
4274
4275void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004276window_damage(struct window *window, int32_t x, int32_t y,
4277 int32_t width, int32_t height)
4278{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004279 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004280}
4281
Casey Dahlin9074db52012-04-19 22:50:09 -04004282static void
4283surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004284 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004285{
Rob Bradford7507b572012-05-15 17:55:34 +01004286 struct window *window = data;
4287 struct output *output;
4288 struct output *output_found = NULL;
4289 struct window_output *window_output;
4290
4291 wl_list_for_each(output, &window->display->output_list, link) {
4292 if (output->output == wl_output) {
4293 output_found = output;
4294 break;
4295 }
4296 }
4297
4298 if (!output_found)
4299 return;
4300
Brian Lovinbc919262013-08-07 15:34:59 -07004301 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004302 window_output->output = output_found;
4303
4304 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004305
4306 if (window->output_handler)
4307 window->output_handler(window, output_found, 1,
4308 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004309}
4310
4311static void
4312surface_leave(void *data,
4313 struct wl_surface *wl_surface, struct wl_output *output)
4314{
Rob Bradford7507b572012-05-15 17:55:34 +01004315 struct window *window = data;
4316 struct window_output *window_output;
4317 struct window_output *window_output_found = NULL;
4318
4319 wl_list_for_each(window_output, &window->window_output_list, link) {
4320 if (window_output->output->output == output) {
4321 window_output_found = window_output;
4322 break;
4323 }
4324 }
4325
4326 if (window_output_found) {
4327 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004328
4329 if (window->output_handler)
4330 window->output_handler(window, window_output->output,
4331 0, window->user_data);
4332
Rob Bradford7507b572012-05-15 17:55:34 +01004333 free(window_output_found);
4334 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004335}
4336
4337static const struct wl_surface_listener surface_listener = {
4338 surface_enter,
4339 surface_leave
4340};
4341
Pekka Paalanen4e373742013-02-13 16:17:13 +02004342static struct surface *
4343surface_create(struct window *window)
4344{
4345 struct display *display = window->display;
4346 struct surface *surface;
4347
Brian Lovinbc919262013-08-07 15:34:59 -07004348 surface = xmalloc(sizeof *surface);
4349 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004350 if (!surface)
4351 return NULL;
4352
4353 surface->window = window;
4354 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004355 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004356 wl_surface_add_listener(surface->surface, &surface_listener, window);
4357
Pekka Paalanen35e82632013-04-25 13:57:48 +03004358 wl_list_insert(&window->subsurface_list, &surface->link);
4359
Pekka Paalanen4e373742013-02-13 16:17:13 +02004360 return surface;
4361}
4362
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004363static struct window *
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004364window_create_internal(struct display *display, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004365{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004366 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004367 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004368
Peter Huttererf3d62272013-08-08 11:57:05 +10004369 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004370 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004371 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004372
4373 surface = surface_create(window);
4374 window->main_surface = surface;
4375
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004376 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004377 window->shell_surface =
4378 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004379 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004380 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004381 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004382
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004383 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004384 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004385 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004386 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004387
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004388 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004389#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004390 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004391#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004392 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004393#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004394 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004395 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004396
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004397 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004398 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004399 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004400
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004401 if (window->shell_surface) {
4402 wl_shell_surface_set_user_data(window->shell_surface, window);
4403 wl_shell_surface_add_listener(window->shell_surface,
4404 &shell_surface_listener, window);
4405 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004406
Rob Bradford7507b572012-05-15 17:55:34 +01004407 wl_list_init (&window->window_output_list);
4408
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004409 return window;
4410}
4411
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004412struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004413window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004414{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004415 return window_create_internal(display, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004416}
4417
4418struct window *
4419window_create_custom(struct display *display)
4420{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004421 return window_create_internal(display, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004422}
4423
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004424static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004425menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004426{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004427 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004428 int next;
4429
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004430 frame_interior(menu->frame, &x, &y, &width, &height);
4431 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004432 if (menu->current != next) {
4433 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004434 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004435 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004436}
4437
4438static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004439menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004440 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004441 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004442{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004443 struct menu *menu = data;
4444
4445 if (widget == menu->widget)
4446 menu_set_item(data, y);
4447
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004448 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004449}
4450
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004451static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004452menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004453 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004454{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004455 struct menu *menu = data;
4456
4457 if (widget == menu->widget)
4458 menu_set_item(data, y);
4459
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004460 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004461}
4462
4463static void
4464menu_leave_handler(struct widget *widget, struct input *input, void *data)
4465{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004466 struct menu *menu = data;
4467
4468 if (widget == menu->widget)
4469 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004470}
4471
4472static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004473menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004474 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004475 uint32_t button, enum wl_pointer_button_state state,
4476 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004477
4478{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004479 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004480
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004481 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4482 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004483 /* Either relase after press-drag-release or
4484 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004485 menu->func(menu->parent, input,
4486 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004487 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004488 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004489 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004490 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004491 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004492}
4493
4494static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004495menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004496{
4497 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004498 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004499 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004500
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004501 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004502
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004503 frame_repaint(menu->frame, cr);
4504 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004505
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004506 theme_set_background_source(menu->window->display->theme,
4507 cr, THEME_FRAME_ACTIVE);
4508 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004509 cairo_fill(cr);
4510
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004511 cairo_select_font_face(cr, "sans",
4512 CAIRO_FONT_SLANT_NORMAL,
4513 CAIRO_FONT_WEIGHT_NORMAL);
4514 cairo_set_font_size(cr, 12);
4515
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004516 for (i = 0; i < menu->count; i++) {
4517 if (i == menu->current) {
4518 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004519 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004520 cairo_fill(cr);
4521 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004522 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004523 cairo_show_text(cr, menu->entries[i]);
4524 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004525 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4526 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004527 cairo_show_text(cr, menu->entries[i]);
4528 }
4529 }
4530
4531 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004532}
4533
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004534void
4535window_show_menu(struct display *display,
4536 struct input *input, uint32_t time, struct window *parent,
4537 int32_t x, int32_t y,
4538 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004539{
4540 struct window *window;
4541 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004542 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004543
4544 menu = malloc(sizeof *menu);
4545 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004546 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004547
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004548 window = window_create_internal(parent->display, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004549 if (!window) {
4550 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004551 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004552 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004553
4554 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004555 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004556 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004557 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4558 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004559 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004560 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004561 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004562 menu->entries = entries;
4563 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004564 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004565 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004566 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004567 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004568 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004569 window->type = TYPE_MENU;
4570 window->x = x;
4571 window->y = y;
4572
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004573 input_ungrab(input);
4574
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004575 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004576 widget_set_enter_handler(menu->widget, menu_enter_handler);
4577 widget_set_leave_handler(menu->widget, menu_leave_handler);
4578 widget_set_motion_handler(menu->widget, menu_motion_handler);
4579 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004580
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004581 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004582 frame_resize_inside(menu->frame, 200, count * 20);
4583 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4584 window_schedule_resize(window, frame_width(menu->frame),
4585 frame_height(menu->frame));
4586
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004587 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4588 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4589 display_get_serial(window->display),
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004590 parent->main_surface->surface,
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004591 window->x - ix, window->y - iy, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004592}
4593
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004594void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004595window_set_buffer_type(struct window *window, enum window_buffer_type type)
4596{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004597 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004598}
4599
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004600void
4601window_set_preferred_format(struct window *window,
4602 enum preferred_format format)
4603{
4604 window->preferred_format = format;
4605}
4606
Pekka Paalanen35e82632013-04-25 13:57:48 +03004607struct widget *
4608window_add_subsurface(struct window *window, void *data,
4609 enum subsurface_mode default_mode)
4610{
4611 struct widget *widget;
4612 struct surface *surface;
4613 struct wl_surface *parent;
4614 struct wl_subcompositor *subcompo = window->display->subcompositor;
4615
Pekka Paalanen35e82632013-04-25 13:57:48 +03004616 surface = surface_create(window);
4617 widget = widget_create(window, surface, data);
4618 wl_list_init(&widget->link);
4619 surface->widget = widget;
4620
4621 parent = window->main_surface->surface;
4622 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4623 surface->surface,
4624 parent);
4625 surface->synchronized = 1;
4626
4627 switch (default_mode) {
4628 case SUBSURFACE_SYNCHRONIZED:
4629 surface->synchronized_default = 1;
4630 break;
4631 case SUBSURFACE_DESYNCHRONIZED:
4632 surface->synchronized_default = 0;
4633 break;
4634 default:
4635 assert(!"bad enum subsurface_mode");
4636 }
4637
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004638 window->resize_needed = 1;
4639 window_schedule_redraw(window);
4640
Pekka Paalanen35e82632013-04-25 13:57:48 +03004641 return widget;
4642}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004643
4644static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004645display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004646 struct wl_output *wl_output,
4647 int x, int y,
4648 int physical_width,
4649 int physical_height,
4650 int subpixel,
4651 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004652 const char *model,
4653 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004654{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004655 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004656
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004657 output->allocation.x = x;
4658 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004659 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004660}
4661
4662static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004663display_handle_done(void *data,
4664 struct wl_output *wl_output)
4665{
4666}
4667
4668static void
4669display_handle_scale(void *data,
4670 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004671 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004672{
4673 struct output *output = data;
4674
4675 output->scale = scale;
4676}
4677
4678static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004679display_handle_mode(void *data,
4680 struct wl_output *wl_output,
4681 uint32_t flags,
4682 int width,
4683 int height,
4684 int refresh)
4685{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004686 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004687 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004688
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004689 if (flags & WL_OUTPUT_MODE_CURRENT) {
4690 output->allocation.width = width;
4691 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004692 if (display->output_configure_handler)
4693 (*display->output_configure_handler)(
4694 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004695 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004696}
4697
4698static const struct wl_output_listener output_listener = {
4699 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004700 display_handle_mode,
4701 display_handle_done,
4702 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004703};
4704
4705static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004706display_add_output(struct display *d, uint32_t id)
4707{
4708 struct output *output;
4709
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004710 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004711 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004712 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004713 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004714 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004715 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004716 wl_list_insert(d->output_list.prev, &output->link);
4717
4718 wl_output_add_listener(output->output, &output_listener, output);
4719}
4720
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004721static void
4722output_destroy(struct output *output)
4723{
4724 if (output->destroy_handler)
4725 (*output->destroy_handler)(output, output->user_data);
4726
4727 wl_output_destroy(output->output);
4728 wl_list_remove(&output->link);
4729 free(output);
4730}
4731
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004732static void
4733display_destroy_output(struct display *d, uint32_t id)
4734{
4735 struct output *output;
4736
4737 wl_list_for_each(output, &d->output_list, link) {
4738 if (output->server_output_id == id) {
4739 output_destroy(output);
4740 break;
4741 }
4742 }
4743}
4744
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004745void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004746display_set_global_handler(struct display *display,
4747 display_global_handler_t handler)
4748{
4749 struct global *global;
4750
4751 display->global_handler = handler;
4752 if (!handler)
4753 return;
4754
4755 wl_list_for_each(global, &display->global_list, link)
4756 display->global_handler(display,
4757 global->name, global->interface,
4758 global->version, display->user_data);
4759}
4760
4761void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004762display_set_global_handler_remove(struct display *display,
4763 display_global_handler_t remove_handler)
4764{
4765 display->global_handler_remove = remove_handler;
4766 if (!remove_handler)
4767 return;
4768}
4769
4770void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004771display_set_output_configure_handler(struct display *display,
4772 display_output_handler_t handler)
4773{
4774 struct output *output;
4775
4776 display->output_configure_handler = handler;
4777 if (!handler)
4778 return;
4779
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004780 wl_list_for_each(output, &display->output_list, link) {
4781 if (output->allocation.width == 0 &&
4782 output->allocation.height == 0)
4783 continue;
4784
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004785 (*display->output_configure_handler)(output,
4786 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004787 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004788}
4789
4790void
4791output_set_user_data(struct output *output, void *data)
4792{
4793 output->user_data = data;
4794}
4795
4796void *
4797output_get_user_data(struct output *output)
4798{
4799 return output->user_data;
4800}
4801
4802void
4803output_set_destroy_handler(struct output *output,
4804 display_output_handler_t handler)
4805{
4806 output->destroy_handler = handler;
4807 /* FIXME: implement this, once we have way to remove outputs */
4808}
4809
4810void
Scott Moreau4e072362012-09-29 02:03:11 -06004811output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004812{
Scott Moreau4e072362012-09-29 02:03:11 -06004813 struct rectangle allocation = output->allocation;
4814
4815 switch (output->transform) {
4816 case WL_OUTPUT_TRANSFORM_90:
4817 case WL_OUTPUT_TRANSFORM_270:
4818 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4819 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4820 /* Swap width and height */
4821 allocation.width = output->allocation.height;
4822 allocation.height = output->allocation.width;
4823 break;
4824 }
4825
4826 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004827}
4828
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004829struct wl_output *
4830output_get_wl_output(struct output *output)
4831{
4832 return output->output;
4833}
4834
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004835enum wl_output_transform
4836output_get_transform(struct output *output)
4837{
4838 return output->transform;
4839}
4840
Alexander Larssonafd319a2013-05-22 14:41:27 +02004841uint32_t
4842output_get_scale(struct output *output)
4843{
4844 return output->scale;
4845}
4846
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004847static void
Daniel Stone97f68542012-05-30 16:32:01 +01004848fini_xkb(struct input *input)
4849{
4850 xkb_state_unref(input->xkb.state);
4851 xkb_map_unref(input->xkb.keymap);
4852}
4853
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004854#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004855
Daniel Stone97f68542012-05-30 16:32:01 +01004856static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004857display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004858{
4859 struct input *input;
4860
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004861 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004862 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004863 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004864 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004865 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004866 input->pointer_focus = NULL;
4867 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004868 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004869 wl_list_insert(d->input_list.prev, &input->link);
4870
Daniel Stone37816df2012-05-16 18:45:18 +01004871 wl_seat_add_listener(input->seat, &seat_listener, input);
4872 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004873
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004874 input->data_device =
4875 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004876 input->seat);
4877 wl_data_device_add_listener(input->data_device, &data_device_listener,
4878 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004879
4880 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004881
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004882 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4883 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004884 input->repeat_task.run = keyboard_repeat_func;
4885 display_watch_fd(d, input->repeat_timer_fd,
4886 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004887}
4888
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004889static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004890input_destroy(struct input *input)
4891{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004892 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004893 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004894
4895 if (input->drag_offer)
4896 data_offer_destroy(input->drag_offer);
4897
4898 if (input->selection_offer)
4899 data_offer_destroy(input->selection_offer);
4900
4901 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004902
4903 if (input->display->seat_version >= 3) {
4904 if (input->pointer)
4905 wl_pointer_release(input->pointer);
4906 if (input->keyboard)
4907 wl_keyboard_release(input->keyboard);
4908 }
4909
Daniel Stone97f68542012-05-30 16:32:01 +01004910 fini_xkb(input);
4911
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004912 wl_surface_destroy(input->pointer_surface);
4913
Pekka Paalanene1207c72011-12-16 12:02:09 +02004914 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004915 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004916 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004917 free(input);
4918}
4919
4920static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004921init_workspace_manager(struct display *d, uint32_t id)
4922{
4923 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004924 wl_registry_bind(d->registry, id,
4925 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004926 if (d->workspace_manager != NULL)
4927 workspace_manager_add_listener(d->workspace_manager,
4928 &workspace_manager_listener,
4929 d);
4930}
4931
4932static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004933shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4934{
4935 struct display *d = data;
4936
4937 if (format == WL_SHM_FORMAT_RGB565)
4938 d->has_rgb565 = 1;
4939}
4940
4941struct wl_shm_listener shm_listener = {
4942 shm_format
4943};
4944
4945static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004946registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4947 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004948{
4949 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004950 struct global *global;
4951
Brian Lovinbc919262013-08-07 15:34:59 -07004952 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004953 global->name = id;
4954 global->interface = strdup(interface);
4955 global->version = version;
4956 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004957
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004958 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004959 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004960 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004961 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004962 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004963 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01004964 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004965 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004966 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004967 d->shell = wl_registry_bind(registry,
4968 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004969 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004970 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004971 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004972 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4973 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004974 wl_registry_bind(registry, id,
4975 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004976 } else if (strcmp(interface, "text_cursor_position") == 0) {
4977 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004978 wl_registry_bind(registry, id,
4979 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004980 } else if (strcmp(interface, "workspace_manager") == 0) {
4981 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004982 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4983 d->subcompositor =
4984 wl_registry_bind(registry, id,
4985 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004986 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004987
4988 if (d->global_handler)
4989 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004990}
4991
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004992static void
4993registry_handle_global_remove(void *data, struct wl_registry *registry,
4994 uint32_t name)
4995{
4996 struct display *d = data;
4997 struct global *global;
4998 struct global *tmp;
4999
5000 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5001 if (global->name != name)
5002 continue;
5003
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005004 if (strcmp(global->interface, "wl_output") == 0)
5005 display_destroy_output(d, name);
5006
5007 /* XXX: Should destroy remaining bound globals */
5008
5009 if (d->global_handler_remove)
5010 d->global_handler_remove(d, name, global->interface,
5011 global->version, d->user_data);
5012
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005013 wl_list_remove(&global->link);
5014 free(global->interface);
5015 free(global);
5016 }
5017}
5018
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005019void *
5020display_bind(struct display *display, uint32_t name,
5021 const struct wl_interface *interface, uint32_t version)
5022{
5023 return wl_registry_bind(display->registry, name, interface, version);
5024}
5025
5026static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005027 registry_handle_global,
5028 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005029};
5030
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005031#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005032static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005033init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005034{
5035 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005036 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005037
Rob Clark6396ed32012-03-11 19:48:41 -05005038#ifdef USE_CAIRO_GLESV2
5039# define GL_BIT EGL_OPENGL_ES2_BIT
5040#else
5041# define GL_BIT EGL_OPENGL_BIT
5042#endif
5043
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005044 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005045 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005046 EGL_RED_SIZE, 1,
5047 EGL_GREEN_SIZE, 1,
5048 EGL_BLUE_SIZE, 1,
5049 EGL_ALPHA_SIZE, 1,
5050 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005051 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005052 EGL_NONE
5053 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005054
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005055#ifdef USE_CAIRO_GLESV2
5056 static const EGLint context_attribs[] = {
5057 EGL_CONTEXT_CLIENT_VERSION, 2,
5058 EGL_NONE
5059 };
5060 EGLint api = EGL_OPENGL_ES_API;
5061#else
5062 EGLint *context_attribs = NULL;
5063 EGLint api = EGL_OPENGL_API;
5064#endif
5065
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005066 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005067 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005068 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005069 return -1;
5070 }
5071
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005072 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005073 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005074 return -1;
5075 }
5076
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005077 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5078 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005079 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005080 return -1;
5081 }
5082
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005083 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005084 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005085 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005086 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005087 return -1;
5088 }
5089
Benjamin Franzke0c991632011-09-27 21:57:31 +02005090 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5091 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005092 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005093 return -1;
5094 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005095
5096 return 0;
5097}
5098
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005099static void
5100fini_egl(struct display *display)
5101{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005102 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005103
5104 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5105 EGL_NO_CONTEXT);
5106
5107 eglTerminate(display->dpy);
5108 eglReleaseThread();
5109}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005110#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005111
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005112static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005113init_dummy_surface(struct display *display)
5114{
5115 int len;
5116 void *data;
5117
5118 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5119 data = malloc(len);
5120 display->dummy_surface =
5121 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5122 1, 1, len);
5123 display->dummy_surface_data = data;
5124}
5125
5126static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005127handle_display_data(struct task *task, uint32_t events)
5128{
5129 struct display *display =
5130 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005131 struct epoll_event ep;
5132 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005133
5134 display->display_fd_events = events;
5135
5136 if (events & EPOLLERR || events & EPOLLHUP) {
5137 display_exit(display);
5138 return;
5139 }
5140
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005141 if (events & EPOLLIN) {
5142 ret = wl_display_dispatch(display->display);
5143 if (ret == -1) {
5144 display_exit(display);
5145 return;
5146 }
5147 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005148
5149 if (events & EPOLLOUT) {
5150 ret = wl_display_flush(display->display);
5151 if (ret == 0) {
5152 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5153 ep.data.ptr = &display->display_task;
5154 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5155 display->display_fd, &ep);
5156 } else if (ret == -1 && errno != EAGAIN) {
5157 display_exit(display);
5158 return;
5159 }
5160 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005161}
5162
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005163static void
5164log_handler(const char *format, va_list args)
5165{
5166 vfprintf(stderr, format, args);
5167}
5168
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005169struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005170display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005171{
5172 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005173
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005174 wl_log_set_handler_client(log_handler);
5175
Peter Huttererf3d62272013-08-08 11:57:05 +10005176 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005177 if (d == NULL)
5178 return NULL;
5179
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005180 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005181 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005182 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005183 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005184 return NULL;
5185 }
5186
Rob Bradford5ab9c752013-07-26 16:29:43 +01005187 d->xkb_context = xkb_context_new(0);
5188 if (d->xkb_context == NULL) {
5189 fprintf(stderr, "Failed to create XKB context\n");
5190 free(d);
5191 return NULL;
5192 }
5193
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005194 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005195 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005196 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005197 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5198 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005199
5200 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005201 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005202 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005203 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005204
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005205 d->workspace = 0;
5206 d->workspace_count = 1;
5207
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005208 d->registry = wl_display_get_registry(d->display);
5209 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005210
5211 if (wl_display_dispatch(d->display) < 0) {
5212 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5213 return NULL;
5214 }
5215
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005216#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005217 if (init_egl(d) < 0)
5218 fprintf(stderr, "EGL does not seem to work, "
5219 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005220#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005221
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005222 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005223
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005224 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005225
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005226 wl_list_init(&d->window_list);
5227
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005228 init_dummy_surface(d);
5229
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005230 return d;
5231}
5232
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005233static void
5234display_destroy_outputs(struct display *display)
5235{
5236 struct output *tmp;
5237 struct output *output;
5238
5239 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5240 output_destroy(output);
5241}
5242
Pekka Paalanene1207c72011-12-16 12:02:09 +02005243static void
5244display_destroy_inputs(struct display *display)
5245{
5246 struct input *tmp;
5247 struct input *input;
5248
5249 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5250 input_destroy(input);
5251}
5252
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005253void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005254display_destroy(struct display *display)
5255{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005256 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005257 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5258 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005259
5260 if (!wl_list_empty(&display->deferred_list))
5261 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5262
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005263 cairo_surface_destroy(display->dummy_surface);
5264 free(display->dummy_surface_data);
5265
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005266 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005267 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005268
Daniel Stone97f68542012-05-30 16:32:01 +01005269 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005270
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005271 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005272 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005273
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005274#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005275 if (display->argb_device)
5276 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005277#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005278
Pekka Paalanen35e82632013-04-25 13:57:48 +03005279 if (display->subcompositor)
5280 wl_subcompositor_destroy(display->subcompositor);
5281
Pekka Paalanenc2052982011-12-16 11:41:32 +02005282 if (display->shell)
5283 wl_shell_destroy(display->shell);
5284
5285 if (display->shm)
5286 wl_shm_destroy(display->shm);
5287
5288 if (display->data_device_manager)
5289 wl_data_device_manager_destroy(display->data_device_manager);
5290
5291 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005292 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005293
5294 close(display->epoll_fd);
5295
U. Artie Eoff44874d92012-10-02 21:12:35 -07005296 if (!(display->display_fd_events & EPOLLERR) &&
5297 !(display->display_fd_events & EPOLLHUP))
5298 wl_display_flush(display->display);
5299
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005300 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005301 free(display);
5302}
5303
5304void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005305display_set_user_data(struct display *display, void *data)
5306{
5307 display->user_data = data;
5308}
5309
5310void *
5311display_get_user_data(struct display *display)
5312{
5313 return display->user_data;
5314}
5315
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005316struct wl_display *
5317display_get_display(struct display *display)
5318{
5319 return display->display;
5320}
5321
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005322int
5323display_has_subcompositor(struct display *display)
5324{
5325 if (display->subcompositor)
5326 return 1;
5327
5328 wl_display_roundtrip(display->display);
5329
5330 return display->subcompositor != NULL;
5331}
5332
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005333cairo_device_t *
5334display_get_cairo_device(struct display *display)
5335{
5336 return display->argb_device;
5337}
5338
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005339struct output *
5340display_get_output(struct display *display)
5341{
5342 return container_of(display->output_list.next, struct output, link);
5343}
5344
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005345struct wl_compositor *
5346display_get_compositor(struct display *display)
5347{
5348 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005349}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005350
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005351uint32_t
5352display_get_serial(struct display *display)
5353{
5354 return display->serial;
5355}
5356
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005357EGLDisplay
5358display_get_egl_display(struct display *d)
5359{
5360 return d->dpy;
5361}
5362
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005363struct wl_data_source *
5364display_create_data_source(struct display *display)
5365{
5366 return wl_data_device_manager_create_data_source(display->data_device_manager);
5367}
5368
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005369EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005370display_get_argb_egl_config(struct display *d)
5371{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005372 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005373}
5374
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005375int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005376display_acquire_window_surface(struct display *display,
5377 struct window *window,
5378 EGLContext ctx)
5379{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005380 struct surface *surface = window->main_surface;
5381
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005382 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005383 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005384
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005385 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005386 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005387}
5388
5389void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005390display_release_window_surface(struct display *display,
5391 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005392{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005393 struct surface *surface = window->main_surface;
5394
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005395 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005396 return;
5397
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005398 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005399}
5400
5401void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005402display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005403{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005404 wl_list_insert(&display->deferred_list, &task->link);
5405}
5406
5407void
5408display_watch_fd(struct display *display,
5409 int fd, uint32_t events, struct task *task)
5410{
5411 struct epoll_event ep;
5412
5413 ep.events = events;
5414 ep.data.ptr = task;
5415 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5416}
5417
5418void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005419display_unwatch_fd(struct display *display, int fd)
5420{
5421 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5422}
5423
5424void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005425display_run(struct display *display)
5426{
5427 struct task *task;
5428 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005429 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005430
Pekka Paalanen826d7952011-12-15 10:14:07 +02005431 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005432 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005433 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005434 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005435 struct task, link);
5436 wl_list_remove(&task->link);
5437 task->run(task, 0);
5438 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005439
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005440 wl_display_dispatch_pending(display->display);
5441
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005442 if (!display->running)
5443 break;
5444
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005445 ret = wl_display_flush(display->display);
5446 if (ret < 0 && errno == EAGAIN) {
5447 ep[0].events =
5448 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5449 ep[0].data.ptr = &display->display_task;
5450
5451 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5452 display->display_fd, &ep[0]);
5453 } else if (ret < 0) {
5454 break;
5455 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005456
5457 count = epoll_wait(display->epoll_fd,
5458 ep, ARRAY_LENGTH(ep), -1);
5459 for (i = 0; i < count; i++) {
5460 task = ep[i].data.ptr;
5461 task->run(task, ep[i].events);
5462 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005463 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005464}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005465
5466void
5467display_exit(struct display *display)
5468{
5469 display->running = 0;
5470}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005471
5472void
5473keysym_modifiers_add(struct wl_array *modifiers_map,
5474 const char *name)
5475{
5476 size_t len = strlen(name) + 1;
5477 char *p;
5478
5479 p = wl_array_add(modifiers_map, len);
5480
5481 if (p == NULL)
5482 return;
5483
5484 strncpy(p, name, len);
5485}
5486
5487static xkb_mod_index_t
5488keysym_modifiers_get_index(struct wl_array *modifiers_map,
5489 const char *name)
5490{
5491 xkb_mod_index_t index = 0;
5492 char *p = modifiers_map->data;
5493
5494 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5495 if (strcmp(p, name) == 0)
5496 return index;
5497
5498 index++;
5499 p += strlen(p) + 1;
5500 }
5501
5502 return XKB_MOD_INVALID;
5503}
5504
5505xkb_mod_mask_t
5506keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5507 const char *name)
5508{
5509 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5510
5511 if (index == XKB_MOD_INVALID)
5512 return XKB_MOD_INVALID;
5513
5514 return 1 << index;
5515}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005516
5517void *
5518fail_on_null(void *p)
5519{
5520 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005521 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005522 exit(EXIT_FAILURE);
5523 }
5524
5525 return p;
5526}
5527
5528void *
5529xmalloc(size_t s)
5530{
5531 return fail_on_null(malloc(s));
5532}
5533
Peter Huttererf3d62272013-08-08 11:57:05 +10005534void *
5535xzalloc(size_t s)
5536{
5537 return fail_on_null(zalloc(s));
5538}
5539
Kristian Høgsbergce278412013-07-25 15:20:20 -07005540char *
5541xstrdup(const char *s)
5542{
5543 return fail_on_null(strdup(s));
5544}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005545
5546void *
5547xrealloc(char *p, size_t s)
5548{
5549 return fail_on_null(realloc(p, s));
5550}