blob: c13d2070bad77599db4d60a0037d3d8ca1d3dcae [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;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500131};
132
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400133enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400134 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400136 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500137 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500139 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_CUSTOM
141};
Rob Bradford7507b572012-05-15 17:55:34 +0100142
143struct window_output {
144 struct output *output;
145 struct wl_list link;
146};
147
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200148struct toysurface {
149 /*
150 * Prepare the surface for drawing. Makes sure there is a surface
151 * of the right size available for rendering, and returns it.
152 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200153 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200154 * If flags has SURFACE_HINT_RESIZE set, the user is
155 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156 * Returns the Cairo surface to draw to.
157 */
158 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200159 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200160 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161
162 /*
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
166 */
167 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200168 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200169 struct rectangle *server_allocation);
170
171 /*
172 * Make the toysurface current with the given EGL context.
173 * Returns 0 on success, and negative of failure.
174 */
175 int (*acquire)(struct toysurface *base, EGLContext ctx);
176
177 /*
178 * Release the toysurface from the EGL context, returning control
179 * to Cairo.
180 */
181 void (*release)(struct toysurface *base);
182
183 /*
184 * Destroy the toysurface, including the Cairo surface, any
185 * backing storage, and the Wayland protocol objects.
186 */
187 void (*destroy)(struct toysurface *base);
188};
189
Pekka Paalanen4e373742013-02-13 16:17:13 +0200190struct surface {
191 struct window *window;
192
193 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300194 struct wl_subsurface *subsurface;
195 int synchronized;
196 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200198 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300199 int redraw_needed;
200 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300201 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200202
203 struct rectangle allocation;
204 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200205
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200206 struct wl_region *input_region;
207 struct wl_region *opaque_region;
208
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209 enum window_buffer_type buffer_type;
210 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200211 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200212
213 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300214
215 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200216};
217
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500218struct window {
219 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500220 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100221 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500222 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200223 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400224 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500225 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500226 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400227 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400228 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300229 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400230 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400231 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100232 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400233 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400234 int focus_count;
235
Pekka Paalanen99436862012-11-19 17:15:59 +0200236 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200237 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000238 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400239
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500240 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500241 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400242 window_data_handler_t data_handler;
243 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500244 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400245 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200246 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400247
Pekka Paalanen4e373742013-02-13 16:17:13 +0200248 struct surface *main_surface;
249 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300250
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200251 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500252
Pekka Paalanen35e82632013-04-25 13:57:48 +0300253 /* struct surface::link, contains also main_surface */
254 struct wl_list subsurface_list;
255
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500256 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400257 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500258};
259
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500260struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500261 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200262 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300263 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500264 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400265 struct wl_list link;
266 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500267 widget_resize_handler_t resize_handler;
268 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500269 widget_enter_handler_t enter_handler;
270 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500271 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500272 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200273 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400274 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500275 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300276 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500277 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400278};
279
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400280struct input {
281 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100282 struct wl_seat *seat;
283 struct wl_pointer *pointer;
284 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400285 struct window *pointer_focus;
286 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300287 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300288 uint32_t cursor_anim_start;
289 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300290 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400291 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400292 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400293 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400294 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400295 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400296
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500297 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500298 struct widget *grab;
299 uint32_t grab_button;
300
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400301 struct wl_data_device *data_device;
302 struct data_offer *drag_offer;
303 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100304
305 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100306 struct xkb_keymap *keymap;
307 struct xkb_state *state;
308 xkb_mod_mask_t control_mask;
309 xkb_mod_mask_t alt_mask;
310 xkb_mod_mask_t shift_mask;
311 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400312
313 struct task repeat_task;
314 int repeat_timer_fd;
315 uint32_t repeat_sym;
316 uint32_t repeat_key;
317 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400318};
319
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500320struct output {
321 struct display *display;
322 struct wl_output *output;
323 struct rectangle allocation;
324 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600325 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200326 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200327
328 display_output_handler_t destroy_handler;
329 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500330};
331
Martin Minarik1998b152012-05-10 02:04:35 +0200332enum frame_button_action {
333 FRAME_BUTTON_NULL = 0,
334 FRAME_BUTTON_ICON = 1,
335 FRAME_BUTTON_CLOSE = 2,
336 FRAME_BUTTON_MINIMIZE = 3,
337 FRAME_BUTTON_MAXIMIZE = 4,
338};
339
340enum frame_button_pointer {
341 FRAME_BUTTON_DEFAULT = 0,
342 FRAME_BUTTON_OVER = 1,
343 FRAME_BUTTON_ACTIVE = 2,
344};
345
346enum frame_button_align {
347 FRAME_BUTTON_RIGHT = 0,
348 FRAME_BUTTON_LEFT = 1,
349};
350
351enum frame_button_decoration {
352 FRAME_BUTTON_NONE = 0,
353 FRAME_BUTTON_FANCY = 1,
354};
355
356struct frame_button {
357 struct widget *widget;
358 struct frame *frame;
359 cairo_surface_t *icon;
360 enum frame_button_action type;
361 enum frame_button_pointer state;
362 struct wl_list link; /* buttons_list */
363 enum frame_button_align align;
364 enum frame_button_decoration decoration;
365};
366
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500367struct frame {
368 struct widget *widget;
369 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200370 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500371};
372
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500373struct menu {
374 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500375 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500376 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500377 const char **entries;
378 uint32_t time;
379 int current;
380 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400381 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500382 menu_func_t func;
383};
384
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300385struct tooltip {
386 struct widget *parent;
387 struct window *window;
388 struct widget *widget;
389 char *entry;
390 struct task tooltip_task;
391 int tooltip_fd;
392 float x, y;
393};
394
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700395struct shm_pool {
396 struct wl_shm_pool *pool;
397 size_t size;
398 size_t used;
399 void *data;
400};
401
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400402enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300403 CURSOR_DEFAULT = 100,
404 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400405};
406
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500407enum window_location {
408 WINDOW_INTERIOR = 0,
409 WINDOW_RESIZING_TOP = 1,
410 WINDOW_RESIZING_BOTTOM = 2,
411 WINDOW_RESIZING_LEFT = 4,
412 WINDOW_RESIZING_TOP_LEFT = 5,
413 WINDOW_RESIZING_BOTTOM_LEFT = 6,
414 WINDOW_RESIZING_RIGHT = 8,
415 WINDOW_RESIZING_TOP_RIGHT = 9,
416 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
417 WINDOW_RESIZING_MASK = 15,
418 WINDOW_EXTERIOR = 16,
419 WINDOW_TITLEBAR = 17,
420 WINDOW_CLIENT_AREA = 18,
421};
422
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200423static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424
Pekka Paalanen97777442013-05-22 10:20:05 +0300425/* #define DEBUG */
426
427#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300428
429static void
430debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
431__attribute__ ((format (printf, 4, 5)));
432
433static void
434debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
435{
436 va_list ap;
437 struct timeval tv;
438
439 gettimeofday(&tv, NULL);
440 fprintf(stderr, "%8ld.%03ld ",
441 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
442
443 if (proxy)
444 fprintf(stderr, "%s@%d ",
445 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
446
447 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
448 fprintf(stderr, "%s ", func);
449
450 va_start(ap, fmt);
451 vfprintf(stderr, fmt, ap);
452 va_end(ap);
453}
454
455#define DBG(fmt, ...) \
456 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
457
458#define DBG_OBJ(obj, fmt, ...) \
459 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
460
461#else
462
463#define DBG(...) do {} while (0)
464#define DBG_OBJ(...) do {} while (0)
465
466#endif
467
Alexander Larsson1818e312013-05-22 14:41:31 +0200468static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200469surface_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 +0200470{
471 int32_t tmp;
472
473 switch (buffer_transform) {
474 case WL_OUTPUT_TRANSFORM_90:
475 case WL_OUTPUT_TRANSFORM_270:
476 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
477 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
478 tmp = *width;
479 *width = *height;
480 *height = tmp;
481 break;
482 default:
483 break;
484 }
485
486 *width *= buffer_scale;
487 *height *= buffer_scale;
488}
489
490static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200491buffer_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 +0200492{
493 int32_t tmp;
494
495 switch (buffer_transform) {
496 case WL_OUTPUT_TRANSFORM_90:
497 case WL_OUTPUT_TRANSFORM_270:
498 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
499 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
500 tmp = *width;
501 *width = *height;
502 *height = tmp;
503 break;
504 default:
505 break;
506 }
507
508 *width /= buffer_scale;
509 *height /= buffer_scale;
510}
511
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500512#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400513
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200514struct egl_window_surface {
515 struct toysurface base;
516 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100517 struct display *display;
518 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519 struct wl_egl_window *egl_window;
520 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521};
522
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523static struct egl_window_surface *
524to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100527}
528
529static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200531 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200532 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100533{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534 struct egl_window_surface *surface = to_egl_window_surface(base);
535
Alexander Larsson1818e312013-05-22 14:41:31 +0200536 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
537
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200538 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
539 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
540
541 return cairo_surface_reference(surface->cairo_surface);
542}
543
544static void
545egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200546 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200547 struct rectangle *server_allocation)
548{
549 struct egl_window_surface *surface = to_egl_window_surface(base);
550
551 cairo_gl_surface_swapbuffers(surface->cairo_surface);
552 wl_egl_window_get_attached_size(surface->egl_window,
553 &server_allocation->width,
554 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200555
556 buffer_to_surface_size (buffer_transform, buffer_scale,
557 &server_allocation->width,
558 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559}
560
561static int
562egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
563{
564 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200565 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100566
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200567 device = cairo_surface_get_device(surface->cairo_surface);
568 if (!device)
569 return -1;
570
571 if (!ctx) {
572 if (device == surface->display->argb_device)
573 ctx = surface->display->argb_ctx;
574 else
575 assert(0);
576 }
577
578 cairo_device_flush(device);
579 cairo_device_acquire(device);
580 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
581 surface->egl_surface, ctx))
582 fprintf(stderr, "failed to make surface current\n");
583
584 return 0;
585}
586
587static void
588egl_window_surface_release(struct toysurface *base)
589{
590 struct egl_window_surface *surface = to_egl_window_surface(base);
591 cairo_device_t *device;
592
593 device = cairo_surface_get_device(surface->cairo_surface);
594 if (!device)
595 return;
596
597 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
598 surface->display->argb_ctx))
599 fprintf(stderr, "failed to make context current\n");
600
601 cairo_device_release(device);
602}
603
604static void
605egl_window_surface_destroy(struct toysurface *base)
606{
607 struct egl_window_surface *surface = to_egl_window_surface(base);
608 struct display *d = surface->display;
609
610 cairo_surface_destroy(surface->cairo_surface);
611 eglDestroySurface(d->dpy, surface->egl_surface);
612 wl_egl_window_destroy(surface->egl_window);
613 surface->surface = NULL;
614
615 free(surface);
616}
617
618static struct toysurface *
619egl_window_surface_create(struct display *display,
620 struct wl_surface *wl_surface,
621 uint32_t flags,
622 struct rectangle *rectangle)
623{
624 struct egl_window_surface *surface;
625
Pekka Paalanenb3627362012-11-19 17:16:00 +0200626 if (display->dpy == EGL_NO_DISPLAY)
627 return NULL;
628
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200629 surface = calloc(1, sizeof *surface);
630 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 return NULL;
632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 surface->base.prepare = egl_window_surface_prepare;
634 surface->base.swap = egl_window_surface_swap;
635 surface->base.acquire = egl_window_surface_acquire;
636 surface->base.release = egl_window_surface_release;
637 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200639 surface->display = display;
640 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400641
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200642 surface->egl_window = wl_egl_window_create(surface->surface,
643 rectangle->width,
644 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100645
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200646 surface->egl_surface = eglCreateWindowSurface(display->dpy,
647 display->argb_config,
648 surface->egl_window,
649 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200651 surface->cairo_surface =
652 cairo_gl_surface_create_for_egl(display->argb_device,
653 surface->egl_surface,
654 rectangle->width,
655 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100656
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200657 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100658}
659
Pekka Paalanenb3627362012-11-19 17:16:00 +0200660#else
661
662static struct toysurface *
663egl_window_surface_create(struct display *display,
664 struct wl_surface *wl_surface,
665 uint32_t flags,
666 struct rectangle *rectangle)
667{
668 return NULL;
669}
670
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400671#endif
672
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200673struct shm_surface_data {
674 struct wl_buffer *buffer;
675 struct shm_pool *pool;
676};
677
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400678struct wl_buffer *
679display_get_buffer_for_surface(struct display *display,
680 cairo_surface_t *surface)
681{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200682 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400683
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200684 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400685
686 return data->buffer;
687}
688
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500689static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700690shm_pool_destroy(struct shm_pool *pool);
691
692static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400693shm_surface_data_destroy(void *p)
694{
695 struct shm_surface_data *data = p;
696
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200697 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700698 if (data->pool)
699 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300700
701 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702}
703
Kristian Høgsberg16626282012-04-03 11:21:27 -0400704static struct wl_shm_pool *
705make_shm_pool(struct display *display, int size, void **data)
706{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400707 struct wl_shm_pool *pool;
708 int fd;
709
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300710 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400711 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300712 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
713 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400714 return NULL;
715 }
716
717 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400718 if (*data == MAP_FAILED) {
719 fprintf(stderr, "mmap failed: %m\n");
720 close(fd);
721 return NULL;
722 }
723
724 pool = wl_shm_create_pool(display->shm, fd, size);
725
726 close(fd);
727
728 return pool;
729}
730
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731static struct shm_pool *
732shm_pool_create(struct display *display, size_t size)
733{
734 struct shm_pool *pool = malloc(sizeof *pool);
735
736 if (!pool)
737 return NULL;
738
739 pool->pool = make_shm_pool(display, size, &pool->data);
740 if (!pool->pool) {
741 free(pool);
742 return NULL;
743 }
744
745 pool->size = size;
746 pool->used = 0;
747
748 return pool;
749}
750
751static void *
752shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
753{
754 if (pool->used + size > pool->size)
755 return NULL;
756
757 *offset = pool->used;
758 pool->used += size;
759
760 return (char *) pool->data + *offset;
761}
762
763/* destroy the pool. this does not unmap the memory though */
764static void
765shm_pool_destroy(struct shm_pool *pool)
766{
767 munmap(pool->data, pool->size);
768 wl_shm_pool_destroy(pool->pool);
769 free(pool);
770}
771
772/* Start allocating from the beginning of the pool again */
773static void
774shm_pool_reset(struct shm_pool *pool)
775{
776 pool->used = 0;
777}
778
779static int
780data_length_for_shm_surface(struct rectangle *rect)
781{
782 int stride;
783
784 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
785 rect->width);
786 return stride * rect->height;
787}
788
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500789static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700790display_create_shm_surface_from_pool(struct display *display,
791 struct rectangle *rectangle,
792 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400793{
794 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400795 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400796 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700797 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400798 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400799
800 data = malloc(sizeof *data);
801 if (data == NULL)
802 return NULL;
803
804 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
805 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700806 length = stride * rectangle->height;
807 data->pool = NULL;
808 map = shm_pool_allocate(pool, length, &offset);
809
810 if (!map) {
811 free(data);
812 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400813 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400814
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400815 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400816 CAIRO_FORMAT_ARGB32,
817 rectangle->width,
818 rectangle->height,
819 stride);
820
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200821 cairo_surface_set_user_data(surface, &shm_surface_data_key,
822 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400824 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500825 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400826 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500827 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400828
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200829 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
830 rectangle->width,
831 rectangle->height,
832 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700834 return surface;
835}
836
837static cairo_surface_t *
838display_create_shm_surface(struct display *display,
839 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200840 struct shm_pool *alternate_pool,
841 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700842{
843 struct shm_surface_data *data;
844 struct shm_pool *pool;
845 cairo_surface_t *surface;
846
Pekka Paalanen99436862012-11-19 17:15:59 +0200847 if (alternate_pool) {
848 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700849 surface = display_create_shm_surface_from_pool(display,
850 rectangle,
851 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200852 alternate_pool);
853 if (surface) {
854 data = cairo_surface_get_user_data(surface,
855 &shm_surface_data_key);
856 goto out;
857 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700858 }
859
860 pool = shm_pool_create(display,
861 data_length_for_shm_surface(rectangle));
862 if (!pool)
863 return NULL;
864
865 surface =
866 display_create_shm_surface_from_pool(display, rectangle,
867 flags, pool);
868
869 if (!surface) {
870 shm_pool_destroy(pool);
871 return NULL;
872 }
873
874 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200875 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700876 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400877
Pekka Paalanen99436862012-11-19 17:15:59 +0200878out:
879 if (data_ret)
880 *data_ret = data;
881
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400882 return surface;
883}
884
nobled7b87cb02011-02-01 18:51:47 +0000885static int
886check_size(struct rectangle *rect)
887{
888 if (rect->width && rect->height)
889 return 0;
890
891 fprintf(stderr, "tried to create surface of "
892 "width: %d, height: %d\n", rect->width, rect->height);
893 return -1;
894}
895
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400896cairo_surface_t *
897display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400899 struct rectangle *rectangle,
900 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400901{
nobled7b87cb02011-02-01 18:51:47 +0000902 if (check_size(rectangle) < 0)
903 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200904
905 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200906 return display_create_shm_surface(display, rectangle, flags,
907 NULL, NULL);
908}
909
Pekka Paalanena4eda732012-11-19 17:16:02 +0200910struct shm_surface_leaf {
911 cairo_surface_t *cairo_surface;
912 /* 'data' is automatically destroyed, when 'cairo_surface' is */
913 struct shm_surface_data *data;
914
915 struct shm_pool *resize_pool;
916 int busy;
917};
918
919static void
920shm_surface_leaf_release(struct shm_surface_leaf *leaf)
921{
922 if (leaf->cairo_surface)
923 cairo_surface_destroy(leaf->cairo_surface);
924 /* leaf->data already destroyed via cairo private */
925
926 if (leaf->resize_pool)
927 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200928
929 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200930}
931
Pekka Paalanenaef02542013-04-25 13:57:47 +0300932#define MAX_LEAVES 3
933
Pekka Paalanen99436862012-11-19 17:15:59 +0200934struct shm_surface {
935 struct toysurface base;
936 struct display *display;
937 struct wl_surface *surface;
938 uint32_t flags;
939 int dx, dy;
940
Pekka Paalanenaef02542013-04-25 13:57:47 +0300941 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200943};
944
945static struct shm_surface *
946to_shm_surface(struct toysurface *base)
947{
948 return container_of(base, struct shm_surface, base);
949}
950
Pekka Paalanena4eda732012-11-19 17:16:02 +0200951static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300952shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
953{
954#ifdef DEBUG
955 struct shm_surface_leaf *leaf;
956 char bufs[MAX_LEAVES + 1];
957 int i;
958
959 for (i = 0; i < MAX_LEAVES; i++) {
960 leaf = &surface->leaf[i];
961
962 if (leaf->busy)
963 bufs[i] = 'b';
964 else if (leaf->cairo_surface)
965 bufs[i] = 'a';
966 else
967 bufs[i] = ' ';
968 }
969
970 bufs[MAX_LEAVES] = '\0';
971 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
972#endif
973}
974
975static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200976shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
977{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200978 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300979 struct shm_surface_leaf *leaf;
980 int i;
981 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300982
983 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200984
Pekka Paalanenaef02542013-04-25 13:57:47 +0300985 for (i = 0; i < MAX_LEAVES; i++) {
986 leaf = &surface->leaf[i];
987 if (leaf->data && leaf->data->buffer == buffer) {
988 leaf->busy = 0;
989 break;
990 }
991 }
992 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200993
Pekka Paalanenaef02542013-04-25 13:57:47 +0300994 /* Leave one free leaf with storage, release others */
995 free_found = 0;
996 for (i = 0; i < MAX_LEAVES; i++) {
997 leaf = &surface->leaf[i];
998
999 if (!leaf->cairo_surface || leaf->busy)
1000 continue;
1001
1002 if (!free_found)
1003 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001004 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001005 shm_surface_leaf_release(leaf);
1006 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001007
Pekka Paalanen97777442013-05-22 10:20:05 +03001008 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001009}
1010
1011static const struct wl_buffer_listener shm_surface_buffer_listener = {
1012 shm_surface_buffer_release
1013};
1014
Pekka Paalanen99436862012-11-19 17:15:59 +02001015static cairo_surface_t *
1016shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001017 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001018 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001019{
Pekka Paalanenec076692012-11-30 13:37:27 +02001020 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001021 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001022 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001023 struct shm_surface_leaf *leaf = NULL;
1024 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001025
1026 surface->dx = dx;
1027 surface->dy = dy;
1028
Pekka Paalanenaef02542013-04-25 13:57:47 +03001029 /* pick a free buffer, preferrably one that already has storage */
1030 for (i = 0; i < MAX_LEAVES; i++) {
1031 if (surface->leaf[i].busy)
1032 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001033
Pekka Paalanenaef02542013-04-25 13:57:47 +03001034 if (!leaf || surface->leaf[i].cairo_surface)
1035 leaf = &surface->leaf[i];
1036 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001037 DBG_OBJ(surface->surface, "pick leaf %d\n",
1038 (int)(leaf - &surface->leaf[0]));
1039
Pekka Paalanenaef02542013-04-25 13:57:47 +03001040 if (!leaf) {
1041 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001042 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001043 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001045 }
1046
Pekka Paalanena4eda732012-11-19 17:16:02 +02001047 if (!resize_hint && leaf->resize_pool) {
1048 cairo_surface_destroy(leaf->cairo_surface);
1049 leaf->cairo_surface = NULL;
1050 shm_pool_destroy(leaf->resize_pool);
1051 leaf->resize_pool = NULL;
1052 }
1053
Alexander Larsson1818e312013-05-22 14:41:31 +02001054 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1055
Pekka Paalanena4eda732012-11-19 17:16:02 +02001056 if (leaf->cairo_surface &&
1057 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1058 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001059 goto out;
1060
Pekka Paalanena4eda732012-11-19 17:16:02 +02001061 if (leaf->cairo_surface)
1062 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001063
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001064#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001065 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001066 /* Create a big pool to allocate from, while continuously
1067 * resizing. Mmapping a new pool in the server
1068 * is relatively expensive, so reusing a pool performs
1069 * better, but may temporarily reserve unneeded memory.
1070 */
1071 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001072 leaf->resize_pool = shm_pool_create(surface->display,
1073 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001074 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001075#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
Alexander Larsson1818e312013-05-22 14:41:31 +02001077 rect.width = width;
1078 rect.height = height;
1079
Pekka Paalanena4eda732012-11-19 17:16:02 +02001080 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001081 display_create_shm_surface(surface->display, &rect,
1082 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001083 leaf->resize_pool,
1084 &leaf->data);
1085 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001086 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001087
1088out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001089 surface->current = leaf;
1090
1091 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001092}
1093
1094static void
1095shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001096 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001097 struct rectangle *server_allocation)
1098{
1099 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001101
1102 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001103 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001104 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001106
Alexander Larsson1818e312013-05-22 14:41:31 +02001107 buffer_to_surface_size (buffer_transform, buffer_scale,
1108 &server_allocation->width,
1109 &server_allocation->height);
1110
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001112 surface->dx, surface->dy);
1113 wl_surface_damage(surface->surface, 0, 0,
1114 server_allocation->width, server_allocation->height);
1115 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116
Pekka Paalanen71233882013-04-25 13:57:53 +03001117 DBG_OBJ(surface->surface, "leaf %d busy\n",
1118 (int)(leaf - &surface->leaf[0]));
1119
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 leaf->busy = 1;
1121 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001122}
1123
1124static int
1125shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1126{
1127 return -1;
1128}
1129
1130static void
1131shm_surface_release(struct toysurface *base)
1132{
1133}
1134
1135static void
1136shm_surface_destroy(struct toysurface *base)
1137{
1138 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001139 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001140
Pekka Paalanenaef02542013-04-25 13:57:47 +03001141 for (i = 0; i < MAX_LEAVES; i++)
1142 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001143
1144 free(surface);
1145}
1146
1147static struct toysurface *
1148shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1149 uint32_t flags, struct rectangle *rectangle)
1150{
1151 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001152 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001153
1154 surface = calloc(1, sizeof *surface);
1155 if (!surface)
1156 return NULL;
1157
1158 surface->base.prepare = shm_surface_prepare;
1159 surface->base.swap = shm_surface_swap;
1160 surface->base.acquire = shm_surface_acquire;
1161 surface->base.release = shm_surface_release;
1162 surface->base.destroy = shm_surface_destroy;
1163
1164 surface->display = display;
1165 surface->surface = wl_surface;
1166 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001167
1168 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001169}
1170
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001171/*
1172 * The following correspondences between file names and cursors was copied
1173 * from: https://bugs.kde.org/attachment.cgi?id=67313
1174 */
1175
1176static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001177 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001178 "sw-resize",
1179 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001180};
1181
1182static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001183 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001184 "se-resize",
1185 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001186};
1187
1188static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001189 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001190 "s-resize",
1191 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001192};
1193
1194static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001195 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001196 "closedhand",
1197 "208530c400c041818281048008011002"
1198};
1199
1200static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001201 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001202 "default",
1203 "top_left_arrow",
1204 "left-arrow"
1205};
1206
1207static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001209 "w-resize",
1210 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001211};
1212
1213static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001214 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001215 "e-resize",
1216 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001217};
1218
1219static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001220 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001221 "nw-resize",
1222 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001223};
1224
1225static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001226 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001227 "ne-resize",
1228 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001229};
1230
1231static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001233 "n-resize",
1234 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235};
1236
1237static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001238 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001239 "ibeam",
1240 "text"
1241};
1242
1243static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001244 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001245 "pointer",
1246 "pointing_hand",
1247 "e29285e634086352946a0e7090d73106"
1248};
1249
1250static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001251 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252 "wait",
1253 "0426c94ea35c87780ff01dc239897213"
1254};
1255
1256struct cursor_alternatives {
1257 const char **names;
1258 size_t count;
1259};
1260
1261static const struct cursor_alternatives cursors[] = {
1262 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1263 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1264 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1265 {grabbings, ARRAY_LENGTH(grabbings)},
1266 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1267 {left_sides, ARRAY_LENGTH(left_sides)},
1268 {right_sides, ARRAY_LENGTH(right_sides)},
1269 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1270 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1271 {top_sides, ARRAY_LENGTH(top_sides)},
1272 {xterms, ARRAY_LENGTH(xterms)},
1273 {hand1s, ARRAY_LENGTH(hand1s)},
1274 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001275};
1276
1277static void
1278create_cursors(struct display *display)
1279{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001280 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001281 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001282 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001283 unsigned int i, j;
1284 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001285 struct config_key shell_keys[] = {
1286 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001287 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001288 };
1289 struct config_section cs[] = {
1290 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1291 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001292
Ossama Othmana50e6e42013-05-14 09:48:26 -07001293 config_fd = open_config_file("weston.ini");
1294 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1295 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001296
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001297 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001298 display->cursors =
1299 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1300
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001301 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001302 cursor = NULL;
1303 for (j = 0; !cursor && j < cursors[i].count; ++j)
1304 cursor = wl_cursor_theme_get_cursor(
1305 display->cursor_theme, cursors[i].names[j]);
1306
1307 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001308 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001309 cursors[i].names[0]);
1310
1311 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001312 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001313}
1314
1315static void
1316destroy_cursors(struct display *display)
1317{
1318 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001319 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001320}
1321
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001322struct wl_cursor_image *
1323display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001324{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001325 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001326
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001327 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001328}
1329
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001330static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001331surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001332{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001333 if (!surface->cairo_surface)
1334 return;
1335
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001336 if (surface->opaque_region) {
1337 wl_surface_set_opaque_region(surface->surface,
1338 surface->opaque_region);
1339 wl_region_destroy(surface->opaque_region);
1340 surface->opaque_region = NULL;
1341 }
1342
1343 if (surface->input_region) {
1344 wl_surface_set_input_region(surface->surface,
1345 surface->input_region);
1346 wl_region_destroy(surface->input_region);
1347 surface->input_region = NULL;
1348 }
1349
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001350 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001351 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001353
Pekka Paalanen89dee002013-02-13 16:17:20 +02001354 cairo_surface_destroy(surface->cairo_surface);
1355 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001356}
1357
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001358int
1359window_has_focus(struct window *window)
1360{
1361 return window->focus_count > 0;
1362}
1363
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001364static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001365window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001366{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001367 struct surface *surface;
1368
Pekka Paalanen89dee002013-02-13 16:17:20 +02001369 if (window->type == TYPE_NONE) {
1370 window->type = TYPE_TOPLEVEL;
1371 if (window->shell_surface)
1372 wl_shell_surface_set_toplevel(window->shell_surface);
1373 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001374
Pekka Paalanen35e82632013-04-25 13:57:48 +03001375 wl_list_for_each(surface, &window->subsurface_list, link) {
1376 if (surface == window->main_surface)
1377 continue;
1378
1379 surface_flush(surface);
1380 }
1381
Pekka Paalanen89dee002013-02-13 16:17:20 +02001382 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001383}
1384
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001385struct display *
1386window_get_display(struct window *window)
1387{
1388 return window->display;
1389}
1390
Pekka Paalanen89dee002013-02-13 16:17:20 +02001391static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001392surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001393{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001394 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001395 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001396
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001397 if (!surface->toysurface && display->dpy &&
1398 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001399 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001400 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001401 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001402 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001403 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001404 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001405
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001406 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001408 surface->surface,
1409 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001410
Pekka Paalanen89dee002013-02-13 16:17:20 +02001411 surface->cairo_surface = surface->toysurface->prepare(
1412 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001413 allocation.width, allocation.height, flags,
1414 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001415}
1416
1417static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001418window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001419{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001420 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001421 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001422 int dx = 0;
1423 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424
Pekka Paalanenec076692012-11-30 13:37:27 +02001425 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001426 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001427
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001428 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1429 dx = surface->server_allocation.width -
1430 surface->allocation.width;
1431
1432 if (window->resize_edges & WINDOW_RESIZING_TOP)
1433 dy = surface->server_allocation.height -
1434 surface->allocation.height;
1435
1436 window->resize_edges = 0;
1437
Pekka Paalanen89dee002013-02-13 16:17:20 +02001438 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001439}
1440
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001441int
1442window_get_buffer_transform(struct window *window)
1443{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001444 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001445}
1446
1447void
1448window_set_buffer_transform(struct window *window,
1449 enum wl_output_transform transform)
1450{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001451 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001452 wl_surface_set_buffer_transform(window->main_surface->surface,
1453 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001454}
1455
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001456void
1457window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001458 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001459{
1460 window->main_surface->buffer_scale = scale;
1461 wl_surface_set_buffer_scale(window->main_surface->surface,
1462 scale);
1463}
1464
1465uint32_t
1466window_get_buffer_scale(struct window *window)
1467{
1468 return window->main_surface->buffer_scale;
1469}
1470
Alexander Larssond68f5232013-05-22 14:41:33 +02001471uint32_t
1472window_get_output_scale(struct window *window)
1473{
1474 struct window_output *window_output;
1475 struct window_output *window_output_tmp;
1476 int scale = 1;
1477
1478 wl_list_for_each_safe(window_output, window_output_tmp,
1479 &window->window_output_list, link) {
1480 if (window_output->output->scale > scale)
1481 scale = window_output->output->scale;
1482 }
1483
1484 return scale;
1485}
1486
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001487static void frame_destroy(struct frame *frame);
1488
Pekka Paalanen4e373742013-02-13 16:17:13 +02001489static void
1490surface_destroy(struct surface *surface)
1491{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001492 if (surface->frame_cb)
1493 wl_callback_destroy(surface->frame_cb);
1494
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001495 if (surface->input_region)
1496 wl_region_destroy(surface->input_region);
1497
1498 if (surface->opaque_region)
1499 wl_region_destroy(surface->opaque_region);
1500
Pekka Paalanen35e82632013-04-25 13:57:48 +03001501 if (surface->subsurface)
1502 wl_subsurface_destroy(surface->subsurface);
1503
Pekka Paalanen4e373742013-02-13 16:17:13 +02001504 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001505
1506 if (surface->toysurface)
1507 surface->toysurface->destroy(surface->toysurface);
1508
Pekka Paalanen35e82632013-04-25 13:57:48 +03001509 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001510 free(surface);
1511}
1512
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001513void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001514window_destroy(struct window *window)
1515{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001516 struct display *display = window->display;
1517 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001518 struct window_output *window_output;
1519 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001520
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001521 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001522
1523 wl_list_for_each(input, &display->input_list, link) {
1524 if (input->pointer_focus == window)
1525 input->pointer_focus = NULL;
1526 if (input->keyboard_focus == window)
1527 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001528 if (input->focus_widget &&
1529 input->focus_widget->window == window)
1530 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001531 }
1532
Rob Bradford7507b572012-05-15 17:55:34 +01001533 wl_list_for_each_safe(window_output, window_output_tmp,
1534 &window->window_output_list, link) {
1535 free (window_output);
1536 }
1537
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001538 if (window->frame)
1539 frame_destroy(window->frame);
1540
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001541 if (window->shell_surface)
1542 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001543
1544 surface_destroy(window->main_surface);
1545
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001546 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001547
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001548 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001549 free(window);
1550}
1551
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001552static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001553widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001554{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001555 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001556
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001557 wl_list_for_each(child, &widget->child_list, link) {
1558 target = widget_find_widget(child, x, y);
1559 if (target)
1560 return target;
1561 }
1562
1563 if (widget->allocation.x <= x &&
1564 x < widget->allocation.x + widget->allocation.width &&
1565 widget->allocation.y <= y &&
1566 y < widget->allocation.y + widget->allocation.height) {
1567 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568 }
1569
1570 return NULL;
1571}
1572
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001573static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001574window_find_widget(struct window *window, int32_t x, int32_t y)
1575{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001576 struct surface *surface;
1577 struct widget *widget;
1578
1579 wl_list_for_each(surface, &window->subsurface_list, link) {
1580 widget = widget_find_widget(surface->widget, x, y);
1581 if (widget)
1582 return widget;
1583 }
1584
1585 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001586}
1587
1588static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001589widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001590{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001591 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001592
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001593 widget = malloc(sizeof *widget);
1594 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001595 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001596 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001597 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001598 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001599 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001600 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001601 widget->tooltip = NULL;
1602 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001603 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001604
1605 return widget;
1606}
1607
1608struct widget *
1609window_add_widget(struct window *window, void *data)
1610{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001611 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001613 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001614 wl_list_init(&widget->link);
1615 window->main_surface->widget = widget;
1616
1617 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001618}
1619
1620struct widget *
1621widget_add_widget(struct widget *parent, void *data)
1622{
1623 struct widget *widget;
1624
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001625 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001626 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001627
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001628 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001629}
1630
1631void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001632widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001633{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001634 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001635 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001636 struct input *input;
1637
Pekka Paalanen35e82632013-04-25 13:57:48 +03001638 /* Destroy the sub-surface along with the root widget */
1639 if (surface->widget == widget && surface->subsurface)
1640 surface_destroy(widget->surface);
1641
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001642 if (widget->tooltip) {
1643 free(widget->tooltip);
1644 widget->tooltip = NULL;
1645 }
1646
Pekka Paalanene156fb62012-01-19 13:51:38 +02001647 wl_list_for_each(input, &display->input_list, link) {
1648 if (input->focus_widget == widget)
1649 input->focus_widget = NULL;
1650 }
1651
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001652 wl_list_remove(&widget->link);
1653 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001654}
1655
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001656void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001657widget_set_default_cursor(struct widget *widget, int cursor)
1658{
1659 widget->default_cursor = cursor;
1660}
1661
1662void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001663widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001665 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001666}
1667
1668void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001669widget_set_size(struct widget *widget, int32_t width, int32_t height)
1670{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001671 widget->allocation.width = width;
1672 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001673}
1674
1675void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001676widget_set_allocation(struct widget *widget,
1677 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001678{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001679 widget->allocation.x = x;
1680 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001681 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001682}
1683
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001684void
1685widget_set_transparent(struct widget *widget, int transparent)
1686{
1687 widget->opaque = !transparent;
1688}
1689
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001691widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001692{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001693 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694}
1695
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001696static cairo_surface_t *
1697widget_get_cairo_surface(struct widget *widget)
1698{
1699 struct surface *surface = widget->surface;
1700 struct window *window = widget->window;
1701
1702 if (!surface->cairo_surface) {
1703 if (surface == window->main_surface)
1704 window_create_main_surface(window);
1705 else
1706 surface_create_surface(surface, 0, 0, 0);
1707 }
1708
1709 return surface->cairo_surface;
1710}
1711
Alexander Larsson15901f02013-05-22 14:41:25 +02001712static void
1713widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1714{
1715 struct surface *surface = widget->surface;
1716 double angle;
1717 cairo_matrix_t m;
1718 enum wl_output_transform transform;
1719 int surface_width, surface_height;
1720 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001721 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001722
1723 surface_width = surface->allocation.width;
1724 surface_height = surface->allocation.height;
1725
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001726 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001727 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001728
Alexander Larsson15901f02013-05-22 14:41:25 +02001729 switch (transform) {
1730 case WL_OUTPUT_TRANSFORM_FLIPPED:
1731 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1732 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1733 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1734 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1735 break;
1736 default:
1737 cairo_matrix_init_identity(&m);
1738 break;
1739 }
1740
1741 switch (transform) {
1742 case WL_OUTPUT_TRANSFORM_NORMAL:
1743 default:
1744 angle = 0;
1745 translate_x = 0;
1746 translate_y = 0;
1747 break;
1748 case WL_OUTPUT_TRANSFORM_FLIPPED:
1749 angle = 0;
1750 translate_x = surface_width;
1751 translate_y = 0;
1752 break;
1753 case WL_OUTPUT_TRANSFORM_90:
1754 angle = M_PI_2;
1755 translate_x = surface_height;
1756 translate_y = 0;
1757 break;
1758 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1759 angle = M_PI_2;
1760 translate_x = surface_height;
1761 translate_y = surface_width;
1762 break;
1763 case WL_OUTPUT_TRANSFORM_180:
1764 angle = M_PI;
1765 translate_x = surface_width;
1766 translate_y = surface_height;
1767 break;
1768 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1769 angle = M_PI;
1770 translate_x = 0;
1771 translate_y = surface_height;
1772 break;
1773 case WL_OUTPUT_TRANSFORM_270:
1774 angle = M_PI + M_PI_2;
1775 translate_x = 0;
1776 translate_y = surface_width;
1777 break;
1778 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1779 angle = M_PI + M_PI_2;
1780 translate_x = 0;
1781 translate_y = 0;
1782 break;
1783 }
1784
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001785 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001786 cairo_translate(cr, translate_x, translate_y);
1787 cairo_rotate(cr, angle);
1788 cairo_transform(cr, &m);
1789}
1790
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001791cairo_t *
1792widget_cairo_create(struct widget *widget)
1793{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001794 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001795 cairo_surface_t *cairo_surface;
1796 cairo_t *cr;
1797
1798 cairo_surface = widget_get_cairo_surface(widget);
1799 cr = cairo_create(cairo_surface);
1800
Alexander Larsson15901f02013-05-22 14:41:25 +02001801 widget_cairo_update_transform(widget, cr);
1802
Pekka Paalanen35e82632013-04-25 13:57:48 +03001803 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1804
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001805 return cr;
1806}
1807
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001808struct wl_surface *
1809widget_get_wl_surface(struct widget *widget)
1810{
1811 return widget->surface->surface;
1812}
1813
1814uint32_t
1815widget_get_last_time(struct widget *widget)
1816{
1817 return widget->surface->last_time;
1818}
1819
1820void
1821widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1822{
1823 struct wl_compositor *comp = widget->window->display->compositor;
1824 struct surface *surface = widget->surface;
1825
1826 if (!surface->input_region)
1827 surface->input_region = wl_compositor_create_region(comp);
1828
1829 if (rect) {
1830 wl_region_add(surface->input_region,
1831 rect->x, rect->y, rect->width, rect->height);
1832 }
1833}
1834
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001835void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001836widget_set_resize_handler(struct widget *widget,
1837 widget_resize_handler_t handler)
1838{
1839 widget->resize_handler = handler;
1840}
1841
1842void
1843widget_set_redraw_handler(struct widget *widget,
1844 widget_redraw_handler_t handler)
1845{
1846 widget->redraw_handler = handler;
1847}
1848
1849void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001850widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001851{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001852 widget->enter_handler = handler;
1853}
1854
1855void
1856widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1857{
1858 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001859}
1860
1861void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001862widget_set_motion_handler(struct widget *widget,
1863 widget_motion_handler_t handler)
1864{
1865 widget->motion_handler = handler;
1866}
1867
1868void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001869widget_set_button_handler(struct widget *widget,
1870 widget_button_handler_t handler)
1871{
1872 widget->button_handler = handler;
1873}
1874
1875void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001876widget_set_axis_handler(struct widget *widget,
1877 widget_axis_handler_t handler)
1878{
1879 widget->axis_handler = handler;
1880}
1881
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001882static void
1883window_schedule_redraw_task(struct window *window);
1884
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001885void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001886widget_schedule_redraw(struct widget *widget)
1887{
Pekka Paalanen71233882013-04-25 13:57:53 +03001888 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001889 widget->surface->redraw_needed = 1;
1890 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001891}
1892
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001893cairo_surface_t *
1894window_get_surface(struct window *window)
1895{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001896 cairo_surface_t *cairo_surface;
1897
1898 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1899
1900 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001901}
1902
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001903struct wl_surface *
1904window_get_wl_surface(struct window *window)
1905{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001906 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001907}
1908
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001909struct wl_shell_surface *
1910window_get_wl_shell_surface(struct window *window)
1911{
1912 return window->shell_surface;
1913}
1914
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001915static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001916tooltip_redraw_handler(struct widget *widget, void *data)
1917{
1918 cairo_t *cr;
1919 const int32_t r = 3;
1920 struct tooltip *tooltip = data;
1921 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001922
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001923 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001924 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1925 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1926 cairo_paint(cr);
1927
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001928 width = widget->allocation.width;
1929 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001930 rounded_rect(cr, 0, 0, width, height, r);
1931
1932 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1933 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1934 cairo_fill(cr);
1935
1936 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1937 cairo_move_to(cr, 10, 16);
1938 cairo_show_text(cr, tooltip->entry);
1939 cairo_destroy(cr);
1940}
1941
1942static cairo_text_extents_t
1943get_text_extents(struct tooltip *tooltip)
1944{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001945 cairo_t *cr;
1946 cairo_text_extents_t extents;
1947
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001948 /* Use the dummy_surface because tooltip's surface was not
1949 * created yet, and parent does not have a valid surface
1950 * outside repaint, either.
1951 */
1952 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001953 cairo_text_extents(cr, tooltip->entry, &extents);
1954 cairo_destroy(cr);
1955
1956 return extents;
1957}
1958
1959static int
1960window_create_tooltip(struct tooltip *tooltip)
1961{
1962 struct widget *parent = tooltip->parent;
1963 struct display *display = parent->window->display;
1964 struct window *window;
1965 const int offset_y = 27;
1966 const int margin = 3;
1967 cairo_text_extents_t extents;
1968
1969 if (tooltip->widget)
1970 return 0;
1971
1972 window = window_create_transient(display, parent->window, tooltip->x,
1973 tooltip->y + offset_y,
1974 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1975 if (!window)
1976 return -1;
1977
1978 tooltip->window = window;
1979 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1980
1981 extents = get_text_extents(tooltip);
1982 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1983 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1984
1985 return 0;
1986}
1987
1988void
1989widget_destroy_tooltip(struct widget *parent)
1990{
1991 struct tooltip *tooltip = parent->tooltip;
1992
1993 parent->tooltip_count = 0;
1994 if (!tooltip)
1995 return;
1996
1997 if (tooltip->widget) {
1998 widget_destroy(tooltip->widget);
1999 window_destroy(tooltip->window);
2000 tooltip->widget = NULL;
2001 tooltip->window = NULL;
2002 }
2003
2004 close(tooltip->tooltip_fd);
2005 free(tooltip->entry);
2006 free(tooltip);
2007 parent->tooltip = NULL;
2008}
2009
2010static void
2011tooltip_func(struct task *task, uint32_t events)
2012{
2013 struct tooltip *tooltip =
2014 container_of(task, struct tooltip, tooltip_task);
2015 uint64_t exp;
2016
Martin Olsson8df662a2012-07-08 03:03:47 +02002017 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2018 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002019 window_create_tooltip(tooltip);
2020}
2021
2022#define TOOLTIP_TIMEOUT 500
2023static int
2024tooltip_timer_reset(struct tooltip *tooltip)
2025{
2026 struct itimerspec its;
2027
2028 its.it_interval.tv_sec = 0;
2029 its.it_interval.tv_nsec = 0;
2030 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2031 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2032 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2033 fprintf(stderr, "could not set timerfd\n: %m");
2034 return -1;
2035 }
2036
2037 return 0;
2038}
2039
2040int
2041widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2042{
2043 struct tooltip *tooltip = parent->tooltip;
2044
2045 parent->tooltip_count++;
2046 if (tooltip) {
2047 tooltip->x = x;
2048 tooltip->y = y;
2049 tooltip_timer_reset(tooltip);
2050 return 0;
2051 }
2052
2053 /* the handler might be triggered too fast via input device motion, so
2054 * we need this check here to make sure tooltip is fully initialized */
2055 if (parent->tooltip_count > 1)
2056 return 0;
2057
2058 tooltip = malloc(sizeof *tooltip);
2059 if (!tooltip)
2060 return -1;
2061
2062 parent->tooltip = tooltip;
2063 tooltip->parent = parent;
2064 tooltip->widget = NULL;
2065 tooltip->window = NULL;
2066 tooltip->x = x;
2067 tooltip->y = y;
2068 tooltip->entry = strdup(entry);
2069 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2070 if (tooltip->tooltip_fd < 0) {
2071 fprintf(stderr, "could not create timerfd\n: %m");
2072 return -1;
2073 }
2074
2075 tooltip->tooltip_task.run = tooltip_func;
2076 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2077 EPOLLIN, &tooltip->tooltip_task);
2078 tooltip_timer_reset(tooltip);
2079
2080 return 0;
2081}
2082
2083static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002084workspace_manager_state(void *data,
2085 struct workspace_manager *workspace_manager,
2086 uint32_t current,
2087 uint32_t count)
2088{
2089 struct display *display = data;
2090
2091 display->workspace = current;
2092 display->workspace_count = count;
2093}
2094
2095static const struct workspace_manager_listener workspace_manager_listener = {
2096 workspace_manager_state
2097};
2098
2099static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002100frame_resize_handler(struct widget *widget,
2101 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002102{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002103 struct frame *frame = data;
2104 struct widget *child = frame->child;
2105 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002106 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002107 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002108 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002109 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002110 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002111 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002112 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002113
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002114 switch (widget->window->type) {
2115 case TYPE_FULLSCREEN:
2116 decoration_width = 0;
2117 decoration_height = 0;
2118
2119 allocation.x = 0;
2120 allocation.y = 0;
2121 allocation.width = width;
2122 allocation.height = height;
2123 opaque_margin = 0;
2124
2125 wl_list_for_each(button, &frame->buttons_list, link)
2126 button->widget->opaque = 1;
2127 break;
2128 case TYPE_MAXIMIZED:
2129 decoration_width = t->width * 2;
2130 decoration_height = t->width + t->titlebar_height;
2131
2132 allocation.x = t->width;
2133 allocation.y = t->titlebar_height;
2134 allocation.width = width - decoration_width;
2135 allocation.height = height - decoration_height;
2136
2137 opaque_margin = 0;
2138
2139 wl_list_for_each(button, &frame->buttons_list, link)
2140 button->widget->opaque = 0;
2141 break;
2142 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002143 decoration_width = (t->width + t->margin) * 2;
2144 decoration_height = t->width +
2145 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002146
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002147 allocation.x = t->width + t->margin;
2148 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002149 allocation.width = width - decoration_width;
2150 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002151
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002152 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002153
2154 wl_list_for_each(button, &frame->buttons_list, link)
2155 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002156 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002157 }
2158
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159 widget_set_allocation(child, allocation.x, allocation.y,
2160 allocation.width, allocation.height);
2161
2162 if (child->resize_handler)
2163 child->resize_handler(child,
2164 allocation.width,
2165 allocation.height,
2166 child->user_data);
2167
Scott Moreauf7e498c2012-05-14 11:39:29 -06002168 width = child->allocation.width + decoration_width;
2169 height = child->allocation.height + decoration_height;
2170
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002171 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2172
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002173 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002174 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002175 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002176 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002177 shadow_margin, shadow_margin,
2178 width - 2 * shadow_margin,
2179 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002180 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002181 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002182 }
2183
Scott Moreauf7e498c2012-05-14 11:39:29 -06002184 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002185
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002186 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002187 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002188 opaque_margin, opaque_margin,
2189 widget->allocation.width - 2 * opaque_margin,
2190 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002191
2192 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002193 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2194 x_l = t->width + shadow_margin;
2195 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002196 wl_list_for_each(button, &frame->buttons_list, link) {
2197 const int button_padding = 4;
2198 w = cairo_image_surface_get_width(button->icon);
2199 h = cairo_image_surface_get_height(button->icon);
2200
2201 if (button->decoration == FRAME_BUTTON_FANCY)
2202 w += 10;
2203
2204 if (button->align == FRAME_BUTTON_LEFT) {
2205 widget_set_allocation(button->widget,
2206 x_l, y , w + 1, h + 1);
2207 x_l += w;
2208 x_l += button_padding;
2209 } else {
2210 x_r -= w;
2211 widget_set_allocation(button->widget,
2212 x_r, y , w + 1, h + 1);
2213 x_r -= button_padding;
2214 }
2215 }
2216}
2217
2218static int
2219frame_button_enter_handler(struct widget *widget,
2220 struct input *input, float x, float y, void *data)
2221{
2222 struct frame_button *frame_button = data;
2223
2224 widget_schedule_redraw(frame_button->widget);
2225 frame_button->state = FRAME_BUTTON_OVER;
2226
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002227 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002228}
2229
2230static void
2231frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2232{
2233 struct frame_button *frame_button = data;
2234
2235 widget_schedule_redraw(frame_button->widget);
2236 frame_button->state = FRAME_BUTTON_DEFAULT;
2237}
2238
2239static void
2240frame_button_button_handler(struct widget *widget,
2241 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002242 uint32_t button,
2243 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002244{
2245 struct frame_button *frame_button = data;
2246 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002247 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002248
2249 if (button != BTN_LEFT)
2250 return;
2251
2252 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002253 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002254 frame_button->state = FRAME_BUTTON_ACTIVE;
2255 widget_schedule_redraw(frame_button->widget);
2256
2257 if (frame_button->type == FRAME_BUTTON_ICON)
2258 window_show_frame_menu(window, input, time);
2259 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002260 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002261 frame_button->state = FRAME_BUTTON_DEFAULT;
2262 widget_schedule_redraw(frame_button->widget);
2263 break;
2264 }
2265
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002266 if (!was_pressed)
2267 return;
2268
Martin Minarik1998b152012-05-10 02:04:35 +02002269 switch (frame_button->type) {
2270 case FRAME_BUTTON_CLOSE:
2271 if (window->close_handler)
2272 window->close_handler(window->parent,
2273 window->user_data);
2274 else
2275 display_exit(window->display);
2276 break;
2277 case FRAME_BUTTON_MINIMIZE:
2278 fprintf(stderr,"Minimize stub\n");
2279 break;
2280 case FRAME_BUTTON_MAXIMIZE:
2281 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2282 break;
2283 default:
2284 /* Unknown operation */
2285 break;
2286 }
2287}
2288
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002289static int
2290frame_button_motion_handler(struct widget *widget,
2291 struct input *input, uint32_t time,
2292 float x, float y, void *data)
2293{
2294 struct frame_button *frame_button = data;
2295 enum frame_button_pointer previous_button_state = frame_button->state;
2296
2297 /* only track state for a pressed button */
2298 if (input->grab != widget)
2299 return CURSOR_LEFT_PTR;
2300
2301 if (x > widget->allocation.x &&
2302 x < (widget->allocation.x + widget->allocation.width) &&
2303 y > widget->allocation.y &&
2304 y < (widget->allocation.y + widget->allocation.height)) {
2305 frame_button->state = FRAME_BUTTON_ACTIVE;
2306 } else {
2307 frame_button->state = FRAME_BUTTON_DEFAULT;
2308 }
2309
2310 if (frame_button->state != previous_button_state)
2311 widget_schedule_redraw(frame_button->widget);
2312
2313 return CURSOR_LEFT_PTR;
2314}
2315
Martin Minarik1998b152012-05-10 02:04:35 +02002316static void
2317frame_button_redraw_handler(struct widget *widget, void *data)
2318{
2319 struct frame_button *frame_button = data;
2320 cairo_t *cr;
2321 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002322
2323 x = widget->allocation.x;
2324 y = widget->allocation.y;
2325 width = widget->allocation.width;
2326 height = widget->allocation.height;
2327
2328 if (!width)
2329 return;
2330 if (!height)
2331 return;
2332 if (widget->opaque)
2333 return;
2334
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002335 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002336
2337 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2338 cairo_set_line_width(cr, 1);
2339
2340 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2341 cairo_rectangle (cr, x, y, 25, 16);
2342
2343 cairo_stroke_preserve(cr);
2344
2345 switch (frame_button->state) {
2346 case FRAME_BUTTON_DEFAULT:
2347 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2348 break;
2349 case FRAME_BUTTON_OVER:
2350 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2351 break;
2352 case FRAME_BUTTON_ACTIVE:
2353 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2354 break;
2355 }
2356
2357 cairo_fill (cr);
2358
2359 x += 4;
2360 }
2361
2362 cairo_set_source_surface(cr, frame_button->icon, x, y);
2363 cairo_paint(cr);
2364
2365 cairo_destroy(cr);
2366}
2367
2368static struct widget *
2369frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2370 enum frame_button_align align, enum frame_button_decoration style)
2371{
2372 struct frame_button *frame_button;
2373 const char *icon = data;
2374
2375 frame_button = malloc (sizeof *frame_button);
2376 memset(frame_button, 0, sizeof *frame_button);
2377
2378 frame_button->icon = cairo_image_surface_create_from_png(icon);
2379 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2380 frame_button->frame = frame;
2381 frame_button->type = type;
2382 frame_button->align = align;
2383 frame_button->decoration = style;
2384
2385 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2386
2387 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2388 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2389 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2390 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002391 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002392 return frame_button->widget;
2393}
2394
2395static void
2396frame_button_destroy(struct frame_button *frame_button)
2397{
2398 widget_destroy(frame_button->widget);
2399 wl_list_remove(&frame_button->link);
2400 cairo_surface_destroy(frame_button->icon);
2401 free(frame_button);
2402
2403 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002404}
2405
2406static void
2407frame_redraw_handler(struct widget *widget, void *data)
2408{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002409 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002410 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002411 struct theme *t = window->display->theme;
2412 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002413
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002414 if (window->type == TYPE_FULLSCREEN)
2415 return;
2416
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002417 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002418
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002419 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002420 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002421 if (window->type == TYPE_MAXIMIZED)
2422 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002423 theme_render_frame(t, cr, widget->allocation.width,
2424 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002425
2426 cairo_destroy(cr);
2427}
2428
2429static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002430frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002431{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002432 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002433 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002434 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002435
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002436 if (window->type != TYPE_TOPLEVEL)
2437 return CURSOR_LEFT_PTR;
2438
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002439 location = theme_get_location(t, input->sx, input->sy,
2440 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002441 frame->widget->allocation.height,
2442 window->type == TYPE_MAXIMIZED ?
2443 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002444
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002445 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002446 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002447 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002448 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002449 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002450 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002451 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002452 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002453 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002454 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002455 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002456 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002457 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002458 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002459 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002460 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002461 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002462 case THEME_LOCATION_EXTERIOR:
2463 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002464 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002465 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002466 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002467}
2468
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002469static void
2470frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002471{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002472 struct display *display;
2473
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002474 switch (index) {
2475 case 0: /* close */
2476 if (window->close_handler)
2477 window->close_handler(window->parent,
2478 window->user_data);
2479 else
2480 display_exit(window->display);
2481 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002482 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002483 display = window->display;
2484 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002485 workspace_manager_move_surface(
2486 display->workspace_manager,
2487 window->main_surface->surface,
2488 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002489 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002490 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002491 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002492 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002493 workspace_manager_move_surface(
2494 display->workspace_manager,
2495 window->main_surface->surface,
2496 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002497 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002498 case 3: /* fullscreen */
2499 /* we don't have a way to get out of fullscreen for now */
2500 if (window->fullscreen_handler)
2501 window->fullscreen_handler(window, window->user_data);
2502 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002503 }
2504}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002505
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002506void
2507window_show_frame_menu(struct window *window,
2508 struct input *input, uint32_t time)
2509{
2510 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002511 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002512
2513 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002514 "Close",
2515 "Move to workspace above", "Move to workspace below",
2516 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002517 };
2518
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002519 if (window->fullscreen_handler)
2520 count = ARRAY_LENGTH(entries);
2521 else
2522 count = ARRAY_LENGTH(entries) - 1;
2523
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002524 input_get_position(input, &x, &y);
2525 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002526 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002527}
2528
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002529static int
2530frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002531 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002532{
2533 return frame_get_pointer_image_for_location(data, input);
2534}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002535
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002536static int
2537frame_motion_handler(struct widget *widget,
2538 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002539 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002540{
2541 return frame_get_pointer_image_for_location(data, input);
2542}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002543
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002544static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002545frame_button_handler(struct widget *widget,
2546 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002547 uint32_t button, enum wl_pointer_button_state state,
2548 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002549
2550{
2551 struct frame *frame = data;
2552 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002553 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002554 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002555
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002556 if (window->type != TYPE_TOPLEVEL)
2557 return;
2558
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002559 location = theme_get_location(display->theme, input->sx, input->sy,
2560 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002561 frame->widget->allocation.height,
2562 window->type == TYPE_MAXIMIZED ?
2563 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002564
Daniel Stone4dbadb12012-05-30 16:31:51 +01002565 if (window->display->shell && button == BTN_LEFT &&
2566 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002567 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002568 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002569 if (!window->shell_surface)
2570 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002571 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002572 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002573 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002574 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002575 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002576 case THEME_LOCATION_RESIZING_TOP:
2577 case THEME_LOCATION_RESIZING_BOTTOM:
2578 case THEME_LOCATION_RESIZING_LEFT:
2579 case THEME_LOCATION_RESIZING_RIGHT:
2580 case THEME_LOCATION_RESIZING_TOP_LEFT:
2581 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2582 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2583 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002584 if (!window->shell_surface)
2585 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002586 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002587
Pekka Paalanen99436862012-11-19 17:15:59 +02002588 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002589 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002590 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002591 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002592 break;
2593 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002594 } else if (button == BTN_RIGHT &&
2595 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002596 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002597 }
2598}
2599
2600struct widget *
2601frame_create(struct window *window, void *data)
2602{
2603 struct frame *frame;
2604
2605 frame = malloc(sizeof *frame);
2606 memset(frame, 0, sizeof *frame);
2607
2608 frame->widget = window_add_widget(window, frame);
2609 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002610
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002611 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2612 widget_set_resize_handler(frame->widget, frame_resize_handler);
2613 widget_set_enter_handler(frame->widget, frame_enter_handler);
2614 widget_set_motion_handler(frame->widget, frame_motion_handler);
2615 widget_set_button_handler(frame->widget, frame_button_handler);
2616
Martin Minarik1998b152012-05-10 02:04:35 +02002617 /* Create empty list for frame buttons */
2618 wl_list_init(&frame->buttons_list);
2619
2620 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2621 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2622
2623 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2624 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2625
2626 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2627 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2628
2629 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2630 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2631
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002632 window->frame = frame;
2633
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002634 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002635}
2636
Kristian Høgsberga1627922012-06-20 17:30:03 -04002637void
2638frame_set_child_size(struct widget *widget, int child_width, int child_height)
2639{
2640 struct display *display = widget->window->display;
2641 struct theme *t = display->theme;
2642 int decoration_width, decoration_height;
2643 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002644 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002645
2646 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002647 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002648 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002649 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002650
2651 width = child_width + decoration_width;
2652 height = child_height + decoration_height;
2653 } else {
2654 width = child_width;
2655 height = child_height;
2656 }
2657
2658 window_schedule_resize(widget->window, width, height);
2659}
2660
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002661static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002662frame_destroy(struct frame *frame)
2663{
Martin Minarik1998b152012-05-10 02:04:35 +02002664 struct frame_button *button, *tmp;
2665
2666 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2667 frame_button_destroy(button);
2668
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002669 /* frame->child must be destroyed by the application */
2670 widget_destroy(frame->widget);
2671 free(frame);
2672}
2673
2674static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002675input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002676 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002677{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002678 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002679 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002680
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002681 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002682 return;
2683
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002684 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002685 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002686 widget = old;
2687 if (input->grab)
2688 widget = input->grab;
2689 if (widget->leave_handler)
2690 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002691 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002692 }
2693
2694 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002695 widget = focus;
2696 if (input->grab)
2697 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002698 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002699 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002700 cursor = widget->enter_handler(focus, input, x, y,
2701 widget->user_data);
2702 else
2703 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002704
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002705 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002706 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002707}
2708
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002709void
2710input_grab(struct input *input, struct widget *widget, uint32_t button)
2711{
2712 input->grab = widget;
2713 input->grab_button = button;
2714}
2715
2716void
2717input_ungrab(struct input *input)
2718{
2719 struct widget *widget;
2720
2721 input->grab = NULL;
2722 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002723 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002724 input->sx, input->sy);
2725 input_set_focus_widget(input, widget, input->sx, input->sy);
2726 }
2727}
2728
2729static void
2730input_remove_pointer_focus(struct input *input)
2731{
2732 struct window *window = input->pointer_focus;
2733
2734 if (!window)
2735 return;
2736
2737 input_set_focus_widget(input, NULL, 0, 0);
2738
2739 input->pointer_focus = NULL;
2740 input->current_cursor = CURSOR_UNSET;
2741}
2742
2743static void
2744pointer_handle_enter(void *data, struct wl_pointer *pointer,
2745 uint32_t serial, struct wl_surface *surface,
2746 wl_fixed_t sx_w, wl_fixed_t sy_w)
2747{
2748 struct input *input = data;
2749 struct window *window;
2750 struct widget *widget;
2751 float sx = wl_fixed_to_double(sx_w);
2752 float sy = wl_fixed_to_double(sy_w);
2753
2754 if (!surface) {
2755 /* enter event for a window we've just destroyed */
2756 return;
2757 }
2758
2759 input->display->serial = serial;
2760 input->pointer_enter_serial = serial;
2761 input->pointer_focus = wl_surface_get_user_data(surface);
2762 window = input->pointer_focus;
2763
Pekka Paalanen99436862012-11-19 17:15:59 +02002764 if (window->resizing) {
2765 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002766 /* Schedule a redraw to free the pool */
2767 window_schedule_redraw(window);
2768 }
2769
2770 input->sx = sx;
2771 input->sy = sy;
2772
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002773 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002774 input_set_focus_widget(input, widget, sx, sy);
2775}
2776
2777static void
2778pointer_handle_leave(void *data, struct wl_pointer *pointer,
2779 uint32_t serial, struct wl_surface *surface)
2780{
2781 struct input *input = data;
2782
2783 input->display->serial = serial;
2784 input_remove_pointer_focus(input);
2785}
2786
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002787static void
Daniel Stone37816df2012-05-16 18:45:18 +01002788pointer_handle_motion(void *data, struct wl_pointer *pointer,
2789 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002790{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002791 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002792 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002793 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002794 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002795 float sx = wl_fixed_to_double(sx_w);
2796 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002797
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002798 input->sx = sx;
2799 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002800
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002801 if (!window)
2802 return;
2803
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002804 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002805 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002806 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002807 }
2808
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002809 if (input->grab)
2810 widget = input->grab;
2811 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002812 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002813 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002814 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002815 input, time, sx, sy,
2816 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002817 else
2818 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002819
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002820 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002821}
2822
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002823static void
Daniel Stone37816df2012-05-16 18:45:18 +01002824pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002825 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002826{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002827 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002828 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002829 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002830
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002831 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002832 if (input->focus_widget && input->grab == NULL &&
2833 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002834 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002835
Neil Roberts6b28aad2012-01-23 19:11:18 +00002836 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002837 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002838 (*widget->button_handler)(widget,
2839 input, time,
2840 button, state,
2841 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002842
Daniel Stone4dbadb12012-05-30 16:31:51 +01002843 if (input->grab && input->grab_button == button &&
2844 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002845 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002846}
2847
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002848static void
Daniel Stone37816df2012-05-16 18:45:18 +01002849pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002850 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002851{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002852 struct input *input = data;
2853 struct widget *widget;
2854
2855 widget = input->focus_widget;
2856 if (input->grab)
2857 widget = input->grab;
2858 if (widget && widget->axis_handler)
2859 (*widget->axis_handler)(widget,
2860 input, time,
2861 axis, value,
2862 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002863}
2864
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002865static const struct wl_pointer_listener pointer_listener = {
2866 pointer_handle_enter,
2867 pointer_handle_leave,
2868 pointer_handle_motion,
2869 pointer_handle_button,
2870 pointer_handle_axis,
2871};
2872
2873static void
2874input_remove_keyboard_focus(struct input *input)
2875{
2876 struct window *window = input->keyboard_focus;
2877 struct itimerspec its;
2878
2879 its.it_interval.tv_sec = 0;
2880 its.it_interval.tv_nsec = 0;
2881 its.it_value.tv_sec = 0;
2882 its.it_value.tv_nsec = 0;
2883 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2884
2885 if (!window)
2886 return;
2887
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002888 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002889 if (window->keyboard_focus_handler)
2890 (*window->keyboard_focus_handler)(window, NULL,
2891 window->user_data);
2892
2893 input->keyboard_focus = NULL;
2894}
2895
2896static void
2897keyboard_repeat_func(struct task *task, uint32_t events)
2898{
2899 struct input *input =
2900 container_of(task, struct input, repeat_task);
2901 struct window *window = input->keyboard_focus;
2902 uint64_t exp;
2903
2904 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2905 /* If we change the timer between the fd becoming
2906 * readable and getting here, there'll be nothing to
2907 * read and we get EAGAIN. */
2908 return;
2909
2910 if (window && window->key_handler) {
2911 (*window->key_handler)(window, input, input->repeat_time,
2912 input->repeat_key, input->repeat_sym,
2913 WL_KEYBOARD_KEY_STATE_PRESSED,
2914 window->user_data);
2915 }
2916}
2917
2918static void
2919keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2920 uint32_t format, int fd, uint32_t size)
2921{
2922 struct input *input = data;
2923 char *map_str;
2924
2925 if (!data) {
2926 close(fd);
2927 return;
2928 }
2929
2930 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2931 close(fd);
2932 return;
2933 }
2934
2935 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2936 if (map_str == MAP_FAILED) {
2937 close(fd);
2938 return;
2939 }
2940
2941 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2942 map_str,
2943 XKB_KEYMAP_FORMAT_TEXT_V1,
2944 0);
2945 munmap(map_str, size);
2946 close(fd);
2947
2948 if (!input->xkb.keymap) {
2949 fprintf(stderr, "failed to compile keymap\n");
2950 return;
2951 }
2952
2953 input->xkb.state = xkb_state_new(input->xkb.keymap);
2954 if (!input->xkb.state) {
2955 fprintf(stderr, "failed to create XKB state\n");
2956 xkb_map_unref(input->xkb.keymap);
2957 input->xkb.keymap = NULL;
2958 return;
2959 }
2960
2961 input->xkb.control_mask =
2962 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2963 input->xkb.alt_mask =
2964 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2965 input->xkb.shift_mask =
2966 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2967}
2968
2969static void
2970keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2971 uint32_t serial, struct wl_surface *surface,
2972 struct wl_array *keys)
2973{
2974 struct input *input = data;
2975 struct window *window;
2976
2977 input->display->serial = serial;
2978 input->keyboard_focus = wl_surface_get_user_data(surface);
2979
2980 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002981 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002982 if (window->keyboard_focus_handler)
2983 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002984 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002985}
2986
2987static void
2988keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2989 uint32_t serial, struct wl_surface *surface)
2990{
2991 struct input *input = data;
2992
2993 input->display->serial = serial;
2994 input_remove_keyboard_focus(input);
2995}
2996
Scott Moreau210d0792012-03-22 10:47:01 -06002997static void
Daniel Stone37816df2012-05-16 18:45:18 +01002998keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002999 uint32_t serial, uint32_t time, uint32_t key,
3000 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003001{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003002 struct input *input = data;
3003 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003004 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003005 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003006 const xkb_keysym_t *syms;
3007 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003008 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003009
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003010 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003011 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003012 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003013 return;
3014
Daniel Stone97f68542012-05-30 16:32:01 +01003015 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003016
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003017 sym = XKB_KEY_NoSymbol;
3018 if (num_syms == 1)
3019 sym = syms[0];
3020
3021 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003022 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003023 window_set_maximized(window,
3024 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003025 } else if (sym == XKB_KEY_F11 &&
3026 window->fullscreen_handler &&
3027 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3028 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003029 } else if (sym == XKB_KEY_F4 &&
3030 input->modifiers == MOD_ALT_MASK &&
3031 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3032 if (window->close_handler)
3033 window->close_handler(window->parent,
3034 window->user_data);
3035 else
3036 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003037 } else if (window->key_handler) {
3038 (*window->key_handler)(window, input, time, key,
3039 sym, state, window->user_data);
3040 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003041
3042 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3043 key == input->repeat_key) {
3044 its.it_interval.tv_sec = 0;
3045 its.it_interval.tv_nsec = 0;
3046 its.it_value.tv_sec = 0;
3047 its.it_value.tv_nsec = 0;
3048 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3049 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3050 input->repeat_sym = sym;
3051 input->repeat_key = key;
3052 input->repeat_time = time;
3053 its.it_interval.tv_sec = 0;
3054 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3055 its.it_value.tv_sec = 0;
3056 its.it_value.tv_nsec = 400 * 1000 * 1000;
3057 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3058 }
3059}
3060
3061static void
Daniel Stone351eb612012-05-31 15:27:47 -04003062keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3063 uint32_t serial, uint32_t mods_depressed,
3064 uint32_t mods_latched, uint32_t mods_locked,
3065 uint32_t group)
3066{
3067 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003068 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003069
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003070 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3071 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003072 mask = xkb_state_serialize_mods(input->xkb.state,
3073 XKB_STATE_DEPRESSED |
3074 XKB_STATE_LATCHED);
3075 input->modifiers = 0;
3076 if (mask & input->xkb.control_mask)
3077 input->modifiers |= MOD_CONTROL_MASK;
3078 if (mask & input->xkb.alt_mask)
3079 input->modifiers |= MOD_ALT_MASK;
3080 if (mask & input->xkb.shift_mask)
3081 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003082}
3083
Daniel Stone37816df2012-05-16 18:45:18 +01003084static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003085 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003086 keyboard_handle_enter,
3087 keyboard_handle_leave,
3088 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003089 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003090};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003091
3092static void
Daniel Stone37816df2012-05-16 18:45:18 +01003093seat_handle_capabilities(void *data, struct wl_seat *seat,
3094 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003095{
Daniel Stone37816df2012-05-16 18:45:18 +01003096 struct input *input = data;
3097
3098 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3099 input->pointer = wl_seat_get_pointer(seat);
3100 wl_pointer_set_user_data(input->pointer, input);
3101 wl_pointer_add_listener(input->pointer, &pointer_listener,
3102 input);
3103 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3104 wl_pointer_destroy(input->pointer);
3105 input->pointer = NULL;
3106 }
3107
3108 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3109 input->keyboard = wl_seat_get_keyboard(seat);
3110 wl_keyboard_set_user_data(input->keyboard, input);
3111 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3112 input);
3113 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3114 wl_keyboard_destroy(input->keyboard);
3115 input->keyboard = NULL;
3116 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003117}
3118
Daniel Stone37816df2012-05-16 18:45:18 +01003119static const struct wl_seat_listener seat_listener = {
3120 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003121};
3122
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003123void
3124input_get_position(struct input *input, int32_t *x, int32_t *y)
3125{
3126 *x = input->sx;
3127 *y = input->sy;
3128}
3129
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003130struct display *
3131input_get_display(struct input *input)
3132{
3133 return input->display;
3134}
3135
Daniel Stone37816df2012-05-16 18:45:18 +01003136struct wl_seat *
3137input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003138{
Daniel Stone37816df2012-05-16 18:45:18 +01003139 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003140}
3141
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003142uint32_t
3143input_get_modifiers(struct input *input)
3144{
3145 return input->modifiers;
3146}
3147
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003148struct widget *
3149input_get_focus_widget(struct input *input)
3150{
3151 return input->focus_widget;
3152}
3153
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003154struct data_offer {
3155 struct wl_data_offer *offer;
3156 struct input *input;
3157 struct wl_array types;
3158 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003159
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003160 struct task io_task;
3161 int fd;
3162 data_func_t func;
3163 int32_t x, y;
3164 void *user_data;
3165};
3166
3167static void
3168data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3169{
3170 struct data_offer *offer = data;
3171 char **p;
3172
3173 p = wl_array_add(&offer->types, sizeof *p);
3174 *p = strdup(type);
3175}
3176
3177static const struct wl_data_offer_listener data_offer_listener = {
3178 data_offer_offer,
3179};
3180
3181static void
3182data_offer_destroy(struct data_offer *offer)
3183{
3184 char **p;
3185
3186 offer->refcount--;
3187 if (offer->refcount == 0) {
3188 wl_data_offer_destroy(offer->offer);
3189 for (p = offer->types.data; *p; p++)
3190 free(*p);
3191 wl_array_release(&offer->types);
3192 free(offer);
3193 }
3194}
3195
3196static void
3197data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003198 struct wl_data_device *data_device,
3199 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003200{
3201 struct data_offer *offer;
3202
3203 offer = malloc(sizeof *offer);
3204
3205 wl_array_init(&offer->types);
3206 offer->refcount = 1;
3207 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003208 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003209 wl_data_offer_add_listener(offer->offer,
3210 &data_offer_listener, offer);
3211}
3212
3213static void
3214data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003215 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003216 wl_fixed_t x_w, wl_fixed_t y_w,
3217 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003218{
3219 struct input *input = data;
3220 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003221 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003222 float x = wl_fixed_to_double(x_w);
3223 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003224 char **p;
3225
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003226 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003227 window = wl_surface_get_user_data(surface);
3228 input->pointer_focus = window;
3229
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003230 if (offer) {
3231 input->drag_offer = wl_data_offer_get_user_data(offer);
3232
3233 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3234 *p = NULL;
3235
3236 types_data = input->drag_offer->types.data;
3237 } else {
3238 input->drag_offer = NULL;
3239 types_data = NULL;
3240 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003241
3242 window = input->pointer_focus;
3243 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003244 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003245 window->user_data);
3246}
3247
3248static void
3249data_device_leave(void *data, struct wl_data_device *data_device)
3250{
3251 struct input *input = data;
3252
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003253 if (input->drag_offer) {
3254 data_offer_destroy(input->drag_offer);
3255 input->drag_offer = NULL;
3256 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003257}
3258
3259static void
3260data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003261 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003262{
3263 struct input *input = data;
3264 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003265 float x = wl_fixed_to_double(x_w);
3266 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003267 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003268
3269 input->sx = x;
3270 input->sy = y;
3271
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003272 if (input->drag_offer)
3273 types_data = input->drag_offer->types.data;
3274 else
3275 types_data = NULL;
3276
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003277 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003278 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003279 window->user_data);
3280}
3281
3282static void
3283data_device_drop(void *data, struct wl_data_device *data_device)
3284{
3285 struct input *input = data;
3286 struct window *window = input->pointer_focus;
3287
3288 if (window->drop_handler)
3289 window->drop_handler(window, input,
3290 input->sx, input->sy, window->user_data);
3291}
3292
3293static void
3294data_device_selection(void *data,
3295 struct wl_data_device *wl_data_device,
3296 struct wl_data_offer *offer)
3297{
3298 struct input *input = data;
3299 char **p;
3300
3301 if (input->selection_offer)
3302 data_offer_destroy(input->selection_offer);
3303
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003304 if (offer) {
3305 input->selection_offer = wl_data_offer_get_user_data(offer);
3306 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3307 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003308 } else {
3309 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003310 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003311}
3312
3313static const struct wl_data_device_listener data_device_listener = {
3314 data_device_data_offer,
3315 data_device_enter,
3316 data_device_leave,
3317 data_device_motion,
3318 data_device_drop,
3319 data_device_selection
3320};
3321
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003322static void
3323input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003324{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003325 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003326 struct wl_cursor *cursor;
3327 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003328
Daniel Stone80972742012-11-07 17:51:39 +11003329 if (!input->pointer)
3330 return;
3331
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003332 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003333 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003334 return;
3335
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003336 if (index >= (int) cursor->image_count) {
3337 fprintf(stderr, "cursor index out of range\n");
3338 return;
3339 }
3340
3341 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003342 buffer = wl_cursor_image_get_buffer(image);
3343 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003344 return;
3345
Kristian Høgsbergae277372012-08-01 09:41:08 -04003346 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003347 input->pointer_surface,
3348 image->hotspot_x, image->hotspot_y);
3349 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3350 wl_surface_damage(input->pointer_surface, 0, 0,
3351 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003352 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003353}
3354
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003355static const struct wl_callback_listener pointer_surface_listener;
3356
3357static void
3358pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3359 uint32_t time)
3360{
3361 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003362 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003363 int i;
3364
3365 if (callback) {
3366 assert(callback == input->cursor_frame_cb);
3367 wl_callback_destroy(callback);
3368 input->cursor_frame_cb = NULL;
3369 }
3370
Daniel Stone80972742012-11-07 17:51:39 +11003371 if (!input->pointer)
3372 return;
3373
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003374 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003375 wl_pointer_set_cursor(input->pointer,
3376 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003377 NULL, 0, 0);
3378 return;
3379 }
3380
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003381 if (input->current_cursor == CURSOR_UNSET)
3382 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003383 cursor = input->display->cursors[input->current_cursor];
3384 if (!cursor)
3385 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003386
3387 /* FIXME We don't have the current time on the first call so we set
3388 * the animation start to the time of the first frame callback. */
3389 if (time == 0)
3390 input->cursor_anim_start = 0;
3391 else if (input->cursor_anim_start == 0)
3392 input->cursor_anim_start = time;
3393
3394 if (time == 0 || input->cursor_anim_start == 0)
3395 i = 0;
3396 else
3397 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3398
Pekka Paalanenbc106382012-10-10 12:49:31 +03003399 if (cursor->image_count > 1) {
3400 input->cursor_frame_cb =
3401 wl_surface_frame(input->pointer_surface);
3402 wl_callback_add_listener(input->cursor_frame_cb,
3403 &pointer_surface_listener, input);
3404 }
3405
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003406 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003407}
3408
3409static const struct wl_callback_listener pointer_surface_listener = {
3410 pointer_surface_frame_callback
3411};
3412
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003413void
3414input_set_pointer_image(struct input *input, int pointer)
3415{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003416 int force = 0;
3417
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003418 if (!input->pointer)
3419 return;
3420
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003421 if (input->pointer_enter_serial > input->cursor_serial)
3422 force = 1;
3423
3424 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003425 return;
3426
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003427 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003428 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003429 if (!input->cursor_frame_cb)
3430 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003431 else if (force) {
3432 /* The current frame callback may be stuck if, for instance,
3433 * the set cursor request was processed by the server after
3434 * this client lost the focus. In this case the cursor surface
3435 * might not be mapped and the frame callback wouldn't ever
3436 * complete. Send a set_cursor and attach to try to map the
3437 * cursor surface again so that the callback will finish */
3438 input_set_pointer_image_index(input, 0);
3439 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003440}
3441
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003442struct wl_data_device *
3443input_get_data_device(struct input *input)
3444{
3445 return input->data_device;
3446}
3447
3448void
3449input_set_selection(struct input *input,
3450 struct wl_data_source *source, uint32_t time)
3451{
3452 wl_data_device_set_selection(input->data_device, source, time);
3453}
3454
3455void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003456input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003457{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003458 wl_data_offer_accept(input->drag_offer->offer,
3459 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003460}
3461
3462static void
3463offer_io_func(struct task *task, uint32_t events)
3464{
3465 struct data_offer *offer =
3466 container_of(task, struct data_offer, io_task);
3467 unsigned int len;
3468 char buffer[4096];
3469
3470 len = read(offer->fd, buffer, sizeof buffer);
3471 offer->func(buffer, len,
3472 offer->x, offer->y, offer->user_data);
3473
3474 if (len == 0) {
3475 close(offer->fd);
3476 data_offer_destroy(offer);
3477 }
3478}
3479
3480static void
3481data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3482 data_func_t func, void *user_data)
3483{
3484 int p[2];
3485
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003486 if (pipe2(p, O_CLOEXEC) == -1)
3487 return;
3488
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003489 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3490 close(p[1]);
3491
3492 offer->io_task.run = offer_io_func;
3493 offer->fd = p[0];
3494 offer->func = func;
3495 offer->refcount++;
3496 offer->user_data = user_data;
3497
3498 display_watch_fd(offer->input->display,
3499 offer->fd, EPOLLIN, &offer->io_task);
3500}
3501
3502void
3503input_receive_drag_data(struct input *input, const char *mime_type,
3504 data_func_t func, void *data)
3505{
3506 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3507 input->drag_offer->x = input->sx;
3508 input->drag_offer->y = input->sy;
3509}
3510
3511int
3512input_receive_selection_data(struct input *input, const char *mime_type,
3513 data_func_t func, void *data)
3514{
3515 char **p;
3516
3517 if (input->selection_offer == NULL)
3518 return -1;
3519
3520 for (p = input->selection_offer->types.data; *p; p++)
3521 if (strcmp(mime_type, *p) == 0)
3522 break;
3523
3524 if (*p == NULL)
3525 return -1;
3526
3527 data_offer_receive_data(input->selection_offer,
3528 mime_type, func, data);
3529 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003530}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003531
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003532int
3533input_receive_selection_data_to_fd(struct input *input,
3534 const char *mime_type, int fd)
3535{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003536 if (input->selection_offer)
3537 wl_data_offer_receive(input->selection_offer->offer,
3538 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003539
3540 return 0;
3541}
3542
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003543void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003544window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003545{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003546 if (!window->shell_surface)
3547 return;
3548
Daniel Stone37816df2012-05-16 18:45:18 +01003549 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003550}
3551
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003552static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003553surface_set_synchronized(struct surface *surface)
3554{
3555 if (!surface->subsurface)
3556 return;
3557
3558 if (surface->synchronized)
3559 return;
3560
3561 wl_subsurface_set_sync(surface->subsurface);
3562 surface->synchronized = 1;
3563}
3564
3565static void
3566surface_set_synchronized_default(struct surface *surface)
3567{
3568 if (!surface->subsurface)
3569 return;
3570
3571 if (surface->synchronized == surface->synchronized_default)
3572 return;
3573
3574 if (surface->synchronized_default)
3575 wl_subsurface_set_sync(surface->subsurface);
3576 else
3577 wl_subsurface_set_desync(surface->subsurface);
3578
3579 surface->synchronized = surface->synchronized_default;
3580}
3581
3582static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003583surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003584{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003585 struct widget *widget = surface->widget;
3586 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003587
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003588 if (surface->input_region) {
3589 wl_region_destroy(surface->input_region);
3590 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003591 }
3592
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003593 if (surface->opaque_region)
3594 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003595
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003596 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003597
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003598 if (widget->resize_handler)
3599 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003600 widget->allocation.width,
3601 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003602 widget->user_data);
3603
Pekka Paalanen35e82632013-04-25 13:57:48 +03003604 if (surface->subsurface &&
3605 (surface->allocation.x != widget->allocation.x ||
3606 surface->allocation.y != widget->allocation.y)) {
3607 wl_subsurface_set_position(surface->subsurface,
3608 widget->allocation.x,
3609 widget->allocation.y);
3610 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003611 if (surface->allocation.width != widget->allocation.width ||
3612 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003613 window_schedule_redraw(widget->window);
3614 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003615 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003616
3617 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003618 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003619 widget->allocation.width,
3620 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003621}
3622
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003623static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003624hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3625{
3626 /*
3627 * This hack should be removed, when EGL respects
3628 * eglSwapInterval(0).
3629 *
3630 * If this window has sub-surfaces, especially a free-running
3631 * EGL-widget, we need to post the parent surface once with
3632 * all the old state to guarantee, that the EGL-widget will
3633 * receive its frame callback soon. Otherwise, a forced call
3634 * to eglSwapBuffers may end up blocking, waiting for a frame
3635 * event that will never come, because we will commit the parent
3636 * surface with all new state only after eglSwapBuffers returns.
3637 *
3638 * This assumes, that:
3639 * 1. When the EGL widget's resize hook is called, it pauses.
3640 * 2. When the EGL widget's redraw hook is called, it forces a
3641 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3642 * In a single threaded application condition 1 is a no-op.
3643 *
3644 * XXX: This should actually be after the surface_resize() calls,
3645 * but cannot, because then it would commit the incomplete state
3646 * accumulated from the widget resize hooks.
3647 */
3648 if (window->subsurface_list.next != &window->main_surface->link ||
3649 window->subsurface_list.prev != &window->main_surface->link)
3650 wl_surface_commit(window->main_surface->surface);
3651}
3652
3653static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003654idle_resize(struct window *window)
3655{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003656 struct surface *surface;
3657
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003658 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003659 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003660
Pekka Paalanen71233882013-04-25 13:57:53 +03003661 DBG("from %dx%d to %dx%d\n",
3662 window->main_surface->server_allocation.width,
3663 window->main_surface->server_allocation.height,
3664 window->pending_allocation.width,
3665 window->pending_allocation.height);
3666
Pekka Paalanene9297f82013-04-25 13:57:51 +03003667 hack_prevent_EGL_sub_surface_deadlock(window);
3668
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003669 widget_set_allocation(window->main_surface->widget,
3670 window->pending_allocation.x,
3671 window->pending_allocation.y,
3672 window->pending_allocation.width,
3673 window->pending_allocation.height);
3674
3675 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003676
3677 /* The main surface is in the list, too. Main surface's
3678 * resize_handler is responsible for calling widget_set_allocation()
3679 * on all sub-surface root widgets, so they will be resized
3680 * properly.
3681 */
3682 wl_list_for_each(surface, &window->subsurface_list, link) {
3683 if (surface == window->main_surface)
3684 continue;
3685
3686 surface_set_synchronized(surface);
3687 surface_resize(surface);
3688 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003689}
3690
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003691void
3692window_schedule_resize(struct window *window, int width, int height)
3693{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003694 window->pending_allocation.x = 0;
3695 window->pending_allocation.y = 0;
3696 window->pending_allocation.width = width;
3697 window->pending_allocation.height = height;
3698
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003699 if (window->min_allocation.width == 0)
3700 window->min_allocation = window->pending_allocation;
3701 if (window->pending_allocation.width < window->min_allocation.width)
3702 window->pending_allocation.width = window->min_allocation.width;
3703 if (window->pending_allocation.height < window->min_allocation.height)
3704 window->pending_allocation.height = window->min_allocation.height;
3705
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003706 window->resize_needed = 1;
3707 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003708}
3709
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003710void
3711widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3712{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003713 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003714}
3715
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003716static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003717handle_ping(void *data, struct wl_shell_surface *shell_surface,
3718 uint32_t serial)
3719{
3720 wl_shell_surface_pong(shell_surface, serial);
3721}
3722
3723static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003724handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003725 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003726{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003727 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003728
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003729 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003730 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003731}
3732
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003733static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003734menu_destroy(struct menu *menu)
3735{
3736 widget_destroy(menu->widget);
3737 window_destroy(menu->window);
3738 free(menu);
3739}
3740
3741static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003742handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3743{
3744 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003745 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003746
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003747 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003748 * device. Or just use wl_callback. And this really needs to
3749 * be a window vfunc that the menu can set. And we need the
3750 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003751
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003752 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003753 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003754}
3755
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003756static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003757 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003758 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003759 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003760};
3761
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003762void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003763window_get_allocation(struct window *window,
3764 struct rectangle *allocation)
3765{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003766 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003767}
3768
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003769static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003770widget_redraw(struct widget *widget)
3771{
3772 struct widget *child;
3773
3774 if (widget->redraw_handler)
3775 widget->redraw_handler(widget, widget->user_data);
3776 wl_list_for_each(child, &widget->child_list, link)
3777 widget_redraw(child);
3778}
3779
3780static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003781frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3782{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003783 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003784
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003785 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003786 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003787 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003788 surface->frame_cb = NULL;
3789
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003790 surface->last_time = time;
3791
Pekka Paalanen71233882013-04-25 13:57:53 +03003792 if (surface->redraw_needed || surface->window->redraw_needed) {
3793 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003794 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003795 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003796}
3797
3798static const struct wl_callback_listener listener = {
3799 frame_callback
3800};
3801
3802static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003803surface_redraw(struct surface *surface)
3804{
Pekka Paalanen71233882013-04-25 13:57:53 +03003805 DBG_OBJ(surface->surface, "begin\n");
3806
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003807 if (!surface->window->redraw_needed && !surface->redraw_needed)
3808 return;
3809
3810 /* Whole-window redraw forces a redraw even if the previous has
3811 * not yet hit the screen.
3812 */
3813 if (surface->frame_cb) {
3814 if (!surface->window->redraw_needed)
3815 return;
3816
Pekka Paalanen71233882013-04-25 13:57:53 +03003817 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003818 wl_callback_destroy(surface->frame_cb);
3819 }
3820
3821 surface->frame_cb = wl_surface_frame(surface->surface);
3822 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003823 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003824
3825 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003826 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003827 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003828 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003829}
3830
3831static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003832idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003833{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003834 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003835 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003836
Pekka Paalanen71233882013-04-25 13:57:53 +03003837 DBG(" --------- \n");
3838
Pekka Paalaneneebff542013-04-25 13:57:52 +03003839 wl_list_init(&window->redraw_task.link);
3840 window->redraw_task_scheduled = 0;
3841
3842 if (window->resize_needed) {
3843 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003844 if (window->main_surface->frame_cb) {
3845 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003846 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003847 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003848
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003849 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003850 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003851
Pekka Paalanen35e82632013-04-25 13:57:48 +03003852 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003853 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003854
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003855 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003856 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003857
3858 wl_list_for_each(surface, &window->subsurface_list, link)
3859 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003860}
3861
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003862static void
3863window_schedule_redraw_task(struct window *window)
3864{
3865 if (window->configure_requests)
3866 return;
3867 if (!window->redraw_task_scheduled) {
3868 window->redraw_task.run = idle_redraw;
3869 display_defer(window->display, &window->redraw_task);
3870 window->redraw_task_scheduled = 1;
3871 }
3872}
3873
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003874void
3875window_schedule_redraw(struct window *window)
3876{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003877 struct surface *surface;
3878
Pekka Paalanen71233882013-04-25 13:57:53 +03003879 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3880
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003881 wl_list_for_each(surface, &window->subsurface_list, link)
3882 surface->redraw_needed = 1;
3883
3884 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003885}
3886
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003887int
3888window_is_fullscreen(struct window *window)
3889{
3890 return window->type == TYPE_FULLSCREEN;
3891}
3892
MoD063a8822013-03-02 23:43:57 +00003893static void
3894configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3895{
3896 struct window *window = data;
3897
3898 wl_callback_destroy(callback);
3899 window->configure_requests--;
3900
3901 if (!window->configure_requests)
3902 window_schedule_redraw(window);
3903}
3904
3905static struct wl_callback_listener configure_request_listener = {
3906 configure_request_completed,
3907};
3908
3909static void
3910window_defer_redraw_until_configure(struct window* window)
3911{
3912 struct wl_callback *callback;
3913
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003914 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003915 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003916 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003917 }
3918
3919 callback = wl_display_sync(window->display->display);
3920 wl_callback_add_listener(callback, &configure_request_listener, window);
3921 window->configure_requests++;
3922}
3923
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003924void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003925window_set_fullscreen(struct window *window, int fullscreen)
3926{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003927 if (!window->display->shell)
3928 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003929
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003930 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003931 return;
3932
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003933 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003934 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003935 if (window->type == TYPE_TOPLEVEL) {
3936 window->saved_allocation = window->main_surface->allocation;
3937 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003938 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003939 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003940 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003941 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003942 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003943 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003944 if (window->saved_type == TYPE_MAXIMIZED) {
3945 window_set_maximized(window, 1);
3946 } else {
3947 window->type = TYPE_TOPLEVEL;
3948 wl_shell_surface_set_toplevel(window->shell_surface);
3949 window_schedule_resize(window,
3950 window->saved_allocation.width,
3951 window->saved_allocation.height);
3952 }
3953
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003954 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003955}
3956
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003957void
3958window_set_fullscreen_method(struct window *window,
3959 enum wl_shell_surface_fullscreen_method method)
3960{
3961 window->fullscreen_method = method;
3962}
3963
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003964int
3965window_is_maximized(struct window *window)
3966{
3967 return window->type == TYPE_MAXIMIZED;
3968}
3969
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003970void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003971window_set_maximized(struct window *window, int maximized)
3972{
3973 if (!window->display->shell)
3974 return;
3975
3976 if ((window->type == TYPE_MAXIMIZED) == maximized)
3977 return;
3978
3979 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003980 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003981 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3982 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003983 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003984 } else if (window->type == TYPE_FULLSCREEN) {
3985 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3986 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003987 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003988 } else {
3989 wl_shell_surface_set_toplevel(window->shell_surface);
3990 window->type = TYPE_TOPLEVEL;
3991 window_schedule_resize(window,
3992 window->saved_allocation.width,
3993 window->saved_allocation.height);
3994 }
3995}
3996
3997void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003998window_set_user_data(struct window *window, void *data)
3999{
4000 window->user_data = data;
4001}
4002
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004003void *
4004window_get_user_data(struct window *window)
4005{
4006 return window->user_data;
4007}
4008
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004009void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004010window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004011 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004012{
4013 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004014}
4015
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004016void
4017window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004018 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004019{
4020 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004021}
4022
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004023void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004024window_set_data_handler(struct window *window, window_data_handler_t handler)
4025{
4026 window->data_handler = handler;
4027}
4028
4029void
4030window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4031{
4032 window->drop_handler = handler;
4033}
4034
4035void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004036window_set_close_handler(struct window *window,
4037 window_close_handler_t handler)
4038{
4039 window->close_handler = handler;
4040}
4041
4042void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004043window_set_fullscreen_handler(struct window *window,
4044 window_fullscreen_handler_t handler)
4045{
4046 window->fullscreen_handler = handler;
4047}
4048
4049void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004050window_set_output_handler(struct window *window,
4051 window_output_handler_t handler)
4052{
4053 window->output_handler = handler;
4054}
4055
4056void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004057window_set_title(struct window *window, const char *title)
4058{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004059 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004060 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004061 if (window->shell_surface)
4062 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004063}
4064
4065const char *
4066window_get_title(struct window *window)
4067{
4068 return window->title;
4069}
4070
4071void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004072window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4073{
4074 struct text_cursor_position *text_cursor_position =
4075 window->display->text_cursor_position;
4076
Scott Moreau9295ce02012-06-01 12:46:10 -06004077 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004078 return;
4079
4080 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004081 window->main_surface->surface,
4082 wl_fixed_from_int(x),
4083 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004084}
4085
4086void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004087window_damage(struct window *window, int32_t x, int32_t y,
4088 int32_t width, int32_t height)
4089{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004090 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004091}
4092
Casey Dahlin9074db52012-04-19 22:50:09 -04004093static void
4094surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004095 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004096{
Rob Bradford7507b572012-05-15 17:55:34 +01004097 struct window *window = data;
4098 struct output *output;
4099 struct output *output_found = NULL;
4100 struct window_output *window_output;
4101
4102 wl_list_for_each(output, &window->display->output_list, link) {
4103 if (output->output == wl_output) {
4104 output_found = output;
4105 break;
4106 }
4107 }
4108
4109 if (!output_found)
4110 return;
4111
4112 window_output = malloc (sizeof *window_output);
4113 window_output->output = output_found;
4114
4115 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004116
4117 if (window->output_handler)
4118 window->output_handler(window, output_found, 1,
4119 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004120}
4121
4122static void
4123surface_leave(void *data,
4124 struct wl_surface *wl_surface, struct wl_output *output)
4125{
Rob Bradford7507b572012-05-15 17:55:34 +01004126 struct window *window = data;
4127 struct window_output *window_output;
4128 struct window_output *window_output_found = NULL;
4129
4130 wl_list_for_each(window_output, &window->window_output_list, link) {
4131 if (window_output->output->output == output) {
4132 window_output_found = window_output;
4133 break;
4134 }
4135 }
4136
4137 if (window_output_found) {
4138 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004139
4140 if (window->output_handler)
4141 window->output_handler(window, window_output->output,
4142 0, window->user_data);
4143
Rob Bradford7507b572012-05-15 17:55:34 +01004144 free(window_output_found);
4145 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004146}
4147
4148static const struct wl_surface_listener surface_listener = {
4149 surface_enter,
4150 surface_leave
4151};
4152
Pekka Paalanen4e373742013-02-13 16:17:13 +02004153static struct surface *
4154surface_create(struct window *window)
4155{
4156 struct display *display = window->display;
4157 struct surface *surface;
4158
4159 surface = calloc(1, sizeof *surface);
4160 if (!surface)
4161 return NULL;
4162
4163 surface->window = window;
4164 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004165 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004166 wl_surface_add_listener(surface->surface, &surface_listener, window);
4167
Pekka Paalanen35e82632013-04-25 13:57:48 +03004168 wl_list_insert(&window->subsurface_list, &surface->link);
4169
Pekka Paalanen4e373742013-02-13 16:17:13 +02004170 return surface;
4171}
4172
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004173static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004174window_create_internal(struct display *display,
4175 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004176{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004177 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004178 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004179
4180 window = malloc(sizeof *window);
4181 if (window == NULL)
4182 return NULL;
4183
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004184 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004185 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004186 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004187 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004188
4189 surface = surface_create(window);
4190 window->main_surface = surface;
4191
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004192 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004193 window->shell_surface =
4194 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004195 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004196 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004197
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004198 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004199 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004200 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004201
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004202 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004203#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004204 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004205#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004206 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004207#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004208 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004209 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004210
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004211 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004212 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004213 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004214
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004215 if (window->shell_surface) {
4216 wl_shell_surface_set_user_data(window->shell_surface, window);
4217 wl_shell_surface_add_listener(window->shell_surface,
4218 &shell_surface_listener, window);
4219 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004220
Rob Bradford7507b572012-05-15 17:55:34 +01004221 wl_list_init (&window->window_output_list);
4222
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004223 return window;
4224}
4225
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004226struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004227window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004228{
4229 struct window *window;
4230
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004231 window = window_create_internal(display, NULL, TYPE_NONE);
4232 if (!window)
4233 return NULL;
4234
4235 return window;
4236}
4237
4238struct window *
4239window_create_custom(struct display *display)
4240{
4241 struct window *window;
4242
4243 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004244 if (!window)
4245 return NULL;
4246
4247 return window;
4248}
4249
4250struct window *
4251window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004252 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004253{
4254 struct window *window;
4255
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004256 window = window_create_internal(parent->display,
4257 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004258 if (!window)
4259 return NULL;
4260
4261 window->x = x;
4262 window->y = y;
4263
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004264 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004265 wl_shell_surface_set_transient(
4266 window->shell_surface,
4267 window->parent->main_surface->surface,
4268 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004269
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004270 return window;
4271}
4272
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004273static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004274menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004275{
4276 int next;
4277
4278 next = (sy - 8) / 20;
4279 if (menu->current != next) {
4280 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004281 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004282 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004283}
4284
4285static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004286menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004287 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004288 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004289{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004290 struct menu *menu = data;
4291
4292 if (widget == menu->widget)
4293 menu_set_item(data, y);
4294
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004295 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004296}
4297
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004298static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004299menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004300 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004301{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004302 struct menu *menu = data;
4303
4304 if (widget == menu->widget)
4305 menu_set_item(data, y);
4306
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004307 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004308}
4309
4310static void
4311menu_leave_handler(struct widget *widget, struct input *input, void *data)
4312{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004313 struct menu *menu = data;
4314
4315 if (widget == menu->widget)
4316 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004317}
4318
4319static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004320menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004321 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004322 uint32_t button, enum wl_pointer_button_state state,
4323 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004324
4325{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004326 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004327
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004328 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4329 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004330 /* Either relase after press-drag-release or
4331 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004332 menu->func(menu->window->parent,
4333 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004334 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004335 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004336 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004337 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004338 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004339}
4340
4341static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004342menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004343{
4344 cairo_t *cr;
4345 const int32_t r = 3, margin = 3;
4346 struct menu *menu = data;
4347 int32_t width, height, i;
4348
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004349 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004350 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4351 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4352 cairo_paint(cr);
4353
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004354 width = widget->allocation.width;
4355 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004356 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004357
4358 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004359 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4360 cairo_fill(cr);
4361
4362 for (i = 0; i < menu->count; i++) {
4363 if (i == menu->current) {
4364 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4365 cairo_rectangle(cr, margin, i * 20 + margin,
4366 width - 2 * margin, 20);
4367 cairo_fill(cr);
4368 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4369 cairo_move_to(cr, 10, i * 20 + 16);
4370 cairo_show_text(cr, menu->entries[i]);
4371 } else {
4372 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4373 cairo_move_to(cr, 10, i * 20 + 16);
4374 cairo_show_text(cr, menu->entries[i]);
4375 }
4376 }
4377
4378 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004379}
4380
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004381void
4382window_show_menu(struct display *display,
4383 struct input *input, uint32_t time, struct window *parent,
4384 int32_t x, int32_t y,
4385 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004386{
4387 struct window *window;
4388 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004389 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004390
4391 menu = malloc(sizeof *menu);
4392 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004393 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004394
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004395 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004396 if (!window) {
4397 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004398 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004399 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004400
4401 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004402 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004403 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4404 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004405 menu->entries = entries;
4406 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004407 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004408 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004409 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004410 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004411 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004412 window->type = TYPE_MENU;
4413 window->x = x;
4414 window->y = y;
4415
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004416 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004417 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004418 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004419 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004420 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004421
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004422 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004423 widget_set_enter_handler(menu->widget, menu_enter_handler);
4424 widget_set_leave_handler(menu->widget, menu_leave_handler);
4425 widget_set_motion_handler(menu->widget, menu_motion_handler);
4426 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004427
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004428 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004429 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004430}
4431
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004432void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004433window_set_buffer_type(struct window *window, enum window_buffer_type type)
4434{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004435 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004436}
4437
Pekka Paalanen35e82632013-04-25 13:57:48 +03004438struct widget *
4439window_add_subsurface(struct window *window, void *data,
4440 enum subsurface_mode default_mode)
4441{
4442 struct widget *widget;
4443 struct surface *surface;
4444 struct wl_surface *parent;
4445 struct wl_subcompositor *subcompo = window->display->subcompositor;
4446
4447 if (!subcompo)
4448 return NULL;
4449
4450 surface = surface_create(window);
4451 widget = widget_create(window, surface, data);
4452 wl_list_init(&widget->link);
4453 surface->widget = widget;
4454
4455 parent = window->main_surface->surface;
4456 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4457 surface->surface,
4458 parent);
4459 surface->synchronized = 1;
4460
4461 switch (default_mode) {
4462 case SUBSURFACE_SYNCHRONIZED:
4463 surface->synchronized_default = 1;
4464 break;
4465 case SUBSURFACE_DESYNCHRONIZED:
4466 surface->synchronized_default = 0;
4467 break;
4468 default:
4469 assert(!"bad enum subsurface_mode");
4470 }
4471
4472 return widget;
4473}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004474
4475static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004476display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004477 struct wl_output *wl_output,
4478 int x, int y,
4479 int physical_width,
4480 int physical_height,
4481 int subpixel,
4482 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004483 const char *model,
4484 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004485{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004486 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004487
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004488 output->allocation.x = x;
4489 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004490 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004491}
4492
4493static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004494display_handle_done(void *data,
4495 struct wl_output *wl_output)
4496{
4497}
4498
4499static void
4500display_handle_scale(void *data,
4501 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004502 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004503{
4504 struct output *output = data;
4505
4506 output->scale = scale;
4507}
4508
4509static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004510display_handle_mode(void *data,
4511 struct wl_output *wl_output,
4512 uint32_t flags,
4513 int width,
4514 int height,
4515 int refresh)
4516{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004517 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004518 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004519
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004520 if (flags & WL_OUTPUT_MODE_CURRENT) {
4521 output->allocation.width = width;
4522 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004523 if (display->output_configure_handler)
4524 (*display->output_configure_handler)(
4525 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004526 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004527}
4528
4529static const struct wl_output_listener output_listener = {
4530 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004531 display_handle_mode,
4532 display_handle_done,
4533 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004534};
4535
4536static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004537display_add_output(struct display *d, uint32_t id)
4538{
4539 struct output *output;
4540
4541 output = malloc(sizeof *output);
4542 if (output == NULL)
4543 return;
4544
4545 memset(output, 0, sizeof *output);
4546 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004547 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004548 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004549 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004550 wl_list_insert(d->output_list.prev, &output->link);
4551
4552 wl_output_add_listener(output->output, &output_listener, output);
4553}
4554
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004555static void
4556output_destroy(struct output *output)
4557{
4558 if (output->destroy_handler)
4559 (*output->destroy_handler)(output, output->user_data);
4560
4561 wl_output_destroy(output->output);
4562 wl_list_remove(&output->link);
4563 free(output);
4564}
4565
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004566void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004567display_set_global_handler(struct display *display,
4568 display_global_handler_t handler)
4569{
4570 struct global *global;
4571
4572 display->global_handler = handler;
4573 if (!handler)
4574 return;
4575
4576 wl_list_for_each(global, &display->global_list, link)
4577 display->global_handler(display,
4578 global->name, global->interface,
4579 global->version, display->user_data);
4580}
4581
4582void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004583display_set_output_configure_handler(struct display *display,
4584 display_output_handler_t handler)
4585{
4586 struct output *output;
4587
4588 display->output_configure_handler = handler;
4589 if (!handler)
4590 return;
4591
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004592 wl_list_for_each(output, &display->output_list, link) {
4593 if (output->allocation.width == 0 &&
4594 output->allocation.height == 0)
4595 continue;
4596
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004597 (*display->output_configure_handler)(output,
4598 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004599 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004600}
4601
4602void
4603output_set_user_data(struct output *output, void *data)
4604{
4605 output->user_data = data;
4606}
4607
4608void *
4609output_get_user_data(struct output *output)
4610{
4611 return output->user_data;
4612}
4613
4614void
4615output_set_destroy_handler(struct output *output,
4616 display_output_handler_t handler)
4617{
4618 output->destroy_handler = handler;
4619 /* FIXME: implement this, once we have way to remove outputs */
4620}
4621
4622void
Scott Moreau4e072362012-09-29 02:03:11 -06004623output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004624{
Scott Moreau4e072362012-09-29 02:03:11 -06004625 struct rectangle allocation = output->allocation;
4626
4627 switch (output->transform) {
4628 case WL_OUTPUT_TRANSFORM_90:
4629 case WL_OUTPUT_TRANSFORM_270:
4630 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4631 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4632 /* Swap width and height */
4633 allocation.width = output->allocation.height;
4634 allocation.height = output->allocation.width;
4635 break;
4636 }
4637
4638 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004639}
4640
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004641struct wl_output *
4642output_get_wl_output(struct output *output)
4643{
4644 return output->output;
4645}
4646
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004647enum wl_output_transform
4648output_get_transform(struct output *output)
4649{
4650 return output->transform;
4651}
4652
Alexander Larssonafd319a2013-05-22 14:41:27 +02004653uint32_t
4654output_get_scale(struct output *output)
4655{
4656 return output->scale;
4657}
4658
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004659static void
Daniel Stone97f68542012-05-30 16:32:01 +01004660fini_xkb(struct input *input)
4661{
4662 xkb_state_unref(input->xkb.state);
4663 xkb_map_unref(input->xkb.keymap);
4664}
4665
4666static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004667display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004668{
4669 struct input *input;
4670
4671 input = malloc(sizeof *input);
4672 if (input == NULL)
4673 return;
4674
4675 memset(input, 0, sizeof *input);
4676 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004677 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004678 input->pointer_focus = NULL;
4679 input->keyboard_focus = NULL;
4680 wl_list_insert(d->input_list.prev, &input->link);
4681
Daniel Stone37816df2012-05-16 18:45:18 +01004682 wl_seat_add_listener(input->seat, &seat_listener, input);
4683 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004684
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004685 input->data_device =
4686 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004687 input->seat);
4688 wl_data_device_add_listener(input->data_device, &data_device_listener,
4689 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004690
4691 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004692
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004693 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4694 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004695 input->repeat_task.run = keyboard_repeat_func;
4696 display_watch_fd(d, input->repeat_timer_fd,
4697 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004698}
4699
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004700static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004701input_destroy(struct input *input)
4702{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004703 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004704 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004705
4706 if (input->drag_offer)
4707 data_offer_destroy(input->drag_offer);
4708
4709 if (input->selection_offer)
4710 data_offer_destroy(input->selection_offer);
4711
4712 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004713 fini_xkb(input);
4714
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004715 wl_surface_destroy(input->pointer_surface);
4716
Pekka Paalanene1207c72011-12-16 12:02:09 +02004717 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004718 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004719 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004720 free(input);
4721}
4722
4723static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004724init_workspace_manager(struct display *d, uint32_t id)
4725{
4726 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004727 wl_registry_bind(d->registry, id,
4728 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004729 if (d->workspace_manager != NULL)
4730 workspace_manager_add_listener(d->workspace_manager,
4731 &workspace_manager_listener,
4732 d);
4733}
4734
4735static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004736registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4737 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004738{
4739 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004740 struct global *global;
4741
4742 global = malloc(sizeof *global);
4743 global->name = id;
4744 global->interface = strdup(interface);
4745 global->version = version;
4746 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004747
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004748 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004749 d->compositor = wl_registry_bind(registry, id,
4750 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004751 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004752 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004753 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004754 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004755 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004756 d->shell = wl_registry_bind(registry,
4757 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004758 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004759 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004760 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4761 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004762 wl_registry_bind(registry, id,
4763 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004764 } else if (strcmp(interface, "text_cursor_position") == 0) {
4765 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004766 wl_registry_bind(registry, id,
4767 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004768 } else if (strcmp(interface, "workspace_manager") == 0) {
4769 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004770 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4771 d->subcompositor =
4772 wl_registry_bind(registry, id,
4773 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004774 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004775
4776 if (d->global_handler)
4777 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004778}
4779
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004780static void
4781registry_handle_global_remove(void *data, struct wl_registry *registry,
4782 uint32_t name)
4783{
4784 struct display *d = data;
4785 struct global *global;
4786 struct global *tmp;
4787
4788 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4789 if (global->name != name)
4790 continue;
4791
4792 /* XXX: Should destroy bound globals, and call
4793 * the counterpart of display::global_handler
4794 */
4795 wl_list_remove(&global->link);
4796 free(global->interface);
4797 free(global);
4798 }
4799}
4800
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004801void *
4802display_bind(struct display *display, uint32_t name,
4803 const struct wl_interface *interface, uint32_t version)
4804{
4805 return wl_registry_bind(display->registry, name, interface, version);
4806}
4807
4808static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004809 registry_handle_global,
4810 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004811};
4812
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004813#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004814static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004815init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004816{
4817 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004818 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004819
Rob Clark6396ed32012-03-11 19:48:41 -05004820#ifdef USE_CAIRO_GLESV2
4821# define GL_BIT EGL_OPENGL_ES2_BIT
4822#else
4823# define GL_BIT EGL_OPENGL_BIT
4824#endif
4825
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004826 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004827 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004828 EGL_RED_SIZE, 1,
4829 EGL_GREEN_SIZE, 1,
4830 EGL_BLUE_SIZE, 1,
4831 EGL_ALPHA_SIZE, 1,
4832 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004833 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004834 EGL_NONE
4835 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004836
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004837#ifdef USE_CAIRO_GLESV2
4838 static const EGLint context_attribs[] = {
4839 EGL_CONTEXT_CLIENT_VERSION, 2,
4840 EGL_NONE
4841 };
4842 EGLint api = EGL_OPENGL_ES_API;
4843#else
4844 EGLint *context_attribs = NULL;
4845 EGLint api = EGL_OPENGL_API;
4846#endif
4847
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004848 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004849 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004850 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004851 return -1;
4852 }
4853
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004854 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004855 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004856 return -1;
4857 }
4858
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004859 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4860 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004861 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004862 return -1;
4863 }
4864
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004865 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004866 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004867 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004868 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004869 return -1;
4870 }
4871
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004872 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004873 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004874 return -1;
4875 }
4876
Benjamin Franzke0c991632011-09-27 21:57:31 +02004877 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4878 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004879 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004880 return -1;
4881 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004882
4883 return 0;
4884}
4885
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004886static void
4887fini_egl(struct display *display)
4888{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004889 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004890
4891 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4892 EGL_NO_CONTEXT);
4893
4894 eglTerminate(display->dpy);
4895 eglReleaseThread();
4896}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004897#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004898
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004899static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004900init_dummy_surface(struct display *display)
4901{
4902 int len;
4903 void *data;
4904
4905 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4906 data = malloc(len);
4907 display->dummy_surface =
4908 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4909 1, 1, len);
4910 display->dummy_surface_data = data;
4911}
4912
4913static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004914handle_display_data(struct task *task, uint32_t events)
4915{
4916 struct display *display =
4917 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004918 struct epoll_event ep;
4919 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004920
4921 display->display_fd_events = events;
4922
4923 if (events & EPOLLERR || events & EPOLLHUP) {
4924 display_exit(display);
4925 return;
4926 }
4927
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004928 if (events & EPOLLIN) {
4929 ret = wl_display_dispatch(display->display);
4930 if (ret == -1) {
4931 display_exit(display);
4932 return;
4933 }
4934 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004935
4936 if (events & EPOLLOUT) {
4937 ret = wl_display_flush(display->display);
4938 if (ret == 0) {
4939 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4940 ep.data.ptr = &display->display_task;
4941 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4942 display->display_fd, &ep);
4943 } else if (ret == -1 && errno != EAGAIN) {
4944 display_exit(display);
4945 return;
4946 }
4947 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004948}
4949
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004950static void
4951log_handler(const char *format, va_list args)
4952{
4953 vfprintf(stderr, format, args);
4954}
4955
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004956struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004957display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004958{
4959 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004960
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004961 wl_log_set_handler_client(log_handler);
4962
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004963 d = malloc(sizeof *d);
4964 if (d == NULL)
4965 return NULL;
4966
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004967 memset(d, 0, sizeof *d);
4968
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004969 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004970 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004971 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004972 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004973 return NULL;
4974 }
4975
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004976 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004977 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004978 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004979 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4980 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004981
4982 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004983 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004984 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004985 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004986
Daniel Stone97f68542012-05-30 16:32:01 +01004987 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004988 if (d->xkb_context == NULL) {
4989 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004990 return NULL;
4991 }
4992
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004993 d->workspace = 0;
4994 d->workspace_count = 1;
4995
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004996 d->registry = wl_display_get_registry(d->display);
4997 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004998
4999 if (wl_display_dispatch(d->display) < 0) {
5000 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5001 return NULL;
5002 }
5003
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005004#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005005 if (init_egl(d) < 0)
5006 fprintf(stderr, "EGL does not seem to work, "
5007 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005008#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005009
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005010 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005011
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005012 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005013
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005014 wl_list_init(&d->window_list);
5015
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005016 init_dummy_surface(d);
5017
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005018 return d;
5019}
5020
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005021static void
5022display_destroy_outputs(struct display *display)
5023{
5024 struct output *tmp;
5025 struct output *output;
5026
5027 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5028 output_destroy(output);
5029}
5030
Pekka Paalanene1207c72011-12-16 12:02:09 +02005031static void
5032display_destroy_inputs(struct display *display)
5033{
5034 struct input *tmp;
5035 struct input *input;
5036
5037 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5038 input_destroy(input);
5039}
5040
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005041void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005042display_destroy(struct display *display)
5043{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005044 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005045 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5046 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005047
5048 if (!wl_list_empty(&display->deferred_list))
5049 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5050
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005051 cairo_surface_destroy(display->dummy_surface);
5052 free(display->dummy_surface_data);
5053
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005054 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005055 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005056
Daniel Stone97f68542012-05-30 16:32:01 +01005057 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005058
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005059 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005060 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005061
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005062#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005063 if (display->argb_device)
5064 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005065#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005066
Pekka Paalanen35e82632013-04-25 13:57:48 +03005067 if (display->subcompositor)
5068 wl_subcompositor_destroy(display->subcompositor);
5069
Pekka Paalanenc2052982011-12-16 11:41:32 +02005070 if (display->shell)
5071 wl_shell_destroy(display->shell);
5072
5073 if (display->shm)
5074 wl_shm_destroy(display->shm);
5075
5076 if (display->data_device_manager)
5077 wl_data_device_manager_destroy(display->data_device_manager);
5078
5079 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005080 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005081
5082 close(display->epoll_fd);
5083
U. Artie Eoff44874d92012-10-02 21:12:35 -07005084 if (!(display->display_fd_events & EPOLLERR) &&
5085 !(display->display_fd_events & EPOLLHUP))
5086 wl_display_flush(display->display);
5087
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005088 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005089 free(display);
5090}
5091
5092void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005093display_set_user_data(struct display *display, void *data)
5094{
5095 display->user_data = data;
5096}
5097
5098void *
5099display_get_user_data(struct display *display)
5100{
5101 return display->user_data;
5102}
5103
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005104struct wl_display *
5105display_get_display(struct display *display)
5106{
5107 return display->display;
5108}
5109
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005110cairo_device_t *
5111display_get_cairo_device(struct display *display)
5112{
5113 return display->argb_device;
5114}
5115
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005116struct output *
5117display_get_output(struct display *display)
5118{
5119 return container_of(display->output_list.next, struct output, link);
5120}
5121
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005122struct wl_compositor *
5123display_get_compositor(struct display *display)
5124{
5125 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005126}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005127
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005128uint32_t
5129display_get_serial(struct display *display)
5130{
5131 return display->serial;
5132}
5133
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005134EGLDisplay
5135display_get_egl_display(struct display *d)
5136{
5137 return d->dpy;
5138}
5139
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005140struct wl_data_source *
5141display_create_data_source(struct display *display)
5142{
5143 return wl_data_device_manager_create_data_source(display->data_device_manager);
5144}
5145
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005146EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005147display_get_argb_egl_config(struct display *d)
5148{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005149 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005150}
5151
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005152struct wl_shell *
5153display_get_shell(struct display *display)
5154{
5155 return display->shell;
5156}
5157
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005158int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005159display_acquire_window_surface(struct display *display,
5160 struct window *window,
5161 EGLContext ctx)
5162{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005163 struct surface *surface = window->main_surface;
5164
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005165 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005166 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005167
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005168 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005169 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005170}
5171
5172void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005173display_release_window_surface(struct display *display,
5174 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005175{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005176 struct surface *surface = window->main_surface;
5177
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005178 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005179 return;
5180
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005181 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005182}
5183
5184void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005185display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005186{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005187 wl_list_insert(&display->deferred_list, &task->link);
5188}
5189
5190void
5191display_watch_fd(struct display *display,
5192 int fd, uint32_t events, struct task *task)
5193{
5194 struct epoll_event ep;
5195
5196 ep.events = events;
5197 ep.data.ptr = task;
5198 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5199}
5200
5201void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005202display_unwatch_fd(struct display *display, int fd)
5203{
5204 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5205}
5206
5207void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005208display_run(struct display *display)
5209{
5210 struct task *task;
5211 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005212 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005213
Pekka Paalanen826d7952011-12-15 10:14:07 +02005214 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005215 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005216 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005217 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005218 struct task, link);
5219 wl_list_remove(&task->link);
5220 task->run(task, 0);
5221 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005222
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005223 wl_display_dispatch_pending(display->display);
5224
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005225 if (!display->running)
5226 break;
5227
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005228 ret = wl_display_flush(display->display);
5229 if (ret < 0 && errno == EAGAIN) {
5230 ep[0].events =
5231 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5232 ep[0].data.ptr = &display->display_task;
5233
5234 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5235 display->display_fd, &ep[0]);
5236 } else if (ret < 0) {
5237 break;
5238 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005239
5240 count = epoll_wait(display->epoll_fd,
5241 ep, ARRAY_LENGTH(ep), -1);
5242 for (i = 0; i < count; i++) {
5243 task = ep[i].data.ptr;
5244 task->run(task, ep[i].events);
5245 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005246 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005247}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005248
5249void
5250display_exit(struct display *display)
5251{
5252 display->running = 0;
5253}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005254
5255void
5256keysym_modifiers_add(struct wl_array *modifiers_map,
5257 const char *name)
5258{
5259 size_t len = strlen(name) + 1;
5260 char *p;
5261
5262 p = wl_array_add(modifiers_map, len);
5263
5264 if (p == NULL)
5265 return;
5266
5267 strncpy(p, name, len);
5268}
5269
5270static xkb_mod_index_t
5271keysym_modifiers_get_index(struct wl_array *modifiers_map,
5272 const char *name)
5273{
5274 xkb_mod_index_t index = 0;
5275 char *p = modifiers_map->data;
5276
5277 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5278 if (strcmp(p, name) == 0)
5279 return index;
5280
5281 index++;
5282 p += strlen(p) + 1;
5283 }
5284
5285 return XKB_MOD_INVALID;
5286}
5287
5288xkb_mod_mask_t
5289keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5290 const char *name)
5291{
5292 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5293
5294 if (index == XKB_MOD_INVALID)
5295 return XKB_MOD_INVALID;
5296
5297 return 1 << index;
5298}