blob: 685474529683c4cb2651ccca47899d01ad0ab06b [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;
Rob Bradford08031182013-08-13 20:11:03 +0100133 int seat_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500134};
135
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400136enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400137 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400139 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500140 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400141 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500142 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400143 TYPE_CUSTOM
144};
Rob Bradford7507b572012-05-15 17:55:34 +0100145
146struct window_output {
147 struct output *output;
148 struct wl_list link;
149};
150
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200151struct toysurface {
152 /*
153 * Prepare the surface for drawing. Makes sure there is a surface
154 * of the right size available for rendering, and returns it.
155 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200156 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200157 * If flags has SURFACE_HINT_RESIZE set, the user is
158 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200159 * Returns the Cairo surface to draw to.
160 */
161 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200162 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200163 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164
165 /*
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
169 */
170 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200171 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200172 struct rectangle *server_allocation);
173
174 /*
175 * Make the toysurface current with the given EGL context.
176 * Returns 0 on success, and negative of failure.
177 */
178 int (*acquire)(struct toysurface *base, EGLContext ctx);
179
180 /*
181 * Release the toysurface from the EGL context, returning control
182 * to Cairo.
183 */
184 void (*release)(struct toysurface *base);
185
186 /*
187 * Destroy the toysurface, including the Cairo surface, any
188 * backing storage, and the Wayland protocol objects.
189 */
190 void (*destroy)(struct toysurface *base);
191};
192
Pekka Paalanen4e373742013-02-13 16:17:13 +0200193struct surface {
194 struct window *window;
195
196 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300197 struct wl_subsurface *subsurface;
198 int synchronized;
199 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200200 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200201 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300202 int redraw_needed;
203 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300204 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200205
206 struct rectangle allocation;
207 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200208
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200209 struct wl_region *input_region;
210 struct wl_region *opaque_region;
211
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200212 enum window_buffer_type buffer_type;
213 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200214 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200215
216 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300217
218 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200219};
220
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500221struct window {
222 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500223 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100224 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500225 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200226 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400227 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500228 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500229 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400230 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400231 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300232 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400233 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400234 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100235 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400236 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400237 int focus_count;
238
Pekka Paalanen99436862012-11-19 17:15:59 +0200239 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200240 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000241 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400242
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200243 enum preferred_format preferred_format;
244
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500245 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500246 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500249 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400250 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200251 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400252
Pekka Paalanen4e373742013-02-13 16:17:13 +0200253 struct surface *main_surface;
254 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300255
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200256 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500257
Pekka Paalanen35e82632013-04-25 13:57:48 +0300258 /* struct surface::link, contains also main_surface */
259 struct wl_list subsurface_list;
260
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500261 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400262 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500263};
264
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500265struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500266 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200267 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300268 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500269 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400270 struct wl_list link;
271 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500272 widget_resize_handler_t resize_handler;
273 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500274 widget_enter_handler_t enter_handler;
275 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500276 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500277 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700278 widget_touch_down_handler_t touch_down_handler;
279 widget_touch_up_handler_t touch_up_handler;
280 widget_touch_motion_handler_t touch_motion_handler;
281 widget_touch_frame_handler_t touch_frame_handler;
282 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200283 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400284 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500285 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300286 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500287 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400288};
289
Rusty Lynch041815a2013-08-08 21:20:38 -0700290struct touch_point {
291 int32_t id;
292 struct widget *widget;
293 struct wl_list link;
294};
295
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400296struct input {
297 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303 struct window *pointer_focus;
304 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300306 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300309 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400310 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400311 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400312 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400313 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400315
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500316 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500317 struct widget *grab;
318 uint32_t grab_button;
319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100323
324 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100325 struct xkb_keymap *keymap;
326 struct xkb_state *state;
327 xkb_mod_mask_t control_mask;
328 xkb_mod_mask_t alt_mask;
329 xkb_mod_mask_t shift_mask;
330 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400331
332 struct task repeat_task;
333 int repeat_timer_fd;
334 uint32_t repeat_sym;
335 uint32_t repeat_key;
336 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400337};
338
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500339struct output {
340 struct display *display;
341 struct wl_output *output;
342 struct rectangle allocation;
343 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600344 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200345 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200346
347 display_output_handler_t destroy_handler;
348 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500349};
350
Martin Minarik1998b152012-05-10 02:04:35 +0200351enum frame_button_action {
352 FRAME_BUTTON_NULL = 0,
353 FRAME_BUTTON_ICON = 1,
354 FRAME_BUTTON_CLOSE = 2,
355 FRAME_BUTTON_MINIMIZE = 3,
356 FRAME_BUTTON_MAXIMIZE = 4,
357};
358
359enum frame_button_pointer {
360 FRAME_BUTTON_DEFAULT = 0,
361 FRAME_BUTTON_OVER = 1,
362 FRAME_BUTTON_ACTIVE = 2,
363};
364
365enum frame_button_align {
366 FRAME_BUTTON_RIGHT = 0,
367 FRAME_BUTTON_LEFT = 1,
368};
369
370enum frame_button_decoration {
371 FRAME_BUTTON_NONE = 0,
372 FRAME_BUTTON_FANCY = 1,
373};
374
375struct frame_button {
376 struct widget *widget;
377 struct frame *frame;
378 cairo_surface_t *icon;
379 enum frame_button_action type;
380 enum frame_button_pointer state;
381 struct wl_list link; /* buttons_list */
382 enum frame_button_align align;
383 enum frame_button_decoration decoration;
384};
385
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500386struct frame {
387 struct widget *widget;
388 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200389 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500390};
391
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500392struct menu {
393 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500394 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500395 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500396 const char **entries;
397 uint32_t time;
398 int current;
399 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400400 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500401 menu_func_t func;
402};
403
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300404struct tooltip {
405 struct widget *parent;
406 struct window *window;
407 struct widget *widget;
408 char *entry;
409 struct task tooltip_task;
410 int tooltip_fd;
411 float x, y;
412};
413
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700414struct shm_pool {
415 struct wl_shm_pool *pool;
416 size_t size;
417 size_t used;
418 void *data;
419};
420
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400421enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300422 CURSOR_DEFAULT = 100,
423 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400424};
425
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500426enum window_location {
427 WINDOW_INTERIOR = 0,
428 WINDOW_RESIZING_TOP = 1,
429 WINDOW_RESIZING_BOTTOM = 2,
430 WINDOW_RESIZING_LEFT = 4,
431 WINDOW_RESIZING_TOP_LEFT = 5,
432 WINDOW_RESIZING_BOTTOM_LEFT = 6,
433 WINDOW_RESIZING_RIGHT = 8,
434 WINDOW_RESIZING_TOP_RIGHT = 9,
435 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
436 WINDOW_RESIZING_MASK = 15,
437 WINDOW_EXTERIOR = 16,
438 WINDOW_TITLEBAR = 17,
439 WINDOW_CLIENT_AREA = 18,
440};
441
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200442static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400443
Pekka Paalanen97777442013-05-22 10:20:05 +0300444/* #define DEBUG */
445
446#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300447
448static void
449debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
450__attribute__ ((format (printf, 4, 5)));
451
452static void
453debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
454{
455 va_list ap;
456 struct timeval tv;
457
458 gettimeofday(&tv, NULL);
459 fprintf(stderr, "%8ld.%03ld ",
460 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
461
462 if (proxy)
463 fprintf(stderr, "%s@%d ",
464 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
465
466 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
467 fprintf(stderr, "%s ", func);
468
469 va_start(ap, fmt);
470 vfprintf(stderr, fmt, ap);
471 va_end(ap);
472}
473
474#define DBG(fmt, ...) \
475 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
476
477#define DBG_OBJ(obj, fmt, ...) \
478 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
479
480#else
481
482#define DBG(...) do {} while (0)
483#define DBG_OBJ(...) do {} while (0)
484
485#endif
486
Alexander Larsson1818e312013-05-22 14:41:31 +0200487static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200488surface_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 +0200489{
490 int32_t tmp;
491
492 switch (buffer_transform) {
493 case WL_OUTPUT_TRANSFORM_90:
494 case WL_OUTPUT_TRANSFORM_270:
495 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
496 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
497 tmp = *width;
498 *width = *height;
499 *height = tmp;
500 break;
501 default:
502 break;
503 }
504
505 *width *= buffer_scale;
506 *height *= buffer_scale;
507}
508
509static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200510buffer_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 +0200511{
512 int32_t tmp;
513
514 switch (buffer_transform) {
515 case WL_OUTPUT_TRANSFORM_90:
516 case WL_OUTPUT_TRANSFORM_270:
517 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
518 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
519 tmp = *width;
520 *width = *height;
521 *height = tmp;
522 break;
523 default:
524 break;
525 }
526
527 *width /= buffer_scale;
528 *height /= buffer_scale;
529}
530
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500531#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533struct egl_window_surface {
534 struct toysurface base;
535 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100536 struct display *display;
537 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200538 struct wl_egl_window *egl_window;
539 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100540};
541
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200542static struct egl_window_surface *
543to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100544{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200545 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100546}
547
548static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200549egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200550 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200551 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100552{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200553 struct egl_window_surface *surface = to_egl_window_surface(base);
554
Alexander Larsson1818e312013-05-22 14:41:31 +0200555 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
556
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200557 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
558 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
559
560 return cairo_surface_reference(surface->cairo_surface);
561}
562
563static void
564egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200565 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200566 struct rectangle *server_allocation)
567{
568 struct egl_window_surface *surface = to_egl_window_surface(base);
569
570 cairo_gl_surface_swapbuffers(surface->cairo_surface);
571 wl_egl_window_get_attached_size(surface->egl_window,
572 &server_allocation->width,
573 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200574
575 buffer_to_surface_size (buffer_transform, buffer_scale,
576 &server_allocation->width,
577 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200578}
579
580static int
581egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
582{
583 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200584 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100585
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200586 device = cairo_surface_get_device(surface->cairo_surface);
587 if (!device)
588 return -1;
589
590 if (!ctx) {
591 if (device == surface->display->argb_device)
592 ctx = surface->display->argb_ctx;
593 else
594 assert(0);
595 }
596
597 cairo_device_flush(device);
598 cairo_device_acquire(device);
599 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
600 surface->egl_surface, ctx))
601 fprintf(stderr, "failed to make surface current\n");
602
603 return 0;
604}
605
606static void
607egl_window_surface_release(struct toysurface *base)
608{
609 struct egl_window_surface *surface = to_egl_window_surface(base);
610 cairo_device_t *device;
611
612 device = cairo_surface_get_device(surface->cairo_surface);
613 if (!device)
614 return;
615
616 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
617 surface->display->argb_ctx))
618 fprintf(stderr, "failed to make context current\n");
619
620 cairo_device_release(device);
621}
622
623static void
624egl_window_surface_destroy(struct toysurface *base)
625{
626 struct egl_window_surface *surface = to_egl_window_surface(base);
627 struct display *d = surface->display;
628
629 cairo_surface_destroy(surface->cairo_surface);
630 eglDestroySurface(d->dpy, surface->egl_surface);
631 wl_egl_window_destroy(surface->egl_window);
632 surface->surface = NULL;
633
634 free(surface);
635}
636
637static struct toysurface *
638egl_window_surface_create(struct display *display,
639 struct wl_surface *wl_surface,
640 uint32_t flags,
641 struct rectangle *rectangle)
642{
643 struct egl_window_surface *surface;
644
Pekka Paalanenb3627362012-11-19 17:16:00 +0200645 if (display->dpy == EGL_NO_DISPLAY)
646 return NULL;
647
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200648 surface = calloc(1, sizeof *surface);
649 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650 return NULL;
651
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200652 surface->base.prepare = egl_window_surface_prepare;
653 surface->base.swap = egl_window_surface_swap;
654 surface->base.acquire = egl_window_surface_acquire;
655 surface->base.release = egl_window_surface_release;
656 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100657
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200658 surface->display = display;
659 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400660
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200661 surface->egl_window = wl_egl_window_create(surface->surface,
662 rectangle->width,
663 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100664
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200665 surface->egl_surface = eglCreateWindowSurface(display->dpy,
666 display->argb_config,
667 surface->egl_window,
668 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100669
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200670 surface->cairo_surface =
671 cairo_gl_surface_create_for_egl(display->argb_device,
672 surface->egl_surface,
673 rectangle->width,
674 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100675
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200676 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100677}
678
Pekka Paalanenb3627362012-11-19 17:16:00 +0200679#else
680
681static struct toysurface *
682egl_window_surface_create(struct display *display,
683 struct wl_surface *wl_surface,
684 uint32_t flags,
685 struct rectangle *rectangle)
686{
687 return NULL;
688}
689
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690#endif
691
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200692struct shm_surface_data {
693 struct wl_buffer *buffer;
694 struct shm_pool *pool;
695};
696
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400697struct wl_buffer *
698display_get_buffer_for_surface(struct display *display,
699 cairo_surface_t *surface)
700{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200701 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200703 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704
705 return data->buffer;
706}
707
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500708static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700709shm_pool_destroy(struct shm_pool *pool);
710
711static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400712shm_surface_data_destroy(void *p)
713{
714 struct shm_surface_data *data = p;
715
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200716 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700717 if (data->pool)
718 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300719
720 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400721}
722
Kristian Høgsberg16626282012-04-03 11:21:27 -0400723static struct wl_shm_pool *
724make_shm_pool(struct display *display, int size, void **data)
725{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400726 struct wl_shm_pool *pool;
727 int fd;
728
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300729 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400730 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300731 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
732 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400733 return NULL;
734 }
735
736 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400737 if (*data == MAP_FAILED) {
738 fprintf(stderr, "mmap failed: %m\n");
739 close(fd);
740 return NULL;
741 }
742
743 pool = wl_shm_create_pool(display->shm, fd, size);
744
745 close(fd);
746
747 return pool;
748}
749
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700750static struct shm_pool *
751shm_pool_create(struct display *display, size_t size)
752{
753 struct shm_pool *pool = malloc(sizeof *pool);
754
755 if (!pool)
756 return NULL;
757
758 pool->pool = make_shm_pool(display, size, &pool->data);
759 if (!pool->pool) {
760 free(pool);
761 return NULL;
762 }
763
764 pool->size = size;
765 pool->used = 0;
766
767 return pool;
768}
769
770static void *
771shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
772{
773 if (pool->used + size > pool->size)
774 return NULL;
775
776 *offset = pool->used;
777 pool->used += size;
778
779 return (char *) pool->data + *offset;
780}
781
782/* destroy the pool. this does not unmap the memory though */
783static void
784shm_pool_destroy(struct shm_pool *pool)
785{
786 munmap(pool->data, pool->size);
787 wl_shm_pool_destroy(pool->pool);
788 free(pool);
789}
790
791/* Start allocating from the beginning of the pool again */
792static void
793shm_pool_reset(struct shm_pool *pool)
794{
795 pool->used = 0;
796}
797
798static int
799data_length_for_shm_surface(struct rectangle *rect)
800{
801 int stride;
802
803 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
804 rect->width);
805 return stride * rect->height;
806}
807
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500808static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700809display_create_shm_surface_from_pool(struct display *display,
810 struct rectangle *rectangle,
811 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400812{
813 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400814 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400815 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200816 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700817 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400818 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400819
820 data = malloc(sizeof *data);
821 if (data == NULL)
822 return NULL;
823
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200824 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
825 cairo_format = CAIRO_FORMAT_RGB16_565;
826 else
827 cairo_format = CAIRO_FORMAT_ARGB32;
828
829 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700830 length = stride * rectangle->height;
831 data->pool = NULL;
832 map = shm_pool_allocate(pool, length, &offset);
833
834 if (!map) {
835 free(data);
836 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400837 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400838
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400839 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200840 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400841 rectangle->width,
842 rectangle->height,
843 stride);
844
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200845 cairo_surface_set_user_data(surface, &shm_surface_data_key,
846 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200848 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
849 format = WL_SHM_FORMAT_RGB565;
850 else {
851 if (flags & SURFACE_OPAQUE)
852 format = WL_SHM_FORMAT_XRGB8888;
853 else
854 format = WL_SHM_FORMAT_ARGB8888;
855 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400856
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200857 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
858 rectangle->width,
859 rectangle->height,
860 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400861
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700862 return surface;
863}
864
865static cairo_surface_t *
866display_create_shm_surface(struct display *display,
867 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200868 struct shm_pool *alternate_pool,
869 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700870{
871 struct shm_surface_data *data;
872 struct shm_pool *pool;
873 cairo_surface_t *surface;
874
Pekka Paalanen99436862012-11-19 17:15:59 +0200875 if (alternate_pool) {
876 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700877 surface = display_create_shm_surface_from_pool(display,
878 rectangle,
879 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200880 alternate_pool);
881 if (surface) {
882 data = cairo_surface_get_user_data(surface,
883 &shm_surface_data_key);
884 goto out;
885 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700886 }
887
888 pool = shm_pool_create(display,
889 data_length_for_shm_surface(rectangle));
890 if (!pool)
891 return NULL;
892
893 surface =
894 display_create_shm_surface_from_pool(display, rectangle,
895 flags, pool);
896
897 if (!surface) {
898 shm_pool_destroy(pool);
899 return NULL;
900 }
901
902 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200903 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700904 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400905
Pekka Paalanen99436862012-11-19 17:15:59 +0200906out:
907 if (data_ret)
908 *data_ret = data;
909
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400910 return surface;
911}
912
nobled7b87cb02011-02-01 18:51:47 +0000913static int
914check_size(struct rectangle *rect)
915{
916 if (rect->width && rect->height)
917 return 0;
918
919 fprintf(stderr, "tried to create surface of "
920 "width: %d, height: %d\n", rect->width, rect->height);
921 return -1;
922}
923
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400924cairo_surface_t *
925display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100926 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400927 struct rectangle *rectangle,
928 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400929{
nobled7b87cb02011-02-01 18:51:47 +0000930 if (check_size(rectangle) < 0)
931 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200932
933 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200934 return display_create_shm_surface(display, rectangle, flags,
935 NULL, NULL);
936}
937
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938struct shm_surface_leaf {
939 cairo_surface_t *cairo_surface;
940 /* 'data' is automatically destroyed, when 'cairo_surface' is */
941 struct shm_surface_data *data;
942
943 struct shm_pool *resize_pool;
944 int busy;
945};
946
947static void
948shm_surface_leaf_release(struct shm_surface_leaf *leaf)
949{
950 if (leaf->cairo_surface)
951 cairo_surface_destroy(leaf->cairo_surface);
952 /* leaf->data already destroyed via cairo private */
953
954 if (leaf->resize_pool)
955 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200956
957 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200958}
959
Pekka Paalanenaef02542013-04-25 13:57:47 +0300960#define MAX_LEAVES 3
961
Pekka Paalanen99436862012-11-19 17:15:59 +0200962struct shm_surface {
963 struct toysurface base;
964 struct display *display;
965 struct wl_surface *surface;
966 uint32_t flags;
967 int dx, dy;
968
Pekka Paalanenaef02542013-04-25 13:57:47 +0300969 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200970 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200971};
972
973static struct shm_surface *
974to_shm_surface(struct toysurface *base)
975{
976 return container_of(base, struct shm_surface, base);
977}
978
Pekka Paalanena4eda732012-11-19 17:16:02 +0200979static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300980shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
981{
982#ifdef DEBUG
983 struct shm_surface_leaf *leaf;
984 char bufs[MAX_LEAVES + 1];
985 int i;
986
987 for (i = 0; i < MAX_LEAVES; i++) {
988 leaf = &surface->leaf[i];
989
990 if (leaf->busy)
991 bufs[i] = 'b';
992 else if (leaf->cairo_surface)
993 bufs[i] = 'a';
994 else
995 bufs[i] = ' ';
996 }
997
998 bufs[MAX_LEAVES] = '\0';
999 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1000#endif
1001}
1002
1003static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001004shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1005{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001006 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001007 struct shm_surface_leaf *leaf;
1008 int i;
1009 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001010
1011 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001012
Pekka Paalanenaef02542013-04-25 13:57:47 +03001013 for (i = 0; i < MAX_LEAVES; i++) {
1014 leaf = &surface->leaf[i];
1015 if (leaf->data && leaf->data->buffer == buffer) {
1016 leaf->busy = 0;
1017 break;
1018 }
1019 }
1020 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001021
Pekka Paalanenaef02542013-04-25 13:57:47 +03001022 /* Leave one free leaf with storage, release others */
1023 free_found = 0;
1024 for (i = 0; i < MAX_LEAVES; i++) {
1025 leaf = &surface->leaf[i];
1026
1027 if (!leaf->cairo_surface || leaf->busy)
1028 continue;
1029
1030 if (!free_found)
1031 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001032 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001033 shm_surface_leaf_release(leaf);
1034 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001035
Pekka Paalanen97777442013-05-22 10:20:05 +03001036 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001037}
1038
1039static const struct wl_buffer_listener shm_surface_buffer_listener = {
1040 shm_surface_buffer_release
1041};
1042
Pekka Paalanen99436862012-11-19 17:15:59 +02001043static cairo_surface_t *
1044shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001045 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001046 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001047{
Pekka Paalanenec076692012-11-30 13:37:27 +02001048 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001049 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001050 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001051 struct shm_surface_leaf *leaf = NULL;
1052 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001053
1054 surface->dx = dx;
1055 surface->dy = dy;
1056
Pekka Paalanenaef02542013-04-25 13:57:47 +03001057 /* pick a free buffer, preferrably one that already has storage */
1058 for (i = 0; i < MAX_LEAVES; i++) {
1059 if (surface->leaf[i].busy)
1060 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001061
Pekka Paalanenaef02542013-04-25 13:57:47 +03001062 if (!leaf || surface->leaf[i].cairo_surface)
1063 leaf = &surface->leaf[i];
1064 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001065 DBG_OBJ(surface->surface, "pick leaf %d\n",
1066 (int)(leaf - &surface->leaf[0]));
1067
Pekka Paalanenaef02542013-04-25 13:57:47 +03001068 if (!leaf) {
1069 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001070 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001071 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001072 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001073 }
1074
Pekka Paalanena4eda732012-11-19 17:16:02 +02001075 if (!resize_hint && leaf->resize_pool) {
1076 cairo_surface_destroy(leaf->cairo_surface);
1077 leaf->cairo_surface = NULL;
1078 shm_pool_destroy(leaf->resize_pool);
1079 leaf->resize_pool = NULL;
1080 }
1081
Alexander Larsson1818e312013-05-22 14:41:31 +02001082 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1083
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 if (leaf->cairo_surface &&
1085 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1086 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001087 goto out;
1088
Pekka Paalanena4eda732012-11-19 17:16:02 +02001089 if (leaf->cairo_surface)
1090 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001091
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001092#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001094 /* Create a big pool to allocate from, while continuously
1095 * resizing. Mmapping a new pool in the server
1096 * is relatively expensive, so reusing a pool performs
1097 * better, but may temporarily reserve unneeded memory.
1098 */
1099 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 leaf->resize_pool = shm_pool_create(surface->display,
1101 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001102 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001103#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001104
Alexander Larsson1818e312013-05-22 14:41:31 +02001105 rect.width = width;
1106 rect.height = height;
1107
Pekka Paalanena4eda732012-11-19 17:16:02 +02001108 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001109 display_create_shm_surface(surface->display, &rect,
1110 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 leaf->resize_pool,
1112 &leaf->data);
1113 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001114 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001115
1116out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001117 surface->current = leaf;
1118
1119 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001120}
1121
1122static void
1123shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001124 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001125 struct rectangle *server_allocation)
1126{
1127 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001128 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001129
1130 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001131 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001132 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001133 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001134
Alexander Larsson1818e312013-05-22 14:41:31 +02001135 buffer_to_surface_size (buffer_transform, buffer_scale,
1136 &server_allocation->width,
1137 &server_allocation->height);
1138
Pekka Paalanena4eda732012-11-19 17:16:02 +02001139 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001140 surface->dx, surface->dy);
1141 wl_surface_damage(surface->surface, 0, 0,
1142 server_allocation->width, server_allocation->height);
1143 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001144
Pekka Paalanen71233882013-04-25 13:57:53 +03001145 DBG_OBJ(surface->surface, "leaf %d busy\n",
1146 (int)(leaf - &surface->leaf[0]));
1147
Pekka Paalanena4eda732012-11-19 17:16:02 +02001148 leaf->busy = 1;
1149 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001150}
1151
1152static int
1153shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1154{
1155 return -1;
1156}
1157
1158static void
1159shm_surface_release(struct toysurface *base)
1160{
1161}
1162
1163static void
1164shm_surface_destroy(struct toysurface *base)
1165{
1166 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001167 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001168
Pekka Paalanenaef02542013-04-25 13:57:47 +03001169 for (i = 0; i < MAX_LEAVES; i++)
1170 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001171
1172 free(surface);
1173}
1174
1175static struct toysurface *
1176shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1177 uint32_t flags, struct rectangle *rectangle)
1178{
1179 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001180 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001181
Brian Lovinbc919262013-08-07 15:34:59 -07001182 surface = xmalloc(sizeof *surface);
1183 memset(surface, 0, sizeof *surface);
1184
Pekka Paalanen99436862012-11-19 17:15:59 +02001185 if (!surface)
1186 return NULL;
1187
1188 surface->base.prepare = shm_surface_prepare;
1189 surface->base.swap = shm_surface_swap;
1190 surface->base.acquire = shm_surface_acquire;
1191 surface->base.release = shm_surface_release;
1192 surface->base.destroy = shm_surface_destroy;
1193
1194 surface->display = display;
1195 surface->surface = wl_surface;
1196 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001197
1198 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001199}
1200
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001201/*
1202 * The following correspondences between file names and cursors was copied
1203 * from: https://bugs.kde.org/attachment.cgi?id=67313
1204 */
1205
1206static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001207 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001208 "sw-resize",
1209 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001210};
1211
1212static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001213 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001214 "se-resize",
1215 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001216};
1217
1218static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001219 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001220 "s-resize",
1221 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001222};
1223
1224static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001225 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001226 "closedhand",
1227 "208530c400c041818281048008011002"
1228};
1229
1230static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001231 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001232 "default",
1233 "top_left_arrow",
1234 "left-arrow"
1235};
1236
1237static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001238 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001239 "w-resize",
1240 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001241};
1242
1243static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001244 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001245 "e-resize",
1246 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001247};
1248
1249static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001250 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001251 "nw-resize",
1252 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001253};
1254
1255static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001256 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001257 "ne-resize",
1258 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001259};
1260
1261static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001262 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001263 "n-resize",
1264 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001265};
1266
1267static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001268 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001269 "ibeam",
1270 "text"
1271};
1272
1273static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001274 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001275 "pointer",
1276 "pointing_hand",
1277 "e29285e634086352946a0e7090d73106"
1278};
1279
1280static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001281 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001282 "wait",
1283 "0426c94ea35c87780ff01dc239897213"
1284};
1285
1286struct cursor_alternatives {
1287 const char **names;
1288 size_t count;
1289};
1290
1291static const struct cursor_alternatives cursors[] = {
1292 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1293 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1294 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1295 {grabbings, ARRAY_LENGTH(grabbings)},
1296 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1297 {left_sides, ARRAY_LENGTH(left_sides)},
1298 {right_sides, ARRAY_LENGTH(right_sides)},
1299 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1300 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1301 {top_sides, ARRAY_LENGTH(top_sides)},
1302 {xterms, ARRAY_LENGTH(xterms)},
1303 {hand1s, ARRAY_LENGTH(hand1s)},
1304 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001305};
1306
1307static void
1308create_cursors(struct display *display)
1309{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001310 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001311 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001312 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001313 unsigned int i, j;
1314 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001315 struct config_key shell_keys[] = {
1316 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001317 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001318 };
1319 struct config_section cs[] = {
1320 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1321 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001322
Ossama Othmana50e6e42013-05-14 09:48:26 -07001323 config_fd = open_config_file("weston.ini");
1324 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1325 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001326
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001327 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001328 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001329 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001330
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001331 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001332 cursor = NULL;
1333 for (j = 0; !cursor && j < cursors[i].count; ++j)
1334 cursor = wl_cursor_theme_get_cursor(
1335 display->cursor_theme, cursors[i].names[j]);
1336
1337 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001338 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001339 cursors[i].names[0]);
1340
1341 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001342 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001343}
1344
1345static void
1346destroy_cursors(struct display *display)
1347{
1348 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001349 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001350}
1351
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001352struct wl_cursor_image *
1353display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001354{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001355 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001356
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001357 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001358}
1359
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001360static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001361surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001362{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001363 if (!surface->cairo_surface)
1364 return;
1365
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001366 if (surface->opaque_region) {
1367 wl_surface_set_opaque_region(surface->surface,
1368 surface->opaque_region);
1369 wl_region_destroy(surface->opaque_region);
1370 surface->opaque_region = NULL;
1371 }
1372
1373 if (surface->input_region) {
1374 wl_surface_set_input_region(surface->surface,
1375 surface->input_region);
1376 wl_region_destroy(surface->input_region);
1377 surface->input_region = NULL;
1378 }
1379
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001380 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001381 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001382 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001383
Pekka Paalanen89dee002013-02-13 16:17:20 +02001384 cairo_surface_destroy(surface->cairo_surface);
1385 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001386}
1387
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001388int
1389window_has_focus(struct window *window)
1390{
1391 return window->focus_count > 0;
1392}
1393
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001394static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001395window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001396{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001397 struct surface *surface;
1398
Pekka Paalanen89dee002013-02-13 16:17:20 +02001399 if (window->type == TYPE_NONE) {
1400 window->type = TYPE_TOPLEVEL;
1401 if (window->shell_surface)
1402 wl_shell_surface_set_toplevel(window->shell_surface);
1403 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001404
Pekka Paalanen35e82632013-04-25 13:57:48 +03001405 wl_list_for_each(surface, &window->subsurface_list, link) {
1406 if (surface == window->main_surface)
1407 continue;
1408
1409 surface_flush(surface);
1410 }
1411
Pekka Paalanen89dee002013-02-13 16:17:20 +02001412 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001413}
1414
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001415struct display *
1416window_get_display(struct window *window)
1417{
1418 return window->display;
1419}
1420
Pekka Paalanen89dee002013-02-13 16:17:20 +02001421static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001422surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001423{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001425 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001426
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001427 if (!surface->toysurface && display->dpy &&
1428 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001429 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001430 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001431 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001432 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001433 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001434 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001435
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001436 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001437 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001438 surface->surface,
1439 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001440
Pekka Paalanen89dee002013-02-13 16:17:20 +02001441 surface->cairo_surface = surface->toysurface->prepare(
1442 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001443 allocation.width, allocation.height, flags,
1444 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001445}
1446
1447static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001448window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001449{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001450 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001451 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001452 int dx = 0;
1453 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001454
Pekka Paalanenec076692012-11-30 13:37:27 +02001455 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001456 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001457
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001458 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1459 flags |= SURFACE_HINT_RGB565;
1460
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001461 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1462 dx = surface->server_allocation.width -
1463 surface->allocation.width;
1464
1465 if (window->resize_edges & WINDOW_RESIZING_TOP)
1466 dy = surface->server_allocation.height -
1467 surface->allocation.height;
1468
1469 window->resize_edges = 0;
1470
Pekka Paalanen89dee002013-02-13 16:17:20 +02001471 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001472}
1473
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001474int
1475window_get_buffer_transform(struct window *window)
1476{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001477 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001478}
1479
1480void
1481window_set_buffer_transform(struct window *window,
1482 enum wl_output_transform transform)
1483{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001484 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001485 wl_surface_set_buffer_transform(window->main_surface->surface,
1486 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001487}
1488
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001489void
1490window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001491 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001492{
1493 window->main_surface->buffer_scale = scale;
1494 wl_surface_set_buffer_scale(window->main_surface->surface,
1495 scale);
1496}
1497
1498uint32_t
1499window_get_buffer_scale(struct window *window)
1500{
1501 return window->main_surface->buffer_scale;
1502}
1503
Alexander Larssond68f5232013-05-22 14:41:33 +02001504uint32_t
1505window_get_output_scale(struct window *window)
1506{
1507 struct window_output *window_output;
1508 struct window_output *window_output_tmp;
1509 int scale = 1;
1510
1511 wl_list_for_each_safe(window_output, window_output_tmp,
1512 &window->window_output_list, link) {
1513 if (window_output->output->scale > scale)
1514 scale = window_output->output->scale;
1515 }
1516
1517 return scale;
1518}
1519
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001520static void frame_destroy(struct frame *frame);
1521
Pekka Paalanen4e373742013-02-13 16:17:13 +02001522static void
1523surface_destroy(struct surface *surface)
1524{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001525 if (surface->frame_cb)
1526 wl_callback_destroy(surface->frame_cb);
1527
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001528 if (surface->input_region)
1529 wl_region_destroy(surface->input_region);
1530
1531 if (surface->opaque_region)
1532 wl_region_destroy(surface->opaque_region);
1533
Pekka Paalanen35e82632013-04-25 13:57:48 +03001534 if (surface->subsurface)
1535 wl_subsurface_destroy(surface->subsurface);
1536
Pekka Paalanen4e373742013-02-13 16:17:13 +02001537 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001538
1539 if (surface->toysurface)
1540 surface->toysurface->destroy(surface->toysurface);
1541
Pekka Paalanen35e82632013-04-25 13:57:48 +03001542 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001543 free(surface);
1544}
1545
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001546void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001547window_destroy(struct window *window)
1548{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001549 struct display *display = window->display;
1550 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001551 struct window_output *window_output;
1552 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001553
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001554 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001555
Rusty Lynch1084da52013-08-15 09:10:08 -07001556 wl_list_for_each(input, &display->input_list, link) {
1557 if (input->touch_focus == window)
1558 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001559 if (input->pointer_focus == window)
1560 input->pointer_focus = NULL;
1561 if (input->keyboard_focus == window)
1562 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001563 if (input->focus_widget &&
1564 input->focus_widget->window == window)
1565 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001566 }
1567
Rob Bradford7507b572012-05-15 17:55:34 +01001568 wl_list_for_each_safe(window_output, window_output_tmp,
1569 &window->window_output_list, link) {
1570 free (window_output);
1571 }
1572
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001573 if (window->frame)
1574 frame_destroy(window->frame);
1575
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001576 if (window->shell_surface)
1577 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001578
1579 surface_destroy(window->main_surface);
1580
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001581 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001582
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001583 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001584 free(window);
1585}
1586
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001587static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001588widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001589{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001590 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001591
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001592 wl_list_for_each(child, &widget->child_list, link) {
1593 target = widget_find_widget(child, x, y);
1594 if (target)
1595 return target;
1596 }
1597
1598 if (widget->allocation.x <= x &&
1599 x < widget->allocation.x + widget->allocation.width &&
1600 widget->allocation.y <= y &&
1601 y < widget->allocation.y + widget->allocation.height) {
1602 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001603 }
1604
1605 return NULL;
1606}
1607
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001608static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001609window_find_widget(struct window *window, int32_t x, int32_t y)
1610{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001611 struct surface *surface;
1612 struct widget *widget;
1613
1614 wl_list_for_each(surface, &window->subsurface_list, link) {
1615 widget = widget_find_widget(surface->widget, x, y);
1616 if (widget)
1617 return widget;
1618 }
1619
1620 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001621}
1622
1623static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001624widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001625{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001626 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001627
Peter Huttererf3d62272013-08-08 11:57:05 +10001628 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001629 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001630 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001631 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001632 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001633 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001634 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001635 widget->tooltip = NULL;
1636 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001637 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001638
1639 return widget;
1640}
1641
1642struct widget *
1643window_add_widget(struct window *window, void *data)
1644{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001645 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001646
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001647 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001648 wl_list_init(&widget->link);
1649 window->main_surface->widget = widget;
1650
1651 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001652}
1653
1654struct widget *
1655widget_add_widget(struct widget *parent, void *data)
1656{
1657 struct widget *widget;
1658
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001659 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001660 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001661
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001662 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663}
1664
1665void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001666widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001667{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001668 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001669 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001670 struct input *input;
1671
Pekka Paalanen35e82632013-04-25 13:57:48 +03001672 /* Destroy the sub-surface along with the root widget */
1673 if (surface->widget == widget && surface->subsurface)
1674 surface_destroy(widget->surface);
1675
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001676 if (widget->tooltip) {
1677 free(widget->tooltip);
1678 widget->tooltip = NULL;
1679 }
1680
Pekka Paalanene156fb62012-01-19 13:51:38 +02001681 wl_list_for_each(input, &display->input_list, link) {
1682 if (input->focus_widget == widget)
1683 input->focus_widget = NULL;
1684 }
1685
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001686 wl_list_remove(&widget->link);
1687 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001688}
1689
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001691widget_set_default_cursor(struct widget *widget, int cursor)
1692{
1693 widget->default_cursor = cursor;
1694}
1695
1696void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001697widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001698{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001699 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001700}
1701
1702void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001703widget_set_size(struct widget *widget, int32_t width, int32_t height)
1704{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001705 widget->allocation.width = width;
1706 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001707}
1708
1709void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001710widget_set_allocation(struct widget *widget,
1711 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001712{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001713 widget->allocation.x = x;
1714 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001715 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001716}
1717
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001718void
1719widget_set_transparent(struct widget *widget, int transparent)
1720{
1721 widget->opaque = !transparent;
1722}
1723
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001724void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001725widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001726{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001727 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001728}
1729
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001730static cairo_surface_t *
1731widget_get_cairo_surface(struct widget *widget)
1732{
1733 struct surface *surface = widget->surface;
1734 struct window *window = widget->window;
1735
1736 if (!surface->cairo_surface) {
1737 if (surface == window->main_surface)
1738 window_create_main_surface(window);
1739 else
1740 surface_create_surface(surface, 0, 0, 0);
1741 }
1742
1743 return surface->cairo_surface;
1744}
1745
Alexander Larsson15901f02013-05-22 14:41:25 +02001746static void
1747widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1748{
1749 struct surface *surface = widget->surface;
1750 double angle;
1751 cairo_matrix_t m;
1752 enum wl_output_transform transform;
1753 int surface_width, surface_height;
1754 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001755 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001756
1757 surface_width = surface->allocation.width;
1758 surface_height = surface->allocation.height;
1759
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001760 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001761 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001762
Alexander Larsson15901f02013-05-22 14:41:25 +02001763 switch (transform) {
1764 case WL_OUTPUT_TRANSFORM_FLIPPED:
1765 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1766 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1767 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1768 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1769 break;
1770 default:
1771 cairo_matrix_init_identity(&m);
1772 break;
1773 }
1774
1775 switch (transform) {
1776 case WL_OUTPUT_TRANSFORM_NORMAL:
1777 default:
1778 angle = 0;
1779 translate_x = 0;
1780 translate_y = 0;
1781 break;
1782 case WL_OUTPUT_TRANSFORM_FLIPPED:
1783 angle = 0;
1784 translate_x = surface_width;
1785 translate_y = 0;
1786 break;
1787 case WL_OUTPUT_TRANSFORM_90:
1788 angle = M_PI_2;
1789 translate_x = surface_height;
1790 translate_y = 0;
1791 break;
1792 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1793 angle = M_PI_2;
1794 translate_x = surface_height;
1795 translate_y = surface_width;
1796 break;
1797 case WL_OUTPUT_TRANSFORM_180:
1798 angle = M_PI;
1799 translate_x = surface_width;
1800 translate_y = surface_height;
1801 break;
1802 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1803 angle = M_PI;
1804 translate_x = 0;
1805 translate_y = surface_height;
1806 break;
1807 case WL_OUTPUT_TRANSFORM_270:
1808 angle = M_PI + M_PI_2;
1809 translate_x = 0;
1810 translate_y = surface_width;
1811 break;
1812 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1813 angle = M_PI + M_PI_2;
1814 translate_x = 0;
1815 translate_y = 0;
1816 break;
1817 }
1818
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001819 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001820 cairo_translate(cr, translate_x, translate_y);
1821 cairo_rotate(cr, angle);
1822 cairo_transform(cr, &m);
1823}
1824
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001825cairo_t *
1826widget_cairo_create(struct widget *widget)
1827{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001828 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001829 cairo_surface_t *cairo_surface;
1830 cairo_t *cr;
1831
1832 cairo_surface = widget_get_cairo_surface(widget);
1833 cr = cairo_create(cairo_surface);
1834
Alexander Larsson15901f02013-05-22 14:41:25 +02001835 widget_cairo_update_transform(widget, cr);
1836
Pekka Paalanen35e82632013-04-25 13:57:48 +03001837 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1838
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001839 return cr;
1840}
1841
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001842struct wl_surface *
1843widget_get_wl_surface(struct widget *widget)
1844{
1845 return widget->surface->surface;
1846}
1847
1848uint32_t
1849widget_get_last_time(struct widget *widget)
1850{
1851 return widget->surface->last_time;
1852}
1853
1854void
1855widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1856{
1857 struct wl_compositor *comp = widget->window->display->compositor;
1858 struct surface *surface = widget->surface;
1859
1860 if (!surface->input_region)
1861 surface->input_region = wl_compositor_create_region(comp);
1862
1863 if (rect) {
1864 wl_region_add(surface->input_region,
1865 rect->x, rect->y, rect->width, rect->height);
1866 }
1867}
1868
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001869void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001870widget_set_resize_handler(struct widget *widget,
1871 widget_resize_handler_t handler)
1872{
1873 widget->resize_handler = handler;
1874}
1875
1876void
1877widget_set_redraw_handler(struct widget *widget,
1878 widget_redraw_handler_t handler)
1879{
1880 widget->redraw_handler = handler;
1881}
1882
1883void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001884widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001885{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001886 widget->enter_handler = handler;
1887}
1888
1889void
1890widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1891{
1892 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001893}
1894
1895void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001896widget_set_motion_handler(struct widget *widget,
1897 widget_motion_handler_t handler)
1898{
1899 widget->motion_handler = handler;
1900}
1901
1902void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001903widget_set_button_handler(struct widget *widget,
1904 widget_button_handler_t handler)
1905{
1906 widget->button_handler = handler;
1907}
1908
1909void
Rusty Lynch041815a2013-08-08 21:20:38 -07001910widget_set_touch_up_handler(struct widget *widget,
1911 widget_touch_up_handler_t handler)
1912{
1913 widget->touch_up_handler = handler;
1914}
1915
1916void
1917widget_set_touch_down_handler(struct widget *widget,
1918 widget_touch_down_handler_t handler)
1919{
1920 widget->touch_down_handler = handler;
1921}
1922
1923void
1924widget_set_touch_motion_handler(struct widget *widget,
1925 widget_touch_motion_handler_t handler)
1926{
1927 widget->touch_motion_handler = handler;
1928}
1929
1930void
1931widget_set_touch_frame_handler(struct widget *widget,
1932 widget_touch_frame_handler_t handler)
1933{
1934 widget->touch_frame_handler = handler;
1935}
1936
1937void
1938widget_set_touch_cancel_handler(struct widget *widget,
1939 widget_touch_cancel_handler_t handler)
1940{
1941 widget->touch_cancel_handler = handler;
1942}
1943
1944void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001945widget_set_axis_handler(struct widget *widget,
1946 widget_axis_handler_t handler)
1947{
1948 widget->axis_handler = handler;
1949}
1950
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001951static void
1952window_schedule_redraw_task(struct window *window);
1953
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001954void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001955widget_schedule_redraw(struct widget *widget)
1956{
Pekka Paalanen71233882013-04-25 13:57:53 +03001957 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001958 widget->surface->redraw_needed = 1;
1959 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001960}
1961
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001962cairo_surface_t *
1963window_get_surface(struct window *window)
1964{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001965 cairo_surface_t *cairo_surface;
1966
1967 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1968
1969 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001970}
1971
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001972struct wl_surface *
1973window_get_wl_surface(struct window *window)
1974{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001975 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001976}
1977
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001978struct wl_shell_surface *
1979window_get_wl_shell_surface(struct window *window)
1980{
1981 return window->shell_surface;
1982}
1983
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001984static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001985tooltip_redraw_handler(struct widget *widget, void *data)
1986{
1987 cairo_t *cr;
1988 const int32_t r = 3;
1989 struct tooltip *tooltip = data;
1990 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001991
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001992 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001993 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1994 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1995 cairo_paint(cr);
1996
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001997 width = widget->allocation.width;
1998 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001999 rounded_rect(cr, 0, 0, width, height, r);
2000
2001 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2002 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2003 cairo_fill(cr);
2004
2005 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2006 cairo_move_to(cr, 10, 16);
2007 cairo_show_text(cr, tooltip->entry);
2008 cairo_destroy(cr);
2009}
2010
2011static cairo_text_extents_t
2012get_text_extents(struct tooltip *tooltip)
2013{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002014 cairo_t *cr;
2015 cairo_text_extents_t extents;
2016
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002017 /* Use the dummy_surface because tooltip's surface was not
2018 * created yet, and parent does not have a valid surface
2019 * outside repaint, either.
2020 */
2021 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002022 cairo_text_extents(cr, tooltip->entry, &extents);
2023 cairo_destroy(cr);
2024
2025 return extents;
2026}
2027
2028static int
2029window_create_tooltip(struct tooltip *tooltip)
2030{
2031 struct widget *parent = tooltip->parent;
2032 struct display *display = parent->window->display;
2033 struct window *window;
2034 const int offset_y = 27;
2035 const int margin = 3;
2036 cairo_text_extents_t extents;
2037
2038 if (tooltip->widget)
2039 return 0;
2040
2041 window = window_create_transient(display, parent->window, tooltip->x,
2042 tooltip->y + offset_y,
2043 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2044 if (!window)
2045 return -1;
2046
2047 tooltip->window = window;
2048 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2049
2050 extents = get_text_extents(tooltip);
2051 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2052 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2053
2054 return 0;
2055}
2056
2057void
2058widget_destroy_tooltip(struct widget *parent)
2059{
2060 struct tooltip *tooltip = parent->tooltip;
2061
2062 parent->tooltip_count = 0;
2063 if (!tooltip)
2064 return;
2065
2066 if (tooltip->widget) {
2067 widget_destroy(tooltip->widget);
2068 window_destroy(tooltip->window);
2069 tooltip->widget = NULL;
2070 tooltip->window = NULL;
2071 }
2072
2073 close(tooltip->tooltip_fd);
2074 free(tooltip->entry);
2075 free(tooltip);
2076 parent->tooltip = NULL;
2077}
2078
2079static void
2080tooltip_func(struct task *task, uint32_t events)
2081{
2082 struct tooltip *tooltip =
2083 container_of(task, struct tooltip, tooltip_task);
2084 uint64_t exp;
2085
Martin Olsson8df662a2012-07-08 03:03:47 +02002086 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2087 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002088 window_create_tooltip(tooltip);
2089}
2090
2091#define TOOLTIP_TIMEOUT 500
2092static int
2093tooltip_timer_reset(struct tooltip *tooltip)
2094{
2095 struct itimerspec its;
2096
2097 its.it_interval.tv_sec = 0;
2098 its.it_interval.tv_nsec = 0;
2099 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2100 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2101 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2102 fprintf(stderr, "could not set timerfd\n: %m");
2103 return -1;
2104 }
2105
2106 return 0;
2107}
2108
2109int
2110widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2111{
2112 struct tooltip *tooltip = parent->tooltip;
2113
2114 parent->tooltip_count++;
2115 if (tooltip) {
2116 tooltip->x = x;
2117 tooltip->y = y;
2118 tooltip_timer_reset(tooltip);
2119 return 0;
2120 }
2121
2122 /* the handler might be triggered too fast via input device motion, so
2123 * we need this check here to make sure tooltip is fully initialized */
2124 if (parent->tooltip_count > 1)
2125 return 0;
2126
2127 tooltip = malloc(sizeof *tooltip);
2128 if (!tooltip)
2129 return -1;
2130
2131 parent->tooltip = tooltip;
2132 tooltip->parent = parent;
2133 tooltip->widget = NULL;
2134 tooltip->window = NULL;
2135 tooltip->x = x;
2136 tooltip->y = y;
2137 tooltip->entry = strdup(entry);
2138 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2139 if (tooltip->tooltip_fd < 0) {
2140 fprintf(stderr, "could not create timerfd\n: %m");
2141 return -1;
2142 }
2143
2144 tooltip->tooltip_task.run = tooltip_func;
2145 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2146 EPOLLIN, &tooltip->tooltip_task);
2147 tooltip_timer_reset(tooltip);
2148
2149 return 0;
2150}
2151
2152static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002153workspace_manager_state(void *data,
2154 struct workspace_manager *workspace_manager,
2155 uint32_t current,
2156 uint32_t count)
2157{
2158 struct display *display = data;
2159
2160 display->workspace = current;
2161 display->workspace_count = count;
2162}
2163
2164static const struct workspace_manager_listener workspace_manager_listener = {
2165 workspace_manager_state
2166};
2167
2168static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002169frame_resize_handler(struct widget *widget,
2170 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002171{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002172 struct frame *frame = data;
2173 struct widget *child = frame->child;
2174 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002175 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002176 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002177 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002178 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002179 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002180 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002181 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002182
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002183 switch (widget->window->type) {
2184 case TYPE_FULLSCREEN:
2185 decoration_width = 0;
2186 decoration_height = 0;
2187
2188 allocation.x = 0;
2189 allocation.y = 0;
2190 allocation.width = width;
2191 allocation.height = height;
2192 opaque_margin = 0;
2193
2194 wl_list_for_each(button, &frame->buttons_list, link)
2195 button->widget->opaque = 1;
2196 break;
2197 case TYPE_MAXIMIZED:
2198 decoration_width = t->width * 2;
2199 decoration_height = t->width + t->titlebar_height;
2200
2201 allocation.x = t->width;
2202 allocation.y = t->titlebar_height;
2203 allocation.width = width - decoration_width;
2204 allocation.height = height - decoration_height;
2205
2206 opaque_margin = 0;
2207
2208 wl_list_for_each(button, &frame->buttons_list, link)
2209 button->widget->opaque = 0;
2210 break;
2211 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002212 decoration_width = (t->width + t->margin) * 2;
2213 decoration_height = t->width +
2214 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002215
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002216 allocation.x = t->width + t->margin;
2217 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002218 allocation.width = width - decoration_width;
2219 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002220
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002221 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002222
2223 wl_list_for_each(button, &frame->buttons_list, link)
2224 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002225 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002226 }
2227
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002228 widget_set_allocation(child, allocation.x, allocation.y,
2229 allocation.width, allocation.height);
2230
2231 if (child->resize_handler)
2232 child->resize_handler(child,
2233 allocation.width,
2234 allocation.height,
2235 child->user_data);
2236
Scott Moreauf7e498c2012-05-14 11:39:29 -06002237 width = child->allocation.width + decoration_width;
2238 height = child->allocation.height + decoration_height;
2239
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002240 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2241
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002242 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002243 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002244 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002245 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002246 shadow_margin, shadow_margin,
2247 width - 2 * shadow_margin,
2248 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002249 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002250 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002251 }
2252
Scott Moreauf7e498c2012-05-14 11:39:29 -06002253 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002254
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002255 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002256 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002257 opaque_margin, opaque_margin,
2258 widget->allocation.width - 2 * opaque_margin,
2259 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002260
2261 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002262 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2263 x_l = t->width + shadow_margin;
2264 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002265 wl_list_for_each(button, &frame->buttons_list, link) {
2266 const int button_padding = 4;
2267 w = cairo_image_surface_get_width(button->icon);
2268 h = cairo_image_surface_get_height(button->icon);
2269
2270 if (button->decoration == FRAME_BUTTON_FANCY)
2271 w += 10;
2272
2273 if (button->align == FRAME_BUTTON_LEFT) {
2274 widget_set_allocation(button->widget,
2275 x_l, y , w + 1, h + 1);
2276 x_l += w;
2277 x_l += button_padding;
2278 } else {
2279 x_r -= w;
2280 widget_set_allocation(button->widget,
2281 x_r, y , w + 1, h + 1);
2282 x_r -= button_padding;
2283 }
2284 }
2285}
2286
2287static int
2288frame_button_enter_handler(struct widget *widget,
2289 struct input *input, float x, float y, void *data)
2290{
2291 struct frame_button *frame_button = data;
2292
2293 widget_schedule_redraw(frame_button->widget);
2294 frame_button->state = FRAME_BUTTON_OVER;
2295
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002296 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002297}
2298
2299static void
2300frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2301{
2302 struct frame_button *frame_button = data;
2303
2304 widget_schedule_redraw(frame_button->widget);
2305 frame_button->state = FRAME_BUTTON_DEFAULT;
2306}
2307
2308static void
2309frame_button_button_handler(struct widget *widget,
2310 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002311 uint32_t button,
2312 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002313{
2314 struct frame_button *frame_button = data;
2315 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002316 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002317
2318 if (button != BTN_LEFT)
2319 return;
2320
2321 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002322 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002323 frame_button->state = FRAME_BUTTON_ACTIVE;
2324 widget_schedule_redraw(frame_button->widget);
2325
2326 if (frame_button->type == FRAME_BUTTON_ICON)
2327 window_show_frame_menu(window, input, time);
2328 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002329 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002330 frame_button->state = FRAME_BUTTON_DEFAULT;
2331 widget_schedule_redraw(frame_button->widget);
2332 break;
2333 }
2334
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002335 if (!was_pressed)
2336 return;
2337
Martin Minarik1998b152012-05-10 02:04:35 +02002338 switch (frame_button->type) {
2339 case FRAME_BUTTON_CLOSE:
2340 if (window->close_handler)
2341 window->close_handler(window->parent,
2342 window->user_data);
2343 else
2344 display_exit(window->display);
2345 break;
2346 case FRAME_BUTTON_MINIMIZE:
2347 fprintf(stderr,"Minimize stub\n");
2348 break;
2349 case FRAME_BUTTON_MAXIMIZE:
2350 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2351 break;
2352 default:
2353 /* Unknown operation */
2354 break;
2355 }
2356}
2357
Rusty Lynch041815a2013-08-08 21:20:38 -07002358static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002359frame_button_touch_down_handler(struct widget *widget, struct input *input,
2360 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch041815a2013-08-08 21:20:38 -07002361 float x, float y, void *data)
2362{
2363 struct frame_button *frame_button = data;
2364 struct window *window = widget->window;
2365
2366 switch (frame_button->type) {
2367 case FRAME_BUTTON_CLOSE:
2368 if (window->close_handler)
2369 window->close_handler(window->parent,
2370 window->user_data);
2371 else
2372 display_exit(window->display);
2373 break;
2374 case FRAME_BUTTON_MINIMIZE:
2375 fprintf(stderr,"Minimize stub\n");
2376 break;
2377 case FRAME_BUTTON_MAXIMIZE:
2378 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2379 break;
2380 default:
2381 /* Unknown operation */
2382 break;
2383 }
2384}
2385
2386
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002387static int
2388frame_button_motion_handler(struct widget *widget,
2389 struct input *input, uint32_t time,
2390 float x, float y, void *data)
2391{
2392 struct frame_button *frame_button = data;
2393 enum frame_button_pointer previous_button_state = frame_button->state;
2394
2395 /* only track state for a pressed button */
2396 if (input->grab != widget)
2397 return CURSOR_LEFT_PTR;
2398
2399 if (x > widget->allocation.x &&
2400 x < (widget->allocation.x + widget->allocation.width) &&
2401 y > widget->allocation.y &&
2402 y < (widget->allocation.y + widget->allocation.height)) {
2403 frame_button->state = FRAME_BUTTON_ACTIVE;
2404 } else {
2405 frame_button->state = FRAME_BUTTON_DEFAULT;
2406 }
2407
2408 if (frame_button->state != previous_button_state)
2409 widget_schedule_redraw(frame_button->widget);
2410
2411 return CURSOR_LEFT_PTR;
2412}
2413
Martin Minarik1998b152012-05-10 02:04:35 +02002414static void
2415frame_button_redraw_handler(struct widget *widget, void *data)
2416{
2417 struct frame_button *frame_button = data;
2418 cairo_t *cr;
2419 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002420
2421 x = widget->allocation.x;
2422 y = widget->allocation.y;
2423 width = widget->allocation.width;
2424 height = widget->allocation.height;
2425
2426 if (!width)
2427 return;
2428 if (!height)
2429 return;
2430 if (widget->opaque)
2431 return;
2432
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002433 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002434
2435 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2436 cairo_set_line_width(cr, 1);
2437
2438 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2439 cairo_rectangle (cr, x, y, 25, 16);
2440
2441 cairo_stroke_preserve(cr);
2442
2443 switch (frame_button->state) {
2444 case FRAME_BUTTON_DEFAULT:
2445 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2446 break;
2447 case FRAME_BUTTON_OVER:
2448 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2449 break;
2450 case FRAME_BUTTON_ACTIVE:
2451 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2452 break;
2453 }
2454
2455 cairo_fill (cr);
2456
2457 x += 4;
2458 }
2459
2460 cairo_set_source_surface(cr, frame_button->icon, x, y);
2461 cairo_paint(cr);
2462
2463 cairo_destroy(cr);
2464}
2465
2466static struct widget *
2467frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2468 enum frame_button_align align, enum frame_button_decoration style)
2469{
2470 struct frame_button *frame_button;
2471 const char *icon = data;
2472
Peter Huttererf3d62272013-08-08 11:57:05 +10002473 frame_button = xzalloc (sizeof *frame_button);
Martin Minarik1998b152012-05-10 02:04:35 +02002474 frame_button->icon = cairo_image_surface_create_from_png(icon);
2475 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2476 frame_button->frame = frame;
2477 frame_button->type = type;
2478 frame_button->align = align;
2479 frame_button->decoration = style;
2480
2481 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2482
2483 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2484 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2485 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
Rusty Lynch041815a2013-08-08 21:20:38 -07002486 widget_set_touch_down_handler(frame_button->widget, frame_button_touch_down_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002487 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002488 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002489 return frame_button->widget;
2490}
2491
2492static void
2493frame_button_destroy(struct frame_button *frame_button)
2494{
2495 widget_destroy(frame_button->widget);
2496 wl_list_remove(&frame_button->link);
2497 cairo_surface_destroy(frame_button->icon);
2498 free(frame_button);
2499
2500 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002501}
2502
2503static void
2504frame_redraw_handler(struct widget *widget, void *data)
2505{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002506 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002507 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002508 struct theme *t = window->display->theme;
2509 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002510
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002511 if (window->type == TYPE_FULLSCREEN)
2512 return;
2513
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002514 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002515
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002516 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002517 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002518 if (window->type == TYPE_MAXIMIZED)
2519 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002520 theme_render_frame(t, cr, widget->allocation.width,
2521 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002522
2523 cairo_destroy(cr);
2524}
2525
2526static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002527frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002528{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002529 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002530 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002531 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002532
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002533 if (window->type != TYPE_TOPLEVEL)
2534 return CURSOR_LEFT_PTR;
2535
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002536 location = theme_get_location(t, input->sx, input->sy,
2537 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002538 frame->widget->allocation.height,
2539 window->type == TYPE_MAXIMIZED ?
2540 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002541
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002542 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002543 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002544 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002545 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002546 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002547 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002548 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002549 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002550 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002551 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002552 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002553 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002554 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002555 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002556 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002557 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002558 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002559 case THEME_LOCATION_EXTERIOR:
2560 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002561 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002562 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002563 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002564}
2565
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002566static void
2567frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002568{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002569 struct display *display;
2570
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002571 switch (index) {
2572 case 0: /* close */
2573 if (window->close_handler)
2574 window->close_handler(window->parent,
2575 window->user_data);
2576 else
2577 display_exit(window->display);
2578 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002579 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002580 display = window->display;
2581 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002582 workspace_manager_move_surface(
2583 display->workspace_manager,
2584 window->main_surface->surface,
2585 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002586 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002587 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002588 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002589 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002590 workspace_manager_move_surface(
2591 display->workspace_manager,
2592 window->main_surface->surface,
2593 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002594 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002595 case 3: /* fullscreen */
2596 /* we don't have a way to get out of fullscreen for now */
2597 if (window->fullscreen_handler)
2598 window->fullscreen_handler(window, window->user_data);
2599 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002600 }
2601}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002602
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002603void
2604window_show_frame_menu(struct window *window,
2605 struct input *input, uint32_t time)
2606{
2607 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002608 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002609
2610 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002611 "Close",
2612 "Move to workspace above", "Move to workspace below",
2613 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002614 };
2615
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002616 if (window->fullscreen_handler)
2617 count = ARRAY_LENGTH(entries);
2618 else
2619 count = ARRAY_LENGTH(entries) - 1;
2620
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002621 input_get_position(input, &x, &y);
2622 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002623 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002624}
2625
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002626static int
2627frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002628 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002629{
2630 return frame_get_pointer_image_for_location(data, input);
2631}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002632
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002633static int
2634frame_motion_handler(struct widget *widget,
2635 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002636 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002637{
2638 return frame_get_pointer_image_for_location(data, input);
2639}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002640
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002641static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002642frame_button_handler(struct widget *widget,
2643 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002644 uint32_t button, enum wl_pointer_button_state state,
2645 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002646
2647{
2648 struct frame *frame = data;
2649 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002650 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002651 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002652
Rob Bradford3c9d9672013-07-11 10:35:55 +01002653 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002654 return;
2655
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002656 location = theme_get_location(display->theme, input->sx, input->sy,
2657 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002658 frame->widget->allocation.height,
2659 window->type == TYPE_MAXIMIZED ?
2660 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002661
Daniel Stone4dbadb12012-05-30 16:31:51 +01002662 if (window->display->shell && button == BTN_LEFT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002663 window->type == TYPE_TOPLEVEL) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002664 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002665 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002666 if (!window->shell_surface)
2667 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002668 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002669 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002670 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002671 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002672 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002673 case THEME_LOCATION_RESIZING_TOP:
2674 case THEME_LOCATION_RESIZING_BOTTOM:
2675 case THEME_LOCATION_RESIZING_LEFT:
2676 case THEME_LOCATION_RESIZING_RIGHT:
2677 case THEME_LOCATION_RESIZING_TOP_LEFT:
2678 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2679 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2680 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002681 if (!window->shell_surface)
2682 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002683 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002684
Pekka Paalanen99436862012-11-19 17:15:59 +02002685 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002686 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002687 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002688 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002689 break;
2690 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002691 } else if (button == BTN_RIGHT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002692 (window->type == TYPE_TOPLEVEL ||
2693 window->type == TYPE_MAXIMIZED)) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002694 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002695 }
2696}
2697
Rusty Lynch1084da52013-08-15 09:10:08 -07002698static void
2699frame_touch_down_handler(struct widget *widget, struct input *input,
2700 uint32_t serial, uint32_t time, int32_t id,
2701 float x, float y, void *data)
2702{
2703 struct window *window = widget->window;
2704 struct display *display = window->display;
2705
2706 wl_shell_surface_move(window->shell_surface,
2707 input_get_seat(input),
2708 display->serial);
2709}
2710
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002711struct widget *
2712frame_create(struct window *window, void *data)
2713{
2714 struct frame *frame;
2715
Peter Huttererf3d62272013-08-08 11:57:05 +10002716 frame = xzalloc(sizeof *frame);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002717 frame->widget = window_add_widget(window, frame);
2718 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002719
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002720 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2721 widget_set_resize_handler(frame->widget, frame_resize_handler);
2722 widget_set_enter_handler(frame->widget, frame_enter_handler);
2723 widget_set_motion_handler(frame->widget, frame_motion_handler);
2724 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002725 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002726
Martin Minarik1998b152012-05-10 02:04:35 +02002727 /* Create empty list for frame buttons */
2728 wl_list_init(&frame->buttons_list);
2729
2730 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2731 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2732
2733 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2734 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2735
2736 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2737 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2738
2739 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2740 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2741
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002742 window->frame = frame;
2743
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002744 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002745}
2746
Kristian Høgsberga1627922012-06-20 17:30:03 -04002747void
2748frame_set_child_size(struct widget *widget, int child_width, int child_height)
2749{
2750 struct display *display = widget->window->display;
2751 struct theme *t = display->theme;
2752 int decoration_width, decoration_height;
2753 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002754 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002755
2756 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002757 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002758 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002759 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002760
2761 width = child_width + decoration_width;
2762 height = child_height + decoration_height;
2763 } else {
2764 width = child_width;
2765 height = child_height;
2766 }
2767
2768 window_schedule_resize(widget->window, width, height);
2769}
2770
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002771static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002772frame_destroy(struct frame *frame)
2773{
Martin Minarik1998b152012-05-10 02:04:35 +02002774 struct frame_button *button, *tmp;
2775
2776 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2777 frame_button_destroy(button);
2778
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002779 /* frame->child must be destroyed by the application */
2780 widget_destroy(frame->widget);
2781 free(frame);
2782}
2783
2784static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002785input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002786 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002787{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002788 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002789 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002790
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002791 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002792 return;
2793
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002794 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002795 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002796 widget = old;
2797 if (input->grab)
2798 widget = input->grab;
2799 if (widget->leave_handler)
2800 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002801 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002802 }
2803
2804 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002805 widget = focus;
2806 if (input->grab)
2807 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002808 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002809 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002810 cursor = widget->enter_handler(focus, input, x, y,
2811 widget->user_data);
2812 else
2813 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002814
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002815 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002816 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002817}
2818
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002819void
2820input_grab(struct input *input, struct widget *widget, uint32_t button)
2821{
2822 input->grab = widget;
2823 input->grab_button = button;
2824}
2825
2826void
2827input_ungrab(struct input *input)
2828{
2829 struct widget *widget;
2830
2831 input->grab = NULL;
2832 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002833 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002834 input->sx, input->sy);
2835 input_set_focus_widget(input, widget, input->sx, input->sy);
2836 }
2837}
2838
2839static void
2840input_remove_pointer_focus(struct input *input)
2841{
2842 struct window *window = input->pointer_focus;
2843
2844 if (!window)
2845 return;
2846
2847 input_set_focus_widget(input, NULL, 0, 0);
2848
2849 input->pointer_focus = NULL;
2850 input->current_cursor = CURSOR_UNSET;
2851}
2852
2853static void
2854pointer_handle_enter(void *data, struct wl_pointer *pointer,
2855 uint32_t serial, struct wl_surface *surface,
2856 wl_fixed_t sx_w, wl_fixed_t sy_w)
2857{
2858 struct input *input = data;
2859 struct window *window;
2860 struct widget *widget;
2861 float sx = wl_fixed_to_double(sx_w);
2862 float sy = wl_fixed_to_double(sy_w);
2863
2864 if (!surface) {
2865 /* enter event for a window we've just destroyed */
2866 return;
2867 }
2868
2869 input->display->serial = serial;
2870 input->pointer_enter_serial = serial;
2871 input->pointer_focus = wl_surface_get_user_data(surface);
2872 window = input->pointer_focus;
2873
Pekka Paalanen99436862012-11-19 17:15:59 +02002874 if (window->resizing) {
2875 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002876 /* Schedule a redraw to free the pool */
2877 window_schedule_redraw(window);
2878 }
2879
2880 input->sx = sx;
2881 input->sy = sy;
2882
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002883 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002884 input_set_focus_widget(input, widget, sx, sy);
2885}
2886
2887static void
2888pointer_handle_leave(void *data, struct wl_pointer *pointer,
2889 uint32_t serial, struct wl_surface *surface)
2890{
2891 struct input *input = data;
2892
2893 input->display->serial = serial;
2894 input_remove_pointer_focus(input);
2895}
2896
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002897static void
Daniel Stone37816df2012-05-16 18:45:18 +01002898pointer_handle_motion(void *data, struct wl_pointer *pointer,
2899 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002900{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002901 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002902 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002903 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002904 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002905 float sx = wl_fixed_to_double(sx_w);
2906 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002907
Paul Winwoodb22bf572013-08-29 10:52:54 +01002908 input->sx = sx;
2909 input->sy = sy;
2910
2911 if (!window)
2912 return;
2913
Rob Bradford5f087742013-07-11 19:41:27 +01002914 /* when making the window smaller - e.g. after a unmaximise we might
2915 * still have a pending motion event that the compositor has picked
2916 * based on the old surface dimensions
2917 */
2918 if (sx > window->main_surface->allocation.width ||
2919 sy > window->main_surface->allocation.height)
2920 return;
2921
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002922 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002923 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002924 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002925 }
2926
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002927 if (input->grab)
2928 widget = input->grab;
2929 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002930 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002931 if (widget) {
2932 if (widget->motion_handler)
2933 cursor = widget->motion_handler(input->focus_widget,
2934 input, time, sx, sy,
2935 widget->user_data);
2936 else
2937 cursor = widget->default_cursor;
2938 } else
2939 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002940
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002941 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002942}
2943
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002944static void
Daniel Stone37816df2012-05-16 18:45:18 +01002945pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002946 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002947{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002948 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002949 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002950 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002951
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002952 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002953 if (input->focus_widget && input->grab == NULL &&
2954 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002955 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002956
Neil Roberts6b28aad2012-01-23 19:11:18 +00002957 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002958 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002959 (*widget->button_handler)(widget,
2960 input, time,
2961 button, state,
2962 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002963
Daniel Stone4dbadb12012-05-30 16:31:51 +01002964 if (input->grab && input->grab_button == button &&
2965 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002966 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002967}
2968
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002969static void
Daniel Stone37816df2012-05-16 18:45:18 +01002970pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002971 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002972{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002973 struct input *input = data;
2974 struct widget *widget;
2975
2976 widget = input->focus_widget;
2977 if (input->grab)
2978 widget = input->grab;
2979 if (widget && widget->axis_handler)
2980 (*widget->axis_handler)(widget,
2981 input, time,
2982 axis, value,
2983 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002984}
2985
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002986static const struct wl_pointer_listener pointer_listener = {
2987 pointer_handle_enter,
2988 pointer_handle_leave,
2989 pointer_handle_motion,
2990 pointer_handle_button,
2991 pointer_handle_axis,
2992};
2993
2994static void
2995input_remove_keyboard_focus(struct input *input)
2996{
2997 struct window *window = input->keyboard_focus;
2998 struct itimerspec its;
2999
3000 its.it_interval.tv_sec = 0;
3001 its.it_interval.tv_nsec = 0;
3002 its.it_value.tv_sec = 0;
3003 its.it_value.tv_nsec = 0;
3004 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3005
3006 if (!window)
3007 return;
3008
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003009 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003010 if (window->keyboard_focus_handler)
3011 (*window->keyboard_focus_handler)(window, NULL,
3012 window->user_data);
3013
3014 input->keyboard_focus = NULL;
3015}
3016
3017static void
3018keyboard_repeat_func(struct task *task, uint32_t events)
3019{
3020 struct input *input =
3021 container_of(task, struct input, repeat_task);
3022 struct window *window = input->keyboard_focus;
3023 uint64_t exp;
3024
3025 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
3026 /* If we change the timer between the fd becoming
3027 * readable and getting here, there'll be nothing to
3028 * read and we get EAGAIN. */
3029 return;
3030
3031 if (window && window->key_handler) {
3032 (*window->key_handler)(window, input, input->repeat_time,
3033 input->repeat_key, input->repeat_sym,
3034 WL_KEYBOARD_KEY_STATE_PRESSED,
3035 window->user_data);
3036 }
3037}
3038
3039static void
3040keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
3041 uint32_t format, int fd, uint32_t size)
3042{
3043 struct input *input = data;
3044 char *map_str;
3045
3046 if (!data) {
3047 close(fd);
3048 return;
3049 }
3050
3051 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3052 close(fd);
3053 return;
3054 }
3055
3056 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3057 if (map_str == MAP_FAILED) {
3058 close(fd);
3059 return;
3060 }
3061
3062 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
3063 map_str,
3064 XKB_KEYMAP_FORMAT_TEXT_V1,
3065 0);
3066 munmap(map_str, size);
3067 close(fd);
3068
3069 if (!input->xkb.keymap) {
3070 fprintf(stderr, "failed to compile keymap\n");
3071 return;
3072 }
3073
3074 input->xkb.state = xkb_state_new(input->xkb.keymap);
3075 if (!input->xkb.state) {
3076 fprintf(stderr, "failed to create XKB state\n");
3077 xkb_map_unref(input->xkb.keymap);
3078 input->xkb.keymap = NULL;
3079 return;
3080 }
3081
3082 input->xkb.control_mask =
3083 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
3084 input->xkb.alt_mask =
3085 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
3086 input->xkb.shift_mask =
3087 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
3088}
3089
3090static void
3091keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3092 uint32_t serial, struct wl_surface *surface,
3093 struct wl_array *keys)
3094{
3095 struct input *input = data;
3096 struct window *window;
3097
3098 input->display->serial = serial;
3099 input->keyboard_focus = wl_surface_get_user_data(surface);
3100
3101 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003102 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003103 if (window->keyboard_focus_handler)
3104 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003105 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003106}
3107
3108static void
3109keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3110 uint32_t serial, struct wl_surface *surface)
3111{
3112 struct input *input = data;
3113
3114 input->display->serial = serial;
3115 input_remove_keyboard_focus(input);
3116}
3117
Scott Moreau210d0792012-03-22 10:47:01 -06003118static void
Daniel Stone37816df2012-05-16 18:45:18 +01003119keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003120 uint32_t serial, uint32_t time, uint32_t key,
3121 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003122{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003123 struct input *input = data;
3124 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003125 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003126 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003127 const xkb_keysym_t *syms;
3128 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003129 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003130
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003131 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003132 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003133 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003134 return;
3135
Daniel Stone97f68542012-05-30 16:32:01 +01003136 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003137
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003138 sym = XKB_KEY_NoSymbol;
3139 if (num_syms == 1)
3140 sym = syms[0];
3141
3142 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003143 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003144 window_set_maximized(window,
3145 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003146 } else if (sym == XKB_KEY_F11 &&
3147 window->fullscreen_handler &&
3148 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3149 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003150 } else if (sym == XKB_KEY_F4 &&
3151 input->modifiers == MOD_ALT_MASK &&
3152 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3153 if (window->close_handler)
3154 window->close_handler(window->parent,
3155 window->user_data);
3156 else
3157 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003158 } else if (window->key_handler) {
3159 (*window->key_handler)(window, input, time, key,
3160 sym, state, window->user_data);
3161 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003162
3163 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3164 key == input->repeat_key) {
3165 its.it_interval.tv_sec = 0;
3166 its.it_interval.tv_nsec = 0;
3167 its.it_value.tv_sec = 0;
3168 its.it_value.tv_nsec = 0;
3169 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3170 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3171 input->repeat_sym = sym;
3172 input->repeat_key = key;
3173 input->repeat_time = time;
3174 its.it_interval.tv_sec = 0;
3175 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3176 its.it_value.tv_sec = 0;
3177 its.it_value.tv_nsec = 400 * 1000 * 1000;
3178 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3179 }
3180}
3181
3182static void
Daniel Stone351eb612012-05-31 15:27:47 -04003183keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3184 uint32_t serial, uint32_t mods_depressed,
3185 uint32_t mods_latched, uint32_t mods_locked,
3186 uint32_t group)
3187{
3188 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003189 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003190
Matt Ropere61561f2013-06-24 16:52:43 +01003191 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3192 if (!input->xkb.keymap)
3193 return;
3194
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003195 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3196 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003197 mask = xkb_state_serialize_mods(input->xkb.state,
3198 XKB_STATE_DEPRESSED |
3199 XKB_STATE_LATCHED);
3200 input->modifiers = 0;
3201 if (mask & input->xkb.control_mask)
3202 input->modifiers |= MOD_CONTROL_MASK;
3203 if (mask & input->xkb.alt_mask)
3204 input->modifiers |= MOD_ALT_MASK;
3205 if (mask & input->xkb.shift_mask)
3206 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003207}
3208
Daniel Stone37816df2012-05-16 18:45:18 +01003209static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003210 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003211 keyboard_handle_enter,
3212 keyboard_handle_leave,
3213 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003214 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003215};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003216
3217static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003218touch_handle_down(void *data, struct wl_touch *wl_touch,
3219 uint32_t serial, uint32_t time, struct wl_surface *surface,
3220 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3221{
3222 struct input *input = data;
3223 struct widget *widget;
3224 float sx = wl_fixed_to_double(x_w);
3225 float sy = wl_fixed_to_double(y_w);
3226
Rusty Lynch1084da52013-08-15 09:10:08 -07003227 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003228 input->touch_focus = wl_surface_get_user_data(surface);
3229 if (!input->touch_focus) {
3230 DBG("Failed to find to touch focus for surface %p\n", surface);
3231 return;
3232 }
3233
3234 widget = window_find_widget(input->touch_focus,
3235 wl_fixed_to_double(x_w),
3236 wl_fixed_to_double(y_w));
3237 if (widget) {
3238 struct touch_point *tp = xmalloc(sizeof *tp);
3239 if (tp) {
3240 tp->id = id;
3241 tp->widget = widget;
3242 wl_list_insert(&input->touch_point_list, &tp->link);
3243
3244 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003245 (*widget->touch_down_handler)(widget, input,
3246 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003247 sx, sy,
3248 widget->user_data);
3249 }
3250 }
3251}
3252
3253static void
3254touch_handle_up(void *data, struct wl_touch *wl_touch,
3255 uint32_t serial, uint32_t time, int32_t id)
3256{
3257 struct input *input = data;
3258 struct touch_point *tp, *tmp;
3259
Rusty Lynch041815a2013-08-08 21:20:38 -07003260 if (!input->touch_focus) {
3261 DBG("No touch focus found for touch up event!\n");
3262 return;
3263 }
3264
3265 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3266 if (tp->id != id)
3267 continue;
3268
3269 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003270 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003271 time, id,
3272 tp->widget->user_data);
3273
3274 wl_list_remove(&tp->link);
3275 free(tp);
3276
3277 return;
3278 }
3279}
3280
3281static void
3282touch_handle_motion(void *data, struct wl_touch *wl_touch,
3283 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3284{
3285 struct input *input = data;
3286 struct touch_point *tp;
3287 float sx = wl_fixed_to_double(x_w);
3288 float sy = wl_fixed_to_double(y_w);
3289
3290 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3291
3292 if (!input->touch_focus) {
3293 DBG("No touch focus found for touch motion event!\n");
3294 return;
3295 }
3296
3297 wl_list_for_each(tp, &input->touch_point_list, link) {
3298 if (tp->id != id)
3299 continue;
3300
3301 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003302 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003303 id, sx, sy,
3304 tp->widget->user_data);
3305 return;
3306 }
3307}
3308
3309static void
3310touch_handle_frame(void *data, struct wl_touch *wl_touch)
3311{
3312 struct input *input = data;
3313 struct touch_point *tp, *tmp;
3314
3315 DBG("touch_handle_frame\n");
3316
3317 if (!input->touch_focus) {
3318 DBG("No touch focus found for touch frame event!\n");
3319 return;
3320 }
3321
3322 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3323 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003324 (*tp->widget->touch_frame_handler)(tp->widget, input,
3325 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003326
3327 wl_list_remove(&tp->link);
3328 free(tp);
3329 }
3330}
3331
3332static void
3333touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3334{
3335 struct input *input = data;
3336 struct touch_point *tp, *tmp;
3337
3338 DBG("touch_handle_cancel\n");
3339
3340 if (!input->touch_focus) {
3341 DBG("No touch focus found for touch cancel event!\n");
3342 return;
3343 }
3344
3345 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3346 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003347 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3348 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003349
3350 wl_list_remove(&tp->link);
3351 free(tp);
3352 }
3353}
3354
3355static const struct wl_touch_listener touch_listener = {
3356 touch_handle_down,
3357 touch_handle_up,
3358 touch_handle_motion,
3359 touch_handle_frame,
3360 touch_handle_cancel,
3361};
3362
3363static void
Daniel Stone37816df2012-05-16 18:45:18 +01003364seat_handle_capabilities(void *data, struct wl_seat *seat,
3365 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003366{
Daniel Stone37816df2012-05-16 18:45:18 +01003367 struct input *input = data;
3368
3369 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3370 input->pointer = wl_seat_get_pointer(seat);
3371 wl_pointer_set_user_data(input->pointer, input);
3372 wl_pointer_add_listener(input->pointer, &pointer_listener,
3373 input);
3374 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3375 wl_pointer_destroy(input->pointer);
3376 input->pointer = NULL;
3377 }
3378
3379 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3380 input->keyboard = wl_seat_get_keyboard(seat);
3381 wl_keyboard_set_user_data(input->keyboard, input);
3382 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3383 input);
3384 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3385 wl_keyboard_destroy(input->keyboard);
3386 input->keyboard = NULL;
3387 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003388
3389 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3390 input->touch = wl_seat_get_touch(seat);
3391 wl_touch_set_user_data(input->touch, input);
3392 wl_touch_add_listener(input->touch, &touch_listener, input);
3393 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3394 wl_touch_destroy(input->touch);
3395 input->touch = NULL;
3396 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003397}
3398
Rob Bradford08031182013-08-13 20:11:03 +01003399static void
3400seat_handle_name(void *data, struct wl_seat *seat,
3401 const char *name)
3402{
3403
3404}
3405
Daniel Stone37816df2012-05-16 18:45:18 +01003406static const struct wl_seat_listener seat_listener = {
3407 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003408 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003409};
3410
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003411void
3412input_get_position(struct input *input, int32_t *x, int32_t *y)
3413{
3414 *x = input->sx;
3415 *y = input->sy;
3416}
3417
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003418struct display *
3419input_get_display(struct input *input)
3420{
3421 return input->display;
3422}
3423
Daniel Stone37816df2012-05-16 18:45:18 +01003424struct wl_seat *
3425input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003426{
Daniel Stone37816df2012-05-16 18:45:18 +01003427 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003428}
3429
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003430uint32_t
3431input_get_modifiers(struct input *input)
3432{
3433 return input->modifiers;
3434}
3435
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003436struct widget *
3437input_get_focus_widget(struct input *input)
3438{
3439 return input->focus_widget;
3440}
3441
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003442struct data_offer {
3443 struct wl_data_offer *offer;
3444 struct input *input;
3445 struct wl_array types;
3446 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003447
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003448 struct task io_task;
3449 int fd;
3450 data_func_t func;
3451 int32_t x, y;
3452 void *user_data;
3453};
3454
3455static void
3456data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3457{
3458 struct data_offer *offer = data;
3459 char **p;
3460
3461 p = wl_array_add(&offer->types, sizeof *p);
3462 *p = strdup(type);
3463}
3464
3465static const struct wl_data_offer_listener data_offer_listener = {
3466 data_offer_offer,
3467};
3468
3469static void
3470data_offer_destroy(struct data_offer *offer)
3471{
3472 char **p;
3473
3474 offer->refcount--;
3475 if (offer->refcount == 0) {
3476 wl_data_offer_destroy(offer->offer);
3477 for (p = offer->types.data; *p; p++)
3478 free(*p);
3479 wl_array_release(&offer->types);
3480 free(offer);
3481 }
3482}
3483
3484static void
3485data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003486 struct wl_data_device *data_device,
3487 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003488{
3489 struct data_offer *offer;
3490
Brian Lovinbc919262013-08-07 15:34:59 -07003491 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003492
3493 wl_array_init(&offer->types);
3494 offer->refcount = 1;
3495 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003496 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003497 wl_data_offer_add_listener(offer->offer,
3498 &data_offer_listener, offer);
3499}
3500
3501static void
3502data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003503 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003504 wl_fixed_t x_w, wl_fixed_t y_w,
3505 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003506{
3507 struct input *input = data;
3508 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003509 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003510 float x = wl_fixed_to_double(x_w);
3511 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003512 char **p;
3513
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003514 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003515 window = wl_surface_get_user_data(surface);
3516 input->pointer_focus = window;
3517
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003518 if (offer) {
3519 input->drag_offer = wl_data_offer_get_user_data(offer);
3520
3521 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3522 *p = NULL;
3523
3524 types_data = input->drag_offer->types.data;
3525 } else {
3526 input->drag_offer = NULL;
3527 types_data = NULL;
3528 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003529
3530 window = input->pointer_focus;
3531 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003532 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003533 window->user_data);
3534}
3535
3536static void
3537data_device_leave(void *data, struct wl_data_device *data_device)
3538{
3539 struct input *input = data;
3540
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003541 if (input->drag_offer) {
3542 data_offer_destroy(input->drag_offer);
3543 input->drag_offer = NULL;
3544 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003545}
3546
3547static void
3548data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003549 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003550{
3551 struct input *input = data;
3552 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003553 float x = wl_fixed_to_double(x_w);
3554 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003555 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003556
3557 input->sx = x;
3558 input->sy = y;
3559
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003560 if (input->drag_offer)
3561 types_data = input->drag_offer->types.data;
3562 else
3563 types_data = NULL;
3564
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003565 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003566 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003567 window->user_data);
3568}
3569
3570static void
3571data_device_drop(void *data, struct wl_data_device *data_device)
3572{
3573 struct input *input = data;
3574 struct window *window = input->pointer_focus;
3575
3576 if (window->drop_handler)
3577 window->drop_handler(window, input,
3578 input->sx, input->sy, window->user_data);
3579}
3580
3581static void
3582data_device_selection(void *data,
3583 struct wl_data_device *wl_data_device,
3584 struct wl_data_offer *offer)
3585{
3586 struct input *input = data;
3587 char **p;
3588
3589 if (input->selection_offer)
3590 data_offer_destroy(input->selection_offer);
3591
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003592 if (offer) {
3593 input->selection_offer = wl_data_offer_get_user_data(offer);
3594 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3595 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003596 } else {
3597 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003598 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003599}
3600
3601static const struct wl_data_device_listener data_device_listener = {
3602 data_device_data_offer,
3603 data_device_enter,
3604 data_device_leave,
3605 data_device_motion,
3606 data_device_drop,
3607 data_device_selection
3608};
3609
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003610static void
3611input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003612{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003613 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003614 struct wl_cursor *cursor;
3615 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003616
Daniel Stone80972742012-11-07 17:51:39 +11003617 if (!input->pointer)
3618 return;
3619
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003620 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003621 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003622 return;
3623
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003624 if (index >= (int) cursor->image_count) {
3625 fprintf(stderr, "cursor index out of range\n");
3626 return;
3627 }
3628
3629 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003630 buffer = wl_cursor_image_get_buffer(image);
3631 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003632 return;
3633
Kristian Høgsbergae277372012-08-01 09:41:08 -04003634 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003635 input->pointer_surface,
3636 image->hotspot_x, image->hotspot_y);
3637 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3638 wl_surface_damage(input->pointer_surface, 0, 0,
3639 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003640 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003641}
3642
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003643static const struct wl_callback_listener pointer_surface_listener;
3644
3645static void
3646pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3647 uint32_t time)
3648{
3649 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003650 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003651 int i;
3652
3653 if (callback) {
3654 assert(callback == input->cursor_frame_cb);
3655 wl_callback_destroy(callback);
3656 input->cursor_frame_cb = NULL;
3657 }
3658
Daniel Stone80972742012-11-07 17:51:39 +11003659 if (!input->pointer)
3660 return;
3661
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003662 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003663 wl_pointer_set_cursor(input->pointer,
3664 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003665 NULL, 0, 0);
3666 return;
3667 }
3668
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003669 if (input->current_cursor == CURSOR_UNSET)
3670 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003671 cursor = input->display->cursors[input->current_cursor];
3672 if (!cursor)
3673 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003674
3675 /* FIXME We don't have the current time on the first call so we set
3676 * the animation start to the time of the first frame callback. */
3677 if (time == 0)
3678 input->cursor_anim_start = 0;
3679 else if (input->cursor_anim_start == 0)
3680 input->cursor_anim_start = time;
3681
3682 if (time == 0 || input->cursor_anim_start == 0)
3683 i = 0;
3684 else
3685 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3686
Pekka Paalanenbc106382012-10-10 12:49:31 +03003687 if (cursor->image_count > 1) {
3688 input->cursor_frame_cb =
3689 wl_surface_frame(input->pointer_surface);
3690 wl_callback_add_listener(input->cursor_frame_cb,
3691 &pointer_surface_listener, input);
3692 }
3693
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003694 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003695}
3696
3697static const struct wl_callback_listener pointer_surface_listener = {
3698 pointer_surface_frame_callback
3699};
3700
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003701void
3702input_set_pointer_image(struct input *input, int pointer)
3703{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003704 int force = 0;
3705
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003706 if (!input->pointer)
3707 return;
3708
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003709 if (input->pointer_enter_serial > input->cursor_serial)
3710 force = 1;
3711
3712 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003713 return;
3714
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003715 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003716 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003717 if (!input->cursor_frame_cb)
3718 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003719 else if (force) {
3720 /* The current frame callback may be stuck if, for instance,
3721 * the set cursor request was processed by the server after
3722 * this client lost the focus. In this case the cursor surface
3723 * might not be mapped and the frame callback wouldn't ever
3724 * complete. Send a set_cursor and attach to try to map the
3725 * cursor surface again so that the callback will finish */
3726 input_set_pointer_image_index(input, 0);
3727 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003728}
3729
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003730struct wl_data_device *
3731input_get_data_device(struct input *input)
3732{
3733 return input->data_device;
3734}
3735
3736void
3737input_set_selection(struct input *input,
3738 struct wl_data_source *source, uint32_t time)
3739{
3740 wl_data_device_set_selection(input->data_device, source, time);
3741}
3742
3743void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003744input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003745{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003746 wl_data_offer_accept(input->drag_offer->offer,
3747 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003748}
3749
3750static void
3751offer_io_func(struct task *task, uint32_t events)
3752{
3753 struct data_offer *offer =
3754 container_of(task, struct data_offer, io_task);
3755 unsigned int len;
3756 char buffer[4096];
3757
3758 len = read(offer->fd, buffer, sizeof buffer);
3759 offer->func(buffer, len,
3760 offer->x, offer->y, offer->user_data);
3761
3762 if (len == 0) {
3763 close(offer->fd);
3764 data_offer_destroy(offer);
3765 }
3766}
3767
3768static void
3769data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3770 data_func_t func, void *user_data)
3771{
3772 int p[2];
3773
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003774 if (pipe2(p, O_CLOEXEC) == -1)
3775 return;
3776
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003777 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3778 close(p[1]);
3779
3780 offer->io_task.run = offer_io_func;
3781 offer->fd = p[0];
3782 offer->func = func;
3783 offer->refcount++;
3784 offer->user_data = user_data;
3785
3786 display_watch_fd(offer->input->display,
3787 offer->fd, EPOLLIN, &offer->io_task);
3788}
3789
3790void
3791input_receive_drag_data(struct input *input, const char *mime_type,
3792 data_func_t func, void *data)
3793{
3794 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3795 input->drag_offer->x = input->sx;
3796 input->drag_offer->y = input->sy;
3797}
3798
3799int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003800input_receive_drag_data_to_fd(struct input *input,
3801 const char *mime_type, int fd)
3802{
3803 if (input->drag_offer)
3804 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3805
3806 return 0;
3807}
3808
3809int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003810input_receive_selection_data(struct input *input, const char *mime_type,
3811 data_func_t func, void *data)
3812{
3813 char **p;
3814
3815 if (input->selection_offer == NULL)
3816 return -1;
3817
3818 for (p = input->selection_offer->types.data; *p; p++)
3819 if (strcmp(mime_type, *p) == 0)
3820 break;
3821
3822 if (*p == NULL)
3823 return -1;
3824
3825 data_offer_receive_data(input->selection_offer,
3826 mime_type, func, data);
3827 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003828}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003829
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003830int
3831input_receive_selection_data_to_fd(struct input *input,
3832 const char *mime_type, int fd)
3833{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003834 if (input->selection_offer)
3835 wl_data_offer_receive(input->selection_offer->offer,
3836 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003837
3838 return 0;
3839}
3840
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003841void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003842window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003843{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003844 if (!window->shell_surface)
3845 return;
3846
Daniel Stone37816df2012-05-16 18:45:18 +01003847 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003848}
3849
Rusty Lynch1084da52013-08-15 09:10:08 -07003850void
3851window_touch_move(struct window *window, struct input *input, uint32_t serial)
3852{
3853 if (!window->shell_surface)
3854 return;
3855
3856 wl_shell_surface_move(window->shell_surface, input->seat,
3857 window->display->serial);
3858}
3859
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003860static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003861surface_set_synchronized(struct surface *surface)
3862{
3863 if (!surface->subsurface)
3864 return;
3865
3866 if (surface->synchronized)
3867 return;
3868
3869 wl_subsurface_set_sync(surface->subsurface);
3870 surface->synchronized = 1;
3871}
3872
3873static void
3874surface_set_synchronized_default(struct surface *surface)
3875{
3876 if (!surface->subsurface)
3877 return;
3878
3879 if (surface->synchronized == surface->synchronized_default)
3880 return;
3881
3882 if (surface->synchronized_default)
3883 wl_subsurface_set_sync(surface->subsurface);
3884 else
3885 wl_subsurface_set_desync(surface->subsurface);
3886
3887 surface->synchronized = surface->synchronized_default;
3888}
3889
3890static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003891surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003892{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003893 struct widget *widget = surface->widget;
3894 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003895
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003896 if (surface->input_region) {
3897 wl_region_destroy(surface->input_region);
3898 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003899 }
3900
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003901 if (surface->opaque_region)
3902 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003903
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003904 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003905
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003906 if (widget->resize_handler)
3907 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003908 widget->allocation.width,
3909 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003910 widget->user_data);
3911
Pekka Paalanen35e82632013-04-25 13:57:48 +03003912 if (surface->subsurface &&
3913 (surface->allocation.x != widget->allocation.x ||
3914 surface->allocation.y != widget->allocation.y)) {
3915 wl_subsurface_set_position(surface->subsurface,
3916 widget->allocation.x,
3917 widget->allocation.y);
3918 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003919 if (surface->allocation.width != widget->allocation.width ||
3920 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003921 window_schedule_redraw(widget->window);
3922 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003923 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003924
3925 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003926 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003927 widget->allocation.width,
3928 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003929}
3930
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003931static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003932hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3933{
3934 /*
3935 * This hack should be removed, when EGL respects
3936 * eglSwapInterval(0).
3937 *
3938 * If this window has sub-surfaces, especially a free-running
3939 * EGL-widget, we need to post the parent surface once with
3940 * all the old state to guarantee, that the EGL-widget will
3941 * receive its frame callback soon. Otherwise, a forced call
3942 * to eglSwapBuffers may end up blocking, waiting for a frame
3943 * event that will never come, because we will commit the parent
3944 * surface with all new state only after eglSwapBuffers returns.
3945 *
3946 * This assumes, that:
3947 * 1. When the EGL widget's resize hook is called, it pauses.
3948 * 2. When the EGL widget's redraw hook is called, it forces a
3949 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3950 * In a single threaded application condition 1 is a no-op.
3951 *
3952 * XXX: This should actually be after the surface_resize() calls,
3953 * but cannot, because then it would commit the incomplete state
3954 * accumulated from the widget resize hooks.
3955 */
3956 if (window->subsurface_list.next != &window->main_surface->link ||
3957 window->subsurface_list.prev != &window->main_surface->link)
3958 wl_surface_commit(window->main_surface->surface);
3959}
3960
3961static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003962idle_resize(struct window *window)
3963{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003964 struct surface *surface;
3965
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003966 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003967 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003968
Pekka Paalanen71233882013-04-25 13:57:53 +03003969 DBG("from %dx%d to %dx%d\n",
3970 window->main_surface->server_allocation.width,
3971 window->main_surface->server_allocation.height,
3972 window->pending_allocation.width,
3973 window->pending_allocation.height);
3974
Pekka Paalanene9297f82013-04-25 13:57:51 +03003975 hack_prevent_EGL_sub_surface_deadlock(window);
3976
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003977 widget_set_allocation(window->main_surface->widget,
3978 window->pending_allocation.x,
3979 window->pending_allocation.y,
3980 window->pending_allocation.width,
3981 window->pending_allocation.height);
3982
3983 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003984
3985 /* The main surface is in the list, too. Main surface's
3986 * resize_handler is responsible for calling widget_set_allocation()
3987 * on all sub-surface root widgets, so they will be resized
3988 * properly.
3989 */
3990 wl_list_for_each(surface, &window->subsurface_list, link) {
3991 if (surface == window->main_surface)
3992 continue;
3993
3994 surface_set_synchronized(surface);
3995 surface_resize(surface);
3996 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003997}
3998
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003999void
4000window_schedule_resize(struct window *window, int width, int height)
4001{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004002 window->pending_allocation.x = 0;
4003 window->pending_allocation.y = 0;
4004 window->pending_allocation.width = width;
4005 window->pending_allocation.height = height;
4006
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004007 if (window->min_allocation.width == 0)
4008 window->min_allocation = window->pending_allocation;
4009 if (window->pending_allocation.width < window->min_allocation.width)
4010 window->pending_allocation.width = window->min_allocation.width;
4011 if (window->pending_allocation.height < window->min_allocation.height)
4012 window->pending_allocation.height = window->min_allocation.height;
4013
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004014 window->resize_needed = 1;
4015 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004016}
4017
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004018void
4019widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4020{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004021 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004022}
4023
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004024static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06004025handle_ping(void *data, struct wl_shell_surface *shell_surface,
4026 uint32_t serial)
4027{
4028 wl_shell_surface_pong(shell_surface, serial);
4029}
4030
4031static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004032handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004033 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004034{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004035 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004036
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01004037 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004038 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004039}
4040
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004041static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004042menu_destroy(struct menu *menu)
4043{
4044 widget_destroy(menu->widget);
4045 window_destroy(menu->window);
4046 free(menu);
4047}
4048
4049static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004050handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
4051{
4052 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02004053 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004054
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004055 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004056 * device. Or just use wl_callback. And this really needs to
4057 * be a window vfunc that the menu can set. And we need the
4058 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004059
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004060 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004061 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004062}
4063
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004064static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06004065 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004066 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004067 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004068};
4069
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004070void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004071window_get_allocation(struct window *window,
4072 struct rectangle *allocation)
4073{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004074 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004075}
4076
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004077static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004078widget_redraw(struct widget *widget)
4079{
4080 struct widget *child;
4081
4082 if (widget->redraw_handler)
4083 widget->redraw_handler(widget, widget->user_data);
4084 wl_list_for_each(child, &widget->child_list, link)
4085 widget_redraw(child);
4086}
4087
4088static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004089frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4090{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004091 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004092
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004093 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004094 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004095 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004096 surface->frame_cb = NULL;
4097
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004098 surface->last_time = time;
4099
Pekka Paalanen71233882013-04-25 13:57:53 +03004100 if (surface->redraw_needed || surface->window->redraw_needed) {
4101 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004102 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004103 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004104}
4105
4106static const struct wl_callback_listener listener = {
4107 frame_callback
4108};
4109
4110static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004111surface_redraw(struct surface *surface)
4112{
Pekka Paalanen71233882013-04-25 13:57:53 +03004113 DBG_OBJ(surface->surface, "begin\n");
4114
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004115 if (!surface->window->redraw_needed && !surface->redraw_needed)
4116 return;
4117
4118 /* Whole-window redraw forces a redraw even if the previous has
4119 * not yet hit the screen.
4120 */
4121 if (surface->frame_cb) {
4122 if (!surface->window->redraw_needed)
4123 return;
4124
Pekka Paalanen71233882013-04-25 13:57:53 +03004125 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004126 wl_callback_destroy(surface->frame_cb);
4127 }
4128
4129 surface->frame_cb = wl_surface_frame(surface->surface);
4130 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004131 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004132
4133 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004134 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004135 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004136 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004137}
4138
4139static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004140idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004141{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004142 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004143 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004144
Pekka Paalanen71233882013-04-25 13:57:53 +03004145 DBG(" --------- \n");
4146
Pekka Paalaneneebff542013-04-25 13:57:52 +03004147 wl_list_init(&window->redraw_task.link);
4148 window->redraw_task_scheduled = 0;
4149
4150 if (window->resize_needed) {
4151 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004152 if (window->main_surface->frame_cb) {
4153 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004154 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004155 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004156
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004157 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03004158 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004159
Pekka Paalanen35e82632013-04-25 13:57:48 +03004160 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004161 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004162
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004163 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004164 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004165
4166 wl_list_for_each(surface, &window->subsurface_list, link)
4167 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004168}
4169
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004170static void
4171window_schedule_redraw_task(struct window *window)
4172{
4173 if (window->configure_requests)
4174 return;
4175 if (!window->redraw_task_scheduled) {
4176 window->redraw_task.run = idle_redraw;
4177 display_defer(window->display, &window->redraw_task);
4178 window->redraw_task_scheduled = 1;
4179 }
4180}
4181
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004182void
4183window_schedule_redraw(struct window *window)
4184{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004185 struct surface *surface;
4186
Pekka Paalanen71233882013-04-25 13:57:53 +03004187 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4188
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004189 wl_list_for_each(surface, &window->subsurface_list, link)
4190 surface->redraw_needed = 1;
4191
4192 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004193}
4194
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004195int
4196window_is_fullscreen(struct window *window)
4197{
4198 return window->type == TYPE_FULLSCREEN;
4199}
4200
MoD063a8822013-03-02 23:43:57 +00004201static void
4202configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4203{
4204 struct window *window = data;
4205
4206 wl_callback_destroy(callback);
4207 window->configure_requests--;
4208
4209 if (!window->configure_requests)
4210 window_schedule_redraw(window);
4211}
4212
4213static struct wl_callback_listener configure_request_listener = {
4214 configure_request_completed,
4215};
4216
4217static void
4218window_defer_redraw_until_configure(struct window* window)
4219{
4220 struct wl_callback *callback;
4221
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004222 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004223 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004224 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004225 }
4226
4227 callback = wl_display_sync(window->display->display);
4228 wl_callback_add_listener(callback, &configure_request_listener, window);
4229 window->configure_requests++;
4230}
4231
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004232void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004233window_set_fullscreen(struct window *window, int fullscreen)
4234{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004235 if (!window->display->shell)
4236 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004237
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004238 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004239 return;
4240
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004241 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004242 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004243 if (window->type == TYPE_TOPLEVEL) {
4244 window->saved_allocation = window->main_surface->allocation;
4245 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004246 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004247 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004248 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004249 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004250 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004251 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004252 if (window->saved_type == TYPE_MAXIMIZED) {
4253 window_set_maximized(window, 1);
4254 } else {
4255 window->type = TYPE_TOPLEVEL;
4256 wl_shell_surface_set_toplevel(window->shell_surface);
4257 window_schedule_resize(window,
4258 window->saved_allocation.width,
4259 window->saved_allocation.height);
4260 }
4261
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004262 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004263}
4264
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004265void
4266window_set_fullscreen_method(struct window *window,
4267 enum wl_shell_surface_fullscreen_method method)
4268{
4269 window->fullscreen_method = method;
4270}
4271
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004272int
4273window_is_maximized(struct window *window)
4274{
4275 return window->type == TYPE_MAXIMIZED;
4276}
4277
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004278void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004279window_set_maximized(struct window *window, int maximized)
4280{
4281 if (!window->display->shell)
4282 return;
4283
4284 if ((window->type == TYPE_MAXIMIZED) == maximized)
4285 return;
4286
4287 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004288 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004289 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4290 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004291 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004292 } else if (window->type == TYPE_FULLSCREEN) {
4293 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4294 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004295 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004296 } else {
4297 wl_shell_surface_set_toplevel(window->shell_surface);
4298 window->type = TYPE_TOPLEVEL;
4299 window_schedule_resize(window,
4300 window->saved_allocation.width,
4301 window->saved_allocation.height);
4302 }
4303}
4304
4305void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004306window_set_user_data(struct window *window, void *data)
4307{
4308 window->user_data = data;
4309}
4310
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004311void *
4312window_get_user_data(struct window *window)
4313{
4314 return window->user_data;
4315}
4316
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004317void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004318window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004319 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004320{
4321 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004322}
4323
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004324void
4325window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004326 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004327{
4328 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004329}
4330
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004331void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004332window_set_data_handler(struct window *window, window_data_handler_t handler)
4333{
4334 window->data_handler = handler;
4335}
4336
4337void
4338window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4339{
4340 window->drop_handler = handler;
4341}
4342
4343void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004344window_set_close_handler(struct window *window,
4345 window_close_handler_t handler)
4346{
4347 window->close_handler = handler;
4348}
4349
4350void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004351window_set_fullscreen_handler(struct window *window,
4352 window_fullscreen_handler_t handler)
4353{
4354 window->fullscreen_handler = handler;
4355}
4356
4357void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004358window_set_output_handler(struct window *window,
4359 window_output_handler_t handler)
4360{
4361 window->output_handler = handler;
4362}
4363
4364void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004365window_set_title(struct window *window, const char *title)
4366{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004367 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004368 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004369 if (window->shell_surface)
4370 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004371}
4372
4373const char *
4374window_get_title(struct window *window)
4375{
4376 return window->title;
4377}
4378
4379void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004380window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4381{
4382 struct text_cursor_position *text_cursor_position =
4383 window->display->text_cursor_position;
4384
Scott Moreau9295ce02012-06-01 12:46:10 -06004385 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004386 return;
4387
4388 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004389 window->main_surface->surface,
4390 wl_fixed_from_int(x),
4391 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004392}
4393
4394void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004395window_damage(struct window *window, int32_t x, int32_t y,
4396 int32_t width, int32_t height)
4397{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004398 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004399}
4400
Casey Dahlin9074db52012-04-19 22:50:09 -04004401static void
4402surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004403 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004404{
Rob Bradford7507b572012-05-15 17:55:34 +01004405 struct window *window = data;
4406 struct output *output;
4407 struct output *output_found = NULL;
4408 struct window_output *window_output;
4409
4410 wl_list_for_each(output, &window->display->output_list, link) {
4411 if (output->output == wl_output) {
4412 output_found = output;
4413 break;
4414 }
4415 }
4416
4417 if (!output_found)
4418 return;
4419
Brian Lovinbc919262013-08-07 15:34:59 -07004420 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004421 window_output->output = output_found;
4422
4423 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004424
4425 if (window->output_handler)
4426 window->output_handler(window, output_found, 1,
4427 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004428}
4429
4430static void
4431surface_leave(void *data,
4432 struct wl_surface *wl_surface, struct wl_output *output)
4433{
Rob Bradford7507b572012-05-15 17:55:34 +01004434 struct window *window = data;
4435 struct window_output *window_output;
4436 struct window_output *window_output_found = NULL;
4437
4438 wl_list_for_each(window_output, &window->window_output_list, link) {
4439 if (window_output->output->output == output) {
4440 window_output_found = window_output;
4441 break;
4442 }
4443 }
4444
4445 if (window_output_found) {
4446 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004447
4448 if (window->output_handler)
4449 window->output_handler(window, window_output->output,
4450 0, window->user_data);
4451
Rob Bradford7507b572012-05-15 17:55:34 +01004452 free(window_output_found);
4453 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004454}
4455
4456static const struct wl_surface_listener surface_listener = {
4457 surface_enter,
4458 surface_leave
4459};
4460
Pekka Paalanen4e373742013-02-13 16:17:13 +02004461static struct surface *
4462surface_create(struct window *window)
4463{
4464 struct display *display = window->display;
4465 struct surface *surface;
4466
Brian Lovinbc919262013-08-07 15:34:59 -07004467 surface = xmalloc(sizeof *surface);
4468 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004469 if (!surface)
4470 return NULL;
4471
4472 surface->window = window;
4473 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004474 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004475 wl_surface_add_listener(surface->surface, &surface_listener, window);
4476
Pekka Paalanen35e82632013-04-25 13:57:48 +03004477 wl_list_insert(&window->subsurface_list, &surface->link);
4478
Pekka Paalanen4e373742013-02-13 16:17:13 +02004479 return surface;
4480}
4481
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004482static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004483window_create_internal(struct display *display,
4484 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004485{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004486 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004487 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004488
Peter Huttererf3d62272013-08-08 11:57:05 +10004489 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004490 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004491 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004492 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004493
4494 surface = surface_create(window);
4495 window->main_surface = surface;
4496
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004497 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004498 window->shell_surface =
4499 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004500 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004501 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004502 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004503
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004504 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004505 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004506 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004507 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004508
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004509 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004510#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004511 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004512#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004513 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004514#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004515 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004516 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004517
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004518 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004519 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004520 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004521
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004522 if (window->shell_surface) {
4523 wl_shell_surface_set_user_data(window->shell_surface, window);
4524 wl_shell_surface_add_listener(window->shell_surface,
4525 &shell_surface_listener, window);
4526 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004527
Rob Bradford7507b572012-05-15 17:55:34 +01004528 wl_list_init (&window->window_output_list);
4529
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004530 return window;
4531}
4532
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004533struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004534window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004535{
Kristian Høgsberg41624832013-07-25 16:53:10 -07004536 return window_create_internal(display, NULL, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004537}
4538
4539struct window *
4540window_create_custom(struct display *display)
4541{
Kristian Høgsbergce278412013-07-25 15:20:20 -07004542 return window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004543}
4544
4545struct window *
4546window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004547 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004548{
4549 struct window *window;
4550
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004551 window = window_create_internal(parent->display,
4552 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004553
4554 window->x = x;
4555 window->y = y;
4556
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004557 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004558 wl_shell_surface_set_transient(
4559 window->shell_surface,
4560 window->parent->main_surface->surface,
4561 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004562
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004563 return window;
4564}
4565
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004566static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004567menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004568{
4569 int next;
4570
4571 next = (sy - 8) / 20;
4572 if (menu->current != next) {
4573 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004574 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004575 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004576}
4577
4578static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004579menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004580 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004581 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004582{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004583 struct menu *menu = data;
4584
4585 if (widget == menu->widget)
4586 menu_set_item(data, y);
4587
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004588 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004589}
4590
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004591static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004592menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004593 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004594{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004595 struct menu *menu = data;
4596
4597 if (widget == menu->widget)
4598 menu_set_item(data, y);
4599
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004600 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004601}
4602
4603static void
4604menu_leave_handler(struct widget *widget, struct input *input, void *data)
4605{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004606 struct menu *menu = data;
4607
4608 if (widget == menu->widget)
4609 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004610}
4611
4612static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004613menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004614 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004615 uint32_t button, enum wl_pointer_button_state state,
4616 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004617
4618{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004619 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004620
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004621 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4622 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004623 /* Either relase after press-drag-release or
4624 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004625 menu->func(menu->window->parent,
4626 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004627 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004628 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004629 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004630 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004631 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004632}
4633
4634static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004635menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004636{
4637 cairo_t *cr;
4638 const int32_t r = 3, margin = 3;
4639 struct menu *menu = data;
4640 int32_t width, height, i;
4641
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004642 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004643 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4644 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4645 cairo_paint(cr);
4646
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004647 width = widget->allocation.width;
4648 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004649 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004650
4651 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004652 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4653 cairo_fill(cr);
4654
4655 for (i = 0; i < menu->count; i++) {
4656 if (i == menu->current) {
4657 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4658 cairo_rectangle(cr, margin, i * 20 + margin,
4659 width - 2 * margin, 20);
4660 cairo_fill(cr);
4661 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4662 cairo_move_to(cr, 10, i * 20 + 16);
4663 cairo_show_text(cr, menu->entries[i]);
4664 } else {
4665 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4666 cairo_move_to(cr, 10, i * 20 + 16);
4667 cairo_show_text(cr, menu->entries[i]);
4668 }
4669 }
4670
4671 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004672}
4673
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004674void
4675window_show_menu(struct display *display,
4676 struct input *input, uint32_t time, struct window *parent,
4677 int32_t x, int32_t y,
4678 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004679{
4680 struct window *window;
4681 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004682 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004683
4684 menu = malloc(sizeof *menu);
4685 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004686 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004687
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004688 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004689 if (!window) {
4690 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004691 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004692 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004693
4694 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004695 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004696 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4697 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004698 menu->entries = entries;
4699 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004700 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004701 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004702 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004703 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004704 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004705 window->type = TYPE_MENU;
4706 window->x = x;
4707 window->y = y;
4708
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004709 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004710 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004711 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004712 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004713 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004714
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004715 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004716 widget_set_enter_handler(menu->widget, menu_enter_handler);
4717 widget_set_leave_handler(menu->widget, menu_leave_handler);
4718 widget_set_motion_handler(menu->widget, menu_motion_handler);
4719 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004720
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004721 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004722 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004723}
4724
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004725void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004726window_set_buffer_type(struct window *window, enum window_buffer_type type)
4727{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004728 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004729}
4730
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004731void
4732window_set_preferred_format(struct window *window,
4733 enum preferred_format format)
4734{
4735 window->preferred_format = format;
4736}
4737
Pekka Paalanen35e82632013-04-25 13:57:48 +03004738struct widget *
4739window_add_subsurface(struct window *window, void *data,
4740 enum subsurface_mode default_mode)
4741{
4742 struct widget *widget;
4743 struct surface *surface;
4744 struct wl_surface *parent;
4745 struct wl_subcompositor *subcompo = window->display->subcompositor;
4746
Pekka Paalanen35e82632013-04-25 13:57:48 +03004747 surface = surface_create(window);
4748 widget = widget_create(window, surface, data);
4749 wl_list_init(&widget->link);
4750 surface->widget = widget;
4751
4752 parent = window->main_surface->surface;
4753 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4754 surface->surface,
4755 parent);
4756 surface->synchronized = 1;
4757
4758 switch (default_mode) {
4759 case SUBSURFACE_SYNCHRONIZED:
4760 surface->synchronized_default = 1;
4761 break;
4762 case SUBSURFACE_DESYNCHRONIZED:
4763 surface->synchronized_default = 0;
4764 break;
4765 default:
4766 assert(!"bad enum subsurface_mode");
4767 }
4768
4769 return widget;
4770}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004771
4772static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004773display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004774 struct wl_output *wl_output,
4775 int x, int y,
4776 int physical_width,
4777 int physical_height,
4778 int subpixel,
4779 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004780 const char *model,
4781 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004782{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004783 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004784
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004785 output->allocation.x = x;
4786 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004787 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004788}
4789
4790static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004791display_handle_done(void *data,
4792 struct wl_output *wl_output)
4793{
4794}
4795
4796static void
4797display_handle_scale(void *data,
4798 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004799 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004800{
4801 struct output *output = data;
4802
4803 output->scale = scale;
4804}
4805
4806static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004807display_handle_mode(void *data,
4808 struct wl_output *wl_output,
4809 uint32_t flags,
4810 int width,
4811 int height,
4812 int refresh)
4813{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004814 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004815 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004816
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004817 if (flags & WL_OUTPUT_MODE_CURRENT) {
4818 output->allocation.width = width;
4819 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004820 if (display->output_configure_handler)
4821 (*display->output_configure_handler)(
4822 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004823 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004824}
4825
4826static const struct wl_output_listener output_listener = {
4827 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004828 display_handle_mode,
4829 display_handle_done,
4830 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004831};
4832
4833static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004834display_add_output(struct display *d, uint32_t id)
4835{
4836 struct output *output;
4837
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004838 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004839 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004840 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004841 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004842 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004843 wl_list_insert(d->output_list.prev, &output->link);
4844
4845 wl_output_add_listener(output->output, &output_listener, output);
4846}
4847
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004848static void
4849output_destroy(struct output *output)
4850{
4851 if (output->destroy_handler)
4852 (*output->destroy_handler)(output, output->user_data);
4853
4854 wl_output_destroy(output->output);
4855 wl_list_remove(&output->link);
4856 free(output);
4857}
4858
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004859void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004860display_set_global_handler(struct display *display,
4861 display_global_handler_t handler)
4862{
4863 struct global *global;
4864
4865 display->global_handler = handler;
4866 if (!handler)
4867 return;
4868
4869 wl_list_for_each(global, &display->global_list, link)
4870 display->global_handler(display,
4871 global->name, global->interface,
4872 global->version, display->user_data);
4873}
4874
4875void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004876display_set_output_configure_handler(struct display *display,
4877 display_output_handler_t handler)
4878{
4879 struct output *output;
4880
4881 display->output_configure_handler = handler;
4882 if (!handler)
4883 return;
4884
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004885 wl_list_for_each(output, &display->output_list, link) {
4886 if (output->allocation.width == 0 &&
4887 output->allocation.height == 0)
4888 continue;
4889
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004890 (*display->output_configure_handler)(output,
4891 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004892 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004893}
4894
4895void
4896output_set_user_data(struct output *output, void *data)
4897{
4898 output->user_data = data;
4899}
4900
4901void *
4902output_get_user_data(struct output *output)
4903{
4904 return output->user_data;
4905}
4906
4907void
4908output_set_destroy_handler(struct output *output,
4909 display_output_handler_t handler)
4910{
4911 output->destroy_handler = handler;
4912 /* FIXME: implement this, once we have way to remove outputs */
4913}
4914
4915void
Scott Moreau4e072362012-09-29 02:03:11 -06004916output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004917{
Scott Moreau4e072362012-09-29 02:03:11 -06004918 struct rectangle allocation = output->allocation;
4919
4920 switch (output->transform) {
4921 case WL_OUTPUT_TRANSFORM_90:
4922 case WL_OUTPUT_TRANSFORM_270:
4923 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4924 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4925 /* Swap width and height */
4926 allocation.width = output->allocation.height;
4927 allocation.height = output->allocation.width;
4928 break;
4929 }
4930
4931 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004932}
4933
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004934struct wl_output *
4935output_get_wl_output(struct output *output)
4936{
4937 return output->output;
4938}
4939
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004940enum wl_output_transform
4941output_get_transform(struct output *output)
4942{
4943 return output->transform;
4944}
4945
Alexander Larssonafd319a2013-05-22 14:41:27 +02004946uint32_t
4947output_get_scale(struct output *output)
4948{
4949 return output->scale;
4950}
4951
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004952static void
Daniel Stone97f68542012-05-30 16:32:01 +01004953fini_xkb(struct input *input)
4954{
4955 xkb_state_unref(input->xkb.state);
4956 xkb_map_unref(input->xkb.keymap);
4957}
4958
Rob Bradford08031182013-08-13 20:11:03 +01004959#define MAX(a,b) ((a) > (b) ? a : b)
4960
Daniel Stone97f68542012-05-30 16:32:01 +01004961static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004962display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004963{
4964 struct input *input;
4965
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004966 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004967 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004968 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4969 MAX(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004970 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004971 input->pointer_focus = NULL;
4972 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004973 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004974 wl_list_insert(d->input_list.prev, &input->link);
4975
Daniel Stone37816df2012-05-16 18:45:18 +01004976 wl_seat_add_listener(input->seat, &seat_listener, input);
4977 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004978
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004979 input->data_device =
4980 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004981 input->seat);
4982 wl_data_device_add_listener(input->data_device, &data_device_listener,
4983 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004984
4985 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004986
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004987 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4988 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004989 input->repeat_task.run = keyboard_repeat_func;
4990 display_watch_fd(d, input->repeat_timer_fd,
4991 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004992}
4993
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004994static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004995input_destroy(struct input *input)
4996{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004997 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004998 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004999
5000 if (input->drag_offer)
5001 data_offer_destroy(input->drag_offer);
5002
5003 if (input->selection_offer)
5004 data_offer_destroy(input->selection_offer);
5005
5006 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005007
5008 if (input->display->seat_version >= 3) {
5009 if (input->pointer)
5010 wl_pointer_release(input->pointer);
5011 if (input->keyboard)
5012 wl_keyboard_release(input->keyboard);
5013 }
5014
Daniel Stone97f68542012-05-30 16:32:01 +01005015 fini_xkb(input);
5016
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005017 wl_surface_destroy(input->pointer_surface);
5018
Pekka Paalanene1207c72011-12-16 12:02:09 +02005019 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005020 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005021 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005022 free(input);
5023}
5024
5025static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005026init_workspace_manager(struct display *d, uint32_t id)
5027{
5028 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005029 wl_registry_bind(d->registry, id,
5030 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005031 if (d->workspace_manager != NULL)
5032 workspace_manager_add_listener(d->workspace_manager,
5033 &workspace_manager_listener,
5034 d);
5035}
5036
5037static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005038shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5039{
5040 struct display *d = data;
5041
5042 if (format == WL_SHM_FORMAT_RGB565)
5043 d->has_rgb565 = 1;
5044}
5045
5046struct wl_shm_listener shm_listener = {
5047 shm_format
5048};
5049
5050static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005051registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5052 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005053{
5054 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005055 struct global *global;
5056
Brian Lovinbc919262013-08-07 15:34:59 -07005057 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005058 global->name = id;
5059 global->interface = strdup(interface);
5060 global->version = version;
5061 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005062
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005063 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005064 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005065 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005066 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005067 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005068 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005069 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005070 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005071 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005072 d->shell = wl_registry_bind(registry,
5073 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005074 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005075 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005076 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005077 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5078 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005079 wl_registry_bind(registry, id,
5080 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005081 } else if (strcmp(interface, "text_cursor_position") == 0) {
5082 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005083 wl_registry_bind(registry, id,
5084 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005085 } else if (strcmp(interface, "workspace_manager") == 0) {
5086 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005087 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5088 d->subcompositor =
5089 wl_registry_bind(registry, id,
5090 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005091 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005092
5093 if (d->global_handler)
5094 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005095}
5096
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005097static void
5098registry_handle_global_remove(void *data, struct wl_registry *registry,
5099 uint32_t name)
5100{
5101 struct display *d = data;
5102 struct global *global;
5103 struct global *tmp;
5104
5105 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5106 if (global->name != name)
5107 continue;
5108
5109 /* XXX: Should destroy bound globals, and call
5110 * the counterpart of display::global_handler
5111 */
5112 wl_list_remove(&global->link);
5113 free(global->interface);
5114 free(global);
5115 }
5116}
5117
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005118void *
5119display_bind(struct display *display, uint32_t name,
5120 const struct wl_interface *interface, uint32_t version)
5121{
5122 return wl_registry_bind(display->registry, name, interface, version);
5123}
5124
5125static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005126 registry_handle_global,
5127 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005128};
5129
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005130#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005131static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005132init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005133{
5134 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005135 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005136
Rob Clark6396ed32012-03-11 19:48:41 -05005137#ifdef USE_CAIRO_GLESV2
5138# define GL_BIT EGL_OPENGL_ES2_BIT
5139#else
5140# define GL_BIT EGL_OPENGL_BIT
5141#endif
5142
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005143 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005144 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005145 EGL_RED_SIZE, 1,
5146 EGL_GREEN_SIZE, 1,
5147 EGL_BLUE_SIZE, 1,
5148 EGL_ALPHA_SIZE, 1,
5149 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005150 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005151 EGL_NONE
5152 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005153
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005154#ifdef USE_CAIRO_GLESV2
5155 static const EGLint context_attribs[] = {
5156 EGL_CONTEXT_CLIENT_VERSION, 2,
5157 EGL_NONE
5158 };
5159 EGLint api = EGL_OPENGL_ES_API;
5160#else
5161 EGLint *context_attribs = NULL;
5162 EGLint api = EGL_OPENGL_API;
5163#endif
5164
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005165 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005166 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005167 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005168 return -1;
5169 }
5170
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005171 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005172 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005173 return -1;
5174 }
5175
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005176 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5177 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005178 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005179 return -1;
5180 }
5181
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005182 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005183 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005184 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005185 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005186 return -1;
5187 }
5188
Benjamin Franzke0c991632011-09-27 21:57:31 +02005189 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5190 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005191 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005192 return -1;
5193 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005194
5195 return 0;
5196}
5197
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005198static void
5199fini_egl(struct display *display)
5200{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005201 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005202
5203 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5204 EGL_NO_CONTEXT);
5205
5206 eglTerminate(display->dpy);
5207 eglReleaseThread();
5208}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005209#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005210
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005211static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005212init_dummy_surface(struct display *display)
5213{
5214 int len;
5215 void *data;
5216
5217 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5218 data = malloc(len);
5219 display->dummy_surface =
5220 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5221 1, 1, len);
5222 display->dummy_surface_data = data;
5223}
5224
5225static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005226handle_display_data(struct task *task, uint32_t events)
5227{
5228 struct display *display =
5229 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005230 struct epoll_event ep;
5231 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005232
5233 display->display_fd_events = events;
5234
5235 if (events & EPOLLERR || events & EPOLLHUP) {
5236 display_exit(display);
5237 return;
5238 }
5239
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005240 if (events & EPOLLIN) {
5241 ret = wl_display_dispatch(display->display);
5242 if (ret == -1) {
5243 display_exit(display);
5244 return;
5245 }
5246 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005247
5248 if (events & EPOLLOUT) {
5249 ret = wl_display_flush(display->display);
5250 if (ret == 0) {
5251 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5252 ep.data.ptr = &display->display_task;
5253 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5254 display->display_fd, &ep);
5255 } else if (ret == -1 && errno != EAGAIN) {
5256 display_exit(display);
5257 return;
5258 }
5259 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005260}
5261
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005262static void
5263log_handler(const char *format, va_list args)
5264{
5265 vfprintf(stderr, format, args);
5266}
5267
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005268struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005269display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005270{
5271 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005272
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005273 wl_log_set_handler_client(log_handler);
5274
Peter Huttererf3d62272013-08-08 11:57:05 +10005275 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005276 if (d == NULL)
5277 return NULL;
5278
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005279 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005280 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005281 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005282 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005283 return NULL;
5284 }
5285
Rob Bradford5ab9c752013-07-26 16:29:43 +01005286 d->xkb_context = xkb_context_new(0);
5287 if (d->xkb_context == NULL) {
5288 fprintf(stderr, "Failed to create XKB context\n");
5289 free(d);
5290 return NULL;
5291 }
5292
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005293 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005294 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005295 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005296 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5297 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005298
5299 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005300 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005301 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005302 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005303
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005304 d->workspace = 0;
5305 d->workspace_count = 1;
5306
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005307 d->registry = wl_display_get_registry(d->display);
5308 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005309
5310 if (wl_display_dispatch(d->display) < 0) {
5311 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5312 return NULL;
5313 }
5314
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005315#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005316 if (init_egl(d) < 0)
5317 fprintf(stderr, "EGL does not seem to work, "
5318 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005319#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005320
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005321 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005322
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005323 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005324
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005325 wl_list_init(&d->window_list);
5326
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005327 init_dummy_surface(d);
5328
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005329 return d;
5330}
5331
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005332static void
5333display_destroy_outputs(struct display *display)
5334{
5335 struct output *tmp;
5336 struct output *output;
5337
5338 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5339 output_destroy(output);
5340}
5341
Pekka Paalanene1207c72011-12-16 12:02:09 +02005342static void
5343display_destroy_inputs(struct display *display)
5344{
5345 struct input *tmp;
5346 struct input *input;
5347
5348 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5349 input_destroy(input);
5350}
5351
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005352void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005353display_destroy(struct display *display)
5354{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005355 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005356 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5357 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005358
5359 if (!wl_list_empty(&display->deferred_list))
5360 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5361
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005362 cairo_surface_destroy(display->dummy_surface);
5363 free(display->dummy_surface_data);
5364
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005365 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005366 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005367
Daniel Stone97f68542012-05-30 16:32:01 +01005368 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005369
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005370 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005371 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005372
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005373#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005374 if (display->argb_device)
5375 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005376#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005377
Pekka Paalanen35e82632013-04-25 13:57:48 +03005378 if (display->subcompositor)
5379 wl_subcompositor_destroy(display->subcompositor);
5380
Pekka Paalanenc2052982011-12-16 11:41:32 +02005381 if (display->shell)
5382 wl_shell_destroy(display->shell);
5383
5384 if (display->shm)
5385 wl_shm_destroy(display->shm);
5386
5387 if (display->data_device_manager)
5388 wl_data_device_manager_destroy(display->data_device_manager);
5389
5390 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005391 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005392
5393 close(display->epoll_fd);
5394
U. Artie Eoff44874d92012-10-02 21:12:35 -07005395 if (!(display->display_fd_events & EPOLLERR) &&
5396 !(display->display_fd_events & EPOLLHUP))
5397 wl_display_flush(display->display);
5398
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005399 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005400 free(display);
5401}
5402
5403void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005404display_set_user_data(struct display *display, void *data)
5405{
5406 display->user_data = data;
5407}
5408
5409void *
5410display_get_user_data(struct display *display)
5411{
5412 return display->user_data;
5413}
5414
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005415struct wl_display *
5416display_get_display(struct display *display)
5417{
5418 return display->display;
5419}
5420
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005421int
5422display_has_subcompositor(struct display *display)
5423{
5424 if (display->subcompositor)
5425 return 1;
5426
5427 wl_display_roundtrip(display->display);
5428
5429 return display->subcompositor != NULL;
5430}
5431
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005432cairo_device_t *
5433display_get_cairo_device(struct display *display)
5434{
5435 return display->argb_device;
5436}
5437
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005438struct output *
5439display_get_output(struct display *display)
5440{
5441 return container_of(display->output_list.next, struct output, link);
5442}
5443
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005444struct wl_compositor *
5445display_get_compositor(struct display *display)
5446{
5447 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005448}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005449
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005450uint32_t
5451display_get_serial(struct display *display)
5452{
5453 return display->serial;
5454}
5455
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005456EGLDisplay
5457display_get_egl_display(struct display *d)
5458{
5459 return d->dpy;
5460}
5461
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005462struct wl_data_source *
5463display_create_data_source(struct display *display)
5464{
5465 return wl_data_device_manager_create_data_source(display->data_device_manager);
5466}
5467
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005468EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005469display_get_argb_egl_config(struct display *d)
5470{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005471 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005472}
5473
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005474struct wl_shell *
5475display_get_shell(struct display *display)
5476{
5477 return display->shell;
5478}
5479
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005480int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005481display_acquire_window_surface(struct display *display,
5482 struct window *window,
5483 EGLContext ctx)
5484{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005485 struct surface *surface = window->main_surface;
5486
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005487 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005488 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005489
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005490 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005491 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005492}
5493
5494void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005495display_release_window_surface(struct display *display,
5496 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005497{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005498 struct surface *surface = window->main_surface;
5499
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005500 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005501 return;
5502
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005503 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005504}
5505
5506void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005507display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005508{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005509 wl_list_insert(&display->deferred_list, &task->link);
5510}
5511
5512void
5513display_watch_fd(struct display *display,
5514 int fd, uint32_t events, struct task *task)
5515{
5516 struct epoll_event ep;
5517
5518 ep.events = events;
5519 ep.data.ptr = task;
5520 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5521}
5522
5523void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005524display_unwatch_fd(struct display *display, int fd)
5525{
5526 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5527}
5528
5529void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005530display_run(struct display *display)
5531{
5532 struct task *task;
5533 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005534 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005535
Pekka Paalanen826d7952011-12-15 10:14:07 +02005536 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005537 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005538 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005539 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005540 struct task, link);
5541 wl_list_remove(&task->link);
5542 task->run(task, 0);
5543 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005544
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005545 wl_display_dispatch_pending(display->display);
5546
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005547 if (!display->running)
5548 break;
5549
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005550 ret = wl_display_flush(display->display);
5551 if (ret < 0 && errno == EAGAIN) {
5552 ep[0].events =
5553 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5554 ep[0].data.ptr = &display->display_task;
5555
5556 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5557 display->display_fd, &ep[0]);
5558 } else if (ret < 0) {
5559 break;
5560 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005561
5562 count = epoll_wait(display->epoll_fd,
5563 ep, ARRAY_LENGTH(ep), -1);
5564 for (i = 0; i < count; i++) {
5565 task = ep[i].data.ptr;
5566 task->run(task, ep[i].events);
5567 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005568 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005569}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005570
5571void
5572display_exit(struct display *display)
5573{
5574 display->running = 0;
5575}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005576
5577void
5578keysym_modifiers_add(struct wl_array *modifiers_map,
5579 const char *name)
5580{
5581 size_t len = strlen(name) + 1;
5582 char *p;
5583
5584 p = wl_array_add(modifiers_map, len);
5585
5586 if (p == NULL)
5587 return;
5588
5589 strncpy(p, name, len);
5590}
5591
5592static xkb_mod_index_t
5593keysym_modifiers_get_index(struct wl_array *modifiers_map,
5594 const char *name)
5595{
5596 xkb_mod_index_t index = 0;
5597 char *p = modifiers_map->data;
5598
5599 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5600 if (strcmp(p, name) == 0)
5601 return index;
5602
5603 index++;
5604 p += strlen(p) + 1;
5605 }
5606
5607 return XKB_MOD_INVALID;
5608}
5609
5610xkb_mod_mask_t
5611keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5612 const char *name)
5613{
5614 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5615
5616 if (index == XKB_MOD_INVALID)
5617 return XKB_MOD_INVALID;
5618
5619 return 1 << index;
5620}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005621
5622void *
5623fail_on_null(void *p)
5624{
5625 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005626 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005627 exit(EXIT_FAILURE);
5628 }
5629
5630 return p;
5631}
5632
5633void *
5634xmalloc(size_t s)
5635{
5636 return fail_on_null(malloc(s));
5637}
5638
Peter Huttererf3d62272013-08-08 11:57:05 +10005639void *
5640xzalloc(size_t s)
5641{
5642 return fail_on_null(zalloc(s));
5643}
5644
Kristian Høgsbergce278412013-07-25 15:20:20 -07005645char *
5646xstrdup(const char *s)
5647{
5648 return fail_on_null(strdup(s));
5649}