blob: aed2663378b5f93e1b0fcf37cc0bcccc1a25345a [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{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001310 struct weston_config *config;
1311 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001312 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001313 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001314 unsigned int i, j;
1315 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001316
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001317 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001318 s = weston_config_get_section(config, "shell", NULL, NULL);
1319 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1320 weston_config_section_get_int(s, "cursor-size", &size, 32);
1321 weston_config_destroy(config);
1322
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001323 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001324 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001325 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001326 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001327
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001328 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001329 cursor = NULL;
1330 for (j = 0; !cursor && j < cursors[i].count; ++j)
1331 cursor = wl_cursor_theme_get_cursor(
1332 display->cursor_theme, cursors[i].names[j]);
1333
1334 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001335 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001336 cursors[i].names[0]);
1337
1338 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001339 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001340}
1341
1342static void
1343destroy_cursors(struct display *display)
1344{
1345 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001346 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001347}
1348
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001349struct wl_cursor_image *
1350display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001351{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001352 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001353
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001354 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001355}
1356
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001357static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001358surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001359{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001360 if (!surface->cairo_surface)
1361 return;
1362
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001363 if (surface->opaque_region) {
1364 wl_surface_set_opaque_region(surface->surface,
1365 surface->opaque_region);
1366 wl_region_destroy(surface->opaque_region);
1367 surface->opaque_region = NULL;
1368 }
1369
1370 if (surface->input_region) {
1371 wl_surface_set_input_region(surface->surface,
1372 surface->input_region);
1373 wl_region_destroy(surface->input_region);
1374 surface->input_region = NULL;
1375 }
1376
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001377 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001378 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001379 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001380
Pekka Paalanen89dee002013-02-13 16:17:20 +02001381 cairo_surface_destroy(surface->cairo_surface);
1382 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001383}
1384
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001385int
1386window_has_focus(struct window *window)
1387{
1388 return window->focus_count > 0;
1389}
1390
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001391static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001392window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001393{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001394 struct surface *surface;
1395
Pekka Paalanen89dee002013-02-13 16:17:20 +02001396 if (window->type == TYPE_NONE) {
1397 window->type = TYPE_TOPLEVEL;
1398 if (window->shell_surface)
1399 wl_shell_surface_set_toplevel(window->shell_surface);
1400 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001401
Pekka Paalanen35e82632013-04-25 13:57:48 +03001402 wl_list_for_each(surface, &window->subsurface_list, link) {
1403 if (surface == window->main_surface)
1404 continue;
1405
1406 surface_flush(surface);
1407 }
1408
Pekka Paalanen89dee002013-02-13 16:17:20 +02001409 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001410}
1411
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001412struct display *
1413window_get_display(struct window *window)
1414{
1415 return window->display;
1416}
1417
Pekka Paalanen89dee002013-02-13 16:17:20 +02001418static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001419surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001420{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001421 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001422 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001423
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424 if (!surface->toysurface && display->dpy &&
1425 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001426 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001427 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001428 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001429 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001430 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001431 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001432
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001433 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001434 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001435 surface->surface,
1436 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001437
Pekka Paalanen89dee002013-02-13 16:17:20 +02001438 surface->cairo_surface = surface->toysurface->prepare(
1439 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001440 allocation.width, allocation.height, flags,
1441 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001442}
1443
1444static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001445window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001446{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001447 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001448 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001449 int dx = 0;
1450 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001451
Pekka Paalanenec076692012-11-30 13:37:27 +02001452 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001453 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001454
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001455 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1456 flags |= SURFACE_HINT_RGB565;
1457
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001458 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1459 dx = surface->server_allocation.width -
1460 surface->allocation.width;
1461
1462 if (window->resize_edges & WINDOW_RESIZING_TOP)
1463 dy = surface->server_allocation.height -
1464 surface->allocation.height;
1465
1466 window->resize_edges = 0;
1467
Pekka Paalanen89dee002013-02-13 16:17:20 +02001468 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001469}
1470
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001471int
1472window_get_buffer_transform(struct window *window)
1473{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001474 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001475}
1476
1477void
1478window_set_buffer_transform(struct window *window,
1479 enum wl_output_transform transform)
1480{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001481 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001482 wl_surface_set_buffer_transform(window->main_surface->surface,
1483 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001484}
1485
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001486void
1487window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001488 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001489{
1490 window->main_surface->buffer_scale = scale;
1491 wl_surface_set_buffer_scale(window->main_surface->surface,
1492 scale);
1493}
1494
1495uint32_t
1496window_get_buffer_scale(struct window *window)
1497{
1498 return window->main_surface->buffer_scale;
1499}
1500
Alexander Larssond68f5232013-05-22 14:41:33 +02001501uint32_t
1502window_get_output_scale(struct window *window)
1503{
1504 struct window_output *window_output;
1505 struct window_output *window_output_tmp;
1506 int scale = 1;
1507
1508 wl_list_for_each_safe(window_output, window_output_tmp,
1509 &window->window_output_list, link) {
1510 if (window_output->output->scale > scale)
1511 scale = window_output->output->scale;
1512 }
1513
1514 return scale;
1515}
1516
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001517static void window_frame_destroy(struct frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001518
Pekka Paalanen4e373742013-02-13 16:17:13 +02001519static void
1520surface_destroy(struct surface *surface)
1521{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001522 if (surface->frame_cb)
1523 wl_callback_destroy(surface->frame_cb);
1524
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001525 if (surface->input_region)
1526 wl_region_destroy(surface->input_region);
1527
1528 if (surface->opaque_region)
1529 wl_region_destroy(surface->opaque_region);
1530
Pekka Paalanen35e82632013-04-25 13:57:48 +03001531 if (surface->subsurface)
1532 wl_subsurface_destroy(surface->subsurface);
1533
Pekka Paalanen4e373742013-02-13 16:17:13 +02001534 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001535
1536 if (surface->toysurface)
1537 surface->toysurface->destroy(surface->toysurface);
1538
Pekka Paalanen35e82632013-04-25 13:57:48 +03001539 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001540 free(surface);
1541}
1542
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001543void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001544window_destroy(struct window *window)
1545{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001546 struct display *display = window->display;
1547 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001548 struct window_output *window_output;
1549 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001550
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001551 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001552
Rusty Lynch1084da52013-08-15 09:10:08 -07001553 wl_list_for_each(input, &display->input_list, link) {
1554 if (input->touch_focus == window)
1555 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001556 if (input->pointer_focus == window)
1557 input->pointer_focus = NULL;
1558 if (input->keyboard_focus == window)
1559 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001560 if (input->focus_widget &&
1561 input->focus_widget->window == window)
1562 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001563 }
1564
Rob Bradford7507b572012-05-15 17:55:34 +01001565 wl_list_for_each_safe(window_output, window_output_tmp,
1566 &window->window_output_list, link) {
1567 free (window_output);
1568 }
1569
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001570 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001571 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001572
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001573 if (window->shell_surface)
1574 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001575
1576 surface_destroy(window->main_surface);
1577
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001578 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001579
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001580 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001581 free(window);
1582}
1583
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001584static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001585widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001586{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001588
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001589 wl_list_for_each(child, &widget->child_list, link) {
1590 target = widget_find_widget(child, x, y);
1591 if (target)
1592 return target;
1593 }
1594
1595 if (widget->allocation.x <= x &&
1596 x < widget->allocation.x + widget->allocation.width &&
1597 widget->allocation.y <= y &&
1598 y < widget->allocation.y + widget->allocation.height) {
1599 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001600 }
1601
1602 return NULL;
1603}
1604
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001605static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001606window_find_widget(struct window *window, int32_t x, int32_t y)
1607{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001608 struct surface *surface;
1609 struct widget *widget;
1610
1611 wl_list_for_each(surface, &window->subsurface_list, link) {
1612 widget = widget_find_widget(surface->widget, x, y);
1613 if (widget)
1614 return widget;
1615 }
1616
1617 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001618}
1619
1620static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001621widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001622{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001623 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001624
Peter Huttererf3d62272013-08-08 11:57:05 +10001625 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001626 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001627 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001628 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001629 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001630 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001631 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001632 widget->tooltip = NULL;
1633 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001634 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001635
1636 return widget;
1637}
1638
1639struct widget *
1640window_add_widget(struct window *window, void *data)
1641{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001642 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001643
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001644 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001645 wl_list_init(&widget->link);
1646 window->main_surface->widget = widget;
1647
1648 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001649}
1650
1651struct widget *
1652widget_add_widget(struct widget *parent, void *data)
1653{
1654 struct widget *widget;
1655
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001656 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001657 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001658
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001659 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001660}
1661
1662void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001663widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001665 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001666 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001667 struct input *input;
1668
Pekka Paalanen35e82632013-04-25 13:57:48 +03001669 /* Destroy the sub-surface along with the root widget */
1670 if (surface->widget == widget && surface->subsurface)
1671 surface_destroy(widget->surface);
1672
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001673 if (widget->tooltip) {
1674 free(widget->tooltip);
1675 widget->tooltip = NULL;
1676 }
1677
Pekka Paalanene156fb62012-01-19 13:51:38 +02001678 wl_list_for_each(input, &display->input_list, link) {
1679 if (input->focus_widget == widget)
1680 input->focus_widget = NULL;
1681 }
1682
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001683 wl_list_remove(&widget->link);
1684 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001685}
1686
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001687void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001688widget_set_default_cursor(struct widget *widget, int cursor)
1689{
1690 widget->default_cursor = cursor;
1691}
1692
1693void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001694widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001695{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001696 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001697}
1698
1699void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001700widget_set_size(struct widget *widget, int32_t width, int32_t height)
1701{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001702 widget->allocation.width = width;
1703 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001704}
1705
1706void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001707widget_set_allocation(struct widget *widget,
1708 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001709{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001710 widget->allocation.x = x;
1711 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001712 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001713}
1714
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001715void
1716widget_set_transparent(struct widget *widget, int transparent)
1717{
1718 widget->opaque = !transparent;
1719}
1720
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001721void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001722widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001723{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001724 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001725}
1726
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001727static cairo_surface_t *
1728widget_get_cairo_surface(struct widget *widget)
1729{
1730 struct surface *surface = widget->surface;
1731 struct window *window = widget->window;
1732
1733 if (!surface->cairo_surface) {
1734 if (surface == window->main_surface)
1735 window_create_main_surface(window);
1736 else
1737 surface_create_surface(surface, 0, 0, 0);
1738 }
1739
1740 return surface->cairo_surface;
1741}
1742
Alexander Larsson15901f02013-05-22 14:41:25 +02001743static void
1744widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1745{
1746 struct surface *surface = widget->surface;
1747 double angle;
1748 cairo_matrix_t m;
1749 enum wl_output_transform transform;
1750 int surface_width, surface_height;
1751 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001752 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001753
1754 surface_width = surface->allocation.width;
1755 surface_height = surface->allocation.height;
1756
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001757 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001758 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001759
Alexander Larsson15901f02013-05-22 14:41:25 +02001760 switch (transform) {
1761 case WL_OUTPUT_TRANSFORM_FLIPPED:
1762 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1763 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1764 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1765 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1766 break;
1767 default:
1768 cairo_matrix_init_identity(&m);
1769 break;
1770 }
1771
1772 switch (transform) {
1773 case WL_OUTPUT_TRANSFORM_NORMAL:
1774 default:
1775 angle = 0;
1776 translate_x = 0;
1777 translate_y = 0;
1778 break;
1779 case WL_OUTPUT_TRANSFORM_FLIPPED:
1780 angle = 0;
1781 translate_x = surface_width;
1782 translate_y = 0;
1783 break;
1784 case WL_OUTPUT_TRANSFORM_90:
1785 angle = M_PI_2;
1786 translate_x = surface_height;
1787 translate_y = 0;
1788 break;
1789 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1790 angle = M_PI_2;
1791 translate_x = surface_height;
1792 translate_y = surface_width;
1793 break;
1794 case WL_OUTPUT_TRANSFORM_180:
1795 angle = M_PI;
1796 translate_x = surface_width;
1797 translate_y = surface_height;
1798 break;
1799 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1800 angle = M_PI;
1801 translate_x = 0;
1802 translate_y = surface_height;
1803 break;
1804 case WL_OUTPUT_TRANSFORM_270:
1805 angle = M_PI + M_PI_2;
1806 translate_x = 0;
1807 translate_y = surface_width;
1808 break;
1809 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1810 angle = M_PI + M_PI_2;
1811 translate_x = 0;
1812 translate_y = 0;
1813 break;
1814 }
1815
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001816 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001817 cairo_translate(cr, translate_x, translate_y);
1818 cairo_rotate(cr, angle);
1819 cairo_transform(cr, &m);
1820}
1821
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001822cairo_t *
1823widget_cairo_create(struct widget *widget)
1824{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001825 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001826 cairo_surface_t *cairo_surface;
1827 cairo_t *cr;
1828
1829 cairo_surface = widget_get_cairo_surface(widget);
1830 cr = cairo_create(cairo_surface);
1831
Alexander Larsson15901f02013-05-22 14:41:25 +02001832 widget_cairo_update_transform(widget, cr);
1833
Pekka Paalanen35e82632013-04-25 13:57:48 +03001834 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1835
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001836 return cr;
1837}
1838
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001839struct wl_surface *
1840widget_get_wl_surface(struct widget *widget)
1841{
1842 return widget->surface->surface;
1843}
1844
1845uint32_t
1846widget_get_last_time(struct widget *widget)
1847{
1848 return widget->surface->last_time;
1849}
1850
1851void
1852widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1853{
1854 struct wl_compositor *comp = widget->window->display->compositor;
1855 struct surface *surface = widget->surface;
1856
1857 if (!surface->input_region)
1858 surface->input_region = wl_compositor_create_region(comp);
1859
1860 if (rect) {
1861 wl_region_add(surface->input_region,
1862 rect->x, rect->y, rect->width, rect->height);
1863 }
1864}
1865
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001866void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001867widget_set_resize_handler(struct widget *widget,
1868 widget_resize_handler_t handler)
1869{
1870 widget->resize_handler = handler;
1871}
1872
1873void
1874widget_set_redraw_handler(struct widget *widget,
1875 widget_redraw_handler_t handler)
1876{
1877 widget->redraw_handler = handler;
1878}
1879
1880void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001881widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001882{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001883 widget->enter_handler = handler;
1884}
1885
1886void
1887widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1888{
1889 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001890}
1891
1892void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001893widget_set_motion_handler(struct widget *widget,
1894 widget_motion_handler_t handler)
1895{
1896 widget->motion_handler = handler;
1897}
1898
1899void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001900widget_set_button_handler(struct widget *widget,
1901 widget_button_handler_t handler)
1902{
1903 widget->button_handler = handler;
1904}
1905
1906void
Rusty Lynch041815a2013-08-08 21:20:38 -07001907widget_set_touch_up_handler(struct widget *widget,
1908 widget_touch_up_handler_t handler)
1909{
1910 widget->touch_up_handler = handler;
1911}
1912
1913void
1914widget_set_touch_down_handler(struct widget *widget,
1915 widget_touch_down_handler_t handler)
1916{
1917 widget->touch_down_handler = handler;
1918}
1919
1920void
1921widget_set_touch_motion_handler(struct widget *widget,
1922 widget_touch_motion_handler_t handler)
1923{
1924 widget->touch_motion_handler = handler;
1925}
1926
1927void
1928widget_set_touch_frame_handler(struct widget *widget,
1929 widget_touch_frame_handler_t handler)
1930{
1931 widget->touch_frame_handler = handler;
1932}
1933
1934void
1935widget_set_touch_cancel_handler(struct widget *widget,
1936 widget_touch_cancel_handler_t handler)
1937{
1938 widget->touch_cancel_handler = handler;
1939}
1940
1941void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001942widget_set_axis_handler(struct widget *widget,
1943 widget_axis_handler_t handler)
1944{
1945 widget->axis_handler = handler;
1946}
1947
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001948static void
1949window_schedule_redraw_task(struct window *window);
1950
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001951void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001952widget_schedule_redraw(struct widget *widget)
1953{
Pekka Paalanen71233882013-04-25 13:57:53 +03001954 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001955 widget->surface->redraw_needed = 1;
1956 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001957}
1958
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001959cairo_surface_t *
1960window_get_surface(struct window *window)
1961{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001962 cairo_surface_t *cairo_surface;
1963
1964 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1965
1966 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001967}
1968
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001969struct wl_surface *
1970window_get_wl_surface(struct window *window)
1971{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001972 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001973}
1974
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001975struct wl_shell_surface *
1976window_get_wl_shell_surface(struct window *window)
1977{
1978 return window->shell_surface;
1979}
1980
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001981static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001982tooltip_redraw_handler(struct widget *widget, void *data)
1983{
1984 cairo_t *cr;
1985 const int32_t r = 3;
1986 struct tooltip *tooltip = data;
1987 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001988
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001989 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001990 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1991 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1992 cairo_paint(cr);
1993
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001994 width = widget->allocation.width;
1995 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001996 rounded_rect(cr, 0, 0, width, height, r);
1997
1998 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1999 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2000 cairo_fill(cr);
2001
2002 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2003 cairo_move_to(cr, 10, 16);
2004 cairo_show_text(cr, tooltip->entry);
2005 cairo_destroy(cr);
2006}
2007
2008static cairo_text_extents_t
2009get_text_extents(struct tooltip *tooltip)
2010{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002011 cairo_t *cr;
2012 cairo_text_extents_t extents;
2013
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002014 /* Use the dummy_surface because tooltip's surface was not
2015 * created yet, and parent does not have a valid surface
2016 * outside repaint, either.
2017 */
2018 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002019 cairo_text_extents(cr, tooltip->entry, &extents);
2020 cairo_destroy(cr);
2021
2022 return extents;
2023}
2024
2025static int
2026window_create_tooltip(struct tooltip *tooltip)
2027{
2028 struct widget *parent = tooltip->parent;
2029 struct display *display = parent->window->display;
2030 struct window *window;
2031 const int offset_y = 27;
2032 const int margin = 3;
2033 cairo_text_extents_t extents;
2034
2035 if (tooltip->widget)
2036 return 0;
2037
2038 window = window_create_transient(display, parent->window, tooltip->x,
2039 tooltip->y + offset_y,
2040 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2041 if (!window)
2042 return -1;
2043
2044 tooltip->window = window;
2045 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2046
2047 extents = get_text_extents(tooltip);
2048 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2049 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2050
2051 return 0;
2052}
2053
2054void
2055widget_destroy_tooltip(struct widget *parent)
2056{
2057 struct tooltip *tooltip = parent->tooltip;
2058
2059 parent->tooltip_count = 0;
2060 if (!tooltip)
2061 return;
2062
2063 if (tooltip->widget) {
2064 widget_destroy(tooltip->widget);
2065 window_destroy(tooltip->window);
2066 tooltip->widget = NULL;
2067 tooltip->window = NULL;
2068 }
2069
2070 close(tooltip->tooltip_fd);
2071 free(tooltip->entry);
2072 free(tooltip);
2073 parent->tooltip = NULL;
2074}
2075
2076static void
2077tooltip_func(struct task *task, uint32_t events)
2078{
2079 struct tooltip *tooltip =
2080 container_of(task, struct tooltip, tooltip_task);
2081 uint64_t exp;
2082
Martin Olsson8df662a2012-07-08 03:03:47 +02002083 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2084 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002085 window_create_tooltip(tooltip);
2086}
2087
2088#define TOOLTIP_TIMEOUT 500
2089static int
2090tooltip_timer_reset(struct tooltip *tooltip)
2091{
2092 struct itimerspec its;
2093
2094 its.it_interval.tv_sec = 0;
2095 its.it_interval.tv_nsec = 0;
2096 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2097 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2098 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2099 fprintf(stderr, "could not set timerfd\n: %m");
2100 return -1;
2101 }
2102
2103 return 0;
2104}
2105
2106int
2107widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2108{
2109 struct tooltip *tooltip = parent->tooltip;
2110
2111 parent->tooltip_count++;
2112 if (tooltip) {
2113 tooltip->x = x;
2114 tooltip->y = y;
2115 tooltip_timer_reset(tooltip);
2116 return 0;
2117 }
2118
2119 /* the handler might be triggered too fast via input device motion, so
2120 * we need this check here to make sure tooltip is fully initialized */
2121 if (parent->tooltip_count > 1)
2122 return 0;
2123
2124 tooltip = malloc(sizeof *tooltip);
2125 if (!tooltip)
2126 return -1;
2127
2128 parent->tooltip = tooltip;
2129 tooltip->parent = parent;
2130 tooltip->widget = NULL;
2131 tooltip->window = NULL;
2132 tooltip->x = x;
2133 tooltip->y = y;
2134 tooltip->entry = strdup(entry);
2135 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2136 if (tooltip->tooltip_fd < 0) {
2137 fprintf(stderr, "could not create timerfd\n: %m");
2138 return -1;
2139 }
2140
2141 tooltip->tooltip_task.run = tooltip_func;
2142 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2143 EPOLLIN, &tooltip->tooltip_task);
2144 tooltip_timer_reset(tooltip);
2145
2146 return 0;
2147}
2148
2149static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002150workspace_manager_state(void *data,
2151 struct workspace_manager *workspace_manager,
2152 uint32_t current,
2153 uint32_t count)
2154{
2155 struct display *display = data;
2156
2157 display->workspace = current;
2158 display->workspace_count = count;
2159}
2160
2161static const struct workspace_manager_listener workspace_manager_listener = {
2162 workspace_manager_state
2163};
2164
2165static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002166frame_resize_handler(struct widget *widget,
2167 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002168{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002169 struct frame *frame = data;
2170 struct widget *child = frame->child;
2171 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002172 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002173 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002174 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002175 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002176 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002178 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002179
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002180 switch (widget->window->type) {
2181 case TYPE_FULLSCREEN:
2182 decoration_width = 0;
2183 decoration_height = 0;
2184
2185 allocation.x = 0;
2186 allocation.y = 0;
2187 allocation.width = width;
2188 allocation.height = height;
2189 opaque_margin = 0;
2190
2191 wl_list_for_each(button, &frame->buttons_list, link)
2192 button->widget->opaque = 1;
2193 break;
2194 case TYPE_MAXIMIZED:
2195 decoration_width = t->width * 2;
2196 decoration_height = t->width + t->titlebar_height;
2197
2198 allocation.x = t->width;
2199 allocation.y = t->titlebar_height;
2200 allocation.width = width - decoration_width;
2201 allocation.height = height - decoration_height;
2202
2203 opaque_margin = 0;
2204
2205 wl_list_for_each(button, &frame->buttons_list, link)
2206 button->widget->opaque = 0;
2207 break;
2208 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002209 decoration_width = (t->width + t->margin) * 2;
2210 decoration_height = t->width +
2211 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002213 allocation.x = t->width + t->margin;
2214 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002215 allocation.width = width - decoration_width;
2216 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002217
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002218 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002219
2220 wl_list_for_each(button, &frame->buttons_list, link)
2221 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002222 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002223 }
2224
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002225 widget_set_allocation(child, allocation.x, allocation.y,
2226 allocation.width, allocation.height);
2227
2228 if (child->resize_handler)
2229 child->resize_handler(child,
2230 allocation.width,
2231 allocation.height,
2232 child->user_data);
2233
Scott Moreauf7e498c2012-05-14 11:39:29 -06002234 width = child->allocation.width + decoration_width;
2235 height = child->allocation.height + decoration_height;
2236
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002237 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2238
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002239 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002240 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002241 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002242 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002243 shadow_margin, shadow_margin,
2244 width - 2 * shadow_margin,
2245 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002246 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002247 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002248 }
2249
Scott Moreauf7e498c2012-05-14 11:39:29 -06002250 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002251
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002252 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002253 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002254 opaque_margin, opaque_margin,
2255 widget->allocation.width - 2 * opaque_margin,
2256 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002257
2258 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002259 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2260 x_l = t->width + shadow_margin;
2261 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002262 wl_list_for_each(button, &frame->buttons_list, link) {
2263 const int button_padding = 4;
2264 w = cairo_image_surface_get_width(button->icon);
2265 h = cairo_image_surface_get_height(button->icon);
2266
2267 if (button->decoration == FRAME_BUTTON_FANCY)
2268 w += 10;
2269
2270 if (button->align == FRAME_BUTTON_LEFT) {
2271 widget_set_allocation(button->widget,
2272 x_l, y , w + 1, h + 1);
2273 x_l += w;
2274 x_l += button_padding;
2275 } else {
2276 x_r -= w;
2277 widget_set_allocation(button->widget,
2278 x_r, y , w + 1, h + 1);
2279 x_r -= button_padding;
2280 }
2281 }
2282}
2283
2284static int
2285frame_button_enter_handler(struct widget *widget,
2286 struct input *input, float x, float y, void *data)
2287{
2288 struct frame_button *frame_button = data;
2289
2290 widget_schedule_redraw(frame_button->widget);
2291 frame_button->state = FRAME_BUTTON_OVER;
2292
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002293 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002294}
2295
2296static void
2297frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2298{
2299 struct frame_button *frame_button = data;
2300
2301 widget_schedule_redraw(frame_button->widget);
2302 frame_button->state = FRAME_BUTTON_DEFAULT;
2303}
2304
2305static void
2306frame_button_button_handler(struct widget *widget,
2307 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002308 uint32_t button,
2309 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002310{
2311 struct frame_button *frame_button = data;
2312 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002313 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002314
2315 if (button != BTN_LEFT)
2316 return;
2317
2318 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002319 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002320 frame_button->state = FRAME_BUTTON_ACTIVE;
2321 widget_schedule_redraw(frame_button->widget);
2322
2323 if (frame_button->type == FRAME_BUTTON_ICON)
2324 window_show_frame_menu(window, input, time);
2325 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002326 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002327 frame_button->state = FRAME_BUTTON_DEFAULT;
2328 widget_schedule_redraw(frame_button->widget);
2329 break;
2330 }
2331
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002332 if (!was_pressed)
2333 return;
2334
Martin Minarik1998b152012-05-10 02:04:35 +02002335 switch (frame_button->type) {
2336 case FRAME_BUTTON_CLOSE:
2337 if (window->close_handler)
2338 window->close_handler(window->parent,
2339 window->user_data);
2340 else
2341 display_exit(window->display);
2342 break;
2343 case FRAME_BUTTON_MINIMIZE:
2344 fprintf(stderr,"Minimize stub\n");
2345 break;
2346 case FRAME_BUTTON_MAXIMIZE:
2347 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2348 break;
2349 default:
2350 /* Unknown operation */
2351 break;
2352 }
2353}
2354
Rusty Lynch041815a2013-08-08 21:20:38 -07002355static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002356frame_button_touch_down_handler(struct widget *widget, struct input *input,
2357 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch041815a2013-08-08 21:20:38 -07002358 float x, float y, void *data)
2359{
2360 struct frame_button *frame_button = data;
2361 struct window *window = widget->window;
2362
2363 switch (frame_button->type) {
2364 case FRAME_BUTTON_CLOSE:
2365 if (window->close_handler)
2366 window->close_handler(window->parent,
2367 window->user_data);
2368 else
2369 display_exit(window->display);
2370 break;
2371 case FRAME_BUTTON_MINIMIZE:
2372 fprintf(stderr,"Minimize stub\n");
2373 break;
2374 case FRAME_BUTTON_MAXIMIZE:
2375 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2376 break;
2377 default:
2378 /* Unknown operation */
2379 break;
2380 }
2381}
2382
2383
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002384static int
2385frame_button_motion_handler(struct widget *widget,
2386 struct input *input, uint32_t time,
2387 float x, float y, void *data)
2388{
2389 struct frame_button *frame_button = data;
2390 enum frame_button_pointer previous_button_state = frame_button->state;
2391
2392 /* only track state for a pressed button */
2393 if (input->grab != widget)
2394 return CURSOR_LEFT_PTR;
2395
2396 if (x > widget->allocation.x &&
2397 x < (widget->allocation.x + widget->allocation.width) &&
2398 y > widget->allocation.y &&
2399 y < (widget->allocation.y + widget->allocation.height)) {
2400 frame_button->state = FRAME_BUTTON_ACTIVE;
2401 } else {
2402 frame_button->state = FRAME_BUTTON_DEFAULT;
2403 }
2404
2405 if (frame_button->state != previous_button_state)
2406 widget_schedule_redraw(frame_button->widget);
2407
2408 return CURSOR_LEFT_PTR;
2409}
2410
Martin Minarik1998b152012-05-10 02:04:35 +02002411static void
2412frame_button_redraw_handler(struct widget *widget, void *data)
2413{
2414 struct frame_button *frame_button = data;
2415 cairo_t *cr;
2416 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002417
2418 x = widget->allocation.x;
2419 y = widget->allocation.y;
2420 width = widget->allocation.width;
2421 height = widget->allocation.height;
2422
2423 if (!width)
2424 return;
2425 if (!height)
2426 return;
2427 if (widget->opaque)
2428 return;
2429
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002430 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002431
2432 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2433 cairo_set_line_width(cr, 1);
2434
2435 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2436 cairo_rectangle (cr, x, y, 25, 16);
2437
2438 cairo_stroke_preserve(cr);
2439
2440 switch (frame_button->state) {
2441 case FRAME_BUTTON_DEFAULT:
2442 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2443 break;
2444 case FRAME_BUTTON_OVER:
2445 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2446 break;
2447 case FRAME_BUTTON_ACTIVE:
2448 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2449 break;
2450 }
2451
2452 cairo_fill (cr);
2453
2454 x += 4;
2455 }
2456
2457 cairo_set_source_surface(cr, frame_button->icon, x, y);
2458 cairo_paint(cr);
2459
2460 cairo_destroy(cr);
2461}
2462
2463static struct widget *
2464frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2465 enum frame_button_align align, enum frame_button_decoration style)
2466{
2467 struct frame_button *frame_button;
2468 const char *icon = data;
2469
Peter Huttererf3d62272013-08-08 11:57:05 +10002470 frame_button = xzalloc (sizeof *frame_button);
Martin Minarik1998b152012-05-10 02:04:35 +02002471 frame_button->icon = cairo_image_surface_create_from_png(icon);
2472 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2473 frame_button->frame = frame;
2474 frame_button->type = type;
2475 frame_button->align = align;
2476 frame_button->decoration = style;
2477
2478 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2479
2480 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2481 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2482 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
Rusty Lynch041815a2013-08-08 21:20:38 -07002483 widget_set_touch_down_handler(frame_button->widget, frame_button_touch_down_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002484 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002485 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002486 return frame_button->widget;
2487}
2488
2489static void
2490frame_button_destroy(struct frame_button *frame_button)
2491{
2492 widget_destroy(frame_button->widget);
2493 wl_list_remove(&frame_button->link);
2494 cairo_surface_destroy(frame_button->icon);
2495 free(frame_button);
2496
2497 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498}
2499
2500static void
2501frame_redraw_handler(struct widget *widget, void *data)
2502{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002503 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002504 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002505 struct theme *t = window->display->theme;
2506 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002507
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002508 if (window->type == TYPE_FULLSCREEN)
2509 return;
2510
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002511 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002512
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002513 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002514 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002515 if (window->type == TYPE_MAXIMIZED)
2516 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002517 theme_render_frame(t, cr, widget->allocation.width,
2518 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002519
2520 cairo_destroy(cr);
2521}
2522
2523static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002524frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002525{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002526 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002527 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002528 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002529
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002530 if (window->type != TYPE_TOPLEVEL)
2531 return CURSOR_LEFT_PTR;
2532
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002533 location = theme_get_location(t, input->sx, input->sy,
2534 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002535 frame->widget->allocation.height,
2536 window->type == TYPE_MAXIMIZED ?
2537 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002538
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002539 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002540 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002541 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002542 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002543 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002544 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002545 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002546 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002547 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002548 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002549 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002550 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002551 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002552 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002553 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002554 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002555 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002556 case THEME_LOCATION_EXTERIOR:
2557 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002558 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002559 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002560 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002561}
2562
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002563static void
2564frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002565{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002566 struct display *display;
2567
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002568 switch (index) {
2569 case 0: /* close */
2570 if (window->close_handler)
2571 window->close_handler(window->parent,
2572 window->user_data);
2573 else
2574 display_exit(window->display);
2575 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002576 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002577 display = window->display;
2578 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002579 workspace_manager_move_surface(
2580 display->workspace_manager,
2581 window->main_surface->surface,
2582 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002583 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002584 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002585 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002586 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002587 workspace_manager_move_surface(
2588 display->workspace_manager,
2589 window->main_surface->surface,
2590 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002591 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002592 case 3: /* fullscreen */
2593 /* we don't have a way to get out of fullscreen for now */
2594 if (window->fullscreen_handler)
2595 window->fullscreen_handler(window, window->user_data);
2596 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002597 }
2598}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002599
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002600void
2601window_show_frame_menu(struct window *window,
2602 struct input *input, uint32_t time)
2603{
2604 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002605 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002606
2607 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002608 "Close",
2609 "Move to workspace above", "Move to workspace below",
2610 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002611 };
2612
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002613 if (window->fullscreen_handler)
2614 count = ARRAY_LENGTH(entries);
2615 else
2616 count = ARRAY_LENGTH(entries) - 1;
2617
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002618 input_get_position(input, &x, &y);
2619 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002620 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002621}
2622
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002623static int
2624frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002625 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002626{
2627 return frame_get_pointer_image_for_location(data, input);
2628}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002629
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002630static int
2631frame_motion_handler(struct widget *widget,
2632 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002633 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002634{
2635 return frame_get_pointer_image_for_location(data, input);
2636}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002637
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002638static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002639frame_button_handler(struct widget *widget,
2640 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002641 uint32_t button, enum wl_pointer_button_state state,
2642 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002643
2644{
2645 struct frame *frame = data;
2646 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002647 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002648 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002649
Rob Bradford3c9d9672013-07-11 10:35:55 +01002650 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002651 return;
2652
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002653 location = theme_get_location(display->theme, input->sx, input->sy,
2654 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002655 frame->widget->allocation.height,
2656 window->type == TYPE_MAXIMIZED ?
2657 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002658
Daniel Stone4dbadb12012-05-30 16:31:51 +01002659 if (window->display->shell && button == BTN_LEFT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002660 window->type == TYPE_TOPLEVEL) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002661 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002662 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002663 if (!window->shell_surface)
2664 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002665 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002666 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002667 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002668 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002669 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002670 case THEME_LOCATION_RESIZING_TOP:
2671 case THEME_LOCATION_RESIZING_BOTTOM:
2672 case THEME_LOCATION_RESIZING_LEFT:
2673 case THEME_LOCATION_RESIZING_RIGHT:
2674 case THEME_LOCATION_RESIZING_TOP_LEFT:
2675 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2676 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2677 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002678 if (!window->shell_surface)
2679 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002680 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002681
Pekka Paalanen99436862012-11-19 17:15:59 +02002682 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002683 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002684 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002685 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002686 break;
2687 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002688 } else if (button == BTN_RIGHT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002689 (window->type == TYPE_TOPLEVEL ||
2690 window->type == TYPE_MAXIMIZED)) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002691 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002692 }
2693}
2694
Rusty Lynch1084da52013-08-15 09:10:08 -07002695static void
2696frame_touch_down_handler(struct widget *widget, struct input *input,
2697 uint32_t serial, uint32_t time, int32_t id,
2698 float x, float y, void *data)
2699{
2700 struct window *window = widget->window;
2701 struct display *display = window->display;
2702
2703 wl_shell_surface_move(window->shell_surface,
2704 input_get_seat(input),
2705 display->serial);
2706}
2707
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002708struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002709window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002710{
2711 struct frame *frame;
2712
Peter Huttererf3d62272013-08-08 11:57:05 +10002713 frame = xzalloc(sizeof *frame);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002714 frame->widget = window_add_widget(window, frame);
2715 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002716
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002717 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2718 widget_set_resize_handler(frame->widget, frame_resize_handler);
2719 widget_set_enter_handler(frame->widget, frame_enter_handler);
2720 widget_set_motion_handler(frame->widget, frame_motion_handler);
2721 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002722 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002723
Martin Minarik1998b152012-05-10 02:04:35 +02002724 /* Create empty list for frame buttons */
2725 wl_list_init(&frame->buttons_list);
2726
2727 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2728 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2729
2730 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2731 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2732
2733 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2734 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2735
2736 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2737 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2738
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002739 window->frame = frame;
2740
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002741 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002742}
2743
Kristian Høgsberga1627922012-06-20 17:30:03 -04002744void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002745window_frame_set_child_size(struct widget *widget, int child_width,
2746 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002747{
2748 struct display *display = widget->window->display;
2749 struct theme *t = display->theme;
2750 int decoration_width, decoration_height;
2751 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002752 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002753
2754 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002755 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002756 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002757 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002758
2759 width = child_width + decoration_width;
2760 height = child_height + decoration_height;
2761 } else {
2762 width = child_width;
2763 height = child_height;
2764 }
2765
2766 window_schedule_resize(widget->window, width, height);
2767}
2768
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002769static void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002770window_frame_destroy(struct frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002771{
Martin Minarik1998b152012-05-10 02:04:35 +02002772 struct frame_button *button, *tmp;
2773
2774 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2775 frame_button_destroy(button);
2776
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002777 /* frame->child must be destroyed by the application */
2778 widget_destroy(frame->widget);
2779 free(frame);
2780}
2781
2782static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002783input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002784 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002785{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002786 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002787 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002788
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002789 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002790 return;
2791
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002792 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002793 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002794 widget = old;
2795 if (input->grab)
2796 widget = input->grab;
2797 if (widget->leave_handler)
2798 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002799 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002800 }
2801
2802 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002803 widget = focus;
2804 if (input->grab)
2805 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002806 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002807 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002808 cursor = widget->enter_handler(focus, input, x, y,
2809 widget->user_data);
2810 else
2811 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002812
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002813 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002814 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002815}
2816
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002817void
2818input_grab(struct input *input, struct widget *widget, uint32_t button)
2819{
2820 input->grab = widget;
2821 input->grab_button = button;
2822}
2823
2824void
2825input_ungrab(struct input *input)
2826{
2827 struct widget *widget;
2828
2829 input->grab = NULL;
2830 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002831 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002832 input->sx, input->sy);
2833 input_set_focus_widget(input, widget, input->sx, input->sy);
2834 }
2835}
2836
2837static void
2838input_remove_pointer_focus(struct input *input)
2839{
2840 struct window *window = input->pointer_focus;
2841
2842 if (!window)
2843 return;
2844
2845 input_set_focus_widget(input, NULL, 0, 0);
2846
2847 input->pointer_focus = NULL;
2848 input->current_cursor = CURSOR_UNSET;
2849}
2850
2851static void
2852pointer_handle_enter(void *data, struct wl_pointer *pointer,
2853 uint32_t serial, struct wl_surface *surface,
2854 wl_fixed_t sx_w, wl_fixed_t sy_w)
2855{
2856 struct input *input = data;
2857 struct window *window;
2858 struct widget *widget;
2859 float sx = wl_fixed_to_double(sx_w);
2860 float sy = wl_fixed_to_double(sy_w);
2861
2862 if (!surface) {
2863 /* enter event for a window we've just destroyed */
2864 return;
2865 }
2866
2867 input->display->serial = serial;
2868 input->pointer_enter_serial = serial;
2869 input->pointer_focus = wl_surface_get_user_data(surface);
2870 window = input->pointer_focus;
2871
Pekka Paalanen99436862012-11-19 17:15:59 +02002872 if (window->resizing) {
2873 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002874 /* Schedule a redraw to free the pool */
2875 window_schedule_redraw(window);
2876 }
2877
2878 input->sx = sx;
2879 input->sy = sy;
2880
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002881 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002882 input_set_focus_widget(input, widget, sx, sy);
2883}
2884
2885static void
2886pointer_handle_leave(void *data, struct wl_pointer *pointer,
2887 uint32_t serial, struct wl_surface *surface)
2888{
2889 struct input *input = data;
2890
2891 input->display->serial = serial;
2892 input_remove_pointer_focus(input);
2893}
2894
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002895static void
Daniel Stone37816df2012-05-16 18:45:18 +01002896pointer_handle_motion(void *data, struct wl_pointer *pointer,
2897 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002898{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002899 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002900 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002901 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002902 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002903 float sx = wl_fixed_to_double(sx_w);
2904 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002905
Paul Winwoodb22bf572013-08-29 10:52:54 +01002906 input->sx = sx;
2907 input->sy = sy;
2908
2909 if (!window)
2910 return;
2911
Rob Bradford5f087742013-07-11 19:41:27 +01002912 /* when making the window smaller - e.g. after a unmaximise we might
2913 * still have a pending motion event that the compositor has picked
2914 * based on the old surface dimensions
2915 */
2916 if (sx > window->main_surface->allocation.width ||
2917 sy > window->main_surface->allocation.height)
2918 return;
2919
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002920 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002921 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002922 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002923 }
2924
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002925 if (input->grab)
2926 widget = input->grab;
2927 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002928 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002929 if (widget) {
2930 if (widget->motion_handler)
2931 cursor = widget->motion_handler(input->focus_widget,
2932 input, time, sx, sy,
2933 widget->user_data);
2934 else
2935 cursor = widget->default_cursor;
2936 } else
2937 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002938
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002939 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002940}
2941
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002942static void
Daniel Stone37816df2012-05-16 18:45:18 +01002943pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002944 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002945{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002946 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002947 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002948 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002949
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002950 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002951 if (input->focus_widget && input->grab == NULL &&
2952 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002953 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002954
Neil Roberts6b28aad2012-01-23 19:11:18 +00002955 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002956 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002957 (*widget->button_handler)(widget,
2958 input, time,
2959 button, state,
2960 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002961
Daniel Stone4dbadb12012-05-30 16:31:51 +01002962 if (input->grab && input->grab_button == button &&
2963 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002964 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002965}
2966
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002967static void
Daniel Stone37816df2012-05-16 18:45:18 +01002968pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002969 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002970{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002971 struct input *input = data;
2972 struct widget *widget;
2973
2974 widget = input->focus_widget;
2975 if (input->grab)
2976 widget = input->grab;
2977 if (widget && widget->axis_handler)
2978 (*widget->axis_handler)(widget,
2979 input, time,
2980 axis, value,
2981 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002982}
2983
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002984static const struct wl_pointer_listener pointer_listener = {
2985 pointer_handle_enter,
2986 pointer_handle_leave,
2987 pointer_handle_motion,
2988 pointer_handle_button,
2989 pointer_handle_axis,
2990};
2991
2992static void
2993input_remove_keyboard_focus(struct input *input)
2994{
2995 struct window *window = input->keyboard_focus;
2996 struct itimerspec its;
2997
2998 its.it_interval.tv_sec = 0;
2999 its.it_interval.tv_nsec = 0;
3000 its.it_value.tv_sec = 0;
3001 its.it_value.tv_nsec = 0;
3002 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3003
3004 if (!window)
3005 return;
3006
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003007 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003008 if (window->keyboard_focus_handler)
3009 (*window->keyboard_focus_handler)(window, NULL,
3010 window->user_data);
3011
3012 input->keyboard_focus = NULL;
3013}
3014
3015static void
3016keyboard_repeat_func(struct task *task, uint32_t events)
3017{
3018 struct input *input =
3019 container_of(task, struct input, repeat_task);
3020 struct window *window = input->keyboard_focus;
3021 uint64_t exp;
3022
3023 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
3024 /* If we change the timer between the fd becoming
3025 * readable and getting here, there'll be nothing to
3026 * read and we get EAGAIN. */
3027 return;
3028
3029 if (window && window->key_handler) {
3030 (*window->key_handler)(window, input, input->repeat_time,
3031 input->repeat_key, input->repeat_sym,
3032 WL_KEYBOARD_KEY_STATE_PRESSED,
3033 window->user_data);
3034 }
3035}
3036
3037static void
3038keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
3039 uint32_t format, int fd, uint32_t size)
3040{
3041 struct input *input = data;
3042 char *map_str;
3043
3044 if (!data) {
3045 close(fd);
3046 return;
3047 }
3048
3049 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3050 close(fd);
3051 return;
3052 }
3053
3054 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3055 if (map_str == MAP_FAILED) {
3056 close(fd);
3057 return;
3058 }
3059
3060 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
3061 map_str,
3062 XKB_KEYMAP_FORMAT_TEXT_V1,
3063 0);
3064 munmap(map_str, size);
3065 close(fd);
3066
3067 if (!input->xkb.keymap) {
3068 fprintf(stderr, "failed to compile keymap\n");
3069 return;
3070 }
3071
3072 input->xkb.state = xkb_state_new(input->xkb.keymap);
3073 if (!input->xkb.state) {
3074 fprintf(stderr, "failed to create XKB state\n");
3075 xkb_map_unref(input->xkb.keymap);
3076 input->xkb.keymap = NULL;
3077 return;
3078 }
3079
3080 input->xkb.control_mask =
3081 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
3082 input->xkb.alt_mask =
3083 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
3084 input->xkb.shift_mask =
3085 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
3086}
3087
3088static void
3089keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3090 uint32_t serial, struct wl_surface *surface,
3091 struct wl_array *keys)
3092{
3093 struct input *input = data;
3094 struct window *window;
3095
3096 input->display->serial = serial;
3097 input->keyboard_focus = wl_surface_get_user_data(surface);
3098
3099 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003100 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003101 if (window->keyboard_focus_handler)
3102 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003103 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003104}
3105
3106static void
3107keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3108 uint32_t serial, struct wl_surface *surface)
3109{
3110 struct input *input = data;
3111
3112 input->display->serial = serial;
3113 input_remove_keyboard_focus(input);
3114}
3115
Scott Moreau210d0792012-03-22 10:47:01 -06003116static void
Daniel Stone37816df2012-05-16 18:45:18 +01003117keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003118 uint32_t serial, uint32_t time, uint32_t key,
3119 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003120{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003121 struct input *input = data;
3122 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003123 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003124 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003125 const xkb_keysym_t *syms;
3126 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003127 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003128
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003129 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003130 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003131 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003132 return;
3133
Daniel Stone97f68542012-05-30 16:32:01 +01003134 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003135
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003136 sym = XKB_KEY_NoSymbol;
3137 if (num_syms == 1)
3138 sym = syms[0];
3139
3140 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003141 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003142 window_set_maximized(window,
3143 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003144 } else if (sym == XKB_KEY_F11 &&
3145 window->fullscreen_handler &&
3146 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3147 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003148 } else if (sym == XKB_KEY_F4 &&
3149 input->modifiers == MOD_ALT_MASK &&
3150 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3151 if (window->close_handler)
3152 window->close_handler(window->parent,
3153 window->user_data);
3154 else
3155 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003156 } else if (window->key_handler) {
3157 (*window->key_handler)(window, input, time, key,
3158 sym, state, window->user_data);
3159 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003160
3161 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3162 key == input->repeat_key) {
3163 its.it_interval.tv_sec = 0;
3164 its.it_interval.tv_nsec = 0;
3165 its.it_value.tv_sec = 0;
3166 its.it_value.tv_nsec = 0;
3167 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3168 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3169 input->repeat_sym = sym;
3170 input->repeat_key = key;
3171 input->repeat_time = time;
3172 its.it_interval.tv_sec = 0;
3173 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3174 its.it_value.tv_sec = 0;
3175 its.it_value.tv_nsec = 400 * 1000 * 1000;
3176 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3177 }
3178}
3179
3180static void
Daniel Stone351eb612012-05-31 15:27:47 -04003181keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3182 uint32_t serial, uint32_t mods_depressed,
3183 uint32_t mods_latched, uint32_t mods_locked,
3184 uint32_t group)
3185{
3186 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003187 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003188
Matt Ropere61561f2013-06-24 16:52:43 +01003189 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3190 if (!input->xkb.keymap)
3191 return;
3192
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003193 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3194 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003195 mask = xkb_state_serialize_mods(input->xkb.state,
3196 XKB_STATE_DEPRESSED |
3197 XKB_STATE_LATCHED);
3198 input->modifiers = 0;
3199 if (mask & input->xkb.control_mask)
3200 input->modifiers |= MOD_CONTROL_MASK;
3201 if (mask & input->xkb.alt_mask)
3202 input->modifiers |= MOD_ALT_MASK;
3203 if (mask & input->xkb.shift_mask)
3204 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003205}
3206
Daniel Stone37816df2012-05-16 18:45:18 +01003207static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003208 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003209 keyboard_handle_enter,
3210 keyboard_handle_leave,
3211 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003212 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003213};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003214
3215static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003216touch_handle_down(void *data, struct wl_touch *wl_touch,
3217 uint32_t serial, uint32_t time, struct wl_surface *surface,
3218 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3219{
3220 struct input *input = data;
3221 struct widget *widget;
3222 float sx = wl_fixed_to_double(x_w);
3223 float sy = wl_fixed_to_double(y_w);
3224
Rusty Lynch1084da52013-08-15 09:10:08 -07003225 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003226 input->touch_focus = wl_surface_get_user_data(surface);
3227 if (!input->touch_focus) {
3228 DBG("Failed to find to touch focus for surface %p\n", surface);
3229 return;
3230 }
3231
3232 widget = window_find_widget(input->touch_focus,
3233 wl_fixed_to_double(x_w),
3234 wl_fixed_to_double(y_w));
3235 if (widget) {
3236 struct touch_point *tp = xmalloc(sizeof *tp);
3237 if (tp) {
3238 tp->id = id;
3239 tp->widget = widget;
3240 wl_list_insert(&input->touch_point_list, &tp->link);
3241
3242 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003243 (*widget->touch_down_handler)(widget, input,
3244 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003245 sx, sy,
3246 widget->user_data);
3247 }
3248 }
3249}
3250
3251static void
3252touch_handle_up(void *data, struct wl_touch *wl_touch,
3253 uint32_t serial, uint32_t time, int32_t id)
3254{
3255 struct input *input = data;
3256 struct touch_point *tp, *tmp;
3257
Rusty Lynch041815a2013-08-08 21:20:38 -07003258 if (!input->touch_focus) {
3259 DBG("No touch focus found for touch up event!\n");
3260 return;
3261 }
3262
3263 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3264 if (tp->id != id)
3265 continue;
3266
3267 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003268 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003269 time, id,
3270 tp->widget->user_data);
3271
3272 wl_list_remove(&tp->link);
3273 free(tp);
3274
3275 return;
3276 }
3277}
3278
3279static void
3280touch_handle_motion(void *data, struct wl_touch *wl_touch,
3281 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3282{
3283 struct input *input = data;
3284 struct touch_point *tp;
3285 float sx = wl_fixed_to_double(x_w);
3286 float sy = wl_fixed_to_double(y_w);
3287
3288 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3289
3290 if (!input->touch_focus) {
3291 DBG("No touch focus found for touch motion event!\n");
3292 return;
3293 }
3294
3295 wl_list_for_each(tp, &input->touch_point_list, link) {
3296 if (tp->id != id)
3297 continue;
3298
3299 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003300 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003301 id, sx, sy,
3302 tp->widget->user_data);
3303 return;
3304 }
3305}
3306
3307static void
3308touch_handle_frame(void *data, struct wl_touch *wl_touch)
3309{
3310 struct input *input = data;
3311 struct touch_point *tp, *tmp;
3312
3313 DBG("touch_handle_frame\n");
3314
3315 if (!input->touch_focus) {
3316 DBG("No touch focus found for touch frame event!\n");
3317 return;
3318 }
3319
3320 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3321 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003322 (*tp->widget->touch_frame_handler)(tp->widget, input,
3323 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003324
3325 wl_list_remove(&tp->link);
3326 free(tp);
3327 }
3328}
3329
3330static void
3331touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3332{
3333 struct input *input = data;
3334 struct touch_point *tp, *tmp;
3335
3336 DBG("touch_handle_cancel\n");
3337
3338 if (!input->touch_focus) {
3339 DBG("No touch focus found for touch cancel event!\n");
3340 return;
3341 }
3342
3343 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3344 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003345 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3346 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003347
3348 wl_list_remove(&tp->link);
3349 free(tp);
3350 }
3351}
3352
3353static const struct wl_touch_listener touch_listener = {
3354 touch_handle_down,
3355 touch_handle_up,
3356 touch_handle_motion,
3357 touch_handle_frame,
3358 touch_handle_cancel,
3359};
3360
3361static void
Daniel Stone37816df2012-05-16 18:45:18 +01003362seat_handle_capabilities(void *data, struct wl_seat *seat,
3363 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003364{
Daniel Stone37816df2012-05-16 18:45:18 +01003365 struct input *input = data;
3366
3367 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3368 input->pointer = wl_seat_get_pointer(seat);
3369 wl_pointer_set_user_data(input->pointer, input);
3370 wl_pointer_add_listener(input->pointer, &pointer_listener,
3371 input);
3372 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3373 wl_pointer_destroy(input->pointer);
3374 input->pointer = NULL;
3375 }
3376
3377 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3378 input->keyboard = wl_seat_get_keyboard(seat);
3379 wl_keyboard_set_user_data(input->keyboard, input);
3380 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3381 input);
3382 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3383 wl_keyboard_destroy(input->keyboard);
3384 input->keyboard = NULL;
3385 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003386
3387 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3388 input->touch = wl_seat_get_touch(seat);
3389 wl_touch_set_user_data(input->touch, input);
3390 wl_touch_add_listener(input->touch, &touch_listener, input);
3391 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3392 wl_touch_destroy(input->touch);
3393 input->touch = NULL;
3394 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003395}
3396
Rob Bradford08031182013-08-13 20:11:03 +01003397static void
3398seat_handle_name(void *data, struct wl_seat *seat,
3399 const char *name)
3400{
3401
3402}
3403
Daniel Stone37816df2012-05-16 18:45:18 +01003404static const struct wl_seat_listener seat_listener = {
3405 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003406 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003407};
3408
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003409void
3410input_get_position(struct input *input, int32_t *x, int32_t *y)
3411{
3412 *x = input->sx;
3413 *y = input->sy;
3414}
3415
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003416struct display *
3417input_get_display(struct input *input)
3418{
3419 return input->display;
3420}
3421
Daniel Stone37816df2012-05-16 18:45:18 +01003422struct wl_seat *
3423input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003424{
Daniel Stone37816df2012-05-16 18:45:18 +01003425 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003426}
3427
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003428uint32_t
3429input_get_modifiers(struct input *input)
3430{
3431 return input->modifiers;
3432}
3433
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003434struct widget *
3435input_get_focus_widget(struct input *input)
3436{
3437 return input->focus_widget;
3438}
3439
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003440struct data_offer {
3441 struct wl_data_offer *offer;
3442 struct input *input;
3443 struct wl_array types;
3444 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003445
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003446 struct task io_task;
3447 int fd;
3448 data_func_t func;
3449 int32_t x, y;
3450 void *user_data;
3451};
3452
3453static void
3454data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3455{
3456 struct data_offer *offer = data;
3457 char **p;
3458
3459 p = wl_array_add(&offer->types, sizeof *p);
3460 *p = strdup(type);
3461}
3462
3463static const struct wl_data_offer_listener data_offer_listener = {
3464 data_offer_offer,
3465};
3466
3467static void
3468data_offer_destroy(struct data_offer *offer)
3469{
3470 char **p;
3471
3472 offer->refcount--;
3473 if (offer->refcount == 0) {
3474 wl_data_offer_destroy(offer->offer);
3475 for (p = offer->types.data; *p; p++)
3476 free(*p);
3477 wl_array_release(&offer->types);
3478 free(offer);
3479 }
3480}
3481
3482static void
3483data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003484 struct wl_data_device *data_device,
3485 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003486{
3487 struct data_offer *offer;
3488
Brian Lovinbc919262013-08-07 15:34:59 -07003489 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003490
3491 wl_array_init(&offer->types);
3492 offer->refcount = 1;
3493 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003494 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003495 wl_data_offer_add_listener(offer->offer,
3496 &data_offer_listener, offer);
3497}
3498
3499static void
3500data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003501 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003502 wl_fixed_t x_w, wl_fixed_t y_w,
3503 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003504{
3505 struct input *input = data;
3506 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003507 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003508 float x = wl_fixed_to_double(x_w);
3509 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003510 char **p;
3511
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003512 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003513 window = wl_surface_get_user_data(surface);
3514 input->pointer_focus = window;
3515
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003516 if (offer) {
3517 input->drag_offer = wl_data_offer_get_user_data(offer);
3518
3519 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3520 *p = NULL;
3521
3522 types_data = input->drag_offer->types.data;
3523 } else {
3524 input->drag_offer = NULL;
3525 types_data = NULL;
3526 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003527
3528 window = input->pointer_focus;
3529 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003530 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003531 window->user_data);
3532}
3533
3534static void
3535data_device_leave(void *data, struct wl_data_device *data_device)
3536{
3537 struct input *input = data;
3538
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003539 if (input->drag_offer) {
3540 data_offer_destroy(input->drag_offer);
3541 input->drag_offer = NULL;
3542 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003543}
3544
3545static void
3546data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003547 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003548{
3549 struct input *input = data;
3550 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003551 float x = wl_fixed_to_double(x_w);
3552 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003553 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003554
3555 input->sx = x;
3556 input->sy = y;
3557
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003558 if (input->drag_offer)
3559 types_data = input->drag_offer->types.data;
3560 else
3561 types_data = NULL;
3562
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003563 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003564 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003565 window->user_data);
3566}
3567
3568static void
3569data_device_drop(void *data, struct wl_data_device *data_device)
3570{
3571 struct input *input = data;
3572 struct window *window = input->pointer_focus;
3573
3574 if (window->drop_handler)
3575 window->drop_handler(window, input,
3576 input->sx, input->sy, window->user_data);
3577}
3578
3579static void
3580data_device_selection(void *data,
3581 struct wl_data_device *wl_data_device,
3582 struct wl_data_offer *offer)
3583{
3584 struct input *input = data;
3585 char **p;
3586
3587 if (input->selection_offer)
3588 data_offer_destroy(input->selection_offer);
3589
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003590 if (offer) {
3591 input->selection_offer = wl_data_offer_get_user_data(offer);
3592 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3593 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003594 } else {
3595 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003596 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003597}
3598
3599static const struct wl_data_device_listener data_device_listener = {
3600 data_device_data_offer,
3601 data_device_enter,
3602 data_device_leave,
3603 data_device_motion,
3604 data_device_drop,
3605 data_device_selection
3606};
3607
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003608static void
3609input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003610{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003611 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003612 struct wl_cursor *cursor;
3613 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003614
Daniel Stone80972742012-11-07 17:51:39 +11003615 if (!input->pointer)
3616 return;
3617
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003618 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003619 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003620 return;
3621
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003622 if (index >= (int) cursor->image_count) {
3623 fprintf(stderr, "cursor index out of range\n");
3624 return;
3625 }
3626
3627 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003628 buffer = wl_cursor_image_get_buffer(image);
3629 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003630 return;
3631
Kristian Høgsbergae277372012-08-01 09:41:08 -04003632 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003633 input->pointer_surface,
3634 image->hotspot_x, image->hotspot_y);
3635 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3636 wl_surface_damage(input->pointer_surface, 0, 0,
3637 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003638 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003639}
3640
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003641static const struct wl_callback_listener pointer_surface_listener;
3642
3643static void
3644pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3645 uint32_t time)
3646{
3647 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003648 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003649 int i;
3650
3651 if (callback) {
3652 assert(callback == input->cursor_frame_cb);
3653 wl_callback_destroy(callback);
3654 input->cursor_frame_cb = NULL;
3655 }
3656
Daniel Stone80972742012-11-07 17:51:39 +11003657 if (!input->pointer)
3658 return;
3659
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003660 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003661 wl_pointer_set_cursor(input->pointer,
3662 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003663 NULL, 0, 0);
3664 return;
3665 }
3666
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003667 if (input->current_cursor == CURSOR_UNSET)
3668 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003669 cursor = input->display->cursors[input->current_cursor];
3670 if (!cursor)
3671 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003672
3673 /* FIXME We don't have the current time on the first call so we set
3674 * the animation start to the time of the first frame callback. */
3675 if (time == 0)
3676 input->cursor_anim_start = 0;
3677 else if (input->cursor_anim_start == 0)
3678 input->cursor_anim_start = time;
3679
3680 if (time == 0 || input->cursor_anim_start == 0)
3681 i = 0;
3682 else
3683 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3684
Pekka Paalanenbc106382012-10-10 12:49:31 +03003685 if (cursor->image_count > 1) {
3686 input->cursor_frame_cb =
3687 wl_surface_frame(input->pointer_surface);
3688 wl_callback_add_listener(input->cursor_frame_cb,
3689 &pointer_surface_listener, input);
3690 }
3691
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003692 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003693}
3694
3695static const struct wl_callback_listener pointer_surface_listener = {
3696 pointer_surface_frame_callback
3697};
3698
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003699void
3700input_set_pointer_image(struct input *input, int pointer)
3701{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003702 int force = 0;
3703
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003704 if (!input->pointer)
3705 return;
3706
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003707 if (input->pointer_enter_serial > input->cursor_serial)
3708 force = 1;
3709
3710 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003711 return;
3712
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003713 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003714 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003715 if (!input->cursor_frame_cb)
3716 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003717 else if (force) {
3718 /* The current frame callback may be stuck if, for instance,
3719 * the set cursor request was processed by the server after
3720 * this client lost the focus. In this case the cursor surface
3721 * might not be mapped and the frame callback wouldn't ever
3722 * complete. Send a set_cursor and attach to try to map the
3723 * cursor surface again so that the callback will finish */
3724 input_set_pointer_image_index(input, 0);
3725 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003726}
3727
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003728struct wl_data_device *
3729input_get_data_device(struct input *input)
3730{
3731 return input->data_device;
3732}
3733
3734void
3735input_set_selection(struct input *input,
3736 struct wl_data_source *source, uint32_t time)
3737{
3738 wl_data_device_set_selection(input->data_device, source, time);
3739}
3740
3741void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003742input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003743{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003744 wl_data_offer_accept(input->drag_offer->offer,
3745 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003746}
3747
3748static void
3749offer_io_func(struct task *task, uint32_t events)
3750{
3751 struct data_offer *offer =
3752 container_of(task, struct data_offer, io_task);
3753 unsigned int len;
3754 char buffer[4096];
3755
3756 len = read(offer->fd, buffer, sizeof buffer);
3757 offer->func(buffer, len,
3758 offer->x, offer->y, offer->user_data);
3759
3760 if (len == 0) {
3761 close(offer->fd);
3762 data_offer_destroy(offer);
3763 }
3764}
3765
3766static void
3767data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3768 data_func_t func, void *user_data)
3769{
3770 int p[2];
3771
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003772 if (pipe2(p, O_CLOEXEC) == -1)
3773 return;
3774
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003775 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3776 close(p[1]);
3777
3778 offer->io_task.run = offer_io_func;
3779 offer->fd = p[0];
3780 offer->func = func;
3781 offer->refcount++;
3782 offer->user_data = user_data;
3783
3784 display_watch_fd(offer->input->display,
3785 offer->fd, EPOLLIN, &offer->io_task);
3786}
3787
3788void
3789input_receive_drag_data(struct input *input, const char *mime_type,
3790 data_func_t func, void *data)
3791{
3792 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3793 input->drag_offer->x = input->sx;
3794 input->drag_offer->y = input->sy;
3795}
3796
3797int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003798input_receive_drag_data_to_fd(struct input *input,
3799 const char *mime_type, int fd)
3800{
3801 if (input->drag_offer)
3802 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3803
3804 return 0;
3805}
3806
3807int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003808input_receive_selection_data(struct input *input, const char *mime_type,
3809 data_func_t func, void *data)
3810{
3811 char **p;
3812
3813 if (input->selection_offer == NULL)
3814 return -1;
3815
3816 for (p = input->selection_offer->types.data; *p; p++)
3817 if (strcmp(mime_type, *p) == 0)
3818 break;
3819
3820 if (*p == NULL)
3821 return -1;
3822
3823 data_offer_receive_data(input->selection_offer,
3824 mime_type, func, data);
3825 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003826}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003827
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003828int
3829input_receive_selection_data_to_fd(struct input *input,
3830 const char *mime_type, int fd)
3831{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003832 if (input->selection_offer)
3833 wl_data_offer_receive(input->selection_offer->offer,
3834 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003835
3836 return 0;
3837}
3838
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003839void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003840window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003841{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003842 if (!window->shell_surface)
3843 return;
3844
Daniel Stone37816df2012-05-16 18:45:18 +01003845 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003846}
3847
Rusty Lynch1084da52013-08-15 09:10:08 -07003848void
3849window_touch_move(struct window *window, struct input *input, uint32_t serial)
3850{
3851 if (!window->shell_surface)
3852 return;
3853
3854 wl_shell_surface_move(window->shell_surface, input->seat,
3855 window->display->serial);
3856}
3857
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003858static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003859surface_set_synchronized(struct surface *surface)
3860{
3861 if (!surface->subsurface)
3862 return;
3863
3864 if (surface->synchronized)
3865 return;
3866
3867 wl_subsurface_set_sync(surface->subsurface);
3868 surface->synchronized = 1;
3869}
3870
3871static void
3872surface_set_synchronized_default(struct surface *surface)
3873{
3874 if (!surface->subsurface)
3875 return;
3876
3877 if (surface->synchronized == surface->synchronized_default)
3878 return;
3879
3880 if (surface->synchronized_default)
3881 wl_subsurface_set_sync(surface->subsurface);
3882 else
3883 wl_subsurface_set_desync(surface->subsurface);
3884
3885 surface->synchronized = surface->synchronized_default;
3886}
3887
3888static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003889surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003890{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003891 struct widget *widget = surface->widget;
3892 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003893
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003894 if (surface->input_region) {
3895 wl_region_destroy(surface->input_region);
3896 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003897 }
3898
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003899 if (surface->opaque_region)
3900 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003901
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003902 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003903
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003904 if (widget->resize_handler)
3905 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003906 widget->allocation.width,
3907 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003908 widget->user_data);
3909
Pekka Paalanen35e82632013-04-25 13:57:48 +03003910 if (surface->subsurface &&
3911 (surface->allocation.x != widget->allocation.x ||
3912 surface->allocation.y != widget->allocation.y)) {
3913 wl_subsurface_set_position(surface->subsurface,
3914 widget->allocation.x,
3915 widget->allocation.y);
3916 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003917 if (surface->allocation.width != widget->allocation.width ||
3918 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003919 window_schedule_redraw(widget->window);
3920 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003921 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003922
3923 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003924 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003925 widget->allocation.width,
3926 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003927}
3928
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003929static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003930hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3931{
3932 /*
3933 * This hack should be removed, when EGL respects
3934 * eglSwapInterval(0).
3935 *
3936 * If this window has sub-surfaces, especially a free-running
3937 * EGL-widget, we need to post the parent surface once with
3938 * all the old state to guarantee, that the EGL-widget will
3939 * receive its frame callback soon. Otherwise, a forced call
3940 * to eglSwapBuffers may end up blocking, waiting for a frame
3941 * event that will never come, because we will commit the parent
3942 * surface with all new state only after eglSwapBuffers returns.
3943 *
3944 * This assumes, that:
3945 * 1. When the EGL widget's resize hook is called, it pauses.
3946 * 2. When the EGL widget's redraw hook is called, it forces a
3947 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3948 * In a single threaded application condition 1 is a no-op.
3949 *
3950 * XXX: This should actually be after the surface_resize() calls,
3951 * but cannot, because then it would commit the incomplete state
3952 * accumulated from the widget resize hooks.
3953 */
3954 if (window->subsurface_list.next != &window->main_surface->link ||
3955 window->subsurface_list.prev != &window->main_surface->link)
3956 wl_surface_commit(window->main_surface->surface);
3957}
3958
3959static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003960idle_resize(struct window *window)
3961{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003962 struct surface *surface;
3963
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003964 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003965 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003966
Pekka Paalanen71233882013-04-25 13:57:53 +03003967 DBG("from %dx%d to %dx%d\n",
3968 window->main_surface->server_allocation.width,
3969 window->main_surface->server_allocation.height,
3970 window->pending_allocation.width,
3971 window->pending_allocation.height);
3972
Pekka Paalanene9297f82013-04-25 13:57:51 +03003973 hack_prevent_EGL_sub_surface_deadlock(window);
3974
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003975 widget_set_allocation(window->main_surface->widget,
3976 window->pending_allocation.x,
3977 window->pending_allocation.y,
3978 window->pending_allocation.width,
3979 window->pending_allocation.height);
3980
3981 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003982
3983 /* The main surface is in the list, too. Main surface's
3984 * resize_handler is responsible for calling widget_set_allocation()
3985 * on all sub-surface root widgets, so they will be resized
3986 * properly.
3987 */
3988 wl_list_for_each(surface, &window->subsurface_list, link) {
3989 if (surface == window->main_surface)
3990 continue;
3991
3992 surface_set_synchronized(surface);
3993 surface_resize(surface);
3994 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003995}
3996
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003997void
3998window_schedule_resize(struct window *window, int width, int height)
3999{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004000 window->pending_allocation.x = 0;
4001 window->pending_allocation.y = 0;
4002 window->pending_allocation.width = width;
4003 window->pending_allocation.height = height;
4004
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004005 if (window->min_allocation.width == 0)
4006 window->min_allocation = window->pending_allocation;
4007 if (window->pending_allocation.width < window->min_allocation.width)
4008 window->pending_allocation.width = window->min_allocation.width;
4009 if (window->pending_allocation.height < window->min_allocation.height)
4010 window->pending_allocation.height = window->min_allocation.height;
4011
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004012 window->resize_needed = 1;
4013 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004014}
4015
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004016void
4017widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4018{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004019 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004020}
4021
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004022static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06004023handle_ping(void *data, struct wl_shell_surface *shell_surface,
4024 uint32_t serial)
4025{
4026 wl_shell_surface_pong(shell_surface, serial);
4027}
4028
4029static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004030handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004031 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004032{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004033 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004034
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01004035 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004036 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004037}
4038
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004039static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004040menu_destroy(struct menu *menu)
4041{
4042 widget_destroy(menu->widget);
4043 window_destroy(menu->window);
4044 free(menu);
4045}
4046
4047static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004048handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
4049{
4050 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02004051 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004052
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004053 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004054 * device. Or just use wl_callback. And this really needs to
4055 * be a window vfunc that the menu can set. And we need the
4056 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004057
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004058 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004059 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004060}
4061
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004062static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06004063 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004064 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004065 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004066};
4067
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004068void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004069window_get_allocation(struct window *window,
4070 struct rectangle *allocation)
4071{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004072 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004073}
4074
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004075static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004076widget_redraw(struct widget *widget)
4077{
4078 struct widget *child;
4079
4080 if (widget->redraw_handler)
4081 widget->redraw_handler(widget, widget->user_data);
4082 wl_list_for_each(child, &widget->child_list, link)
4083 widget_redraw(child);
4084}
4085
4086static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004087frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4088{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004089 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004090
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004091 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004092 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004093 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004094 surface->frame_cb = NULL;
4095
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004096 surface->last_time = time;
4097
Pekka Paalanen71233882013-04-25 13:57:53 +03004098 if (surface->redraw_needed || surface->window->redraw_needed) {
4099 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004100 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004101 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004102}
4103
4104static const struct wl_callback_listener listener = {
4105 frame_callback
4106};
4107
4108static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004109surface_redraw(struct surface *surface)
4110{
Pekka Paalanen71233882013-04-25 13:57:53 +03004111 DBG_OBJ(surface->surface, "begin\n");
4112
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004113 if (!surface->window->redraw_needed && !surface->redraw_needed)
4114 return;
4115
4116 /* Whole-window redraw forces a redraw even if the previous has
4117 * not yet hit the screen.
4118 */
4119 if (surface->frame_cb) {
4120 if (!surface->window->redraw_needed)
4121 return;
4122
Pekka Paalanen71233882013-04-25 13:57:53 +03004123 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004124 wl_callback_destroy(surface->frame_cb);
4125 }
4126
4127 surface->frame_cb = wl_surface_frame(surface->surface);
4128 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004129 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004130
4131 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004132 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004133 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004134 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004135}
4136
4137static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004138idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004139{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004140 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004141 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004142
Pekka Paalanen71233882013-04-25 13:57:53 +03004143 DBG(" --------- \n");
4144
Pekka Paalaneneebff542013-04-25 13:57:52 +03004145 wl_list_init(&window->redraw_task.link);
4146 window->redraw_task_scheduled = 0;
4147
4148 if (window->resize_needed) {
4149 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004150 if (window->main_surface->frame_cb) {
4151 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004152 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004153 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004154
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004155 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03004156 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004157
Pekka Paalanen35e82632013-04-25 13:57:48 +03004158 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004159 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004160
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004161 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004162 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004163
4164 wl_list_for_each(surface, &window->subsurface_list, link)
4165 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004166}
4167
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004168static void
4169window_schedule_redraw_task(struct window *window)
4170{
4171 if (window->configure_requests)
4172 return;
4173 if (!window->redraw_task_scheduled) {
4174 window->redraw_task.run = idle_redraw;
4175 display_defer(window->display, &window->redraw_task);
4176 window->redraw_task_scheduled = 1;
4177 }
4178}
4179
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004180void
4181window_schedule_redraw(struct window *window)
4182{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004183 struct surface *surface;
4184
Pekka Paalanen71233882013-04-25 13:57:53 +03004185 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4186
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004187 wl_list_for_each(surface, &window->subsurface_list, link)
4188 surface->redraw_needed = 1;
4189
4190 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004191}
4192
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004193int
4194window_is_fullscreen(struct window *window)
4195{
4196 return window->type == TYPE_FULLSCREEN;
4197}
4198
MoD063a8822013-03-02 23:43:57 +00004199static void
4200configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4201{
4202 struct window *window = data;
4203
4204 wl_callback_destroy(callback);
4205 window->configure_requests--;
4206
4207 if (!window->configure_requests)
4208 window_schedule_redraw(window);
4209}
4210
4211static struct wl_callback_listener configure_request_listener = {
4212 configure_request_completed,
4213};
4214
4215static void
4216window_defer_redraw_until_configure(struct window* window)
4217{
4218 struct wl_callback *callback;
4219
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004220 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004221 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004222 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004223 }
4224
4225 callback = wl_display_sync(window->display->display);
4226 wl_callback_add_listener(callback, &configure_request_listener, window);
4227 window->configure_requests++;
4228}
4229
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004230void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004231window_set_fullscreen(struct window *window, int fullscreen)
4232{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004233 if (!window->display->shell)
4234 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004235
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004236 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004237 return;
4238
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004239 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004240 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004241 if (window->type == TYPE_TOPLEVEL) {
4242 window->saved_allocation = window->main_surface->allocation;
4243 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004244 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004245 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004246 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004247 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004248 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004249 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004250 if (window->saved_type == TYPE_MAXIMIZED) {
4251 window_set_maximized(window, 1);
4252 } else {
4253 window->type = TYPE_TOPLEVEL;
4254 wl_shell_surface_set_toplevel(window->shell_surface);
4255 window_schedule_resize(window,
4256 window->saved_allocation.width,
4257 window->saved_allocation.height);
4258 }
4259
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004260 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004261}
4262
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004263void
4264window_set_fullscreen_method(struct window *window,
4265 enum wl_shell_surface_fullscreen_method method)
4266{
4267 window->fullscreen_method = method;
4268}
4269
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004270int
4271window_is_maximized(struct window *window)
4272{
4273 return window->type == TYPE_MAXIMIZED;
4274}
4275
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004276void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004277window_set_maximized(struct window *window, int maximized)
4278{
4279 if (!window->display->shell)
4280 return;
4281
4282 if ((window->type == TYPE_MAXIMIZED) == maximized)
4283 return;
4284
4285 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004286 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004287 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4288 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004289 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004290 } else if (window->type == TYPE_FULLSCREEN) {
4291 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4292 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004293 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004294 } else {
4295 wl_shell_surface_set_toplevel(window->shell_surface);
4296 window->type = TYPE_TOPLEVEL;
4297 window_schedule_resize(window,
4298 window->saved_allocation.width,
4299 window->saved_allocation.height);
4300 }
4301}
4302
4303void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004304window_set_user_data(struct window *window, void *data)
4305{
4306 window->user_data = data;
4307}
4308
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004309void *
4310window_get_user_data(struct window *window)
4311{
4312 return window->user_data;
4313}
4314
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004315void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004316window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004317 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004318{
4319 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004320}
4321
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004322void
4323window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004324 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004325{
4326 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004327}
4328
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004329void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004330window_set_data_handler(struct window *window, window_data_handler_t handler)
4331{
4332 window->data_handler = handler;
4333}
4334
4335void
4336window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4337{
4338 window->drop_handler = handler;
4339}
4340
4341void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004342window_set_close_handler(struct window *window,
4343 window_close_handler_t handler)
4344{
4345 window->close_handler = handler;
4346}
4347
4348void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004349window_set_fullscreen_handler(struct window *window,
4350 window_fullscreen_handler_t handler)
4351{
4352 window->fullscreen_handler = handler;
4353}
4354
4355void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004356window_set_output_handler(struct window *window,
4357 window_output_handler_t handler)
4358{
4359 window->output_handler = handler;
4360}
4361
4362void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004363window_set_title(struct window *window, const char *title)
4364{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004365 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004366 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004367 if (window->shell_surface)
4368 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004369}
4370
4371const char *
4372window_get_title(struct window *window)
4373{
4374 return window->title;
4375}
4376
4377void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004378window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4379{
4380 struct text_cursor_position *text_cursor_position =
4381 window->display->text_cursor_position;
4382
Scott Moreau9295ce02012-06-01 12:46:10 -06004383 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004384 return;
4385
4386 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004387 window->main_surface->surface,
4388 wl_fixed_from_int(x),
4389 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004390}
4391
4392void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004393window_damage(struct window *window, int32_t x, int32_t y,
4394 int32_t width, int32_t height)
4395{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004396 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004397}
4398
Casey Dahlin9074db52012-04-19 22:50:09 -04004399static void
4400surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004401 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004402{
Rob Bradford7507b572012-05-15 17:55:34 +01004403 struct window *window = data;
4404 struct output *output;
4405 struct output *output_found = NULL;
4406 struct window_output *window_output;
4407
4408 wl_list_for_each(output, &window->display->output_list, link) {
4409 if (output->output == wl_output) {
4410 output_found = output;
4411 break;
4412 }
4413 }
4414
4415 if (!output_found)
4416 return;
4417
Brian Lovinbc919262013-08-07 15:34:59 -07004418 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004419 window_output->output = output_found;
4420
4421 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004422
4423 if (window->output_handler)
4424 window->output_handler(window, output_found, 1,
4425 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004426}
4427
4428static void
4429surface_leave(void *data,
4430 struct wl_surface *wl_surface, struct wl_output *output)
4431{
Rob Bradford7507b572012-05-15 17:55:34 +01004432 struct window *window = data;
4433 struct window_output *window_output;
4434 struct window_output *window_output_found = NULL;
4435
4436 wl_list_for_each(window_output, &window->window_output_list, link) {
4437 if (window_output->output->output == output) {
4438 window_output_found = window_output;
4439 break;
4440 }
4441 }
4442
4443 if (window_output_found) {
4444 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004445
4446 if (window->output_handler)
4447 window->output_handler(window, window_output->output,
4448 0, window->user_data);
4449
Rob Bradford7507b572012-05-15 17:55:34 +01004450 free(window_output_found);
4451 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004452}
4453
4454static const struct wl_surface_listener surface_listener = {
4455 surface_enter,
4456 surface_leave
4457};
4458
Pekka Paalanen4e373742013-02-13 16:17:13 +02004459static struct surface *
4460surface_create(struct window *window)
4461{
4462 struct display *display = window->display;
4463 struct surface *surface;
4464
Brian Lovinbc919262013-08-07 15:34:59 -07004465 surface = xmalloc(sizeof *surface);
4466 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004467 if (!surface)
4468 return NULL;
4469
4470 surface->window = window;
4471 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004472 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004473 wl_surface_add_listener(surface->surface, &surface_listener, window);
4474
Pekka Paalanen35e82632013-04-25 13:57:48 +03004475 wl_list_insert(&window->subsurface_list, &surface->link);
4476
Pekka Paalanen4e373742013-02-13 16:17:13 +02004477 return surface;
4478}
4479
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004480static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004481window_create_internal(struct display *display,
4482 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004483{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004484 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004485 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004486
Peter Huttererf3d62272013-08-08 11:57:05 +10004487 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004488 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004489 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004490 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004491
4492 surface = surface_create(window);
4493 window->main_surface = surface;
4494
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004495 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004496 window->shell_surface =
4497 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004498 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004499 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004500 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004501
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004502 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004503 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004504 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004505 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004506
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004507 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004508#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004509 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004510#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004511 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004512#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004513 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004514 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004515
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004516 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004517 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004518 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004519
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004520 if (window->shell_surface) {
4521 wl_shell_surface_set_user_data(window->shell_surface, window);
4522 wl_shell_surface_add_listener(window->shell_surface,
4523 &shell_surface_listener, window);
4524 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004525
Rob Bradford7507b572012-05-15 17:55:34 +01004526 wl_list_init (&window->window_output_list);
4527
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004528 return window;
4529}
4530
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004531struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004532window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004533{
Kristian Høgsberg41624832013-07-25 16:53:10 -07004534 return window_create_internal(display, NULL, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004535}
4536
4537struct window *
4538window_create_custom(struct display *display)
4539{
Kristian Høgsbergce278412013-07-25 15:20:20 -07004540 return window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004541}
4542
4543struct window *
4544window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004545 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004546{
4547 struct window *window;
4548
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004549 window = window_create_internal(parent->display,
4550 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004551
4552 window->x = x;
4553 window->y = y;
4554
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004555 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004556 wl_shell_surface_set_transient(
4557 window->shell_surface,
4558 window->parent->main_surface->surface,
4559 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004560
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004561 return window;
4562}
4563
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004564static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004565menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004566{
4567 int next;
4568
4569 next = (sy - 8) / 20;
4570 if (menu->current != next) {
4571 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004572 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004573 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004574}
4575
4576static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004577menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004578 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004579 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004580{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004581 struct menu *menu = data;
4582
4583 if (widget == menu->widget)
4584 menu_set_item(data, y);
4585
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004586 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004587}
4588
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004589static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004590menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004591 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004592{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004593 struct menu *menu = data;
4594
4595 if (widget == menu->widget)
4596 menu_set_item(data, y);
4597
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004598 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004599}
4600
4601static void
4602menu_leave_handler(struct widget *widget, struct input *input, void *data)
4603{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004604 struct menu *menu = data;
4605
4606 if (widget == menu->widget)
4607 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004608}
4609
4610static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004611menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004612 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004613 uint32_t button, enum wl_pointer_button_state state,
4614 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004615
4616{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004617 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004618
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004619 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4620 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004621 /* Either relase after press-drag-release or
4622 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004623 menu->func(menu->window->parent,
4624 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004625 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004626 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004627 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004628 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004629 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004630}
4631
4632static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004633menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004634{
4635 cairo_t *cr;
4636 const int32_t r = 3, margin = 3;
4637 struct menu *menu = data;
4638 int32_t width, height, i;
4639
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004640 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004641 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4642 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4643 cairo_paint(cr);
4644
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004645 width = widget->allocation.width;
4646 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004647 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004648
4649 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004650 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4651 cairo_fill(cr);
4652
4653 for (i = 0; i < menu->count; i++) {
4654 if (i == menu->current) {
4655 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4656 cairo_rectangle(cr, margin, i * 20 + margin,
4657 width - 2 * margin, 20);
4658 cairo_fill(cr);
4659 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4660 cairo_move_to(cr, 10, i * 20 + 16);
4661 cairo_show_text(cr, menu->entries[i]);
4662 } else {
4663 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4664 cairo_move_to(cr, 10, i * 20 + 16);
4665 cairo_show_text(cr, menu->entries[i]);
4666 }
4667 }
4668
4669 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004670}
4671
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004672void
4673window_show_menu(struct display *display,
4674 struct input *input, uint32_t time, struct window *parent,
4675 int32_t x, int32_t y,
4676 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004677{
4678 struct window *window;
4679 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004680 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004681
4682 menu = malloc(sizeof *menu);
4683 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004684 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004685
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004686 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004687 if (!window) {
4688 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004689 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004690 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004691
4692 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004693 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004694 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4695 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004696 menu->entries = entries;
4697 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004698 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004699 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004700 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004701 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004702 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004703 window->type = TYPE_MENU;
4704 window->x = x;
4705 window->y = y;
4706
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004707 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004708 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004709 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004710 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004711 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004712
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004713 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004714 widget_set_enter_handler(menu->widget, menu_enter_handler);
4715 widget_set_leave_handler(menu->widget, menu_leave_handler);
4716 widget_set_motion_handler(menu->widget, menu_motion_handler);
4717 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004718
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004719 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004720 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004721}
4722
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004723void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004724window_set_buffer_type(struct window *window, enum window_buffer_type type)
4725{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004726 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004727}
4728
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004729void
4730window_set_preferred_format(struct window *window,
4731 enum preferred_format format)
4732{
4733 window->preferred_format = format;
4734}
4735
Pekka Paalanen35e82632013-04-25 13:57:48 +03004736struct widget *
4737window_add_subsurface(struct window *window, void *data,
4738 enum subsurface_mode default_mode)
4739{
4740 struct widget *widget;
4741 struct surface *surface;
4742 struct wl_surface *parent;
4743 struct wl_subcompositor *subcompo = window->display->subcompositor;
4744
Pekka Paalanen35e82632013-04-25 13:57:48 +03004745 surface = surface_create(window);
4746 widget = widget_create(window, surface, data);
4747 wl_list_init(&widget->link);
4748 surface->widget = widget;
4749
4750 parent = window->main_surface->surface;
4751 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4752 surface->surface,
4753 parent);
4754 surface->synchronized = 1;
4755
4756 switch (default_mode) {
4757 case SUBSURFACE_SYNCHRONIZED:
4758 surface->synchronized_default = 1;
4759 break;
4760 case SUBSURFACE_DESYNCHRONIZED:
4761 surface->synchronized_default = 0;
4762 break;
4763 default:
4764 assert(!"bad enum subsurface_mode");
4765 }
4766
4767 return widget;
4768}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004769
4770static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004771display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004772 struct wl_output *wl_output,
4773 int x, int y,
4774 int physical_width,
4775 int physical_height,
4776 int subpixel,
4777 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004778 const char *model,
4779 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004780{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004781 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004782
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004783 output->allocation.x = x;
4784 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004785 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004786}
4787
4788static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004789display_handle_done(void *data,
4790 struct wl_output *wl_output)
4791{
4792}
4793
4794static void
4795display_handle_scale(void *data,
4796 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004797 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004798{
4799 struct output *output = data;
4800
4801 output->scale = scale;
4802}
4803
4804static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004805display_handle_mode(void *data,
4806 struct wl_output *wl_output,
4807 uint32_t flags,
4808 int width,
4809 int height,
4810 int refresh)
4811{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004812 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004813 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004814
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004815 if (flags & WL_OUTPUT_MODE_CURRENT) {
4816 output->allocation.width = width;
4817 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004818 if (display->output_configure_handler)
4819 (*display->output_configure_handler)(
4820 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004821 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004822}
4823
4824static const struct wl_output_listener output_listener = {
4825 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004826 display_handle_mode,
4827 display_handle_done,
4828 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004829};
4830
4831static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004832display_add_output(struct display *d, uint32_t id)
4833{
4834 struct output *output;
4835
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004836 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004837 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004838 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004839 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004840 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004841 wl_list_insert(d->output_list.prev, &output->link);
4842
4843 wl_output_add_listener(output->output, &output_listener, output);
4844}
4845
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004846static void
4847output_destroy(struct output *output)
4848{
4849 if (output->destroy_handler)
4850 (*output->destroy_handler)(output, output->user_data);
4851
4852 wl_output_destroy(output->output);
4853 wl_list_remove(&output->link);
4854 free(output);
4855}
4856
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004857void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004858display_set_global_handler(struct display *display,
4859 display_global_handler_t handler)
4860{
4861 struct global *global;
4862
4863 display->global_handler = handler;
4864 if (!handler)
4865 return;
4866
4867 wl_list_for_each(global, &display->global_list, link)
4868 display->global_handler(display,
4869 global->name, global->interface,
4870 global->version, display->user_data);
4871}
4872
4873void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004874display_set_output_configure_handler(struct display *display,
4875 display_output_handler_t handler)
4876{
4877 struct output *output;
4878
4879 display->output_configure_handler = handler;
4880 if (!handler)
4881 return;
4882
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004883 wl_list_for_each(output, &display->output_list, link) {
4884 if (output->allocation.width == 0 &&
4885 output->allocation.height == 0)
4886 continue;
4887
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004888 (*display->output_configure_handler)(output,
4889 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004890 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004891}
4892
4893void
4894output_set_user_data(struct output *output, void *data)
4895{
4896 output->user_data = data;
4897}
4898
4899void *
4900output_get_user_data(struct output *output)
4901{
4902 return output->user_data;
4903}
4904
4905void
4906output_set_destroy_handler(struct output *output,
4907 display_output_handler_t handler)
4908{
4909 output->destroy_handler = handler;
4910 /* FIXME: implement this, once we have way to remove outputs */
4911}
4912
4913void
Scott Moreau4e072362012-09-29 02:03:11 -06004914output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004915{
Scott Moreau4e072362012-09-29 02:03:11 -06004916 struct rectangle allocation = output->allocation;
4917
4918 switch (output->transform) {
4919 case WL_OUTPUT_TRANSFORM_90:
4920 case WL_OUTPUT_TRANSFORM_270:
4921 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4922 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4923 /* Swap width and height */
4924 allocation.width = output->allocation.height;
4925 allocation.height = output->allocation.width;
4926 break;
4927 }
4928
4929 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004930}
4931
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004932struct wl_output *
4933output_get_wl_output(struct output *output)
4934{
4935 return output->output;
4936}
4937
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004938enum wl_output_transform
4939output_get_transform(struct output *output)
4940{
4941 return output->transform;
4942}
4943
Alexander Larssonafd319a2013-05-22 14:41:27 +02004944uint32_t
4945output_get_scale(struct output *output)
4946{
4947 return output->scale;
4948}
4949
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004950static void
Daniel Stone97f68542012-05-30 16:32:01 +01004951fini_xkb(struct input *input)
4952{
4953 xkb_state_unref(input->xkb.state);
4954 xkb_map_unref(input->xkb.keymap);
4955}
4956
Rob Bradford08031182013-08-13 20:11:03 +01004957#define MAX(a,b) ((a) > (b) ? a : b)
4958
Daniel Stone97f68542012-05-30 16:32:01 +01004959static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004960display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004961{
4962 struct input *input;
4963
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004964 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004965 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004966 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4967 MAX(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004968 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004969 input->pointer_focus = NULL;
4970 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004971 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004972 wl_list_insert(d->input_list.prev, &input->link);
4973
Daniel Stone37816df2012-05-16 18:45:18 +01004974 wl_seat_add_listener(input->seat, &seat_listener, input);
4975 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004976
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004977 input->data_device =
4978 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004979 input->seat);
4980 wl_data_device_add_listener(input->data_device, &data_device_listener,
4981 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004982
4983 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004984
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004985 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4986 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004987 input->repeat_task.run = keyboard_repeat_func;
4988 display_watch_fd(d, input->repeat_timer_fd,
4989 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004990}
4991
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004992static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004993input_destroy(struct input *input)
4994{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004995 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004996 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004997
4998 if (input->drag_offer)
4999 data_offer_destroy(input->drag_offer);
5000
5001 if (input->selection_offer)
5002 data_offer_destroy(input->selection_offer);
5003
5004 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005005
5006 if (input->display->seat_version >= 3) {
5007 if (input->pointer)
5008 wl_pointer_release(input->pointer);
5009 if (input->keyboard)
5010 wl_keyboard_release(input->keyboard);
5011 }
5012
Daniel Stone97f68542012-05-30 16:32:01 +01005013 fini_xkb(input);
5014
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005015 wl_surface_destroy(input->pointer_surface);
5016
Pekka Paalanene1207c72011-12-16 12:02:09 +02005017 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005018 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005019 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005020 free(input);
5021}
5022
5023static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005024init_workspace_manager(struct display *d, uint32_t id)
5025{
5026 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005027 wl_registry_bind(d->registry, id,
5028 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005029 if (d->workspace_manager != NULL)
5030 workspace_manager_add_listener(d->workspace_manager,
5031 &workspace_manager_listener,
5032 d);
5033}
5034
5035static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005036shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5037{
5038 struct display *d = data;
5039
5040 if (format == WL_SHM_FORMAT_RGB565)
5041 d->has_rgb565 = 1;
5042}
5043
5044struct wl_shm_listener shm_listener = {
5045 shm_format
5046};
5047
5048static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005049registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5050 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005051{
5052 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005053 struct global *global;
5054
Brian Lovinbc919262013-08-07 15:34:59 -07005055 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005056 global->name = id;
5057 global->interface = strdup(interface);
5058 global->version = version;
5059 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005060
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005061 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005062 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005063 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005064 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005065 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005066 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005067 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005068 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005069 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005070 d->shell = wl_registry_bind(registry,
5071 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005072 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005073 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005074 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005075 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5076 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005077 wl_registry_bind(registry, id,
5078 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005079 } else if (strcmp(interface, "text_cursor_position") == 0) {
5080 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005081 wl_registry_bind(registry, id,
5082 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005083 } else if (strcmp(interface, "workspace_manager") == 0) {
5084 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005085 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5086 d->subcompositor =
5087 wl_registry_bind(registry, id,
5088 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005089 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005090
5091 if (d->global_handler)
5092 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005093}
5094
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005095static void
5096registry_handle_global_remove(void *data, struct wl_registry *registry,
5097 uint32_t name)
5098{
5099 struct display *d = data;
5100 struct global *global;
5101 struct global *tmp;
5102
5103 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5104 if (global->name != name)
5105 continue;
5106
5107 /* XXX: Should destroy bound globals, and call
5108 * the counterpart of display::global_handler
5109 */
5110 wl_list_remove(&global->link);
5111 free(global->interface);
5112 free(global);
5113 }
5114}
5115
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005116void *
5117display_bind(struct display *display, uint32_t name,
5118 const struct wl_interface *interface, uint32_t version)
5119{
5120 return wl_registry_bind(display->registry, name, interface, version);
5121}
5122
5123static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005124 registry_handle_global,
5125 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005126};
5127
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005128#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005129static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005130init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005131{
5132 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005133 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005134
Rob Clark6396ed32012-03-11 19:48:41 -05005135#ifdef USE_CAIRO_GLESV2
5136# define GL_BIT EGL_OPENGL_ES2_BIT
5137#else
5138# define GL_BIT EGL_OPENGL_BIT
5139#endif
5140
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005141 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005142 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005143 EGL_RED_SIZE, 1,
5144 EGL_GREEN_SIZE, 1,
5145 EGL_BLUE_SIZE, 1,
5146 EGL_ALPHA_SIZE, 1,
5147 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005148 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005149 EGL_NONE
5150 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005151
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005152#ifdef USE_CAIRO_GLESV2
5153 static const EGLint context_attribs[] = {
5154 EGL_CONTEXT_CLIENT_VERSION, 2,
5155 EGL_NONE
5156 };
5157 EGLint api = EGL_OPENGL_ES_API;
5158#else
5159 EGLint *context_attribs = NULL;
5160 EGLint api = EGL_OPENGL_API;
5161#endif
5162
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005163 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005164 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005165 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005166 return -1;
5167 }
5168
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005169 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005170 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005171 return -1;
5172 }
5173
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005174 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5175 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005176 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005177 return -1;
5178 }
5179
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005180 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005181 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005182 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005183 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005184 return -1;
5185 }
5186
Benjamin Franzke0c991632011-09-27 21:57:31 +02005187 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5188 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005189 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005190 return -1;
5191 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005192
5193 return 0;
5194}
5195
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005196static void
5197fini_egl(struct display *display)
5198{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005199 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005200
5201 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5202 EGL_NO_CONTEXT);
5203
5204 eglTerminate(display->dpy);
5205 eglReleaseThread();
5206}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005207#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005208
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005209static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005210init_dummy_surface(struct display *display)
5211{
5212 int len;
5213 void *data;
5214
5215 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5216 data = malloc(len);
5217 display->dummy_surface =
5218 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5219 1, 1, len);
5220 display->dummy_surface_data = data;
5221}
5222
5223static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005224handle_display_data(struct task *task, uint32_t events)
5225{
5226 struct display *display =
5227 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005228 struct epoll_event ep;
5229 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005230
5231 display->display_fd_events = events;
5232
5233 if (events & EPOLLERR || events & EPOLLHUP) {
5234 display_exit(display);
5235 return;
5236 }
5237
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005238 if (events & EPOLLIN) {
5239 ret = wl_display_dispatch(display->display);
5240 if (ret == -1) {
5241 display_exit(display);
5242 return;
5243 }
5244 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005245
5246 if (events & EPOLLOUT) {
5247 ret = wl_display_flush(display->display);
5248 if (ret == 0) {
5249 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5250 ep.data.ptr = &display->display_task;
5251 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5252 display->display_fd, &ep);
5253 } else if (ret == -1 && errno != EAGAIN) {
5254 display_exit(display);
5255 return;
5256 }
5257 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005258}
5259
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005260static void
5261log_handler(const char *format, va_list args)
5262{
5263 vfprintf(stderr, format, args);
5264}
5265
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005266struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005267display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005268{
5269 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005270
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005271 wl_log_set_handler_client(log_handler);
5272
Peter Huttererf3d62272013-08-08 11:57:05 +10005273 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005274 if (d == NULL)
5275 return NULL;
5276
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005277 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005278 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005279 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005280 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005281 return NULL;
5282 }
5283
Rob Bradford5ab9c752013-07-26 16:29:43 +01005284 d->xkb_context = xkb_context_new(0);
5285 if (d->xkb_context == NULL) {
5286 fprintf(stderr, "Failed to create XKB context\n");
5287 free(d);
5288 return NULL;
5289 }
5290
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005291 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005292 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005293 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005294 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5295 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005296
5297 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005298 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005299 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005300 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005301
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005302 d->workspace = 0;
5303 d->workspace_count = 1;
5304
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005305 d->registry = wl_display_get_registry(d->display);
5306 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005307
5308 if (wl_display_dispatch(d->display) < 0) {
5309 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5310 return NULL;
5311 }
5312
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005313#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005314 if (init_egl(d) < 0)
5315 fprintf(stderr, "EGL does not seem to work, "
5316 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005317#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005318
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005319 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005320
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005321 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005322
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005323 wl_list_init(&d->window_list);
5324
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005325 init_dummy_surface(d);
5326
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005327 return d;
5328}
5329
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005330static void
5331display_destroy_outputs(struct display *display)
5332{
5333 struct output *tmp;
5334 struct output *output;
5335
5336 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5337 output_destroy(output);
5338}
5339
Pekka Paalanene1207c72011-12-16 12:02:09 +02005340static void
5341display_destroy_inputs(struct display *display)
5342{
5343 struct input *tmp;
5344 struct input *input;
5345
5346 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5347 input_destroy(input);
5348}
5349
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005350void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005351display_destroy(struct display *display)
5352{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005353 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005354 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5355 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005356
5357 if (!wl_list_empty(&display->deferred_list))
5358 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5359
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005360 cairo_surface_destroy(display->dummy_surface);
5361 free(display->dummy_surface_data);
5362
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005363 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005364 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005365
Daniel Stone97f68542012-05-30 16:32:01 +01005366 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005367
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005368 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005369 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005370
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005371#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005372 if (display->argb_device)
5373 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005374#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005375
Pekka Paalanen35e82632013-04-25 13:57:48 +03005376 if (display->subcompositor)
5377 wl_subcompositor_destroy(display->subcompositor);
5378
Pekka Paalanenc2052982011-12-16 11:41:32 +02005379 if (display->shell)
5380 wl_shell_destroy(display->shell);
5381
5382 if (display->shm)
5383 wl_shm_destroy(display->shm);
5384
5385 if (display->data_device_manager)
5386 wl_data_device_manager_destroy(display->data_device_manager);
5387
5388 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005389 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005390
5391 close(display->epoll_fd);
5392
U. Artie Eoff44874d92012-10-02 21:12:35 -07005393 if (!(display->display_fd_events & EPOLLERR) &&
5394 !(display->display_fd_events & EPOLLHUP))
5395 wl_display_flush(display->display);
5396
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005397 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005398 free(display);
5399}
5400
5401void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005402display_set_user_data(struct display *display, void *data)
5403{
5404 display->user_data = data;
5405}
5406
5407void *
5408display_get_user_data(struct display *display)
5409{
5410 return display->user_data;
5411}
5412
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005413struct wl_display *
5414display_get_display(struct display *display)
5415{
5416 return display->display;
5417}
5418
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005419int
5420display_has_subcompositor(struct display *display)
5421{
5422 if (display->subcompositor)
5423 return 1;
5424
5425 wl_display_roundtrip(display->display);
5426
5427 return display->subcompositor != NULL;
5428}
5429
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005430cairo_device_t *
5431display_get_cairo_device(struct display *display)
5432{
5433 return display->argb_device;
5434}
5435
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005436struct output *
5437display_get_output(struct display *display)
5438{
5439 return container_of(display->output_list.next, struct output, link);
5440}
5441
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005442struct wl_compositor *
5443display_get_compositor(struct display *display)
5444{
5445 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005446}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005447
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005448uint32_t
5449display_get_serial(struct display *display)
5450{
5451 return display->serial;
5452}
5453
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005454EGLDisplay
5455display_get_egl_display(struct display *d)
5456{
5457 return d->dpy;
5458}
5459
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005460struct wl_data_source *
5461display_create_data_source(struct display *display)
5462{
5463 return wl_data_device_manager_create_data_source(display->data_device_manager);
5464}
5465
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005466EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005467display_get_argb_egl_config(struct display *d)
5468{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005469 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005470}
5471
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005472struct wl_shell *
5473display_get_shell(struct display *display)
5474{
5475 return display->shell;
5476}
5477
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005478int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005479display_acquire_window_surface(struct display *display,
5480 struct window *window,
5481 EGLContext ctx)
5482{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005483 struct surface *surface = window->main_surface;
5484
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005485 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005486 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005487
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005488 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005489 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005490}
5491
5492void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005493display_release_window_surface(struct display *display,
5494 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005495{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005496 struct surface *surface = window->main_surface;
5497
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005498 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005499 return;
5500
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005501 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005502}
5503
5504void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005505display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005506{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005507 wl_list_insert(&display->deferred_list, &task->link);
5508}
5509
5510void
5511display_watch_fd(struct display *display,
5512 int fd, uint32_t events, struct task *task)
5513{
5514 struct epoll_event ep;
5515
5516 ep.events = events;
5517 ep.data.ptr = task;
5518 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5519}
5520
5521void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005522display_unwatch_fd(struct display *display, int fd)
5523{
5524 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5525}
5526
5527void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005528display_run(struct display *display)
5529{
5530 struct task *task;
5531 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005532 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005533
Pekka Paalanen826d7952011-12-15 10:14:07 +02005534 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005535 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005536 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005537 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005538 struct task, link);
5539 wl_list_remove(&task->link);
5540 task->run(task, 0);
5541 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005542
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005543 wl_display_dispatch_pending(display->display);
5544
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005545 if (!display->running)
5546 break;
5547
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005548 ret = wl_display_flush(display->display);
5549 if (ret < 0 && errno == EAGAIN) {
5550 ep[0].events =
5551 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5552 ep[0].data.ptr = &display->display_task;
5553
5554 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5555 display->display_fd, &ep[0]);
5556 } else if (ret < 0) {
5557 break;
5558 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005559
5560 count = epoll_wait(display->epoll_fd,
5561 ep, ARRAY_LENGTH(ep), -1);
5562 for (i = 0; i < count; i++) {
5563 task = ep[i].data.ptr;
5564 task->run(task, ep[i].events);
5565 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005566 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005567}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005568
5569void
5570display_exit(struct display *display)
5571{
5572 display->running = 0;
5573}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005574
5575void
5576keysym_modifiers_add(struct wl_array *modifiers_map,
5577 const char *name)
5578{
5579 size_t len = strlen(name) + 1;
5580 char *p;
5581
5582 p = wl_array_add(modifiers_map, len);
5583
5584 if (p == NULL)
5585 return;
5586
5587 strncpy(p, name, len);
5588}
5589
5590static xkb_mod_index_t
5591keysym_modifiers_get_index(struct wl_array *modifiers_map,
5592 const char *name)
5593{
5594 xkb_mod_index_t index = 0;
5595 char *p = modifiers_map->data;
5596
5597 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5598 if (strcmp(p, name) == 0)
5599 return index;
5600
5601 index++;
5602 p += strlen(p) + 1;
5603 }
5604
5605 return XKB_MOD_INVALID;
5606}
5607
5608xkb_mod_mask_t
5609keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5610 const char *name)
5611{
5612 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5613
5614 if (index == XKB_MOD_INVALID)
5615 return XKB_MOD_INVALID;
5616
5617 return 1 << index;
5618}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005619
5620void *
5621fail_on_null(void *p)
5622{
5623 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005624 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005625 exit(EXIT_FAILURE);
5626 }
5627
5628 return p;
5629}
5630
5631void *
5632xmalloc(size_t s)
5633{
5634 return fail_on_null(malloc(s));
5635}
5636
Peter Huttererf3d62272013-08-08 11:57:05 +10005637void *
5638xzalloc(size_t s)
5639{
5640 return fail_on_null(zalloc(s));
5641}
5642
Kristian Høgsbergce278412013-07-25 15:20:20 -07005643char *
5644xstrdup(const char *s)
5645{
5646 return fail_on_null(strdup(s));
5647}