blob: c8e421b312bdbb6673e00b7462a67b76c70d1af4 [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
Matt Ropere61561f2013-06-24 16:52:43 +01003070 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3071 if (!input->xkb.keymap)
3072 return;
3073
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003074 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3075 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003076 mask = xkb_state_serialize_mods(input->xkb.state,
3077 XKB_STATE_DEPRESSED |
3078 XKB_STATE_LATCHED);
3079 input->modifiers = 0;
3080 if (mask & input->xkb.control_mask)
3081 input->modifiers |= MOD_CONTROL_MASK;
3082 if (mask & input->xkb.alt_mask)
3083 input->modifiers |= MOD_ALT_MASK;
3084 if (mask & input->xkb.shift_mask)
3085 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003086}
3087
Daniel Stone37816df2012-05-16 18:45:18 +01003088static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003089 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003090 keyboard_handle_enter,
3091 keyboard_handle_leave,
3092 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003093 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003094};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003095
3096static void
Daniel Stone37816df2012-05-16 18:45:18 +01003097seat_handle_capabilities(void *data, struct wl_seat *seat,
3098 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003099{
Daniel Stone37816df2012-05-16 18:45:18 +01003100 struct input *input = data;
3101
3102 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3103 input->pointer = wl_seat_get_pointer(seat);
3104 wl_pointer_set_user_data(input->pointer, input);
3105 wl_pointer_add_listener(input->pointer, &pointer_listener,
3106 input);
3107 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3108 wl_pointer_destroy(input->pointer);
3109 input->pointer = NULL;
3110 }
3111
3112 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3113 input->keyboard = wl_seat_get_keyboard(seat);
3114 wl_keyboard_set_user_data(input->keyboard, input);
3115 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3116 input);
3117 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3118 wl_keyboard_destroy(input->keyboard);
3119 input->keyboard = NULL;
3120 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003121}
3122
Daniel Stone37816df2012-05-16 18:45:18 +01003123static const struct wl_seat_listener seat_listener = {
3124 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003125};
3126
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003127void
3128input_get_position(struct input *input, int32_t *x, int32_t *y)
3129{
3130 *x = input->sx;
3131 *y = input->sy;
3132}
3133
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003134struct display *
3135input_get_display(struct input *input)
3136{
3137 return input->display;
3138}
3139
Daniel Stone37816df2012-05-16 18:45:18 +01003140struct wl_seat *
3141input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003142{
Daniel Stone37816df2012-05-16 18:45:18 +01003143 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003144}
3145
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003146uint32_t
3147input_get_modifiers(struct input *input)
3148{
3149 return input->modifiers;
3150}
3151
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003152struct widget *
3153input_get_focus_widget(struct input *input)
3154{
3155 return input->focus_widget;
3156}
3157
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003158struct data_offer {
3159 struct wl_data_offer *offer;
3160 struct input *input;
3161 struct wl_array types;
3162 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003163
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003164 struct task io_task;
3165 int fd;
3166 data_func_t func;
3167 int32_t x, y;
3168 void *user_data;
3169};
3170
3171static void
3172data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3173{
3174 struct data_offer *offer = data;
3175 char **p;
3176
3177 p = wl_array_add(&offer->types, sizeof *p);
3178 *p = strdup(type);
3179}
3180
3181static const struct wl_data_offer_listener data_offer_listener = {
3182 data_offer_offer,
3183};
3184
3185static void
3186data_offer_destroy(struct data_offer *offer)
3187{
3188 char **p;
3189
3190 offer->refcount--;
3191 if (offer->refcount == 0) {
3192 wl_data_offer_destroy(offer->offer);
3193 for (p = offer->types.data; *p; p++)
3194 free(*p);
3195 wl_array_release(&offer->types);
3196 free(offer);
3197 }
3198}
3199
3200static void
3201data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003202 struct wl_data_device *data_device,
3203 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003204{
3205 struct data_offer *offer;
3206
3207 offer = malloc(sizeof *offer);
3208
3209 wl_array_init(&offer->types);
3210 offer->refcount = 1;
3211 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003212 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003213 wl_data_offer_add_listener(offer->offer,
3214 &data_offer_listener, offer);
3215}
3216
3217static void
3218data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003219 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003220 wl_fixed_t x_w, wl_fixed_t y_w,
3221 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003222{
3223 struct input *input = data;
3224 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003225 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003226 float x = wl_fixed_to_double(x_w);
3227 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003228 char **p;
3229
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003230 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003231 window = wl_surface_get_user_data(surface);
3232 input->pointer_focus = window;
3233
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003234 if (offer) {
3235 input->drag_offer = wl_data_offer_get_user_data(offer);
3236
3237 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3238 *p = NULL;
3239
3240 types_data = input->drag_offer->types.data;
3241 } else {
3242 input->drag_offer = NULL;
3243 types_data = NULL;
3244 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003245
3246 window = input->pointer_focus;
3247 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003248 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003249 window->user_data);
3250}
3251
3252static void
3253data_device_leave(void *data, struct wl_data_device *data_device)
3254{
3255 struct input *input = data;
3256
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003257 if (input->drag_offer) {
3258 data_offer_destroy(input->drag_offer);
3259 input->drag_offer = NULL;
3260 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003261}
3262
3263static void
3264data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003265 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003266{
3267 struct input *input = data;
3268 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003269 float x = wl_fixed_to_double(x_w);
3270 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003271 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003272
3273 input->sx = x;
3274 input->sy = y;
3275
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003276 if (input->drag_offer)
3277 types_data = input->drag_offer->types.data;
3278 else
3279 types_data = NULL;
3280
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003281 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003282 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003283 window->user_data);
3284}
3285
3286static void
3287data_device_drop(void *data, struct wl_data_device *data_device)
3288{
3289 struct input *input = data;
3290 struct window *window = input->pointer_focus;
3291
3292 if (window->drop_handler)
3293 window->drop_handler(window, input,
3294 input->sx, input->sy, window->user_data);
3295}
3296
3297static void
3298data_device_selection(void *data,
3299 struct wl_data_device *wl_data_device,
3300 struct wl_data_offer *offer)
3301{
3302 struct input *input = data;
3303 char **p;
3304
3305 if (input->selection_offer)
3306 data_offer_destroy(input->selection_offer);
3307
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003308 if (offer) {
3309 input->selection_offer = wl_data_offer_get_user_data(offer);
3310 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3311 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003312 } else {
3313 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003314 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003315}
3316
3317static const struct wl_data_device_listener data_device_listener = {
3318 data_device_data_offer,
3319 data_device_enter,
3320 data_device_leave,
3321 data_device_motion,
3322 data_device_drop,
3323 data_device_selection
3324};
3325
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003326static void
3327input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003328{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003329 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003330 struct wl_cursor *cursor;
3331 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003332
Daniel Stone80972742012-11-07 17:51:39 +11003333 if (!input->pointer)
3334 return;
3335
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003336 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003337 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003338 return;
3339
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003340 if (index >= (int) cursor->image_count) {
3341 fprintf(stderr, "cursor index out of range\n");
3342 return;
3343 }
3344
3345 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003346 buffer = wl_cursor_image_get_buffer(image);
3347 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003348 return;
3349
Kristian Høgsbergae277372012-08-01 09:41:08 -04003350 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003351 input->pointer_surface,
3352 image->hotspot_x, image->hotspot_y);
3353 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3354 wl_surface_damage(input->pointer_surface, 0, 0,
3355 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003356 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003357}
3358
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003359static const struct wl_callback_listener pointer_surface_listener;
3360
3361static void
3362pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3363 uint32_t time)
3364{
3365 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003366 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003367 int i;
3368
3369 if (callback) {
3370 assert(callback == input->cursor_frame_cb);
3371 wl_callback_destroy(callback);
3372 input->cursor_frame_cb = NULL;
3373 }
3374
Daniel Stone80972742012-11-07 17:51:39 +11003375 if (!input->pointer)
3376 return;
3377
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003378 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003379 wl_pointer_set_cursor(input->pointer,
3380 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003381 NULL, 0, 0);
3382 return;
3383 }
3384
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003385 if (input->current_cursor == CURSOR_UNSET)
3386 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003387 cursor = input->display->cursors[input->current_cursor];
3388 if (!cursor)
3389 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003390
3391 /* FIXME We don't have the current time on the first call so we set
3392 * the animation start to the time of the first frame callback. */
3393 if (time == 0)
3394 input->cursor_anim_start = 0;
3395 else if (input->cursor_anim_start == 0)
3396 input->cursor_anim_start = time;
3397
3398 if (time == 0 || input->cursor_anim_start == 0)
3399 i = 0;
3400 else
3401 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3402
Pekka Paalanenbc106382012-10-10 12:49:31 +03003403 if (cursor->image_count > 1) {
3404 input->cursor_frame_cb =
3405 wl_surface_frame(input->pointer_surface);
3406 wl_callback_add_listener(input->cursor_frame_cb,
3407 &pointer_surface_listener, input);
3408 }
3409
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003410 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003411}
3412
3413static const struct wl_callback_listener pointer_surface_listener = {
3414 pointer_surface_frame_callback
3415};
3416
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003417void
3418input_set_pointer_image(struct input *input, int pointer)
3419{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003420 int force = 0;
3421
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003422 if (!input->pointer)
3423 return;
3424
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003425 if (input->pointer_enter_serial > input->cursor_serial)
3426 force = 1;
3427
3428 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003429 return;
3430
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003431 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003432 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003433 if (!input->cursor_frame_cb)
3434 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003435 else if (force) {
3436 /* The current frame callback may be stuck if, for instance,
3437 * the set cursor request was processed by the server after
3438 * this client lost the focus. In this case the cursor surface
3439 * might not be mapped and the frame callback wouldn't ever
3440 * complete. Send a set_cursor and attach to try to map the
3441 * cursor surface again so that the callback will finish */
3442 input_set_pointer_image_index(input, 0);
3443 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003444}
3445
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003446struct wl_data_device *
3447input_get_data_device(struct input *input)
3448{
3449 return input->data_device;
3450}
3451
3452void
3453input_set_selection(struct input *input,
3454 struct wl_data_source *source, uint32_t time)
3455{
3456 wl_data_device_set_selection(input->data_device, source, time);
3457}
3458
3459void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003460input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003461{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003462 wl_data_offer_accept(input->drag_offer->offer,
3463 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003464}
3465
3466static void
3467offer_io_func(struct task *task, uint32_t events)
3468{
3469 struct data_offer *offer =
3470 container_of(task, struct data_offer, io_task);
3471 unsigned int len;
3472 char buffer[4096];
3473
3474 len = read(offer->fd, buffer, sizeof buffer);
3475 offer->func(buffer, len,
3476 offer->x, offer->y, offer->user_data);
3477
3478 if (len == 0) {
3479 close(offer->fd);
3480 data_offer_destroy(offer);
3481 }
3482}
3483
3484static void
3485data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3486 data_func_t func, void *user_data)
3487{
3488 int p[2];
3489
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003490 if (pipe2(p, O_CLOEXEC) == -1)
3491 return;
3492
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003493 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3494 close(p[1]);
3495
3496 offer->io_task.run = offer_io_func;
3497 offer->fd = p[0];
3498 offer->func = func;
3499 offer->refcount++;
3500 offer->user_data = user_data;
3501
3502 display_watch_fd(offer->input->display,
3503 offer->fd, EPOLLIN, &offer->io_task);
3504}
3505
3506void
3507input_receive_drag_data(struct input *input, const char *mime_type,
3508 data_func_t func, void *data)
3509{
3510 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3511 input->drag_offer->x = input->sx;
3512 input->drag_offer->y = input->sy;
3513}
3514
3515int
3516input_receive_selection_data(struct input *input, const char *mime_type,
3517 data_func_t func, void *data)
3518{
3519 char **p;
3520
3521 if (input->selection_offer == NULL)
3522 return -1;
3523
3524 for (p = input->selection_offer->types.data; *p; p++)
3525 if (strcmp(mime_type, *p) == 0)
3526 break;
3527
3528 if (*p == NULL)
3529 return -1;
3530
3531 data_offer_receive_data(input->selection_offer,
3532 mime_type, func, data);
3533 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003534}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003535
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003536int
3537input_receive_selection_data_to_fd(struct input *input,
3538 const char *mime_type, int fd)
3539{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003540 if (input->selection_offer)
3541 wl_data_offer_receive(input->selection_offer->offer,
3542 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003543
3544 return 0;
3545}
3546
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003547void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003548window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003549{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003550 if (!window->shell_surface)
3551 return;
3552
Daniel Stone37816df2012-05-16 18:45:18 +01003553 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003554}
3555
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003556static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003557surface_set_synchronized(struct surface *surface)
3558{
3559 if (!surface->subsurface)
3560 return;
3561
3562 if (surface->synchronized)
3563 return;
3564
3565 wl_subsurface_set_sync(surface->subsurface);
3566 surface->synchronized = 1;
3567}
3568
3569static void
3570surface_set_synchronized_default(struct surface *surface)
3571{
3572 if (!surface->subsurface)
3573 return;
3574
3575 if (surface->synchronized == surface->synchronized_default)
3576 return;
3577
3578 if (surface->synchronized_default)
3579 wl_subsurface_set_sync(surface->subsurface);
3580 else
3581 wl_subsurface_set_desync(surface->subsurface);
3582
3583 surface->synchronized = surface->synchronized_default;
3584}
3585
3586static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003587surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003588{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003589 struct widget *widget = surface->widget;
3590 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003591
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003592 if (surface->input_region) {
3593 wl_region_destroy(surface->input_region);
3594 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003595 }
3596
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003597 if (surface->opaque_region)
3598 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003599
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003600 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003601
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003602 if (widget->resize_handler)
3603 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003604 widget->allocation.width,
3605 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003606 widget->user_data);
3607
Pekka Paalanen35e82632013-04-25 13:57:48 +03003608 if (surface->subsurface &&
3609 (surface->allocation.x != widget->allocation.x ||
3610 surface->allocation.y != widget->allocation.y)) {
3611 wl_subsurface_set_position(surface->subsurface,
3612 widget->allocation.x,
3613 widget->allocation.y);
3614 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003615 if (surface->allocation.width != widget->allocation.width ||
3616 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003617 window_schedule_redraw(widget->window);
3618 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003619 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003620
3621 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003622 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003623 widget->allocation.width,
3624 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003625}
3626
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003627static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003628hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3629{
3630 /*
3631 * This hack should be removed, when EGL respects
3632 * eglSwapInterval(0).
3633 *
3634 * If this window has sub-surfaces, especially a free-running
3635 * EGL-widget, we need to post the parent surface once with
3636 * all the old state to guarantee, that the EGL-widget will
3637 * receive its frame callback soon. Otherwise, a forced call
3638 * to eglSwapBuffers may end up blocking, waiting for a frame
3639 * event that will never come, because we will commit the parent
3640 * surface with all new state only after eglSwapBuffers returns.
3641 *
3642 * This assumes, that:
3643 * 1. When the EGL widget's resize hook is called, it pauses.
3644 * 2. When the EGL widget's redraw hook is called, it forces a
3645 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3646 * In a single threaded application condition 1 is a no-op.
3647 *
3648 * XXX: This should actually be after the surface_resize() calls,
3649 * but cannot, because then it would commit the incomplete state
3650 * accumulated from the widget resize hooks.
3651 */
3652 if (window->subsurface_list.next != &window->main_surface->link ||
3653 window->subsurface_list.prev != &window->main_surface->link)
3654 wl_surface_commit(window->main_surface->surface);
3655}
3656
3657static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003658idle_resize(struct window *window)
3659{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003660 struct surface *surface;
3661
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003662 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003663 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003664
Pekka Paalanen71233882013-04-25 13:57:53 +03003665 DBG("from %dx%d to %dx%d\n",
3666 window->main_surface->server_allocation.width,
3667 window->main_surface->server_allocation.height,
3668 window->pending_allocation.width,
3669 window->pending_allocation.height);
3670
Pekka Paalanene9297f82013-04-25 13:57:51 +03003671 hack_prevent_EGL_sub_surface_deadlock(window);
3672
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003673 widget_set_allocation(window->main_surface->widget,
3674 window->pending_allocation.x,
3675 window->pending_allocation.y,
3676 window->pending_allocation.width,
3677 window->pending_allocation.height);
3678
3679 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003680
3681 /* The main surface is in the list, too. Main surface's
3682 * resize_handler is responsible for calling widget_set_allocation()
3683 * on all sub-surface root widgets, so they will be resized
3684 * properly.
3685 */
3686 wl_list_for_each(surface, &window->subsurface_list, link) {
3687 if (surface == window->main_surface)
3688 continue;
3689
3690 surface_set_synchronized(surface);
3691 surface_resize(surface);
3692 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003693}
3694
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003695void
3696window_schedule_resize(struct window *window, int width, int height)
3697{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003698 window->pending_allocation.x = 0;
3699 window->pending_allocation.y = 0;
3700 window->pending_allocation.width = width;
3701 window->pending_allocation.height = height;
3702
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003703 if (window->min_allocation.width == 0)
3704 window->min_allocation = window->pending_allocation;
3705 if (window->pending_allocation.width < window->min_allocation.width)
3706 window->pending_allocation.width = window->min_allocation.width;
3707 if (window->pending_allocation.height < window->min_allocation.height)
3708 window->pending_allocation.height = window->min_allocation.height;
3709
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003710 window->resize_needed = 1;
3711 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003712}
3713
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003714void
3715widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3716{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003717 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003718}
3719
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003720static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003721handle_ping(void *data, struct wl_shell_surface *shell_surface,
3722 uint32_t serial)
3723{
3724 wl_shell_surface_pong(shell_surface, serial);
3725}
3726
3727static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003728handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003729 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003730{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003731 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003732
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003733 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003734 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003735}
3736
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003737static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003738menu_destroy(struct menu *menu)
3739{
3740 widget_destroy(menu->widget);
3741 window_destroy(menu->window);
3742 free(menu);
3743}
3744
3745static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003746handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3747{
3748 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003749 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003750
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003751 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003752 * device. Or just use wl_callback. And this really needs to
3753 * be a window vfunc that the menu can set. And we need the
3754 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003755
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003756 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003757 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003758}
3759
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003760static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003761 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003762 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003763 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003764};
3765
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003766void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003767window_get_allocation(struct window *window,
3768 struct rectangle *allocation)
3769{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003770 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003771}
3772
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003773static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003774widget_redraw(struct widget *widget)
3775{
3776 struct widget *child;
3777
3778 if (widget->redraw_handler)
3779 widget->redraw_handler(widget, widget->user_data);
3780 wl_list_for_each(child, &widget->child_list, link)
3781 widget_redraw(child);
3782}
3783
3784static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003785frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3786{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003787 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003788
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003789 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003790 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003791 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003792 surface->frame_cb = NULL;
3793
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003794 surface->last_time = time;
3795
Pekka Paalanen71233882013-04-25 13:57:53 +03003796 if (surface->redraw_needed || surface->window->redraw_needed) {
3797 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003798 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003799 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003800}
3801
3802static const struct wl_callback_listener listener = {
3803 frame_callback
3804};
3805
3806static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003807surface_redraw(struct surface *surface)
3808{
Pekka Paalanen71233882013-04-25 13:57:53 +03003809 DBG_OBJ(surface->surface, "begin\n");
3810
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003811 if (!surface->window->redraw_needed && !surface->redraw_needed)
3812 return;
3813
3814 /* Whole-window redraw forces a redraw even if the previous has
3815 * not yet hit the screen.
3816 */
3817 if (surface->frame_cb) {
3818 if (!surface->window->redraw_needed)
3819 return;
3820
Pekka Paalanen71233882013-04-25 13:57:53 +03003821 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003822 wl_callback_destroy(surface->frame_cb);
3823 }
3824
3825 surface->frame_cb = wl_surface_frame(surface->surface);
3826 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003827 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003828
3829 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003830 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003831 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003832 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003833}
3834
3835static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003836idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003837{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003838 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003839 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003840
Pekka Paalanen71233882013-04-25 13:57:53 +03003841 DBG(" --------- \n");
3842
Pekka Paalaneneebff542013-04-25 13:57:52 +03003843 wl_list_init(&window->redraw_task.link);
3844 window->redraw_task_scheduled = 0;
3845
3846 if (window->resize_needed) {
3847 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003848 if (window->main_surface->frame_cb) {
3849 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003850 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003851 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003852
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003853 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003854 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003855
Pekka Paalanen35e82632013-04-25 13:57:48 +03003856 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003857 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003858
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003859 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003860 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003861
3862 wl_list_for_each(surface, &window->subsurface_list, link)
3863 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003864}
3865
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003866static void
3867window_schedule_redraw_task(struct window *window)
3868{
3869 if (window->configure_requests)
3870 return;
3871 if (!window->redraw_task_scheduled) {
3872 window->redraw_task.run = idle_redraw;
3873 display_defer(window->display, &window->redraw_task);
3874 window->redraw_task_scheduled = 1;
3875 }
3876}
3877
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003878void
3879window_schedule_redraw(struct window *window)
3880{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003881 struct surface *surface;
3882
Pekka Paalanen71233882013-04-25 13:57:53 +03003883 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3884
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003885 wl_list_for_each(surface, &window->subsurface_list, link)
3886 surface->redraw_needed = 1;
3887
3888 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003889}
3890
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003891int
3892window_is_fullscreen(struct window *window)
3893{
3894 return window->type == TYPE_FULLSCREEN;
3895}
3896
MoD063a8822013-03-02 23:43:57 +00003897static void
3898configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3899{
3900 struct window *window = data;
3901
3902 wl_callback_destroy(callback);
3903 window->configure_requests--;
3904
3905 if (!window->configure_requests)
3906 window_schedule_redraw(window);
3907}
3908
3909static struct wl_callback_listener configure_request_listener = {
3910 configure_request_completed,
3911};
3912
3913static void
3914window_defer_redraw_until_configure(struct window* window)
3915{
3916 struct wl_callback *callback;
3917
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003918 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003919 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003920 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003921 }
3922
3923 callback = wl_display_sync(window->display->display);
3924 wl_callback_add_listener(callback, &configure_request_listener, window);
3925 window->configure_requests++;
3926}
3927
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003928void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003929window_set_fullscreen(struct window *window, int fullscreen)
3930{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003931 if (!window->display->shell)
3932 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003933
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003934 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003935 return;
3936
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003937 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003938 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003939 if (window->type == TYPE_TOPLEVEL) {
3940 window->saved_allocation = window->main_surface->allocation;
3941 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003942 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003943 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003944 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003945 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003946 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003947 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003948 if (window->saved_type == TYPE_MAXIMIZED) {
3949 window_set_maximized(window, 1);
3950 } else {
3951 window->type = TYPE_TOPLEVEL;
3952 wl_shell_surface_set_toplevel(window->shell_surface);
3953 window_schedule_resize(window,
3954 window->saved_allocation.width,
3955 window->saved_allocation.height);
3956 }
3957
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003958 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003959}
3960
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003961void
3962window_set_fullscreen_method(struct window *window,
3963 enum wl_shell_surface_fullscreen_method method)
3964{
3965 window->fullscreen_method = method;
3966}
3967
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003968int
3969window_is_maximized(struct window *window)
3970{
3971 return window->type == TYPE_MAXIMIZED;
3972}
3973
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003974void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003975window_set_maximized(struct window *window, int maximized)
3976{
3977 if (!window->display->shell)
3978 return;
3979
3980 if ((window->type == TYPE_MAXIMIZED) == maximized)
3981 return;
3982
3983 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003984 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003985 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);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003988 } else if (window->type == TYPE_FULLSCREEN) {
3989 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3990 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003991 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003992 } else {
3993 wl_shell_surface_set_toplevel(window->shell_surface);
3994 window->type = TYPE_TOPLEVEL;
3995 window_schedule_resize(window,
3996 window->saved_allocation.width,
3997 window->saved_allocation.height);
3998 }
3999}
4000
4001void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004002window_set_user_data(struct window *window, void *data)
4003{
4004 window->user_data = data;
4005}
4006
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004007void *
4008window_get_user_data(struct window *window)
4009{
4010 return window->user_data;
4011}
4012
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004013void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004014window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004015 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004016{
4017 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004018}
4019
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004020void
4021window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004022 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004023{
4024 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004025}
4026
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004027void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004028window_set_data_handler(struct window *window, window_data_handler_t handler)
4029{
4030 window->data_handler = handler;
4031}
4032
4033void
4034window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4035{
4036 window->drop_handler = handler;
4037}
4038
4039void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004040window_set_close_handler(struct window *window,
4041 window_close_handler_t handler)
4042{
4043 window->close_handler = handler;
4044}
4045
4046void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004047window_set_fullscreen_handler(struct window *window,
4048 window_fullscreen_handler_t handler)
4049{
4050 window->fullscreen_handler = handler;
4051}
4052
4053void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004054window_set_output_handler(struct window *window,
4055 window_output_handler_t handler)
4056{
4057 window->output_handler = handler;
4058}
4059
4060void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004061window_set_title(struct window *window, const char *title)
4062{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004063 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004064 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004065 if (window->shell_surface)
4066 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004067}
4068
4069const char *
4070window_get_title(struct window *window)
4071{
4072 return window->title;
4073}
4074
4075void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004076window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4077{
4078 struct text_cursor_position *text_cursor_position =
4079 window->display->text_cursor_position;
4080
Scott Moreau9295ce02012-06-01 12:46:10 -06004081 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004082 return;
4083
4084 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004085 window->main_surface->surface,
4086 wl_fixed_from_int(x),
4087 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004088}
4089
4090void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004091window_damage(struct window *window, int32_t x, int32_t y,
4092 int32_t width, int32_t height)
4093{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004094 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004095}
4096
Casey Dahlin9074db52012-04-19 22:50:09 -04004097static void
4098surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004099 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004100{
Rob Bradford7507b572012-05-15 17:55:34 +01004101 struct window *window = data;
4102 struct output *output;
4103 struct output *output_found = NULL;
4104 struct window_output *window_output;
4105
4106 wl_list_for_each(output, &window->display->output_list, link) {
4107 if (output->output == wl_output) {
4108 output_found = output;
4109 break;
4110 }
4111 }
4112
4113 if (!output_found)
4114 return;
4115
4116 window_output = malloc (sizeof *window_output);
4117 window_output->output = output_found;
4118
4119 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004120
4121 if (window->output_handler)
4122 window->output_handler(window, output_found, 1,
4123 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004124}
4125
4126static void
4127surface_leave(void *data,
4128 struct wl_surface *wl_surface, struct wl_output *output)
4129{
Rob Bradford7507b572012-05-15 17:55:34 +01004130 struct window *window = data;
4131 struct window_output *window_output;
4132 struct window_output *window_output_found = NULL;
4133
4134 wl_list_for_each(window_output, &window->window_output_list, link) {
4135 if (window_output->output->output == output) {
4136 window_output_found = window_output;
4137 break;
4138 }
4139 }
4140
4141 if (window_output_found) {
4142 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004143
4144 if (window->output_handler)
4145 window->output_handler(window, window_output->output,
4146 0, window->user_data);
4147
Rob Bradford7507b572012-05-15 17:55:34 +01004148 free(window_output_found);
4149 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004150}
4151
4152static const struct wl_surface_listener surface_listener = {
4153 surface_enter,
4154 surface_leave
4155};
4156
Pekka Paalanen4e373742013-02-13 16:17:13 +02004157static struct surface *
4158surface_create(struct window *window)
4159{
4160 struct display *display = window->display;
4161 struct surface *surface;
4162
4163 surface = calloc(1, sizeof *surface);
4164 if (!surface)
4165 return NULL;
4166
4167 surface->window = window;
4168 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004169 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004170 wl_surface_add_listener(surface->surface, &surface_listener, window);
4171
Pekka Paalanen35e82632013-04-25 13:57:48 +03004172 wl_list_insert(&window->subsurface_list, &surface->link);
4173
Pekka Paalanen4e373742013-02-13 16:17:13 +02004174 return surface;
4175}
4176
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004177static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004178window_create_internal(struct display *display,
4179 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004180{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004181 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004182 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004183
4184 window = malloc(sizeof *window);
4185 if (window == NULL)
4186 return NULL;
4187
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004188 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004189 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004190 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004191 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004192
4193 surface = surface_create(window);
4194 window->main_surface = surface;
4195
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004196 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004197 window->shell_surface =
4198 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004199 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004200 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004201
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004202 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004203 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004204 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004205
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004206 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004207#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004208 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004209#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004210 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004211#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004212 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004213 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004214
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004215 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004216 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004217 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004218
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004219 if (window->shell_surface) {
4220 wl_shell_surface_set_user_data(window->shell_surface, window);
4221 wl_shell_surface_add_listener(window->shell_surface,
4222 &shell_surface_listener, window);
4223 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004224
Rob Bradford7507b572012-05-15 17:55:34 +01004225 wl_list_init (&window->window_output_list);
4226
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004227 return window;
4228}
4229
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004230struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004231window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004232{
4233 struct window *window;
4234
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004235 window = window_create_internal(display, NULL, TYPE_NONE);
4236 if (!window)
4237 return NULL;
4238
4239 return window;
4240}
4241
4242struct window *
4243window_create_custom(struct display *display)
4244{
4245 struct window *window;
4246
4247 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004248 if (!window)
4249 return NULL;
4250
4251 return window;
4252}
4253
4254struct window *
4255window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004256 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004257{
4258 struct window *window;
4259
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004260 window = window_create_internal(parent->display,
4261 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004262 if (!window)
4263 return NULL;
4264
4265 window->x = x;
4266 window->y = y;
4267
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004268 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004269 wl_shell_surface_set_transient(
4270 window->shell_surface,
4271 window->parent->main_surface->surface,
4272 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004273
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004274 return window;
4275}
4276
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004277static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004278menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004279{
4280 int next;
4281
4282 next = (sy - 8) / 20;
4283 if (menu->current != next) {
4284 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004285 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004286 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004287}
4288
4289static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004290menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004291 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004292 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004293{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004294 struct menu *menu = data;
4295
4296 if (widget == menu->widget)
4297 menu_set_item(data, y);
4298
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004299 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004300}
4301
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004302static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004303menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004304 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004305{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004306 struct menu *menu = data;
4307
4308 if (widget == menu->widget)
4309 menu_set_item(data, y);
4310
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004311 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004312}
4313
4314static void
4315menu_leave_handler(struct widget *widget, struct input *input, void *data)
4316{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004317 struct menu *menu = data;
4318
4319 if (widget == menu->widget)
4320 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004321}
4322
4323static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004324menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004325 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004326 uint32_t button, enum wl_pointer_button_state state,
4327 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004328
4329{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004330 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004331
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004332 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4333 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004334 /* Either relase after press-drag-release or
4335 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004336 menu->func(menu->window->parent,
4337 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004338 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004339 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004340 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004341 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004342 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004343}
4344
4345static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004346menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004347{
4348 cairo_t *cr;
4349 const int32_t r = 3, margin = 3;
4350 struct menu *menu = data;
4351 int32_t width, height, i;
4352
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004353 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004354 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4355 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4356 cairo_paint(cr);
4357
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004358 width = widget->allocation.width;
4359 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004360 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004361
4362 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004363 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4364 cairo_fill(cr);
4365
4366 for (i = 0; i < menu->count; i++) {
4367 if (i == menu->current) {
4368 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4369 cairo_rectangle(cr, margin, i * 20 + margin,
4370 width - 2 * margin, 20);
4371 cairo_fill(cr);
4372 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4373 cairo_move_to(cr, 10, i * 20 + 16);
4374 cairo_show_text(cr, menu->entries[i]);
4375 } else {
4376 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4377 cairo_move_to(cr, 10, i * 20 + 16);
4378 cairo_show_text(cr, menu->entries[i]);
4379 }
4380 }
4381
4382 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004383}
4384
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004385void
4386window_show_menu(struct display *display,
4387 struct input *input, uint32_t time, struct window *parent,
4388 int32_t x, int32_t y,
4389 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004390{
4391 struct window *window;
4392 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004393 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004394
4395 menu = malloc(sizeof *menu);
4396 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004397 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004398
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004399 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004400 if (!window) {
4401 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004402 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004403 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004404
4405 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004406 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004407 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4408 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004409 menu->entries = entries;
4410 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004411 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004412 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004413 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004414 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004415 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004416 window->type = TYPE_MENU;
4417 window->x = x;
4418 window->y = y;
4419
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004420 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004421 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004422 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004423 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004424 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004425
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004426 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004427 widget_set_enter_handler(menu->widget, menu_enter_handler);
4428 widget_set_leave_handler(menu->widget, menu_leave_handler);
4429 widget_set_motion_handler(menu->widget, menu_motion_handler);
4430 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004431
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004432 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004433 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004434}
4435
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004436void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004437window_set_buffer_type(struct window *window, enum window_buffer_type type)
4438{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004439 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004440}
4441
Pekka Paalanen35e82632013-04-25 13:57:48 +03004442struct widget *
4443window_add_subsurface(struct window *window, void *data,
4444 enum subsurface_mode default_mode)
4445{
4446 struct widget *widget;
4447 struct surface *surface;
4448 struct wl_surface *parent;
4449 struct wl_subcompositor *subcompo = window->display->subcompositor;
4450
4451 if (!subcompo)
4452 return NULL;
4453
4454 surface = surface_create(window);
4455 widget = widget_create(window, surface, data);
4456 wl_list_init(&widget->link);
4457 surface->widget = widget;
4458
4459 parent = window->main_surface->surface;
4460 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4461 surface->surface,
4462 parent);
4463 surface->synchronized = 1;
4464
4465 switch (default_mode) {
4466 case SUBSURFACE_SYNCHRONIZED:
4467 surface->synchronized_default = 1;
4468 break;
4469 case SUBSURFACE_DESYNCHRONIZED:
4470 surface->synchronized_default = 0;
4471 break;
4472 default:
4473 assert(!"bad enum subsurface_mode");
4474 }
4475
4476 return widget;
4477}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004478
4479static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004480display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004481 struct wl_output *wl_output,
4482 int x, int y,
4483 int physical_width,
4484 int physical_height,
4485 int subpixel,
4486 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004487 const char *model,
4488 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004489{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004490 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004491
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004492 output->allocation.x = x;
4493 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004494 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004495}
4496
4497static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004498display_handle_done(void *data,
4499 struct wl_output *wl_output)
4500{
4501}
4502
4503static void
4504display_handle_scale(void *data,
4505 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004506 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004507{
4508 struct output *output = data;
4509
4510 output->scale = scale;
4511}
4512
4513static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004514display_handle_mode(void *data,
4515 struct wl_output *wl_output,
4516 uint32_t flags,
4517 int width,
4518 int height,
4519 int refresh)
4520{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004521 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004522 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004523
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004524 if (flags & WL_OUTPUT_MODE_CURRENT) {
4525 output->allocation.width = width;
4526 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004527 if (display->output_configure_handler)
4528 (*display->output_configure_handler)(
4529 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004530 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004531}
4532
4533static const struct wl_output_listener output_listener = {
4534 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004535 display_handle_mode,
4536 display_handle_done,
4537 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004538};
4539
4540static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004541display_add_output(struct display *d, uint32_t id)
4542{
4543 struct output *output;
4544
4545 output = malloc(sizeof *output);
4546 if (output == NULL)
4547 return;
4548
4549 memset(output, 0, sizeof *output);
4550 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004551 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004552 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004553 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004554 wl_list_insert(d->output_list.prev, &output->link);
4555
4556 wl_output_add_listener(output->output, &output_listener, output);
4557}
4558
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004559static void
4560output_destroy(struct output *output)
4561{
4562 if (output->destroy_handler)
4563 (*output->destroy_handler)(output, output->user_data);
4564
4565 wl_output_destroy(output->output);
4566 wl_list_remove(&output->link);
4567 free(output);
4568}
4569
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004570void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004571display_set_global_handler(struct display *display,
4572 display_global_handler_t handler)
4573{
4574 struct global *global;
4575
4576 display->global_handler = handler;
4577 if (!handler)
4578 return;
4579
4580 wl_list_for_each(global, &display->global_list, link)
4581 display->global_handler(display,
4582 global->name, global->interface,
4583 global->version, display->user_data);
4584}
4585
4586void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004587display_set_output_configure_handler(struct display *display,
4588 display_output_handler_t handler)
4589{
4590 struct output *output;
4591
4592 display->output_configure_handler = handler;
4593 if (!handler)
4594 return;
4595
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004596 wl_list_for_each(output, &display->output_list, link) {
4597 if (output->allocation.width == 0 &&
4598 output->allocation.height == 0)
4599 continue;
4600
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004601 (*display->output_configure_handler)(output,
4602 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004603 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004604}
4605
4606void
4607output_set_user_data(struct output *output, void *data)
4608{
4609 output->user_data = data;
4610}
4611
4612void *
4613output_get_user_data(struct output *output)
4614{
4615 return output->user_data;
4616}
4617
4618void
4619output_set_destroy_handler(struct output *output,
4620 display_output_handler_t handler)
4621{
4622 output->destroy_handler = handler;
4623 /* FIXME: implement this, once we have way to remove outputs */
4624}
4625
4626void
Scott Moreau4e072362012-09-29 02:03:11 -06004627output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004628{
Scott Moreau4e072362012-09-29 02:03:11 -06004629 struct rectangle allocation = output->allocation;
4630
4631 switch (output->transform) {
4632 case WL_OUTPUT_TRANSFORM_90:
4633 case WL_OUTPUT_TRANSFORM_270:
4634 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4635 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4636 /* Swap width and height */
4637 allocation.width = output->allocation.height;
4638 allocation.height = output->allocation.width;
4639 break;
4640 }
4641
4642 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004643}
4644
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004645struct wl_output *
4646output_get_wl_output(struct output *output)
4647{
4648 return output->output;
4649}
4650
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004651enum wl_output_transform
4652output_get_transform(struct output *output)
4653{
4654 return output->transform;
4655}
4656
Alexander Larssonafd319a2013-05-22 14:41:27 +02004657uint32_t
4658output_get_scale(struct output *output)
4659{
4660 return output->scale;
4661}
4662
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004663static void
Daniel Stone97f68542012-05-30 16:32:01 +01004664fini_xkb(struct input *input)
4665{
4666 xkb_state_unref(input->xkb.state);
4667 xkb_map_unref(input->xkb.keymap);
4668}
4669
4670static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004671display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004672{
4673 struct input *input;
4674
4675 input = malloc(sizeof *input);
4676 if (input == NULL)
4677 return;
4678
4679 memset(input, 0, sizeof *input);
4680 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004681 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004682 input->pointer_focus = NULL;
4683 input->keyboard_focus = NULL;
4684 wl_list_insert(d->input_list.prev, &input->link);
4685
Daniel Stone37816df2012-05-16 18:45:18 +01004686 wl_seat_add_listener(input->seat, &seat_listener, input);
4687 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004688
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004689 input->data_device =
4690 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004691 input->seat);
4692 wl_data_device_add_listener(input->data_device, &data_device_listener,
4693 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004694
4695 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004696
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004697 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4698 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004699 input->repeat_task.run = keyboard_repeat_func;
4700 display_watch_fd(d, input->repeat_timer_fd,
4701 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004702}
4703
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004704static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004705input_destroy(struct input *input)
4706{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004707 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004708 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004709
4710 if (input->drag_offer)
4711 data_offer_destroy(input->drag_offer);
4712
4713 if (input->selection_offer)
4714 data_offer_destroy(input->selection_offer);
4715
4716 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004717 fini_xkb(input);
4718
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004719 wl_surface_destroy(input->pointer_surface);
4720
Pekka Paalanene1207c72011-12-16 12:02:09 +02004721 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004722 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004723 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004724 free(input);
4725}
4726
4727static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004728init_workspace_manager(struct display *d, uint32_t id)
4729{
4730 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004731 wl_registry_bind(d->registry, id,
4732 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004733 if (d->workspace_manager != NULL)
4734 workspace_manager_add_listener(d->workspace_manager,
4735 &workspace_manager_listener,
4736 d);
4737}
4738
4739static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004740registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4741 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004742{
4743 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004744 struct global *global;
4745
4746 global = malloc(sizeof *global);
4747 global->name = id;
4748 global->interface = strdup(interface);
4749 global->version = version;
4750 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004751
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004752 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004753 d->compositor = wl_registry_bind(registry, id,
4754 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004755 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004756 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004757 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004758 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004759 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004760 d->shell = wl_registry_bind(registry,
4761 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004762 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004763 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004764 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4765 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004766 wl_registry_bind(registry, id,
4767 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004768 } else if (strcmp(interface, "text_cursor_position") == 0) {
4769 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004770 wl_registry_bind(registry, id,
4771 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004772 } else if (strcmp(interface, "workspace_manager") == 0) {
4773 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004774 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4775 d->subcompositor =
4776 wl_registry_bind(registry, id,
4777 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004778 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004779
4780 if (d->global_handler)
4781 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004782}
4783
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004784static void
4785registry_handle_global_remove(void *data, struct wl_registry *registry,
4786 uint32_t name)
4787{
4788 struct display *d = data;
4789 struct global *global;
4790 struct global *tmp;
4791
4792 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4793 if (global->name != name)
4794 continue;
4795
4796 /* XXX: Should destroy bound globals, and call
4797 * the counterpart of display::global_handler
4798 */
4799 wl_list_remove(&global->link);
4800 free(global->interface);
4801 free(global);
4802 }
4803}
4804
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004805void *
4806display_bind(struct display *display, uint32_t name,
4807 const struct wl_interface *interface, uint32_t version)
4808{
4809 return wl_registry_bind(display->registry, name, interface, version);
4810}
4811
4812static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004813 registry_handle_global,
4814 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004815};
4816
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004817#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004818static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004819init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004820{
4821 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004822 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004823
Rob Clark6396ed32012-03-11 19:48:41 -05004824#ifdef USE_CAIRO_GLESV2
4825# define GL_BIT EGL_OPENGL_ES2_BIT
4826#else
4827# define GL_BIT EGL_OPENGL_BIT
4828#endif
4829
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004830 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004831 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004832 EGL_RED_SIZE, 1,
4833 EGL_GREEN_SIZE, 1,
4834 EGL_BLUE_SIZE, 1,
4835 EGL_ALPHA_SIZE, 1,
4836 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004837 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004838 EGL_NONE
4839 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004840
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004841#ifdef USE_CAIRO_GLESV2
4842 static const EGLint context_attribs[] = {
4843 EGL_CONTEXT_CLIENT_VERSION, 2,
4844 EGL_NONE
4845 };
4846 EGLint api = EGL_OPENGL_ES_API;
4847#else
4848 EGLint *context_attribs = NULL;
4849 EGLint api = EGL_OPENGL_API;
4850#endif
4851
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004852 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004853 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004854 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004855 return -1;
4856 }
4857
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004858 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004859 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004860 return -1;
4861 }
4862
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004863 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4864 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004865 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004866 return -1;
4867 }
4868
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004869 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004870 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004871 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004872 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004873 return -1;
4874 }
4875
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004876 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004877 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004878 return -1;
4879 }
4880
Benjamin Franzke0c991632011-09-27 21:57:31 +02004881 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4882 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004883 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004884 return -1;
4885 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004886
4887 return 0;
4888}
4889
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004890static void
4891fini_egl(struct display *display)
4892{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004893 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004894
4895 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4896 EGL_NO_CONTEXT);
4897
4898 eglTerminate(display->dpy);
4899 eglReleaseThread();
4900}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004901#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004902
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004903static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004904init_dummy_surface(struct display *display)
4905{
4906 int len;
4907 void *data;
4908
4909 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4910 data = malloc(len);
4911 display->dummy_surface =
4912 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4913 1, 1, len);
4914 display->dummy_surface_data = data;
4915}
4916
4917static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004918handle_display_data(struct task *task, uint32_t events)
4919{
4920 struct display *display =
4921 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004922 struct epoll_event ep;
4923 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004924
4925 display->display_fd_events = events;
4926
4927 if (events & EPOLLERR || events & EPOLLHUP) {
4928 display_exit(display);
4929 return;
4930 }
4931
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004932 if (events & EPOLLIN) {
4933 ret = wl_display_dispatch(display->display);
4934 if (ret == -1) {
4935 display_exit(display);
4936 return;
4937 }
4938 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004939
4940 if (events & EPOLLOUT) {
4941 ret = wl_display_flush(display->display);
4942 if (ret == 0) {
4943 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4944 ep.data.ptr = &display->display_task;
4945 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4946 display->display_fd, &ep);
4947 } else if (ret == -1 && errno != EAGAIN) {
4948 display_exit(display);
4949 return;
4950 }
4951 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004952}
4953
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004954static void
4955log_handler(const char *format, va_list args)
4956{
4957 vfprintf(stderr, format, args);
4958}
4959
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004960struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004961display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004962{
4963 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004964
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004965 wl_log_set_handler_client(log_handler);
4966
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004967 d = malloc(sizeof *d);
4968 if (d == NULL)
4969 return NULL;
4970
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004971 memset(d, 0, sizeof *d);
4972
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004973 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004974 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004975 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004976 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004977 return NULL;
4978 }
4979
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004980 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004981 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004982 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004983 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4984 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004985
4986 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004987 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004988 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004989 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004990
Daniel Stone97f68542012-05-30 16:32:01 +01004991 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004992 if (d->xkb_context == NULL) {
4993 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004994 return NULL;
4995 }
4996
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004997 d->workspace = 0;
4998 d->workspace_count = 1;
4999
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005000 d->registry = wl_display_get_registry(d->display);
5001 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005002
5003 if (wl_display_dispatch(d->display) < 0) {
5004 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5005 return NULL;
5006 }
5007
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005008#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005009 if (init_egl(d) < 0)
5010 fprintf(stderr, "EGL does not seem to work, "
5011 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005012#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005013
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005014 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005015
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005016 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005017
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005018 wl_list_init(&d->window_list);
5019
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005020 init_dummy_surface(d);
5021
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005022 return d;
5023}
5024
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005025static void
5026display_destroy_outputs(struct display *display)
5027{
5028 struct output *tmp;
5029 struct output *output;
5030
5031 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5032 output_destroy(output);
5033}
5034
Pekka Paalanene1207c72011-12-16 12:02:09 +02005035static void
5036display_destroy_inputs(struct display *display)
5037{
5038 struct input *tmp;
5039 struct input *input;
5040
5041 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5042 input_destroy(input);
5043}
5044
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005045void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005046display_destroy(struct display *display)
5047{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005048 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005049 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5050 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005051
5052 if (!wl_list_empty(&display->deferred_list))
5053 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5054
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005055 cairo_surface_destroy(display->dummy_surface);
5056 free(display->dummy_surface_data);
5057
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005058 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005059 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005060
Daniel Stone97f68542012-05-30 16:32:01 +01005061 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005062
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005063 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005064 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005065
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005066#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005067 if (display->argb_device)
5068 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005069#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005070
Pekka Paalanen35e82632013-04-25 13:57:48 +03005071 if (display->subcompositor)
5072 wl_subcompositor_destroy(display->subcompositor);
5073
Pekka Paalanenc2052982011-12-16 11:41:32 +02005074 if (display->shell)
5075 wl_shell_destroy(display->shell);
5076
5077 if (display->shm)
5078 wl_shm_destroy(display->shm);
5079
5080 if (display->data_device_manager)
5081 wl_data_device_manager_destroy(display->data_device_manager);
5082
5083 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005084 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005085
5086 close(display->epoll_fd);
5087
U. Artie Eoff44874d92012-10-02 21:12:35 -07005088 if (!(display->display_fd_events & EPOLLERR) &&
5089 !(display->display_fd_events & EPOLLHUP))
5090 wl_display_flush(display->display);
5091
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005092 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005093 free(display);
5094}
5095
5096void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005097display_set_user_data(struct display *display, void *data)
5098{
5099 display->user_data = data;
5100}
5101
5102void *
5103display_get_user_data(struct display *display)
5104{
5105 return display->user_data;
5106}
5107
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005108struct wl_display *
5109display_get_display(struct display *display)
5110{
5111 return display->display;
5112}
5113
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005114cairo_device_t *
5115display_get_cairo_device(struct display *display)
5116{
5117 return display->argb_device;
5118}
5119
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005120struct output *
5121display_get_output(struct display *display)
5122{
5123 return container_of(display->output_list.next, struct output, link);
5124}
5125
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005126struct wl_compositor *
5127display_get_compositor(struct display *display)
5128{
5129 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005130}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005131
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005132uint32_t
5133display_get_serial(struct display *display)
5134{
5135 return display->serial;
5136}
5137
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005138EGLDisplay
5139display_get_egl_display(struct display *d)
5140{
5141 return d->dpy;
5142}
5143
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005144struct wl_data_source *
5145display_create_data_source(struct display *display)
5146{
5147 return wl_data_device_manager_create_data_source(display->data_device_manager);
5148}
5149
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005150EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005151display_get_argb_egl_config(struct display *d)
5152{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005153 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005154}
5155
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005156struct wl_shell *
5157display_get_shell(struct display *display)
5158{
5159 return display->shell;
5160}
5161
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005162int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005163display_acquire_window_surface(struct display *display,
5164 struct window *window,
5165 EGLContext ctx)
5166{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005167 struct surface *surface = window->main_surface;
5168
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005169 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005170 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005171
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005172 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005173 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005174}
5175
5176void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005177display_release_window_surface(struct display *display,
5178 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005179{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005180 struct surface *surface = window->main_surface;
5181
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005182 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005183 return;
5184
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005185 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005186}
5187
5188void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005189display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005190{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005191 wl_list_insert(&display->deferred_list, &task->link);
5192}
5193
5194void
5195display_watch_fd(struct display *display,
5196 int fd, uint32_t events, struct task *task)
5197{
5198 struct epoll_event ep;
5199
5200 ep.events = events;
5201 ep.data.ptr = task;
5202 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5203}
5204
5205void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005206display_unwatch_fd(struct display *display, int fd)
5207{
5208 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5209}
5210
5211void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005212display_run(struct display *display)
5213{
5214 struct task *task;
5215 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005216 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005217
Pekka Paalanen826d7952011-12-15 10:14:07 +02005218 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005219 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005220 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005221 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005222 struct task, link);
5223 wl_list_remove(&task->link);
5224 task->run(task, 0);
5225 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005226
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005227 wl_display_dispatch_pending(display->display);
5228
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005229 if (!display->running)
5230 break;
5231
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005232 ret = wl_display_flush(display->display);
5233 if (ret < 0 && errno == EAGAIN) {
5234 ep[0].events =
5235 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5236 ep[0].data.ptr = &display->display_task;
5237
5238 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5239 display->display_fd, &ep[0]);
5240 } else if (ret < 0) {
5241 break;
5242 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005243
5244 count = epoll_wait(display->epoll_fd,
5245 ep, ARRAY_LENGTH(ep), -1);
5246 for (i = 0; i < count; i++) {
5247 task = ep[i].data.ptr;
5248 task->run(task, ep[i].events);
5249 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005250 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005251}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005252
5253void
5254display_exit(struct display *display)
5255{
5256 display->running = 0;
5257}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005258
5259void
5260keysym_modifiers_add(struct wl_array *modifiers_map,
5261 const char *name)
5262{
5263 size_t len = strlen(name) + 1;
5264 char *p;
5265
5266 p = wl_array_add(modifiers_map, len);
5267
5268 if (p == NULL)
5269 return;
5270
5271 strncpy(p, name, len);
5272}
5273
5274static xkb_mod_index_t
5275keysym_modifiers_get_index(struct wl_array *modifiers_map,
5276 const char *name)
5277{
5278 xkb_mod_index_t index = 0;
5279 char *p = modifiers_map->data;
5280
5281 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5282 if (strcmp(p, name) == 0)
5283 return index;
5284
5285 index++;
5286 p += strlen(p) + 1;
5287 }
5288
5289 return XKB_MOD_INVALID;
5290}
5291
5292xkb_mod_mask_t
5293keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5294 const char *name)
5295{
5296 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5297
5298 if (index == XKB_MOD_INVALID)
5299 return XKB_MOD_INVALID;
5300
5301 return 1 << index;
5302}