blob: 5b20da57054b8b6b2f36809853cc75a94fc65e09 [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);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001324 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001325 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001326
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001327 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001328 cursor = NULL;
1329 for (j = 0; !cursor && j < cursors[i].count; ++j)
1330 cursor = wl_cursor_theme_get_cursor(
1331 display->cursor_theme, cursors[i].names[j]);
1332
1333 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001334 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001335 cursors[i].names[0]);
1336
1337 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001338 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001339}
1340
1341static void
1342destroy_cursors(struct display *display)
1343{
1344 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001345 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001346}
1347
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001348struct wl_cursor_image *
1349display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001350{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001351 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001352
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001353 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001354}
1355
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001356static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001357surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001358{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001359 if (!surface->cairo_surface)
1360 return;
1361
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001362 if (surface->opaque_region) {
1363 wl_surface_set_opaque_region(surface->surface,
1364 surface->opaque_region);
1365 wl_region_destroy(surface->opaque_region);
1366 surface->opaque_region = NULL;
1367 }
1368
1369 if (surface->input_region) {
1370 wl_surface_set_input_region(surface->surface,
1371 surface->input_region);
1372 wl_region_destroy(surface->input_region);
1373 surface->input_region = NULL;
1374 }
1375
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001376 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001377 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001378 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001379
Pekka Paalanen89dee002013-02-13 16:17:20 +02001380 cairo_surface_destroy(surface->cairo_surface);
1381 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001382}
1383
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001384int
1385window_has_focus(struct window *window)
1386{
1387 return window->focus_count > 0;
1388}
1389
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001390static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001391window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001392{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001393 struct surface *surface;
1394
Pekka Paalanen89dee002013-02-13 16:17:20 +02001395 if (window->type == TYPE_NONE) {
1396 window->type = TYPE_TOPLEVEL;
1397 if (window->shell_surface)
1398 wl_shell_surface_set_toplevel(window->shell_surface);
1399 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001400
Pekka Paalanen35e82632013-04-25 13:57:48 +03001401 wl_list_for_each(surface, &window->subsurface_list, link) {
1402 if (surface == window->main_surface)
1403 continue;
1404
1405 surface_flush(surface);
1406 }
1407
Pekka Paalanen89dee002013-02-13 16:17:20 +02001408 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001409}
1410
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001411struct display *
1412window_get_display(struct window *window)
1413{
1414 return window->display;
1415}
1416
Pekka Paalanen89dee002013-02-13 16:17:20 +02001417static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001418surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001419{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001420 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001421 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001422
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001423 if (!surface->toysurface && display->dpy &&
1424 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001425 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001426 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001427 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001428 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001429 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001430 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001431
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001432 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001433 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001434 surface->surface,
1435 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001436
Pekka Paalanen89dee002013-02-13 16:17:20 +02001437 surface->cairo_surface = surface->toysurface->prepare(
1438 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001439 allocation.width, allocation.height, flags,
1440 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001441}
1442
1443static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001444window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001445{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001446 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001447 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001448 int dx = 0;
1449 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001450
Pekka Paalanenec076692012-11-30 13:37:27 +02001451 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001452 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001453
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001454 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1455 flags |= SURFACE_HINT_RGB565;
1456
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001457 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1458 dx = surface->server_allocation.width -
1459 surface->allocation.width;
1460
1461 if (window->resize_edges & WINDOW_RESIZING_TOP)
1462 dy = surface->server_allocation.height -
1463 surface->allocation.height;
1464
1465 window->resize_edges = 0;
1466
Pekka Paalanen89dee002013-02-13 16:17:20 +02001467 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001468}
1469
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001470int
1471window_get_buffer_transform(struct window *window)
1472{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001473 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001474}
1475
1476void
1477window_set_buffer_transform(struct window *window,
1478 enum wl_output_transform transform)
1479{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001480 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001481 wl_surface_set_buffer_transform(window->main_surface->surface,
1482 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001483}
1484
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001485void
1486window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001487 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001488{
1489 window->main_surface->buffer_scale = scale;
1490 wl_surface_set_buffer_scale(window->main_surface->surface,
1491 scale);
1492}
1493
1494uint32_t
1495window_get_buffer_scale(struct window *window)
1496{
1497 return window->main_surface->buffer_scale;
1498}
1499
Alexander Larssond68f5232013-05-22 14:41:33 +02001500uint32_t
1501window_get_output_scale(struct window *window)
1502{
1503 struct window_output *window_output;
1504 struct window_output *window_output_tmp;
1505 int scale = 1;
1506
1507 wl_list_for_each_safe(window_output, window_output_tmp,
1508 &window->window_output_list, link) {
1509 if (window_output->output->scale > scale)
1510 scale = window_output->output->scale;
1511 }
1512
1513 return scale;
1514}
1515
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001516static void frame_destroy(struct frame *frame);
1517
Pekka Paalanen4e373742013-02-13 16:17:13 +02001518static void
1519surface_destroy(struct surface *surface)
1520{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001521 if (surface->frame_cb)
1522 wl_callback_destroy(surface->frame_cb);
1523
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001524 if (surface->input_region)
1525 wl_region_destroy(surface->input_region);
1526
1527 if (surface->opaque_region)
1528 wl_region_destroy(surface->opaque_region);
1529
Pekka Paalanen35e82632013-04-25 13:57:48 +03001530 if (surface->subsurface)
1531 wl_subsurface_destroy(surface->subsurface);
1532
Pekka Paalanen4e373742013-02-13 16:17:13 +02001533 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001534
1535 if (surface->toysurface)
1536 surface->toysurface->destroy(surface->toysurface);
1537
Pekka Paalanen35e82632013-04-25 13:57:48 +03001538 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001539 free(surface);
1540}
1541
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001542void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001543window_destroy(struct window *window)
1544{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001545 struct display *display = window->display;
1546 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001547 struct window_output *window_output;
1548 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001549
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001550 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001551
Rusty Lynch1084da52013-08-15 09:10:08 -07001552 wl_list_for_each(input, &display->input_list, link) {
1553 if (input->touch_focus == window)
1554 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001555 if (input->pointer_focus == window)
1556 input->pointer_focus = NULL;
1557 if (input->keyboard_focus == window)
1558 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001559 if (input->focus_widget &&
1560 input->focus_widget->window == window)
1561 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001562 }
1563
Rob Bradford7507b572012-05-15 17:55:34 +01001564 wl_list_for_each_safe(window_output, window_output_tmp,
1565 &window->window_output_list, link) {
1566 free (window_output);
1567 }
1568
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001569 if (window->frame)
1570 frame_destroy(window->frame);
1571
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001572 if (window->shell_surface)
1573 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001574
1575 surface_destroy(window->main_surface);
1576
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001577 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001578
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001579 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001580 free(window);
1581}
1582
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001583static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001584widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001585{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001586 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001587
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001588 wl_list_for_each(child, &widget->child_list, link) {
1589 target = widget_find_widget(child, x, y);
1590 if (target)
1591 return target;
1592 }
1593
1594 if (widget->allocation.x <= x &&
1595 x < widget->allocation.x + widget->allocation.width &&
1596 widget->allocation.y <= y &&
1597 y < widget->allocation.y + widget->allocation.height) {
1598 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001599 }
1600
1601 return NULL;
1602}
1603
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001604static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001605window_find_widget(struct window *window, int32_t x, int32_t y)
1606{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001607 struct surface *surface;
1608 struct widget *widget;
1609
1610 wl_list_for_each(surface, &window->subsurface_list, link) {
1611 widget = widget_find_widget(surface->widget, x, y);
1612 if (widget)
1613 return widget;
1614 }
1615
1616 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001617}
1618
1619static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001620widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001621{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001622 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001623
Peter Huttererf3d62272013-08-08 11:57:05 +10001624 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001625 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001626 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001627 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001628 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001629 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001630 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001631 widget->tooltip = NULL;
1632 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001633 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001634
1635 return widget;
1636}
1637
1638struct widget *
1639window_add_widget(struct window *window, void *data)
1640{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001641 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001642
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001643 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001644 wl_list_init(&widget->link);
1645 window->main_surface->widget = widget;
1646
1647 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001648}
1649
1650struct widget *
1651widget_add_widget(struct widget *parent, void *data)
1652{
1653 struct widget *widget;
1654
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001655 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001656 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001657
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001658 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001659}
1660
1661void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001662widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001664 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001665 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001666 struct input *input;
1667
Pekka Paalanen35e82632013-04-25 13:57:48 +03001668 /* Destroy the sub-surface along with the root widget */
1669 if (surface->widget == widget && surface->subsurface)
1670 surface_destroy(widget->surface);
1671
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001672 if (widget->tooltip) {
1673 free(widget->tooltip);
1674 widget->tooltip = NULL;
1675 }
1676
Pekka Paalanene156fb62012-01-19 13:51:38 +02001677 wl_list_for_each(input, &display->input_list, link) {
1678 if (input->focus_widget == widget)
1679 input->focus_widget = NULL;
1680 }
1681
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001682 wl_list_remove(&widget->link);
1683 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001684}
1685
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001687widget_set_default_cursor(struct widget *widget, int cursor)
1688{
1689 widget->default_cursor = cursor;
1690}
1691
1692void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001693widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001695 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001696}
1697
1698void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001699widget_set_size(struct widget *widget, int32_t width, int32_t height)
1700{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001701 widget->allocation.width = width;
1702 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001703}
1704
1705void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001706widget_set_allocation(struct widget *widget,
1707 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001709 widget->allocation.x = x;
1710 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001711 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001712}
1713
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001714void
1715widget_set_transparent(struct widget *widget, int transparent)
1716{
1717 widget->opaque = !transparent;
1718}
1719
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001720void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001721widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001722{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001723 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001724}
1725
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001726static cairo_surface_t *
1727widget_get_cairo_surface(struct widget *widget)
1728{
1729 struct surface *surface = widget->surface;
1730 struct window *window = widget->window;
1731
1732 if (!surface->cairo_surface) {
1733 if (surface == window->main_surface)
1734 window_create_main_surface(window);
1735 else
1736 surface_create_surface(surface, 0, 0, 0);
1737 }
1738
1739 return surface->cairo_surface;
1740}
1741
Alexander Larsson15901f02013-05-22 14:41:25 +02001742static void
1743widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1744{
1745 struct surface *surface = widget->surface;
1746 double angle;
1747 cairo_matrix_t m;
1748 enum wl_output_transform transform;
1749 int surface_width, surface_height;
1750 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001751 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001752
1753 surface_width = surface->allocation.width;
1754 surface_height = surface->allocation.height;
1755
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001756 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001757 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001758
Alexander Larsson15901f02013-05-22 14:41:25 +02001759 switch (transform) {
1760 case WL_OUTPUT_TRANSFORM_FLIPPED:
1761 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1762 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1763 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1764 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1765 break;
1766 default:
1767 cairo_matrix_init_identity(&m);
1768 break;
1769 }
1770
1771 switch (transform) {
1772 case WL_OUTPUT_TRANSFORM_NORMAL:
1773 default:
1774 angle = 0;
1775 translate_x = 0;
1776 translate_y = 0;
1777 break;
1778 case WL_OUTPUT_TRANSFORM_FLIPPED:
1779 angle = 0;
1780 translate_x = surface_width;
1781 translate_y = 0;
1782 break;
1783 case WL_OUTPUT_TRANSFORM_90:
1784 angle = M_PI_2;
1785 translate_x = surface_height;
1786 translate_y = 0;
1787 break;
1788 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1789 angle = M_PI_2;
1790 translate_x = surface_height;
1791 translate_y = surface_width;
1792 break;
1793 case WL_OUTPUT_TRANSFORM_180:
1794 angle = M_PI;
1795 translate_x = surface_width;
1796 translate_y = surface_height;
1797 break;
1798 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1799 angle = M_PI;
1800 translate_x = 0;
1801 translate_y = surface_height;
1802 break;
1803 case WL_OUTPUT_TRANSFORM_270:
1804 angle = M_PI + M_PI_2;
1805 translate_x = 0;
1806 translate_y = surface_width;
1807 break;
1808 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1809 angle = M_PI + M_PI_2;
1810 translate_x = 0;
1811 translate_y = 0;
1812 break;
1813 }
1814
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001815 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001816 cairo_translate(cr, translate_x, translate_y);
1817 cairo_rotate(cr, angle);
1818 cairo_transform(cr, &m);
1819}
1820
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001821cairo_t *
1822widget_cairo_create(struct widget *widget)
1823{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001824 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001825 cairo_surface_t *cairo_surface;
1826 cairo_t *cr;
1827
1828 cairo_surface = widget_get_cairo_surface(widget);
1829 cr = cairo_create(cairo_surface);
1830
Alexander Larsson15901f02013-05-22 14:41:25 +02001831 widget_cairo_update_transform(widget, cr);
1832
Pekka Paalanen35e82632013-04-25 13:57:48 +03001833 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1834
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001835 return cr;
1836}
1837
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001838struct wl_surface *
1839widget_get_wl_surface(struct widget *widget)
1840{
1841 return widget->surface->surface;
1842}
1843
1844uint32_t
1845widget_get_last_time(struct widget *widget)
1846{
1847 return widget->surface->last_time;
1848}
1849
1850void
1851widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1852{
1853 struct wl_compositor *comp = widget->window->display->compositor;
1854 struct surface *surface = widget->surface;
1855
1856 if (!surface->input_region)
1857 surface->input_region = wl_compositor_create_region(comp);
1858
1859 if (rect) {
1860 wl_region_add(surface->input_region,
1861 rect->x, rect->y, rect->width, rect->height);
1862 }
1863}
1864
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001865void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001866widget_set_resize_handler(struct widget *widget,
1867 widget_resize_handler_t handler)
1868{
1869 widget->resize_handler = handler;
1870}
1871
1872void
1873widget_set_redraw_handler(struct widget *widget,
1874 widget_redraw_handler_t handler)
1875{
1876 widget->redraw_handler = handler;
1877}
1878
1879void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001880widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001881{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001882 widget->enter_handler = handler;
1883}
1884
1885void
1886widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1887{
1888 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001889}
1890
1891void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001892widget_set_motion_handler(struct widget *widget,
1893 widget_motion_handler_t handler)
1894{
1895 widget->motion_handler = handler;
1896}
1897
1898void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001899widget_set_button_handler(struct widget *widget,
1900 widget_button_handler_t handler)
1901{
1902 widget->button_handler = handler;
1903}
1904
1905void
Rusty Lynch041815a2013-08-08 21:20:38 -07001906widget_set_touch_up_handler(struct widget *widget,
1907 widget_touch_up_handler_t handler)
1908{
1909 widget->touch_up_handler = handler;
1910}
1911
1912void
1913widget_set_touch_down_handler(struct widget *widget,
1914 widget_touch_down_handler_t handler)
1915{
1916 widget->touch_down_handler = handler;
1917}
1918
1919void
1920widget_set_touch_motion_handler(struct widget *widget,
1921 widget_touch_motion_handler_t handler)
1922{
1923 widget->touch_motion_handler = handler;
1924}
1925
1926void
1927widget_set_touch_frame_handler(struct widget *widget,
1928 widget_touch_frame_handler_t handler)
1929{
1930 widget->touch_frame_handler = handler;
1931}
1932
1933void
1934widget_set_touch_cancel_handler(struct widget *widget,
1935 widget_touch_cancel_handler_t handler)
1936{
1937 widget->touch_cancel_handler = handler;
1938}
1939
1940void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001941widget_set_axis_handler(struct widget *widget,
1942 widget_axis_handler_t handler)
1943{
1944 widget->axis_handler = handler;
1945}
1946
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001947static void
1948window_schedule_redraw_task(struct window *window);
1949
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001950void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001951widget_schedule_redraw(struct widget *widget)
1952{
Pekka Paalanen71233882013-04-25 13:57:53 +03001953 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001954 widget->surface->redraw_needed = 1;
1955 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001956}
1957
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001958cairo_surface_t *
1959window_get_surface(struct window *window)
1960{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001961 cairo_surface_t *cairo_surface;
1962
1963 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1964
1965 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001966}
1967
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001968struct wl_surface *
1969window_get_wl_surface(struct window *window)
1970{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001971 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001972}
1973
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001974struct wl_shell_surface *
1975window_get_wl_shell_surface(struct window *window)
1976{
1977 return window->shell_surface;
1978}
1979
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001980static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001981tooltip_redraw_handler(struct widget *widget, void *data)
1982{
1983 cairo_t *cr;
1984 const int32_t r = 3;
1985 struct tooltip *tooltip = data;
1986 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001987
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001988 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001989 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1990 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1991 cairo_paint(cr);
1992
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001993 width = widget->allocation.width;
1994 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001995 rounded_rect(cr, 0, 0, width, height, r);
1996
1997 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1998 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1999 cairo_fill(cr);
2000
2001 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2002 cairo_move_to(cr, 10, 16);
2003 cairo_show_text(cr, tooltip->entry);
2004 cairo_destroy(cr);
2005}
2006
2007static cairo_text_extents_t
2008get_text_extents(struct tooltip *tooltip)
2009{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002010 cairo_t *cr;
2011 cairo_text_extents_t extents;
2012
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002013 /* Use the dummy_surface because tooltip's surface was not
2014 * created yet, and parent does not have a valid surface
2015 * outside repaint, either.
2016 */
2017 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002018 cairo_text_extents(cr, tooltip->entry, &extents);
2019 cairo_destroy(cr);
2020
2021 return extents;
2022}
2023
2024static int
2025window_create_tooltip(struct tooltip *tooltip)
2026{
2027 struct widget *parent = tooltip->parent;
2028 struct display *display = parent->window->display;
2029 struct window *window;
2030 const int offset_y = 27;
2031 const int margin = 3;
2032 cairo_text_extents_t extents;
2033
2034 if (tooltip->widget)
2035 return 0;
2036
2037 window = window_create_transient(display, parent->window, tooltip->x,
2038 tooltip->y + offset_y,
2039 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2040 if (!window)
2041 return -1;
2042
2043 tooltip->window = window;
2044 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2045
2046 extents = get_text_extents(tooltip);
2047 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2048 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2049
2050 return 0;
2051}
2052
2053void
2054widget_destroy_tooltip(struct widget *parent)
2055{
2056 struct tooltip *tooltip = parent->tooltip;
2057
2058 parent->tooltip_count = 0;
2059 if (!tooltip)
2060 return;
2061
2062 if (tooltip->widget) {
2063 widget_destroy(tooltip->widget);
2064 window_destroy(tooltip->window);
2065 tooltip->widget = NULL;
2066 tooltip->window = NULL;
2067 }
2068
2069 close(tooltip->tooltip_fd);
2070 free(tooltip->entry);
2071 free(tooltip);
2072 parent->tooltip = NULL;
2073}
2074
2075static void
2076tooltip_func(struct task *task, uint32_t events)
2077{
2078 struct tooltip *tooltip =
2079 container_of(task, struct tooltip, tooltip_task);
2080 uint64_t exp;
2081
Martin Olsson8df662a2012-07-08 03:03:47 +02002082 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2083 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002084 window_create_tooltip(tooltip);
2085}
2086
2087#define TOOLTIP_TIMEOUT 500
2088static int
2089tooltip_timer_reset(struct tooltip *tooltip)
2090{
2091 struct itimerspec its;
2092
2093 its.it_interval.tv_sec = 0;
2094 its.it_interval.tv_nsec = 0;
2095 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2096 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2097 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2098 fprintf(stderr, "could not set timerfd\n: %m");
2099 return -1;
2100 }
2101
2102 return 0;
2103}
2104
2105int
2106widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2107{
2108 struct tooltip *tooltip = parent->tooltip;
2109
2110 parent->tooltip_count++;
2111 if (tooltip) {
2112 tooltip->x = x;
2113 tooltip->y = y;
2114 tooltip_timer_reset(tooltip);
2115 return 0;
2116 }
2117
2118 /* the handler might be triggered too fast via input device motion, so
2119 * we need this check here to make sure tooltip is fully initialized */
2120 if (parent->tooltip_count > 1)
2121 return 0;
2122
2123 tooltip = malloc(sizeof *tooltip);
2124 if (!tooltip)
2125 return -1;
2126
2127 parent->tooltip = tooltip;
2128 tooltip->parent = parent;
2129 tooltip->widget = NULL;
2130 tooltip->window = NULL;
2131 tooltip->x = x;
2132 tooltip->y = y;
2133 tooltip->entry = strdup(entry);
2134 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2135 if (tooltip->tooltip_fd < 0) {
2136 fprintf(stderr, "could not create timerfd\n: %m");
2137 return -1;
2138 }
2139
2140 tooltip->tooltip_task.run = tooltip_func;
2141 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2142 EPOLLIN, &tooltip->tooltip_task);
2143 tooltip_timer_reset(tooltip);
2144
2145 return 0;
2146}
2147
2148static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002149workspace_manager_state(void *data,
2150 struct workspace_manager *workspace_manager,
2151 uint32_t current,
2152 uint32_t count)
2153{
2154 struct display *display = data;
2155
2156 display->workspace = current;
2157 display->workspace_count = count;
2158}
2159
2160static const struct workspace_manager_listener workspace_manager_listener = {
2161 workspace_manager_state
2162};
2163
2164static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002165frame_resize_handler(struct widget *widget,
2166 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002167{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002168 struct frame *frame = data;
2169 struct widget *child = frame->child;
2170 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002171 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002172 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002173 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002174 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002175 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002176 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002177 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002178
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002179 switch (widget->window->type) {
2180 case TYPE_FULLSCREEN:
2181 decoration_width = 0;
2182 decoration_height = 0;
2183
2184 allocation.x = 0;
2185 allocation.y = 0;
2186 allocation.width = width;
2187 allocation.height = height;
2188 opaque_margin = 0;
2189
2190 wl_list_for_each(button, &frame->buttons_list, link)
2191 button->widget->opaque = 1;
2192 break;
2193 case TYPE_MAXIMIZED:
2194 decoration_width = t->width * 2;
2195 decoration_height = t->width + t->titlebar_height;
2196
2197 allocation.x = t->width;
2198 allocation.y = t->titlebar_height;
2199 allocation.width = width - decoration_width;
2200 allocation.height = height - decoration_height;
2201
2202 opaque_margin = 0;
2203
2204 wl_list_for_each(button, &frame->buttons_list, link)
2205 button->widget->opaque = 0;
2206 break;
2207 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002208 decoration_width = (t->width + t->margin) * 2;
2209 decoration_height = t->width +
2210 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002212 allocation.x = t->width + t->margin;
2213 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002214 allocation.width = width - decoration_width;
2215 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002216
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002217 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002218
2219 wl_list_for_each(button, &frame->buttons_list, link)
2220 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002221 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002222 }
2223
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 widget_set_allocation(child, allocation.x, allocation.y,
2225 allocation.width, allocation.height);
2226
2227 if (child->resize_handler)
2228 child->resize_handler(child,
2229 allocation.width,
2230 allocation.height,
2231 child->user_data);
2232
Scott Moreauf7e498c2012-05-14 11:39:29 -06002233 width = child->allocation.width + decoration_width;
2234 height = child->allocation.height + decoration_height;
2235
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002236 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2237
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002238 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002239 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002240 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002241 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002242 shadow_margin, shadow_margin,
2243 width - 2 * shadow_margin,
2244 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002245 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002246 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002247 }
2248
Scott Moreauf7e498c2012-05-14 11:39:29 -06002249 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002250
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002251 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002252 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002253 opaque_margin, opaque_margin,
2254 widget->allocation.width - 2 * opaque_margin,
2255 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002256
2257 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002258 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2259 x_l = t->width + shadow_margin;
2260 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002261 wl_list_for_each(button, &frame->buttons_list, link) {
2262 const int button_padding = 4;
2263 w = cairo_image_surface_get_width(button->icon);
2264 h = cairo_image_surface_get_height(button->icon);
2265
2266 if (button->decoration == FRAME_BUTTON_FANCY)
2267 w += 10;
2268
2269 if (button->align == FRAME_BUTTON_LEFT) {
2270 widget_set_allocation(button->widget,
2271 x_l, y , w + 1, h + 1);
2272 x_l += w;
2273 x_l += button_padding;
2274 } else {
2275 x_r -= w;
2276 widget_set_allocation(button->widget,
2277 x_r, y , w + 1, h + 1);
2278 x_r -= button_padding;
2279 }
2280 }
2281}
2282
2283static int
2284frame_button_enter_handler(struct widget *widget,
2285 struct input *input, float x, float y, void *data)
2286{
2287 struct frame_button *frame_button = data;
2288
2289 widget_schedule_redraw(frame_button->widget);
2290 frame_button->state = FRAME_BUTTON_OVER;
2291
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002292 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002293}
2294
2295static void
2296frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2297{
2298 struct frame_button *frame_button = data;
2299
2300 widget_schedule_redraw(frame_button->widget);
2301 frame_button->state = FRAME_BUTTON_DEFAULT;
2302}
2303
2304static void
2305frame_button_button_handler(struct widget *widget,
2306 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002307 uint32_t button,
2308 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002309{
2310 struct frame_button *frame_button = data;
2311 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002312 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002313
2314 if (button != BTN_LEFT)
2315 return;
2316
2317 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002318 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002319 frame_button->state = FRAME_BUTTON_ACTIVE;
2320 widget_schedule_redraw(frame_button->widget);
2321
2322 if (frame_button->type == FRAME_BUTTON_ICON)
2323 window_show_frame_menu(window, input, time);
2324 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002325 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002326 frame_button->state = FRAME_BUTTON_DEFAULT;
2327 widget_schedule_redraw(frame_button->widget);
2328 break;
2329 }
2330
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002331 if (!was_pressed)
2332 return;
2333
Martin Minarik1998b152012-05-10 02:04:35 +02002334 switch (frame_button->type) {
2335 case FRAME_BUTTON_CLOSE:
2336 if (window->close_handler)
2337 window->close_handler(window->parent,
2338 window->user_data);
2339 else
2340 display_exit(window->display);
2341 break;
2342 case FRAME_BUTTON_MINIMIZE:
2343 fprintf(stderr,"Minimize stub\n");
2344 break;
2345 case FRAME_BUTTON_MAXIMIZE:
2346 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2347 break;
2348 default:
2349 /* Unknown operation */
2350 break;
2351 }
2352}
2353
Rusty Lynch041815a2013-08-08 21:20:38 -07002354static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002355frame_button_touch_down_handler(struct widget *widget, struct input *input,
2356 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch041815a2013-08-08 21:20:38 -07002357 float x, float y, void *data)
2358{
2359 struct frame_button *frame_button = data;
2360 struct window *window = widget->window;
2361
2362 switch (frame_button->type) {
2363 case FRAME_BUTTON_CLOSE:
2364 if (window->close_handler)
2365 window->close_handler(window->parent,
2366 window->user_data);
2367 else
2368 display_exit(window->display);
2369 break;
2370 case FRAME_BUTTON_MINIMIZE:
2371 fprintf(stderr,"Minimize stub\n");
2372 break;
2373 case FRAME_BUTTON_MAXIMIZE:
2374 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2375 break;
2376 default:
2377 /* Unknown operation */
2378 break;
2379 }
2380}
2381
2382
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002383static int
2384frame_button_motion_handler(struct widget *widget,
2385 struct input *input, uint32_t time,
2386 float x, float y, void *data)
2387{
2388 struct frame_button *frame_button = data;
2389 enum frame_button_pointer previous_button_state = frame_button->state;
2390
2391 /* only track state for a pressed button */
2392 if (input->grab != widget)
2393 return CURSOR_LEFT_PTR;
2394
2395 if (x > widget->allocation.x &&
2396 x < (widget->allocation.x + widget->allocation.width) &&
2397 y > widget->allocation.y &&
2398 y < (widget->allocation.y + widget->allocation.height)) {
2399 frame_button->state = FRAME_BUTTON_ACTIVE;
2400 } else {
2401 frame_button->state = FRAME_BUTTON_DEFAULT;
2402 }
2403
2404 if (frame_button->state != previous_button_state)
2405 widget_schedule_redraw(frame_button->widget);
2406
2407 return CURSOR_LEFT_PTR;
2408}
2409
Martin Minarik1998b152012-05-10 02:04:35 +02002410static void
2411frame_button_redraw_handler(struct widget *widget, void *data)
2412{
2413 struct frame_button *frame_button = data;
2414 cairo_t *cr;
2415 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002416
2417 x = widget->allocation.x;
2418 y = widget->allocation.y;
2419 width = widget->allocation.width;
2420 height = widget->allocation.height;
2421
2422 if (!width)
2423 return;
2424 if (!height)
2425 return;
2426 if (widget->opaque)
2427 return;
2428
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002429 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002430
2431 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2432 cairo_set_line_width(cr, 1);
2433
2434 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2435 cairo_rectangle (cr, x, y, 25, 16);
2436
2437 cairo_stroke_preserve(cr);
2438
2439 switch (frame_button->state) {
2440 case FRAME_BUTTON_DEFAULT:
2441 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2442 break;
2443 case FRAME_BUTTON_OVER:
2444 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2445 break;
2446 case FRAME_BUTTON_ACTIVE:
2447 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2448 break;
2449 }
2450
2451 cairo_fill (cr);
2452
2453 x += 4;
2454 }
2455
2456 cairo_set_source_surface(cr, frame_button->icon, x, y);
2457 cairo_paint(cr);
2458
2459 cairo_destroy(cr);
2460}
2461
2462static struct widget *
2463frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2464 enum frame_button_align align, enum frame_button_decoration style)
2465{
2466 struct frame_button *frame_button;
2467 const char *icon = data;
2468
Peter Huttererf3d62272013-08-08 11:57:05 +10002469 frame_button = xzalloc (sizeof *frame_button);
Martin Minarik1998b152012-05-10 02:04:35 +02002470 frame_button->icon = cairo_image_surface_create_from_png(icon);
2471 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2472 frame_button->frame = frame;
2473 frame_button->type = type;
2474 frame_button->align = align;
2475 frame_button->decoration = style;
2476
2477 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2478
2479 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2480 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2481 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
Rusty Lynch041815a2013-08-08 21:20:38 -07002482 widget_set_touch_down_handler(frame_button->widget, frame_button_touch_down_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002483 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002484 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002485 return frame_button->widget;
2486}
2487
2488static void
2489frame_button_destroy(struct frame_button *frame_button)
2490{
2491 widget_destroy(frame_button->widget);
2492 wl_list_remove(&frame_button->link);
2493 cairo_surface_destroy(frame_button->icon);
2494 free(frame_button);
2495
2496 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002497}
2498
2499static void
2500frame_redraw_handler(struct widget *widget, void *data)
2501{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002502 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002503 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002504 struct theme *t = window->display->theme;
2505 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002506
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002507 if (window->type == TYPE_FULLSCREEN)
2508 return;
2509
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002510 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002511
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002512 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002513 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002514 if (window->type == TYPE_MAXIMIZED)
2515 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002516 theme_render_frame(t, cr, widget->allocation.width,
2517 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002518
2519 cairo_destroy(cr);
2520}
2521
2522static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002523frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002524{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002525 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002526 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002527 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002528
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002529 if (window->type != TYPE_TOPLEVEL)
2530 return CURSOR_LEFT_PTR;
2531
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002532 location = theme_get_location(t, input->sx, input->sy,
2533 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002534 frame->widget->allocation.height,
2535 window->type == TYPE_MAXIMIZED ?
2536 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002537
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002538 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002539 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002540 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002541 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002542 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002543 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002544 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002545 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002546 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002547 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002548 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002549 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002550 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002551 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002552 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002553 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002554 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002555 case THEME_LOCATION_EXTERIOR:
2556 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002557 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002558 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002559 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002560}
2561
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002562static void
2563frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002564{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002565 struct display *display;
2566
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002567 switch (index) {
2568 case 0: /* close */
2569 if (window->close_handler)
2570 window->close_handler(window->parent,
2571 window->user_data);
2572 else
2573 display_exit(window->display);
2574 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002575 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002576 display = window->display;
2577 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002578 workspace_manager_move_surface(
2579 display->workspace_manager,
2580 window->main_surface->surface,
2581 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002582 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002583 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002584 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002585 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002586 workspace_manager_move_surface(
2587 display->workspace_manager,
2588 window->main_surface->surface,
2589 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002590 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002591 case 3: /* fullscreen */
2592 /* we don't have a way to get out of fullscreen for now */
2593 if (window->fullscreen_handler)
2594 window->fullscreen_handler(window, window->user_data);
2595 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002596 }
2597}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002598
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002599void
2600window_show_frame_menu(struct window *window,
2601 struct input *input, uint32_t time)
2602{
2603 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002604 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002605
2606 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002607 "Close",
2608 "Move to workspace above", "Move to workspace below",
2609 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002610 };
2611
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002612 if (window->fullscreen_handler)
2613 count = ARRAY_LENGTH(entries);
2614 else
2615 count = ARRAY_LENGTH(entries) - 1;
2616
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002617 input_get_position(input, &x, &y);
2618 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002619 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002620}
2621
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002622static int
2623frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002624 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002625{
2626 return frame_get_pointer_image_for_location(data, input);
2627}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002628
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002629static int
2630frame_motion_handler(struct widget *widget,
2631 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002632 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002633{
2634 return frame_get_pointer_image_for_location(data, input);
2635}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002636
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002637static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002638frame_button_handler(struct widget *widget,
2639 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002640 uint32_t button, enum wl_pointer_button_state state,
2641 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002642
2643{
2644 struct frame *frame = data;
2645 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002646 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002647 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002648
Rob Bradford3c9d9672013-07-11 10:35:55 +01002649 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002650 return;
2651
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002652 location = theme_get_location(display->theme, input->sx, input->sy,
2653 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002654 frame->widget->allocation.height,
2655 window->type == TYPE_MAXIMIZED ?
2656 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002657
Daniel Stone4dbadb12012-05-30 16:31:51 +01002658 if (window->display->shell && button == BTN_LEFT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002659 window->type == TYPE_TOPLEVEL) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002660 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002661 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002662 if (!window->shell_surface)
2663 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002664 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002665 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002666 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002667 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002668 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002669 case THEME_LOCATION_RESIZING_TOP:
2670 case THEME_LOCATION_RESIZING_BOTTOM:
2671 case THEME_LOCATION_RESIZING_LEFT:
2672 case THEME_LOCATION_RESIZING_RIGHT:
2673 case THEME_LOCATION_RESIZING_TOP_LEFT:
2674 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2675 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2676 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002677 if (!window->shell_surface)
2678 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002679 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002680
Pekka Paalanen99436862012-11-19 17:15:59 +02002681 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002682 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002683 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002684 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002685 break;
2686 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002687 } else if (button == BTN_RIGHT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002688 (window->type == TYPE_TOPLEVEL ||
2689 window->type == TYPE_MAXIMIZED)) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002690 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002691 }
2692}
2693
Rusty Lynch1084da52013-08-15 09:10:08 -07002694static void
2695frame_touch_down_handler(struct widget *widget, struct input *input,
2696 uint32_t serial, uint32_t time, int32_t id,
2697 float x, float y, void *data)
2698{
2699 struct window *window = widget->window;
2700 struct display *display = window->display;
2701
2702 wl_shell_surface_move(window->shell_surface,
2703 input_get_seat(input),
2704 display->serial);
2705}
2706
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002707struct widget *
2708frame_create(struct window *window, void *data)
2709{
2710 struct frame *frame;
2711
Peter Huttererf3d62272013-08-08 11:57:05 +10002712 frame = xzalloc(sizeof *frame);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002713 frame->widget = window_add_widget(window, frame);
2714 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002715
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002716 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2717 widget_set_resize_handler(frame->widget, frame_resize_handler);
2718 widget_set_enter_handler(frame->widget, frame_enter_handler);
2719 widget_set_motion_handler(frame->widget, frame_motion_handler);
2720 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002721 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002722
Martin Minarik1998b152012-05-10 02:04:35 +02002723 /* Create empty list for frame buttons */
2724 wl_list_init(&frame->buttons_list);
2725
2726 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2727 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2728
2729 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2730 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2731
2732 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2733 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2734
2735 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2736 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2737
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002738 window->frame = frame;
2739
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002740 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002741}
2742
Kristian Høgsberga1627922012-06-20 17:30:03 -04002743void
2744frame_set_child_size(struct widget *widget, int child_width, int child_height)
2745{
2746 struct display *display = widget->window->display;
2747 struct theme *t = display->theme;
2748 int decoration_width, decoration_height;
2749 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002750 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002751
2752 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002753 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002754 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002755 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002756
2757 width = child_width + decoration_width;
2758 height = child_height + decoration_height;
2759 } else {
2760 width = child_width;
2761 height = child_height;
2762 }
2763
2764 window_schedule_resize(widget->window, width, height);
2765}
2766
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002767static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002768frame_destroy(struct frame *frame)
2769{
Martin Minarik1998b152012-05-10 02:04:35 +02002770 struct frame_button *button, *tmp;
2771
2772 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2773 frame_button_destroy(button);
2774
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002775 /* frame->child must be destroyed by the application */
2776 widget_destroy(frame->widget);
2777 free(frame);
2778}
2779
2780static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002781input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002782 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002783{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002784 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002785 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002786
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002787 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002788 return;
2789
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002790 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002791 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002792 widget = old;
2793 if (input->grab)
2794 widget = input->grab;
2795 if (widget->leave_handler)
2796 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002797 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002798 }
2799
2800 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002801 widget = focus;
2802 if (input->grab)
2803 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002804 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002805 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002806 cursor = widget->enter_handler(focus, input, x, y,
2807 widget->user_data);
2808 else
2809 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002810
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002811 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002812 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002813}
2814
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002815void
2816input_grab(struct input *input, struct widget *widget, uint32_t button)
2817{
2818 input->grab = widget;
2819 input->grab_button = button;
2820}
2821
2822void
2823input_ungrab(struct input *input)
2824{
2825 struct widget *widget;
2826
2827 input->grab = NULL;
2828 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002829 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002830 input->sx, input->sy);
2831 input_set_focus_widget(input, widget, input->sx, input->sy);
2832 }
2833}
2834
2835static void
2836input_remove_pointer_focus(struct input *input)
2837{
2838 struct window *window = input->pointer_focus;
2839
2840 if (!window)
2841 return;
2842
2843 input_set_focus_widget(input, NULL, 0, 0);
2844
2845 input->pointer_focus = NULL;
2846 input->current_cursor = CURSOR_UNSET;
2847}
2848
2849static void
2850pointer_handle_enter(void *data, struct wl_pointer *pointer,
2851 uint32_t serial, struct wl_surface *surface,
2852 wl_fixed_t sx_w, wl_fixed_t sy_w)
2853{
2854 struct input *input = data;
2855 struct window *window;
2856 struct widget *widget;
2857 float sx = wl_fixed_to_double(sx_w);
2858 float sy = wl_fixed_to_double(sy_w);
2859
2860 if (!surface) {
2861 /* enter event for a window we've just destroyed */
2862 return;
2863 }
2864
2865 input->display->serial = serial;
2866 input->pointer_enter_serial = serial;
2867 input->pointer_focus = wl_surface_get_user_data(surface);
2868 window = input->pointer_focus;
2869
Pekka Paalanen99436862012-11-19 17:15:59 +02002870 if (window->resizing) {
2871 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002872 /* Schedule a redraw to free the pool */
2873 window_schedule_redraw(window);
2874 }
2875
2876 input->sx = sx;
2877 input->sy = sy;
2878
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002879 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002880 input_set_focus_widget(input, widget, sx, sy);
2881}
2882
2883static void
2884pointer_handle_leave(void *data, struct wl_pointer *pointer,
2885 uint32_t serial, struct wl_surface *surface)
2886{
2887 struct input *input = data;
2888
2889 input->display->serial = serial;
2890 input_remove_pointer_focus(input);
2891}
2892
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002893static void
Daniel Stone37816df2012-05-16 18:45:18 +01002894pointer_handle_motion(void *data, struct wl_pointer *pointer,
2895 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002896{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002897 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002898 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002899 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002900 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002901 float sx = wl_fixed_to_double(sx_w);
2902 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002903
Paul Winwoodb22bf572013-08-29 10:52:54 +01002904 input->sx = sx;
2905 input->sy = sy;
2906
2907 if (!window)
2908 return;
2909
Rob Bradford5f087742013-07-11 19:41:27 +01002910 /* when making the window smaller - e.g. after a unmaximise we might
2911 * still have a pending motion event that the compositor has picked
2912 * based on the old surface dimensions
2913 */
2914 if (sx > window->main_surface->allocation.width ||
2915 sy > window->main_surface->allocation.height)
2916 return;
2917
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002918 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002919 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002920 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002921 }
2922
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002923 if (input->grab)
2924 widget = input->grab;
2925 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002926 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002927 if (widget) {
2928 if (widget->motion_handler)
2929 cursor = widget->motion_handler(input->focus_widget,
2930 input, time, sx, sy,
2931 widget->user_data);
2932 else
2933 cursor = widget->default_cursor;
2934 } else
2935 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002936
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002937 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002938}
2939
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002940static void
Daniel Stone37816df2012-05-16 18:45:18 +01002941pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002942 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002943{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002944 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002945 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002946 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002947
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002948 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002949 if (input->focus_widget && input->grab == NULL &&
2950 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002951 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002952
Neil Roberts6b28aad2012-01-23 19:11:18 +00002953 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002954 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002955 (*widget->button_handler)(widget,
2956 input, time,
2957 button, state,
2958 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002959
Daniel Stone4dbadb12012-05-30 16:31:51 +01002960 if (input->grab && input->grab_button == button &&
2961 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002962 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002963}
2964
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002965static void
Daniel Stone37816df2012-05-16 18:45:18 +01002966pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002967 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002968{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002969 struct input *input = data;
2970 struct widget *widget;
2971
2972 widget = input->focus_widget;
2973 if (input->grab)
2974 widget = input->grab;
2975 if (widget && widget->axis_handler)
2976 (*widget->axis_handler)(widget,
2977 input, time,
2978 axis, value,
2979 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002980}
2981
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002982static const struct wl_pointer_listener pointer_listener = {
2983 pointer_handle_enter,
2984 pointer_handle_leave,
2985 pointer_handle_motion,
2986 pointer_handle_button,
2987 pointer_handle_axis,
2988};
2989
2990static void
2991input_remove_keyboard_focus(struct input *input)
2992{
2993 struct window *window = input->keyboard_focus;
2994 struct itimerspec its;
2995
2996 its.it_interval.tv_sec = 0;
2997 its.it_interval.tv_nsec = 0;
2998 its.it_value.tv_sec = 0;
2999 its.it_value.tv_nsec = 0;
3000 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3001
3002 if (!window)
3003 return;
3004
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003005 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003006 if (window->keyboard_focus_handler)
3007 (*window->keyboard_focus_handler)(window, NULL,
3008 window->user_data);
3009
3010 input->keyboard_focus = NULL;
3011}
3012
3013static void
3014keyboard_repeat_func(struct task *task, uint32_t events)
3015{
3016 struct input *input =
3017 container_of(task, struct input, repeat_task);
3018 struct window *window = input->keyboard_focus;
3019 uint64_t exp;
3020
3021 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
3022 /* If we change the timer between the fd becoming
3023 * readable and getting here, there'll be nothing to
3024 * read and we get EAGAIN. */
3025 return;
3026
3027 if (window && window->key_handler) {
3028 (*window->key_handler)(window, input, input->repeat_time,
3029 input->repeat_key, input->repeat_sym,
3030 WL_KEYBOARD_KEY_STATE_PRESSED,
3031 window->user_data);
3032 }
3033}
3034
3035static void
3036keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
3037 uint32_t format, int fd, uint32_t size)
3038{
3039 struct input *input = data;
3040 char *map_str;
3041
3042 if (!data) {
3043 close(fd);
3044 return;
3045 }
3046
3047 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3048 close(fd);
3049 return;
3050 }
3051
3052 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3053 if (map_str == MAP_FAILED) {
3054 close(fd);
3055 return;
3056 }
3057
3058 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
3059 map_str,
3060 XKB_KEYMAP_FORMAT_TEXT_V1,
3061 0);
3062 munmap(map_str, size);
3063 close(fd);
3064
3065 if (!input->xkb.keymap) {
3066 fprintf(stderr, "failed to compile keymap\n");
3067 return;
3068 }
3069
3070 input->xkb.state = xkb_state_new(input->xkb.keymap);
3071 if (!input->xkb.state) {
3072 fprintf(stderr, "failed to create XKB state\n");
3073 xkb_map_unref(input->xkb.keymap);
3074 input->xkb.keymap = NULL;
3075 return;
3076 }
3077
3078 input->xkb.control_mask =
3079 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
3080 input->xkb.alt_mask =
3081 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
3082 input->xkb.shift_mask =
3083 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
3084}
3085
3086static void
3087keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3088 uint32_t serial, struct wl_surface *surface,
3089 struct wl_array *keys)
3090{
3091 struct input *input = data;
3092 struct window *window;
3093
3094 input->display->serial = serial;
3095 input->keyboard_focus = wl_surface_get_user_data(surface);
3096
3097 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003098 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003099 if (window->keyboard_focus_handler)
3100 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003101 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003102}
3103
3104static void
3105keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3106 uint32_t serial, struct wl_surface *surface)
3107{
3108 struct input *input = data;
3109
3110 input->display->serial = serial;
3111 input_remove_keyboard_focus(input);
3112}
3113
Scott Moreau210d0792012-03-22 10:47:01 -06003114static void
Daniel Stone37816df2012-05-16 18:45:18 +01003115keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003116 uint32_t serial, uint32_t time, uint32_t key,
3117 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003118{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003119 struct input *input = data;
3120 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003121 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003122 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003123 const xkb_keysym_t *syms;
3124 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003125 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003126
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003127 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003128 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003129 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003130 return;
3131
Daniel Stone97f68542012-05-30 16:32:01 +01003132 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003133
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003134 sym = XKB_KEY_NoSymbol;
3135 if (num_syms == 1)
3136 sym = syms[0];
3137
3138 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003139 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003140 window_set_maximized(window,
3141 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003142 } else if (sym == XKB_KEY_F11 &&
3143 window->fullscreen_handler &&
3144 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3145 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003146 } else if (sym == XKB_KEY_F4 &&
3147 input->modifiers == MOD_ALT_MASK &&
3148 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3149 if (window->close_handler)
3150 window->close_handler(window->parent,
3151 window->user_data);
3152 else
3153 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003154 } else if (window->key_handler) {
3155 (*window->key_handler)(window, input, time, key,
3156 sym, state, window->user_data);
3157 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003158
3159 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3160 key == input->repeat_key) {
3161 its.it_interval.tv_sec = 0;
3162 its.it_interval.tv_nsec = 0;
3163 its.it_value.tv_sec = 0;
3164 its.it_value.tv_nsec = 0;
3165 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3166 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3167 input->repeat_sym = sym;
3168 input->repeat_key = key;
3169 input->repeat_time = time;
3170 its.it_interval.tv_sec = 0;
3171 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3172 its.it_value.tv_sec = 0;
3173 its.it_value.tv_nsec = 400 * 1000 * 1000;
3174 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3175 }
3176}
3177
3178static void
Daniel Stone351eb612012-05-31 15:27:47 -04003179keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3180 uint32_t serial, uint32_t mods_depressed,
3181 uint32_t mods_latched, uint32_t mods_locked,
3182 uint32_t group)
3183{
3184 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003185 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003186
Matt Ropere61561f2013-06-24 16:52:43 +01003187 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3188 if (!input->xkb.keymap)
3189 return;
3190
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003191 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3192 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003193 mask = xkb_state_serialize_mods(input->xkb.state,
3194 XKB_STATE_DEPRESSED |
3195 XKB_STATE_LATCHED);
3196 input->modifiers = 0;
3197 if (mask & input->xkb.control_mask)
3198 input->modifiers |= MOD_CONTROL_MASK;
3199 if (mask & input->xkb.alt_mask)
3200 input->modifiers |= MOD_ALT_MASK;
3201 if (mask & input->xkb.shift_mask)
3202 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003203}
3204
Daniel Stone37816df2012-05-16 18:45:18 +01003205static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003206 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003207 keyboard_handle_enter,
3208 keyboard_handle_leave,
3209 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003210 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003211};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003212
3213static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003214touch_handle_down(void *data, struct wl_touch *wl_touch,
3215 uint32_t serial, uint32_t time, struct wl_surface *surface,
3216 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3217{
3218 struct input *input = data;
3219 struct widget *widget;
3220 float sx = wl_fixed_to_double(x_w);
3221 float sy = wl_fixed_to_double(y_w);
3222
Rusty Lynch1084da52013-08-15 09:10:08 -07003223 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003224 input->touch_focus = wl_surface_get_user_data(surface);
3225 if (!input->touch_focus) {
3226 DBG("Failed to find to touch focus for surface %p\n", surface);
3227 return;
3228 }
3229
3230 widget = window_find_widget(input->touch_focus,
3231 wl_fixed_to_double(x_w),
3232 wl_fixed_to_double(y_w));
3233 if (widget) {
3234 struct touch_point *tp = xmalloc(sizeof *tp);
3235 if (tp) {
3236 tp->id = id;
3237 tp->widget = widget;
3238 wl_list_insert(&input->touch_point_list, &tp->link);
3239
3240 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003241 (*widget->touch_down_handler)(widget, input,
3242 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003243 sx, sy,
3244 widget->user_data);
3245 }
3246 }
3247}
3248
3249static void
3250touch_handle_up(void *data, struct wl_touch *wl_touch,
3251 uint32_t serial, uint32_t time, int32_t id)
3252{
3253 struct input *input = data;
3254 struct touch_point *tp, *tmp;
3255
Rusty Lynch041815a2013-08-08 21:20:38 -07003256 if (!input->touch_focus) {
3257 DBG("No touch focus found for touch up event!\n");
3258 return;
3259 }
3260
3261 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3262 if (tp->id != id)
3263 continue;
3264
3265 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003266 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003267 time, id,
3268 tp->widget->user_data);
3269
3270 wl_list_remove(&tp->link);
3271 free(tp);
3272
3273 return;
3274 }
3275}
3276
3277static void
3278touch_handle_motion(void *data, struct wl_touch *wl_touch,
3279 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3280{
3281 struct input *input = data;
3282 struct touch_point *tp;
3283 float sx = wl_fixed_to_double(x_w);
3284 float sy = wl_fixed_to_double(y_w);
3285
3286 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3287
3288 if (!input->touch_focus) {
3289 DBG("No touch focus found for touch motion event!\n");
3290 return;
3291 }
3292
3293 wl_list_for_each(tp, &input->touch_point_list, link) {
3294 if (tp->id != id)
3295 continue;
3296
3297 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003298 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003299 id, sx, sy,
3300 tp->widget->user_data);
3301 return;
3302 }
3303}
3304
3305static void
3306touch_handle_frame(void *data, struct wl_touch *wl_touch)
3307{
3308 struct input *input = data;
3309 struct touch_point *tp, *tmp;
3310
3311 DBG("touch_handle_frame\n");
3312
3313 if (!input->touch_focus) {
3314 DBG("No touch focus found for touch frame event!\n");
3315 return;
3316 }
3317
3318 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3319 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003320 (*tp->widget->touch_frame_handler)(tp->widget, input,
3321 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003322
3323 wl_list_remove(&tp->link);
3324 free(tp);
3325 }
3326}
3327
3328static void
3329touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3330{
3331 struct input *input = data;
3332 struct touch_point *tp, *tmp;
3333
3334 DBG("touch_handle_cancel\n");
3335
3336 if (!input->touch_focus) {
3337 DBG("No touch focus found for touch cancel event!\n");
3338 return;
3339 }
3340
3341 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3342 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003343 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3344 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003345
3346 wl_list_remove(&tp->link);
3347 free(tp);
3348 }
3349}
3350
3351static const struct wl_touch_listener touch_listener = {
3352 touch_handle_down,
3353 touch_handle_up,
3354 touch_handle_motion,
3355 touch_handle_frame,
3356 touch_handle_cancel,
3357};
3358
3359static void
Daniel Stone37816df2012-05-16 18:45:18 +01003360seat_handle_capabilities(void *data, struct wl_seat *seat,
3361 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003362{
Daniel Stone37816df2012-05-16 18:45:18 +01003363 struct input *input = data;
3364
3365 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3366 input->pointer = wl_seat_get_pointer(seat);
3367 wl_pointer_set_user_data(input->pointer, input);
3368 wl_pointer_add_listener(input->pointer, &pointer_listener,
3369 input);
3370 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3371 wl_pointer_destroy(input->pointer);
3372 input->pointer = NULL;
3373 }
3374
3375 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3376 input->keyboard = wl_seat_get_keyboard(seat);
3377 wl_keyboard_set_user_data(input->keyboard, input);
3378 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3379 input);
3380 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3381 wl_keyboard_destroy(input->keyboard);
3382 input->keyboard = NULL;
3383 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003384
3385 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3386 input->touch = wl_seat_get_touch(seat);
3387 wl_touch_set_user_data(input->touch, input);
3388 wl_touch_add_listener(input->touch, &touch_listener, input);
3389 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3390 wl_touch_destroy(input->touch);
3391 input->touch = NULL;
3392 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003393}
3394
Rob Bradford08031182013-08-13 20:11:03 +01003395static void
3396seat_handle_name(void *data, struct wl_seat *seat,
3397 const char *name)
3398{
3399
3400}
3401
Daniel Stone37816df2012-05-16 18:45:18 +01003402static const struct wl_seat_listener seat_listener = {
3403 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003404 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003405};
3406
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003407void
3408input_get_position(struct input *input, int32_t *x, int32_t *y)
3409{
3410 *x = input->sx;
3411 *y = input->sy;
3412}
3413
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003414struct display *
3415input_get_display(struct input *input)
3416{
3417 return input->display;
3418}
3419
Daniel Stone37816df2012-05-16 18:45:18 +01003420struct wl_seat *
3421input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003422{
Daniel Stone37816df2012-05-16 18:45:18 +01003423 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003424}
3425
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003426uint32_t
3427input_get_modifiers(struct input *input)
3428{
3429 return input->modifiers;
3430}
3431
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003432struct widget *
3433input_get_focus_widget(struct input *input)
3434{
3435 return input->focus_widget;
3436}
3437
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003438struct data_offer {
3439 struct wl_data_offer *offer;
3440 struct input *input;
3441 struct wl_array types;
3442 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003443
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003444 struct task io_task;
3445 int fd;
3446 data_func_t func;
3447 int32_t x, y;
3448 void *user_data;
3449};
3450
3451static void
3452data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3453{
3454 struct data_offer *offer = data;
3455 char **p;
3456
3457 p = wl_array_add(&offer->types, sizeof *p);
3458 *p = strdup(type);
3459}
3460
3461static const struct wl_data_offer_listener data_offer_listener = {
3462 data_offer_offer,
3463};
3464
3465static void
3466data_offer_destroy(struct data_offer *offer)
3467{
3468 char **p;
3469
3470 offer->refcount--;
3471 if (offer->refcount == 0) {
3472 wl_data_offer_destroy(offer->offer);
3473 for (p = offer->types.data; *p; p++)
3474 free(*p);
3475 wl_array_release(&offer->types);
3476 free(offer);
3477 }
3478}
3479
3480static void
3481data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003482 struct wl_data_device *data_device,
3483 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003484{
3485 struct data_offer *offer;
3486
Brian Lovinbc919262013-08-07 15:34:59 -07003487 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003488
3489 wl_array_init(&offer->types);
3490 offer->refcount = 1;
3491 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003492 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003493 wl_data_offer_add_listener(offer->offer,
3494 &data_offer_listener, offer);
3495}
3496
3497static void
3498data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003499 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003500 wl_fixed_t x_w, wl_fixed_t y_w,
3501 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003502{
3503 struct input *input = data;
3504 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003505 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003506 float x = wl_fixed_to_double(x_w);
3507 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003508 char **p;
3509
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003510 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003511 window = wl_surface_get_user_data(surface);
3512 input->pointer_focus = window;
3513
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003514 if (offer) {
3515 input->drag_offer = wl_data_offer_get_user_data(offer);
3516
3517 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3518 *p = NULL;
3519
3520 types_data = input->drag_offer->types.data;
3521 } else {
3522 input->drag_offer = NULL;
3523 types_data = NULL;
3524 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003525
3526 window = input->pointer_focus;
3527 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003528 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003529 window->user_data);
3530}
3531
3532static void
3533data_device_leave(void *data, struct wl_data_device *data_device)
3534{
3535 struct input *input = data;
3536
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003537 if (input->drag_offer) {
3538 data_offer_destroy(input->drag_offer);
3539 input->drag_offer = NULL;
3540 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003541}
3542
3543static void
3544data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003545 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003546{
3547 struct input *input = data;
3548 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003549 float x = wl_fixed_to_double(x_w);
3550 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003551 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003552
3553 input->sx = x;
3554 input->sy = y;
3555
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003556 if (input->drag_offer)
3557 types_data = input->drag_offer->types.data;
3558 else
3559 types_data = NULL;
3560
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003561 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003562 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003563 window->user_data);
3564}
3565
3566static void
3567data_device_drop(void *data, struct wl_data_device *data_device)
3568{
3569 struct input *input = data;
3570 struct window *window = input->pointer_focus;
3571
3572 if (window->drop_handler)
3573 window->drop_handler(window, input,
3574 input->sx, input->sy, window->user_data);
3575}
3576
3577static void
3578data_device_selection(void *data,
3579 struct wl_data_device *wl_data_device,
3580 struct wl_data_offer *offer)
3581{
3582 struct input *input = data;
3583 char **p;
3584
3585 if (input->selection_offer)
3586 data_offer_destroy(input->selection_offer);
3587
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003588 if (offer) {
3589 input->selection_offer = wl_data_offer_get_user_data(offer);
3590 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3591 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003592 } else {
3593 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003594 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003595}
3596
3597static const struct wl_data_device_listener data_device_listener = {
3598 data_device_data_offer,
3599 data_device_enter,
3600 data_device_leave,
3601 data_device_motion,
3602 data_device_drop,
3603 data_device_selection
3604};
3605
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003606static void
3607input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003608{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003609 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003610 struct wl_cursor *cursor;
3611 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003612
Daniel Stone80972742012-11-07 17:51:39 +11003613 if (!input->pointer)
3614 return;
3615
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003616 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003617 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003618 return;
3619
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003620 if (index >= (int) cursor->image_count) {
3621 fprintf(stderr, "cursor index out of range\n");
3622 return;
3623 }
3624
3625 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003626 buffer = wl_cursor_image_get_buffer(image);
3627 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003628 return;
3629
Kristian Høgsbergae277372012-08-01 09:41:08 -04003630 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003631 input->pointer_surface,
3632 image->hotspot_x, image->hotspot_y);
3633 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3634 wl_surface_damage(input->pointer_surface, 0, 0,
3635 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003636 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003637}
3638
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003639static const struct wl_callback_listener pointer_surface_listener;
3640
3641static void
3642pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3643 uint32_t time)
3644{
3645 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003646 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003647 int i;
3648
3649 if (callback) {
3650 assert(callback == input->cursor_frame_cb);
3651 wl_callback_destroy(callback);
3652 input->cursor_frame_cb = NULL;
3653 }
3654
Daniel Stone80972742012-11-07 17:51:39 +11003655 if (!input->pointer)
3656 return;
3657
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003658 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003659 wl_pointer_set_cursor(input->pointer,
3660 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003661 NULL, 0, 0);
3662 return;
3663 }
3664
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003665 if (input->current_cursor == CURSOR_UNSET)
3666 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003667 cursor = input->display->cursors[input->current_cursor];
3668 if (!cursor)
3669 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003670
3671 /* FIXME We don't have the current time on the first call so we set
3672 * the animation start to the time of the first frame callback. */
3673 if (time == 0)
3674 input->cursor_anim_start = 0;
3675 else if (input->cursor_anim_start == 0)
3676 input->cursor_anim_start = time;
3677
3678 if (time == 0 || input->cursor_anim_start == 0)
3679 i = 0;
3680 else
3681 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3682
Pekka Paalanenbc106382012-10-10 12:49:31 +03003683 if (cursor->image_count > 1) {
3684 input->cursor_frame_cb =
3685 wl_surface_frame(input->pointer_surface);
3686 wl_callback_add_listener(input->cursor_frame_cb,
3687 &pointer_surface_listener, input);
3688 }
3689
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003690 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003691}
3692
3693static const struct wl_callback_listener pointer_surface_listener = {
3694 pointer_surface_frame_callback
3695};
3696
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003697void
3698input_set_pointer_image(struct input *input, int pointer)
3699{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003700 int force = 0;
3701
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003702 if (!input->pointer)
3703 return;
3704
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003705 if (input->pointer_enter_serial > input->cursor_serial)
3706 force = 1;
3707
3708 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003709 return;
3710
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003711 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003712 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003713 if (!input->cursor_frame_cb)
3714 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003715 else if (force) {
3716 /* The current frame callback may be stuck if, for instance,
3717 * the set cursor request was processed by the server after
3718 * this client lost the focus. In this case the cursor surface
3719 * might not be mapped and the frame callback wouldn't ever
3720 * complete. Send a set_cursor and attach to try to map the
3721 * cursor surface again so that the callback will finish */
3722 input_set_pointer_image_index(input, 0);
3723 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003724}
3725
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003726struct wl_data_device *
3727input_get_data_device(struct input *input)
3728{
3729 return input->data_device;
3730}
3731
3732void
3733input_set_selection(struct input *input,
3734 struct wl_data_source *source, uint32_t time)
3735{
3736 wl_data_device_set_selection(input->data_device, source, time);
3737}
3738
3739void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003740input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003741{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003742 wl_data_offer_accept(input->drag_offer->offer,
3743 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003744}
3745
3746static void
3747offer_io_func(struct task *task, uint32_t events)
3748{
3749 struct data_offer *offer =
3750 container_of(task, struct data_offer, io_task);
3751 unsigned int len;
3752 char buffer[4096];
3753
3754 len = read(offer->fd, buffer, sizeof buffer);
3755 offer->func(buffer, len,
3756 offer->x, offer->y, offer->user_data);
3757
3758 if (len == 0) {
3759 close(offer->fd);
3760 data_offer_destroy(offer);
3761 }
3762}
3763
3764static void
3765data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3766 data_func_t func, void *user_data)
3767{
3768 int p[2];
3769
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003770 if (pipe2(p, O_CLOEXEC) == -1)
3771 return;
3772
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003773 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3774 close(p[1]);
3775
3776 offer->io_task.run = offer_io_func;
3777 offer->fd = p[0];
3778 offer->func = func;
3779 offer->refcount++;
3780 offer->user_data = user_data;
3781
3782 display_watch_fd(offer->input->display,
3783 offer->fd, EPOLLIN, &offer->io_task);
3784}
3785
3786void
3787input_receive_drag_data(struct input *input, const char *mime_type,
3788 data_func_t func, void *data)
3789{
3790 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3791 input->drag_offer->x = input->sx;
3792 input->drag_offer->y = input->sy;
3793}
3794
3795int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003796input_receive_drag_data_to_fd(struct input *input,
3797 const char *mime_type, int fd)
3798{
3799 if (input->drag_offer)
3800 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3801
3802 return 0;
3803}
3804
3805int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003806input_receive_selection_data(struct input *input, const char *mime_type,
3807 data_func_t func, void *data)
3808{
3809 char **p;
3810
3811 if (input->selection_offer == NULL)
3812 return -1;
3813
3814 for (p = input->selection_offer->types.data; *p; p++)
3815 if (strcmp(mime_type, *p) == 0)
3816 break;
3817
3818 if (*p == NULL)
3819 return -1;
3820
3821 data_offer_receive_data(input->selection_offer,
3822 mime_type, func, data);
3823 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003824}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003825
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003826int
3827input_receive_selection_data_to_fd(struct input *input,
3828 const char *mime_type, int fd)
3829{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003830 if (input->selection_offer)
3831 wl_data_offer_receive(input->selection_offer->offer,
3832 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003833
3834 return 0;
3835}
3836
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003837void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003838window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003839{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003840 if (!window->shell_surface)
3841 return;
3842
Daniel Stone37816df2012-05-16 18:45:18 +01003843 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003844}
3845
Rusty Lynch1084da52013-08-15 09:10:08 -07003846void
3847window_touch_move(struct window *window, struct input *input, uint32_t serial)
3848{
3849 if (!window->shell_surface)
3850 return;
3851
3852 wl_shell_surface_move(window->shell_surface, input->seat,
3853 window->display->serial);
3854}
3855
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003856static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003857surface_set_synchronized(struct surface *surface)
3858{
3859 if (!surface->subsurface)
3860 return;
3861
3862 if (surface->synchronized)
3863 return;
3864
3865 wl_subsurface_set_sync(surface->subsurface);
3866 surface->synchronized = 1;
3867}
3868
3869static void
3870surface_set_synchronized_default(struct surface *surface)
3871{
3872 if (!surface->subsurface)
3873 return;
3874
3875 if (surface->synchronized == surface->synchronized_default)
3876 return;
3877
3878 if (surface->synchronized_default)
3879 wl_subsurface_set_sync(surface->subsurface);
3880 else
3881 wl_subsurface_set_desync(surface->subsurface);
3882
3883 surface->synchronized = surface->synchronized_default;
3884}
3885
3886static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003887surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003888{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003889 struct widget *widget = surface->widget;
3890 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003891
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003892 if (surface->input_region) {
3893 wl_region_destroy(surface->input_region);
3894 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003895 }
3896
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003897 if (surface->opaque_region)
3898 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003899
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003900 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003901
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003902 if (widget->resize_handler)
3903 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003904 widget->allocation.width,
3905 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003906 widget->user_data);
3907
Pekka Paalanen35e82632013-04-25 13:57:48 +03003908 if (surface->subsurface &&
3909 (surface->allocation.x != widget->allocation.x ||
3910 surface->allocation.y != widget->allocation.y)) {
3911 wl_subsurface_set_position(surface->subsurface,
3912 widget->allocation.x,
3913 widget->allocation.y);
3914 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003915 if (surface->allocation.width != widget->allocation.width ||
3916 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003917 window_schedule_redraw(widget->window);
3918 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003919 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003920
3921 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003922 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003923 widget->allocation.width,
3924 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003925}
3926
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003927static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003928hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3929{
3930 /*
3931 * This hack should be removed, when EGL respects
3932 * eglSwapInterval(0).
3933 *
3934 * If this window has sub-surfaces, especially a free-running
3935 * EGL-widget, we need to post the parent surface once with
3936 * all the old state to guarantee, that the EGL-widget will
3937 * receive its frame callback soon. Otherwise, a forced call
3938 * to eglSwapBuffers may end up blocking, waiting for a frame
3939 * event that will never come, because we will commit the parent
3940 * surface with all new state only after eglSwapBuffers returns.
3941 *
3942 * This assumes, that:
3943 * 1. When the EGL widget's resize hook is called, it pauses.
3944 * 2. When the EGL widget's redraw hook is called, it forces a
3945 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3946 * In a single threaded application condition 1 is a no-op.
3947 *
3948 * XXX: This should actually be after the surface_resize() calls,
3949 * but cannot, because then it would commit the incomplete state
3950 * accumulated from the widget resize hooks.
3951 */
3952 if (window->subsurface_list.next != &window->main_surface->link ||
3953 window->subsurface_list.prev != &window->main_surface->link)
3954 wl_surface_commit(window->main_surface->surface);
3955}
3956
3957static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003958idle_resize(struct window *window)
3959{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003960 struct surface *surface;
3961
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003962 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003963 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003964
Pekka Paalanen71233882013-04-25 13:57:53 +03003965 DBG("from %dx%d to %dx%d\n",
3966 window->main_surface->server_allocation.width,
3967 window->main_surface->server_allocation.height,
3968 window->pending_allocation.width,
3969 window->pending_allocation.height);
3970
Pekka Paalanene9297f82013-04-25 13:57:51 +03003971 hack_prevent_EGL_sub_surface_deadlock(window);
3972
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003973 widget_set_allocation(window->main_surface->widget,
3974 window->pending_allocation.x,
3975 window->pending_allocation.y,
3976 window->pending_allocation.width,
3977 window->pending_allocation.height);
3978
3979 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003980
3981 /* The main surface is in the list, too. Main surface's
3982 * resize_handler is responsible for calling widget_set_allocation()
3983 * on all sub-surface root widgets, so they will be resized
3984 * properly.
3985 */
3986 wl_list_for_each(surface, &window->subsurface_list, link) {
3987 if (surface == window->main_surface)
3988 continue;
3989
3990 surface_set_synchronized(surface);
3991 surface_resize(surface);
3992 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003993}
3994
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003995void
3996window_schedule_resize(struct window *window, int width, int height)
3997{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003998 window->pending_allocation.x = 0;
3999 window->pending_allocation.y = 0;
4000 window->pending_allocation.width = width;
4001 window->pending_allocation.height = height;
4002
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004003 if (window->min_allocation.width == 0)
4004 window->min_allocation = window->pending_allocation;
4005 if (window->pending_allocation.width < window->min_allocation.width)
4006 window->pending_allocation.width = window->min_allocation.width;
4007 if (window->pending_allocation.height < window->min_allocation.height)
4008 window->pending_allocation.height = window->min_allocation.height;
4009
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004010 window->resize_needed = 1;
4011 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004012}
4013
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004014void
4015widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4016{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004017 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004018}
4019
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004020static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06004021handle_ping(void *data, struct wl_shell_surface *shell_surface,
4022 uint32_t serial)
4023{
4024 wl_shell_surface_pong(shell_surface, serial);
4025}
4026
4027static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004028handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004029 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004030{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004031 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004032
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01004033 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004034 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004035}
4036
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004037static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004038menu_destroy(struct menu *menu)
4039{
4040 widget_destroy(menu->widget);
4041 window_destroy(menu->window);
4042 free(menu);
4043}
4044
4045static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004046handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
4047{
4048 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02004049 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004050
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004051 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004052 * device. Or just use wl_callback. And this really needs to
4053 * be a window vfunc that the menu can set. And we need the
4054 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004055
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004056 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004057 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004058}
4059
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004060static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06004061 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004062 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004063 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004064};
4065
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004066void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004067window_get_allocation(struct window *window,
4068 struct rectangle *allocation)
4069{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004070 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004071}
4072
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004073static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004074widget_redraw(struct widget *widget)
4075{
4076 struct widget *child;
4077
4078 if (widget->redraw_handler)
4079 widget->redraw_handler(widget, widget->user_data);
4080 wl_list_for_each(child, &widget->child_list, link)
4081 widget_redraw(child);
4082}
4083
4084static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004085frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4086{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004087 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004088
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004089 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004090 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004091 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004092 surface->frame_cb = NULL;
4093
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004094 surface->last_time = time;
4095
Pekka Paalanen71233882013-04-25 13:57:53 +03004096 if (surface->redraw_needed || surface->window->redraw_needed) {
4097 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004098 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004099 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004100}
4101
4102static const struct wl_callback_listener listener = {
4103 frame_callback
4104};
4105
4106static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004107surface_redraw(struct surface *surface)
4108{
Pekka Paalanen71233882013-04-25 13:57:53 +03004109 DBG_OBJ(surface->surface, "begin\n");
4110
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004111 if (!surface->window->redraw_needed && !surface->redraw_needed)
4112 return;
4113
4114 /* Whole-window redraw forces a redraw even if the previous has
4115 * not yet hit the screen.
4116 */
4117 if (surface->frame_cb) {
4118 if (!surface->window->redraw_needed)
4119 return;
4120
Pekka Paalanen71233882013-04-25 13:57:53 +03004121 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004122 wl_callback_destroy(surface->frame_cb);
4123 }
4124
4125 surface->frame_cb = wl_surface_frame(surface->surface);
4126 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004127 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004128
4129 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004130 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004131 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004132 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004133}
4134
4135static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004136idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004137{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004138 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004139 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004140
Pekka Paalanen71233882013-04-25 13:57:53 +03004141 DBG(" --------- \n");
4142
Pekka Paalaneneebff542013-04-25 13:57:52 +03004143 wl_list_init(&window->redraw_task.link);
4144 window->redraw_task_scheduled = 0;
4145
4146 if (window->resize_needed) {
4147 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004148 if (window->main_surface->frame_cb) {
4149 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004150 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004151 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004152
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004153 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03004154 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004155
Pekka Paalanen35e82632013-04-25 13:57:48 +03004156 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004157 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004158
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004159 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004160 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004161
4162 wl_list_for_each(surface, &window->subsurface_list, link)
4163 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004164}
4165
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004166static void
4167window_schedule_redraw_task(struct window *window)
4168{
4169 if (window->configure_requests)
4170 return;
4171 if (!window->redraw_task_scheduled) {
4172 window->redraw_task.run = idle_redraw;
4173 display_defer(window->display, &window->redraw_task);
4174 window->redraw_task_scheduled = 1;
4175 }
4176}
4177
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004178void
4179window_schedule_redraw(struct window *window)
4180{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004181 struct surface *surface;
4182
Pekka Paalanen71233882013-04-25 13:57:53 +03004183 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4184
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004185 wl_list_for_each(surface, &window->subsurface_list, link)
4186 surface->redraw_needed = 1;
4187
4188 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004189}
4190
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004191int
4192window_is_fullscreen(struct window *window)
4193{
4194 return window->type == TYPE_FULLSCREEN;
4195}
4196
MoD063a8822013-03-02 23:43:57 +00004197static void
4198configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4199{
4200 struct window *window = data;
4201
4202 wl_callback_destroy(callback);
4203 window->configure_requests--;
4204
4205 if (!window->configure_requests)
4206 window_schedule_redraw(window);
4207}
4208
4209static struct wl_callback_listener configure_request_listener = {
4210 configure_request_completed,
4211};
4212
4213static void
4214window_defer_redraw_until_configure(struct window* window)
4215{
4216 struct wl_callback *callback;
4217
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004218 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004219 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004220 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004221 }
4222
4223 callback = wl_display_sync(window->display->display);
4224 wl_callback_add_listener(callback, &configure_request_listener, window);
4225 window->configure_requests++;
4226}
4227
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004228void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004229window_set_fullscreen(struct window *window, int fullscreen)
4230{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004231 if (!window->display->shell)
4232 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004233
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004234 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004235 return;
4236
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004237 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004238 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004239 if (window->type == TYPE_TOPLEVEL) {
4240 window->saved_allocation = window->main_surface->allocation;
4241 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004242 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004243 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004244 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004245 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004246 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004247 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004248 if (window->saved_type == TYPE_MAXIMIZED) {
4249 window_set_maximized(window, 1);
4250 } else {
4251 window->type = TYPE_TOPLEVEL;
4252 wl_shell_surface_set_toplevel(window->shell_surface);
4253 window_schedule_resize(window,
4254 window->saved_allocation.width,
4255 window->saved_allocation.height);
4256 }
4257
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004258 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004259}
4260
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004261void
4262window_set_fullscreen_method(struct window *window,
4263 enum wl_shell_surface_fullscreen_method method)
4264{
4265 window->fullscreen_method = method;
4266}
4267
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004268int
4269window_is_maximized(struct window *window)
4270{
4271 return window->type == TYPE_MAXIMIZED;
4272}
4273
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004274void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004275window_set_maximized(struct window *window, int maximized)
4276{
4277 if (!window->display->shell)
4278 return;
4279
4280 if ((window->type == TYPE_MAXIMIZED) == maximized)
4281 return;
4282
4283 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004284 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004285 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4286 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004287 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004288 } else if (window->type == TYPE_FULLSCREEN) {
4289 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4290 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004291 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004292 } else {
4293 wl_shell_surface_set_toplevel(window->shell_surface);
4294 window->type = TYPE_TOPLEVEL;
4295 window_schedule_resize(window,
4296 window->saved_allocation.width,
4297 window->saved_allocation.height);
4298 }
4299}
4300
4301void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004302window_set_user_data(struct window *window, void *data)
4303{
4304 window->user_data = data;
4305}
4306
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004307void *
4308window_get_user_data(struct window *window)
4309{
4310 return window->user_data;
4311}
4312
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004313void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004314window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004315 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004316{
4317 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004318}
4319
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004320void
4321window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004322 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004323{
4324 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004325}
4326
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004327void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004328window_set_data_handler(struct window *window, window_data_handler_t handler)
4329{
4330 window->data_handler = handler;
4331}
4332
4333void
4334window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4335{
4336 window->drop_handler = handler;
4337}
4338
4339void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004340window_set_close_handler(struct window *window,
4341 window_close_handler_t handler)
4342{
4343 window->close_handler = handler;
4344}
4345
4346void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004347window_set_fullscreen_handler(struct window *window,
4348 window_fullscreen_handler_t handler)
4349{
4350 window->fullscreen_handler = handler;
4351}
4352
4353void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004354window_set_output_handler(struct window *window,
4355 window_output_handler_t handler)
4356{
4357 window->output_handler = handler;
4358}
4359
4360void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004361window_set_title(struct window *window, const char *title)
4362{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004363 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004364 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004365 if (window->shell_surface)
4366 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004367}
4368
4369const char *
4370window_get_title(struct window *window)
4371{
4372 return window->title;
4373}
4374
4375void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004376window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4377{
4378 struct text_cursor_position *text_cursor_position =
4379 window->display->text_cursor_position;
4380
Scott Moreau9295ce02012-06-01 12:46:10 -06004381 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004382 return;
4383
4384 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004385 window->main_surface->surface,
4386 wl_fixed_from_int(x),
4387 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004388}
4389
4390void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004391window_damage(struct window *window, int32_t x, int32_t y,
4392 int32_t width, int32_t height)
4393{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004394 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004395}
4396
Casey Dahlin9074db52012-04-19 22:50:09 -04004397static void
4398surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004399 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004400{
Rob Bradford7507b572012-05-15 17:55:34 +01004401 struct window *window = data;
4402 struct output *output;
4403 struct output *output_found = NULL;
4404 struct window_output *window_output;
4405
4406 wl_list_for_each(output, &window->display->output_list, link) {
4407 if (output->output == wl_output) {
4408 output_found = output;
4409 break;
4410 }
4411 }
4412
4413 if (!output_found)
4414 return;
4415
Brian Lovinbc919262013-08-07 15:34:59 -07004416 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004417 window_output->output = output_found;
4418
4419 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004420
4421 if (window->output_handler)
4422 window->output_handler(window, output_found, 1,
4423 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004424}
4425
4426static void
4427surface_leave(void *data,
4428 struct wl_surface *wl_surface, struct wl_output *output)
4429{
Rob Bradford7507b572012-05-15 17:55:34 +01004430 struct window *window = data;
4431 struct window_output *window_output;
4432 struct window_output *window_output_found = NULL;
4433
4434 wl_list_for_each(window_output, &window->window_output_list, link) {
4435 if (window_output->output->output == output) {
4436 window_output_found = window_output;
4437 break;
4438 }
4439 }
4440
4441 if (window_output_found) {
4442 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004443
4444 if (window->output_handler)
4445 window->output_handler(window, window_output->output,
4446 0, window->user_data);
4447
Rob Bradford7507b572012-05-15 17:55:34 +01004448 free(window_output_found);
4449 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004450}
4451
4452static const struct wl_surface_listener surface_listener = {
4453 surface_enter,
4454 surface_leave
4455};
4456
Pekka Paalanen4e373742013-02-13 16:17:13 +02004457static struct surface *
4458surface_create(struct window *window)
4459{
4460 struct display *display = window->display;
4461 struct surface *surface;
4462
Brian Lovinbc919262013-08-07 15:34:59 -07004463 surface = xmalloc(sizeof *surface);
4464 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004465 if (!surface)
4466 return NULL;
4467
4468 surface->window = window;
4469 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004470 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004471 wl_surface_add_listener(surface->surface, &surface_listener, window);
4472
Pekka Paalanen35e82632013-04-25 13:57:48 +03004473 wl_list_insert(&window->subsurface_list, &surface->link);
4474
Pekka Paalanen4e373742013-02-13 16:17:13 +02004475 return surface;
4476}
4477
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004478static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004479window_create_internal(struct display *display,
4480 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004481{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004482 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004483 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004484
Peter Huttererf3d62272013-08-08 11:57:05 +10004485 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004486 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004487 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004488 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004489
4490 surface = surface_create(window);
4491 window->main_surface = surface;
4492
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004493 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004494 window->shell_surface =
4495 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004496 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004497 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004498 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004499
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004500 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004501 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004502 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004503 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004504
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004505 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004506#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004507 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004508#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004509 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004510#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004511 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004512 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004513
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004514 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004515 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004516 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004517
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004518 if (window->shell_surface) {
4519 wl_shell_surface_set_user_data(window->shell_surface, window);
4520 wl_shell_surface_add_listener(window->shell_surface,
4521 &shell_surface_listener, window);
4522 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004523
Rob Bradford7507b572012-05-15 17:55:34 +01004524 wl_list_init (&window->window_output_list);
4525
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004526 return window;
4527}
4528
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004529struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004530window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004531{
Kristian Høgsberg41624832013-07-25 16:53:10 -07004532 return window_create_internal(display, NULL, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004533}
4534
4535struct window *
4536window_create_custom(struct display *display)
4537{
Kristian Høgsbergce278412013-07-25 15:20:20 -07004538 return window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004539}
4540
4541struct window *
4542window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004543 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004544{
4545 struct window *window;
4546
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004547 window = window_create_internal(parent->display,
4548 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004549
4550 window->x = x;
4551 window->y = y;
4552
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004553 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004554 wl_shell_surface_set_transient(
4555 window->shell_surface,
4556 window->parent->main_surface->surface,
4557 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004558
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004559 return window;
4560}
4561
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004562static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004563menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004564{
4565 int next;
4566
4567 next = (sy - 8) / 20;
4568 if (menu->current != next) {
4569 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004570 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004571 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004572}
4573
4574static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004575menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004576 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004577 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004578{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004579 struct menu *menu = data;
4580
4581 if (widget == menu->widget)
4582 menu_set_item(data, y);
4583
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004584 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004585}
4586
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004587static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004588menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004589 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004590{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004591 struct menu *menu = data;
4592
4593 if (widget == menu->widget)
4594 menu_set_item(data, y);
4595
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004596 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004597}
4598
4599static void
4600menu_leave_handler(struct widget *widget, struct input *input, void *data)
4601{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004602 struct menu *menu = data;
4603
4604 if (widget == menu->widget)
4605 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004606}
4607
4608static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004609menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004610 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004611 uint32_t button, enum wl_pointer_button_state state,
4612 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004613
4614{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004615 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004616
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004617 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4618 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004619 /* Either relase after press-drag-release or
4620 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004621 menu->func(menu->window->parent,
4622 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004623 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004624 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004625 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004626 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004627 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004628}
4629
4630static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004631menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004632{
4633 cairo_t *cr;
4634 const int32_t r = 3, margin = 3;
4635 struct menu *menu = data;
4636 int32_t width, height, i;
4637
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004638 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004639 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4640 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4641 cairo_paint(cr);
4642
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004643 width = widget->allocation.width;
4644 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004645 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004646
4647 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004648 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4649 cairo_fill(cr);
4650
4651 for (i = 0; i < menu->count; i++) {
4652 if (i == menu->current) {
4653 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4654 cairo_rectangle(cr, margin, i * 20 + margin,
4655 width - 2 * margin, 20);
4656 cairo_fill(cr);
4657 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4658 cairo_move_to(cr, 10, i * 20 + 16);
4659 cairo_show_text(cr, menu->entries[i]);
4660 } else {
4661 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4662 cairo_move_to(cr, 10, i * 20 + 16);
4663 cairo_show_text(cr, menu->entries[i]);
4664 }
4665 }
4666
4667 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004668}
4669
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004670void
4671window_show_menu(struct display *display,
4672 struct input *input, uint32_t time, struct window *parent,
4673 int32_t x, int32_t y,
4674 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004675{
4676 struct window *window;
4677 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004678 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004679
4680 menu = malloc(sizeof *menu);
4681 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004682 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004683
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004684 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004685 if (!window) {
4686 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004687 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004688 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004689
4690 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004691 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004692 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4693 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004694 menu->entries = entries;
4695 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004696 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004697 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004698 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004699 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004700 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004701 window->type = TYPE_MENU;
4702 window->x = x;
4703 window->y = y;
4704
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004705 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004706 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004707 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004708 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004709 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004710
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004711 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004712 widget_set_enter_handler(menu->widget, menu_enter_handler);
4713 widget_set_leave_handler(menu->widget, menu_leave_handler);
4714 widget_set_motion_handler(menu->widget, menu_motion_handler);
4715 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004716
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004717 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004718 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004719}
4720
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004721void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004722window_set_buffer_type(struct window *window, enum window_buffer_type type)
4723{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004724 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004725}
4726
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004727void
4728window_set_preferred_format(struct window *window,
4729 enum preferred_format format)
4730{
4731 window->preferred_format = format;
4732}
4733
Pekka Paalanen35e82632013-04-25 13:57:48 +03004734struct widget *
4735window_add_subsurface(struct window *window, void *data,
4736 enum subsurface_mode default_mode)
4737{
4738 struct widget *widget;
4739 struct surface *surface;
4740 struct wl_surface *parent;
4741 struct wl_subcompositor *subcompo = window->display->subcompositor;
4742
Pekka Paalanen35e82632013-04-25 13:57:48 +03004743 surface = surface_create(window);
4744 widget = widget_create(window, surface, data);
4745 wl_list_init(&widget->link);
4746 surface->widget = widget;
4747
4748 parent = window->main_surface->surface;
4749 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4750 surface->surface,
4751 parent);
4752 surface->synchronized = 1;
4753
4754 switch (default_mode) {
4755 case SUBSURFACE_SYNCHRONIZED:
4756 surface->synchronized_default = 1;
4757 break;
4758 case SUBSURFACE_DESYNCHRONIZED:
4759 surface->synchronized_default = 0;
4760 break;
4761 default:
4762 assert(!"bad enum subsurface_mode");
4763 }
4764
4765 return widget;
4766}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004767
4768static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004769display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004770 struct wl_output *wl_output,
4771 int x, int y,
4772 int physical_width,
4773 int physical_height,
4774 int subpixel,
4775 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004776 const char *model,
4777 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004778{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004779 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004780
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004781 output->allocation.x = x;
4782 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004783 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004784}
4785
4786static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004787display_handle_done(void *data,
4788 struct wl_output *wl_output)
4789{
4790}
4791
4792static void
4793display_handle_scale(void *data,
4794 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004795 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004796{
4797 struct output *output = data;
4798
4799 output->scale = scale;
4800}
4801
4802static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004803display_handle_mode(void *data,
4804 struct wl_output *wl_output,
4805 uint32_t flags,
4806 int width,
4807 int height,
4808 int refresh)
4809{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004810 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004811 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004812
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004813 if (flags & WL_OUTPUT_MODE_CURRENT) {
4814 output->allocation.width = width;
4815 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004816 if (display->output_configure_handler)
4817 (*display->output_configure_handler)(
4818 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004819 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004820}
4821
4822static const struct wl_output_listener output_listener = {
4823 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004824 display_handle_mode,
4825 display_handle_done,
4826 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004827};
4828
4829static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004830display_add_output(struct display *d, uint32_t id)
4831{
4832 struct output *output;
4833
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004834 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004835 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004836 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004837 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004838 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004839 wl_list_insert(d->output_list.prev, &output->link);
4840
4841 wl_output_add_listener(output->output, &output_listener, output);
4842}
4843
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004844static void
4845output_destroy(struct output *output)
4846{
4847 if (output->destroy_handler)
4848 (*output->destroy_handler)(output, output->user_data);
4849
4850 wl_output_destroy(output->output);
4851 wl_list_remove(&output->link);
4852 free(output);
4853}
4854
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004855void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004856display_set_global_handler(struct display *display,
4857 display_global_handler_t handler)
4858{
4859 struct global *global;
4860
4861 display->global_handler = handler;
4862 if (!handler)
4863 return;
4864
4865 wl_list_for_each(global, &display->global_list, link)
4866 display->global_handler(display,
4867 global->name, global->interface,
4868 global->version, display->user_data);
4869}
4870
4871void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004872display_set_output_configure_handler(struct display *display,
4873 display_output_handler_t handler)
4874{
4875 struct output *output;
4876
4877 display->output_configure_handler = handler;
4878 if (!handler)
4879 return;
4880
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004881 wl_list_for_each(output, &display->output_list, link) {
4882 if (output->allocation.width == 0 &&
4883 output->allocation.height == 0)
4884 continue;
4885
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004886 (*display->output_configure_handler)(output,
4887 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004888 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004889}
4890
4891void
4892output_set_user_data(struct output *output, void *data)
4893{
4894 output->user_data = data;
4895}
4896
4897void *
4898output_get_user_data(struct output *output)
4899{
4900 return output->user_data;
4901}
4902
4903void
4904output_set_destroy_handler(struct output *output,
4905 display_output_handler_t handler)
4906{
4907 output->destroy_handler = handler;
4908 /* FIXME: implement this, once we have way to remove outputs */
4909}
4910
4911void
Scott Moreau4e072362012-09-29 02:03:11 -06004912output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004913{
Scott Moreau4e072362012-09-29 02:03:11 -06004914 struct rectangle allocation = output->allocation;
4915
4916 switch (output->transform) {
4917 case WL_OUTPUT_TRANSFORM_90:
4918 case WL_OUTPUT_TRANSFORM_270:
4919 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4920 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4921 /* Swap width and height */
4922 allocation.width = output->allocation.height;
4923 allocation.height = output->allocation.width;
4924 break;
4925 }
4926
4927 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004928}
4929
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004930struct wl_output *
4931output_get_wl_output(struct output *output)
4932{
4933 return output->output;
4934}
4935
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004936enum wl_output_transform
4937output_get_transform(struct output *output)
4938{
4939 return output->transform;
4940}
4941
Alexander Larssonafd319a2013-05-22 14:41:27 +02004942uint32_t
4943output_get_scale(struct output *output)
4944{
4945 return output->scale;
4946}
4947
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004948static void
Daniel Stone97f68542012-05-30 16:32:01 +01004949fini_xkb(struct input *input)
4950{
4951 xkb_state_unref(input->xkb.state);
4952 xkb_map_unref(input->xkb.keymap);
4953}
4954
Rob Bradford08031182013-08-13 20:11:03 +01004955#define MAX(a,b) ((a) > (b) ? a : b)
4956
Daniel Stone97f68542012-05-30 16:32:01 +01004957static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004958display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004959{
4960 struct input *input;
4961
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004962 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004963 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004964 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4965 MAX(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004966 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004967 input->pointer_focus = NULL;
4968 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004969 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004970 wl_list_insert(d->input_list.prev, &input->link);
4971
Daniel Stone37816df2012-05-16 18:45:18 +01004972 wl_seat_add_listener(input->seat, &seat_listener, input);
4973 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004974
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004975 input->data_device =
4976 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004977 input->seat);
4978 wl_data_device_add_listener(input->data_device, &data_device_listener,
4979 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004980
4981 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004982
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004983 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4984 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004985 input->repeat_task.run = keyboard_repeat_func;
4986 display_watch_fd(d, input->repeat_timer_fd,
4987 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004988}
4989
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004990static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004991input_destroy(struct input *input)
4992{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004993 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004994 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004995
4996 if (input->drag_offer)
4997 data_offer_destroy(input->drag_offer);
4998
4999 if (input->selection_offer)
5000 data_offer_destroy(input->selection_offer);
5001
5002 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005003
5004 if (input->display->seat_version >= 3) {
5005 if (input->pointer)
5006 wl_pointer_release(input->pointer);
5007 if (input->keyboard)
5008 wl_keyboard_release(input->keyboard);
5009 }
5010
Daniel Stone97f68542012-05-30 16:32:01 +01005011 fini_xkb(input);
5012
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005013 wl_surface_destroy(input->pointer_surface);
5014
Pekka Paalanene1207c72011-12-16 12:02:09 +02005015 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005016 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005017 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005018 free(input);
5019}
5020
5021static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005022init_workspace_manager(struct display *d, uint32_t id)
5023{
5024 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005025 wl_registry_bind(d->registry, id,
5026 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005027 if (d->workspace_manager != NULL)
5028 workspace_manager_add_listener(d->workspace_manager,
5029 &workspace_manager_listener,
5030 d);
5031}
5032
5033static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005034shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5035{
5036 struct display *d = data;
5037
5038 if (format == WL_SHM_FORMAT_RGB565)
5039 d->has_rgb565 = 1;
5040}
5041
5042struct wl_shm_listener shm_listener = {
5043 shm_format
5044};
5045
5046static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005047registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5048 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005049{
5050 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005051 struct global *global;
5052
Brian Lovinbc919262013-08-07 15:34:59 -07005053 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005054 global->name = id;
5055 global->interface = strdup(interface);
5056 global->version = version;
5057 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005058
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005059 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005060 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005061 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005062 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005063 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005064 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005065 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005066 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005067 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005068 d->shell = wl_registry_bind(registry,
5069 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005070 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005071 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005072 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005073 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5074 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005075 wl_registry_bind(registry, id,
5076 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005077 } else if (strcmp(interface, "text_cursor_position") == 0) {
5078 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005079 wl_registry_bind(registry, id,
5080 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005081 } else if (strcmp(interface, "workspace_manager") == 0) {
5082 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005083 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5084 d->subcompositor =
5085 wl_registry_bind(registry, id,
5086 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005087 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005088
5089 if (d->global_handler)
5090 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005091}
5092
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005093static void
5094registry_handle_global_remove(void *data, struct wl_registry *registry,
5095 uint32_t name)
5096{
5097 struct display *d = data;
5098 struct global *global;
5099 struct global *tmp;
5100
5101 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5102 if (global->name != name)
5103 continue;
5104
5105 /* XXX: Should destroy bound globals, and call
5106 * the counterpart of display::global_handler
5107 */
5108 wl_list_remove(&global->link);
5109 free(global->interface);
5110 free(global);
5111 }
5112}
5113
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005114void *
5115display_bind(struct display *display, uint32_t name,
5116 const struct wl_interface *interface, uint32_t version)
5117{
5118 return wl_registry_bind(display->registry, name, interface, version);
5119}
5120
5121static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005122 registry_handle_global,
5123 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005124};
5125
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005126#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005127static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005128init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005129{
5130 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005131 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005132
Rob Clark6396ed32012-03-11 19:48:41 -05005133#ifdef USE_CAIRO_GLESV2
5134# define GL_BIT EGL_OPENGL_ES2_BIT
5135#else
5136# define GL_BIT EGL_OPENGL_BIT
5137#endif
5138
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005139 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005140 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005141 EGL_RED_SIZE, 1,
5142 EGL_GREEN_SIZE, 1,
5143 EGL_BLUE_SIZE, 1,
5144 EGL_ALPHA_SIZE, 1,
5145 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005146 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005147 EGL_NONE
5148 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005149
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005150#ifdef USE_CAIRO_GLESV2
5151 static const EGLint context_attribs[] = {
5152 EGL_CONTEXT_CLIENT_VERSION, 2,
5153 EGL_NONE
5154 };
5155 EGLint api = EGL_OPENGL_ES_API;
5156#else
5157 EGLint *context_attribs = NULL;
5158 EGLint api = EGL_OPENGL_API;
5159#endif
5160
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005161 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005162 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005163 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005164 return -1;
5165 }
5166
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005167 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005168 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005169 return -1;
5170 }
5171
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005172 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5173 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005174 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005175 return -1;
5176 }
5177
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005178 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005179 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005180 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005181 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005182 return -1;
5183 }
5184
Benjamin Franzke0c991632011-09-27 21:57:31 +02005185 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5186 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005187 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005188 return -1;
5189 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005190
5191 return 0;
5192}
5193
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005194static void
5195fini_egl(struct display *display)
5196{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005197 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005198
5199 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5200 EGL_NO_CONTEXT);
5201
5202 eglTerminate(display->dpy);
5203 eglReleaseThread();
5204}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005205#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005206
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005207static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005208init_dummy_surface(struct display *display)
5209{
5210 int len;
5211 void *data;
5212
5213 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5214 data = malloc(len);
5215 display->dummy_surface =
5216 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5217 1, 1, len);
5218 display->dummy_surface_data = data;
5219}
5220
5221static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005222handle_display_data(struct task *task, uint32_t events)
5223{
5224 struct display *display =
5225 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005226 struct epoll_event ep;
5227 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005228
5229 display->display_fd_events = events;
5230
5231 if (events & EPOLLERR || events & EPOLLHUP) {
5232 display_exit(display);
5233 return;
5234 }
5235
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005236 if (events & EPOLLIN) {
5237 ret = wl_display_dispatch(display->display);
5238 if (ret == -1) {
5239 display_exit(display);
5240 return;
5241 }
5242 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005243
5244 if (events & EPOLLOUT) {
5245 ret = wl_display_flush(display->display);
5246 if (ret == 0) {
5247 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5248 ep.data.ptr = &display->display_task;
5249 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5250 display->display_fd, &ep);
5251 } else if (ret == -1 && errno != EAGAIN) {
5252 display_exit(display);
5253 return;
5254 }
5255 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005256}
5257
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005258static void
5259log_handler(const char *format, va_list args)
5260{
5261 vfprintf(stderr, format, args);
5262}
5263
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005264struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005265display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005266{
5267 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005268
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005269 wl_log_set_handler_client(log_handler);
5270
Peter Huttererf3d62272013-08-08 11:57:05 +10005271 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005272 if (d == NULL)
5273 return NULL;
5274
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005275 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005276 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005277 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005278 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005279 return NULL;
5280 }
5281
Rob Bradford5ab9c752013-07-26 16:29:43 +01005282 d->xkb_context = xkb_context_new(0);
5283 if (d->xkb_context == NULL) {
5284 fprintf(stderr, "Failed to create XKB context\n");
5285 free(d);
5286 return NULL;
5287 }
5288
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005289 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005290 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005291 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005292 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5293 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005294
5295 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005296 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005297 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005298 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005299
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005300 d->workspace = 0;
5301 d->workspace_count = 1;
5302
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005303 d->registry = wl_display_get_registry(d->display);
5304 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005305
5306 if (wl_display_dispatch(d->display) < 0) {
5307 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5308 return NULL;
5309 }
5310
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005311#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005312 if (init_egl(d) < 0)
5313 fprintf(stderr, "EGL does not seem to work, "
5314 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005315#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005316
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005317 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005318
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005319 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005320
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005321 wl_list_init(&d->window_list);
5322
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005323 init_dummy_surface(d);
5324
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005325 return d;
5326}
5327
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005328static void
5329display_destroy_outputs(struct display *display)
5330{
5331 struct output *tmp;
5332 struct output *output;
5333
5334 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5335 output_destroy(output);
5336}
5337
Pekka Paalanene1207c72011-12-16 12:02:09 +02005338static void
5339display_destroy_inputs(struct display *display)
5340{
5341 struct input *tmp;
5342 struct input *input;
5343
5344 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5345 input_destroy(input);
5346}
5347
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005348void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005349display_destroy(struct display *display)
5350{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005351 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005352 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5353 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005354
5355 if (!wl_list_empty(&display->deferred_list))
5356 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5357
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005358 cairo_surface_destroy(display->dummy_surface);
5359 free(display->dummy_surface_data);
5360
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005361 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005362 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005363
Daniel Stone97f68542012-05-30 16:32:01 +01005364 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005365
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005366 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005367 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005368
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005369#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005370 if (display->argb_device)
5371 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005372#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005373
Pekka Paalanen35e82632013-04-25 13:57:48 +03005374 if (display->subcompositor)
5375 wl_subcompositor_destroy(display->subcompositor);
5376
Pekka Paalanenc2052982011-12-16 11:41:32 +02005377 if (display->shell)
5378 wl_shell_destroy(display->shell);
5379
5380 if (display->shm)
5381 wl_shm_destroy(display->shm);
5382
5383 if (display->data_device_manager)
5384 wl_data_device_manager_destroy(display->data_device_manager);
5385
5386 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005387 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005388
5389 close(display->epoll_fd);
5390
U. Artie Eoff44874d92012-10-02 21:12:35 -07005391 if (!(display->display_fd_events & EPOLLERR) &&
5392 !(display->display_fd_events & EPOLLHUP))
5393 wl_display_flush(display->display);
5394
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005395 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005396 free(display);
5397}
5398
5399void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005400display_set_user_data(struct display *display, void *data)
5401{
5402 display->user_data = data;
5403}
5404
5405void *
5406display_get_user_data(struct display *display)
5407{
5408 return display->user_data;
5409}
5410
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005411struct wl_display *
5412display_get_display(struct display *display)
5413{
5414 return display->display;
5415}
5416
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005417int
5418display_has_subcompositor(struct display *display)
5419{
5420 if (display->subcompositor)
5421 return 1;
5422
5423 wl_display_roundtrip(display->display);
5424
5425 return display->subcompositor != NULL;
5426}
5427
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005428cairo_device_t *
5429display_get_cairo_device(struct display *display)
5430{
5431 return display->argb_device;
5432}
5433
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005434struct output *
5435display_get_output(struct display *display)
5436{
5437 return container_of(display->output_list.next, struct output, link);
5438}
5439
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005440struct wl_compositor *
5441display_get_compositor(struct display *display)
5442{
5443 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005444}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005445
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005446uint32_t
5447display_get_serial(struct display *display)
5448{
5449 return display->serial;
5450}
5451
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005452EGLDisplay
5453display_get_egl_display(struct display *d)
5454{
5455 return d->dpy;
5456}
5457
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005458struct wl_data_source *
5459display_create_data_source(struct display *display)
5460{
5461 return wl_data_device_manager_create_data_source(display->data_device_manager);
5462}
5463
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005464EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005465display_get_argb_egl_config(struct display *d)
5466{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005467 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005468}
5469
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005470struct wl_shell *
5471display_get_shell(struct display *display)
5472{
5473 return display->shell;
5474}
5475
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005476int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005477display_acquire_window_surface(struct display *display,
5478 struct window *window,
5479 EGLContext ctx)
5480{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005481 struct surface *surface = window->main_surface;
5482
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005483 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005484 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005485
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005486 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005487 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005488}
5489
5490void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005491display_release_window_surface(struct display *display,
5492 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005493{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005494 struct surface *surface = window->main_surface;
5495
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005496 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005497 return;
5498
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005499 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005500}
5501
5502void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005503display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005504{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005505 wl_list_insert(&display->deferred_list, &task->link);
5506}
5507
5508void
5509display_watch_fd(struct display *display,
5510 int fd, uint32_t events, struct task *task)
5511{
5512 struct epoll_event ep;
5513
5514 ep.events = events;
5515 ep.data.ptr = task;
5516 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5517}
5518
5519void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005520display_unwatch_fd(struct display *display, int fd)
5521{
5522 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5523}
5524
5525void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005526display_run(struct display *display)
5527{
5528 struct task *task;
5529 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005530 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005531
Pekka Paalanen826d7952011-12-15 10:14:07 +02005532 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005533 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005534 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005535 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005536 struct task, link);
5537 wl_list_remove(&task->link);
5538 task->run(task, 0);
5539 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005540
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005541 wl_display_dispatch_pending(display->display);
5542
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005543 if (!display->running)
5544 break;
5545
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005546 ret = wl_display_flush(display->display);
5547 if (ret < 0 && errno == EAGAIN) {
5548 ep[0].events =
5549 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5550 ep[0].data.ptr = &display->display_task;
5551
5552 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5553 display->display_fd, &ep[0]);
5554 } else if (ret < 0) {
5555 break;
5556 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005557
5558 count = epoll_wait(display->epoll_fd,
5559 ep, ARRAY_LENGTH(ep), -1);
5560 for (i = 0; i < count; i++) {
5561 task = ep[i].data.ptr;
5562 task->run(task, ep[i].events);
5563 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005564 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005565}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005566
5567void
5568display_exit(struct display *display)
5569{
5570 display->running = 0;
5571}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005572
5573void
5574keysym_modifiers_add(struct wl_array *modifiers_map,
5575 const char *name)
5576{
5577 size_t len = strlen(name) + 1;
5578 char *p;
5579
5580 p = wl_array_add(modifiers_map, len);
5581
5582 if (p == NULL)
5583 return;
5584
5585 strncpy(p, name, len);
5586}
5587
5588static xkb_mod_index_t
5589keysym_modifiers_get_index(struct wl_array *modifiers_map,
5590 const char *name)
5591{
5592 xkb_mod_index_t index = 0;
5593 char *p = modifiers_map->data;
5594
5595 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5596 if (strcmp(p, name) == 0)
5597 return index;
5598
5599 index++;
5600 p += strlen(p) + 1;
5601 }
5602
5603 return XKB_MOD_INVALID;
5604}
5605
5606xkb_mod_mask_t
5607keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5608 const char *name)
5609{
5610 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5611
5612 if (index == XKB_MOD_INVALID)
5613 return XKB_MOD_INVALID;
5614
5615 return 1 << index;
5616}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005617
5618void *
5619fail_on_null(void *p)
5620{
5621 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005622 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005623 exit(EXIT_FAILURE);
5624 }
5625
5626 return p;
5627}
5628
5629void *
5630xmalloc(size_t s)
5631{
5632 return fail_on_null(malloc(s));
5633}
5634
Peter Huttererf3d62272013-08-08 11:57:05 +10005635void *
5636xzalloc(size_t s)
5637{
5638 return fail_on_null(zalloc(s));
5639}
5640
Kristian Høgsbergce278412013-07-25 15:20:20 -07005641char *
5642xstrdup(const char *s)
5643{
5644 return fail_on_null(strdup(s));
5645}