blob: b9045f34eb6495db02d893245beccfc58d1a16fa [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025
Kristian Høgsberg61017b12008-11-02 18:51:48 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030029#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030042#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030055#else /* HAVE_CAIRO_EGL */
56typedef void *EGLDisplay;
57typedef void *EGLConfig;
58typedef void *EGLContext;
59#define EGL_NO_DISPLAY ((EGLDisplay)0)
60#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061
Daniel Stone9d4f0302012-02-15 16:33:21 +000062#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030063#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020066#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040067#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060068#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020069#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030070#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050071
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050072#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050073
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070074struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030075
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040076struct global {
77 uint32_t name;
78 char *interface;
79 uint32_t version;
80 struct wl_list link;
81};
82
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050083struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050084 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040085 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050086 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030087 struct wl_subcompositor *subcompositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040088 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050094 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020095 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040097 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040098
99 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700100 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400101 struct task display_task;
102
103 int epoll_fd;
104 struct wl_list deferred_list;
105
Pekka Paalanen826d7952011-12-15 10:14:07 +0200106 int running;
107
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400108 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400109 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400110 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500111 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400112
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400113 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400114
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300115 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300116 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400117
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200118 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120
121 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100122
123 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200124
125 uint32_t workspace;
126 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200127
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200131
132 int has_rgb565;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133};
134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400136 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400138 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500139 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500141 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_CUSTOM
143};
Rob Bradford7507b572012-05-15 17:55:34 +0100144
145struct window_output {
146 struct output *output;
147 struct wl_list link;
148};
149
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150struct toysurface {
151 /*
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200155 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200156 * If flags has SURFACE_HINT_RESIZE set, the user is
157 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200158 * Returns the Cairo surface to draw to.
159 */
160 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200161 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200162 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200163
164 /*
165 * Post the surface to the server, returning the server allocation
166 * rectangle. The Cairo surface from prepare() must be destroyed
167 * after calling this.
168 */
169 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200170 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200171 struct rectangle *server_allocation);
172
173 /*
174 * Make the toysurface current with the given EGL context.
175 * Returns 0 on success, and negative of failure.
176 */
177 int (*acquire)(struct toysurface *base, EGLContext ctx);
178
179 /*
180 * Release the toysurface from the EGL context, returning control
181 * to Cairo.
182 */
183 void (*release)(struct toysurface *base);
184
185 /*
186 * Destroy the toysurface, including the Cairo surface, any
187 * backing storage, and the Wayland protocol objects.
188 */
189 void (*destroy)(struct toysurface *base);
190};
191
Pekka Paalanen4e373742013-02-13 16:17:13 +0200192struct surface {
193 struct window *window;
194
195 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300196 struct wl_subsurface *subsurface;
197 int synchronized;
198 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200199 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200200 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300201 int redraw_needed;
202 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300203 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200204
205 struct rectangle allocation;
206 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200207
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200208 struct wl_region *input_region;
209 struct wl_region *opaque_region;
210
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200211 enum window_buffer_type buffer_type;
212 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200213 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200214
215 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300216
217 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200218};
219
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500220struct window {
221 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500222 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100223 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500224 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200225 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400226 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500227 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500228 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400229 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400230 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300231 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400232 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400233 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100234 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400235 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400236 int focus_count;
237
Pekka Paalanen99436862012-11-19 17:15:59 +0200238 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200239 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000240 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400241
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200242 enum preferred_format preferred_format;
243
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500244 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500245 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400246 window_data_handler_t data_handler;
247 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500248 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400249 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200250 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400251
Pekka Paalanen4e373742013-02-13 16:17:13 +0200252 struct surface *main_surface;
253 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300254
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200255 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500256
Pekka Paalanen35e82632013-04-25 13:57:48 +0300257 /* struct surface::link, contains also main_surface */
258 struct wl_list subsurface_list;
259
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500260 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400261 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500262};
263
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500264struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500265 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200266 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300267 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500268 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400269 struct wl_list link;
270 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500271 widget_resize_handler_t resize_handler;
272 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500273 widget_enter_handler_t enter_handler;
274 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500275 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500276 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200277 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400278 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500279 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300280 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500281 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400282};
283
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400284struct input {
285 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100286 struct wl_seat *seat;
287 struct wl_pointer *pointer;
288 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400289 struct window *pointer_focus;
290 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300291 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300292 uint32_t cursor_anim_start;
293 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300294 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400295 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400296 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400297 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400298 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400299 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400300
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500301 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500302 struct widget *grab;
303 uint32_t grab_button;
304
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400305 struct wl_data_device *data_device;
306 struct data_offer *drag_offer;
307 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100308
309 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100310 struct xkb_keymap *keymap;
311 struct xkb_state *state;
312 xkb_mod_mask_t control_mask;
313 xkb_mod_mask_t alt_mask;
314 xkb_mod_mask_t shift_mask;
315 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400316
317 struct task repeat_task;
318 int repeat_timer_fd;
319 uint32_t repeat_sym;
320 uint32_t repeat_key;
321 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400322};
323
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500324struct output {
325 struct display *display;
326 struct wl_output *output;
327 struct rectangle allocation;
328 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600329 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200330 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200331
332 display_output_handler_t destroy_handler;
333 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500334};
335
Martin Minarik1998b152012-05-10 02:04:35 +0200336enum frame_button_action {
337 FRAME_BUTTON_NULL = 0,
338 FRAME_BUTTON_ICON = 1,
339 FRAME_BUTTON_CLOSE = 2,
340 FRAME_BUTTON_MINIMIZE = 3,
341 FRAME_BUTTON_MAXIMIZE = 4,
342};
343
344enum frame_button_pointer {
345 FRAME_BUTTON_DEFAULT = 0,
346 FRAME_BUTTON_OVER = 1,
347 FRAME_BUTTON_ACTIVE = 2,
348};
349
350enum frame_button_align {
351 FRAME_BUTTON_RIGHT = 0,
352 FRAME_BUTTON_LEFT = 1,
353};
354
355enum frame_button_decoration {
356 FRAME_BUTTON_NONE = 0,
357 FRAME_BUTTON_FANCY = 1,
358};
359
360struct frame_button {
361 struct widget *widget;
362 struct frame *frame;
363 cairo_surface_t *icon;
364 enum frame_button_action type;
365 enum frame_button_pointer state;
366 struct wl_list link; /* buttons_list */
367 enum frame_button_align align;
368 enum frame_button_decoration decoration;
369};
370
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500371struct frame {
372 struct widget *widget;
373 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200374 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500375};
376
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500377struct menu {
378 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500379 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500380 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500381 const char **entries;
382 uint32_t time;
383 int current;
384 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400385 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500386 menu_func_t func;
387};
388
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300389struct tooltip {
390 struct widget *parent;
391 struct window *window;
392 struct widget *widget;
393 char *entry;
394 struct task tooltip_task;
395 int tooltip_fd;
396 float x, y;
397};
398
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700399struct shm_pool {
400 struct wl_shm_pool *pool;
401 size_t size;
402 size_t used;
403 void *data;
404};
405
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400406enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300407 CURSOR_DEFAULT = 100,
408 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400409};
410
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500411enum window_location {
412 WINDOW_INTERIOR = 0,
413 WINDOW_RESIZING_TOP = 1,
414 WINDOW_RESIZING_BOTTOM = 2,
415 WINDOW_RESIZING_LEFT = 4,
416 WINDOW_RESIZING_TOP_LEFT = 5,
417 WINDOW_RESIZING_BOTTOM_LEFT = 6,
418 WINDOW_RESIZING_RIGHT = 8,
419 WINDOW_RESIZING_TOP_RIGHT = 9,
420 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
421 WINDOW_RESIZING_MASK = 15,
422 WINDOW_EXTERIOR = 16,
423 WINDOW_TITLEBAR = 17,
424 WINDOW_CLIENT_AREA = 18,
425};
426
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200427static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428
Pekka Paalanen97777442013-05-22 10:20:05 +0300429/* #define DEBUG */
430
431#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300432
433static void
434debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
435__attribute__ ((format (printf, 4, 5)));
436
437static void
438debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
439{
440 va_list ap;
441 struct timeval tv;
442
443 gettimeofday(&tv, NULL);
444 fprintf(stderr, "%8ld.%03ld ",
445 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
446
447 if (proxy)
448 fprintf(stderr, "%s@%d ",
449 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
450
451 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
452 fprintf(stderr, "%s ", func);
453
454 va_start(ap, fmt);
455 vfprintf(stderr, fmt, ap);
456 va_end(ap);
457}
458
459#define DBG(fmt, ...) \
460 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
461
462#define DBG_OBJ(obj, fmt, ...) \
463 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
464
465#else
466
467#define DBG(...) do {} while (0)
468#define DBG_OBJ(...) do {} while (0)
469
470#endif
471
Alexander Larsson1818e312013-05-22 14:41:31 +0200472static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200473surface_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 +0200474{
475 int32_t tmp;
476
477 switch (buffer_transform) {
478 case WL_OUTPUT_TRANSFORM_90:
479 case WL_OUTPUT_TRANSFORM_270:
480 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
481 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
482 tmp = *width;
483 *width = *height;
484 *height = tmp;
485 break;
486 default:
487 break;
488 }
489
490 *width *= buffer_scale;
491 *height *= buffer_scale;
492}
493
494static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200495buffer_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 +0200496{
497 int32_t tmp;
498
499 switch (buffer_transform) {
500 case WL_OUTPUT_TRANSFORM_90:
501 case WL_OUTPUT_TRANSFORM_270:
502 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
503 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
504 tmp = *width;
505 *width = *height;
506 *height = tmp;
507 break;
508 default:
509 break;
510 }
511
512 *width /= buffer_scale;
513 *height /= buffer_scale;
514}
515
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500516#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518struct egl_window_surface {
519 struct toysurface base;
520 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521 struct display *display;
522 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 struct wl_egl_window *egl_window;
524 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525};
526
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527static struct egl_window_surface *
528to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100529{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100531}
532
533static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200535 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200536 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100537{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200538 struct egl_window_surface *surface = to_egl_window_surface(base);
539
Alexander Larsson1818e312013-05-22 14:41:31 +0200540 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
541
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200542 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
543 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
544
545 return cairo_surface_reference(surface->cairo_surface);
546}
547
548static void
549egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200550 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551 struct rectangle *server_allocation)
552{
553 struct egl_window_surface *surface = to_egl_window_surface(base);
554
555 cairo_gl_surface_swapbuffers(surface->cairo_surface);
556 wl_egl_window_get_attached_size(surface->egl_window,
557 &server_allocation->width,
558 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200559
560 buffer_to_surface_size (buffer_transform, buffer_scale,
561 &server_allocation->width,
562 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563}
564
565static int
566egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
567{
568 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200569 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100570
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200571 device = cairo_surface_get_device(surface->cairo_surface);
572 if (!device)
573 return -1;
574
575 if (!ctx) {
576 if (device == surface->display->argb_device)
577 ctx = surface->display->argb_ctx;
578 else
579 assert(0);
580 }
581
582 cairo_device_flush(device);
583 cairo_device_acquire(device);
584 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
585 surface->egl_surface, ctx))
586 fprintf(stderr, "failed to make surface current\n");
587
588 return 0;
589}
590
591static void
592egl_window_surface_release(struct toysurface *base)
593{
594 struct egl_window_surface *surface = to_egl_window_surface(base);
595 cairo_device_t *device;
596
597 device = cairo_surface_get_device(surface->cairo_surface);
598 if (!device)
599 return;
600
601 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
602 surface->display->argb_ctx))
603 fprintf(stderr, "failed to make context current\n");
604
605 cairo_device_release(device);
606}
607
608static void
609egl_window_surface_destroy(struct toysurface *base)
610{
611 struct egl_window_surface *surface = to_egl_window_surface(base);
612 struct display *d = surface->display;
613
614 cairo_surface_destroy(surface->cairo_surface);
615 eglDestroySurface(d->dpy, surface->egl_surface);
616 wl_egl_window_destroy(surface->egl_window);
617 surface->surface = NULL;
618
619 free(surface);
620}
621
622static struct toysurface *
623egl_window_surface_create(struct display *display,
624 struct wl_surface *wl_surface,
625 uint32_t flags,
626 struct rectangle *rectangle)
627{
628 struct egl_window_surface *surface;
629
Pekka Paalanenb3627362012-11-19 17:16:00 +0200630 if (display->dpy == EGL_NO_DISPLAY)
631 return NULL;
632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 surface = calloc(1, sizeof *surface);
634 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 return NULL;
636
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200637 surface->base.prepare = egl_window_surface_prepare;
638 surface->base.swap = egl_window_surface_swap;
639 surface->base.acquire = egl_window_surface_acquire;
640 surface->base.release = egl_window_surface_release;
641 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200643 surface->display = display;
644 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400645
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200646 surface->egl_window = wl_egl_window_create(surface->surface,
647 rectangle->width,
648 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200650 surface->egl_surface = eglCreateWindowSurface(display->dpy,
651 display->argb_config,
652 surface->egl_window,
653 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100654
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200655 surface->cairo_surface =
656 cairo_gl_surface_create_for_egl(display->argb_device,
657 surface->egl_surface,
658 rectangle->width,
659 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100660
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200661 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100662}
663
Pekka Paalanenb3627362012-11-19 17:16:00 +0200664#else
665
666static struct toysurface *
667egl_window_surface_create(struct display *display,
668 struct wl_surface *wl_surface,
669 uint32_t flags,
670 struct rectangle *rectangle)
671{
672 return NULL;
673}
674
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675#endif
676
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200677struct shm_surface_data {
678 struct wl_buffer *buffer;
679 struct shm_pool *pool;
680};
681
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682struct wl_buffer *
683display_get_buffer_for_surface(struct display *display,
684 cairo_surface_t *surface)
685{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200686 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400687
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200688 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689
690 return data->buffer;
691}
692
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500693static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700694shm_pool_destroy(struct shm_pool *pool);
695
696static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400697shm_surface_data_destroy(void *p)
698{
699 struct shm_surface_data *data = p;
700
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200701 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700702 if (data->pool)
703 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300704
705 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706}
707
Kristian Høgsberg16626282012-04-03 11:21:27 -0400708static struct wl_shm_pool *
709make_shm_pool(struct display *display, int size, void **data)
710{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400711 struct wl_shm_pool *pool;
712 int fd;
713
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300714 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400715 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300716 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
717 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400718 return NULL;
719 }
720
721 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400722 if (*data == MAP_FAILED) {
723 fprintf(stderr, "mmap failed: %m\n");
724 close(fd);
725 return NULL;
726 }
727
728 pool = wl_shm_create_pool(display->shm, fd, size);
729
730 close(fd);
731
732 return pool;
733}
734
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700735static struct shm_pool *
736shm_pool_create(struct display *display, size_t size)
737{
738 struct shm_pool *pool = malloc(sizeof *pool);
739
740 if (!pool)
741 return NULL;
742
743 pool->pool = make_shm_pool(display, size, &pool->data);
744 if (!pool->pool) {
745 free(pool);
746 return NULL;
747 }
748
749 pool->size = size;
750 pool->used = 0;
751
752 return pool;
753}
754
755static void *
756shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
757{
758 if (pool->used + size > pool->size)
759 return NULL;
760
761 *offset = pool->used;
762 pool->used += size;
763
764 return (char *) pool->data + *offset;
765}
766
767/* destroy the pool. this does not unmap the memory though */
768static void
769shm_pool_destroy(struct shm_pool *pool)
770{
771 munmap(pool->data, pool->size);
772 wl_shm_pool_destroy(pool->pool);
773 free(pool);
774}
775
776/* Start allocating from the beginning of the pool again */
777static void
778shm_pool_reset(struct shm_pool *pool)
779{
780 pool->used = 0;
781}
782
783static int
784data_length_for_shm_surface(struct rectangle *rect)
785{
786 int stride;
787
788 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
789 rect->width);
790 return stride * rect->height;
791}
792
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500793static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700794display_create_shm_surface_from_pool(struct display *display,
795 struct rectangle *rectangle,
796 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400797{
798 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400799 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400800 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200801 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700802 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400803 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804
805 data = malloc(sizeof *data);
806 if (data == NULL)
807 return NULL;
808
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200809 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
810 cairo_format = CAIRO_FORMAT_RGB16_565;
811 else
812 cairo_format = CAIRO_FORMAT_ARGB32;
813
814 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700815 length = stride * rectangle->height;
816 data->pool = NULL;
817 map = shm_pool_allocate(pool, length, &offset);
818
819 if (!map) {
820 free(data);
821 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400822 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400824 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200825 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826 rectangle->width,
827 rectangle->height,
828 stride);
829
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200830 cairo_surface_set_user_data(surface, &shm_surface_data_key,
831 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400832
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200833 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
834 format = WL_SHM_FORMAT_RGB565;
835 else {
836 if (flags & SURFACE_OPAQUE)
837 format = WL_SHM_FORMAT_XRGB8888;
838 else
839 format = WL_SHM_FORMAT_ARGB8888;
840 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400841
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200842 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
843 rectangle->width,
844 rectangle->height,
845 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400846
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700847 return surface;
848}
849
850static cairo_surface_t *
851display_create_shm_surface(struct display *display,
852 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200853 struct shm_pool *alternate_pool,
854 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700855{
856 struct shm_surface_data *data;
857 struct shm_pool *pool;
858 cairo_surface_t *surface;
859
Pekka Paalanen99436862012-11-19 17:15:59 +0200860 if (alternate_pool) {
861 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700862 surface = display_create_shm_surface_from_pool(display,
863 rectangle,
864 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200865 alternate_pool);
866 if (surface) {
867 data = cairo_surface_get_user_data(surface,
868 &shm_surface_data_key);
869 goto out;
870 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700871 }
872
873 pool = shm_pool_create(display,
874 data_length_for_shm_surface(rectangle));
875 if (!pool)
876 return NULL;
877
878 surface =
879 display_create_shm_surface_from_pool(display, rectangle,
880 flags, pool);
881
882 if (!surface) {
883 shm_pool_destroy(pool);
884 return NULL;
885 }
886
887 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200888 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700889 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890
Pekka Paalanen99436862012-11-19 17:15:59 +0200891out:
892 if (data_ret)
893 *data_ret = data;
894
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400895 return surface;
896}
897
nobled7b87cb02011-02-01 18:51:47 +0000898static int
899check_size(struct rectangle *rect)
900{
901 if (rect->width && rect->height)
902 return 0;
903
904 fprintf(stderr, "tried to create surface of "
905 "width: %d, height: %d\n", rect->width, rect->height);
906 return -1;
907}
908
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909cairo_surface_t *
910display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400912 struct rectangle *rectangle,
913 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400914{
nobled7b87cb02011-02-01 18:51:47 +0000915 if (check_size(rectangle) < 0)
916 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200917
918 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200919 return display_create_shm_surface(display, rectangle, flags,
920 NULL, NULL);
921}
922
Pekka Paalanena4eda732012-11-19 17:16:02 +0200923struct shm_surface_leaf {
924 cairo_surface_t *cairo_surface;
925 /* 'data' is automatically destroyed, when 'cairo_surface' is */
926 struct shm_surface_data *data;
927
928 struct shm_pool *resize_pool;
929 int busy;
930};
931
932static void
933shm_surface_leaf_release(struct shm_surface_leaf *leaf)
934{
935 if (leaf->cairo_surface)
936 cairo_surface_destroy(leaf->cairo_surface);
937 /* leaf->data already destroyed via cairo private */
938
939 if (leaf->resize_pool)
940 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200941
942 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200943}
944
Pekka Paalanenaef02542013-04-25 13:57:47 +0300945#define MAX_LEAVES 3
946
Pekka Paalanen99436862012-11-19 17:15:59 +0200947struct shm_surface {
948 struct toysurface base;
949 struct display *display;
950 struct wl_surface *surface;
951 uint32_t flags;
952 int dx, dy;
953
Pekka Paalanenaef02542013-04-25 13:57:47 +0300954 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200955 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200956};
957
958static struct shm_surface *
959to_shm_surface(struct toysurface *base)
960{
961 return container_of(base, struct shm_surface, base);
962}
963
Pekka Paalanena4eda732012-11-19 17:16:02 +0200964static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300965shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
966{
967#ifdef DEBUG
968 struct shm_surface_leaf *leaf;
969 char bufs[MAX_LEAVES + 1];
970 int i;
971
972 for (i = 0; i < MAX_LEAVES; i++) {
973 leaf = &surface->leaf[i];
974
975 if (leaf->busy)
976 bufs[i] = 'b';
977 else if (leaf->cairo_surface)
978 bufs[i] = 'a';
979 else
980 bufs[i] = ' ';
981 }
982
983 bufs[MAX_LEAVES] = '\0';
984 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
985#endif
986}
987
988static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200989shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
990{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200991 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300992 struct shm_surface_leaf *leaf;
993 int i;
994 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300995
996 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200997
Pekka Paalanenaef02542013-04-25 13:57:47 +0300998 for (i = 0; i < MAX_LEAVES; i++) {
999 leaf = &surface->leaf[i];
1000 if (leaf->data && leaf->data->buffer == buffer) {
1001 leaf->busy = 0;
1002 break;
1003 }
1004 }
1005 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001006
Pekka Paalanenaef02542013-04-25 13:57:47 +03001007 /* Leave one free leaf with storage, release others */
1008 free_found = 0;
1009 for (i = 0; i < MAX_LEAVES; i++) {
1010 leaf = &surface->leaf[i];
1011
1012 if (!leaf->cairo_surface || leaf->busy)
1013 continue;
1014
1015 if (!free_found)
1016 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001017 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001018 shm_surface_leaf_release(leaf);
1019 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001020
Pekka Paalanen97777442013-05-22 10:20:05 +03001021 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001022}
1023
1024static const struct wl_buffer_listener shm_surface_buffer_listener = {
1025 shm_surface_buffer_release
1026};
1027
Pekka Paalanen99436862012-11-19 17:15:59 +02001028static cairo_surface_t *
1029shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001030 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001031 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001032{
Pekka Paalanenec076692012-11-30 13:37:27 +02001033 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001034 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001035 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 struct shm_surface_leaf *leaf = NULL;
1037 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001038
1039 surface->dx = dx;
1040 surface->dy = dy;
1041
Pekka Paalanenaef02542013-04-25 13:57:47 +03001042 /* pick a free buffer, preferrably one that already has storage */
1043 for (i = 0; i < MAX_LEAVES; i++) {
1044 if (surface->leaf[i].busy)
1045 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001046
Pekka Paalanenaef02542013-04-25 13:57:47 +03001047 if (!leaf || surface->leaf[i].cairo_surface)
1048 leaf = &surface->leaf[i];
1049 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001050 DBG_OBJ(surface->surface, "pick leaf %d\n",
1051 (int)(leaf - &surface->leaf[0]));
1052
Pekka Paalanenaef02542013-04-25 13:57:47 +03001053 if (!leaf) {
1054 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001056 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001057 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001058 }
1059
Pekka Paalanena4eda732012-11-19 17:16:02 +02001060 if (!resize_hint && leaf->resize_pool) {
1061 cairo_surface_destroy(leaf->cairo_surface);
1062 leaf->cairo_surface = NULL;
1063 shm_pool_destroy(leaf->resize_pool);
1064 leaf->resize_pool = NULL;
1065 }
1066
Alexander Larsson1818e312013-05-22 14:41:31 +02001067 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1068
Pekka Paalanena4eda732012-11-19 17:16:02 +02001069 if (leaf->cairo_surface &&
1070 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1071 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001072 goto out;
1073
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 if (leaf->cairo_surface)
1075 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001077#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001078 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001079 /* Create a big pool to allocate from, while continuously
1080 * resizing. Mmapping a new pool in the server
1081 * is relatively expensive, so reusing a pool performs
1082 * better, but may temporarily reserve unneeded memory.
1083 */
1084 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001085 leaf->resize_pool = shm_pool_create(surface->display,
1086 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001087 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001088#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001089
Alexander Larsson1818e312013-05-22 14:41:31 +02001090 rect.width = width;
1091 rect.height = height;
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001094 display_create_shm_surface(surface->display, &rect,
1095 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 leaf->resize_pool,
1097 &leaf->data);
1098 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001099 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001100
1101out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001102 surface->current = leaf;
1103
1104 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001105}
1106
1107static void
1108shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001109 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001110 struct rectangle *server_allocation)
1111{
1112 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001113 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001114
1115 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001117 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001118 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001119
Alexander Larsson1818e312013-05-22 14:41:31 +02001120 buffer_to_surface_size (buffer_transform, buffer_scale,
1121 &server_allocation->width,
1122 &server_allocation->height);
1123
Pekka Paalanena4eda732012-11-19 17:16:02 +02001124 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001125 surface->dx, surface->dy);
1126 wl_surface_damage(surface->surface, 0, 0,
1127 server_allocation->width, server_allocation->height);
1128 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001129
Pekka Paalanen71233882013-04-25 13:57:53 +03001130 DBG_OBJ(surface->surface, "leaf %d busy\n",
1131 (int)(leaf - &surface->leaf[0]));
1132
Pekka Paalanena4eda732012-11-19 17:16:02 +02001133 leaf->busy = 1;
1134 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001135}
1136
1137static int
1138shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1139{
1140 return -1;
1141}
1142
1143static void
1144shm_surface_release(struct toysurface *base)
1145{
1146}
1147
1148static void
1149shm_surface_destroy(struct toysurface *base)
1150{
1151 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001152 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001153
Pekka Paalanenaef02542013-04-25 13:57:47 +03001154 for (i = 0; i < MAX_LEAVES; i++)
1155 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001156
1157 free(surface);
1158}
1159
1160static struct toysurface *
1161shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1162 uint32_t flags, struct rectangle *rectangle)
1163{
1164 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001165 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001166
1167 surface = calloc(1, sizeof *surface);
1168 if (!surface)
1169 return NULL;
1170
1171 surface->base.prepare = shm_surface_prepare;
1172 surface->base.swap = shm_surface_swap;
1173 surface->base.acquire = shm_surface_acquire;
1174 surface->base.release = shm_surface_release;
1175 surface->base.destroy = shm_surface_destroy;
1176
1177 surface->display = display;
1178 surface->surface = wl_surface;
1179 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001180
1181 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001182}
1183
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001184/*
1185 * The following correspondences between file names and cursors was copied
1186 * from: https://bugs.kde.org/attachment.cgi?id=67313
1187 */
1188
1189static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001190 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001191 "sw-resize",
1192 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001193};
1194
1195static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001196 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001197 "se-resize",
1198 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001199};
1200
1201static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001202 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001203 "s-resize",
1204 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001205};
1206
1207static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001209 "closedhand",
1210 "208530c400c041818281048008011002"
1211};
1212
1213static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001214 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215 "default",
1216 "top_left_arrow",
1217 "left-arrow"
1218};
1219
1220static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "w-resize",
1223 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "e-resize",
1229 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001234 "nw-resize",
1235 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236};
1237
1238static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001240 "ne-resize",
1241 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242};
1243
1244static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001246 "n-resize",
1247 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248};
1249
1250static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001251 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252 "ibeam",
1253 "text"
1254};
1255
1256static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001257 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258 "pointer",
1259 "pointing_hand",
1260 "e29285e634086352946a0e7090d73106"
1261};
1262
1263static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001264 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001265 "wait",
1266 "0426c94ea35c87780ff01dc239897213"
1267};
1268
1269struct cursor_alternatives {
1270 const char **names;
1271 size_t count;
1272};
1273
1274static const struct cursor_alternatives cursors[] = {
1275 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1276 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1277 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1278 {grabbings, ARRAY_LENGTH(grabbings)},
1279 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1280 {left_sides, ARRAY_LENGTH(left_sides)},
1281 {right_sides, ARRAY_LENGTH(right_sides)},
1282 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1283 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1284 {top_sides, ARRAY_LENGTH(top_sides)},
1285 {xterms, ARRAY_LENGTH(xterms)},
1286 {hand1s, ARRAY_LENGTH(hand1s)},
1287 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001288};
1289
1290static void
1291create_cursors(struct display *display)
1292{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001293 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001294 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001295 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001296 unsigned int i, j;
1297 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001298 struct config_key shell_keys[] = {
1299 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001300 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001301 };
1302 struct config_section cs[] = {
1303 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1304 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001305
Ossama Othmana50e6e42013-05-14 09:48:26 -07001306 config_fd = open_config_file("weston.ini");
1307 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1308 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001309
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001310 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001311 display->cursors =
1312 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1313
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001314 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001315 cursor = NULL;
1316 for (j = 0; !cursor && j < cursors[i].count; ++j)
1317 cursor = wl_cursor_theme_get_cursor(
1318 display->cursor_theme, cursors[i].names[j]);
1319
1320 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001321 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001322 cursors[i].names[0]);
1323
1324 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001325 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001326}
1327
1328static void
1329destroy_cursors(struct display *display)
1330{
1331 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001332 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001333}
1334
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001335struct wl_cursor_image *
1336display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001337{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001338 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001339
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001340 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001341}
1342
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001343static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001344surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001345{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001346 if (!surface->cairo_surface)
1347 return;
1348
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001349 if (surface->opaque_region) {
1350 wl_surface_set_opaque_region(surface->surface,
1351 surface->opaque_region);
1352 wl_region_destroy(surface->opaque_region);
1353 surface->opaque_region = NULL;
1354 }
1355
1356 if (surface->input_region) {
1357 wl_surface_set_input_region(surface->surface,
1358 surface->input_region);
1359 wl_region_destroy(surface->input_region);
1360 surface->input_region = NULL;
1361 }
1362
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001363 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001364 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001365 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001366
Pekka Paalanen89dee002013-02-13 16:17:20 +02001367 cairo_surface_destroy(surface->cairo_surface);
1368 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001369}
1370
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001371int
1372window_has_focus(struct window *window)
1373{
1374 return window->focus_count > 0;
1375}
1376
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001377static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001378window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001379{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001380 struct surface *surface;
1381
Pekka Paalanen89dee002013-02-13 16:17:20 +02001382 if (window->type == TYPE_NONE) {
1383 window->type = TYPE_TOPLEVEL;
1384 if (window->shell_surface)
1385 wl_shell_surface_set_toplevel(window->shell_surface);
1386 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001387
Pekka Paalanen35e82632013-04-25 13:57:48 +03001388 wl_list_for_each(surface, &window->subsurface_list, link) {
1389 if (surface == window->main_surface)
1390 continue;
1391
1392 surface_flush(surface);
1393 }
1394
Pekka Paalanen89dee002013-02-13 16:17:20 +02001395 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001396}
1397
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001398struct display *
1399window_get_display(struct window *window)
1400{
1401 return window->display;
1402}
1403
Pekka Paalanen89dee002013-02-13 16:17:20 +02001404static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001405surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001406{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001408 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001409
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001410 if (!surface->toysurface && display->dpy &&
1411 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001412 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001413 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001414 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001415 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001416 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001417 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001418
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001419 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001420 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001421 surface->surface,
1422 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001423
Pekka Paalanen89dee002013-02-13 16:17:20 +02001424 surface->cairo_surface = surface->toysurface->prepare(
1425 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001426 allocation.width, allocation.height, flags,
1427 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001428}
1429
1430static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001431window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001432{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001433 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001434 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001435 int dx = 0;
1436 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001437
Pekka Paalanenec076692012-11-30 13:37:27 +02001438 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001439 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001440
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001441 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1442 flags |= SURFACE_HINT_RGB565;
1443
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001444 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1445 dx = surface->server_allocation.width -
1446 surface->allocation.width;
1447
1448 if (window->resize_edges & WINDOW_RESIZING_TOP)
1449 dy = surface->server_allocation.height -
1450 surface->allocation.height;
1451
1452 window->resize_edges = 0;
1453
Pekka Paalanen89dee002013-02-13 16:17:20 +02001454 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001455}
1456
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001457int
1458window_get_buffer_transform(struct window *window)
1459{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001460 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001461}
1462
1463void
1464window_set_buffer_transform(struct window *window,
1465 enum wl_output_transform transform)
1466{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001467 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001468 wl_surface_set_buffer_transform(window->main_surface->surface,
1469 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001470}
1471
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001472void
1473window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001474 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001475{
1476 window->main_surface->buffer_scale = scale;
1477 wl_surface_set_buffer_scale(window->main_surface->surface,
1478 scale);
1479}
1480
1481uint32_t
1482window_get_buffer_scale(struct window *window)
1483{
1484 return window->main_surface->buffer_scale;
1485}
1486
Alexander Larssond68f5232013-05-22 14:41:33 +02001487uint32_t
1488window_get_output_scale(struct window *window)
1489{
1490 struct window_output *window_output;
1491 struct window_output *window_output_tmp;
1492 int scale = 1;
1493
1494 wl_list_for_each_safe(window_output, window_output_tmp,
1495 &window->window_output_list, link) {
1496 if (window_output->output->scale > scale)
1497 scale = window_output->output->scale;
1498 }
1499
1500 return scale;
1501}
1502
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001503static void frame_destroy(struct frame *frame);
1504
Pekka Paalanen4e373742013-02-13 16:17:13 +02001505static void
1506surface_destroy(struct surface *surface)
1507{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001508 if (surface->frame_cb)
1509 wl_callback_destroy(surface->frame_cb);
1510
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001511 if (surface->input_region)
1512 wl_region_destroy(surface->input_region);
1513
1514 if (surface->opaque_region)
1515 wl_region_destroy(surface->opaque_region);
1516
Pekka Paalanen35e82632013-04-25 13:57:48 +03001517 if (surface->subsurface)
1518 wl_subsurface_destroy(surface->subsurface);
1519
Pekka Paalanen4e373742013-02-13 16:17:13 +02001520 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001521
1522 if (surface->toysurface)
1523 surface->toysurface->destroy(surface->toysurface);
1524
Pekka Paalanen35e82632013-04-25 13:57:48 +03001525 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001526 free(surface);
1527}
1528
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001529void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001530window_destroy(struct window *window)
1531{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001532 struct display *display = window->display;
1533 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001534 struct window_output *window_output;
1535 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001536
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001537 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001538
1539 wl_list_for_each(input, &display->input_list, link) {
1540 if (input->pointer_focus == window)
1541 input->pointer_focus = NULL;
1542 if (input->keyboard_focus == window)
1543 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001544 if (input->focus_widget &&
1545 input->focus_widget->window == window)
1546 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001547 }
1548
Rob Bradford7507b572012-05-15 17:55:34 +01001549 wl_list_for_each_safe(window_output, window_output_tmp,
1550 &window->window_output_list, link) {
1551 free (window_output);
1552 }
1553
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001554 if (window->frame)
1555 frame_destroy(window->frame);
1556
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001557 if (window->shell_surface)
1558 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001559
1560 surface_destroy(window->main_surface);
1561
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001562 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001563
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001564 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001565 free(window);
1566}
1567
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001568static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001569widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001570{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001571 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001572
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001573 wl_list_for_each(child, &widget->child_list, link) {
1574 target = widget_find_widget(child, x, y);
1575 if (target)
1576 return target;
1577 }
1578
1579 if (widget->allocation.x <= x &&
1580 x < widget->allocation.x + widget->allocation.width &&
1581 widget->allocation.y <= y &&
1582 y < widget->allocation.y + widget->allocation.height) {
1583 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001584 }
1585
1586 return NULL;
1587}
1588
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001589static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001590window_find_widget(struct window *window, int32_t x, int32_t y)
1591{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001592 struct surface *surface;
1593 struct widget *widget;
1594
1595 wl_list_for_each(surface, &window->subsurface_list, link) {
1596 widget = widget_find_widget(surface->widget, x, y);
1597 if (widget)
1598 return widget;
1599 }
1600
1601 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001602}
1603
1604static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001605widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001606{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001607 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001608
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001609 widget = malloc(sizeof *widget);
1610 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001611 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001612 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001613 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001614 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001615 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001616 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001617 widget->tooltip = NULL;
1618 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001619 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001620
1621 return widget;
1622}
1623
1624struct widget *
1625window_add_widget(struct window *window, void *data)
1626{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001627 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001628
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001629 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001630 wl_list_init(&widget->link);
1631 window->main_surface->widget = widget;
1632
1633 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001634}
1635
1636struct widget *
1637widget_add_widget(struct widget *parent, void *data)
1638{
1639 struct widget *widget;
1640
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001641 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001642 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001643
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001644 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001645}
1646
1647void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001648widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001649{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001650 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001651 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001652 struct input *input;
1653
Pekka Paalanen35e82632013-04-25 13:57:48 +03001654 /* Destroy the sub-surface along with the root widget */
1655 if (surface->widget == widget && surface->subsurface)
1656 surface_destroy(widget->surface);
1657
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001658 if (widget->tooltip) {
1659 free(widget->tooltip);
1660 widget->tooltip = NULL;
1661 }
1662
Pekka Paalanene156fb62012-01-19 13:51:38 +02001663 wl_list_for_each(input, &display->input_list, link) {
1664 if (input->focus_widget == widget)
1665 input->focus_widget = NULL;
1666 }
1667
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001668 wl_list_remove(&widget->link);
1669 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001670}
1671
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001672void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001673widget_set_default_cursor(struct widget *widget, int cursor)
1674{
1675 widget->default_cursor = cursor;
1676}
1677
1678void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001679widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001680{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001681 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001682}
1683
1684void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001685widget_set_size(struct widget *widget, int32_t width, int32_t height)
1686{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001687 widget->allocation.width = width;
1688 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001689}
1690
1691void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001692widget_set_allocation(struct widget *widget,
1693 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001695 widget->allocation.x = x;
1696 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001697 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001698}
1699
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001700void
1701widget_set_transparent(struct widget *widget, int transparent)
1702{
1703 widget->opaque = !transparent;
1704}
1705
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001707widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001709 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001710}
1711
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001712static cairo_surface_t *
1713widget_get_cairo_surface(struct widget *widget)
1714{
1715 struct surface *surface = widget->surface;
1716 struct window *window = widget->window;
1717
1718 if (!surface->cairo_surface) {
1719 if (surface == window->main_surface)
1720 window_create_main_surface(window);
1721 else
1722 surface_create_surface(surface, 0, 0, 0);
1723 }
1724
1725 return surface->cairo_surface;
1726}
1727
Alexander Larsson15901f02013-05-22 14:41:25 +02001728static void
1729widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1730{
1731 struct surface *surface = widget->surface;
1732 double angle;
1733 cairo_matrix_t m;
1734 enum wl_output_transform transform;
1735 int surface_width, surface_height;
1736 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001737 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001738
1739 surface_width = surface->allocation.width;
1740 surface_height = surface->allocation.height;
1741
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001742 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001743 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001744
Alexander Larsson15901f02013-05-22 14:41:25 +02001745 switch (transform) {
1746 case WL_OUTPUT_TRANSFORM_FLIPPED:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1748 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1749 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1750 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1751 break;
1752 default:
1753 cairo_matrix_init_identity(&m);
1754 break;
1755 }
1756
1757 switch (transform) {
1758 case WL_OUTPUT_TRANSFORM_NORMAL:
1759 default:
1760 angle = 0;
1761 translate_x = 0;
1762 translate_y = 0;
1763 break;
1764 case WL_OUTPUT_TRANSFORM_FLIPPED:
1765 angle = 0;
1766 translate_x = surface_width;
1767 translate_y = 0;
1768 break;
1769 case WL_OUTPUT_TRANSFORM_90:
1770 angle = M_PI_2;
1771 translate_x = surface_height;
1772 translate_y = 0;
1773 break;
1774 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1775 angle = M_PI_2;
1776 translate_x = surface_height;
1777 translate_y = surface_width;
1778 break;
1779 case WL_OUTPUT_TRANSFORM_180:
1780 angle = M_PI;
1781 translate_x = surface_width;
1782 translate_y = surface_height;
1783 break;
1784 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1785 angle = M_PI;
1786 translate_x = 0;
1787 translate_y = surface_height;
1788 break;
1789 case WL_OUTPUT_TRANSFORM_270:
1790 angle = M_PI + M_PI_2;
1791 translate_x = 0;
1792 translate_y = surface_width;
1793 break;
1794 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1795 angle = M_PI + M_PI_2;
1796 translate_x = 0;
1797 translate_y = 0;
1798 break;
1799 }
1800
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001801 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001802 cairo_translate(cr, translate_x, translate_y);
1803 cairo_rotate(cr, angle);
1804 cairo_transform(cr, &m);
1805}
1806
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001807cairo_t *
1808widget_cairo_create(struct widget *widget)
1809{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001810 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001811 cairo_surface_t *cairo_surface;
1812 cairo_t *cr;
1813
1814 cairo_surface = widget_get_cairo_surface(widget);
1815 cr = cairo_create(cairo_surface);
1816
Alexander Larsson15901f02013-05-22 14:41:25 +02001817 widget_cairo_update_transform(widget, cr);
1818
Pekka Paalanen35e82632013-04-25 13:57:48 +03001819 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1820
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001821 return cr;
1822}
1823
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001824struct wl_surface *
1825widget_get_wl_surface(struct widget *widget)
1826{
1827 return widget->surface->surface;
1828}
1829
1830uint32_t
1831widget_get_last_time(struct widget *widget)
1832{
1833 return widget->surface->last_time;
1834}
1835
1836void
1837widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1838{
1839 struct wl_compositor *comp = widget->window->display->compositor;
1840 struct surface *surface = widget->surface;
1841
1842 if (!surface->input_region)
1843 surface->input_region = wl_compositor_create_region(comp);
1844
1845 if (rect) {
1846 wl_region_add(surface->input_region,
1847 rect->x, rect->y, rect->width, rect->height);
1848 }
1849}
1850
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001851void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001852widget_set_resize_handler(struct widget *widget,
1853 widget_resize_handler_t handler)
1854{
1855 widget->resize_handler = handler;
1856}
1857
1858void
1859widget_set_redraw_handler(struct widget *widget,
1860 widget_redraw_handler_t handler)
1861{
1862 widget->redraw_handler = handler;
1863}
1864
1865void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001866widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001867{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001868 widget->enter_handler = handler;
1869}
1870
1871void
1872widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1873{
1874 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001875}
1876
1877void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001878widget_set_motion_handler(struct widget *widget,
1879 widget_motion_handler_t handler)
1880{
1881 widget->motion_handler = handler;
1882}
1883
1884void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001885widget_set_button_handler(struct widget *widget,
1886 widget_button_handler_t handler)
1887{
1888 widget->button_handler = handler;
1889}
1890
1891void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001892widget_set_axis_handler(struct widget *widget,
1893 widget_axis_handler_t handler)
1894{
1895 widget->axis_handler = handler;
1896}
1897
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001898static void
1899window_schedule_redraw_task(struct window *window);
1900
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001901void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001902widget_schedule_redraw(struct widget *widget)
1903{
Pekka Paalanen71233882013-04-25 13:57:53 +03001904 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001905 widget->surface->redraw_needed = 1;
1906 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001907}
1908
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001909cairo_surface_t *
1910window_get_surface(struct window *window)
1911{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001912 cairo_surface_t *cairo_surface;
1913
1914 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1915
1916 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001917}
1918
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001919struct wl_surface *
1920window_get_wl_surface(struct window *window)
1921{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001922 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001923}
1924
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001925struct wl_shell_surface *
1926window_get_wl_shell_surface(struct window *window)
1927{
1928 return window->shell_surface;
1929}
1930
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001931static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001932tooltip_redraw_handler(struct widget *widget, void *data)
1933{
1934 cairo_t *cr;
1935 const int32_t r = 3;
1936 struct tooltip *tooltip = data;
1937 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001938
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001939 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001940 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1941 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1942 cairo_paint(cr);
1943
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001944 width = widget->allocation.width;
1945 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001946 rounded_rect(cr, 0, 0, width, height, r);
1947
1948 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1949 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1950 cairo_fill(cr);
1951
1952 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1953 cairo_move_to(cr, 10, 16);
1954 cairo_show_text(cr, tooltip->entry);
1955 cairo_destroy(cr);
1956}
1957
1958static cairo_text_extents_t
1959get_text_extents(struct tooltip *tooltip)
1960{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001961 cairo_t *cr;
1962 cairo_text_extents_t extents;
1963
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001964 /* Use the dummy_surface because tooltip's surface was not
1965 * created yet, and parent does not have a valid surface
1966 * outside repaint, either.
1967 */
1968 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001969 cairo_text_extents(cr, tooltip->entry, &extents);
1970 cairo_destroy(cr);
1971
1972 return extents;
1973}
1974
1975static int
1976window_create_tooltip(struct tooltip *tooltip)
1977{
1978 struct widget *parent = tooltip->parent;
1979 struct display *display = parent->window->display;
1980 struct window *window;
1981 const int offset_y = 27;
1982 const int margin = 3;
1983 cairo_text_extents_t extents;
1984
1985 if (tooltip->widget)
1986 return 0;
1987
1988 window = window_create_transient(display, parent->window, tooltip->x,
1989 tooltip->y + offset_y,
1990 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1991 if (!window)
1992 return -1;
1993
1994 tooltip->window = window;
1995 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1996
1997 extents = get_text_extents(tooltip);
1998 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1999 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2000
2001 return 0;
2002}
2003
2004void
2005widget_destroy_tooltip(struct widget *parent)
2006{
2007 struct tooltip *tooltip = parent->tooltip;
2008
2009 parent->tooltip_count = 0;
2010 if (!tooltip)
2011 return;
2012
2013 if (tooltip->widget) {
2014 widget_destroy(tooltip->widget);
2015 window_destroy(tooltip->window);
2016 tooltip->widget = NULL;
2017 tooltip->window = NULL;
2018 }
2019
2020 close(tooltip->tooltip_fd);
2021 free(tooltip->entry);
2022 free(tooltip);
2023 parent->tooltip = NULL;
2024}
2025
2026static void
2027tooltip_func(struct task *task, uint32_t events)
2028{
2029 struct tooltip *tooltip =
2030 container_of(task, struct tooltip, tooltip_task);
2031 uint64_t exp;
2032
Martin Olsson8df662a2012-07-08 03:03:47 +02002033 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2034 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002035 window_create_tooltip(tooltip);
2036}
2037
2038#define TOOLTIP_TIMEOUT 500
2039static int
2040tooltip_timer_reset(struct tooltip *tooltip)
2041{
2042 struct itimerspec its;
2043
2044 its.it_interval.tv_sec = 0;
2045 its.it_interval.tv_nsec = 0;
2046 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2047 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2048 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2049 fprintf(stderr, "could not set timerfd\n: %m");
2050 return -1;
2051 }
2052
2053 return 0;
2054}
2055
2056int
2057widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2058{
2059 struct tooltip *tooltip = parent->tooltip;
2060
2061 parent->tooltip_count++;
2062 if (tooltip) {
2063 tooltip->x = x;
2064 tooltip->y = y;
2065 tooltip_timer_reset(tooltip);
2066 return 0;
2067 }
2068
2069 /* the handler might be triggered too fast via input device motion, so
2070 * we need this check here to make sure tooltip is fully initialized */
2071 if (parent->tooltip_count > 1)
2072 return 0;
2073
2074 tooltip = malloc(sizeof *tooltip);
2075 if (!tooltip)
2076 return -1;
2077
2078 parent->tooltip = tooltip;
2079 tooltip->parent = parent;
2080 tooltip->widget = NULL;
2081 tooltip->window = NULL;
2082 tooltip->x = x;
2083 tooltip->y = y;
2084 tooltip->entry = strdup(entry);
2085 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2086 if (tooltip->tooltip_fd < 0) {
2087 fprintf(stderr, "could not create timerfd\n: %m");
2088 return -1;
2089 }
2090
2091 tooltip->tooltip_task.run = tooltip_func;
2092 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2093 EPOLLIN, &tooltip->tooltip_task);
2094 tooltip_timer_reset(tooltip);
2095
2096 return 0;
2097}
2098
2099static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002100workspace_manager_state(void *data,
2101 struct workspace_manager *workspace_manager,
2102 uint32_t current,
2103 uint32_t count)
2104{
2105 struct display *display = data;
2106
2107 display->workspace = current;
2108 display->workspace_count = count;
2109}
2110
2111static const struct workspace_manager_listener workspace_manager_listener = {
2112 workspace_manager_state
2113};
2114
2115static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002116frame_resize_handler(struct widget *widget,
2117 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002118{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002119 struct frame *frame = data;
2120 struct widget *child = frame->child;
2121 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002122 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002123 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002124 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002125 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002126 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002127 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002128 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002129
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002130 switch (widget->window->type) {
2131 case TYPE_FULLSCREEN:
2132 decoration_width = 0;
2133 decoration_height = 0;
2134
2135 allocation.x = 0;
2136 allocation.y = 0;
2137 allocation.width = width;
2138 allocation.height = height;
2139 opaque_margin = 0;
2140
2141 wl_list_for_each(button, &frame->buttons_list, link)
2142 button->widget->opaque = 1;
2143 break;
2144 case TYPE_MAXIMIZED:
2145 decoration_width = t->width * 2;
2146 decoration_height = t->width + t->titlebar_height;
2147
2148 allocation.x = t->width;
2149 allocation.y = t->titlebar_height;
2150 allocation.width = width - decoration_width;
2151 allocation.height = height - decoration_height;
2152
2153 opaque_margin = 0;
2154
2155 wl_list_for_each(button, &frame->buttons_list, link)
2156 button->widget->opaque = 0;
2157 break;
2158 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002159 decoration_width = (t->width + t->margin) * 2;
2160 decoration_height = t->width +
2161 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002163 allocation.x = t->width + t->margin;
2164 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002165 allocation.width = width - decoration_width;
2166 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002167
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002168 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002169
2170 wl_list_for_each(button, &frame->buttons_list, link)
2171 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002172 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002173 }
2174
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002175 widget_set_allocation(child, allocation.x, allocation.y,
2176 allocation.width, allocation.height);
2177
2178 if (child->resize_handler)
2179 child->resize_handler(child,
2180 allocation.width,
2181 allocation.height,
2182 child->user_data);
2183
Scott Moreauf7e498c2012-05-14 11:39:29 -06002184 width = child->allocation.width + decoration_width;
2185 height = child->allocation.height + decoration_height;
2186
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002187 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2188
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002189 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002190 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002191 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002192 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002193 shadow_margin, shadow_margin,
2194 width - 2 * shadow_margin,
2195 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002196 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002197 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002198 }
2199
Scott Moreauf7e498c2012-05-14 11:39:29 -06002200 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002201
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002202 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002203 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002204 opaque_margin, opaque_margin,
2205 widget->allocation.width - 2 * opaque_margin,
2206 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002207
2208 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002209 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2210 x_l = t->width + shadow_margin;
2211 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002212 wl_list_for_each(button, &frame->buttons_list, link) {
2213 const int button_padding = 4;
2214 w = cairo_image_surface_get_width(button->icon);
2215 h = cairo_image_surface_get_height(button->icon);
2216
2217 if (button->decoration == FRAME_BUTTON_FANCY)
2218 w += 10;
2219
2220 if (button->align == FRAME_BUTTON_LEFT) {
2221 widget_set_allocation(button->widget,
2222 x_l, y , w + 1, h + 1);
2223 x_l += w;
2224 x_l += button_padding;
2225 } else {
2226 x_r -= w;
2227 widget_set_allocation(button->widget,
2228 x_r, y , w + 1, h + 1);
2229 x_r -= button_padding;
2230 }
2231 }
2232}
2233
2234static int
2235frame_button_enter_handler(struct widget *widget,
2236 struct input *input, float x, float y, void *data)
2237{
2238 struct frame_button *frame_button = data;
2239
2240 widget_schedule_redraw(frame_button->widget);
2241 frame_button->state = FRAME_BUTTON_OVER;
2242
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002243 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002244}
2245
2246static void
2247frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2248{
2249 struct frame_button *frame_button = data;
2250
2251 widget_schedule_redraw(frame_button->widget);
2252 frame_button->state = FRAME_BUTTON_DEFAULT;
2253}
2254
2255static void
2256frame_button_button_handler(struct widget *widget,
2257 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002258 uint32_t button,
2259 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002260{
2261 struct frame_button *frame_button = data;
2262 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002263 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002264
2265 if (button != BTN_LEFT)
2266 return;
2267
2268 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002269 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002270 frame_button->state = FRAME_BUTTON_ACTIVE;
2271 widget_schedule_redraw(frame_button->widget);
2272
2273 if (frame_button->type == FRAME_BUTTON_ICON)
2274 window_show_frame_menu(window, input, time);
2275 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002276 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002277 frame_button->state = FRAME_BUTTON_DEFAULT;
2278 widget_schedule_redraw(frame_button->widget);
2279 break;
2280 }
2281
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002282 if (!was_pressed)
2283 return;
2284
Martin Minarik1998b152012-05-10 02:04:35 +02002285 switch (frame_button->type) {
2286 case FRAME_BUTTON_CLOSE:
2287 if (window->close_handler)
2288 window->close_handler(window->parent,
2289 window->user_data);
2290 else
2291 display_exit(window->display);
2292 break;
2293 case FRAME_BUTTON_MINIMIZE:
2294 fprintf(stderr,"Minimize stub\n");
2295 break;
2296 case FRAME_BUTTON_MAXIMIZE:
2297 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2298 break;
2299 default:
2300 /* Unknown operation */
2301 break;
2302 }
2303}
2304
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002305static int
2306frame_button_motion_handler(struct widget *widget,
2307 struct input *input, uint32_t time,
2308 float x, float y, void *data)
2309{
2310 struct frame_button *frame_button = data;
2311 enum frame_button_pointer previous_button_state = frame_button->state;
2312
2313 /* only track state for a pressed button */
2314 if (input->grab != widget)
2315 return CURSOR_LEFT_PTR;
2316
2317 if (x > widget->allocation.x &&
2318 x < (widget->allocation.x + widget->allocation.width) &&
2319 y > widget->allocation.y &&
2320 y < (widget->allocation.y + widget->allocation.height)) {
2321 frame_button->state = FRAME_BUTTON_ACTIVE;
2322 } else {
2323 frame_button->state = FRAME_BUTTON_DEFAULT;
2324 }
2325
2326 if (frame_button->state != previous_button_state)
2327 widget_schedule_redraw(frame_button->widget);
2328
2329 return CURSOR_LEFT_PTR;
2330}
2331
Martin Minarik1998b152012-05-10 02:04:35 +02002332static void
2333frame_button_redraw_handler(struct widget *widget, void *data)
2334{
2335 struct frame_button *frame_button = data;
2336 cairo_t *cr;
2337 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002338
2339 x = widget->allocation.x;
2340 y = widget->allocation.y;
2341 width = widget->allocation.width;
2342 height = widget->allocation.height;
2343
2344 if (!width)
2345 return;
2346 if (!height)
2347 return;
2348 if (widget->opaque)
2349 return;
2350
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002351 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002352
2353 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2354 cairo_set_line_width(cr, 1);
2355
2356 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2357 cairo_rectangle (cr, x, y, 25, 16);
2358
2359 cairo_stroke_preserve(cr);
2360
2361 switch (frame_button->state) {
2362 case FRAME_BUTTON_DEFAULT:
2363 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2364 break;
2365 case FRAME_BUTTON_OVER:
2366 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2367 break;
2368 case FRAME_BUTTON_ACTIVE:
2369 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2370 break;
2371 }
2372
2373 cairo_fill (cr);
2374
2375 x += 4;
2376 }
2377
2378 cairo_set_source_surface(cr, frame_button->icon, x, y);
2379 cairo_paint(cr);
2380
2381 cairo_destroy(cr);
2382}
2383
2384static struct widget *
2385frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2386 enum frame_button_align align, enum frame_button_decoration style)
2387{
2388 struct frame_button *frame_button;
2389 const char *icon = data;
2390
2391 frame_button = malloc (sizeof *frame_button);
2392 memset(frame_button, 0, sizeof *frame_button);
2393
2394 frame_button->icon = cairo_image_surface_create_from_png(icon);
2395 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2396 frame_button->frame = frame;
2397 frame_button->type = type;
2398 frame_button->align = align;
2399 frame_button->decoration = style;
2400
2401 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2402
2403 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2404 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2405 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2406 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002407 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002408 return frame_button->widget;
2409}
2410
2411static void
2412frame_button_destroy(struct frame_button *frame_button)
2413{
2414 widget_destroy(frame_button->widget);
2415 wl_list_remove(&frame_button->link);
2416 cairo_surface_destroy(frame_button->icon);
2417 free(frame_button);
2418
2419 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002420}
2421
2422static void
2423frame_redraw_handler(struct widget *widget, void *data)
2424{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002425 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002426 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002427 struct theme *t = window->display->theme;
2428 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002429
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002430 if (window->type == TYPE_FULLSCREEN)
2431 return;
2432
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002433 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002434
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002435 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002436 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002437 if (window->type == TYPE_MAXIMIZED)
2438 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002439 theme_render_frame(t, cr, widget->allocation.width,
2440 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002441
2442 cairo_destroy(cr);
2443}
2444
2445static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002446frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002447{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002448 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002449 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002450 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002451
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002452 if (window->type != TYPE_TOPLEVEL)
2453 return CURSOR_LEFT_PTR;
2454
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002455 location = theme_get_location(t, input->sx, input->sy,
2456 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002457 frame->widget->allocation.height,
2458 window->type == TYPE_MAXIMIZED ?
2459 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002460
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002461 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002462 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002463 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002464 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002465 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002466 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002467 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002468 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002469 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002470 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002471 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002472 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002473 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002474 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002475 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002476 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002477 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002478 case THEME_LOCATION_EXTERIOR:
2479 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002480 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002481 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002482 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002483}
2484
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002485static void
2486frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002487{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002488 struct display *display;
2489
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002490 switch (index) {
2491 case 0: /* close */
2492 if (window->close_handler)
2493 window->close_handler(window->parent,
2494 window->user_data);
2495 else
2496 display_exit(window->display);
2497 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002498 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002499 display = window->display;
2500 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002501 workspace_manager_move_surface(
2502 display->workspace_manager,
2503 window->main_surface->surface,
2504 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002505 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002506 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002507 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002508 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002509 workspace_manager_move_surface(
2510 display->workspace_manager,
2511 window->main_surface->surface,
2512 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002513 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002514 case 3: /* fullscreen */
2515 /* we don't have a way to get out of fullscreen for now */
2516 if (window->fullscreen_handler)
2517 window->fullscreen_handler(window, window->user_data);
2518 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002519 }
2520}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002521
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002522void
2523window_show_frame_menu(struct window *window,
2524 struct input *input, uint32_t time)
2525{
2526 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002527 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002528
2529 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002530 "Close",
2531 "Move to workspace above", "Move to workspace below",
2532 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002533 };
2534
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002535 if (window->fullscreen_handler)
2536 count = ARRAY_LENGTH(entries);
2537 else
2538 count = ARRAY_LENGTH(entries) - 1;
2539
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002540 input_get_position(input, &x, &y);
2541 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002542 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002543}
2544
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002545static int
2546frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002547 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002548{
2549 return frame_get_pointer_image_for_location(data, input);
2550}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002551
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002552static int
2553frame_motion_handler(struct widget *widget,
2554 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002555 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002556{
2557 return frame_get_pointer_image_for_location(data, input);
2558}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002559
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002560static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002561frame_button_handler(struct widget *widget,
2562 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002563 uint32_t button, enum wl_pointer_button_state state,
2564 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002565
2566{
2567 struct frame *frame = data;
2568 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002569 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002570 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002571
Rob Bradford3c9d9672013-07-11 10:35:55 +01002572 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002573 return;
2574
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002575 location = theme_get_location(display->theme, input->sx, input->sy,
2576 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002577 frame->widget->allocation.height,
2578 window->type == TYPE_MAXIMIZED ?
2579 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002580
Daniel Stone4dbadb12012-05-30 16:31:51 +01002581 if (window->display->shell && button == BTN_LEFT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002582 window->type == TYPE_TOPLEVEL) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002583 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002584 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002585 if (!window->shell_surface)
2586 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002587 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002588 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002589 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002590 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002591 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002592 case THEME_LOCATION_RESIZING_TOP:
2593 case THEME_LOCATION_RESIZING_BOTTOM:
2594 case THEME_LOCATION_RESIZING_LEFT:
2595 case THEME_LOCATION_RESIZING_RIGHT:
2596 case THEME_LOCATION_RESIZING_TOP_LEFT:
2597 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2598 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2599 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002600 if (!window->shell_surface)
2601 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002602 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002603
Pekka Paalanen99436862012-11-19 17:15:59 +02002604 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002605 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002606 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002607 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002608 break;
2609 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002610 } else if (button == BTN_RIGHT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002611 (window->type == TYPE_TOPLEVEL ||
2612 window->type == TYPE_MAXIMIZED)) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002613 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002614 }
2615}
2616
2617struct widget *
2618frame_create(struct window *window, void *data)
2619{
2620 struct frame *frame;
2621
Kristian Høgsberg8eeb30b2013-07-25 16:25:15 -07002622 frame = xmalloc(sizeof *frame);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002623 memset(frame, 0, sizeof *frame);
2624
2625 frame->widget = window_add_widget(window, frame);
2626 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002627
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002628 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2629 widget_set_resize_handler(frame->widget, frame_resize_handler);
2630 widget_set_enter_handler(frame->widget, frame_enter_handler);
2631 widget_set_motion_handler(frame->widget, frame_motion_handler);
2632 widget_set_button_handler(frame->widget, frame_button_handler);
2633
Martin Minarik1998b152012-05-10 02:04:35 +02002634 /* Create empty list for frame buttons */
2635 wl_list_init(&frame->buttons_list);
2636
2637 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2638 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2639
2640 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2641 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2642
2643 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2644 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2645
2646 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2647 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2648
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002649 window->frame = frame;
2650
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002651 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002652}
2653
Kristian Høgsberga1627922012-06-20 17:30:03 -04002654void
2655frame_set_child_size(struct widget *widget, int child_width, int child_height)
2656{
2657 struct display *display = widget->window->display;
2658 struct theme *t = display->theme;
2659 int decoration_width, decoration_height;
2660 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002661 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002662
2663 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002664 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002665 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002666 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002667
2668 width = child_width + decoration_width;
2669 height = child_height + decoration_height;
2670 } else {
2671 width = child_width;
2672 height = child_height;
2673 }
2674
2675 window_schedule_resize(widget->window, width, height);
2676}
2677
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002678static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002679frame_destroy(struct frame *frame)
2680{
Martin Minarik1998b152012-05-10 02:04:35 +02002681 struct frame_button *button, *tmp;
2682
2683 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2684 frame_button_destroy(button);
2685
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002686 /* frame->child must be destroyed by the application */
2687 widget_destroy(frame->widget);
2688 free(frame);
2689}
2690
2691static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002692input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002693 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002694{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002695 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002696 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002697
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002698 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002699 return;
2700
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002701 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002702 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002703 widget = old;
2704 if (input->grab)
2705 widget = input->grab;
2706 if (widget->leave_handler)
2707 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002708 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002709 }
2710
2711 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002712 widget = focus;
2713 if (input->grab)
2714 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002715 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002716 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002717 cursor = widget->enter_handler(focus, input, x, y,
2718 widget->user_data);
2719 else
2720 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002721
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002722 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002723 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002724}
2725
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002726void
2727input_grab(struct input *input, struct widget *widget, uint32_t button)
2728{
2729 input->grab = widget;
2730 input->grab_button = button;
2731}
2732
2733void
2734input_ungrab(struct input *input)
2735{
2736 struct widget *widget;
2737
2738 input->grab = NULL;
2739 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002740 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002741 input->sx, input->sy);
2742 input_set_focus_widget(input, widget, input->sx, input->sy);
2743 }
2744}
2745
2746static void
2747input_remove_pointer_focus(struct input *input)
2748{
2749 struct window *window = input->pointer_focus;
2750
2751 if (!window)
2752 return;
2753
2754 input_set_focus_widget(input, NULL, 0, 0);
2755
2756 input->pointer_focus = NULL;
2757 input->current_cursor = CURSOR_UNSET;
2758}
2759
2760static void
2761pointer_handle_enter(void *data, struct wl_pointer *pointer,
2762 uint32_t serial, struct wl_surface *surface,
2763 wl_fixed_t sx_w, wl_fixed_t sy_w)
2764{
2765 struct input *input = data;
2766 struct window *window;
2767 struct widget *widget;
2768 float sx = wl_fixed_to_double(sx_w);
2769 float sy = wl_fixed_to_double(sy_w);
2770
2771 if (!surface) {
2772 /* enter event for a window we've just destroyed */
2773 return;
2774 }
2775
2776 input->display->serial = serial;
2777 input->pointer_enter_serial = serial;
2778 input->pointer_focus = wl_surface_get_user_data(surface);
2779 window = input->pointer_focus;
2780
Pekka Paalanen99436862012-11-19 17:15:59 +02002781 if (window->resizing) {
2782 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002783 /* Schedule a redraw to free the pool */
2784 window_schedule_redraw(window);
2785 }
2786
2787 input->sx = sx;
2788 input->sy = sy;
2789
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002790 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002791 input_set_focus_widget(input, widget, sx, sy);
2792}
2793
2794static void
2795pointer_handle_leave(void *data, struct wl_pointer *pointer,
2796 uint32_t serial, struct wl_surface *surface)
2797{
2798 struct input *input = data;
2799
2800 input->display->serial = serial;
2801 input_remove_pointer_focus(input);
2802}
2803
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002804static void
Daniel Stone37816df2012-05-16 18:45:18 +01002805pointer_handle_motion(void *data, struct wl_pointer *pointer,
2806 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002807{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002808 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002809 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002810 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002811 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002812 float sx = wl_fixed_to_double(sx_w);
2813 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002814
Rob Bradford5f087742013-07-11 19:41:27 +01002815 /* when making the window smaller - e.g. after a unmaximise we might
2816 * still have a pending motion event that the compositor has picked
2817 * based on the old surface dimensions
2818 */
2819 if (sx > window->main_surface->allocation.width ||
2820 sy > window->main_surface->allocation.height)
2821 return;
2822
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002823 input->sx = sx;
2824 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002825
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002826 if (!window)
2827 return;
2828
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002829 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002830 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002831 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002832 }
2833
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002834 if (input->grab)
2835 widget = input->grab;
2836 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002837 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002838 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002839 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002840 input, time, sx, sy,
2841 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002842 else
2843 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002844
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002845 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002846}
2847
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002848static void
Daniel Stone37816df2012-05-16 18:45:18 +01002849pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002850 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002851{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002852 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002853 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002854 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002855
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002856 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002857 if (input->focus_widget && input->grab == NULL &&
2858 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002859 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002860
Neil Roberts6b28aad2012-01-23 19:11:18 +00002861 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002862 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002863 (*widget->button_handler)(widget,
2864 input, time,
2865 button, state,
2866 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002867
Daniel Stone4dbadb12012-05-30 16:31:51 +01002868 if (input->grab && input->grab_button == button &&
2869 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002870 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002871}
2872
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002873static void
Daniel Stone37816df2012-05-16 18:45:18 +01002874pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002875 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002876{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002877 struct input *input = data;
2878 struct widget *widget;
2879
2880 widget = input->focus_widget;
2881 if (input->grab)
2882 widget = input->grab;
2883 if (widget && widget->axis_handler)
2884 (*widget->axis_handler)(widget,
2885 input, time,
2886 axis, value,
2887 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002888}
2889
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002890static const struct wl_pointer_listener pointer_listener = {
2891 pointer_handle_enter,
2892 pointer_handle_leave,
2893 pointer_handle_motion,
2894 pointer_handle_button,
2895 pointer_handle_axis,
2896};
2897
2898static void
2899input_remove_keyboard_focus(struct input *input)
2900{
2901 struct window *window = input->keyboard_focus;
2902 struct itimerspec its;
2903
2904 its.it_interval.tv_sec = 0;
2905 its.it_interval.tv_nsec = 0;
2906 its.it_value.tv_sec = 0;
2907 its.it_value.tv_nsec = 0;
2908 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2909
2910 if (!window)
2911 return;
2912
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002913 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002914 if (window->keyboard_focus_handler)
2915 (*window->keyboard_focus_handler)(window, NULL,
2916 window->user_data);
2917
2918 input->keyboard_focus = NULL;
2919}
2920
2921static void
2922keyboard_repeat_func(struct task *task, uint32_t events)
2923{
2924 struct input *input =
2925 container_of(task, struct input, repeat_task);
2926 struct window *window = input->keyboard_focus;
2927 uint64_t exp;
2928
2929 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2930 /* If we change the timer between the fd becoming
2931 * readable and getting here, there'll be nothing to
2932 * read and we get EAGAIN. */
2933 return;
2934
2935 if (window && window->key_handler) {
2936 (*window->key_handler)(window, input, input->repeat_time,
2937 input->repeat_key, input->repeat_sym,
2938 WL_KEYBOARD_KEY_STATE_PRESSED,
2939 window->user_data);
2940 }
2941}
2942
2943static void
2944keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2945 uint32_t format, int fd, uint32_t size)
2946{
2947 struct input *input = data;
2948 char *map_str;
2949
2950 if (!data) {
2951 close(fd);
2952 return;
2953 }
2954
2955 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2956 close(fd);
2957 return;
2958 }
2959
2960 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2961 if (map_str == MAP_FAILED) {
2962 close(fd);
2963 return;
2964 }
2965
2966 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2967 map_str,
2968 XKB_KEYMAP_FORMAT_TEXT_V1,
2969 0);
2970 munmap(map_str, size);
2971 close(fd);
2972
2973 if (!input->xkb.keymap) {
2974 fprintf(stderr, "failed to compile keymap\n");
2975 return;
2976 }
2977
2978 input->xkb.state = xkb_state_new(input->xkb.keymap);
2979 if (!input->xkb.state) {
2980 fprintf(stderr, "failed to create XKB state\n");
2981 xkb_map_unref(input->xkb.keymap);
2982 input->xkb.keymap = NULL;
2983 return;
2984 }
2985
2986 input->xkb.control_mask =
2987 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2988 input->xkb.alt_mask =
2989 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2990 input->xkb.shift_mask =
2991 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2992}
2993
2994static void
2995keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2996 uint32_t serial, struct wl_surface *surface,
2997 struct wl_array *keys)
2998{
2999 struct input *input = data;
3000 struct window *window;
3001
3002 input->display->serial = serial;
3003 input->keyboard_focus = wl_surface_get_user_data(surface);
3004
3005 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003006 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003007 if (window->keyboard_focus_handler)
3008 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003009 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003010}
3011
3012static void
3013keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3014 uint32_t serial, struct wl_surface *surface)
3015{
3016 struct input *input = data;
3017
3018 input->display->serial = serial;
3019 input_remove_keyboard_focus(input);
3020}
3021
Scott Moreau210d0792012-03-22 10:47:01 -06003022static void
Daniel Stone37816df2012-05-16 18:45:18 +01003023keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003024 uint32_t serial, uint32_t time, uint32_t key,
3025 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003026{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003027 struct input *input = data;
3028 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003029 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003030 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003031 const xkb_keysym_t *syms;
3032 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003033 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003034
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003035 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003036 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003037 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003038 return;
3039
Daniel Stone97f68542012-05-30 16:32:01 +01003040 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003041
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003042 sym = XKB_KEY_NoSymbol;
3043 if (num_syms == 1)
3044 sym = syms[0];
3045
3046 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003047 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003048 window_set_maximized(window,
3049 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003050 } else if (sym == XKB_KEY_F11 &&
3051 window->fullscreen_handler &&
3052 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3053 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003054 } else if (sym == XKB_KEY_F4 &&
3055 input->modifiers == MOD_ALT_MASK &&
3056 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3057 if (window->close_handler)
3058 window->close_handler(window->parent,
3059 window->user_data);
3060 else
3061 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003062 } else if (window->key_handler) {
3063 (*window->key_handler)(window, input, time, key,
3064 sym, state, window->user_data);
3065 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003066
3067 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3068 key == input->repeat_key) {
3069 its.it_interval.tv_sec = 0;
3070 its.it_interval.tv_nsec = 0;
3071 its.it_value.tv_sec = 0;
3072 its.it_value.tv_nsec = 0;
3073 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3074 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3075 input->repeat_sym = sym;
3076 input->repeat_key = key;
3077 input->repeat_time = time;
3078 its.it_interval.tv_sec = 0;
3079 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3080 its.it_value.tv_sec = 0;
3081 its.it_value.tv_nsec = 400 * 1000 * 1000;
3082 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3083 }
3084}
3085
3086static void
Daniel Stone351eb612012-05-31 15:27:47 -04003087keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3088 uint32_t serial, uint32_t mods_depressed,
3089 uint32_t mods_latched, uint32_t mods_locked,
3090 uint32_t group)
3091{
3092 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003093 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003094
Matt Ropere61561f2013-06-24 16:52:43 +01003095 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3096 if (!input->xkb.keymap)
3097 return;
3098
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003099 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3100 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003101 mask = xkb_state_serialize_mods(input->xkb.state,
3102 XKB_STATE_DEPRESSED |
3103 XKB_STATE_LATCHED);
3104 input->modifiers = 0;
3105 if (mask & input->xkb.control_mask)
3106 input->modifiers |= MOD_CONTROL_MASK;
3107 if (mask & input->xkb.alt_mask)
3108 input->modifiers |= MOD_ALT_MASK;
3109 if (mask & input->xkb.shift_mask)
3110 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003111}
3112
Daniel Stone37816df2012-05-16 18:45:18 +01003113static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003114 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003115 keyboard_handle_enter,
3116 keyboard_handle_leave,
3117 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003118 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003119};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003120
3121static void
Daniel Stone37816df2012-05-16 18:45:18 +01003122seat_handle_capabilities(void *data, struct wl_seat *seat,
3123 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003124{
Daniel Stone37816df2012-05-16 18:45:18 +01003125 struct input *input = data;
3126
3127 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3128 input->pointer = wl_seat_get_pointer(seat);
3129 wl_pointer_set_user_data(input->pointer, input);
3130 wl_pointer_add_listener(input->pointer, &pointer_listener,
3131 input);
3132 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3133 wl_pointer_destroy(input->pointer);
3134 input->pointer = NULL;
3135 }
3136
3137 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3138 input->keyboard = wl_seat_get_keyboard(seat);
3139 wl_keyboard_set_user_data(input->keyboard, input);
3140 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3141 input);
3142 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3143 wl_keyboard_destroy(input->keyboard);
3144 input->keyboard = NULL;
3145 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003146}
3147
Daniel Stone37816df2012-05-16 18:45:18 +01003148static const struct wl_seat_listener seat_listener = {
3149 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003150};
3151
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003152void
3153input_get_position(struct input *input, int32_t *x, int32_t *y)
3154{
3155 *x = input->sx;
3156 *y = input->sy;
3157}
3158
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003159struct display *
3160input_get_display(struct input *input)
3161{
3162 return input->display;
3163}
3164
Daniel Stone37816df2012-05-16 18:45:18 +01003165struct wl_seat *
3166input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003167{
Daniel Stone37816df2012-05-16 18:45:18 +01003168 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003169}
3170
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003171uint32_t
3172input_get_modifiers(struct input *input)
3173{
3174 return input->modifiers;
3175}
3176
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003177struct widget *
3178input_get_focus_widget(struct input *input)
3179{
3180 return input->focus_widget;
3181}
3182
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003183struct data_offer {
3184 struct wl_data_offer *offer;
3185 struct input *input;
3186 struct wl_array types;
3187 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003188
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003189 struct task io_task;
3190 int fd;
3191 data_func_t func;
3192 int32_t x, y;
3193 void *user_data;
3194};
3195
3196static void
3197data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3198{
3199 struct data_offer *offer = data;
3200 char **p;
3201
3202 p = wl_array_add(&offer->types, sizeof *p);
3203 *p = strdup(type);
3204}
3205
3206static const struct wl_data_offer_listener data_offer_listener = {
3207 data_offer_offer,
3208};
3209
3210static void
3211data_offer_destroy(struct data_offer *offer)
3212{
3213 char **p;
3214
3215 offer->refcount--;
3216 if (offer->refcount == 0) {
3217 wl_data_offer_destroy(offer->offer);
3218 for (p = offer->types.data; *p; p++)
3219 free(*p);
3220 wl_array_release(&offer->types);
3221 free(offer);
3222 }
3223}
3224
3225static void
3226data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003227 struct wl_data_device *data_device,
3228 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003229{
3230 struct data_offer *offer;
3231
3232 offer = malloc(sizeof *offer);
3233
3234 wl_array_init(&offer->types);
3235 offer->refcount = 1;
3236 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003237 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003238 wl_data_offer_add_listener(offer->offer,
3239 &data_offer_listener, offer);
3240}
3241
3242static void
3243data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003244 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003245 wl_fixed_t x_w, wl_fixed_t y_w,
3246 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003247{
3248 struct input *input = data;
3249 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003250 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003251 float x = wl_fixed_to_double(x_w);
3252 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003253 char **p;
3254
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003255 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003256 window = wl_surface_get_user_data(surface);
3257 input->pointer_focus = window;
3258
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003259 if (offer) {
3260 input->drag_offer = wl_data_offer_get_user_data(offer);
3261
3262 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3263 *p = NULL;
3264
3265 types_data = input->drag_offer->types.data;
3266 } else {
3267 input->drag_offer = NULL;
3268 types_data = NULL;
3269 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003270
3271 window = input->pointer_focus;
3272 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003273 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003274 window->user_data);
3275}
3276
3277static void
3278data_device_leave(void *data, struct wl_data_device *data_device)
3279{
3280 struct input *input = data;
3281
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003282 if (input->drag_offer) {
3283 data_offer_destroy(input->drag_offer);
3284 input->drag_offer = NULL;
3285 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003286}
3287
3288static void
3289data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003290 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003291{
3292 struct input *input = data;
3293 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003294 float x = wl_fixed_to_double(x_w);
3295 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003296 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003297
3298 input->sx = x;
3299 input->sy = y;
3300
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003301 if (input->drag_offer)
3302 types_data = input->drag_offer->types.data;
3303 else
3304 types_data = NULL;
3305
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003306 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003307 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003308 window->user_data);
3309}
3310
3311static void
3312data_device_drop(void *data, struct wl_data_device *data_device)
3313{
3314 struct input *input = data;
3315 struct window *window = input->pointer_focus;
3316
3317 if (window->drop_handler)
3318 window->drop_handler(window, input,
3319 input->sx, input->sy, window->user_data);
3320}
3321
3322static void
3323data_device_selection(void *data,
3324 struct wl_data_device *wl_data_device,
3325 struct wl_data_offer *offer)
3326{
3327 struct input *input = data;
3328 char **p;
3329
3330 if (input->selection_offer)
3331 data_offer_destroy(input->selection_offer);
3332
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003333 if (offer) {
3334 input->selection_offer = wl_data_offer_get_user_data(offer);
3335 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3336 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003337 } else {
3338 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003339 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003340}
3341
3342static const struct wl_data_device_listener data_device_listener = {
3343 data_device_data_offer,
3344 data_device_enter,
3345 data_device_leave,
3346 data_device_motion,
3347 data_device_drop,
3348 data_device_selection
3349};
3350
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003351static void
3352input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003353{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003354 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003355 struct wl_cursor *cursor;
3356 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003357
Daniel Stone80972742012-11-07 17:51:39 +11003358 if (!input->pointer)
3359 return;
3360
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003361 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003362 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003363 return;
3364
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003365 if (index >= (int) cursor->image_count) {
3366 fprintf(stderr, "cursor index out of range\n");
3367 return;
3368 }
3369
3370 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003371 buffer = wl_cursor_image_get_buffer(image);
3372 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003373 return;
3374
Kristian Høgsbergae277372012-08-01 09:41:08 -04003375 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003376 input->pointer_surface,
3377 image->hotspot_x, image->hotspot_y);
3378 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3379 wl_surface_damage(input->pointer_surface, 0, 0,
3380 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003381 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003382}
3383
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003384static const struct wl_callback_listener pointer_surface_listener;
3385
3386static void
3387pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3388 uint32_t time)
3389{
3390 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003391 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003392 int i;
3393
3394 if (callback) {
3395 assert(callback == input->cursor_frame_cb);
3396 wl_callback_destroy(callback);
3397 input->cursor_frame_cb = NULL;
3398 }
3399
Daniel Stone80972742012-11-07 17:51:39 +11003400 if (!input->pointer)
3401 return;
3402
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003403 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003404 wl_pointer_set_cursor(input->pointer,
3405 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003406 NULL, 0, 0);
3407 return;
3408 }
3409
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003410 if (input->current_cursor == CURSOR_UNSET)
3411 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003412 cursor = input->display->cursors[input->current_cursor];
3413 if (!cursor)
3414 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003415
3416 /* FIXME We don't have the current time on the first call so we set
3417 * the animation start to the time of the first frame callback. */
3418 if (time == 0)
3419 input->cursor_anim_start = 0;
3420 else if (input->cursor_anim_start == 0)
3421 input->cursor_anim_start = time;
3422
3423 if (time == 0 || input->cursor_anim_start == 0)
3424 i = 0;
3425 else
3426 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3427
Pekka Paalanenbc106382012-10-10 12:49:31 +03003428 if (cursor->image_count > 1) {
3429 input->cursor_frame_cb =
3430 wl_surface_frame(input->pointer_surface);
3431 wl_callback_add_listener(input->cursor_frame_cb,
3432 &pointer_surface_listener, input);
3433 }
3434
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003435 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003436}
3437
3438static const struct wl_callback_listener pointer_surface_listener = {
3439 pointer_surface_frame_callback
3440};
3441
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003442void
3443input_set_pointer_image(struct input *input, int pointer)
3444{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003445 int force = 0;
3446
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003447 if (!input->pointer)
3448 return;
3449
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003450 if (input->pointer_enter_serial > input->cursor_serial)
3451 force = 1;
3452
3453 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003454 return;
3455
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003456 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003457 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003458 if (!input->cursor_frame_cb)
3459 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003460 else if (force) {
3461 /* The current frame callback may be stuck if, for instance,
3462 * the set cursor request was processed by the server after
3463 * this client lost the focus. In this case the cursor surface
3464 * might not be mapped and the frame callback wouldn't ever
3465 * complete. Send a set_cursor and attach to try to map the
3466 * cursor surface again so that the callback will finish */
3467 input_set_pointer_image_index(input, 0);
3468 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003469}
3470
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003471struct wl_data_device *
3472input_get_data_device(struct input *input)
3473{
3474 return input->data_device;
3475}
3476
3477void
3478input_set_selection(struct input *input,
3479 struct wl_data_source *source, uint32_t time)
3480{
3481 wl_data_device_set_selection(input->data_device, source, time);
3482}
3483
3484void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003485input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003486{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003487 wl_data_offer_accept(input->drag_offer->offer,
3488 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003489}
3490
3491static void
3492offer_io_func(struct task *task, uint32_t events)
3493{
3494 struct data_offer *offer =
3495 container_of(task, struct data_offer, io_task);
3496 unsigned int len;
3497 char buffer[4096];
3498
3499 len = read(offer->fd, buffer, sizeof buffer);
3500 offer->func(buffer, len,
3501 offer->x, offer->y, offer->user_data);
3502
3503 if (len == 0) {
3504 close(offer->fd);
3505 data_offer_destroy(offer);
3506 }
3507}
3508
3509static void
3510data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3511 data_func_t func, void *user_data)
3512{
3513 int p[2];
3514
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003515 if (pipe2(p, O_CLOEXEC) == -1)
3516 return;
3517
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003518 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3519 close(p[1]);
3520
3521 offer->io_task.run = offer_io_func;
3522 offer->fd = p[0];
3523 offer->func = func;
3524 offer->refcount++;
3525 offer->user_data = user_data;
3526
3527 display_watch_fd(offer->input->display,
3528 offer->fd, EPOLLIN, &offer->io_task);
3529}
3530
3531void
3532input_receive_drag_data(struct input *input, const char *mime_type,
3533 data_func_t func, void *data)
3534{
3535 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3536 input->drag_offer->x = input->sx;
3537 input->drag_offer->y = input->sy;
3538}
3539
3540int
3541input_receive_selection_data(struct input *input, const char *mime_type,
3542 data_func_t func, void *data)
3543{
3544 char **p;
3545
3546 if (input->selection_offer == NULL)
3547 return -1;
3548
3549 for (p = input->selection_offer->types.data; *p; p++)
3550 if (strcmp(mime_type, *p) == 0)
3551 break;
3552
3553 if (*p == NULL)
3554 return -1;
3555
3556 data_offer_receive_data(input->selection_offer,
3557 mime_type, func, data);
3558 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003559}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003560
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003561int
3562input_receive_selection_data_to_fd(struct input *input,
3563 const char *mime_type, int fd)
3564{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003565 if (input->selection_offer)
3566 wl_data_offer_receive(input->selection_offer->offer,
3567 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003568
3569 return 0;
3570}
3571
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003572void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003573window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003574{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003575 if (!window->shell_surface)
3576 return;
3577
Daniel Stone37816df2012-05-16 18:45:18 +01003578 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003579}
3580
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003581static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003582surface_set_synchronized(struct surface *surface)
3583{
3584 if (!surface->subsurface)
3585 return;
3586
3587 if (surface->synchronized)
3588 return;
3589
3590 wl_subsurface_set_sync(surface->subsurface);
3591 surface->synchronized = 1;
3592}
3593
3594static void
3595surface_set_synchronized_default(struct surface *surface)
3596{
3597 if (!surface->subsurface)
3598 return;
3599
3600 if (surface->synchronized == surface->synchronized_default)
3601 return;
3602
3603 if (surface->synchronized_default)
3604 wl_subsurface_set_sync(surface->subsurface);
3605 else
3606 wl_subsurface_set_desync(surface->subsurface);
3607
3608 surface->synchronized = surface->synchronized_default;
3609}
3610
3611static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003612surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003613{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003614 struct widget *widget = surface->widget;
3615 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003616
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003617 if (surface->input_region) {
3618 wl_region_destroy(surface->input_region);
3619 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003620 }
3621
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003622 if (surface->opaque_region)
3623 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003624
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003625 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003626
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003627 if (widget->resize_handler)
3628 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003629 widget->allocation.width,
3630 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003631 widget->user_data);
3632
Pekka Paalanen35e82632013-04-25 13:57:48 +03003633 if (surface->subsurface &&
3634 (surface->allocation.x != widget->allocation.x ||
3635 surface->allocation.y != widget->allocation.y)) {
3636 wl_subsurface_set_position(surface->subsurface,
3637 widget->allocation.x,
3638 widget->allocation.y);
3639 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003640 if (surface->allocation.width != widget->allocation.width ||
3641 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003642 window_schedule_redraw(widget->window);
3643 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003644 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003645
3646 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003647 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003648 widget->allocation.width,
3649 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003650}
3651
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003652static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003653hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3654{
3655 /*
3656 * This hack should be removed, when EGL respects
3657 * eglSwapInterval(0).
3658 *
3659 * If this window has sub-surfaces, especially a free-running
3660 * EGL-widget, we need to post the parent surface once with
3661 * all the old state to guarantee, that the EGL-widget will
3662 * receive its frame callback soon. Otherwise, a forced call
3663 * to eglSwapBuffers may end up blocking, waiting for a frame
3664 * event that will never come, because we will commit the parent
3665 * surface with all new state only after eglSwapBuffers returns.
3666 *
3667 * This assumes, that:
3668 * 1. When the EGL widget's resize hook is called, it pauses.
3669 * 2. When the EGL widget's redraw hook is called, it forces a
3670 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3671 * In a single threaded application condition 1 is a no-op.
3672 *
3673 * XXX: This should actually be after the surface_resize() calls,
3674 * but cannot, because then it would commit the incomplete state
3675 * accumulated from the widget resize hooks.
3676 */
3677 if (window->subsurface_list.next != &window->main_surface->link ||
3678 window->subsurface_list.prev != &window->main_surface->link)
3679 wl_surface_commit(window->main_surface->surface);
3680}
3681
3682static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003683idle_resize(struct window *window)
3684{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003685 struct surface *surface;
3686
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003687 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003688 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003689
Pekka Paalanen71233882013-04-25 13:57:53 +03003690 DBG("from %dx%d to %dx%d\n",
3691 window->main_surface->server_allocation.width,
3692 window->main_surface->server_allocation.height,
3693 window->pending_allocation.width,
3694 window->pending_allocation.height);
3695
Pekka Paalanene9297f82013-04-25 13:57:51 +03003696 hack_prevent_EGL_sub_surface_deadlock(window);
3697
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003698 widget_set_allocation(window->main_surface->widget,
3699 window->pending_allocation.x,
3700 window->pending_allocation.y,
3701 window->pending_allocation.width,
3702 window->pending_allocation.height);
3703
3704 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003705
3706 /* The main surface is in the list, too. Main surface's
3707 * resize_handler is responsible for calling widget_set_allocation()
3708 * on all sub-surface root widgets, so they will be resized
3709 * properly.
3710 */
3711 wl_list_for_each(surface, &window->subsurface_list, link) {
3712 if (surface == window->main_surface)
3713 continue;
3714
3715 surface_set_synchronized(surface);
3716 surface_resize(surface);
3717 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003718}
3719
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003720void
3721window_schedule_resize(struct window *window, int width, int height)
3722{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003723 window->pending_allocation.x = 0;
3724 window->pending_allocation.y = 0;
3725 window->pending_allocation.width = width;
3726 window->pending_allocation.height = height;
3727
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003728 if (window->min_allocation.width == 0)
3729 window->min_allocation = window->pending_allocation;
3730 if (window->pending_allocation.width < window->min_allocation.width)
3731 window->pending_allocation.width = window->min_allocation.width;
3732 if (window->pending_allocation.height < window->min_allocation.height)
3733 window->pending_allocation.height = window->min_allocation.height;
3734
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003735 window->resize_needed = 1;
3736 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003737}
3738
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003739void
3740widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3741{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003742 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003743}
3744
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003745static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003746handle_ping(void *data, struct wl_shell_surface *shell_surface,
3747 uint32_t serial)
3748{
3749 wl_shell_surface_pong(shell_surface, serial);
3750}
3751
3752static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003753handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003754 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003755{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003756 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003757
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003758 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003759 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003760}
3761
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003762static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003763menu_destroy(struct menu *menu)
3764{
3765 widget_destroy(menu->widget);
3766 window_destroy(menu->window);
3767 free(menu);
3768}
3769
3770static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003771handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3772{
3773 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003774 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003775
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003776 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003777 * device. Or just use wl_callback. And this really needs to
3778 * be a window vfunc that the menu can set. And we need the
3779 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003780
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003781 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003782 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003783}
3784
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003785static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003786 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003787 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003788 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003789};
3790
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003791void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003792window_get_allocation(struct window *window,
3793 struct rectangle *allocation)
3794{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003795 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003796}
3797
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003798static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003799widget_redraw(struct widget *widget)
3800{
3801 struct widget *child;
3802
3803 if (widget->redraw_handler)
3804 widget->redraw_handler(widget, widget->user_data);
3805 wl_list_for_each(child, &widget->child_list, link)
3806 widget_redraw(child);
3807}
3808
3809static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003810frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3811{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003812 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003813
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003814 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003815 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003816 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003817 surface->frame_cb = NULL;
3818
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003819 surface->last_time = time;
3820
Pekka Paalanen71233882013-04-25 13:57:53 +03003821 if (surface->redraw_needed || surface->window->redraw_needed) {
3822 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003823 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003824 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003825}
3826
3827static const struct wl_callback_listener listener = {
3828 frame_callback
3829};
3830
3831static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003832surface_redraw(struct surface *surface)
3833{
Pekka Paalanen71233882013-04-25 13:57:53 +03003834 DBG_OBJ(surface->surface, "begin\n");
3835
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003836 if (!surface->window->redraw_needed && !surface->redraw_needed)
3837 return;
3838
3839 /* Whole-window redraw forces a redraw even if the previous has
3840 * not yet hit the screen.
3841 */
3842 if (surface->frame_cb) {
3843 if (!surface->window->redraw_needed)
3844 return;
3845
Pekka Paalanen71233882013-04-25 13:57:53 +03003846 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003847 wl_callback_destroy(surface->frame_cb);
3848 }
3849
3850 surface->frame_cb = wl_surface_frame(surface->surface);
3851 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003852 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003853
3854 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003855 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003856 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003857 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003858}
3859
3860static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003861idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003862{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003863 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003864 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003865
Pekka Paalanen71233882013-04-25 13:57:53 +03003866 DBG(" --------- \n");
3867
Pekka Paalaneneebff542013-04-25 13:57:52 +03003868 wl_list_init(&window->redraw_task.link);
3869 window->redraw_task_scheduled = 0;
3870
3871 if (window->resize_needed) {
3872 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003873 if (window->main_surface->frame_cb) {
3874 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003875 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003876 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003877
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003878 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003879 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003880
Pekka Paalanen35e82632013-04-25 13:57:48 +03003881 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003882 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003883
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003884 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003885 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003886
3887 wl_list_for_each(surface, &window->subsurface_list, link)
3888 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003889}
3890
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003891static void
3892window_schedule_redraw_task(struct window *window)
3893{
3894 if (window->configure_requests)
3895 return;
3896 if (!window->redraw_task_scheduled) {
3897 window->redraw_task.run = idle_redraw;
3898 display_defer(window->display, &window->redraw_task);
3899 window->redraw_task_scheduled = 1;
3900 }
3901}
3902
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003903void
3904window_schedule_redraw(struct window *window)
3905{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003906 struct surface *surface;
3907
Pekka Paalanen71233882013-04-25 13:57:53 +03003908 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3909
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003910 wl_list_for_each(surface, &window->subsurface_list, link)
3911 surface->redraw_needed = 1;
3912
3913 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003914}
3915
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003916int
3917window_is_fullscreen(struct window *window)
3918{
3919 return window->type == TYPE_FULLSCREEN;
3920}
3921
MoD063a8822013-03-02 23:43:57 +00003922static void
3923configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3924{
3925 struct window *window = data;
3926
3927 wl_callback_destroy(callback);
3928 window->configure_requests--;
3929
3930 if (!window->configure_requests)
3931 window_schedule_redraw(window);
3932}
3933
3934static struct wl_callback_listener configure_request_listener = {
3935 configure_request_completed,
3936};
3937
3938static void
3939window_defer_redraw_until_configure(struct window* window)
3940{
3941 struct wl_callback *callback;
3942
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003943 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003944 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003945 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003946 }
3947
3948 callback = wl_display_sync(window->display->display);
3949 wl_callback_add_listener(callback, &configure_request_listener, window);
3950 window->configure_requests++;
3951}
3952
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003953void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003954window_set_fullscreen(struct window *window, int fullscreen)
3955{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003956 if (!window->display->shell)
3957 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003958
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003959 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003960 return;
3961
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003962 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003963 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003964 if (window->type == TYPE_TOPLEVEL) {
3965 window->saved_allocation = window->main_surface->allocation;
3966 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003967 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003968 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003969 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003970 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003971 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003972 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003973 if (window->saved_type == TYPE_MAXIMIZED) {
3974 window_set_maximized(window, 1);
3975 } else {
3976 window->type = TYPE_TOPLEVEL;
3977 wl_shell_surface_set_toplevel(window->shell_surface);
3978 window_schedule_resize(window,
3979 window->saved_allocation.width,
3980 window->saved_allocation.height);
3981 }
3982
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003983 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003984}
3985
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003986void
3987window_set_fullscreen_method(struct window *window,
3988 enum wl_shell_surface_fullscreen_method method)
3989{
3990 window->fullscreen_method = method;
3991}
3992
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003993int
3994window_is_maximized(struct window *window)
3995{
3996 return window->type == TYPE_MAXIMIZED;
3997}
3998
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003999void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004000window_set_maximized(struct window *window, int maximized)
4001{
4002 if (!window->display->shell)
4003 return;
4004
4005 if ((window->type == TYPE_MAXIMIZED) == maximized)
4006 return;
4007
4008 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004009 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004010 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4011 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004012 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004013 } else if (window->type == TYPE_FULLSCREEN) {
4014 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4015 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004016 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004017 } else {
4018 wl_shell_surface_set_toplevel(window->shell_surface);
4019 window->type = TYPE_TOPLEVEL;
4020 window_schedule_resize(window,
4021 window->saved_allocation.width,
4022 window->saved_allocation.height);
4023 }
4024}
4025
4026void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004027window_set_user_data(struct window *window, void *data)
4028{
4029 window->user_data = data;
4030}
4031
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004032void *
4033window_get_user_data(struct window *window)
4034{
4035 return window->user_data;
4036}
4037
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004038void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004039window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004040 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004041{
4042 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004043}
4044
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004045void
4046window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004047 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004048{
4049 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004050}
4051
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004052void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004053window_set_data_handler(struct window *window, window_data_handler_t handler)
4054{
4055 window->data_handler = handler;
4056}
4057
4058void
4059window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4060{
4061 window->drop_handler = handler;
4062}
4063
4064void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004065window_set_close_handler(struct window *window,
4066 window_close_handler_t handler)
4067{
4068 window->close_handler = handler;
4069}
4070
4071void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004072window_set_fullscreen_handler(struct window *window,
4073 window_fullscreen_handler_t handler)
4074{
4075 window->fullscreen_handler = handler;
4076}
4077
4078void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004079window_set_output_handler(struct window *window,
4080 window_output_handler_t handler)
4081{
4082 window->output_handler = handler;
4083}
4084
4085void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004086window_set_title(struct window *window, const char *title)
4087{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004088 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004089 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004090 if (window->shell_surface)
4091 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004092}
4093
4094const char *
4095window_get_title(struct window *window)
4096{
4097 return window->title;
4098}
4099
4100void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004101window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4102{
4103 struct text_cursor_position *text_cursor_position =
4104 window->display->text_cursor_position;
4105
Scott Moreau9295ce02012-06-01 12:46:10 -06004106 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004107 return;
4108
4109 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004110 window->main_surface->surface,
4111 wl_fixed_from_int(x),
4112 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004113}
4114
4115void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004116window_damage(struct window *window, int32_t x, int32_t y,
4117 int32_t width, int32_t height)
4118{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004119 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004120}
4121
Casey Dahlin9074db52012-04-19 22:50:09 -04004122static void
4123surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004124 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004125{
Rob Bradford7507b572012-05-15 17:55:34 +01004126 struct window *window = data;
4127 struct output *output;
4128 struct output *output_found = NULL;
4129 struct window_output *window_output;
4130
4131 wl_list_for_each(output, &window->display->output_list, link) {
4132 if (output->output == wl_output) {
4133 output_found = output;
4134 break;
4135 }
4136 }
4137
4138 if (!output_found)
4139 return;
4140
4141 window_output = malloc (sizeof *window_output);
4142 window_output->output = output_found;
4143
4144 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004145
4146 if (window->output_handler)
4147 window->output_handler(window, output_found, 1,
4148 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004149}
4150
4151static void
4152surface_leave(void *data,
4153 struct wl_surface *wl_surface, struct wl_output *output)
4154{
Rob Bradford7507b572012-05-15 17:55:34 +01004155 struct window *window = data;
4156 struct window_output *window_output;
4157 struct window_output *window_output_found = NULL;
4158
4159 wl_list_for_each(window_output, &window->window_output_list, link) {
4160 if (window_output->output->output == output) {
4161 window_output_found = window_output;
4162 break;
4163 }
4164 }
4165
4166 if (window_output_found) {
4167 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004168
4169 if (window->output_handler)
4170 window->output_handler(window, window_output->output,
4171 0, window->user_data);
4172
Rob Bradford7507b572012-05-15 17:55:34 +01004173 free(window_output_found);
4174 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004175}
4176
4177static const struct wl_surface_listener surface_listener = {
4178 surface_enter,
4179 surface_leave
4180};
4181
Pekka Paalanen4e373742013-02-13 16:17:13 +02004182static struct surface *
4183surface_create(struct window *window)
4184{
4185 struct display *display = window->display;
4186 struct surface *surface;
4187
4188 surface = calloc(1, sizeof *surface);
4189 if (!surface)
4190 return NULL;
4191
4192 surface->window = window;
4193 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004194 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004195 wl_surface_add_listener(surface->surface, &surface_listener, window);
4196
Pekka Paalanen35e82632013-04-25 13:57:48 +03004197 wl_list_insert(&window->subsurface_list, &surface->link);
4198
Pekka Paalanen4e373742013-02-13 16:17:13 +02004199 return surface;
4200}
4201
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004202static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004203window_create_internal(struct display *display,
4204 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004205{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004206 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004207 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004208
Kristian Høgsbergce278412013-07-25 15:20:20 -07004209 window = xmalloc(sizeof *window);
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004210 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004211 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004212 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004213 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004214
4215 surface = surface_create(window);
4216 window->main_surface = surface;
4217
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004218 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004219 window->shell_surface =
4220 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004221 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004222 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004223 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004224
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004225 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004226 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004227 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004228 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004229
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004230 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004231#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004232 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004233#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004234 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004235#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004236 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004237 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004238
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004239 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004240 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004241 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004242
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004243 if (window->shell_surface) {
4244 wl_shell_surface_set_user_data(window->shell_surface, window);
4245 wl_shell_surface_add_listener(window->shell_surface,
4246 &shell_surface_listener, window);
4247 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004248
Rob Bradford7507b572012-05-15 17:55:34 +01004249 wl_list_init (&window->window_output_list);
4250
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004251 return window;
4252}
4253
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004254struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004255window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004256{
Kristian Høgsberg41624832013-07-25 16:53:10 -07004257 return window_create_internal(display, NULL, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004258}
4259
4260struct window *
4261window_create_custom(struct display *display)
4262{
Kristian Høgsbergce278412013-07-25 15:20:20 -07004263 return window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004264}
4265
4266struct window *
4267window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004268 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004269{
4270 struct window *window;
4271
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004272 window = window_create_internal(parent->display,
4273 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004274
4275 window->x = x;
4276 window->y = y;
4277
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004278 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004279 wl_shell_surface_set_transient(
4280 window->shell_surface,
4281 window->parent->main_surface->surface,
4282 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004283
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004284 return window;
4285}
4286
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004287static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004288menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004289{
4290 int next;
4291
4292 next = (sy - 8) / 20;
4293 if (menu->current != next) {
4294 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004295 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004296 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004297}
4298
4299static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004300menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004301 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004302 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004303{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004304 struct menu *menu = data;
4305
4306 if (widget == menu->widget)
4307 menu_set_item(data, y);
4308
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004309 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004310}
4311
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004312static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004313menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004314 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004315{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004316 struct menu *menu = data;
4317
4318 if (widget == menu->widget)
4319 menu_set_item(data, y);
4320
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004321 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004322}
4323
4324static void
4325menu_leave_handler(struct widget *widget, struct input *input, void *data)
4326{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004327 struct menu *menu = data;
4328
4329 if (widget == menu->widget)
4330 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004331}
4332
4333static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004334menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004335 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004336 uint32_t button, enum wl_pointer_button_state state,
4337 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004338
4339{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004340 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004341
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004342 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4343 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004344 /* Either relase after press-drag-release or
4345 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004346 menu->func(menu->window->parent,
4347 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004348 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004349 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004350 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004351 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004352 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004353}
4354
4355static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004356menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004357{
4358 cairo_t *cr;
4359 const int32_t r = 3, margin = 3;
4360 struct menu *menu = data;
4361 int32_t width, height, i;
4362
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004363 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004364 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4365 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4366 cairo_paint(cr);
4367
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004368 width = widget->allocation.width;
4369 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004370 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004371
4372 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004373 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4374 cairo_fill(cr);
4375
4376 for (i = 0; i < menu->count; i++) {
4377 if (i == menu->current) {
4378 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4379 cairo_rectangle(cr, margin, i * 20 + margin,
4380 width - 2 * margin, 20);
4381 cairo_fill(cr);
4382 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4383 cairo_move_to(cr, 10, i * 20 + 16);
4384 cairo_show_text(cr, menu->entries[i]);
4385 } else {
4386 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4387 cairo_move_to(cr, 10, i * 20 + 16);
4388 cairo_show_text(cr, menu->entries[i]);
4389 }
4390 }
4391
4392 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004393}
4394
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004395void
4396window_show_menu(struct display *display,
4397 struct input *input, uint32_t time, struct window *parent,
4398 int32_t x, int32_t y,
4399 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004400{
4401 struct window *window;
4402 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004403 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004404
4405 menu = malloc(sizeof *menu);
4406 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004407 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004408
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004409 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004410 if (!window) {
4411 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004412 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004413 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004414
4415 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004416 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004417 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4418 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004419 menu->entries = entries;
4420 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004421 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004422 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004423 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004424 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004425 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004426 window->type = TYPE_MENU;
4427 window->x = x;
4428 window->y = y;
4429
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004430 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004431 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004432 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004433 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004434 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004435
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004436 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004437 widget_set_enter_handler(menu->widget, menu_enter_handler);
4438 widget_set_leave_handler(menu->widget, menu_leave_handler);
4439 widget_set_motion_handler(menu->widget, menu_motion_handler);
4440 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004441
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004442 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004443 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004444}
4445
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004446void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004447window_set_buffer_type(struct window *window, enum window_buffer_type type)
4448{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004449 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004450}
4451
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004452void
4453window_set_preferred_format(struct window *window,
4454 enum preferred_format format)
4455{
4456 window->preferred_format = format;
4457}
4458
Pekka Paalanen35e82632013-04-25 13:57:48 +03004459struct widget *
4460window_add_subsurface(struct window *window, void *data,
4461 enum subsurface_mode default_mode)
4462{
4463 struct widget *widget;
4464 struct surface *surface;
4465 struct wl_surface *parent;
4466 struct wl_subcompositor *subcompo = window->display->subcompositor;
4467
4468 if (!subcompo)
4469 return NULL;
4470
4471 surface = surface_create(window);
4472 widget = widget_create(window, surface, data);
4473 wl_list_init(&widget->link);
4474 surface->widget = widget;
4475
4476 parent = window->main_surface->surface;
4477 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4478 surface->surface,
4479 parent);
4480 surface->synchronized = 1;
4481
4482 switch (default_mode) {
4483 case SUBSURFACE_SYNCHRONIZED:
4484 surface->synchronized_default = 1;
4485 break;
4486 case SUBSURFACE_DESYNCHRONIZED:
4487 surface->synchronized_default = 0;
4488 break;
4489 default:
4490 assert(!"bad enum subsurface_mode");
4491 }
4492
4493 return widget;
4494}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004495
4496static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004497display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004498 struct wl_output *wl_output,
4499 int x, int y,
4500 int physical_width,
4501 int physical_height,
4502 int subpixel,
4503 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004504 const char *model,
4505 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004506{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004507 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004508
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004509 output->allocation.x = x;
4510 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004511 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004512}
4513
4514static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004515display_handle_done(void *data,
4516 struct wl_output *wl_output)
4517{
4518}
4519
4520static void
4521display_handle_scale(void *data,
4522 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004523 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004524{
4525 struct output *output = data;
4526
4527 output->scale = scale;
4528}
4529
4530static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004531display_handle_mode(void *data,
4532 struct wl_output *wl_output,
4533 uint32_t flags,
4534 int width,
4535 int height,
4536 int refresh)
4537{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004538 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004539 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004540
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004541 if (flags & WL_OUTPUT_MODE_CURRENT) {
4542 output->allocation.width = width;
4543 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004544 if (display->output_configure_handler)
4545 (*display->output_configure_handler)(
4546 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004547 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004548}
4549
4550static const struct wl_output_listener output_listener = {
4551 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004552 display_handle_mode,
4553 display_handle_done,
4554 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004555};
4556
4557static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004558display_add_output(struct display *d, uint32_t id)
4559{
4560 struct output *output;
4561
4562 output = malloc(sizeof *output);
4563 if (output == NULL)
4564 return;
4565
4566 memset(output, 0, sizeof *output);
4567 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004568 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004569 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004570 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004571 wl_list_insert(d->output_list.prev, &output->link);
4572
4573 wl_output_add_listener(output->output, &output_listener, output);
4574}
4575
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004576static void
4577output_destroy(struct output *output)
4578{
4579 if (output->destroy_handler)
4580 (*output->destroy_handler)(output, output->user_data);
4581
4582 wl_output_destroy(output->output);
4583 wl_list_remove(&output->link);
4584 free(output);
4585}
4586
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004587void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004588display_set_global_handler(struct display *display,
4589 display_global_handler_t handler)
4590{
4591 struct global *global;
4592
4593 display->global_handler = handler;
4594 if (!handler)
4595 return;
4596
4597 wl_list_for_each(global, &display->global_list, link)
4598 display->global_handler(display,
4599 global->name, global->interface,
4600 global->version, display->user_data);
4601}
4602
4603void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004604display_set_output_configure_handler(struct display *display,
4605 display_output_handler_t handler)
4606{
4607 struct output *output;
4608
4609 display->output_configure_handler = handler;
4610 if (!handler)
4611 return;
4612
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004613 wl_list_for_each(output, &display->output_list, link) {
4614 if (output->allocation.width == 0 &&
4615 output->allocation.height == 0)
4616 continue;
4617
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004618 (*display->output_configure_handler)(output,
4619 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004620 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004621}
4622
4623void
4624output_set_user_data(struct output *output, void *data)
4625{
4626 output->user_data = data;
4627}
4628
4629void *
4630output_get_user_data(struct output *output)
4631{
4632 return output->user_data;
4633}
4634
4635void
4636output_set_destroy_handler(struct output *output,
4637 display_output_handler_t handler)
4638{
4639 output->destroy_handler = handler;
4640 /* FIXME: implement this, once we have way to remove outputs */
4641}
4642
4643void
Scott Moreau4e072362012-09-29 02:03:11 -06004644output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004645{
Scott Moreau4e072362012-09-29 02:03:11 -06004646 struct rectangle allocation = output->allocation;
4647
4648 switch (output->transform) {
4649 case WL_OUTPUT_TRANSFORM_90:
4650 case WL_OUTPUT_TRANSFORM_270:
4651 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4652 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4653 /* Swap width and height */
4654 allocation.width = output->allocation.height;
4655 allocation.height = output->allocation.width;
4656 break;
4657 }
4658
4659 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004660}
4661
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004662struct wl_output *
4663output_get_wl_output(struct output *output)
4664{
4665 return output->output;
4666}
4667
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004668enum wl_output_transform
4669output_get_transform(struct output *output)
4670{
4671 return output->transform;
4672}
4673
Alexander Larssonafd319a2013-05-22 14:41:27 +02004674uint32_t
4675output_get_scale(struct output *output)
4676{
4677 return output->scale;
4678}
4679
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004680static void
Daniel Stone97f68542012-05-30 16:32:01 +01004681fini_xkb(struct input *input)
4682{
4683 xkb_state_unref(input->xkb.state);
4684 xkb_map_unref(input->xkb.keymap);
4685}
4686
4687static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004688display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004689{
4690 struct input *input;
4691
4692 input = malloc(sizeof *input);
4693 if (input == NULL)
4694 return;
4695
4696 memset(input, 0, sizeof *input);
4697 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004698 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004699 input->pointer_focus = NULL;
4700 input->keyboard_focus = NULL;
4701 wl_list_insert(d->input_list.prev, &input->link);
4702
Daniel Stone37816df2012-05-16 18:45:18 +01004703 wl_seat_add_listener(input->seat, &seat_listener, input);
4704 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004705
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004706 input->data_device =
4707 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004708 input->seat);
4709 wl_data_device_add_listener(input->data_device, &data_device_listener,
4710 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004711
4712 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004713
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004714 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4715 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004716 input->repeat_task.run = keyboard_repeat_func;
4717 display_watch_fd(d, input->repeat_timer_fd,
4718 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004719}
4720
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004721static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004722input_destroy(struct input *input)
4723{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004724 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004725 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004726
4727 if (input->drag_offer)
4728 data_offer_destroy(input->drag_offer);
4729
4730 if (input->selection_offer)
4731 data_offer_destroy(input->selection_offer);
4732
4733 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004734 fini_xkb(input);
4735
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004736 wl_surface_destroy(input->pointer_surface);
4737
Pekka Paalanene1207c72011-12-16 12:02:09 +02004738 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004739 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004740 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004741 free(input);
4742}
4743
4744static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004745init_workspace_manager(struct display *d, uint32_t id)
4746{
4747 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004748 wl_registry_bind(d->registry, id,
4749 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004750 if (d->workspace_manager != NULL)
4751 workspace_manager_add_listener(d->workspace_manager,
4752 &workspace_manager_listener,
4753 d);
4754}
4755
4756static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004757shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4758{
4759 struct display *d = data;
4760
4761 if (format == WL_SHM_FORMAT_RGB565)
4762 d->has_rgb565 = 1;
4763}
4764
4765struct wl_shm_listener shm_listener = {
4766 shm_format
4767};
4768
4769static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004770registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4771 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004772{
4773 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004774 struct global *global;
4775
4776 global = malloc(sizeof *global);
4777 global->name = id;
4778 global->interface = strdup(interface);
4779 global->version = version;
4780 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004781
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004782 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004783 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004784 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004785 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004786 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004787 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004788 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004789 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004790 d->shell = wl_registry_bind(registry,
4791 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004792 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004793 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004794 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004795 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4796 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004797 wl_registry_bind(registry, id,
4798 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004799 } else if (strcmp(interface, "text_cursor_position") == 0) {
4800 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004801 wl_registry_bind(registry, id,
4802 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004803 } else if (strcmp(interface, "workspace_manager") == 0) {
4804 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004805 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4806 d->subcompositor =
4807 wl_registry_bind(registry, id,
4808 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004809 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004810
4811 if (d->global_handler)
4812 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004813}
4814
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004815static void
4816registry_handle_global_remove(void *data, struct wl_registry *registry,
4817 uint32_t name)
4818{
4819 struct display *d = data;
4820 struct global *global;
4821 struct global *tmp;
4822
4823 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4824 if (global->name != name)
4825 continue;
4826
4827 /* XXX: Should destroy bound globals, and call
4828 * the counterpart of display::global_handler
4829 */
4830 wl_list_remove(&global->link);
4831 free(global->interface);
4832 free(global);
4833 }
4834}
4835
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004836void *
4837display_bind(struct display *display, uint32_t name,
4838 const struct wl_interface *interface, uint32_t version)
4839{
4840 return wl_registry_bind(display->registry, name, interface, version);
4841}
4842
4843static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004844 registry_handle_global,
4845 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004846};
4847
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004848#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004849static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004850init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004851{
4852 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004853 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004854
Rob Clark6396ed32012-03-11 19:48:41 -05004855#ifdef USE_CAIRO_GLESV2
4856# define GL_BIT EGL_OPENGL_ES2_BIT
4857#else
4858# define GL_BIT EGL_OPENGL_BIT
4859#endif
4860
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004861 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004862 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004863 EGL_RED_SIZE, 1,
4864 EGL_GREEN_SIZE, 1,
4865 EGL_BLUE_SIZE, 1,
4866 EGL_ALPHA_SIZE, 1,
4867 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004868 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004869 EGL_NONE
4870 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004871
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004872#ifdef USE_CAIRO_GLESV2
4873 static const EGLint context_attribs[] = {
4874 EGL_CONTEXT_CLIENT_VERSION, 2,
4875 EGL_NONE
4876 };
4877 EGLint api = EGL_OPENGL_ES_API;
4878#else
4879 EGLint *context_attribs = NULL;
4880 EGLint api = EGL_OPENGL_API;
4881#endif
4882
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004883 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004884 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004885 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004886 return -1;
4887 }
4888
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004889 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004890 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004891 return -1;
4892 }
4893
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004894 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4895 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004896 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004897 return -1;
4898 }
4899
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004900 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004901 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004902 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004903 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004904 return -1;
4905 }
4906
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004907 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004908 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004909 return -1;
4910 }
4911
Benjamin Franzke0c991632011-09-27 21:57:31 +02004912 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4913 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004914 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004915 return -1;
4916 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004917
4918 return 0;
4919}
4920
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004921static void
4922fini_egl(struct display *display)
4923{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004924 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004925
4926 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4927 EGL_NO_CONTEXT);
4928
4929 eglTerminate(display->dpy);
4930 eglReleaseThread();
4931}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004932#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004933
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004934static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004935init_dummy_surface(struct display *display)
4936{
4937 int len;
4938 void *data;
4939
4940 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4941 data = malloc(len);
4942 display->dummy_surface =
4943 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4944 1, 1, len);
4945 display->dummy_surface_data = data;
4946}
4947
4948static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004949handle_display_data(struct task *task, uint32_t events)
4950{
4951 struct display *display =
4952 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004953 struct epoll_event ep;
4954 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004955
4956 display->display_fd_events = events;
4957
4958 if (events & EPOLLERR || events & EPOLLHUP) {
4959 display_exit(display);
4960 return;
4961 }
4962
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004963 if (events & EPOLLIN) {
4964 ret = wl_display_dispatch(display->display);
4965 if (ret == -1) {
4966 display_exit(display);
4967 return;
4968 }
4969 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004970
4971 if (events & EPOLLOUT) {
4972 ret = wl_display_flush(display->display);
4973 if (ret == 0) {
4974 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4975 ep.data.ptr = &display->display_task;
4976 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4977 display->display_fd, &ep);
4978 } else if (ret == -1 && errno != EAGAIN) {
4979 display_exit(display);
4980 return;
4981 }
4982 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004983}
4984
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004985static void
4986log_handler(const char *format, va_list args)
4987{
4988 vfprintf(stderr, format, args);
4989}
4990
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004991struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004992display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004993{
4994 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004995
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004996 wl_log_set_handler_client(log_handler);
4997
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004998 d = malloc(sizeof *d);
4999 if (d == NULL)
5000 return NULL;
5001
Tim Wiederhake81bd9792011-01-23 23:25:26 +01005002 memset(d, 0, sizeof *d);
5003
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005004 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005005 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005006 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005007 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005008 return NULL;
5009 }
5010
Rob Bradford5ab9c752013-07-26 16:29:43 +01005011 d->xkb_context = xkb_context_new(0);
5012 if (d->xkb_context == NULL) {
5013 fprintf(stderr, "Failed to create XKB context\n");
5014 free(d);
5015 return NULL;
5016 }
5017
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005018 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005019 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005020 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005021 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5022 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005023
5024 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005025 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005026 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005027 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005028
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005029 d->workspace = 0;
5030 d->workspace_count = 1;
5031
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005032 d->registry = wl_display_get_registry(d->display);
5033 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005034
5035 if (wl_display_dispatch(d->display) < 0) {
5036 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5037 return NULL;
5038 }
5039
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005040#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005041 if (init_egl(d) < 0)
5042 fprintf(stderr, "EGL does not seem to work, "
5043 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005044#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005045
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005046 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005047
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005048 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005049
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005050 wl_list_init(&d->window_list);
5051
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005052 init_dummy_surface(d);
5053
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005054 return d;
5055}
5056
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005057static void
5058display_destroy_outputs(struct display *display)
5059{
5060 struct output *tmp;
5061 struct output *output;
5062
5063 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5064 output_destroy(output);
5065}
5066
Pekka Paalanene1207c72011-12-16 12:02:09 +02005067static void
5068display_destroy_inputs(struct display *display)
5069{
5070 struct input *tmp;
5071 struct input *input;
5072
5073 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5074 input_destroy(input);
5075}
5076
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005077void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005078display_destroy(struct display *display)
5079{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005080 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005081 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5082 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005083
5084 if (!wl_list_empty(&display->deferred_list))
5085 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5086
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005087 cairo_surface_destroy(display->dummy_surface);
5088 free(display->dummy_surface_data);
5089
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005090 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005091 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005092
Daniel Stone97f68542012-05-30 16:32:01 +01005093 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005094
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005095 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005096 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005097
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005098#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005099 if (display->argb_device)
5100 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005101#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005102
Pekka Paalanen35e82632013-04-25 13:57:48 +03005103 if (display->subcompositor)
5104 wl_subcompositor_destroy(display->subcompositor);
5105
Pekka Paalanenc2052982011-12-16 11:41:32 +02005106 if (display->shell)
5107 wl_shell_destroy(display->shell);
5108
5109 if (display->shm)
5110 wl_shm_destroy(display->shm);
5111
5112 if (display->data_device_manager)
5113 wl_data_device_manager_destroy(display->data_device_manager);
5114
5115 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005116 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005117
5118 close(display->epoll_fd);
5119
U. Artie Eoff44874d92012-10-02 21:12:35 -07005120 if (!(display->display_fd_events & EPOLLERR) &&
5121 !(display->display_fd_events & EPOLLHUP))
5122 wl_display_flush(display->display);
5123
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005124 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005125 free(display);
5126}
5127
5128void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005129display_set_user_data(struct display *display, void *data)
5130{
5131 display->user_data = data;
5132}
5133
5134void *
5135display_get_user_data(struct display *display)
5136{
5137 return display->user_data;
5138}
5139
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005140struct wl_display *
5141display_get_display(struct display *display)
5142{
5143 return display->display;
5144}
5145
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005146cairo_device_t *
5147display_get_cairo_device(struct display *display)
5148{
5149 return display->argb_device;
5150}
5151
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005152struct output *
5153display_get_output(struct display *display)
5154{
5155 return container_of(display->output_list.next, struct output, link);
5156}
5157
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005158struct wl_compositor *
5159display_get_compositor(struct display *display)
5160{
5161 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005162}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005163
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005164uint32_t
5165display_get_serial(struct display *display)
5166{
5167 return display->serial;
5168}
5169
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005170EGLDisplay
5171display_get_egl_display(struct display *d)
5172{
5173 return d->dpy;
5174}
5175
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005176struct wl_data_source *
5177display_create_data_source(struct display *display)
5178{
5179 return wl_data_device_manager_create_data_source(display->data_device_manager);
5180}
5181
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005182EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005183display_get_argb_egl_config(struct display *d)
5184{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005185 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005186}
5187
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005188struct wl_shell *
5189display_get_shell(struct display *display)
5190{
5191 return display->shell;
5192}
5193
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005194int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005195display_acquire_window_surface(struct display *display,
5196 struct window *window,
5197 EGLContext ctx)
5198{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005199 struct surface *surface = window->main_surface;
5200
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005201 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005202 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005203
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005204 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005205 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005206}
5207
5208void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005209display_release_window_surface(struct display *display,
5210 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005211{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005212 struct surface *surface = window->main_surface;
5213
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005214 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005215 return;
5216
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005217 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005218}
5219
5220void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005221display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005222{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005223 wl_list_insert(&display->deferred_list, &task->link);
5224}
5225
5226void
5227display_watch_fd(struct display *display,
5228 int fd, uint32_t events, struct task *task)
5229{
5230 struct epoll_event ep;
5231
5232 ep.events = events;
5233 ep.data.ptr = task;
5234 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5235}
5236
5237void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005238display_unwatch_fd(struct display *display, int fd)
5239{
5240 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5241}
5242
5243void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005244display_run(struct display *display)
5245{
5246 struct task *task;
5247 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005248 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005249
Pekka Paalanen826d7952011-12-15 10:14:07 +02005250 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005251 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005252 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005253 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005254 struct task, link);
5255 wl_list_remove(&task->link);
5256 task->run(task, 0);
5257 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005258
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005259 wl_display_dispatch_pending(display->display);
5260
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005261 if (!display->running)
5262 break;
5263
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005264 ret = wl_display_flush(display->display);
5265 if (ret < 0 && errno == EAGAIN) {
5266 ep[0].events =
5267 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5268 ep[0].data.ptr = &display->display_task;
5269
5270 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5271 display->display_fd, &ep[0]);
5272 } else if (ret < 0) {
5273 break;
5274 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005275
5276 count = epoll_wait(display->epoll_fd,
5277 ep, ARRAY_LENGTH(ep), -1);
5278 for (i = 0; i < count; i++) {
5279 task = ep[i].data.ptr;
5280 task->run(task, ep[i].events);
5281 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005282 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005283}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005284
5285void
5286display_exit(struct display *display)
5287{
5288 display->running = 0;
5289}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005290
5291void
5292keysym_modifiers_add(struct wl_array *modifiers_map,
5293 const char *name)
5294{
5295 size_t len = strlen(name) + 1;
5296 char *p;
5297
5298 p = wl_array_add(modifiers_map, len);
5299
5300 if (p == NULL)
5301 return;
5302
5303 strncpy(p, name, len);
5304}
5305
5306static xkb_mod_index_t
5307keysym_modifiers_get_index(struct wl_array *modifiers_map,
5308 const char *name)
5309{
5310 xkb_mod_index_t index = 0;
5311 char *p = modifiers_map->data;
5312
5313 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5314 if (strcmp(p, name) == 0)
5315 return index;
5316
5317 index++;
5318 p += strlen(p) + 1;
5319 }
5320
5321 return XKB_MOD_INVALID;
5322}
5323
5324xkb_mod_mask_t
5325keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5326 const char *name)
5327{
5328 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5329
5330 if (index == XKB_MOD_INVALID)
5331 return XKB_MOD_INVALID;
5332
5333 return 1 << index;
5334}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005335
5336void *
5337fail_on_null(void *p)
5338{
5339 if (p == NULL) {
5340 fprintf(stderr, "wayland-scanner: out of memory\n");
5341 exit(EXIT_FAILURE);
5342 }
5343
5344 return p;
5345}
5346
5347void *
5348xmalloc(size_t s)
5349{
5350 return fail_on_null(malloc(s));
5351}
5352
5353char *
5354xstrdup(const char *s)
5355{
5356 return fail_on_null(strdup(s));
5357}