blob: 84d3e73964dbff717022697969c4d019c4f4dd11 [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;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120
121 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100122
123 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200124
125 uint32_t workspace;
126 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200127
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200131
132 int has_rgb565;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133};
134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400136 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400138 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500139 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500141 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_CUSTOM
143};
Rob Bradford7507b572012-05-15 17:55:34 +0100144
145struct window_output {
146 struct output *output;
147 struct wl_list link;
148};
149
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150struct toysurface {
151 /*
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200155 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200156 * If flags has SURFACE_HINT_RESIZE set, the user is
157 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200158 * Returns the Cairo surface to draw to.
159 */
160 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200161 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200162 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200163
164 /*
165 * Post the surface to the server, returning the server allocation
166 * rectangle. The Cairo surface from prepare() must be destroyed
167 * after calling this.
168 */
169 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200170 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200171 struct rectangle *server_allocation);
172
173 /*
174 * Make the toysurface current with the given EGL context.
175 * Returns 0 on success, and negative of failure.
176 */
177 int (*acquire)(struct toysurface *base, EGLContext ctx);
178
179 /*
180 * Release the toysurface from the EGL context, returning control
181 * to Cairo.
182 */
183 void (*release)(struct toysurface *base);
184
185 /*
186 * Destroy the toysurface, including the Cairo surface, any
187 * backing storage, and the Wayland protocol objects.
188 */
189 void (*destroy)(struct toysurface *base);
190};
191
Pekka Paalanen4e373742013-02-13 16:17:13 +0200192struct surface {
193 struct window *window;
194
195 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300196 struct wl_subsurface *subsurface;
197 int synchronized;
198 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200199 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200200 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300201 int redraw_needed;
202 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300203 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200204
205 struct rectangle allocation;
206 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200207
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200208 struct wl_region *input_region;
209 struct wl_region *opaque_region;
210
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200211 enum window_buffer_type buffer_type;
212 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200213 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200214
215 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300216
217 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200218};
219
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500220struct window {
221 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500222 struct window *parent;
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
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200255 struct 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;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400287};
288
Rusty Lynch041815a2013-08-08 21:20:38 -0700289struct touch_point {
290 int32_t id;
291 struct widget *widget;
292 struct wl_list link;
293};
294
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400295struct input {
296 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100297 struct wl_seat *seat;
298 struct wl_pointer *pointer;
299 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700300 struct wl_touch *touch;
301 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400302 struct window *pointer_focus;
303 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700304 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300305 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300306 uint32_t cursor_anim_start;
307 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300308 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400309 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400310 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400311 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400312 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400313 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400314
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500315 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500316 struct widget *grab;
317 uint32_t grab_button;
318
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400319 struct wl_data_device *data_device;
320 struct data_offer *drag_offer;
321 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100322
323 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100324 struct xkb_keymap *keymap;
325 struct xkb_state *state;
326 xkb_mod_mask_t control_mask;
327 xkb_mod_mask_t alt_mask;
328 xkb_mod_mask_t shift_mask;
329 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400330
331 struct task repeat_task;
332 int repeat_timer_fd;
333 uint32_t repeat_sym;
334 uint32_t repeat_key;
335 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400336};
337
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500338struct output {
339 struct display *display;
340 struct wl_output *output;
341 struct rectangle allocation;
342 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600343 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200344 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200345
346 display_output_handler_t destroy_handler;
347 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500348};
349
Martin Minarik1998b152012-05-10 02:04:35 +0200350enum frame_button_action {
351 FRAME_BUTTON_NULL = 0,
352 FRAME_BUTTON_ICON = 1,
353 FRAME_BUTTON_CLOSE = 2,
354 FRAME_BUTTON_MINIMIZE = 3,
355 FRAME_BUTTON_MAXIMIZE = 4,
356};
357
358enum frame_button_pointer {
359 FRAME_BUTTON_DEFAULT = 0,
360 FRAME_BUTTON_OVER = 1,
361 FRAME_BUTTON_ACTIVE = 2,
362};
363
364enum frame_button_align {
365 FRAME_BUTTON_RIGHT = 0,
366 FRAME_BUTTON_LEFT = 1,
367};
368
369enum frame_button_decoration {
370 FRAME_BUTTON_NONE = 0,
371 FRAME_BUTTON_FANCY = 1,
372};
373
374struct frame_button {
375 struct widget *widget;
376 struct frame *frame;
377 cairo_surface_t *icon;
378 enum frame_button_action type;
379 enum frame_button_pointer state;
380 struct wl_list link; /* buttons_list */
381 enum frame_button_align align;
382 enum frame_button_decoration decoration;
383};
384
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500385struct frame {
386 struct widget *widget;
387 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200388 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500389};
390
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500391struct menu {
392 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500393 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500394 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500395 const char **entries;
396 uint32_t time;
397 int current;
398 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400399 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500400 menu_func_t func;
401};
402
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300403struct tooltip {
404 struct widget *parent;
405 struct window *window;
406 struct widget *widget;
407 char *entry;
408 struct task tooltip_task;
409 int tooltip_fd;
410 float x, y;
411};
412
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700413struct shm_pool {
414 struct wl_shm_pool *pool;
415 size_t size;
416 size_t used;
417 void *data;
418};
419
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400420enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300421 CURSOR_DEFAULT = 100,
422 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400423};
424
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500425enum window_location {
426 WINDOW_INTERIOR = 0,
427 WINDOW_RESIZING_TOP = 1,
428 WINDOW_RESIZING_BOTTOM = 2,
429 WINDOW_RESIZING_LEFT = 4,
430 WINDOW_RESIZING_TOP_LEFT = 5,
431 WINDOW_RESIZING_BOTTOM_LEFT = 6,
432 WINDOW_RESIZING_RIGHT = 8,
433 WINDOW_RESIZING_TOP_RIGHT = 9,
434 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
435 WINDOW_RESIZING_MASK = 15,
436 WINDOW_EXTERIOR = 16,
437 WINDOW_TITLEBAR = 17,
438 WINDOW_CLIENT_AREA = 18,
439};
440
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200441static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400442
Pekka Paalanen97777442013-05-22 10:20:05 +0300443/* #define DEBUG */
444
445#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300446
447static void
448debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
449__attribute__ ((format (printf, 4, 5)));
450
451static void
452debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
453{
454 va_list ap;
455 struct timeval tv;
456
457 gettimeofday(&tv, NULL);
458 fprintf(stderr, "%8ld.%03ld ",
459 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
460
461 if (proxy)
462 fprintf(stderr, "%s@%d ",
463 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
464
465 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
466 fprintf(stderr, "%s ", func);
467
468 va_start(ap, fmt);
469 vfprintf(stderr, fmt, ap);
470 va_end(ap);
471}
472
473#define DBG(fmt, ...) \
474 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
475
476#define DBG_OBJ(obj, fmt, ...) \
477 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
478
479#else
480
481#define DBG(...) do {} while (0)
482#define DBG_OBJ(...) do {} while (0)
483
484#endif
485
Alexander Larsson1818e312013-05-22 14:41:31 +0200486static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200487surface_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 +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
508static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200509buffer_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 +0200510{
511 int32_t tmp;
512
513 switch (buffer_transform) {
514 case WL_OUTPUT_TRANSFORM_90:
515 case WL_OUTPUT_TRANSFORM_270:
516 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
517 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
518 tmp = *width;
519 *width = *height;
520 *height = tmp;
521 break;
522 default:
523 break;
524 }
525
526 *width /= buffer_scale;
527 *height /= buffer_scale;
528}
529
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500530#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400531
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200532struct egl_window_surface {
533 struct toysurface base;
534 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100535 struct display *display;
536 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200537 struct wl_egl_window *egl_window;
538 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100539};
540
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200541static struct egl_window_surface *
542to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100543{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200544 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100545}
546
547static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200548egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200549 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200550 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100551{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200552 struct egl_window_surface *surface = to_egl_window_surface(base);
553
Alexander Larsson1818e312013-05-22 14:41:31 +0200554 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
555
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200556 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
557 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
558
559 return cairo_surface_reference(surface->cairo_surface);
560}
561
562static void
563egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200564 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200565 struct rectangle *server_allocation)
566{
567 struct egl_window_surface *surface = to_egl_window_surface(base);
568
569 cairo_gl_surface_swapbuffers(surface->cairo_surface);
570 wl_egl_window_get_attached_size(surface->egl_window,
571 &server_allocation->width,
572 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200573
574 buffer_to_surface_size (buffer_transform, buffer_scale,
575 &server_allocation->width,
576 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200577}
578
579static int
580egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
581{
582 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200583 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100584
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200585 device = cairo_surface_get_device(surface->cairo_surface);
586 if (!device)
587 return -1;
588
589 if (!ctx) {
590 if (device == surface->display->argb_device)
591 ctx = surface->display->argb_ctx;
592 else
593 assert(0);
594 }
595
596 cairo_device_flush(device);
597 cairo_device_acquire(device);
598 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
599 surface->egl_surface, ctx))
600 fprintf(stderr, "failed to make surface current\n");
601
602 return 0;
603}
604
605static void
606egl_window_surface_release(struct toysurface *base)
607{
608 struct egl_window_surface *surface = to_egl_window_surface(base);
609 cairo_device_t *device;
610
611 device = cairo_surface_get_device(surface->cairo_surface);
612 if (!device)
613 return;
614
615 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
616 surface->display->argb_ctx))
617 fprintf(stderr, "failed to make context current\n");
618
619 cairo_device_release(device);
620}
621
622static void
623egl_window_surface_destroy(struct toysurface *base)
624{
625 struct egl_window_surface *surface = to_egl_window_surface(base);
626 struct display *d = surface->display;
627
628 cairo_surface_destroy(surface->cairo_surface);
629 eglDestroySurface(d->dpy, surface->egl_surface);
630 wl_egl_window_destroy(surface->egl_window);
631 surface->surface = NULL;
632
633 free(surface);
634}
635
636static struct toysurface *
637egl_window_surface_create(struct display *display,
638 struct wl_surface *wl_surface,
639 uint32_t flags,
640 struct rectangle *rectangle)
641{
642 struct egl_window_surface *surface;
643
Pekka Paalanenb3627362012-11-19 17:16:00 +0200644 if (display->dpy == EGL_NO_DISPLAY)
645 return NULL;
646
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200647 surface = calloc(1, sizeof *surface);
648 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649 return NULL;
650
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200651 surface->base.prepare = egl_window_surface_prepare;
652 surface->base.swap = egl_window_surface_swap;
653 surface->base.acquire = egl_window_surface_acquire;
654 surface->base.release = egl_window_surface_release;
655 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100656
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200657 surface->display = display;
658 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400659
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200660 surface->egl_window = wl_egl_window_create(surface->surface,
661 rectangle->width,
662 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100663
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200664 surface->egl_surface = eglCreateWindowSurface(display->dpy,
665 display->argb_config,
666 surface->egl_window,
667 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100668
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200669 surface->cairo_surface =
670 cairo_gl_surface_create_for_egl(display->argb_device,
671 surface->egl_surface,
672 rectangle->width,
673 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100674
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200675 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100676}
677
Pekka Paalanenb3627362012-11-19 17:16:00 +0200678#else
679
680static struct toysurface *
681egl_window_surface_create(struct display *display,
682 struct wl_surface *wl_surface,
683 uint32_t flags,
684 struct rectangle *rectangle)
685{
686 return NULL;
687}
688
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689#endif
690
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200691struct shm_surface_data {
692 struct wl_buffer *buffer;
693 struct shm_pool *pool;
694};
695
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400696struct wl_buffer *
697display_get_buffer_for_surface(struct display *display,
698 cairo_surface_t *surface)
699{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200700 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400701
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200702 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400703
704 return data->buffer;
705}
706
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500707static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700708shm_pool_destroy(struct shm_pool *pool);
709
710static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400711shm_surface_data_destroy(void *p)
712{
713 struct shm_surface_data *data = p;
714
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200715 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700716 if (data->pool)
717 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300718
719 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400720}
721
Kristian Høgsberg16626282012-04-03 11:21:27 -0400722static struct wl_shm_pool *
723make_shm_pool(struct display *display, int size, void **data)
724{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400725 struct wl_shm_pool *pool;
726 int fd;
727
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300728 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400729 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300730 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
731 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400732 return NULL;
733 }
734
735 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400736 if (*data == MAP_FAILED) {
737 fprintf(stderr, "mmap failed: %m\n");
738 close(fd);
739 return NULL;
740 }
741
742 pool = wl_shm_create_pool(display->shm, fd, size);
743
744 close(fd);
745
746 return pool;
747}
748
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700749static struct shm_pool *
750shm_pool_create(struct display *display, size_t size)
751{
752 struct shm_pool *pool = malloc(sizeof *pool);
753
754 if (!pool)
755 return NULL;
756
757 pool->pool = make_shm_pool(display, size, &pool->data);
758 if (!pool->pool) {
759 free(pool);
760 return NULL;
761 }
762
763 pool->size = size;
764 pool->used = 0;
765
766 return pool;
767}
768
769static void *
770shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
771{
772 if (pool->used + size > pool->size)
773 return NULL;
774
775 *offset = pool->used;
776 pool->used += size;
777
778 return (char *) pool->data + *offset;
779}
780
781/* destroy the pool. this does not unmap the memory though */
782static void
783shm_pool_destroy(struct shm_pool *pool)
784{
785 munmap(pool->data, pool->size);
786 wl_shm_pool_destroy(pool->pool);
787 free(pool);
788}
789
790/* Start allocating from the beginning of the pool again */
791static void
792shm_pool_reset(struct shm_pool *pool)
793{
794 pool->used = 0;
795}
796
797static int
798data_length_for_shm_surface(struct rectangle *rect)
799{
800 int stride;
801
802 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
803 rect->width);
804 return stride * rect->height;
805}
806
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500807static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700808display_create_shm_surface_from_pool(struct display *display,
809 struct rectangle *rectangle,
810 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400811{
812 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400813 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400814 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200815 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700816 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400817 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400818
819 data = malloc(sizeof *data);
820 if (data == NULL)
821 return NULL;
822
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200823 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
824 cairo_format = CAIRO_FORMAT_RGB16_565;
825 else
826 cairo_format = CAIRO_FORMAT_ARGB32;
827
828 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700829 length = stride * rectangle->height;
830 data->pool = NULL;
831 map = shm_pool_allocate(pool, length, &offset);
832
833 if (!map) {
834 free(data);
835 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400836 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400838 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200839 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400840 rectangle->width,
841 rectangle->height,
842 stride);
843
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200844 cairo_surface_set_user_data(surface, &shm_surface_data_key,
845 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400846
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200847 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
848 format = WL_SHM_FORMAT_RGB565;
849 else {
850 if (flags & SURFACE_OPAQUE)
851 format = WL_SHM_FORMAT_XRGB8888;
852 else
853 format = WL_SHM_FORMAT_ARGB8888;
854 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400855
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200856 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
857 rectangle->width,
858 rectangle->height,
859 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400860
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700861 return surface;
862}
863
864static cairo_surface_t *
865display_create_shm_surface(struct display *display,
866 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200867 struct shm_pool *alternate_pool,
868 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700869{
870 struct shm_surface_data *data;
871 struct shm_pool *pool;
872 cairo_surface_t *surface;
873
Pekka Paalanen99436862012-11-19 17:15:59 +0200874 if (alternate_pool) {
875 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700876 surface = display_create_shm_surface_from_pool(display,
877 rectangle,
878 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200879 alternate_pool);
880 if (surface) {
881 data = cairo_surface_get_user_data(surface,
882 &shm_surface_data_key);
883 goto out;
884 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700885 }
886
887 pool = shm_pool_create(display,
888 data_length_for_shm_surface(rectangle));
889 if (!pool)
890 return NULL;
891
892 surface =
893 display_create_shm_surface_from_pool(display, rectangle,
894 flags, pool);
895
896 if (!surface) {
897 shm_pool_destroy(pool);
898 return NULL;
899 }
900
901 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200902 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700903 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400904
Pekka Paalanen99436862012-11-19 17:15:59 +0200905out:
906 if (data_ret)
907 *data_ret = data;
908
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909 return surface;
910}
911
nobled7b87cb02011-02-01 18:51:47 +0000912static int
913check_size(struct rectangle *rect)
914{
915 if (rect->width && rect->height)
916 return 0;
917
918 fprintf(stderr, "tried to create surface of "
919 "width: %d, height: %d\n", rect->width, rect->height);
920 return -1;
921}
922
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400923cairo_surface_t *
924display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100925 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400926 struct rectangle *rectangle,
927 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928{
nobled7b87cb02011-02-01 18:51:47 +0000929 if (check_size(rectangle) < 0)
930 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200931
932 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200933 return display_create_shm_surface(display, rectangle, flags,
934 NULL, NULL);
935}
936
Pekka Paalanena4eda732012-11-19 17:16:02 +0200937struct shm_surface_leaf {
938 cairo_surface_t *cairo_surface;
939 /* 'data' is automatically destroyed, when 'cairo_surface' is */
940 struct shm_surface_data *data;
941
942 struct shm_pool *resize_pool;
943 int busy;
944};
945
946static void
947shm_surface_leaf_release(struct shm_surface_leaf *leaf)
948{
949 if (leaf->cairo_surface)
950 cairo_surface_destroy(leaf->cairo_surface);
951 /* leaf->data already destroyed via cairo private */
952
953 if (leaf->resize_pool)
954 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200955
956 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200957}
958
Pekka Paalanenaef02542013-04-25 13:57:47 +0300959#define MAX_LEAVES 3
960
Pekka Paalanen99436862012-11-19 17:15:59 +0200961struct shm_surface {
962 struct toysurface base;
963 struct display *display;
964 struct wl_surface *surface;
965 uint32_t flags;
966 int dx, dy;
967
Pekka Paalanenaef02542013-04-25 13:57:47 +0300968 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200969 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200970};
971
972static struct shm_surface *
973to_shm_surface(struct toysurface *base)
974{
975 return container_of(base, struct shm_surface, base);
976}
977
Pekka Paalanena4eda732012-11-19 17:16:02 +0200978static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300979shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
980{
981#ifdef DEBUG
982 struct shm_surface_leaf *leaf;
983 char bufs[MAX_LEAVES + 1];
984 int i;
985
986 for (i = 0; i < MAX_LEAVES; i++) {
987 leaf = &surface->leaf[i];
988
989 if (leaf->busy)
990 bufs[i] = 'b';
991 else if (leaf->cairo_surface)
992 bufs[i] = 'a';
993 else
994 bufs[i] = ' ';
995 }
996
997 bufs[MAX_LEAVES] = '\0';
998 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
999#endif
1000}
1001
1002static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001003shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1004{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001005 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001006 struct shm_surface_leaf *leaf;
1007 int i;
1008 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001009
1010 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001011
Pekka Paalanenaef02542013-04-25 13:57:47 +03001012 for (i = 0; i < MAX_LEAVES; i++) {
1013 leaf = &surface->leaf[i];
1014 if (leaf->data && leaf->data->buffer == buffer) {
1015 leaf->busy = 0;
1016 break;
1017 }
1018 }
1019 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001020
Pekka Paalanenaef02542013-04-25 13:57:47 +03001021 /* Leave one free leaf with storage, release others */
1022 free_found = 0;
1023 for (i = 0; i < MAX_LEAVES; i++) {
1024 leaf = &surface->leaf[i];
1025
1026 if (!leaf->cairo_surface || leaf->busy)
1027 continue;
1028
1029 if (!free_found)
1030 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001031 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001032 shm_surface_leaf_release(leaf);
1033 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001034
Pekka Paalanen97777442013-05-22 10:20:05 +03001035 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001036}
1037
1038static const struct wl_buffer_listener shm_surface_buffer_listener = {
1039 shm_surface_buffer_release
1040};
1041
Pekka Paalanen99436862012-11-19 17:15:59 +02001042static cairo_surface_t *
1043shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001044 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001045 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001046{
Pekka Paalanenec076692012-11-30 13:37:27 +02001047 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001048 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001049 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001050 struct shm_surface_leaf *leaf = NULL;
1051 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001052
1053 surface->dx = dx;
1054 surface->dy = dy;
1055
Pekka Paalanenaef02542013-04-25 13:57:47 +03001056 /* pick a free buffer, preferrably one that already has storage */
1057 for (i = 0; i < MAX_LEAVES; i++) {
1058 if (surface->leaf[i].busy)
1059 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001060
Pekka Paalanenaef02542013-04-25 13:57:47 +03001061 if (!leaf || surface->leaf[i].cairo_surface)
1062 leaf = &surface->leaf[i];
1063 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001064 DBG_OBJ(surface->surface, "pick leaf %d\n",
1065 (int)(leaf - &surface->leaf[0]));
1066
Pekka Paalanenaef02542013-04-25 13:57:47 +03001067 if (!leaf) {
1068 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001069 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001070 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001071 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001072 }
1073
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 if (!resize_hint && leaf->resize_pool) {
1075 cairo_surface_destroy(leaf->cairo_surface);
1076 leaf->cairo_surface = NULL;
1077 shm_pool_destroy(leaf->resize_pool);
1078 leaf->resize_pool = NULL;
1079 }
1080
Alexander Larsson1818e312013-05-22 14:41:31 +02001081 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1082
Pekka Paalanena4eda732012-11-19 17:16:02 +02001083 if (leaf->cairo_surface &&
1084 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1085 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001086 goto out;
1087
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 if (leaf->cairo_surface)
1089 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001090
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001091#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001092 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001093 /* Create a big pool to allocate from, while continuously
1094 * resizing. Mmapping a new pool in the server
1095 * is relatively expensive, so reusing a pool performs
1096 * better, but may temporarily reserve unneeded memory.
1097 */
1098 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001099 leaf->resize_pool = shm_pool_create(surface->display,
1100 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001101 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001102#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001103
Alexander Larsson1818e312013-05-22 14:41:31 +02001104 rect.width = width;
1105 rect.height = height;
1106
Pekka Paalanena4eda732012-11-19 17:16:02 +02001107 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001108 display_create_shm_surface(surface->display, &rect,
1109 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001110 leaf->resize_pool,
1111 &leaf->data);
1112 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001113 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001114
1115out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 surface->current = leaf;
1117
1118 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001119}
1120
1121static void
1122shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001123 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001124 struct rectangle *server_allocation)
1125{
1126 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001128
1129 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001130 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001131 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001132 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001133
Alexander Larsson1818e312013-05-22 14:41:31 +02001134 buffer_to_surface_size (buffer_transform, buffer_scale,
1135 &server_allocation->width,
1136 &server_allocation->height);
1137
Pekka Paalanena4eda732012-11-19 17:16:02 +02001138 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001139 surface->dx, surface->dy);
1140 wl_surface_damage(surface->surface, 0, 0,
1141 server_allocation->width, server_allocation->height);
1142 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001143
Pekka Paalanen71233882013-04-25 13:57:53 +03001144 DBG_OBJ(surface->surface, "leaf %d busy\n",
1145 (int)(leaf - &surface->leaf[0]));
1146
Pekka Paalanena4eda732012-11-19 17:16:02 +02001147 leaf->busy = 1;
1148 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001149}
1150
1151static int
1152shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1153{
1154 return -1;
1155}
1156
1157static void
1158shm_surface_release(struct toysurface *base)
1159{
1160}
1161
1162static void
1163shm_surface_destroy(struct toysurface *base)
1164{
1165 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001166 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001167
Pekka Paalanenaef02542013-04-25 13:57:47 +03001168 for (i = 0; i < MAX_LEAVES; i++)
1169 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001170
1171 free(surface);
1172}
1173
1174static struct toysurface *
1175shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1176 uint32_t flags, struct rectangle *rectangle)
1177{
1178 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001179 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001180
Brian Lovinbc919262013-08-07 15:34:59 -07001181 surface = xmalloc(sizeof *surface);
1182 memset(surface, 0, sizeof *surface);
1183
Pekka Paalanen99436862012-11-19 17:15:59 +02001184 if (!surface)
1185 return NULL;
1186
1187 surface->base.prepare = shm_surface_prepare;
1188 surface->base.swap = shm_surface_swap;
1189 surface->base.acquire = shm_surface_acquire;
1190 surface->base.release = shm_surface_release;
1191 surface->base.destroy = shm_surface_destroy;
1192
1193 surface->display = display;
1194 surface->surface = wl_surface;
1195 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001196
1197 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001198}
1199
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001200/*
1201 * The following correspondences between file names and cursors was copied
1202 * from: https://bugs.kde.org/attachment.cgi?id=67313
1203 */
1204
1205static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001206 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001207 "sw-resize",
1208 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001209};
1210
1211static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001212 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001213 "se-resize",
1214 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215};
1216
1217static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001218 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001219 "s-resize",
1220 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001221};
1222
1223static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001224 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001225 "closedhand",
1226 "208530c400c041818281048008011002"
1227};
1228
1229static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001230 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001231 "default",
1232 "top_left_arrow",
1233 "left-arrow"
1234};
1235
1236static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001237 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001238 "w-resize",
1239 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001240};
1241
1242static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001243 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001244 "e-resize",
1245 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001246};
1247
1248static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001249 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001250 "nw-resize",
1251 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252};
1253
1254static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001255 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001256 "ne-resize",
1257 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258};
1259
1260static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001261 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001262 "n-resize",
1263 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001264};
1265
1266static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001267 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001268 "ibeam",
1269 "text"
1270};
1271
1272static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001273 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001274 "pointer",
1275 "pointing_hand",
1276 "e29285e634086352946a0e7090d73106"
1277};
1278
1279static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001280 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001281 "wait",
1282 "0426c94ea35c87780ff01dc239897213"
1283};
1284
1285struct cursor_alternatives {
1286 const char **names;
1287 size_t count;
1288};
1289
1290static const struct cursor_alternatives cursors[] = {
1291 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1292 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1293 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1294 {grabbings, ARRAY_LENGTH(grabbings)},
1295 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1296 {left_sides, ARRAY_LENGTH(left_sides)},
1297 {right_sides, ARRAY_LENGTH(right_sides)},
1298 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1299 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1300 {top_sides, ARRAY_LENGTH(top_sides)},
1301 {xterms, ARRAY_LENGTH(xterms)},
1302 {hand1s, ARRAY_LENGTH(hand1s)},
1303 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001304};
1305
1306static void
1307create_cursors(struct display *display)
1308{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001309 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001310 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001311 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001312 unsigned int i, j;
1313 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001314 struct config_key shell_keys[] = {
1315 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001316 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001317 };
1318 struct config_section cs[] = {
1319 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1320 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001321
Ossama Othmana50e6e42013-05-14 09:48:26 -07001322 config_fd = open_config_file("weston.ini");
1323 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1324 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001325
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001326 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001327 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001328 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001329
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001330 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001331 cursor = NULL;
1332 for (j = 0; !cursor && j < cursors[i].count; ++j)
1333 cursor = wl_cursor_theme_get_cursor(
1334 display->cursor_theme, cursors[i].names[j]);
1335
1336 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001337 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001338 cursors[i].names[0]);
1339
1340 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001341 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001342}
1343
1344static void
1345destroy_cursors(struct display *display)
1346{
1347 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001348 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001349}
1350
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001351struct wl_cursor_image *
1352display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001353{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001354 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001355
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001356 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001357}
1358
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001359static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001360surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001361{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001362 if (!surface->cairo_surface)
1363 return;
1364
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001365 if (surface->opaque_region) {
1366 wl_surface_set_opaque_region(surface->surface,
1367 surface->opaque_region);
1368 wl_region_destroy(surface->opaque_region);
1369 surface->opaque_region = NULL;
1370 }
1371
1372 if (surface->input_region) {
1373 wl_surface_set_input_region(surface->surface,
1374 surface->input_region);
1375 wl_region_destroy(surface->input_region);
1376 surface->input_region = NULL;
1377 }
1378
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001379 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001380 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001381 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001382
Pekka Paalanen89dee002013-02-13 16:17:20 +02001383 cairo_surface_destroy(surface->cairo_surface);
1384 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001385}
1386
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001387int
1388window_has_focus(struct window *window)
1389{
1390 return window->focus_count > 0;
1391}
1392
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001393static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001394window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001395{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001396 struct surface *surface;
1397
Pekka Paalanen89dee002013-02-13 16:17:20 +02001398 if (window->type == TYPE_NONE) {
1399 window->type = TYPE_TOPLEVEL;
1400 if (window->shell_surface)
1401 wl_shell_surface_set_toplevel(window->shell_surface);
1402 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001403
Pekka Paalanen35e82632013-04-25 13:57:48 +03001404 wl_list_for_each(surface, &window->subsurface_list, link) {
1405 if (surface == window->main_surface)
1406 continue;
1407
1408 surface_flush(surface);
1409 }
1410
Pekka Paalanen89dee002013-02-13 16:17:20 +02001411 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001412}
1413
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001414struct display *
1415window_get_display(struct window *window)
1416{
1417 return window->display;
1418}
1419
Pekka Paalanen89dee002013-02-13 16:17:20 +02001420static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001421surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001422{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001423 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001424 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001425
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001426 if (!surface->toysurface && display->dpy &&
1427 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001428 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001429 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001430 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001431 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001432 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001433 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001434
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001435 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001436 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001437 surface->surface,
1438 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001439
Pekka Paalanen89dee002013-02-13 16:17:20 +02001440 surface->cairo_surface = surface->toysurface->prepare(
1441 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001442 allocation.width, allocation.height, flags,
1443 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001444}
1445
1446static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001447window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001448{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001449 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001450 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001451 int dx = 0;
1452 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001453
Pekka Paalanenec076692012-11-30 13:37:27 +02001454 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001455 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001456
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001457 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1458 flags |= SURFACE_HINT_RGB565;
1459
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001460 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1461 dx = surface->server_allocation.width -
1462 surface->allocation.width;
1463
1464 if (window->resize_edges & WINDOW_RESIZING_TOP)
1465 dy = surface->server_allocation.height -
1466 surface->allocation.height;
1467
1468 window->resize_edges = 0;
1469
Pekka Paalanen89dee002013-02-13 16:17:20 +02001470 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001471}
1472
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001473int
1474window_get_buffer_transform(struct window *window)
1475{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001476 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001477}
1478
1479void
1480window_set_buffer_transform(struct window *window,
1481 enum wl_output_transform transform)
1482{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001483 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001484 wl_surface_set_buffer_transform(window->main_surface->surface,
1485 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001486}
1487
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001488void
1489window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001490 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001491{
1492 window->main_surface->buffer_scale = scale;
1493 wl_surface_set_buffer_scale(window->main_surface->surface,
1494 scale);
1495}
1496
1497uint32_t
1498window_get_buffer_scale(struct window *window)
1499{
1500 return window->main_surface->buffer_scale;
1501}
1502
Alexander Larssond68f5232013-05-22 14:41:33 +02001503uint32_t
1504window_get_output_scale(struct window *window)
1505{
1506 struct window_output *window_output;
1507 struct window_output *window_output_tmp;
1508 int scale = 1;
1509
1510 wl_list_for_each_safe(window_output, window_output_tmp,
1511 &window->window_output_list, link) {
1512 if (window_output->output->scale > scale)
1513 scale = window_output->output->scale;
1514 }
1515
1516 return scale;
1517}
1518
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001519static void frame_destroy(struct frame *frame);
1520
Pekka Paalanen4e373742013-02-13 16:17:13 +02001521static void
1522surface_destroy(struct surface *surface)
1523{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001524 if (surface->frame_cb)
1525 wl_callback_destroy(surface->frame_cb);
1526
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001527 if (surface->input_region)
1528 wl_region_destroy(surface->input_region);
1529
1530 if (surface->opaque_region)
1531 wl_region_destroy(surface->opaque_region);
1532
Pekka Paalanen35e82632013-04-25 13:57:48 +03001533 if (surface->subsurface)
1534 wl_subsurface_destroy(surface->subsurface);
1535
Pekka Paalanen4e373742013-02-13 16:17:13 +02001536 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001537
1538 if (surface->toysurface)
1539 surface->toysurface->destroy(surface->toysurface);
1540
Pekka Paalanen35e82632013-04-25 13:57:48 +03001541 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001542 free(surface);
1543}
1544
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001545void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001546window_destroy(struct window *window)
1547{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001548 struct display *display = window->display;
1549 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001550 struct window_output *window_output;
1551 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001552
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001553 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001554
1555 wl_list_for_each(input, &display->input_list, link) {
1556 if (input->pointer_focus == window)
1557 input->pointer_focus = NULL;
1558 if (input->keyboard_focus == window)
1559 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001560 if (input->focus_widget &&
1561 input->focus_widget->window == window)
1562 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001563 }
1564
Rob Bradford7507b572012-05-15 17:55:34 +01001565 wl_list_for_each_safe(window_output, window_output_tmp,
1566 &window->window_output_list, link) {
1567 free (window_output);
1568 }
1569
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001570 if (window->frame)
1571 frame_destroy(window->frame);
1572
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001573 if (window->shell_surface)
1574 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001575
1576 surface_destroy(window->main_surface);
1577
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001578 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001579
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001580 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001581 free(window);
1582}
1583
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001584static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001585widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001586{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001588
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001589 wl_list_for_each(child, &widget->child_list, link) {
1590 target = widget_find_widget(child, x, y);
1591 if (target)
1592 return target;
1593 }
1594
1595 if (widget->allocation.x <= x &&
1596 x < widget->allocation.x + widget->allocation.width &&
1597 widget->allocation.y <= y &&
1598 y < widget->allocation.y + widget->allocation.height) {
1599 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001600 }
1601
1602 return NULL;
1603}
1604
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001605static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001606window_find_widget(struct window *window, int32_t x, int32_t y)
1607{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001608 struct surface *surface;
1609 struct widget *widget;
1610
1611 wl_list_for_each(surface, &window->subsurface_list, link) {
1612 widget = widget_find_widget(surface->widget, x, y);
1613 if (widget)
1614 return widget;
1615 }
1616
1617 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001618}
1619
1620static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001621widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001622{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001623 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001624
Peter Huttererf3d62272013-08-08 11:57:05 +10001625 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001626 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001627 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001628 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001629 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001630 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001631 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001632 widget->tooltip = NULL;
1633 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001634 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001635
1636 return widget;
1637}
1638
1639struct widget *
1640window_add_widget(struct window *window, void *data)
1641{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001642 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001643
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001644 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001645 wl_list_init(&widget->link);
1646 window->main_surface->widget = widget;
1647
1648 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001649}
1650
1651struct widget *
1652widget_add_widget(struct widget *parent, void *data)
1653{
1654 struct widget *widget;
1655
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001656 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001657 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001658
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001659 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001660}
1661
1662void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001663widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001665 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001666 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001667 struct input *input;
1668
Pekka Paalanen35e82632013-04-25 13:57:48 +03001669 /* Destroy the sub-surface along with the root widget */
1670 if (surface->widget == widget && surface->subsurface)
1671 surface_destroy(widget->surface);
1672
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001673 if (widget->tooltip) {
1674 free(widget->tooltip);
1675 widget->tooltip = NULL;
1676 }
1677
Pekka Paalanene156fb62012-01-19 13:51:38 +02001678 wl_list_for_each(input, &display->input_list, link) {
1679 if (input->focus_widget == widget)
1680 input->focus_widget = NULL;
1681 }
1682
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001683 wl_list_remove(&widget->link);
1684 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001685}
1686
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001687void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001688widget_set_default_cursor(struct widget *widget, int cursor)
1689{
1690 widget->default_cursor = cursor;
1691}
1692
1693void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001694widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001695{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001696 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001697}
1698
1699void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001700widget_set_size(struct widget *widget, int32_t width, int32_t height)
1701{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001702 widget->allocation.width = width;
1703 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001704}
1705
1706void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001707widget_set_allocation(struct widget *widget,
1708 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001709{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001710 widget->allocation.x = x;
1711 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001712 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001713}
1714
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001715void
1716widget_set_transparent(struct widget *widget, int transparent)
1717{
1718 widget->opaque = !transparent;
1719}
1720
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001721void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001722widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001723{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001724 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001725}
1726
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001727static cairo_surface_t *
1728widget_get_cairo_surface(struct widget *widget)
1729{
1730 struct surface *surface = widget->surface;
1731 struct window *window = widget->window;
1732
1733 if (!surface->cairo_surface) {
1734 if (surface == window->main_surface)
1735 window_create_main_surface(window);
1736 else
1737 surface_create_surface(surface, 0, 0, 0);
1738 }
1739
1740 return surface->cairo_surface;
1741}
1742
Alexander Larsson15901f02013-05-22 14:41:25 +02001743static void
1744widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1745{
1746 struct surface *surface = widget->surface;
1747 double angle;
1748 cairo_matrix_t m;
1749 enum wl_output_transform transform;
1750 int surface_width, surface_height;
1751 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001752 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001753
1754 surface_width = surface->allocation.width;
1755 surface_height = surface->allocation.height;
1756
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001757 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001758 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001759
Alexander Larsson15901f02013-05-22 14:41:25 +02001760 switch (transform) {
1761 case WL_OUTPUT_TRANSFORM_FLIPPED:
1762 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1763 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1764 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1765 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1766 break;
1767 default:
1768 cairo_matrix_init_identity(&m);
1769 break;
1770 }
1771
1772 switch (transform) {
1773 case WL_OUTPUT_TRANSFORM_NORMAL:
1774 default:
1775 angle = 0;
1776 translate_x = 0;
1777 translate_y = 0;
1778 break;
1779 case WL_OUTPUT_TRANSFORM_FLIPPED:
1780 angle = 0;
1781 translate_x = surface_width;
1782 translate_y = 0;
1783 break;
1784 case WL_OUTPUT_TRANSFORM_90:
1785 angle = M_PI_2;
1786 translate_x = surface_height;
1787 translate_y = 0;
1788 break;
1789 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1790 angle = M_PI_2;
1791 translate_x = surface_height;
1792 translate_y = surface_width;
1793 break;
1794 case WL_OUTPUT_TRANSFORM_180:
1795 angle = M_PI;
1796 translate_x = surface_width;
1797 translate_y = surface_height;
1798 break;
1799 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1800 angle = M_PI;
1801 translate_x = 0;
1802 translate_y = surface_height;
1803 break;
1804 case WL_OUTPUT_TRANSFORM_270:
1805 angle = M_PI + M_PI_2;
1806 translate_x = 0;
1807 translate_y = surface_width;
1808 break;
1809 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1810 angle = M_PI + M_PI_2;
1811 translate_x = 0;
1812 translate_y = 0;
1813 break;
1814 }
1815
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001816 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001817 cairo_translate(cr, translate_x, translate_y);
1818 cairo_rotate(cr, angle);
1819 cairo_transform(cr, &m);
1820}
1821
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001822cairo_t *
1823widget_cairo_create(struct widget *widget)
1824{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001825 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001826 cairo_surface_t *cairo_surface;
1827 cairo_t *cr;
1828
1829 cairo_surface = widget_get_cairo_surface(widget);
1830 cr = cairo_create(cairo_surface);
1831
Alexander Larsson15901f02013-05-22 14:41:25 +02001832 widget_cairo_update_transform(widget, cr);
1833
Pekka Paalanen35e82632013-04-25 13:57:48 +03001834 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1835
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001836 return cr;
1837}
1838
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001839struct wl_surface *
1840widget_get_wl_surface(struct widget *widget)
1841{
1842 return widget->surface->surface;
1843}
1844
1845uint32_t
1846widget_get_last_time(struct widget *widget)
1847{
1848 return widget->surface->last_time;
1849}
1850
1851void
1852widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1853{
1854 struct wl_compositor *comp = widget->window->display->compositor;
1855 struct surface *surface = widget->surface;
1856
1857 if (!surface->input_region)
1858 surface->input_region = wl_compositor_create_region(comp);
1859
1860 if (rect) {
1861 wl_region_add(surface->input_region,
1862 rect->x, rect->y, rect->width, rect->height);
1863 }
1864}
1865
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001866void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001867widget_set_resize_handler(struct widget *widget,
1868 widget_resize_handler_t handler)
1869{
1870 widget->resize_handler = handler;
1871}
1872
1873void
1874widget_set_redraw_handler(struct widget *widget,
1875 widget_redraw_handler_t handler)
1876{
1877 widget->redraw_handler = handler;
1878}
1879
1880void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001881widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001882{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001883 widget->enter_handler = handler;
1884}
1885
1886void
1887widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1888{
1889 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001890}
1891
1892void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001893widget_set_motion_handler(struct widget *widget,
1894 widget_motion_handler_t handler)
1895{
1896 widget->motion_handler = handler;
1897}
1898
1899void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001900widget_set_button_handler(struct widget *widget,
1901 widget_button_handler_t handler)
1902{
1903 widget->button_handler = handler;
1904}
1905
1906void
Rusty Lynch041815a2013-08-08 21:20:38 -07001907widget_set_touch_up_handler(struct widget *widget,
1908 widget_touch_up_handler_t handler)
1909{
1910 widget->touch_up_handler = handler;
1911}
1912
1913void
1914widget_set_touch_down_handler(struct widget *widget,
1915 widget_touch_down_handler_t handler)
1916{
1917 widget->touch_down_handler = handler;
1918}
1919
1920void
1921widget_set_touch_motion_handler(struct widget *widget,
1922 widget_touch_motion_handler_t handler)
1923{
1924 widget->touch_motion_handler = handler;
1925}
1926
1927void
1928widget_set_touch_frame_handler(struct widget *widget,
1929 widget_touch_frame_handler_t handler)
1930{
1931 widget->touch_frame_handler = handler;
1932}
1933
1934void
1935widget_set_touch_cancel_handler(struct widget *widget,
1936 widget_touch_cancel_handler_t handler)
1937{
1938 widget->touch_cancel_handler = handler;
1939}
1940
1941void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001942widget_set_axis_handler(struct widget *widget,
1943 widget_axis_handler_t handler)
1944{
1945 widget->axis_handler = handler;
1946}
1947
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001948static void
1949window_schedule_redraw_task(struct window *window);
1950
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001951void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001952widget_schedule_redraw(struct widget *widget)
1953{
Pekka Paalanen71233882013-04-25 13:57:53 +03001954 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001955 widget->surface->redraw_needed = 1;
1956 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001957}
1958
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001959cairo_surface_t *
1960window_get_surface(struct window *window)
1961{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001962 cairo_surface_t *cairo_surface;
1963
1964 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1965
1966 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001967}
1968
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001969struct wl_surface *
1970window_get_wl_surface(struct window *window)
1971{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001972 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001973}
1974
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001975struct wl_shell_surface *
1976window_get_wl_shell_surface(struct window *window)
1977{
1978 return window->shell_surface;
1979}
1980
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001981static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001982tooltip_redraw_handler(struct widget *widget, void *data)
1983{
1984 cairo_t *cr;
1985 const int32_t r = 3;
1986 struct tooltip *tooltip = data;
1987 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001988
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001989 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001990 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1991 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1992 cairo_paint(cr);
1993
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001994 width = widget->allocation.width;
1995 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001996 rounded_rect(cr, 0, 0, width, height, r);
1997
1998 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1999 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2000 cairo_fill(cr);
2001
2002 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2003 cairo_move_to(cr, 10, 16);
2004 cairo_show_text(cr, tooltip->entry);
2005 cairo_destroy(cr);
2006}
2007
2008static cairo_text_extents_t
2009get_text_extents(struct tooltip *tooltip)
2010{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002011 cairo_t *cr;
2012 cairo_text_extents_t extents;
2013
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002014 /* Use the dummy_surface because tooltip's surface was not
2015 * created yet, and parent does not have a valid surface
2016 * outside repaint, either.
2017 */
2018 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002019 cairo_text_extents(cr, tooltip->entry, &extents);
2020 cairo_destroy(cr);
2021
2022 return extents;
2023}
2024
2025static int
2026window_create_tooltip(struct tooltip *tooltip)
2027{
2028 struct widget *parent = tooltip->parent;
2029 struct display *display = parent->window->display;
2030 struct window *window;
2031 const int offset_y = 27;
2032 const int margin = 3;
2033 cairo_text_extents_t extents;
2034
2035 if (tooltip->widget)
2036 return 0;
2037
2038 window = window_create_transient(display, parent->window, tooltip->x,
2039 tooltip->y + offset_y,
2040 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2041 if (!window)
2042 return -1;
2043
2044 tooltip->window = window;
2045 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2046
2047 extents = get_text_extents(tooltip);
2048 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2049 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2050
2051 return 0;
2052}
2053
2054void
2055widget_destroy_tooltip(struct widget *parent)
2056{
2057 struct tooltip *tooltip = parent->tooltip;
2058
2059 parent->tooltip_count = 0;
2060 if (!tooltip)
2061 return;
2062
2063 if (tooltip->widget) {
2064 widget_destroy(tooltip->widget);
2065 window_destroy(tooltip->window);
2066 tooltip->widget = NULL;
2067 tooltip->window = NULL;
2068 }
2069
2070 close(tooltip->tooltip_fd);
2071 free(tooltip->entry);
2072 free(tooltip);
2073 parent->tooltip = NULL;
2074}
2075
2076static void
2077tooltip_func(struct task *task, uint32_t events)
2078{
2079 struct tooltip *tooltip =
2080 container_of(task, struct tooltip, tooltip_task);
2081 uint64_t exp;
2082
Martin Olsson8df662a2012-07-08 03:03:47 +02002083 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2084 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002085 window_create_tooltip(tooltip);
2086}
2087
2088#define TOOLTIP_TIMEOUT 500
2089static int
2090tooltip_timer_reset(struct tooltip *tooltip)
2091{
2092 struct itimerspec its;
2093
2094 its.it_interval.tv_sec = 0;
2095 its.it_interval.tv_nsec = 0;
2096 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2097 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2098 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2099 fprintf(stderr, "could not set timerfd\n: %m");
2100 return -1;
2101 }
2102
2103 return 0;
2104}
2105
2106int
2107widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2108{
2109 struct tooltip *tooltip = parent->tooltip;
2110
2111 parent->tooltip_count++;
2112 if (tooltip) {
2113 tooltip->x = x;
2114 tooltip->y = y;
2115 tooltip_timer_reset(tooltip);
2116 return 0;
2117 }
2118
2119 /* the handler might be triggered too fast via input device motion, so
2120 * we need this check here to make sure tooltip is fully initialized */
2121 if (parent->tooltip_count > 1)
2122 return 0;
2123
2124 tooltip = malloc(sizeof *tooltip);
2125 if (!tooltip)
2126 return -1;
2127
2128 parent->tooltip = tooltip;
2129 tooltip->parent = parent;
2130 tooltip->widget = NULL;
2131 tooltip->window = NULL;
2132 tooltip->x = x;
2133 tooltip->y = y;
2134 tooltip->entry = strdup(entry);
2135 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2136 if (tooltip->tooltip_fd < 0) {
2137 fprintf(stderr, "could not create timerfd\n: %m");
2138 return -1;
2139 }
2140
2141 tooltip->tooltip_task.run = tooltip_func;
2142 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2143 EPOLLIN, &tooltip->tooltip_task);
2144 tooltip_timer_reset(tooltip);
2145
2146 return 0;
2147}
2148
2149static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002150workspace_manager_state(void *data,
2151 struct workspace_manager *workspace_manager,
2152 uint32_t current,
2153 uint32_t count)
2154{
2155 struct display *display = data;
2156
2157 display->workspace = current;
2158 display->workspace_count = count;
2159}
2160
2161static const struct workspace_manager_listener workspace_manager_listener = {
2162 workspace_manager_state
2163};
2164
2165static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002166frame_resize_handler(struct widget *widget,
2167 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002168{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002169 struct frame *frame = data;
2170 struct widget *child = frame->child;
2171 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002172 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002173 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002174 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002175 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002176 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002178 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002179
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002180 switch (widget->window->type) {
2181 case TYPE_FULLSCREEN:
2182 decoration_width = 0;
2183 decoration_height = 0;
2184
2185 allocation.x = 0;
2186 allocation.y = 0;
2187 allocation.width = width;
2188 allocation.height = height;
2189 opaque_margin = 0;
2190
2191 wl_list_for_each(button, &frame->buttons_list, link)
2192 button->widget->opaque = 1;
2193 break;
2194 case TYPE_MAXIMIZED:
2195 decoration_width = t->width * 2;
2196 decoration_height = t->width + t->titlebar_height;
2197
2198 allocation.x = t->width;
2199 allocation.y = t->titlebar_height;
2200 allocation.width = width - decoration_width;
2201 allocation.height = height - decoration_height;
2202
2203 opaque_margin = 0;
2204
2205 wl_list_for_each(button, &frame->buttons_list, link)
2206 button->widget->opaque = 0;
2207 break;
2208 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002209 decoration_width = (t->width + t->margin) * 2;
2210 decoration_height = t->width +
2211 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002213 allocation.x = t->width + t->margin;
2214 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002215 allocation.width = width - decoration_width;
2216 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002217
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002218 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002219
2220 wl_list_for_each(button, &frame->buttons_list, link)
2221 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002222 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002223 }
2224
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002225 widget_set_allocation(child, allocation.x, allocation.y,
2226 allocation.width, allocation.height);
2227
2228 if (child->resize_handler)
2229 child->resize_handler(child,
2230 allocation.width,
2231 allocation.height,
2232 child->user_data);
2233
Scott Moreauf7e498c2012-05-14 11:39:29 -06002234 width = child->allocation.width + decoration_width;
2235 height = child->allocation.height + decoration_height;
2236
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002237 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2238
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002239 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002240 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002241 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002242 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002243 shadow_margin, shadow_margin,
2244 width - 2 * shadow_margin,
2245 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002246 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002247 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002248 }
2249
Scott Moreauf7e498c2012-05-14 11:39:29 -06002250 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002251
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002252 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002253 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002254 opaque_margin, opaque_margin,
2255 widget->allocation.width - 2 * opaque_margin,
2256 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002257
2258 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002259 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2260 x_l = t->width + shadow_margin;
2261 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002262 wl_list_for_each(button, &frame->buttons_list, link) {
2263 const int button_padding = 4;
2264 w = cairo_image_surface_get_width(button->icon);
2265 h = cairo_image_surface_get_height(button->icon);
2266
2267 if (button->decoration == FRAME_BUTTON_FANCY)
2268 w += 10;
2269
2270 if (button->align == FRAME_BUTTON_LEFT) {
2271 widget_set_allocation(button->widget,
2272 x_l, y , w + 1, h + 1);
2273 x_l += w;
2274 x_l += button_padding;
2275 } else {
2276 x_r -= w;
2277 widget_set_allocation(button->widget,
2278 x_r, y , w + 1, h + 1);
2279 x_r -= button_padding;
2280 }
2281 }
2282}
2283
2284static int
2285frame_button_enter_handler(struct widget *widget,
2286 struct input *input, float x, float y, void *data)
2287{
2288 struct frame_button *frame_button = data;
2289
2290 widget_schedule_redraw(frame_button->widget);
2291 frame_button->state = FRAME_BUTTON_OVER;
2292
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002293 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002294}
2295
2296static void
2297frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2298{
2299 struct frame_button *frame_button = data;
2300
2301 widget_schedule_redraw(frame_button->widget);
2302 frame_button->state = FRAME_BUTTON_DEFAULT;
2303}
2304
2305static void
2306frame_button_button_handler(struct widget *widget,
2307 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002308 uint32_t button,
2309 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002310{
2311 struct frame_button *frame_button = data;
2312 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002313 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002314
2315 if (button != BTN_LEFT)
2316 return;
2317
2318 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002319 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002320 frame_button->state = FRAME_BUTTON_ACTIVE;
2321 widget_schedule_redraw(frame_button->widget);
2322
2323 if (frame_button->type == FRAME_BUTTON_ICON)
2324 window_show_frame_menu(window, input, time);
2325 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002326 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002327 frame_button->state = FRAME_BUTTON_DEFAULT;
2328 widget_schedule_redraw(frame_button->widget);
2329 break;
2330 }
2331
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002332 if (!was_pressed)
2333 return;
2334
Martin Minarik1998b152012-05-10 02:04:35 +02002335 switch (frame_button->type) {
2336 case FRAME_BUTTON_CLOSE:
2337 if (window->close_handler)
2338 window->close_handler(window->parent,
2339 window->user_data);
2340 else
2341 display_exit(window->display);
2342 break;
2343 case FRAME_BUTTON_MINIMIZE:
2344 fprintf(stderr,"Minimize stub\n");
2345 break;
2346 case FRAME_BUTTON_MAXIMIZE:
2347 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2348 break;
2349 default:
2350 /* Unknown operation */
2351 break;
2352 }
2353}
2354
Rusty Lynch041815a2013-08-08 21:20:38 -07002355static void
2356frame_button_touch_down_handler(struct widget *widget, uint32_t serial,
2357 uint32_t time, int32_t id,
2358 float x, float y, void *data)
2359{
2360 struct frame_button *frame_button = data;
2361 struct window *window = widget->window;
2362
2363 switch (frame_button->type) {
2364 case FRAME_BUTTON_CLOSE:
2365 if (window->close_handler)
2366 window->close_handler(window->parent,
2367 window->user_data);
2368 else
2369 display_exit(window->display);
2370 break;
2371 case FRAME_BUTTON_MINIMIZE:
2372 fprintf(stderr,"Minimize stub\n");
2373 break;
2374 case FRAME_BUTTON_MAXIMIZE:
2375 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2376 break;
2377 default:
2378 /* Unknown operation */
2379 break;
2380 }
2381}
2382
2383
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002384static int
2385frame_button_motion_handler(struct widget *widget,
2386 struct input *input, uint32_t time,
2387 float x, float y, void *data)
2388{
2389 struct frame_button *frame_button = data;
2390 enum frame_button_pointer previous_button_state = frame_button->state;
2391
2392 /* only track state for a pressed button */
2393 if (input->grab != widget)
2394 return CURSOR_LEFT_PTR;
2395
2396 if (x > widget->allocation.x &&
2397 x < (widget->allocation.x + widget->allocation.width) &&
2398 y > widget->allocation.y &&
2399 y < (widget->allocation.y + widget->allocation.height)) {
2400 frame_button->state = FRAME_BUTTON_ACTIVE;
2401 } else {
2402 frame_button->state = FRAME_BUTTON_DEFAULT;
2403 }
2404
2405 if (frame_button->state != previous_button_state)
2406 widget_schedule_redraw(frame_button->widget);
2407
2408 return CURSOR_LEFT_PTR;
2409}
2410
Martin Minarik1998b152012-05-10 02:04:35 +02002411static void
2412frame_button_redraw_handler(struct widget *widget, void *data)
2413{
2414 struct frame_button *frame_button = data;
2415 cairo_t *cr;
2416 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002417
2418 x = widget->allocation.x;
2419 y = widget->allocation.y;
2420 width = widget->allocation.width;
2421 height = widget->allocation.height;
2422
2423 if (!width)
2424 return;
2425 if (!height)
2426 return;
2427 if (widget->opaque)
2428 return;
2429
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002430 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002431
2432 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2433 cairo_set_line_width(cr, 1);
2434
2435 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2436 cairo_rectangle (cr, x, y, 25, 16);
2437
2438 cairo_stroke_preserve(cr);
2439
2440 switch (frame_button->state) {
2441 case FRAME_BUTTON_DEFAULT:
2442 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2443 break;
2444 case FRAME_BUTTON_OVER:
2445 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2446 break;
2447 case FRAME_BUTTON_ACTIVE:
2448 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2449 break;
2450 }
2451
2452 cairo_fill (cr);
2453
2454 x += 4;
2455 }
2456
2457 cairo_set_source_surface(cr, frame_button->icon, x, y);
2458 cairo_paint(cr);
2459
2460 cairo_destroy(cr);
2461}
2462
2463static struct widget *
2464frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2465 enum frame_button_align align, enum frame_button_decoration style)
2466{
2467 struct frame_button *frame_button;
2468 const char *icon = data;
2469
Peter Huttererf3d62272013-08-08 11:57:05 +10002470 frame_button = xzalloc (sizeof *frame_button);
Martin Minarik1998b152012-05-10 02:04:35 +02002471 frame_button->icon = cairo_image_surface_create_from_png(icon);
2472 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2473 frame_button->frame = frame;
2474 frame_button->type = type;
2475 frame_button->align = align;
2476 frame_button->decoration = style;
2477
2478 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2479
2480 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2481 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2482 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
Rusty Lynch041815a2013-08-08 21:20:38 -07002483 widget_set_touch_down_handler(frame_button->widget, frame_button_touch_down_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002484 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002485 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002486 return frame_button->widget;
2487}
2488
2489static void
2490frame_button_destroy(struct frame_button *frame_button)
2491{
2492 widget_destroy(frame_button->widget);
2493 wl_list_remove(&frame_button->link);
2494 cairo_surface_destroy(frame_button->icon);
2495 free(frame_button);
2496
2497 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498}
2499
2500static void
2501frame_redraw_handler(struct widget *widget, void *data)
2502{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002503 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002504 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002505 struct theme *t = window->display->theme;
2506 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002507
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002508 if (window->type == TYPE_FULLSCREEN)
2509 return;
2510
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002511 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002512
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002513 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002514 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002515 if (window->type == TYPE_MAXIMIZED)
2516 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002517 theme_render_frame(t, cr, widget->allocation.width,
2518 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002519
2520 cairo_destroy(cr);
2521}
2522
2523static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002524frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002525{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002526 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002527 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002528 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002529
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002530 if (window->type != TYPE_TOPLEVEL)
2531 return CURSOR_LEFT_PTR;
2532
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002533 location = theme_get_location(t, input->sx, input->sy,
2534 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002535 frame->widget->allocation.height,
2536 window->type == TYPE_MAXIMIZED ?
2537 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002538
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002539 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002540 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002541 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002542 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002543 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002544 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002545 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002546 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002547 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002548 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002549 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002550 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002551 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002552 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002553 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002554 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002555 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002556 case THEME_LOCATION_EXTERIOR:
2557 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002558 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002559 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002560 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002561}
2562
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002563static void
2564frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002565{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002566 struct display *display;
2567
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002568 switch (index) {
2569 case 0: /* close */
2570 if (window->close_handler)
2571 window->close_handler(window->parent,
2572 window->user_data);
2573 else
2574 display_exit(window->display);
2575 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002576 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002577 display = window->display;
2578 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002579 workspace_manager_move_surface(
2580 display->workspace_manager,
2581 window->main_surface->surface,
2582 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002583 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002584 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002585 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002586 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002587 workspace_manager_move_surface(
2588 display->workspace_manager,
2589 window->main_surface->surface,
2590 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002591 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002592 case 3: /* fullscreen */
2593 /* we don't have a way to get out of fullscreen for now */
2594 if (window->fullscreen_handler)
2595 window->fullscreen_handler(window, window->user_data);
2596 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002597 }
2598}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002599
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002600void
2601window_show_frame_menu(struct window *window,
2602 struct input *input, uint32_t time)
2603{
2604 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002605 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002606
2607 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002608 "Close",
2609 "Move to workspace above", "Move to workspace below",
2610 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002611 };
2612
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002613 if (window->fullscreen_handler)
2614 count = ARRAY_LENGTH(entries);
2615 else
2616 count = ARRAY_LENGTH(entries) - 1;
2617
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002618 input_get_position(input, &x, &y);
2619 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002620 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002621}
2622
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002623static int
2624frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002625 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002626{
2627 return frame_get_pointer_image_for_location(data, input);
2628}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002629
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002630static int
2631frame_motion_handler(struct widget *widget,
2632 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002633 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002634{
2635 return frame_get_pointer_image_for_location(data, input);
2636}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002637
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002638static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002639frame_button_handler(struct widget *widget,
2640 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002641 uint32_t button, enum wl_pointer_button_state state,
2642 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002643
2644{
2645 struct frame *frame = data;
2646 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002647 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002648 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002649
Rob Bradford3c9d9672013-07-11 10:35:55 +01002650 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002651 return;
2652
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002653 location = theme_get_location(display->theme, input->sx, input->sy,
2654 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002655 frame->widget->allocation.height,
2656 window->type == TYPE_MAXIMIZED ?
2657 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002658
Daniel Stone4dbadb12012-05-30 16:31:51 +01002659 if (window->display->shell && button == BTN_LEFT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002660 window->type == TYPE_TOPLEVEL) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002661 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002662 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002663 if (!window->shell_surface)
2664 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002665 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002666 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002667 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002668 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002669 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002670 case THEME_LOCATION_RESIZING_TOP:
2671 case THEME_LOCATION_RESIZING_BOTTOM:
2672 case THEME_LOCATION_RESIZING_LEFT:
2673 case THEME_LOCATION_RESIZING_RIGHT:
2674 case THEME_LOCATION_RESIZING_TOP_LEFT:
2675 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2676 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2677 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002678 if (!window->shell_surface)
2679 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002680 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002681
Pekka Paalanen99436862012-11-19 17:15:59 +02002682 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002683 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002684 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002685 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002686 break;
2687 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002688 } else if (button == BTN_RIGHT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002689 (window->type == TYPE_TOPLEVEL ||
2690 window->type == TYPE_MAXIMIZED)) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002691 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002692 }
2693}
2694
2695struct widget *
2696frame_create(struct window *window, void *data)
2697{
2698 struct frame *frame;
2699
Peter Huttererf3d62272013-08-08 11:57:05 +10002700 frame = xzalloc(sizeof *frame);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002701 frame->widget = window_add_widget(window, frame);
2702 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002703
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002704 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2705 widget_set_resize_handler(frame->widget, frame_resize_handler);
2706 widget_set_enter_handler(frame->widget, frame_enter_handler);
2707 widget_set_motion_handler(frame->widget, frame_motion_handler);
2708 widget_set_button_handler(frame->widget, frame_button_handler);
2709
Martin Minarik1998b152012-05-10 02:04:35 +02002710 /* Create empty list for frame buttons */
2711 wl_list_init(&frame->buttons_list);
2712
2713 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2714 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2715
2716 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2717 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2718
2719 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2720 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2721
2722 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2723 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2724
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002725 window->frame = frame;
2726
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002727 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002728}
2729
Kristian Høgsberga1627922012-06-20 17:30:03 -04002730void
2731frame_set_child_size(struct widget *widget, int child_width, int child_height)
2732{
2733 struct display *display = widget->window->display;
2734 struct theme *t = display->theme;
2735 int decoration_width, decoration_height;
2736 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002737 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002738
2739 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002740 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002741 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002742 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002743
2744 width = child_width + decoration_width;
2745 height = child_height + decoration_height;
2746 } else {
2747 width = child_width;
2748 height = child_height;
2749 }
2750
2751 window_schedule_resize(widget->window, width, height);
2752}
2753
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002754static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002755frame_destroy(struct frame *frame)
2756{
Martin Minarik1998b152012-05-10 02:04:35 +02002757 struct frame_button *button, *tmp;
2758
2759 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2760 frame_button_destroy(button);
2761
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002762 /* frame->child must be destroyed by the application */
2763 widget_destroy(frame->widget);
2764 free(frame);
2765}
2766
2767static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002768input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002769 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002770{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002771 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002772 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002773
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002774 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002775 return;
2776
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002777 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002778 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002779 widget = old;
2780 if (input->grab)
2781 widget = input->grab;
2782 if (widget->leave_handler)
2783 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002784 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002785 }
2786
2787 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002788 widget = focus;
2789 if (input->grab)
2790 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002791 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002792 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002793 cursor = widget->enter_handler(focus, input, x, y,
2794 widget->user_data);
2795 else
2796 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002797
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002798 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002799 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002800}
2801
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002802void
2803input_grab(struct input *input, struct widget *widget, uint32_t button)
2804{
2805 input->grab = widget;
2806 input->grab_button = button;
2807}
2808
2809void
2810input_ungrab(struct input *input)
2811{
2812 struct widget *widget;
2813
2814 input->grab = NULL;
2815 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002816 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002817 input->sx, input->sy);
2818 input_set_focus_widget(input, widget, input->sx, input->sy);
2819 }
2820}
2821
2822static void
2823input_remove_pointer_focus(struct input *input)
2824{
2825 struct window *window = input->pointer_focus;
2826
2827 if (!window)
2828 return;
2829
2830 input_set_focus_widget(input, NULL, 0, 0);
2831
2832 input->pointer_focus = NULL;
2833 input->current_cursor = CURSOR_UNSET;
2834}
2835
2836static void
2837pointer_handle_enter(void *data, struct wl_pointer *pointer,
2838 uint32_t serial, struct wl_surface *surface,
2839 wl_fixed_t sx_w, wl_fixed_t sy_w)
2840{
2841 struct input *input = data;
2842 struct window *window;
2843 struct widget *widget;
2844 float sx = wl_fixed_to_double(sx_w);
2845 float sy = wl_fixed_to_double(sy_w);
2846
2847 if (!surface) {
2848 /* enter event for a window we've just destroyed */
2849 return;
2850 }
2851
2852 input->display->serial = serial;
2853 input->pointer_enter_serial = serial;
2854 input->pointer_focus = wl_surface_get_user_data(surface);
2855 window = input->pointer_focus;
2856
Pekka Paalanen99436862012-11-19 17:15:59 +02002857 if (window->resizing) {
2858 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002859 /* Schedule a redraw to free the pool */
2860 window_schedule_redraw(window);
2861 }
2862
2863 input->sx = sx;
2864 input->sy = sy;
2865
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002866 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002867 input_set_focus_widget(input, widget, sx, sy);
2868}
2869
2870static void
2871pointer_handle_leave(void *data, struct wl_pointer *pointer,
2872 uint32_t serial, struct wl_surface *surface)
2873{
2874 struct input *input = data;
2875
2876 input->display->serial = serial;
2877 input_remove_pointer_focus(input);
2878}
2879
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002880static void
Daniel Stone37816df2012-05-16 18:45:18 +01002881pointer_handle_motion(void *data, struct wl_pointer *pointer,
2882 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002883{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002884 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002885 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002886 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002887 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002888 float sx = wl_fixed_to_double(sx_w);
2889 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002890
Rob Bradford5f087742013-07-11 19:41:27 +01002891 /* when making the window smaller - e.g. after a unmaximise we might
2892 * still have a pending motion event that the compositor has picked
2893 * based on the old surface dimensions
2894 */
2895 if (sx > window->main_surface->allocation.width ||
2896 sy > window->main_surface->allocation.height)
2897 return;
2898
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002899 input->sx = sx;
2900 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002901
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002902 if (!window)
2903 return;
2904
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002905 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002906 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002907 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002908 }
2909
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002910 if (input->grab)
2911 widget = input->grab;
2912 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002913 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002914 if (widget) {
2915 if (widget->motion_handler)
2916 cursor = widget->motion_handler(input->focus_widget,
2917 input, time, sx, sy,
2918 widget->user_data);
2919 else
2920 cursor = widget->default_cursor;
2921 } else
2922 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002923
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002924 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002925}
2926
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002927static void
Daniel Stone37816df2012-05-16 18:45:18 +01002928pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002929 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002930{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002931 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002932 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002933 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002934
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002935 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002936 if (input->focus_widget && input->grab == NULL &&
2937 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002938 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002939
Neil Roberts6b28aad2012-01-23 19:11:18 +00002940 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002941 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002942 (*widget->button_handler)(widget,
2943 input, time,
2944 button, state,
2945 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002946
Daniel Stone4dbadb12012-05-30 16:31:51 +01002947 if (input->grab && input->grab_button == button &&
2948 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002949 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002950}
2951
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002952static void
Daniel Stone37816df2012-05-16 18:45:18 +01002953pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002954 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002955{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002956 struct input *input = data;
2957 struct widget *widget;
2958
2959 widget = input->focus_widget;
2960 if (input->grab)
2961 widget = input->grab;
2962 if (widget && widget->axis_handler)
2963 (*widget->axis_handler)(widget,
2964 input, time,
2965 axis, value,
2966 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002967}
2968
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002969static const struct wl_pointer_listener pointer_listener = {
2970 pointer_handle_enter,
2971 pointer_handle_leave,
2972 pointer_handle_motion,
2973 pointer_handle_button,
2974 pointer_handle_axis,
2975};
2976
2977static void
2978input_remove_keyboard_focus(struct input *input)
2979{
2980 struct window *window = input->keyboard_focus;
2981 struct itimerspec its;
2982
2983 its.it_interval.tv_sec = 0;
2984 its.it_interval.tv_nsec = 0;
2985 its.it_value.tv_sec = 0;
2986 its.it_value.tv_nsec = 0;
2987 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2988
2989 if (!window)
2990 return;
2991
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002992 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002993 if (window->keyboard_focus_handler)
2994 (*window->keyboard_focus_handler)(window, NULL,
2995 window->user_data);
2996
2997 input->keyboard_focus = NULL;
2998}
2999
3000static void
3001keyboard_repeat_func(struct task *task, uint32_t events)
3002{
3003 struct input *input =
3004 container_of(task, struct input, repeat_task);
3005 struct window *window = input->keyboard_focus;
3006 uint64_t exp;
3007
3008 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
3009 /* If we change the timer between the fd becoming
3010 * readable and getting here, there'll be nothing to
3011 * read and we get EAGAIN. */
3012 return;
3013
3014 if (window && window->key_handler) {
3015 (*window->key_handler)(window, input, input->repeat_time,
3016 input->repeat_key, input->repeat_sym,
3017 WL_KEYBOARD_KEY_STATE_PRESSED,
3018 window->user_data);
3019 }
3020}
3021
3022static void
3023keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
3024 uint32_t format, int fd, uint32_t size)
3025{
3026 struct input *input = data;
3027 char *map_str;
3028
3029 if (!data) {
3030 close(fd);
3031 return;
3032 }
3033
3034 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3035 close(fd);
3036 return;
3037 }
3038
3039 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3040 if (map_str == MAP_FAILED) {
3041 close(fd);
3042 return;
3043 }
3044
3045 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
3046 map_str,
3047 XKB_KEYMAP_FORMAT_TEXT_V1,
3048 0);
3049 munmap(map_str, size);
3050 close(fd);
3051
3052 if (!input->xkb.keymap) {
3053 fprintf(stderr, "failed to compile keymap\n");
3054 return;
3055 }
3056
3057 input->xkb.state = xkb_state_new(input->xkb.keymap);
3058 if (!input->xkb.state) {
3059 fprintf(stderr, "failed to create XKB state\n");
3060 xkb_map_unref(input->xkb.keymap);
3061 input->xkb.keymap = NULL;
3062 return;
3063 }
3064
3065 input->xkb.control_mask =
3066 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
3067 input->xkb.alt_mask =
3068 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
3069 input->xkb.shift_mask =
3070 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
3071}
3072
3073static void
3074keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3075 uint32_t serial, struct wl_surface *surface,
3076 struct wl_array *keys)
3077{
3078 struct input *input = data;
3079 struct window *window;
3080
3081 input->display->serial = serial;
3082 input->keyboard_focus = wl_surface_get_user_data(surface);
3083
3084 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003085 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003086 if (window->keyboard_focus_handler)
3087 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003088 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003089}
3090
3091static void
3092keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3093 uint32_t serial, struct wl_surface *surface)
3094{
3095 struct input *input = data;
3096
3097 input->display->serial = serial;
3098 input_remove_keyboard_focus(input);
3099}
3100
Scott Moreau210d0792012-03-22 10:47:01 -06003101static void
Daniel Stone37816df2012-05-16 18:45:18 +01003102keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003103 uint32_t serial, uint32_t time, uint32_t key,
3104 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003105{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003106 struct input *input = data;
3107 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003108 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003109 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003110 const xkb_keysym_t *syms;
3111 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003112 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003113
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003114 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003115 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003116 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003117 return;
3118
Daniel Stone97f68542012-05-30 16:32:01 +01003119 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003120
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003121 sym = XKB_KEY_NoSymbol;
3122 if (num_syms == 1)
3123 sym = syms[0];
3124
3125 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003126 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003127 window_set_maximized(window,
3128 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003129 } else if (sym == XKB_KEY_F11 &&
3130 window->fullscreen_handler &&
3131 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3132 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003133 } else if (sym == XKB_KEY_F4 &&
3134 input->modifiers == MOD_ALT_MASK &&
3135 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3136 if (window->close_handler)
3137 window->close_handler(window->parent,
3138 window->user_data);
3139 else
3140 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003141 } else if (window->key_handler) {
3142 (*window->key_handler)(window, input, time, key,
3143 sym, state, window->user_data);
3144 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003145
3146 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3147 key == input->repeat_key) {
3148 its.it_interval.tv_sec = 0;
3149 its.it_interval.tv_nsec = 0;
3150 its.it_value.tv_sec = 0;
3151 its.it_value.tv_nsec = 0;
3152 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3153 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3154 input->repeat_sym = sym;
3155 input->repeat_key = key;
3156 input->repeat_time = time;
3157 its.it_interval.tv_sec = 0;
3158 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3159 its.it_value.tv_sec = 0;
3160 its.it_value.tv_nsec = 400 * 1000 * 1000;
3161 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3162 }
3163}
3164
3165static void
Daniel Stone351eb612012-05-31 15:27:47 -04003166keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3167 uint32_t serial, uint32_t mods_depressed,
3168 uint32_t mods_latched, uint32_t mods_locked,
3169 uint32_t group)
3170{
3171 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003172 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003173
Matt Ropere61561f2013-06-24 16:52:43 +01003174 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3175 if (!input->xkb.keymap)
3176 return;
3177
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003178 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3179 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003180 mask = xkb_state_serialize_mods(input->xkb.state,
3181 XKB_STATE_DEPRESSED |
3182 XKB_STATE_LATCHED);
3183 input->modifiers = 0;
3184 if (mask & input->xkb.control_mask)
3185 input->modifiers |= MOD_CONTROL_MASK;
3186 if (mask & input->xkb.alt_mask)
3187 input->modifiers |= MOD_ALT_MASK;
3188 if (mask & input->xkb.shift_mask)
3189 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003190}
3191
Daniel Stone37816df2012-05-16 18:45:18 +01003192static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003193 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003194 keyboard_handle_enter,
3195 keyboard_handle_leave,
3196 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003197 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003198};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003199
3200static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003201touch_handle_down(void *data, struct wl_touch *wl_touch,
3202 uint32_t serial, uint32_t time, struct wl_surface *surface,
3203 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3204{
3205 struct input *input = data;
3206 struct widget *widget;
3207 float sx = wl_fixed_to_double(x_w);
3208 float sy = wl_fixed_to_double(y_w);
3209
3210 DBG("touch_handle_down: %i %i\n", id, wl_list_length(&input->touch_point_list));
3211
3212 input->touch_focus = wl_surface_get_user_data(surface);
3213 if (!input->touch_focus) {
3214 DBG("Failed to find to touch focus for surface %p\n", surface);
3215 return;
3216 }
3217
3218 widget = window_find_widget(input->touch_focus,
3219 wl_fixed_to_double(x_w),
3220 wl_fixed_to_double(y_w));
3221 if (widget) {
3222 struct touch_point *tp = xmalloc(sizeof *tp);
3223 if (tp) {
3224 tp->id = id;
3225 tp->widget = widget;
3226 wl_list_insert(&input->touch_point_list, &tp->link);
3227
3228 if (widget->touch_down_handler)
3229 (*widget->touch_down_handler)(widget, serial,
3230 time, id,
3231 sx, sy,
3232 widget->user_data);
3233 }
3234 }
3235}
3236
3237static void
3238touch_handle_up(void *data, struct wl_touch *wl_touch,
3239 uint32_t serial, uint32_t time, int32_t id)
3240{
3241 struct input *input = data;
3242 struct touch_point *tp, *tmp;
3243
3244 DBG("touch_handle_up: %i %i\n", id, wl_list_length(&input->touch_point_list));
3245
3246 if (!input->touch_focus) {
3247 DBG("No touch focus found for touch up event!\n");
3248 return;
3249 }
3250
3251 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3252 if (tp->id != id)
3253 continue;
3254
3255 if (tp->widget->touch_up_handler)
3256 (*tp->widget->touch_up_handler)(tp->widget, serial,
3257 time, id,
3258 tp->widget->user_data);
3259
3260 wl_list_remove(&tp->link);
3261 free(tp);
3262
3263 return;
3264 }
3265}
3266
3267static void
3268touch_handle_motion(void *data, struct wl_touch *wl_touch,
3269 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3270{
3271 struct input *input = data;
3272 struct touch_point *tp;
3273 float sx = wl_fixed_to_double(x_w);
3274 float sy = wl_fixed_to_double(y_w);
3275
3276 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3277
3278 if (!input->touch_focus) {
3279 DBG("No touch focus found for touch motion event!\n");
3280 return;
3281 }
3282
3283 wl_list_for_each(tp, &input->touch_point_list, link) {
3284 if (tp->id != id)
3285 continue;
3286
3287 if (tp->widget->touch_motion_handler)
3288 (*tp->widget->touch_motion_handler)(tp->widget, time,
3289 id, sx, sy,
3290 tp->widget->user_data);
3291 return;
3292 }
3293}
3294
3295static void
3296touch_handle_frame(void *data, struct wl_touch *wl_touch)
3297{
3298 struct input *input = data;
3299 struct touch_point *tp, *tmp;
3300
3301 DBG("touch_handle_frame\n");
3302
3303 if (!input->touch_focus) {
3304 DBG("No touch focus found for touch frame event!\n");
3305 return;
3306 }
3307
3308 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3309 if (tp->widget->touch_frame_handler)
3310 (*tp->widget->touch_frame_handler)(tp->widget, tp->widget->user_data);
3311
3312 wl_list_remove(&tp->link);
3313 free(tp);
3314 }
3315}
3316
3317static void
3318touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3319{
3320 struct input *input = data;
3321 struct touch_point *tp, *tmp;
3322
3323 DBG("touch_handle_cancel\n");
3324
3325 if (!input->touch_focus) {
3326 DBG("No touch focus found for touch cancel event!\n");
3327 return;
3328 }
3329
3330 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3331 if (tp->widget->touch_cancel_handler)
3332 (*tp->widget->touch_cancel_handler)(tp->widget, tp->widget->user_data);
3333
3334 wl_list_remove(&tp->link);
3335 free(tp);
3336 }
3337}
3338
3339static const struct wl_touch_listener touch_listener = {
3340 touch_handle_down,
3341 touch_handle_up,
3342 touch_handle_motion,
3343 touch_handle_frame,
3344 touch_handle_cancel,
3345};
3346
3347static void
Daniel Stone37816df2012-05-16 18:45:18 +01003348seat_handle_capabilities(void *data, struct wl_seat *seat,
3349 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003350{
Daniel Stone37816df2012-05-16 18:45:18 +01003351 struct input *input = data;
3352
3353 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3354 input->pointer = wl_seat_get_pointer(seat);
3355 wl_pointer_set_user_data(input->pointer, input);
3356 wl_pointer_add_listener(input->pointer, &pointer_listener,
3357 input);
3358 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3359 wl_pointer_destroy(input->pointer);
3360 input->pointer = NULL;
3361 }
3362
3363 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3364 input->keyboard = wl_seat_get_keyboard(seat);
3365 wl_keyboard_set_user_data(input->keyboard, input);
3366 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3367 input);
3368 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3369 wl_keyboard_destroy(input->keyboard);
3370 input->keyboard = NULL;
3371 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003372
3373 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3374 input->touch = wl_seat_get_touch(seat);
3375 wl_touch_set_user_data(input->touch, input);
3376 wl_touch_add_listener(input->touch, &touch_listener, input);
3377 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3378 wl_touch_destroy(input->touch);
3379 input->touch = NULL;
3380 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003381}
3382
Daniel Stone37816df2012-05-16 18:45:18 +01003383static const struct wl_seat_listener seat_listener = {
3384 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003385};
3386
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003387void
3388input_get_position(struct input *input, int32_t *x, int32_t *y)
3389{
3390 *x = input->sx;
3391 *y = input->sy;
3392}
3393
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003394struct display *
3395input_get_display(struct input *input)
3396{
3397 return input->display;
3398}
3399
Daniel Stone37816df2012-05-16 18:45:18 +01003400struct wl_seat *
3401input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003402{
Daniel Stone37816df2012-05-16 18:45:18 +01003403 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003404}
3405
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003406uint32_t
3407input_get_modifiers(struct input *input)
3408{
3409 return input->modifiers;
3410}
3411
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003412struct widget *
3413input_get_focus_widget(struct input *input)
3414{
3415 return input->focus_widget;
3416}
3417
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003418struct data_offer {
3419 struct wl_data_offer *offer;
3420 struct input *input;
3421 struct wl_array types;
3422 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003423
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003424 struct task io_task;
3425 int fd;
3426 data_func_t func;
3427 int32_t x, y;
3428 void *user_data;
3429};
3430
3431static void
3432data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3433{
3434 struct data_offer *offer = data;
3435 char **p;
3436
3437 p = wl_array_add(&offer->types, sizeof *p);
3438 *p = strdup(type);
3439}
3440
3441static const struct wl_data_offer_listener data_offer_listener = {
3442 data_offer_offer,
3443};
3444
3445static void
3446data_offer_destroy(struct data_offer *offer)
3447{
3448 char **p;
3449
3450 offer->refcount--;
3451 if (offer->refcount == 0) {
3452 wl_data_offer_destroy(offer->offer);
3453 for (p = offer->types.data; *p; p++)
3454 free(*p);
3455 wl_array_release(&offer->types);
3456 free(offer);
3457 }
3458}
3459
3460static void
3461data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003462 struct wl_data_device *data_device,
3463 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003464{
3465 struct data_offer *offer;
3466
Brian Lovinbc919262013-08-07 15:34:59 -07003467 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003468
3469 wl_array_init(&offer->types);
3470 offer->refcount = 1;
3471 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003472 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003473 wl_data_offer_add_listener(offer->offer,
3474 &data_offer_listener, offer);
3475}
3476
3477static void
3478data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003479 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003480 wl_fixed_t x_w, wl_fixed_t y_w,
3481 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003482{
3483 struct input *input = data;
3484 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003485 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003486 float x = wl_fixed_to_double(x_w);
3487 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003488 char **p;
3489
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003490 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003491 window = wl_surface_get_user_data(surface);
3492 input->pointer_focus = window;
3493
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003494 if (offer) {
3495 input->drag_offer = wl_data_offer_get_user_data(offer);
3496
3497 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3498 *p = NULL;
3499
3500 types_data = input->drag_offer->types.data;
3501 } else {
3502 input->drag_offer = NULL;
3503 types_data = NULL;
3504 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003505
3506 window = input->pointer_focus;
3507 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003508 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003509 window->user_data);
3510}
3511
3512static void
3513data_device_leave(void *data, struct wl_data_device *data_device)
3514{
3515 struct input *input = data;
3516
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003517 if (input->drag_offer) {
3518 data_offer_destroy(input->drag_offer);
3519 input->drag_offer = NULL;
3520 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003521}
3522
3523static void
3524data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003525 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003526{
3527 struct input *input = data;
3528 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003529 float x = wl_fixed_to_double(x_w);
3530 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003531 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003532
3533 input->sx = x;
3534 input->sy = y;
3535
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003536 if (input->drag_offer)
3537 types_data = input->drag_offer->types.data;
3538 else
3539 types_data = NULL;
3540
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003541 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003542 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003543 window->user_data);
3544}
3545
3546static void
3547data_device_drop(void *data, struct wl_data_device *data_device)
3548{
3549 struct input *input = data;
3550 struct window *window = input->pointer_focus;
3551
3552 if (window->drop_handler)
3553 window->drop_handler(window, input,
3554 input->sx, input->sy, window->user_data);
3555}
3556
3557static void
3558data_device_selection(void *data,
3559 struct wl_data_device *wl_data_device,
3560 struct wl_data_offer *offer)
3561{
3562 struct input *input = data;
3563 char **p;
3564
3565 if (input->selection_offer)
3566 data_offer_destroy(input->selection_offer);
3567
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003568 if (offer) {
3569 input->selection_offer = wl_data_offer_get_user_data(offer);
3570 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3571 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003572 } else {
3573 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003574 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003575}
3576
3577static const struct wl_data_device_listener data_device_listener = {
3578 data_device_data_offer,
3579 data_device_enter,
3580 data_device_leave,
3581 data_device_motion,
3582 data_device_drop,
3583 data_device_selection
3584};
3585
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003586static void
3587input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003588{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003589 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003590 struct wl_cursor *cursor;
3591 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003592
Daniel Stone80972742012-11-07 17:51:39 +11003593 if (!input->pointer)
3594 return;
3595
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003596 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003597 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003598 return;
3599
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003600 if (index >= (int) cursor->image_count) {
3601 fprintf(stderr, "cursor index out of range\n");
3602 return;
3603 }
3604
3605 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003606 buffer = wl_cursor_image_get_buffer(image);
3607 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003608 return;
3609
Kristian Høgsbergae277372012-08-01 09:41:08 -04003610 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003611 input->pointer_surface,
3612 image->hotspot_x, image->hotspot_y);
3613 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3614 wl_surface_damage(input->pointer_surface, 0, 0,
3615 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003616 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003617}
3618
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003619static const struct wl_callback_listener pointer_surface_listener;
3620
3621static void
3622pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3623 uint32_t time)
3624{
3625 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003626 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003627 int i;
3628
3629 if (callback) {
3630 assert(callback == input->cursor_frame_cb);
3631 wl_callback_destroy(callback);
3632 input->cursor_frame_cb = NULL;
3633 }
3634
Daniel Stone80972742012-11-07 17:51:39 +11003635 if (!input->pointer)
3636 return;
3637
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003638 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003639 wl_pointer_set_cursor(input->pointer,
3640 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003641 NULL, 0, 0);
3642 return;
3643 }
3644
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003645 if (input->current_cursor == CURSOR_UNSET)
3646 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003647 cursor = input->display->cursors[input->current_cursor];
3648 if (!cursor)
3649 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003650
3651 /* FIXME We don't have the current time on the first call so we set
3652 * the animation start to the time of the first frame callback. */
3653 if (time == 0)
3654 input->cursor_anim_start = 0;
3655 else if (input->cursor_anim_start == 0)
3656 input->cursor_anim_start = time;
3657
3658 if (time == 0 || input->cursor_anim_start == 0)
3659 i = 0;
3660 else
3661 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3662
Pekka Paalanenbc106382012-10-10 12:49:31 +03003663 if (cursor->image_count > 1) {
3664 input->cursor_frame_cb =
3665 wl_surface_frame(input->pointer_surface);
3666 wl_callback_add_listener(input->cursor_frame_cb,
3667 &pointer_surface_listener, input);
3668 }
3669
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003670 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003671}
3672
3673static const struct wl_callback_listener pointer_surface_listener = {
3674 pointer_surface_frame_callback
3675};
3676
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003677void
3678input_set_pointer_image(struct input *input, int pointer)
3679{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003680 int force = 0;
3681
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003682 if (!input->pointer)
3683 return;
3684
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003685 if (input->pointer_enter_serial > input->cursor_serial)
3686 force = 1;
3687
3688 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003689 return;
3690
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003691 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003692 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003693 if (!input->cursor_frame_cb)
3694 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003695 else if (force) {
3696 /* The current frame callback may be stuck if, for instance,
3697 * the set cursor request was processed by the server after
3698 * this client lost the focus. In this case the cursor surface
3699 * might not be mapped and the frame callback wouldn't ever
3700 * complete. Send a set_cursor and attach to try to map the
3701 * cursor surface again so that the callback will finish */
3702 input_set_pointer_image_index(input, 0);
3703 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003704}
3705
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003706struct wl_data_device *
3707input_get_data_device(struct input *input)
3708{
3709 return input->data_device;
3710}
3711
3712void
3713input_set_selection(struct input *input,
3714 struct wl_data_source *source, uint32_t time)
3715{
3716 wl_data_device_set_selection(input->data_device, source, time);
3717}
3718
3719void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003720input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003721{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003722 wl_data_offer_accept(input->drag_offer->offer,
3723 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003724}
3725
3726static void
3727offer_io_func(struct task *task, uint32_t events)
3728{
3729 struct data_offer *offer =
3730 container_of(task, struct data_offer, io_task);
3731 unsigned int len;
3732 char buffer[4096];
3733
3734 len = read(offer->fd, buffer, sizeof buffer);
3735 offer->func(buffer, len,
3736 offer->x, offer->y, offer->user_data);
3737
3738 if (len == 0) {
3739 close(offer->fd);
3740 data_offer_destroy(offer);
3741 }
3742}
3743
3744static void
3745data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3746 data_func_t func, void *user_data)
3747{
3748 int p[2];
3749
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003750 if (pipe2(p, O_CLOEXEC) == -1)
3751 return;
3752
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003753 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3754 close(p[1]);
3755
3756 offer->io_task.run = offer_io_func;
3757 offer->fd = p[0];
3758 offer->func = func;
3759 offer->refcount++;
3760 offer->user_data = user_data;
3761
3762 display_watch_fd(offer->input->display,
3763 offer->fd, EPOLLIN, &offer->io_task);
3764}
3765
3766void
3767input_receive_drag_data(struct input *input, const char *mime_type,
3768 data_func_t func, void *data)
3769{
3770 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3771 input->drag_offer->x = input->sx;
3772 input->drag_offer->y = input->sy;
3773}
3774
3775int
3776input_receive_selection_data(struct input *input, const char *mime_type,
3777 data_func_t func, void *data)
3778{
3779 char **p;
3780
3781 if (input->selection_offer == NULL)
3782 return -1;
3783
3784 for (p = input->selection_offer->types.data; *p; p++)
3785 if (strcmp(mime_type, *p) == 0)
3786 break;
3787
3788 if (*p == NULL)
3789 return -1;
3790
3791 data_offer_receive_data(input->selection_offer,
3792 mime_type, func, data);
3793 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003794}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003795
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003796int
3797input_receive_selection_data_to_fd(struct input *input,
3798 const char *mime_type, int fd)
3799{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003800 if (input->selection_offer)
3801 wl_data_offer_receive(input->selection_offer->offer,
3802 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003803
3804 return 0;
3805}
3806
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003807void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003808window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003809{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003810 if (!window->shell_surface)
3811 return;
3812
Daniel Stone37816df2012-05-16 18:45:18 +01003813 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003814}
3815
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003816static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003817surface_set_synchronized(struct surface *surface)
3818{
3819 if (!surface->subsurface)
3820 return;
3821
3822 if (surface->synchronized)
3823 return;
3824
3825 wl_subsurface_set_sync(surface->subsurface);
3826 surface->synchronized = 1;
3827}
3828
3829static void
3830surface_set_synchronized_default(struct surface *surface)
3831{
3832 if (!surface->subsurface)
3833 return;
3834
3835 if (surface->synchronized == surface->synchronized_default)
3836 return;
3837
3838 if (surface->synchronized_default)
3839 wl_subsurface_set_sync(surface->subsurface);
3840 else
3841 wl_subsurface_set_desync(surface->subsurface);
3842
3843 surface->synchronized = surface->synchronized_default;
3844}
3845
3846static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003847surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003848{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003849 struct widget *widget = surface->widget;
3850 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003851
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003852 if (surface->input_region) {
3853 wl_region_destroy(surface->input_region);
3854 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003855 }
3856
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003857 if (surface->opaque_region)
3858 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003859
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003860 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003861
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003862 if (widget->resize_handler)
3863 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003864 widget->allocation.width,
3865 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003866 widget->user_data);
3867
Pekka Paalanen35e82632013-04-25 13:57:48 +03003868 if (surface->subsurface &&
3869 (surface->allocation.x != widget->allocation.x ||
3870 surface->allocation.y != widget->allocation.y)) {
3871 wl_subsurface_set_position(surface->subsurface,
3872 widget->allocation.x,
3873 widget->allocation.y);
3874 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003875 if (surface->allocation.width != widget->allocation.width ||
3876 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003877 window_schedule_redraw(widget->window);
3878 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003879 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003880
3881 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003882 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003883 widget->allocation.width,
3884 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003885}
3886
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003887static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003888hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3889{
3890 /*
3891 * This hack should be removed, when EGL respects
3892 * eglSwapInterval(0).
3893 *
3894 * If this window has sub-surfaces, especially a free-running
3895 * EGL-widget, we need to post the parent surface once with
3896 * all the old state to guarantee, that the EGL-widget will
3897 * receive its frame callback soon. Otherwise, a forced call
3898 * to eglSwapBuffers may end up blocking, waiting for a frame
3899 * event that will never come, because we will commit the parent
3900 * surface with all new state only after eglSwapBuffers returns.
3901 *
3902 * This assumes, that:
3903 * 1. When the EGL widget's resize hook is called, it pauses.
3904 * 2. When the EGL widget's redraw hook is called, it forces a
3905 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3906 * In a single threaded application condition 1 is a no-op.
3907 *
3908 * XXX: This should actually be after the surface_resize() calls,
3909 * but cannot, because then it would commit the incomplete state
3910 * accumulated from the widget resize hooks.
3911 */
3912 if (window->subsurface_list.next != &window->main_surface->link ||
3913 window->subsurface_list.prev != &window->main_surface->link)
3914 wl_surface_commit(window->main_surface->surface);
3915}
3916
3917static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003918idle_resize(struct window *window)
3919{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003920 struct surface *surface;
3921
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003922 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003923 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003924
Pekka Paalanen71233882013-04-25 13:57:53 +03003925 DBG("from %dx%d to %dx%d\n",
3926 window->main_surface->server_allocation.width,
3927 window->main_surface->server_allocation.height,
3928 window->pending_allocation.width,
3929 window->pending_allocation.height);
3930
Pekka Paalanene9297f82013-04-25 13:57:51 +03003931 hack_prevent_EGL_sub_surface_deadlock(window);
3932
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003933 widget_set_allocation(window->main_surface->widget,
3934 window->pending_allocation.x,
3935 window->pending_allocation.y,
3936 window->pending_allocation.width,
3937 window->pending_allocation.height);
3938
3939 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003940
3941 /* The main surface is in the list, too. Main surface's
3942 * resize_handler is responsible for calling widget_set_allocation()
3943 * on all sub-surface root widgets, so they will be resized
3944 * properly.
3945 */
3946 wl_list_for_each(surface, &window->subsurface_list, link) {
3947 if (surface == window->main_surface)
3948 continue;
3949
3950 surface_set_synchronized(surface);
3951 surface_resize(surface);
3952 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003953}
3954
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003955void
3956window_schedule_resize(struct window *window, int width, int height)
3957{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003958 window->pending_allocation.x = 0;
3959 window->pending_allocation.y = 0;
3960 window->pending_allocation.width = width;
3961 window->pending_allocation.height = height;
3962
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003963 if (window->min_allocation.width == 0)
3964 window->min_allocation = window->pending_allocation;
3965 if (window->pending_allocation.width < window->min_allocation.width)
3966 window->pending_allocation.width = window->min_allocation.width;
3967 if (window->pending_allocation.height < window->min_allocation.height)
3968 window->pending_allocation.height = window->min_allocation.height;
3969
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003970 window->resize_needed = 1;
3971 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003972}
3973
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003974void
3975widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3976{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003977 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003978}
3979
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003980static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003981handle_ping(void *data, struct wl_shell_surface *shell_surface,
3982 uint32_t serial)
3983{
3984 wl_shell_surface_pong(shell_surface, serial);
3985}
3986
3987static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003988handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003989 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003990{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003991 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003992
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003993 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003994 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003995}
3996
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003997static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003998menu_destroy(struct menu *menu)
3999{
4000 widget_destroy(menu->widget);
4001 window_destroy(menu->window);
4002 free(menu);
4003}
4004
4005static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004006handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
4007{
4008 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02004009 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004010
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004011 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004012 * device. Or just use wl_callback. And this really needs to
4013 * be a window vfunc that the menu can set. And we need the
4014 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004015
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004016 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004017 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004018}
4019
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004020static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06004021 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004022 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004023 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004024};
4025
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004026void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004027window_get_allocation(struct window *window,
4028 struct rectangle *allocation)
4029{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004030 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004031}
4032
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004033static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004034widget_redraw(struct widget *widget)
4035{
4036 struct widget *child;
4037
4038 if (widget->redraw_handler)
4039 widget->redraw_handler(widget, widget->user_data);
4040 wl_list_for_each(child, &widget->child_list, link)
4041 widget_redraw(child);
4042}
4043
4044static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004045frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4046{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004047 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004048
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004049 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004050 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004051 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004052 surface->frame_cb = NULL;
4053
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004054 surface->last_time = time;
4055
Pekka Paalanen71233882013-04-25 13:57:53 +03004056 if (surface->redraw_needed || surface->window->redraw_needed) {
4057 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004058 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004059 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004060}
4061
4062static const struct wl_callback_listener listener = {
4063 frame_callback
4064};
4065
4066static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004067surface_redraw(struct surface *surface)
4068{
Pekka Paalanen71233882013-04-25 13:57:53 +03004069 DBG_OBJ(surface->surface, "begin\n");
4070
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004071 if (!surface->window->redraw_needed && !surface->redraw_needed)
4072 return;
4073
4074 /* Whole-window redraw forces a redraw even if the previous has
4075 * not yet hit the screen.
4076 */
4077 if (surface->frame_cb) {
4078 if (!surface->window->redraw_needed)
4079 return;
4080
Pekka Paalanen71233882013-04-25 13:57:53 +03004081 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004082 wl_callback_destroy(surface->frame_cb);
4083 }
4084
4085 surface->frame_cb = wl_surface_frame(surface->surface);
4086 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004087 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004088
4089 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004090 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004091 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004092 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004093}
4094
4095static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004096idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004097{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004098 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004099 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004100
Pekka Paalanen71233882013-04-25 13:57:53 +03004101 DBG(" --------- \n");
4102
Pekka Paalaneneebff542013-04-25 13:57:52 +03004103 wl_list_init(&window->redraw_task.link);
4104 window->redraw_task_scheduled = 0;
4105
4106 if (window->resize_needed) {
4107 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004108 if (window->main_surface->frame_cb) {
4109 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004110 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004111 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004112
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004113 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03004114 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004115
Pekka Paalanen35e82632013-04-25 13:57:48 +03004116 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004117 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004118
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004119 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004120 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004121
4122 wl_list_for_each(surface, &window->subsurface_list, link)
4123 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004124}
4125
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004126static void
4127window_schedule_redraw_task(struct window *window)
4128{
4129 if (window->configure_requests)
4130 return;
4131 if (!window->redraw_task_scheduled) {
4132 window->redraw_task.run = idle_redraw;
4133 display_defer(window->display, &window->redraw_task);
4134 window->redraw_task_scheduled = 1;
4135 }
4136}
4137
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004138void
4139window_schedule_redraw(struct window *window)
4140{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004141 struct surface *surface;
4142
Pekka Paalanen71233882013-04-25 13:57:53 +03004143 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4144
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004145 wl_list_for_each(surface, &window->subsurface_list, link)
4146 surface->redraw_needed = 1;
4147
4148 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004149}
4150
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004151int
4152window_is_fullscreen(struct window *window)
4153{
4154 return window->type == TYPE_FULLSCREEN;
4155}
4156
MoD063a8822013-03-02 23:43:57 +00004157static void
4158configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4159{
4160 struct window *window = data;
4161
4162 wl_callback_destroy(callback);
4163 window->configure_requests--;
4164
4165 if (!window->configure_requests)
4166 window_schedule_redraw(window);
4167}
4168
4169static struct wl_callback_listener configure_request_listener = {
4170 configure_request_completed,
4171};
4172
4173static void
4174window_defer_redraw_until_configure(struct window* window)
4175{
4176 struct wl_callback *callback;
4177
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004178 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004179 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004180 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004181 }
4182
4183 callback = wl_display_sync(window->display->display);
4184 wl_callback_add_listener(callback, &configure_request_listener, window);
4185 window->configure_requests++;
4186}
4187
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004188void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004189window_set_fullscreen(struct window *window, int fullscreen)
4190{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004191 if (!window->display->shell)
4192 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004193
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004194 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004195 return;
4196
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004197 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004198 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004199 if (window->type == TYPE_TOPLEVEL) {
4200 window->saved_allocation = window->main_surface->allocation;
4201 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004202 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004203 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004204 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004205 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004206 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004207 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004208 if (window->saved_type == TYPE_MAXIMIZED) {
4209 window_set_maximized(window, 1);
4210 } else {
4211 window->type = TYPE_TOPLEVEL;
4212 wl_shell_surface_set_toplevel(window->shell_surface);
4213 window_schedule_resize(window,
4214 window->saved_allocation.width,
4215 window->saved_allocation.height);
4216 }
4217
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004218 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004219}
4220
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004221void
4222window_set_fullscreen_method(struct window *window,
4223 enum wl_shell_surface_fullscreen_method method)
4224{
4225 window->fullscreen_method = method;
4226}
4227
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004228int
4229window_is_maximized(struct window *window)
4230{
4231 return window->type == TYPE_MAXIMIZED;
4232}
4233
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004234void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004235window_set_maximized(struct window *window, int maximized)
4236{
4237 if (!window->display->shell)
4238 return;
4239
4240 if ((window->type == TYPE_MAXIMIZED) == maximized)
4241 return;
4242
4243 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004244 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004245 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4246 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004247 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004248 } else if (window->type == TYPE_FULLSCREEN) {
4249 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4250 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004251 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004252 } else {
4253 wl_shell_surface_set_toplevel(window->shell_surface);
4254 window->type = TYPE_TOPLEVEL;
4255 window_schedule_resize(window,
4256 window->saved_allocation.width,
4257 window->saved_allocation.height);
4258 }
4259}
4260
4261void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004262window_set_user_data(struct window *window, void *data)
4263{
4264 window->user_data = data;
4265}
4266
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004267void *
4268window_get_user_data(struct window *window)
4269{
4270 return window->user_data;
4271}
4272
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004273void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004274window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004275 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004276{
4277 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004278}
4279
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004280void
4281window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004282 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004283{
4284 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004285}
4286
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004287void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004288window_set_data_handler(struct window *window, window_data_handler_t handler)
4289{
4290 window->data_handler = handler;
4291}
4292
4293void
4294window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4295{
4296 window->drop_handler = handler;
4297}
4298
4299void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004300window_set_close_handler(struct window *window,
4301 window_close_handler_t handler)
4302{
4303 window->close_handler = handler;
4304}
4305
4306void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004307window_set_fullscreen_handler(struct window *window,
4308 window_fullscreen_handler_t handler)
4309{
4310 window->fullscreen_handler = handler;
4311}
4312
4313void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004314window_set_output_handler(struct window *window,
4315 window_output_handler_t handler)
4316{
4317 window->output_handler = handler;
4318}
4319
4320void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004321window_set_title(struct window *window, const char *title)
4322{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004323 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004324 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004325 if (window->shell_surface)
4326 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004327}
4328
4329const char *
4330window_get_title(struct window *window)
4331{
4332 return window->title;
4333}
4334
4335void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004336window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4337{
4338 struct text_cursor_position *text_cursor_position =
4339 window->display->text_cursor_position;
4340
Scott Moreau9295ce02012-06-01 12:46:10 -06004341 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004342 return;
4343
4344 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004345 window->main_surface->surface,
4346 wl_fixed_from_int(x),
4347 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004348}
4349
4350void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004351window_damage(struct window *window, int32_t x, int32_t y,
4352 int32_t width, int32_t height)
4353{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004354 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004355}
4356
Casey Dahlin9074db52012-04-19 22:50:09 -04004357static void
4358surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004359 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004360{
Rob Bradford7507b572012-05-15 17:55:34 +01004361 struct window *window = data;
4362 struct output *output;
4363 struct output *output_found = NULL;
4364 struct window_output *window_output;
4365
4366 wl_list_for_each(output, &window->display->output_list, link) {
4367 if (output->output == wl_output) {
4368 output_found = output;
4369 break;
4370 }
4371 }
4372
4373 if (!output_found)
4374 return;
4375
Brian Lovinbc919262013-08-07 15:34:59 -07004376 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004377 window_output->output = output_found;
4378
4379 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004380
4381 if (window->output_handler)
4382 window->output_handler(window, output_found, 1,
4383 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004384}
4385
4386static void
4387surface_leave(void *data,
4388 struct wl_surface *wl_surface, struct wl_output *output)
4389{
Rob Bradford7507b572012-05-15 17:55:34 +01004390 struct window *window = data;
4391 struct window_output *window_output;
4392 struct window_output *window_output_found = NULL;
4393
4394 wl_list_for_each(window_output, &window->window_output_list, link) {
4395 if (window_output->output->output == output) {
4396 window_output_found = window_output;
4397 break;
4398 }
4399 }
4400
4401 if (window_output_found) {
4402 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004403
4404 if (window->output_handler)
4405 window->output_handler(window, window_output->output,
4406 0, window->user_data);
4407
Rob Bradford7507b572012-05-15 17:55:34 +01004408 free(window_output_found);
4409 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004410}
4411
4412static const struct wl_surface_listener surface_listener = {
4413 surface_enter,
4414 surface_leave
4415};
4416
Pekka Paalanen4e373742013-02-13 16:17:13 +02004417static struct surface *
4418surface_create(struct window *window)
4419{
4420 struct display *display = window->display;
4421 struct surface *surface;
4422
Brian Lovinbc919262013-08-07 15:34:59 -07004423 surface = xmalloc(sizeof *surface);
4424 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004425 if (!surface)
4426 return NULL;
4427
4428 surface->window = window;
4429 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004430 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004431 wl_surface_add_listener(surface->surface, &surface_listener, window);
4432
Pekka Paalanen35e82632013-04-25 13:57:48 +03004433 wl_list_insert(&window->subsurface_list, &surface->link);
4434
Pekka Paalanen4e373742013-02-13 16:17:13 +02004435 return surface;
4436}
4437
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004438static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004439window_create_internal(struct display *display,
4440 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004441{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004442 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004443 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004444
Peter Huttererf3d62272013-08-08 11:57:05 +10004445 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004446 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004447 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004448 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004449
4450 surface = surface_create(window);
4451 window->main_surface = surface;
4452
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004453 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004454 window->shell_surface =
4455 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004456 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004457 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004458 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004459
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004460 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004461 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004462 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004463 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004464
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004465 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004466#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004467 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004468#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004469 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004470#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004471 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004472 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004473
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004474 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004475 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004476 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004477
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004478 if (window->shell_surface) {
4479 wl_shell_surface_set_user_data(window->shell_surface, window);
4480 wl_shell_surface_add_listener(window->shell_surface,
4481 &shell_surface_listener, window);
4482 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004483
Rob Bradford7507b572012-05-15 17:55:34 +01004484 wl_list_init (&window->window_output_list);
4485
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004486 return window;
4487}
4488
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004489struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004490window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004491{
Kristian Høgsberg41624832013-07-25 16:53:10 -07004492 return window_create_internal(display, NULL, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004493}
4494
4495struct window *
4496window_create_custom(struct display *display)
4497{
Kristian Høgsbergce278412013-07-25 15:20:20 -07004498 return window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004499}
4500
4501struct window *
4502window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004503 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004504{
4505 struct window *window;
4506
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004507 window = window_create_internal(parent->display,
4508 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004509
4510 window->x = x;
4511 window->y = y;
4512
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004513 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004514 wl_shell_surface_set_transient(
4515 window->shell_surface,
4516 window->parent->main_surface->surface,
4517 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004518
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004519 return window;
4520}
4521
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004522static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004523menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004524{
4525 int next;
4526
4527 next = (sy - 8) / 20;
4528 if (menu->current != next) {
4529 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004530 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004531 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004532}
4533
4534static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004535menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004536 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004537 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004538{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004539 struct menu *menu = data;
4540
4541 if (widget == menu->widget)
4542 menu_set_item(data, y);
4543
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004544 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004545}
4546
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004547static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004548menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004549 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004550{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004551 struct menu *menu = data;
4552
4553 if (widget == menu->widget)
4554 menu_set_item(data, y);
4555
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004556 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004557}
4558
4559static void
4560menu_leave_handler(struct widget *widget, struct input *input, void *data)
4561{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004562 struct menu *menu = data;
4563
4564 if (widget == menu->widget)
4565 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004566}
4567
4568static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004569menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004570 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004571 uint32_t button, enum wl_pointer_button_state state,
4572 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004573
4574{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004575 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004576
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004577 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4578 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004579 /* Either relase after press-drag-release or
4580 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004581 menu->func(menu->window->parent,
4582 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004583 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004584 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004585 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004586 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004587 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004588}
4589
4590static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004591menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004592{
4593 cairo_t *cr;
4594 const int32_t r = 3, margin = 3;
4595 struct menu *menu = data;
4596 int32_t width, height, i;
4597
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004598 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004599 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4600 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4601 cairo_paint(cr);
4602
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004603 width = widget->allocation.width;
4604 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004605 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004606
4607 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004608 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4609 cairo_fill(cr);
4610
4611 for (i = 0; i < menu->count; i++) {
4612 if (i == menu->current) {
4613 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4614 cairo_rectangle(cr, margin, i * 20 + margin,
4615 width - 2 * margin, 20);
4616 cairo_fill(cr);
4617 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4618 cairo_move_to(cr, 10, i * 20 + 16);
4619 cairo_show_text(cr, menu->entries[i]);
4620 } else {
4621 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4622 cairo_move_to(cr, 10, i * 20 + 16);
4623 cairo_show_text(cr, menu->entries[i]);
4624 }
4625 }
4626
4627 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004628}
4629
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004630void
4631window_show_menu(struct display *display,
4632 struct input *input, uint32_t time, struct window *parent,
4633 int32_t x, int32_t y,
4634 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004635{
4636 struct window *window;
4637 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004638 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004639
4640 menu = malloc(sizeof *menu);
4641 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004642 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004643
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004644 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004645 if (!window) {
4646 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004647 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004648 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004649
4650 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004651 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004652 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4653 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004654 menu->entries = entries;
4655 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004656 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004657 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004658 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004659 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004660 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004661 window->type = TYPE_MENU;
4662 window->x = x;
4663 window->y = y;
4664
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004665 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004666 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004667 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004668 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004669 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004670
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004671 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004672 widget_set_enter_handler(menu->widget, menu_enter_handler);
4673 widget_set_leave_handler(menu->widget, menu_leave_handler);
4674 widget_set_motion_handler(menu->widget, menu_motion_handler);
4675 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004676
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004677 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004678 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004679}
4680
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004681void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004682window_set_buffer_type(struct window *window, enum window_buffer_type type)
4683{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004684 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004685}
4686
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004687void
4688window_set_preferred_format(struct window *window,
4689 enum preferred_format format)
4690{
4691 window->preferred_format = format;
4692}
4693
Pekka Paalanen35e82632013-04-25 13:57:48 +03004694struct widget *
4695window_add_subsurface(struct window *window, void *data,
4696 enum subsurface_mode default_mode)
4697{
4698 struct widget *widget;
4699 struct surface *surface;
4700 struct wl_surface *parent;
4701 struct wl_subcompositor *subcompo = window->display->subcompositor;
4702
Pekka Paalanen35e82632013-04-25 13:57:48 +03004703 surface = surface_create(window);
4704 widget = widget_create(window, surface, data);
4705 wl_list_init(&widget->link);
4706 surface->widget = widget;
4707
4708 parent = window->main_surface->surface;
4709 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4710 surface->surface,
4711 parent);
4712 surface->synchronized = 1;
4713
4714 switch (default_mode) {
4715 case SUBSURFACE_SYNCHRONIZED:
4716 surface->synchronized_default = 1;
4717 break;
4718 case SUBSURFACE_DESYNCHRONIZED:
4719 surface->synchronized_default = 0;
4720 break;
4721 default:
4722 assert(!"bad enum subsurface_mode");
4723 }
4724
4725 return widget;
4726}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004727
4728static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004729display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004730 struct wl_output *wl_output,
4731 int x, int y,
4732 int physical_width,
4733 int physical_height,
4734 int subpixel,
4735 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004736 const char *model,
4737 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004738{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004739 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004740
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004741 output->allocation.x = x;
4742 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004743 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004744}
4745
4746static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004747display_handle_done(void *data,
4748 struct wl_output *wl_output)
4749{
4750}
4751
4752static void
4753display_handle_scale(void *data,
4754 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004755 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004756{
4757 struct output *output = data;
4758
4759 output->scale = scale;
4760}
4761
4762static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004763display_handle_mode(void *data,
4764 struct wl_output *wl_output,
4765 uint32_t flags,
4766 int width,
4767 int height,
4768 int refresh)
4769{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004770 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004771 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004772
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004773 if (flags & WL_OUTPUT_MODE_CURRENT) {
4774 output->allocation.width = width;
4775 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004776 if (display->output_configure_handler)
4777 (*display->output_configure_handler)(
4778 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004779 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004780}
4781
4782static const struct wl_output_listener output_listener = {
4783 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004784 display_handle_mode,
4785 display_handle_done,
4786 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004787};
4788
4789static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004790display_add_output(struct display *d, uint32_t id)
4791{
4792 struct output *output;
4793
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004794 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004795 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004796 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004797 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004798 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004799 wl_list_insert(d->output_list.prev, &output->link);
4800
4801 wl_output_add_listener(output->output, &output_listener, output);
4802}
4803
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004804static void
4805output_destroy(struct output *output)
4806{
4807 if (output->destroy_handler)
4808 (*output->destroy_handler)(output, output->user_data);
4809
4810 wl_output_destroy(output->output);
4811 wl_list_remove(&output->link);
4812 free(output);
4813}
4814
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004815void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004816display_set_global_handler(struct display *display,
4817 display_global_handler_t handler)
4818{
4819 struct global *global;
4820
4821 display->global_handler = handler;
4822 if (!handler)
4823 return;
4824
4825 wl_list_for_each(global, &display->global_list, link)
4826 display->global_handler(display,
4827 global->name, global->interface,
4828 global->version, display->user_data);
4829}
4830
4831void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004832display_set_output_configure_handler(struct display *display,
4833 display_output_handler_t handler)
4834{
4835 struct output *output;
4836
4837 display->output_configure_handler = handler;
4838 if (!handler)
4839 return;
4840
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004841 wl_list_for_each(output, &display->output_list, link) {
4842 if (output->allocation.width == 0 &&
4843 output->allocation.height == 0)
4844 continue;
4845
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004846 (*display->output_configure_handler)(output,
4847 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004848 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004849}
4850
4851void
4852output_set_user_data(struct output *output, void *data)
4853{
4854 output->user_data = data;
4855}
4856
4857void *
4858output_get_user_data(struct output *output)
4859{
4860 return output->user_data;
4861}
4862
4863void
4864output_set_destroy_handler(struct output *output,
4865 display_output_handler_t handler)
4866{
4867 output->destroy_handler = handler;
4868 /* FIXME: implement this, once we have way to remove outputs */
4869}
4870
4871void
Scott Moreau4e072362012-09-29 02:03:11 -06004872output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004873{
Scott Moreau4e072362012-09-29 02:03:11 -06004874 struct rectangle allocation = output->allocation;
4875
4876 switch (output->transform) {
4877 case WL_OUTPUT_TRANSFORM_90:
4878 case WL_OUTPUT_TRANSFORM_270:
4879 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4880 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4881 /* Swap width and height */
4882 allocation.width = output->allocation.height;
4883 allocation.height = output->allocation.width;
4884 break;
4885 }
4886
4887 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004888}
4889
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004890struct wl_output *
4891output_get_wl_output(struct output *output)
4892{
4893 return output->output;
4894}
4895
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004896enum wl_output_transform
4897output_get_transform(struct output *output)
4898{
4899 return output->transform;
4900}
4901
Alexander Larssonafd319a2013-05-22 14:41:27 +02004902uint32_t
4903output_get_scale(struct output *output)
4904{
4905 return output->scale;
4906}
4907
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004908static void
Daniel Stone97f68542012-05-30 16:32:01 +01004909fini_xkb(struct input *input)
4910{
4911 xkb_state_unref(input->xkb.state);
4912 xkb_map_unref(input->xkb.keymap);
4913}
4914
4915static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004916display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004917{
4918 struct input *input;
4919
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004920 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004921 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004922 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004923 input->pointer_focus = NULL;
4924 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004925 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004926 wl_list_insert(d->input_list.prev, &input->link);
4927
Daniel Stone37816df2012-05-16 18:45:18 +01004928 wl_seat_add_listener(input->seat, &seat_listener, input);
4929 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004930
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004931 input->data_device =
4932 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004933 input->seat);
4934 wl_data_device_add_listener(input->data_device, &data_device_listener,
4935 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004936
4937 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004938
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004939 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4940 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004941 input->repeat_task.run = keyboard_repeat_func;
4942 display_watch_fd(d, input->repeat_timer_fd,
4943 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004944}
4945
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004946static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004947input_destroy(struct input *input)
4948{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004949 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004950 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004951
4952 if (input->drag_offer)
4953 data_offer_destroy(input->drag_offer);
4954
4955 if (input->selection_offer)
4956 data_offer_destroy(input->selection_offer);
4957
4958 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004959 fini_xkb(input);
4960
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004961 wl_surface_destroy(input->pointer_surface);
4962
Pekka Paalanene1207c72011-12-16 12:02:09 +02004963 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004964 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004965 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004966 free(input);
4967}
4968
4969static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004970init_workspace_manager(struct display *d, uint32_t id)
4971{
4972 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004973 wl_registry_bind(d->registry, id,
4974 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004975 if (d->workspace_manager != NULL)
4976 workspace_manager_add_listener(d->workspace_manager,
4977 &workspace_manager_listener,
4978 d);
4979}
4980
4981static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004982shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4983{
4984 struct display *d = data;
4985
4986 if (format == WL_SHM_FORMAT_RGB565)
4987 d->has_rgb565 = 1;
4988}
4989
4990struct wl_shm_listener shm_listener = {
4991 shm_format
4992};
4993
4994static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004995registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4996 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004997{
4998 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004999 struct global *global;
5000
Brian Lovinbc919262013-08-07 15:34:59 -07005001 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005002 global->name = id;
5003 global->interface = strdup(interface);
5004 global->version = version;
5005 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005006
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005007 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005008 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005009 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005010 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005011 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005012 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005013 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005014 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005015 d->shell = wl_registry_bind(registry,
5016 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005017 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005018 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005019 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005020 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5021 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005022 wl_registry_bind(registry, id,
5023 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005024 } else if (strcmp(interface, "text_cursor_position") == 0) {
5025 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005026 wl_registry_bind(registry, id,
5027 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005028 } else if (strcmp(interface, "workspace_manager") == 0) {
5029 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005030 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5031 d->subcompositor =
5032 wl_registry_bind(registry, id,
5033 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005034 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005035
5036 if (d->global_handler)
5037 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005038}
5039
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005040static void
5041registry_handle_global_remove(void *data, struct wl_registry *registry,
5042 uint32_t name)
5043{
5044 struct display *d = data;
5045 struct global *global;
5046 struct global *tmp;
5047
5048 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5049 if (global->name != name)
5050 continue;
5051
5052 /* XXX: Should destroy bound globals, and call
5053 * the counterpart of display::global_handler
5054 */
5055 wl_list_remove(&global->link);
5056 free(global->interface);
5057 free(global);
5058 }
5059}
5060
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005061void *
5062display_bind(struct display *display, uint32_t name,
5063 const struct wl_interface *interface, uint32_t version)
5064{
5065 return wl_registry_bind(display->registry, name, interface, version);
5066}
5067
5068static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005069 registry_handle_global,
5070 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005071};
5072
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005073#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005074static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005075init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005076{
5077 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005078 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005079
Rob Clark6396ed32012-03-11 19:48:41 -05005080#ifdef USE_CAIRO_GLESV2
5081# define GL_BIT EGL_OPENGL_ES2_BIT
5082#else
5083# define GL_BIT EGL_OPENGL_BIT
5084#endif
5085
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005086 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005087 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005088 EGL_RED_SIZE, 1,
5089 EGL_GREEN_SIZE, 1,
5090 EGL_BLUE_SIZE, 1,
5091 EGL_ALPHA_SIZE, 1,
5092 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005093 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005094 EGL_NONE
5095 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005096
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005097#ifdef USE_CAIRO_GLESV2
5098 static const EGLint context_attribs[] = {
5099 EGL_CONTEXT_CLIENT_VERSION, 2,
5100 EGL_NONE
5101 };
5102 EGLint api = EGL_OPENGL_ES_API;
5103#else
5104 EGLint *context_attribs = NULL;
5105 EGLint api = EGL_OPENGL_API;
5106#endif
5107
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005108 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005109 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005110 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005111 return -1;
5112 }
5113
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005114 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005115 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005116 return -1;
5117 }
5118
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005119 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5120 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005121 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005122 return -1;
5123 }
5124
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005125 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005126 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005127 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005128 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005129 return -1;
5130 }
5131
Benjamin Franzke0c991632011-09-27 21:57:31 +02005132 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5133 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005134 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005135 return -1;
5136 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005137
5138 return 0;
5139}
5140
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005141static void
5142fini_egl(struct display *display)
5143{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005144 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005145
5146 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5147 EGL_NO_CONTEXT);
5148
5149 eglTerminate(display->dpy);
5150 eglReleaseThread();
5151}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005152#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005153
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005154static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005155init_dummy_surface(struct display *display)
5156{
5157 int len;
5158 void *data;
5159
5160 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5161 data = malloc(len);
5162 display->dummy_surface =
5163 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5164 1, 1, len);
5165 display->dummy_surface_data = data;
5166}
5167
5168static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005169handle_display_data(struct task *task, uint32_t events)
5170{
5171 struct display *display =
5172 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005173 struct epoll_event ep;
5174 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005175
5176 display->display_fd_events = events;
5177
5178 if (events & EPOLLERR || events & EPOLLHUP) {
5179 display_exit(display);
5180 return;
5181 }
5182
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005183 if (events & EPOLLIN) {
5184 ret = wl_display_dispatch(display->display);
5185 if (ret == -1) {
5186 display_exit(display);
5187 return;
5188 }
5189 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005190
5191 if (events & EPOLLOUT) {
5192 ret = wl_display_flush(display->display);
5193 if (ret == 0) {
5194 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5195 ep.data.ptr = &display->display_task;
5196 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5197 display->display_fd, &ep);
5198 } else if (ret == -1 && errno != EAGAIN) {
5199 display_exit(display);
5200 return;
5201 }
5202 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005203}
5204
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005205static void
5206log_handler(const char *format, va_list args)
5207{
5208 vfprintf(stderr, format, args);
5209}
5210
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005211struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005212display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005213{
5214 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005215
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005216 wl_log_set_handler_client(log_handler);
5217
Peter Huttererf3d62272013-08-08 11:57:05 +10005218 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005219 if (d == NULL)
5220 return NULL;
5221
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005222 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005223 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005224 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005225 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005226 return NULL;
5227 }
5228
Rob Bradford5ab9c752013-07-26 16:29:43 +01005229 d->xkb_context = xkb_context_new(0);
5230 if (d->xkb_context == NULL) {
5231 fprintf(stderr, "Failed to create XKB context\n");
5232 free(d);
5233 return NULL;
5234 }
5235
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005236 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005237 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005238 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005239 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5240 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005241
5242 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005243 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005244 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005245 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005246
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005247 d->workspace = 0;
5248 d->workspace_count = 1;
5249
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005250 d->registry = wl_display_get_registry(d->display);
5251 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005252
5253 if (wl_display_dispatch(d->display) < 0) {
5254 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5255 return NULL;
5256 }
5257
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005258#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005259 if (init_egl(d) < 0)
5260 fprintf(stderr, "EGL does not seem to work, "
5261 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005262#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005263
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005264 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005265
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005266 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005267
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005268 wl_list_init(&d->window_list);
5269
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005270 init_dummy_surface(d);
5271
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005272 return d;
5273}
5274
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005275static void
5276display_destroy_outputs(struct display *display)
5277{
5278 struct output *tmp;
5279 struct output *output;
5280
5281 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5282 output_destroy(output);
5283}
5284
Pekka Paalanene1207c72011-12-16 12:02:09 +02005285static void
5286display_destroy_inputs(struct display *display)
5287{
5288 struct input *tmp;
5289 struct input *input;
5290
5291 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5292 input_destroy(input);
5293}
5294
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005295void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005296display_destroy(struct display *display)
5297{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005298 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005299 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5300 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005301
5302 if (!wl_list_empty(&display->deferred_list))
5303 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5304
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005305 cairo_surface_destroy(display->dummy_surface);
5306 free(display->dummy_surface_data);
5307
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005308 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005309 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005310
Daniel Stone97f68542012-05-30 16:32:01 +01005311 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005312
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005313 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005314 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005315
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005316#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005317 if (display->argb_device)
5318 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005319#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005320
Pekka Paalanen35e82632013-04-25 13:57:48 +03005321 if (display->subcompositor)
5322 wl_subcompositor_destroy(display->subcompositor);
5323
Pekka Paalanenc2052982011-12-16 11:41:32 +02005324 if (display->shell)
5325 wl_shell_destroy(display->shell);
5326
5327 if (display->shm)
5328 wl_shm_destroy(display->shm);
5329
5330 if (display->data_device_manager)
5331 wl_data_device_manager_destroy(display->data_device_manager);
5332
5333 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005334 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005335
5336 close(display->epoll_fd);
5337
U. Artie Eoff44874d92012-10-02 21:12:35 -07005338 if (!(display->display_fd_events & EPOLLERR) &&
5339 !(display->display_fd_events & EPOLLHUP))
5340 wl_display_flush(display->display);
5341
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005342 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005343 free(display);
5344}
5345
5346void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005347display_set_user_data(struct display *display, void *data)
5348{
5349 display->user_data = data;
5350}
5351
5352void *
5353display_get_user_data(struct display *display)
5354{
5355 return display->user_data;
5356}
5357
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005358struct wl_display *
5359display_get_display(struct display *display)
5360{
5361 return display->display;
5362}
5363
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005364int
5365display_has_subcompositor(struct display *display)
5366{
5367 if (display->subcompositor)
5368 return 1;
5369
5370 wl_display_roundtrip(display->display);
5371
5372 return display->subcompositor != NULL;
5373}
5374
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005375cairo_device_t *
5376display_get_cairo_device(struct display *display)
5377{
5378 return display->argb_device;
5379}
5380
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005381struct output *
5382display_get_output(struct display *display)
5383{
5384 return container_of(display->output_list.next, struct output, link);
5385}
5386
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005387struct wl_compositor *
5388display_get_compositor(struct display *display)
5389{
5390 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005391}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005392
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005393uint32_t
5394display_get_serial(struct display *display)
5395{
5396 return display->serial;
5397}
5398
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005399EGLDisplay
5400display_get_egl_display(struct display *d)
5401{
5402 return d->dpy;
5403}
5404
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005405struct wl_data_source *
5406display_create_data_source(struct display *display)
5407{
5408 return wl_data_device_manager_create_data_source(display->data_device_manager);
5409}
5410
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005411EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005412display_get_argb_egl_config(struct display *d)
5413{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005414 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005415}
5416
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005417struct wl_shell *
5418display_get_shell(struct display *display)
5419{
5420 return display->shell;
5421}
5422
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005423int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005424display_acquire_window_surface(struct display *display,
5425 struct window *window,
5426 EGLContext ctx)
5427{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005428 struct surface *surface = window->main_surface;
5429
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005430 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005431 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005432
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005433 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005434 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005435}
5436
5437void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005438display_release_window_surface(struct display *display,
5439 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005440{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005441 struct surface *surface = window->main_surface;
5442
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005443 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005444 return;
5445
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005446 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005447}
5448
5449void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005450display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005451{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005452 wl_list_insert(&display->deferred_list, &task->link);
5453}
5454
5455void
5456display_watch_fd(struct display *display,
5457 int fd, uint32_t events, struct task *task)
5458{
5459 struct epoll_event ep;
5460
5461 ep.events = events;
5462 ep.data.ptr = task;
5463 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5464}
5465
5466void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005467display_unwatch_fd(struct display *display, int fd)
5468{
5469 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5470}
5471
5472void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005473display_run(struct display *display)
5474{
5475 struct task *task;
5476 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005477 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005478
Pekka Paalanen826d7952011-12-15 10:14:07 +02005479 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005480 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005481 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005482 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005483 struct task, link);
5484 wl_list_remove(&task->link);
5485 task->run(task, 0);
5486 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005487
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005488 wl_display_dispatch_pending(display->display);
5489
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005490 if (!display->running)
5491 break;
5492
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005493 ret = wl_display_flush(display->display);
5494 if (ret < 0 && errno == EAGAIN) {
5495 ep[0].events =
5496 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5497 ep[0].data.ptr = &display->display_task;
5498
5499 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5500 display->display_fd, &ep[0]);
5501 } else if (ret < 0) {
5502 break;
5503 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005504
5505 count = epoll_wait(display->epoll_fd,
5506 ep, ARRAY_LENGTH(ep), -1);
5507 for (i = 0; i < count; i++) {
5508 task = ep[i].data.ptr;
5509 task->run(task, ep[i].events);
5510 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005511 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005512}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005513
5514void
5515display_exit(struct display *display)
5516{
5517 display->running = 0;
5518}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005519
5520void
5521keysym_modifiers_add(struct wl_array *modifiers_map,
5522 const char *name)
5523{
5524 size_t len = strlen(name) + 1;
5525 char *p;
5526
5527 p = wl_array_add(modifiers_map, len);
5528
5529 if (p == NULL)
5530 return;
5531
5532 strncpy(p, name, len);
5533}
5534
5535static xkb_mod_index_t
5536keysym_modifiers_get_index(struct wl_array *modifiers_map,
5537 const char *name)
5538{
5539 xkb_mod_index_t index = 0;
5540 char *p = modifiers_map->data;
5541
5542 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5543 if (strcmp(p, name) == 0)
5544 return index;
5545
5546 index++;
5547 p += strlen(p) + 1;
5548 }
5549
5550 return XKB_MOD_INVALID;
5551}
5552
5553xkb_mod_mask_t
5554keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5555 const char *name)
5556{
5557 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5558
5559 if (index == XKB_MOD_INVALID)
5560 return XKB_MOD_INVALID;
5561
5562 return 1 << index;
5563}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005564
5565void *
5566fail_on_null(void *p)
5567{
5568 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005569 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005570 exit(EXIT_FAILURE);
5571 }
5572
5573 return p;
5574}
5575
5576void *
5577xmalloc(size_t s)
5578{
5579 return fail_on_null(malloc(s));
5580}
5581
Peter Huttererf3d62272013-08-08 11:57:05 +10005582void *
5583xzalloc(size_t s)
5584{
5585 return fail_on_null(zalloc(s));
5586}
5587
Kristian Høgsbergce278412013-07-25 15:20:20 -07005588char *
5589xstrdup(const char *s)
5590{
5591 return fail_on_null(strdup(s));
5592}