blob: 29f6c8726aff5abf2d7b2582be742acd32dc9ae8 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen99436862012-11-19 17:15:59 +02003 * Copyright © 2012 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
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040024#define _GNU_SOURCE
25
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026#include "../config.h"
27
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040042
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040043#include <pixman.h>
44
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050046#include <wayland-egl.h>
47
Rob Clark6396ed32012-03-11 19:48:41 -050048#ifdef USE_CAIRO_GLESV2
49#include <GLES2/gl2.h>
50#include <GLES2/gl2ext.h>
51#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050053#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <EGL/egl.h>
55#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040057#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030058#else /* HAVE_CAIRO_EGL */
59typedef void *EGLDisplay;
60typedef void *EGLConfig;
61typedef void *EGLContext;
62#define EGL_NO_DISPLAY ((EGLDisplay)0)
63#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050064
Daniel Stone9d4f0302012-02-15 16:33:21 +000065#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030066#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040067
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050068#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020069#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040070#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060071#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020072#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030073#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050074
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050075#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050076
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070077struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030078
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040079struct global {
80 uint32_t name;
81 char *interface;
82 uint32_t version;
83 struct wl_list link;
84};
85
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050086struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050087 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040088 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050089 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040090 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040091 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040092 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060093 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020094 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040095 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050096 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020098 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040099 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400100
101 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700102 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400103 struct task display_task;
104
105 int epoll_fd;
106 struct wl_list deferred_list;
107
Pekka Paalanen826d7952011-12-15 10:14:07 +0200108 int running;
109
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400110 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400111 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500113 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400114
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400115 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400116
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300117 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300118 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400119
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400121 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133};
134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400136 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400138 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500139 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500141 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_CUSTOM
143};
Rob Bradford7507b572012-05-15 17:55:34 +0100144
145struct window_output {
146 struct output *output;
147 struct wl_list link;
148};
149
Pekka Paalanenec076692012-11-30 13:37:27 +0200150enum toysurface_prepare_flags {
151 SURFACE_HINT_RESIZE = 0x01,
152};
153
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200154struct toysurface {
155 /*
156 * Prepare the surface for drawing. Makes sure there is a surface
157 * of the right size available for rendering, and returns it.
158 * dx,dy are the x,y of wl_surface.attach.
159 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200160 * If flags has SURFACE_HINT_RESIZE set, the user is
161 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200162 * Returns the Cairo surface to draw to.
163 */
164 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200165 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200166
167 /*
168 * Post the surface to the server, returning the server allocation
169 * rectangle. The Cairo surface from prepare() must be destroyed
170 * after calling this.
171 */
172 void (*swap)(struct toysurface *base,
173 struct rectangle *server_allocation);
174
175 /*
176 * Make the toysurface current with the given EGL context.
177 * Returns 0 on success, and negative of failure.
178 */
179 int (*acquire)(struct toysurface *base, EGLContext ctx);
180
181 /*
182 * Release the toysurface from the EGL context, returning control
183 * to Cairo.
184 */
185 void (*release)(struct toysurface *base);
186
187 /*
188 * Destroy the toysurface, including the Cairo surface, any
189 * backing storage, and the Wayland protocol objects.
190 */
191 void (*destroy)(struct toysurface *base);
192};
193
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500194struct window {
195 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500196 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100197 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500198 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200199 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500200 struct wl_region *input_region;
201 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500202 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500203 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400204 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500205 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500206 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400207 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400208 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400209 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400210 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400211 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400212 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400213 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400214 int focus_count;
215
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400216 enum window_buffer_type buffer_type;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +0200217 enum wl_output_transform buffer_transform;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200218 struct toysurface *toysurface;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400219 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500220
Pekka Paalanen99436862012-11-19 17:15:59 +0200221 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400222
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500223 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500224 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400225 window_data_handler_t data_handler;
226 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500227 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400228 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200229 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400230
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300231 struct wl_callback *frame_cb;
232
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200233 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500234 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500235
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500236 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400237 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500238};
239
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500240struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500241 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300242 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500243 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400244 struct wl_list link;
245 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500246 widget_resize_handler_t resize_handler;
247 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500248 widget_enter_handler_t enter_handler;
249 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500250 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500251 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200252 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400253 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500254 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300255 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500256 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400257};
258
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400259struct input {
260 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100261 struct wl_seat *seat;
262 struct wl_pointer *pointer;
263 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400264 struct window *pointer_focus;
265 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300266 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300267 uint32_t cursor_anim_start;
268 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300269 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400270 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400271 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400272 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400273 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400274 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400275
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500276 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500277 struct widget *grab;
278 uint32_t grab_button;
279
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400280 struct wl_data_device *data_device;
281 struct data_offer *drag_offer;
282 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100283
284 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100285 struct xkb_keymap *keymap;
286 struct xkb_state *state;
287 xkb_mod_mask_t control_mask;
288 xkb_mod_mask_t alt_mask;
289 xkb_mod_mask_t shift_mask;
290 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400291
292 struct task repeat_task;
293 int repeat_timer_fd;
294 uint32_t repeat_sym;
295 uint32_t repeat_key;
296 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400297};
298
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500299struct output {
300 struct display *display;
301 struct wl_output *output;
302 struct rectangle allocation;
303 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600304 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200305
306 display_output_handler_t destroy_handler;
307 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500308};
309
Martin Minarik1998b152012-05-10 02:04:35 +0200310enum frame_button_action {
311 FRAME_BUTTON_NULL = 0,
312 FRAME_BUTTON_ICON = 1,
313 FRAME_BUTTON_CLOSE = 2,
314 FRAME_BUTTON_MINIMIZE = 3,
315 FRAME_BUTTON_MAXIMIZE = 4,
316};
317
318enum frame_button_pointer {
319 FRAME_BUTTON_DEFAULT = 0,
320 FRAME_BUTTON_OVER = 1,
321 FRAME_BUTTON_ACTIVE = 2,
322};
323
324enum frame_button_align {
325 FRAME_BUTTON_RIGHT = 0,
326 FRAME_BUTTON_LEFT = 1,
327};
328
329enum frame_button_decoration {
330 FRAME_BUTTON_NONE = 0,
331 FRAME_BUTTON_FANCY = 1,
332};
333
334struct frame_button {
335 struct widget *widget;
336 struct frame *frame;
337 cairo_surface_t *icon;
338 enum frame_button_action type;
339 enum frame_button_pointer state;
340 struct wl_list link; /* buttons_list */
341 enum frame_button_align align;
342 enum frame_button_decoration decoration;
343};
344
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500345struct frame {
346 struct widget *widget;
347 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200348 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500349};
350
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500351struct menu {
352 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500353 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500354 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500355 const char **entries;
356 uint32_t time;
357 int current;
358 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400359 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500360 menu_func_t func;
361};
362
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300363struct tooltip {
364 struct widget *parent;
365 struct window *window;
366 struct widget *widget;
367 char *entry;
368 struct task tooltip_task;
369 int tooltip_fd;
370 float x, y;
371};
372
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700373struct shm_pool {
374 struct wl_shm_pool *pool;
375 size_t size;
376 size_t used;
377 void *data;
378};
379
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400380enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300381 CURSOR_DEFAULT = 100,
382 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400383};
384
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500385enum window_location {
386 WINDOW_INTERIOR = 0,
387 WINDOW_RESIZING_TOP = 1,
388 WINDOW_RESIZING_BOTTOM = 2,
389 WINDOW_RESIZING_LEFT = 4,
390 WINDOW_RESIZING_TOP_LEFT = 5,
391 WINDOW_RESIZING_BOTTOM_LEFT = 6,
392 WINDOW_RESIZING_RIGHT = 8,
393 WINDOW_RESIZING_TOP_RIGHT = 9,
394 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
395 WINDOW_RESIZING_MASK = 15,
396 WINDOW_EXTERIOR = 16,
397 WINDOW_TITLEBAR = 17,
398 WINDOW_CLIENT_AREA = 18,
399};
400
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200401static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500403#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200405struct egl_window_surface {
406 struct toysurface base;
407 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100408 struct display *display;
409 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200410 struct wl_egl_window *egl_window;
411 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100412};
413
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200414static struct egl_window_surface *
415to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100416{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200417 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100418}
419
420static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200421egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200422 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100423{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200424 struct egl_window_surface *surface = to_egl_window_surface(base);
425
426 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
427 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
428
429 return cairo_surface_reference(surface->cairo_surface);
430}
431
432static void
433egl_window_surface_swap(struct toysurface *base,
434 struct rectangle *server_allocation)
435{
436 struct egl_window_surface *surface = to_egl_window_surface(base);
437
438 cairo_gl_surface_swapbuffers(surface->cairo_surface);
439 wl_egl_window_get_attached_size(surface->egl_window,
440 &server_allocation->width,
441 &server_allocation->height);
442}
443
444static int
445egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
446{
447 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200448 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100449
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200450 device = cairo_surface_get_device(surface->cairo_surface);
451 if (!device)
452 return -1;
453
454 if (!ctx) {
455 if (device == surface->display->argb_device)
456 ctx = surface->display->argb_ctx;
457 else
458 assert(0);
459 }
460
461 cairo_device_flush(device);
462 cairo_device_acquire(device);
463 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
464 surface->egl_surface, ctx))
465 fprintf(stderr, "failed to make surface current\n");
466
467 return 0;
468}
469
470static void
471egl_window_surface_release(struct toysurface *base)
472{
473 struct egl_window_surface *surface = to_egl_window_surface(base);
474 cairo_device_t *device;
475
476 device = cairo_surface_get_device(surface->cairo_surface);
477 if (!device)
478 return;
479
480 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
481 surface->display->argb_ctx))
482 fprintf(stderr, "failed to make context current\n");
483
484 cairo_device_release(device);
485}
486
487static void
488egl_window_surface_destroy(struct toysurface *base)
489{
490 struct egl_window_surface *surface = to_egl_window_surface(base);
491 struct display *d = surface->display;
492
493 cairo_surface_destroy(surface->cairo_surface);
494 eglDestroySurface(d->dpy, surface->egl_surface);
495 wl_egl_window_destroy(surface->egl_window);
496 surface->surface = NULL;
497
498 free(surface);
499}
500
501static struct toysurface *
502egl_window_surface_create(struct display *display,
503 struct wl_surface *wl_surface,
504 uint32_t flags,
505 struct rectangle *rectangle)
506{
507 struct egl_window_surface *surface;
508
Pekka Paalanenb3627362012-11-19 17:16:00 +0200509 if (display->dpy == EGL_NO_DISPLAY)
510 return NULL;
511
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200512 surface = calloc(1, sizeof *surface);
513 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100514 return NULL;
515
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200516 surface->base.prepare = egl_window_surface_prepare;
517 surface->base.swap = egl_window_surface_swap;
518 surface->base.acquire = egl_window_surface_acquire;
519 surface->base.release = egl_window_surface_release;
520 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 surface->display = display;
523 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400524
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200525 surface->egl_window = wl_egl_window_create(surface->surface,
526 rectangle->width,
527 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100528
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200529 surface->egl_surface = eglCreateWindowSurface(display->dpy,
530 display->argb_config,
531 surface->egl_window,
532 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100533
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534 surface->cairo_surface =
535 cairo_gl_surface_create_for_egl(display->argb_device,
536 surface->egl_surface,
537 rectangle->width,
538 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100539
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200540 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100541}
542
Pekka Paalanenb3627362012-11-19 17:16:00 +0200543#else
544
545static struct toysurface *
546egl_window_surface_create(struct display *display,
547 struct wl_surface *wl_surface,
548 uint32_t flags,
549 struct rectangle *rectangle)
550{
551 return NULL;
552}
553
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554#endif
555
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200556struct shm_surface_data {
557 struct wl_buffer *buffer;
558 struct shm_pool *pool;
559};
560
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561struct wl_buffer *
562display_get_buffer_for_surface(struct display *display,
563 cairo_surface_t *surface)
564{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200565 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200567 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400568
569 return data->buffer;
570}
571
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500572static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700573shm_pool_destroy(struct shm_pool *pool);
574
575static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576shm_surface_data_destroy(void *p)
577{
578 struct shm_surface_data *data = p;
579
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200580 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700581 if (data->pool)
582 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300583
584 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585}
586
Kristian Høgsberg16626282012-04-03 11:21:27 -0400587static struct wl_shm_pool *
588make_shm_pool(struct display *display, int size, void **data)
589{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400590 struct wl_shm_pool *pool;
591 int fd;
592
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300593 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400594 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300595 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
596 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400597 return NULL;
598 }
599
600 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400601 if (*data == MAP_FAILED) {
602 fprintf(stderr, "mmap failed: %m\n");
603 close(fd);
604 return NULL;
605 }
606
607 pool = wl_shm_create_pool(display->shm, fd, size);
608
609 close(fd);
610
611 return pool;
612}
613
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700614static struct shm_pool *
615shm_pool_create(struct display *display, size_t size)
616{
617 struct shm_pool *pool = malloc(sizeof *pool);
618
619 if (!pool)
620 return NULL;
621
622 pool->pool = make_shm_pool(display, size, &pool->data);
623 if (!pool->pool) {
624 free(pool);
625 return NULL;
626 }
627
628 pool->size = size;
629 pool->used = 0;
630
631 return pool;
632}
633
634static void *
635shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
636{
637 if (pool->used + size > pool->size)
638 return NULL;
639
640 *offset = pool->used;
641 pool->used += size;
642
643 return (char *) pool->data + *offset;
644}
645
646/* destroy the pool. this does not unmap the memory though */
647static void
648shm_pool_destroy(struct shm_pool *pool)
649{
650 munmap(pool->data, pool->size);
651 wl_shm_pool_destroy(pool->pool);
652 free(pool);
653}
654
655/* Start allocating from the beginning of the pool again */
656static void
657shm_pool_reset(struct shm_pool *pool)
658{
659 pool->used = 0;
660}
661
662static int
663data_length_for_shm_surface(struct rectangle *rect)
664{
665 int stride;
666
667 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
668 rect->width);
669 return stride * rect->height;
670}
671
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500672static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700673display_create_shm_surface_from_pool(struct display *display,
674 struct rectangle *rectangle,
675 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400676{
677 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400678 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400679 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700680 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400681 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682
683 data = malloc(sizeof *data);
684 if (data == NULL)
685 return NULL;
686
687 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
688 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700689 length = stride * rectangle->height;
690 data->pool = NULL;
691 map = shm_pool_allocate(pool, length, &offset);
692
693 if (!map) {
694 free(data);
695 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400696 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400697
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400698 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400699 CAIRO_FORMAT_ARGB32,
700 rectangle->width,
701 rectangle->height,
702 stride);
703
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200704 cairo_surface_set_user_data(surface, &shm_surface_data_key,
705 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400707 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500708 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400709 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500710 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400711
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200712 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
713 rectangle->width,
714 rectangle->height,
715 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400716
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700717 return surface;
718}
719
720static cairo_surface_t *
721display_create_shm_surface(struct display *display,
722 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200723 struct shm_pool *alternate_pool,
724 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700725{
726 struct shm_surface_data *data;
727 struct shm_pool *pool;
728 cairo_surface_t *surface;
729
Pekka Paalanen99436862012-11-19 17:15:59 +0200730 if (alternate_pool) {
731 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700732 surface = display_create_shm_surface_from_pool(display,
733 rectangle,
734 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200735 alternate_pool);
736 if (surface) {
737 data = cairo_surface_get_user_data(surface,
738 &shm_surface_data_key);
739 goto out;
740 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700741 }
742
743 pool = shm_pool_create(display,
744 data_length_for_shm_surface(rectangle));
745 if (!pool)
746 return NULL;
747
748 surface =
749 display_create_shm_surface_from_pool(display, rectangle,
750 flags, pool);
751
752 if (!surface) {
753 shm_pool_destroy(pool);
754 return NULL;
755 }
756
757 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200758 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700759 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400760
Pekka Paalanen99436862012-11-19 17:15:59 +0200761out:
762 if (data_ret)
763 *data_ret = data;
764
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400765 return surface;
766}
767
nobled7b87cb02011-02-01 18:51:47 +0000768static int
769check_size(struct rectangle *rect)
770{
771 if (rect->width && rect->height)
772 return 0;
773
774 fprintf(stderr, "tried to create surface of "
775 "width: %d, height: %d\n", rect->width, rect->height);
776 return -1;
777}
778
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400779cairo_surface_t *
780display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100781 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400782 struct rectangle *rectangle,
783 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400784{
nobled7b87cb02011-02-01 18:51:47 +0000785 if (check_size(rectangle) < 0)
786 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200787
788 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200789 return display_create_shm_surface(display, rectangle, flags,
790 NULL, NULL);
791}
792
Pekka Paalanena4eda732012-11-19 17:16:02 +0200793struct shm_surface_leaf {
794 cairo_surface_t *cairo_surface;
795 /* 'data' is automatically destroyed, when 'cairo_surface' is */
796 struct shm_surface_data *data;
797
798 struct shm_pool *resize_pool;
799 int busy;
800};
801
802static void
803shm_surface_leaf_release(struct shm_surface_leaf *leaf)
804{
805 if (leaf->cairo_surface)
806 cairo_surface_destroy(leaf->cairo_surface);
807 /* leaf->data already destroyed via cairo private */
808
809 if (leaf->resize_pool)
810 shm_pool_destroy(leaf->resize_pool);
811}
812
Pekka Paalanen99436862012-11-19 17:15:59 +0200813struct shm_surface {
814 struct toysurface base;
815 struct display *display;
816 struct wl_surface *surface;
817 uint32_t flags;
818 int dx, dy;
819
Pekka Paalanena4eda732012-11-19 17:16:02 +0200820 struct shm_surface_leaf leaf[2];
821 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200822};
823
824static struct shm_surface *
825to_shm_surface(struct toysurface *base)
826{
827 return container_of(base, struct shm_surface, base);
828}
829
Pekka Paalanena4eda732012-11-19 17:16:02 +0200830static void
831shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
832{
833 struct shm_surface_leaf *leaf = data;
834
835 leaf->busy = 0;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200836
837 /* If both leaves are now free, we should call
838 * shm_surface_leaf_release(shm_surface::leaf[1]).
839 * However, none of Weston's backends switch dynamically
840 * between early buffer release and requiring double-buffering,
841 * so if both leaves are free, we never used the second
842 * leaf to begin with.
843 */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200844}
845
846static const struct wl_buffer_listener shm_surface_buffer_listener = {
847 shm_surface_buffer_release
848};
849
Pekka Paalanen99436862012-11-19 17:15:59 +0200850static cairo_surface_t *
851shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200852 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200853{
Pekka Paalanenec076692012-11-30 13:37:27 +0200854 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200855 struct shm_surface *surface = to_shm_surface(base);
856 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200857 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200858
859 surface->dx = dx;
860 surface->dy = dy;
861
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200862 /* See shm_surface_buffer_release() */
863 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
864 surface->leaf[1].cairo_surface) {
865 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
866 __func__);
867 }
868
Pekka Paalanena4eda732012-11-19 17:16:02 +0200869 /* pick a free buffer from the two */
870 if (!surface->leaf[0].busy)
871 leaf = &surface->leaf[0];
872 else if (!surface->leaf[1].busy)
873 leaf = &surface->leaf[1];
874 else {
875 fprintf(stderr, "%s: both buffers are held by the server.\n",
876 __func__);
877 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200878 }
879
Pekka Paalanena4eda732012-11-19 17:16:02 +0200880 if (!resize_hint && leaf->resize_pool) {
881 cairo_surface_destroy(leaf->cairo_surface);
882 leaf->cairo_surface = NULL;
883 shm_pool_destroy(leaf->resize_pool);
884 leaf->resize_pool = NULL;
885 }
886
887 if (leaf->cairo_surface &&
888 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
889 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200890 goto out;
891
Pekka Paalanena4eda732012-11-19 17:16:02 +0200892 if (leaf->cairo_surface)
893 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200894
Pekka Paalanena4eda732012-11-19 17:16:02 +0200895 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200896 /* Create a big pool to allocate from, while continuously
897 * resizing. Mmapping a new pool in the server
898 * is relatively expensive, so reusing a pool performs
899 * better, but may temporarily reserve unneeded memory.
900 */
901 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200902 leaf->resize_pool = shm_pool_create(surface->display,
903 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200904 }
905
Pekka Paalanena4eda732012-11-19 17:16:02 +0200906 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200907 display_create_shm_surface(surface->display, &rect,
908 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200909 leaf->resize_pool,
910 &leaf->data);
911 wl_buffer_add_listener(leaf->data->buffer,
912 &shm_surface_buffer_listener, leaf);
Pekka Paalanen99436862012-11-19 17:15:59 +0200913
914out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200915 surface->current = leaf;
916
917 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200918}
919
920static void
921shm_surface_swap(struct toysurface *base,
922 struct rectangle *server_allocation)
923{
924 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200925 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200926
927 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200928 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200929 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200930 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200931
Pekka Paalanena4eda732012-11-19 17:16:02 +0200932 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200933 surface->dx, surface->dy);
934 wl_surface_damage(surface->surface, 0, 0,
935 server_allocation->width, server_allocation->height);
936 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200937
938 leaf->busy = 1;
939 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200940}
941
942static int
943shm_surface_acquire(struct toysurface *base, EGLContext ctx)
944{
945 return -1;
946}
947
948static void
949shm_surface_release(struct toysurface *base)
950{
951}
952
953static void
954shm_surface_destroy(struct toysurface *base)
955{
956 struct shm_surface *surface = to_shm_surface(base);
957
Pekka Paalanena4eda732012-11-19 17:16:02 +0200958 shm_surface_leaf_release(&surface->leaf[0]);
959 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200960
961 free(surface);
962}
963
964static struct toysurface *
965shm_surface_create(struct display *display, struct wl_surface *wl_surface,
966 uint32_t flags, struct rectangle *rectangle)
967{
968 struct shm_surface *surface;
969
970 surface = calloc(1, sizeof *surface);
971 if (!surface)
972 return NULL;
973
974 surface->base.prepare = shm_surface_prepare;
975 surface->base.swap = shm_surface_swap;
976 surface->base.acquire = shm_surface_acquire;
977 surface->base.release = shm_surface_release;
978 surface->base.destroy = shm_surface_destroy;
979
980 surface->display = display;
981 surface->surface = wl_surface;
982 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200983
984 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400985}
986
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200987/*
988 * The following correspondences between file names and cursors was copied
989 * from: https://bugs.kde.org/attachment.cgi?id=67313
990 */
991
992static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300993 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200994 "sw-resize"
995};
996
997static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300998 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200999 "se-resize"
1000};
1001
1002static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001003 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001004 "s-resize"
1005};
1006
1007static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001008 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001009 "closedhand",
1010 "208530c400c041818281048008011002"
1011};
1012
1013static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001014 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001015 "default",
1016 "top_left_arrow",
1017 "left-arrow"
1018};
1019
1020static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001021 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001022 "w-resize"
1023};
1024
1025static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001026 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001027 "e-resize"
1028};
1029
1030static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001031 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001032 "nw-resize"
1033};
1034
1035static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001036 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001037 "ne-resize"
1038};
1039
1040static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001041 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001042 "n-resize"
1043};
1044
1045static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001046 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001047 "ibeam",
1048 "text"
1049};
1050
1051static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001052 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001053 "pointer",
1054 "pointing_hand",
1055 "e29285e634086352946a0e7090d73106"
1056};
1057
1058static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001059 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001060 "wait",
1061 "0426c94ea35c87780ff01dc239897213"
1062};
1063
1064struct cursor_alternatives {
1065 const char **names;
1066 size_t count;
1067};
1068
1069static const struct cursor_alternatives cursors[] = {
1070 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1071 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1072 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1073 {grabbings, ARRAY_LENGTH(grabbings)},
1074 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1075 {left_sides, ARRAY_LENGTH(left_sides)},
1076 {right_sides, ARRAY_LENGTH(right_sides)},
1077 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1078 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1079 {top_sides, ARRAY_LENGTH(top_sides)},
1080 {xterms, ARRAY_LENGTH(xterms)},
1081 {hand1s, ARRAY_LENGTH(hand1s)},
1082 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001083};
1084
1085static void
1086create_cursors(struct display *display)
1087{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001088 char *config_file;
1089 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001090 unsigned int i, j;
1091 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001092 struct config_key shell_keys[] = {
1093 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1094 };
1095 struct config_section cs[] = {
1096 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1097 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001098
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001099 config_file = config_file_path("weston.ini");
1100 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1101 free(config_file);
1102
1103 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001104 display->cursors =
1105 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1106
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001107 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001108 cursor = NULL;
1109 for (j = 0; !cursor && j < cursors[i].count; ++j)
1110 cursor = wl_cursor_theme_get_cursor(
1111 display->cursor_theme, cursors[i].names[j]);
1112
1113 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001114 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001115 cursors[i].names[0]);
1116
1117 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001118 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001119}
1120
1121static void
1122destroy_cursors(struct display *display)
1123{
1124 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001125 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001126}
1127
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001128struct wl_cursor_image *
1129display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001130{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001131 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001132
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001133 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001134}
1135
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001136static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001137window_get_resize_dx_dy(struct window *window, int *x, int *y)
1138{
1139 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1140 *x = window->server_allocation.width - window->allocation.width;
1141 else
1142 *x = 0;
1143
1144 if (window->resize_edges & WINDOW_RESIZING_TOP)
1145 *y = window->server_allocation.height -
1146 window->allocation.height;
1147 else
1148 *y = 0;
1149
1150 window->resize_edges = 0;
1151}
1152
1153static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001154window_attach_surface(struct window *window)
1155{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001156 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001157
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001158 if (window->type == TYPE_NONE) {
1159 window->type = TYPE_TOPLEVEL;
1160 if (display->shell)
1161 wl_shell_surface_set_toplevel(window->shell_surface);
1162 }
1163
Pekka Paalanen512dde82012-10-10 12:49:27 +03001164 if (window->opaque_region) {
1165 wl_surface_set_opaque_region(window->surface,
1166 window->opaque_region);
1167 wl_region_destroy(window->opaque_region);
1168 window->opaque_region = NULL;
1169 }
1170
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001171 if (window->input_region) {
1172 wl_surface_set_input_region(window->surface,
1173 window->input_region);
1174 wl_region_destroy(window->input_region);
1175 window->input_region = NULL;
1176 }
1177
Pekka Paalanen99436862012-11-19 17:15:59 +02001178 window->toysurface->swap(window->toysurface,
1179 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001180}
1181
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001182int
1183window_has_focus(struct window *window)
1184{
1185 return window->focus_count > 0;
1186}
1187
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001188static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001189window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001190{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001191 if (!window->cairo_surface)
1192 return;
1193
1194 window_attach_surface(window);
1195 cairo_surface_destroy(window->cairo_surface);
1196 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001197}
1198
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001199struct display *
1200window_get_display(struct window *window)
1201{
1202 return window->display;
1203}
1204
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001205static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001206window_create_surface(struct window *window)
1207{
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001208 struct rectangle allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001209 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001210 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001211
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001212 if (!window->transparent)
1213 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001214
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001215 switch (window->buffer_transform) {
1216 case WL_OUTPUT_TRANSFORM_90:
1217 case WL_OUTPUT_TRANSFORM_270:
1218 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1219 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1220 allocation.width = window->allocation.height;
1221 allocation.height = window->allocation.width;
1222 break;
1223 default:
1224 break;
1225 }
1226
Pekka Paalanenb3627362012-11-19 17:16:00 +02001227 if (!window->toysurface &&
1228 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1229 window->display->dpy) {
1230 window->toysurface =
1231 egl_window_surface_create(window->display,
1232 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001233 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001234 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001235
Pekka Paalanenb3627362012-11-19 17:16:00 +02001236 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001237 window->toysurface = shm_surface_create(window->display,
1238 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001239 &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001240
Pekka Paalanenec076692012-11-30 13:37:27 +02001241 if (window->resizing)
1242 flags = SURFACE_HINT_RESIZE;
1243 else
1244 flags = 0;
1245
Pekka Paalanen99436862012-11-19 17:15:59 +02001246 window_get_resize_dx_dy(window, &dx, &dy);
1247 window->cairo_surface =
1248 window->toysurface->prepare(window->toysurface, dx, dy,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001249 allocation.width,
1250 allocation.height,
Pekka Paalanenec076692012-11-30 13:37:27 +02001251 flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001252}
1253
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001254int
1255window_get_buffer_transform(struct window *window)
1256{
1257 return window->buffer_transform;
1258}
1259
1260void
1261window_set_buffer_transform(struct window *window,
1262 enum wl_output_transform transform)
1263{
1264 window->buffer_transform = transform;
1265 wl_surface_set_buffer_transform(window->surface, transform);
1266}
1267
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001268static void frame_destroy(struct frame *frame);
1269
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001270void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001271window_destroy(struct window *window)
1272{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001273 struct display *display = window->display;
1274 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001275 struct window_output *window_output;
1276 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001277
1278 if (window->redraw_scheduled)
1279 wl_list_remove(&window->redraw_task.link);
1280
1281 wl_list_for_each(input, &display->input_list, link) {
1282 if (input->pointer_focus == window)
1283 input->pointer_focus = NULL;
1284 if (input->keyboard_focus == window)
1285 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001286 if (input->focus_widget &&
1287 input->focus_widget->window == window)
1288 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001289 }
1290
Rob Bradford7507b572012-05-15 17:55:34 +01001291 wl_list_for_each_safe(window_output, window_output_tmp,
1292 &window->window_output_list, link) {
1293 free (window_output);
1294 }
1295
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001296 if (window->input_region)
1297 wl_region_destroy(window->input_region);
1298 if (window->opaque_region)
1299 wl_region_destroy(window->opaque_region);
1300
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001301 if (window->frame)
1302 frame_destroy(window->frame);
1303
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001304 if (window->shell_surface)
1305 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001306 wl_surface_destroy(window->surface);
1307 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001308
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001309 if (window->toysurface)
1310 window->toysurface->destroy(window->toysurface);
1311
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001312 if (window->frame_cb)
1313 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001314 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001315 free(window);
1316}
1317
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001318static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001319widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001320{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001321 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001322
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001323 wl_list_for_each(child, &widget->child_list, link) {
1324 target = widget_find_widget(child, x, y);
1325 if (target)
1326 return target;
1327 }
1328
1329 if (widget->allocation.x <= x &&
1330 x < widget->allocation.x + widget->allocation.width &&
1331 widget->allocation.y <= y &&
1332 y < widget->allocation.y + widget->allocation.height) {
1333 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001334 }
1335
1336 return NULL;
1337}
1338
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001339static struct widget *
1340widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001341{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001342 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001343
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001344 widget = malloc(sizeof *widget);
1345 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001346 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001347 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001348 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001349 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001350 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001351 widget->tooltip = NULL;
1352 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001353 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001354
1355 return widget;
1356}
1357
1358struct widget *
1359window_add_widget(struct window *window, void *data)
1360{
1361 window->widget = widget_create(window, data);
1362 wl_list_init(&window->widget->link);
1363
1364 return window->widget;
1365}
1366
1367struct widget *
1368widget_add_widget(struct widget *parent, void *data)
1369{
1370 struct widget *widget;
1371
1372 widget = widget_create(parent->window, data);
1373 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001374
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001375 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001376}
1377
1378void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001379widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001380{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001381 struct display *display = widget->window->display;
1382 struct input *input;
1383
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001384 if (widget->tooltip) {
1385 free(widget->tooltip);
1386 widget->tooltip = NULL;
1387 }
1388
Pekka Paalanene156fb62012-01-19 13:51:38 +02001389 wl_list_for_each(input, &display->input_list, link) {
1390 if (input->focus_widget == widget)
1391 input->focus_widget = NULL;
1392 }
1393
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001394 wl_list_remove(&widget->link);
1395 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001396}
1397
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001398void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001399widget_set_default_cursor(struct widget *widget, int cursor)
1400{
1401 widget->default_cursor = cursor;
1402}
1403
1404void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001405widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001406{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001407 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001408}
1409
1410void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001411widget_set_size(struct widget *widget, int32_t width, int32_t height)
1412{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001413 widget->allocation.width = width;
1414 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001415}
1416
1417void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001418widget_set_allocation(struct widget *widget,
1419 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001420{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001421 widget->allocation.x = x;
1422 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001423 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001424}
1425
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001426void
1427widget_set_transparent(struct widget *widget, int transparent)
1428{
1429 widget->opaque = !transparent;
1430}
1431
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001432void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001433widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001434{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001435 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001436}
1437
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001438void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001439widget_set_resize_handler(struct widget *widget,
1440 widget_resize_handler_t handler)
1441{
1442 widget->resize_handler = handler;
1443}
1444
1445void
1446widget_set_redraw_handler(struct widget *widget,
1447 widget_redraw_handler_t handler)
1448{
1449 widget->redraw_handler = handler;
1450}
1451
1452void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001453widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001454{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001455 widget->enter_handler = handler;
1456}
1457
1458void
1459widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1460{
1461 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001462}
1463
1464void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001465widget_set_motion_handler(struct widget *widget,
1466 widget_motion_handler_t handler)
1467{
1468 widget->motion_handler = handler;
1469}
1470
1471void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001472widget_set_button_handler(struct widget *widget,
1473 widget_button_handler_t handler)
1474{
1475 widget->button_handler = handler;
1476}
1477
1478void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001479widget_set_axis_handler(struct widget *widget,
1480 widget_axis_handler_t handler)
1481{
1482 widget->axis_handler = handler;
1483}
1484
1485void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001486widget_schedule_redraw(struct widget *widget)
1487{
1488 window_schedule_redraw(widget->window);
1489}
1490
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001491cairo_surface_t *
1492window_get_surface(struct window *window)
1493{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001494 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001495}
1496
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001497struct wl_surface *
1498window_get_wl_surface(struct window *window)
1499{
1500 return window->surface;
1501}
1502
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001503struct wl_shell_surface *
1504window_get_wl_shell_surface(struct window *window)
1505{
1506 return window->shell_surface;
1507}
1508
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001509static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001510tooltip_redraw_handler(struct widget *widget, void *data)
1511{
1512 cairo_t *cr;
1513 const int32_t r = 3;
1514 struct tooltip *tooltip = data;
1515 int32_t width, height;
1516 struct window *window = widget->window;
1517
1518 cr = cairo_create(window->cairo_surface);
1519 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1520 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1521 cairo_paint(cr);
1522
1523 width = window->allocation.width;
1524 height = window->allocation.height;
1525 rounded_rect(cr, 0, 0, width, height, r);
1526
1527 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1528 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1529 cairo_fill(cr);
1530
1531 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1532 cairo_move_to(cr, 10, 16);
1533 cairo_show_text(cr, tooltip->entry);
1534 cairo_destroy(cr);
1535}
1536
1537static cairo_text_extents_t
1538get_text_extents(struct tooltip *tooltip)
1539{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001540 cairo_t *cr;
1541 cairo_text_extents_t extents;
1542
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001543 /* Use the dummy_surface because tooltip's surface was not
1544 * created yet, and parent does not have a valid surface
1545 * outside repaint, either.
1546 */
1547 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001548 cairo_text_extents(cr, tooltip->entry, &extents);
1549 cairo_destroy(cr);
1550
1551 return extents;
1552}
1553
1554static int
1555window_create_tooltip(struct tooltip *tooltip)
1556{
1557 struct widget *parent = tooltip->parent;
1558 struct display *display = parent->window->display;
1559 struct window *window;
1560 const int offset_y = 27;
1561 const int margin = 3;
1562 cairo_text_extents_t extents;
1563
1564 if (tooltip->widget)
1565 return 0;
1566
1567 window = window_create_transient(display, parent->window, tooltip->x,
1568 tooltip->y + offset_y,
1569 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1570 if (!window)
1571 return -1;
1572
1573 tooltip->window = window;
1574 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1575
1576 extents = get_text_extents(tooltip);
1577 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1578 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1579
1580 return 0;
1581}
1582
1583void
1584widget_destroy_tooltip(struct widget *parent)
1585{
1586 struct tooltip *tooltip = parent->tooltip;
1587
1588 parent->tooltip_count = 0;
1589 if (!tooltip)
1590 return;
1591
1592 if (tooltip->widget) {
1593 widget_destroy(tooltip->widget);
1594 window_destroy(tooltip->window);
1595 tooltip->widget = NULL;
1596 tooltip->window = NULL;
1597 }
1598
1599 close(tooltip->tooltip_fd);
1600 free(tooltip->entry);
1601 free(tooltip);
1602 parent->tooltip = NULL;
1603}
1604
1605static void
1606tooltip_func(struct task *task, uint32_t events)
1607{
1608 struct tooltip *tooltip =
1609 container_of(task, struct tooltip, tooltip_task);
1610 uint64_t exp;
1611
Martin Olsson8df662a2012-07-08 03:03:47 +02001612 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1613 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001614 window_create_tooltip(tooltip);
1615}
1616
1617#define TOOLTIP_TIMEOUT 500
1618static int
1619tooltip_timer_reset(struct tooltip *tooltip)
1620{
1621 struct itimerspec its;
1622
1623 its.it_interval.tv_sec = 0;
1624 its.it_interval.tv_nsec = 0;
1625 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1626 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1627 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1628 fprintf(stderr, "could not set timerfd\n: %m");
1629 return -1;
1630 }
1631
1632 return 0;
1633}
1634
1635int
1636widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1637{
1638 struct tooltip *tooltip = parent->tooltip;
1639
1640 parent->tooltip_count++;
1641 if (tooltip) {
1642 tooltip->x = x;
1643 tooltip->y = y;
1644 tooltip_timer_reset(tooltip);
1645 return 0;
1646 }
1647
1648 /* the handler might be triggered too fast via input device motion, so
1649 * we need this check here to make sure tooltip is fully initialized */
1650 if (parent->tooltip_count > 1)
1651 return 0;
1652
1653 tooltip = malloc(sizeof *tooltip);
1654 if (!tooltip)
1655 return -1;
1656
1657 parent->tooltip = tooltip;
1658 tooltip->parent = parent;
1659 tooltip->widget = NULL;
1660 tooltip->window = NULL;
1661 tooltip->x = x;
1662 tooltip->y = y;
1663 tooltip->entry = strdup(entry);
1664 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1665 if (tooltip->tooltip_fd < 0) {
1666 fprintf(stderr, "could not create timerfd\n: %m");
1667 return -1;
1668 }
1669
1670 tooltip->tooltip_task.run = tooltip_func;
1671 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1672 EPOLLIN, &tooltip->tooltip_task);
1673 tooltip_timer_reset(tooltip);
1674
1675 return 0;
1676}
1677
1678static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001679workspace_manager_state(void *data,
1680 struct workspace_manager *workspace_manager,
1681 uint32_t current,
1682 uint32_t count)
1683{
1684 struct display *display = data;
1685
1686 display->workspace = current;
1687 display->workspace_count = count;
1688}
1689
1690static const struct workspace_manager_listener workspace_manager_listener = {
1691 workspace_manager_state
1692};
1693
1694static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001695frame_resize_handler(struct widget *widget,
1696 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001697{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001698 struct frame *frame = data;
1699 struct widget *child = frame->child;
1700 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001701 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001702 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001703 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001704 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001705 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001706 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001707
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001708 switch (widget->window->type) {
1709 case TYPE_FULLSCREEN:
1710 decoration_width = 0;
1711 decoration_height = 0;
1712
1713 allocation.x = 0;
1714 allocation.y = 0;
1715 allocation.width = width;
1716 allocation.height = height;
1717 opaque_margin = 0;
1718
1719 wl_list_for_each(button, &frame->buttons_list, link)
1720 button->widget->opaque = 1;
1721 break;
1722 case TYPE_MAXIMIZED:
1723 decoration_width = t->width * 2;
1724 decoration_height = t->width + t->titlebar_height;
1725
1726 allocation.x = t->width;
1727 allocation.y = t->titlebar_height;
1728 allocation.width = width - decoration_width;
1729 allocation.height = height - decoration_height;
1730
1731 opaque_margin = 0;
1732
1733 wl_list_for_each(button, &frame->buttons_list, link)
1734 button->widget->opaque = 0;
1735 break;
1736 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001737 decoration_width = (t->width + t->margin) * 2;
1738 decoration_height = t->width +
1739 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001740
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001741 allocation.x = t->width + t->margin;
1742 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001743 allocation.width = width - decoration_width;
1744 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001745
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001746 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001747
1748 wl_list_for_each(button, &frame->buttons_list, link)
1749 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001750 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001751 }
1752
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001753 widget_set_allocation(child, allocation.x, allocation.y,
1754 allocation.width, allocation.height);
1755
1756 if (child->resize_handler)
1757 child->resize_handler(child,
1758 allocation.width,
1759 allocation.height,
1760 child->user_data);
1761
Scott Moreauf7e498c2012-05-14 11:39:29 -06001762 width = child->allocation.width + decoration_width;
1763 height = child->allocation.height + decoration_height;
1764
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001765 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1766
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001767 widget->window->input_region =
1768 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001769 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001770 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001771 shadow_margin, shadow_margin,
1772 width - 2 * shadow_margin,
1773 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001774 } else {
1775 wl_region_add(widget->window->input_region,
1776 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001777 }
1778
Scott Moreauf7e498c2012-05-14 11:39:29 -06001779 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001780
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001781 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001782 wl_region_add(widget->window->opaque_region,
1783 opaque_margin, opaque_margin,
1784 widget->allocation.width - 2 * opaque_margin,
1785 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001786
1787 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001788 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1789 x_l = t->width + shadow_margin;
1790 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001791 wl_list_for_each(button, &frame->buttons_list, link) {
1792 const int button_padding = 4;
1793 w = cairo_image_surface_get_width(button->icon);
1794 h = cairo_image_surface_get_height(button->icon);
1795
1796 if (button->decoration == FRAME_BUTTON_FANCY)
1797 w += 10;
1798
1799 if (button->align == FRAME_BUTTON_LEFT) {
1800 widget_set_allocation(button->widget,
1801 x_l, y , w + 1, h + 1);
1802 x_l += w;
1803 x_l += button_padding;
1804 } else {
1805 x_r -= w;
1806 widget_set_allocation(button->widget,
1807 x_r, y , w + 1, h + 1);
1808 x_r -= button_padding;
1809 }
1810 }
1811}
1812
1813static int
1814frame_button_enter_handler(struct widget *widget,
1815 struct input *input, float x, float y, void *data)
1816{
1817 struct frame_button *frame_button = data;
1818
1819 widget_schedule_redraw(frame_button->widget);
1820 frame_button->state = FRAME_BUTTON_OVER;
1821
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001822 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001823}
1824
1825static void
1826frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1827{
1828 struct frame_button *frame_button = data;
1829
1830 widget_schedule_redraw(frame_button->widget);
1831 frame_button->state = FRAME_BUTTON_DEFAULT;
1832}
1833
1834static void
1835frame_button_button_handler(struct widget *widget,
1836 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001837 uint32_t button,
1838 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001839{
1840 struct frame_button *frame_button = data;
1841 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001842 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001843
1844 if (button != BTN_LEFT)
1845 return;
1846
1847 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001848 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001849 frame_button->state = FRAME_BUTTON_ACTIVE;
1850 widget_schedule_redraw(frame_button->widget);
1851
1852 if (frame_button->type == FRAME_BUTTON_ICON)
1853 window_show_frame_menu(window, input, time);
1854 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001855 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001856 frame_button->state = FRAME_BUTTON_DEFAULT;
1857 widget_schedule_redraw(frame_button->widget);
1858 break;
1859 }
1860
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001861 if (!was_pressed)
1862 return;
1863
Martin Minarik1998b152012-05-10 02:04:35 +02001864 switch (frame_button->type) {
1865 case FRAME_BUTTON_CLOSE:
1866 if (window->close_handler)
1867 window->close_handler(window->parent,
1868 window->user_data);
1869 else
1870 display_exit(window->display);
1871 break;
1872 case FRAME_BUTTON_MINIMIZE:
1873 fprintf(stderr,"Minimize stub\n");
1874 break;
1875 case FRAME_BUTTON_MAXIMIZE:
1876 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1877 break;
1878 default:
1879 /* Unknown operation */
1880 break;
1881 }
1882}
1883
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001884static int
1885frame_button_motion_handler(struct widget *widget,
1886 struct input *input, uint32_t time,
1887 float x, float y, void *data)
1888{
1889 struct frame_button *frame_button = data;
1890 enum frame_button_pointer previous_button_state = frame_button->state;
1891
1892 /* only track state for a pressed button */
1893 if (input->grab != widget)
1894 return CURSOR_LEFT_PTR;
1895
1896 if (x > widget->allocation.x &&
1897 x < (widget->allocation.x + widget->allocation.width) &&
1898 y > widget->allocation.y &&
1899 y < (widget->allocation.y + widget->allocation.height)) {
1900 frame_button->state = FRAME_BUTTON_ACTIVE;
1901 } else {
1902 frame_button->state = FRAME_BUTTON_DEFAULT;
1903 }
1904
1905 if (frame_button->state != previous_button_state)
1906 widget_schedule_redraw(frame_button->widget);
1907
1908 return CURSOR_LEFT_PTR;
1909}
1910
Martin Minarik1998b152012-05-10 02:04:35 +02001911static void
1912frame_button_redraw_handler(struct widget *widget, void *data)
1913{
1914 struct frame_button *frame_button = data;
1915 cairo_t *cr;
1916 int width, height, x, y;
1917 struct window *window = widget->window;
1918
1919 x = widget->allocation.x;
1920 y = widget->allocation.y;
1921 width = widget->allocation.width;
1922 height = widget->allocation.height;
1923
1924 if (!width)
1925 return;
1926 if (!height)
1927 return;
1928 if (widget->opaque)
1929 return;
1930
1931 cr = cairo_create(window->cairo_surface);
1932
1933 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1934 cairo_set_line_width(cr, 1);
1935
1936 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1937 cairo_rectangle (cr, x, y, 25, 16);
1938
1939 cairo_stroke_preserve(cr);
1940
1941 switch (frame_button->state) {
1942 case FRAME_BUTTON_DEFAULT:
1943 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1944 break;
1945 case FRAME_BUTTON_OVER:
1946 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1947 break;
1948 case FRAME_BUTTON_ACTIVE:
1949 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1950 break;
1951 }
1952
1953 cairo_fill (cr);
1954
1955 x += 4;
1956 }
1957
1958 cairo_set_source_surface(cr, frame_button->icon, x, y);
1959 cairo_paint(cr);
1960
1961 cairo_destroy(cr);
1962}
1963
1964static struct widget *
1965frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1966 enum frame_button_align align, enum frame_button_decoration style)
1967{
1968 struct frame_button *frame_button;
1969 const char *icon = data;
1970
1971 frame_button = malloc (sizeof *frame_button);
1972 memset(frame_button, 0, sizeof *frame_button);
1973
1974 frame_button->icon = cairo_image_surface_create_from_png(icon);
1975 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1976 frame_button->frame = frame;
1977 frame_button->type = type;
1978 frame_button->align = align;
1979 frame_button->decoration = style;
1980
1981 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1982
1983 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1984 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1985 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1986 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001987 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001988 return frame_button->widget;
1989}
1990
1991static void
1992frame_button_destroy(struct frame_button *frame_button)
1993{
1994 widget_destroy(frame_button->widget);
1995 wl_list_remove(&frame_button->link);
1996 cairo_surface_destroy(frame_button->icon);
1997 free(frame_button);
1998
1999 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002000}
2001
2002static void
2003frame_redraw_handler(struct widget *widget, void *data)
2004{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002005 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002006 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002007 struct theme *t = window->display->theme;
2008 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002009
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002010 if (window->type == TYPE_FULLSCREEN)
2011 return;
2012
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002013 cr = cairo_create(window->cairo_surface);
2014
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002015 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002016 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002017 if (window->type == TYPE_MAXIMIZED)
2018 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002019 theme_render_frame(t, cr, widget->allocation.width,
2020 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002021
2022 cairo_destroy(cr);
2023}
2024
2025static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002026frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002027{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002028 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002029 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002030 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002031
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002032 location = theme_get_location(t, input->sx, input->sy,
2033 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002034 frame->widget->allocation.height,
2035 window->type == TYPE_MAXIMIZED ?
2036 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002037
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002038 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002039 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002040 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002041 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002042 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002043 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002044 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002045 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002046 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002047 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002048 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002049 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002050 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002051 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002052 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002053 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002054 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002055 case THEME_LOCATION_EXTERIOR:
2056 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002057 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002058 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002059 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002060}
2061
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002062static void
2063frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002064{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002065 struct display *display;
2066
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002067 switch (index) {
2068 case 0: /* close */
2069 if (window->close_handler)
2070 window->close_handler(window->parent,
2071 window->user_data);
2072 else
2073 display_exit(window->display);
2074 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002075 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002076 display = window->display;
2077 if (display->workspace > 0)
2078 workspace_manager_move_surface(display->workspace_manager,
2079 window->surface,
2080 display->workspace - 1);
2081 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002082 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002083 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002084 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002085 workspace_manager_move_surface(display->workspace_manager,
2086 window->surface,
2087 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002088 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002089 case 3: /* fullscreen */
2090 /* we don't have a way to get out of fullscreen for now */
2091 if (window->fullscreen_handler)
2092 window->fullscreen_handler(window, window->user_data);
2093 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002094 }
2095}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002096
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002097void
2098window_show_frame_menu(struct window *window,
2099 struct input *input, uint32_t time)
2100{
2101 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002102 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002103
2104 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002105 "Close",
2106 "Move to workspace above", "Move to workspace below",
2107 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002108 };
2109
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002110 if (window->fullscreen_handler)
2111 count = ARRAY_LENGTH(entries);
2112 else
2113 count = ARRAY_LENGTH(entries) - 1;
2114
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002115 input_get_position(input, &x, &y);
2116 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002117 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002118}
2119
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120static int
2121frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002122 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002123{
2124 return frame_get_pointer_image_for_location(data, input);
2125}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002126
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002127static int
2128frame_motion_handler(struct widget *widget,
2129 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002130 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002131{
2132 return frame_get_pointer_image_for_location(data, input);
2133}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002134
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002135static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002136frame_button_handler(struct widget *widget,
2137 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002138 uint32_t button, enum wl_pointer_button_state state,
2139 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002140
2141{
2142 struct frame *frame = data;
2143 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002144 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002145 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002146
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002147 location = theme_get_location(display->theme, input->sx, input->sy,
2148 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002149 frame->widget->allocation.height,
2150 window->type == TYPE_MAXIMIZED ?
2151 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002152
Daniel Stone4dbadb12012-05-30 16:31:51 +01002153 if (window->display->shell && button == BTN_LEFT &&
2154 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002155 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002156 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002157 if (!window->shell_surface)
2158 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002159 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002160 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002161 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002162 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002163 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002164 case THEME_LOCATION_RESIZING_TOP:
2165 case THEME_LOCATION_RESIZING_BOTTOM:
2166 case THEME_LOCATION_RESIZING_LEFT:
2167 case THEME_LOCATION_RESIZING_RIGHT:
2168 case THEME_LOCATION_RESIZING_TOP_LEFT:
2169 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2170 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2171 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002172 if (!window->shell_surface)
2173 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002174 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002175
Pekka Paalanen99436862012-11-19 17:15:59 +02002176 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002178 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002179 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002180 break;
2181 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002182 } else if (button == BTN_RIGHT &&
2183 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002184 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002185 }
2186}
2187
2188struct widget *
2189frame_create(struct window *window, void *data)
2190{
2191 struct frame *frame;
2192
2193 frame = malloc(sizeof *frame);
2194 memset(frame, 0, sizeof *frame);
2195
2196 frame->widget = window_add_widget(window, frame);
2197 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002198
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002199 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2200 widget_set_resize_handler(frame->widget, frame_resize_handler);
2201 widget_set_enter_handler(frame->widget, frame_enter_handler);
2202 widget_set_motion_handler(frame->widget, frame_motion_handler);
2203 widget_set_button_handler(frame->widget, frame_button_handler);
2204
Martin Minarik1998b152012-05-10 02:04:35 +02002205 /* Create empty list for frame buttons */
2206 wl_list_init(&frame->buttons_list);
2207
2208 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2209 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2210
2211 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2212 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2213
2214 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2215 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2216
2217 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2218 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2219
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002220 window->frame = frame;
2221
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002223}
2224
Kristian Høgsberga1627922012-06-20 17:30:03 -04002225void
2226frame_set_child_size(struct widget *widget, int child_width, int child_height)
2227{
2228 struct display *display = widget->window->display;
2229 struct theme *t = display->theme;
2230 int decoration_width, decoration_height;
2231 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002232 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002233
2234 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002235 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002236 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002237 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002238
2239 width = child_width + decoration_width;
2240 height = child_height + decoration_height;
2241 } else {
2242 width = child_width;
2243 height = child_height;
2244 }
2245
2246 window_schedule_resize(widget->window, width, height);
2247}
2248
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002249static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002250frame_destroy(struct frame *frame)
2251{
Martin Minarik1998b152012-05-10 02:04:35 +02002252 struct frame_button *button, *tmp;
2253
2254 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2255 frame_button_destroy(button);
2256
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002257 /* frame->child must be destroyed by the application */
2258 widget_destroy(frame->widget);
2259 free(frame);
2260}
2261
2262static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002263input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002264 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002265{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002266 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002267 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002268
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002269 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002270 return;
2271
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002272 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002273 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002274 widget = old;
2275 if (input->grab)
2276 widget = input->grab;
2277 if (widget->leave_handler)
2278 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002279 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002280 }
2281
2282 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002283 widget = focus;
2284 if (input->grab)
2285 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002286 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002287 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002288 cursor = widget->enter_handler(focus, input, x, y,
2289 widget->user_data);
2290 else
2291 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002292
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002293 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002294 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002295}
2296
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002297void
2298input_grab(struct input *input, struct widget *widget, uint32_t button)
2299{
2300 input->grab = widget;
2301 input->grab_button = button;
2302}
2303
2304void
2305input_ungrab(struct input *input)
2306{
2307 struct widget *widget;
2308
2309 input->grab = NULL;
2310 if (input->pointer_focus) {
2311 widget = widget_find_widget(input->pointer_focus->widget,
2312 input->sx, input->sy);
2313 input_set_focus_widget(input, widget, input->sx, input->sy);
2314 }
2315}
2316
2317static void
2318input_remove_pointer_focus(struct input *input)
2319{
2320 struct window *window = input->pointer_focus;
2321
2322 if (!window)
2323 return;
2324
2325 input_set_focus_widget(input, NULL, 0, 0);
2326
2327 input->pointer_focus = NULL;
2328 input->current_cursor = CURSOR_UNSET;
2329}
2330
2331static void
2332pointer_handle_enter(void *data, struct wl_pointer *pointer,
2333 uint32_t serial, struct wl_surface *surface,
2334 wl_fixed_t sx_w, wl_fixed_t sy_w)
2335{
2336 struct input *input = data;
2337 struct window *window;
2338 struct widget *widget;
2339 float sx = wl_fixed_to_double(sx_w);
2340 float sy = wl_fixed_to_double(sy_w);
2341
2342 if (!surface) {
2343 /* enter event for a window we've just destroyed */
2344 return;
2345 }
2346
2347 input->display->serial = serial;
2348 input->pointer_enter_serial = serial;
2349 input->pointer_focus = wl_surface_get_user_data(surface);
2350 window = input->pointer_focus;
2351
Pekka Paalanen99436862012-11-19 17:15:59 +02002352 if (window->resizing) {
2353 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002354 /* Schedule a redraw to free the pool */
2355 window_schedule_redraw(window);
2356 }
2357
2358 input->sx = sx;
2359 input->sy = sy;
2360
2361 widget = widget_find_widget(window->widget, sx, sy);
2362 input_set_focus_widget(input, widget, sx, sy);
2363}
2364
2365static void
2366pointer_handle_leave(void *data, struct wl_pointer *pointer,
2367 uint32_t serial, struct wl_surface *surface)
2368{
2369 struct input *input = data;
2370
2371 input->display->serial = serial;
2372 input_remove_pointer_focus(input);
2373}
2374
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002375static void
Daniel Stone37816df2012-05-16 18:45:18 +01002376pointer_handle_motion(void *data, struct wl_pointer *pointer,
2377 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002378{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002379 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002380 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002381 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002382 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002383 float sx = wl_fixed_to_double(sx_w);
2384 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002385
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002386 input->sx = sx;
2387 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002388
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002389 if (!window)
2390 return;
2391
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002392 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002393 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002394 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002395 }
2396
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002397 if (input->grab)
2398 widget = input->grab;
2399 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002400 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002401 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002402 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002403 input, time, sx, sy,
2404 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002405 else
2406 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002407
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002408 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002409}
2410
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002411static void
Daniel Stone37816df2012-05-16 18:45:18 +01002412pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002413 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002414{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002415 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002416 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002417 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002418
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002419 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002420 if (input->focus_widget && input->grab == NULL &&
2421 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002422 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002423
Neil Roberts6b28aad2012-01-23 19:11:18 +00002424 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002425 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002426 (*widget->button_handler)(widget,
2427 input, time,
2428 button, state,
2429 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002430
Daniel Stone4dbadb12012-05-30 16:31:51 +01002431 if (input->grab && input->grab_button == button &&
2432 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002433 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002434}
2435
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002436static void
Daniel Stone37816df2012-05-16 18:45:18 +01002437pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002438 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002439{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002440 struct input *input = data;
2441 struct widget *widget;
2442
2443 widget = input->focus_widget;
2444 if (input->grab)
2445 widget = input->grab;
2446 if (widget && widget->axis_handler)
2447 (*widget->axis_handler)(widget,
2448 input, time,
2449 axis, value,
2450 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002451}
2452
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002453static const struct wl_pointer_listener pointer_listener = {
2454 pointer_handle_enter,
2455 pointer_handle_leave,
2456 pointer_handle_motion,
2457 pointer_handle_button,
2458 pointer_handle_axis,
2459};
2460
2461static void
2462input_remove_keyboard_focus(struct input *input)
2463{
2464 struct window *window = input->keyboard_focus;
2465 struct itimerspec its;
2466
2467 its.it_interval.tv_sec = 0;
2468 its.it_interval.tv_nsec = 0;
2469 its.it_value.tv_sec = 0;
2470 its.it_value.tv_nsec = 0;
2471 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2472
2473 if (!window)
2474 return;
2475
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002476 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002477 if (window->keyboard_focus_handler)
2478 (*window->keyboard_focus_handler)(window, NULL,
2479 window->user_data);
2480
2481 input->keyboard_focus = NULL;
2482}
2483
2484static void
2485keyboard_repeat_func(struct task *task, uint32_t events)
2486{
2487 struct input *input =
2488 container_of(task, struct input, repeat_task);
2489 struct window *window = input->keyboard_focus;
2490 uint64_t exp;
2491
2492 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2493 /* If we change the timer between the fd becoming
2494 * readable and getting here, there'll be nothing to
2495 * read and we get EAGAIN. */
2496 return;
2497
2498 if (window && window->key_handler) {
2499 (*window->key_handler)(window, input, input->repeat_time,
2500 input->repeat_key, input->repeat_sym,
2501 WL_KEYBOARD_KEY_STATE_PRESSED,
2502 window->user_data);
2503 }
2504}
2505
2506static void
2507keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2508 uint32_t format, int fd, uint32_t size)
2509{
2510 struct input *input = data;
2511 char *map_str;
2512
2513 if (!data) {
2514 close(fd);
2515 return;
2516 }
2517
2518 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2519 close(fd);
2520 return;
2521 }
2522
2523 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2524 if (map_str == MAP_FAILED) {
2525 close(fd);
2526 return;
2527 }
2528
2529 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2530 map_str,
2531 XKB_KEYMAP_FORMAT_TEXT_V1,
2532 0);
2533 munmap(map_str, size);
2534 close(fd);
2535
2536 if (!input->xkb.keymap) {
2537 fprintf(stderr, "failed to compile keymap\n");
2538 return;
2539 }
2540
2541 input->xkb.state = xkb_state_new(input->xkb.keymap);
2542 if (!input->xkb.state) {
2543 fprintf(stderr, "failed to create XKB state\n");
2544 xkb_map_unref(input->xkb.keymap);
2545 input->xkb.keymap = NULL;
2546 return;
2547 }
2548
2549 input->xkb.control_mask =
2550 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2551 input->xkb.alt_mask =
2552 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2553 input->xkb.shift_mask =
2554 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2555}
2556
2557static void
2558keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2559 uint32_t serial, struct wl_surface *surface,
2560 struct wl_array *keys)
2561{
2562 struct input *input = data;
2563 struct window *window;
2564
2565 input->display->serial = serial;
2566 input->keyboard_focus = wl_surface_get_user_data(surface);
2567
2568 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002569 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002570 if (window->keyboard_focus_handler)
2571 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002572 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002573}
2574
2575static void
2576keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2577 uint32_t serial, struct wl_surface *surface)
2578{
2579 struct input *input = data;
2580
2581 input->display->serial = serial;
2582 input_remove_keyboard_focus(input);
2583}
2584
Scott Moreau210d0792012-03-22 10:47:01 -06002585static void
Daniel Stone37816df2012-05-16 18:45:18 +01002586keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002587 uint32_t serial, uint32_t time, uint32_t key,
2588 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002589{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002590 struct input *input = data;
2591 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002592 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002593 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002594 const xkb_keysym_t *syms;
2595 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002596 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002597
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002598 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002599 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002600 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002601 return;
2602
Daniel Stone97f68542012-05-30 16:32:01 +01002603 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002604
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002605 sym = XKB_KEY_NoSymbol;
2606 if (num_syms == 1)
2607 sym = syms[0];
2608
2609 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002610 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002611 window_set_maximized(window,
2612 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002613 } else if (sym == XKB_KEY_F11 &&
2614 window->fullscreen_handler &&
2615 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2616 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002617 } else if (sym == XKB_KEY_F4 &&
2618 input->modifiers == MOD_ALT_MASK &&
2619 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2620 if (window->close_handler)
2621 window->close_handler(window->parent,
2622 window->user_data);
2623 else
2624 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002625 } else if (window->key_handler) {
2626 (*window->key_handler)(window, input, time, key,
2627 sym, state, window->user_data);
2628 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002629
2630 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2631 key == input->repeat_key) {
2632 its.it_interval.tv_sec = 0;
2633 its.it_interval.tv_nsec = 0;
2634 its.it_value.tv_sec = 0;
2635 its.it_value.tv_nsec = 0;
2636 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2637 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2638 input->repeat_sym = sym;
2639 input->repeat_key = key;
2640 input->repeat_time = time;
2641 its.it_interval.tv_sec = 0;
2642 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2643 its.it_value.tv_sec = 0;
2644 its.it_value.tv_nsec = 400 * 1000 * 1000;
2645 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2646 }
2647}
2648
2649static void
Daniel Stone351eb612012-05-31 15:27:47 -04002650keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2651 uint32_t serial, uint32_t mods_depressed,
2652 uint32_t mods_latched, uint32_t mods_locked,
2653 uint32_t group)
2654{
2655 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002656 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002657
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002658 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2659 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002660 mask = xkb_state_serialize_mods(input->xkb.state,
2661 XKB_STATE_DEPRESSED |
2662 XKB_STATE_LATCHED);
2663 input->modifiers = 0;
2664 if (mask & input->xkb.control_mask)
2665 input->modifiers |= MOD_CONTROL_MASK;
2666 if (mask & input->xkb.alt_mask)
2667 input->modifiers |= MOD_ALT_MASK;
2668 if (mask & input->xkb.shift_mask)
2669 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002670}
2671
Daniel Stone37816df2012-05-16 18:45:18 +01002672static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002673 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002674 keyboard_handle_enter,
2675 keyboard_handle_leave,
2676 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002677 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002678};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002679
2680static void
Daniel Stone37816df2012-05-16 18:45:18 +01002681seat_handle_capabilities(void *data, struct wl_seat *seat,
2682 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002683{
Daniel Stone37816df2012-05-16 18:45:18 +01002684 struct input *input = data;
2685
2686 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2687 input->pointer = wl_seat_get_pointer(seat);
2688 wl_pointer_set_user_data(input->pointer, input);
2689 wl_pointer_add_listener(input->pointer, &pointer_listener,
2690 input);
2691 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2692 wl_pointer_destroy(input->pointer);
2693 input->pointer = NULL;
2694 }
2695
2696 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2697 input->keyboard = wl_seat_get_keyboard(seat);
2698 wl_keyboard_set_user_data(input->keyboard, input);
2699 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2700 input);
2701 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2702 wl_keyboard_destroy(input->keyboard);
2703 input->keyboard = NULL;
2704 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002705}
2706
Daniel Stone37816df2012-05-16 18:45:18 +01002707static const struct wl_seat_listener seat_listener = {
2708 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002709};
2710
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002711void
2712input_get_position(struct input *input, int32_t *x, int32_t *y)
2713{
2714 *x = input->sx;
2715 *y = input->sy;
2716}
2717
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002718struct display *
2719input_get_display(struct input *input)
2720{
2721 return input->display;
2722}
2723
Daniel Stone37816df2012-05-16 18:45:18 +01002724struct wl_seat *
2725input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002726{
Daniel Stone37816df2012-05-16 18:45:18 +01002727 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002728}
2729
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002730uint32_t
2731input_get_modifiers(struct input *input)
2732{
2733 return input->modifiers;
2734}
2735
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002736struct widget *
2737input_get_focus_widget(struct input *input)
2738{
2739 return input->focus_widget;
2740}
2741
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002742struct data_offer {
2743 struct wl_data_offer *offer;
2744 struct input *input;
2745 struct wl_array types;
2746 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002747
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002748 struct task io_task;
2749 int fd;
2750 data_func_t func;
2751 int32_t x, y;
2752 void *user_data;
2753};
2754
2755static void
2756data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2757{
2758 struct data_offer *offer = data;
2759 char **p;
2760
2761 p = wl_array_add(&offer->types, sizeof *p);
2762 *p = strdup(type);
2763}
2764
2765static const struct wl_data_offer_listener data_offer_listener = {
2766 data_offer_offer,
2767};
2768
2769static void
2770data_offer_destroy(struct data_offer *offer)
2771{
2772 char **p;
2773
2774 offer->refcount--;
2775 if (offer->refcount == 0) {
2776 wl_data_offer_destroy(offer->offer);
2777 for (p = offer->types.data; *p; p++)
2778 free(*p);
2779 wl_array_release(&offer->types);
2780 free(offer);
2781 }
2782}
2783
2784static void
2785data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002786 struct wl_data_device *data_device,
2787 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002788{
2789 struct data_offer *offer;
2790
2791 offer = malloc(sizeof *offer);
2792
2793 wl_array_init(&offer->types);
2794 offer->refcount = 1;
2795 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002796 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002797 wl_data_offer_add_listener(offer->offer,
2798 &data_offer_listener, offer);
2799}
2800
2801static void
2802data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002803 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002804 wl_fixed_t x_w, wl_fixed_t y_w,
2805 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002806{
2807 struct input *input = data;
2808 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002809 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002810 float x = wl_fixed_to_double(x_w);
2811 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002812 char **p;
2813
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002814 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002815 window = wl_surface_get_user_data(surface);
2816 input->pointer_focus = window;
2817
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002818 if (offer) {
2819 input->drag_offer = wl_data_offer_get_user_data(offer);
2820
2821 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2822 *p = NULL;
2823
2824 types_data = input->drag_offer->types.data;
2825 } else {
2826 input->drag_offer = NULL;
2827 types_data = NULL;
2828 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002829
2830 window = input->pointer_focus;
2831 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002832 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002833 window->user_data);
2834}
2835
2836static void
2837data_device_leave(void *data, struct wl_data_device *data_device)
2838{
2839 struct input *input = data;
2840
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002841 if (input->drag_offer) {
2842 data_offer_destroy(input->drag_offer);
2843 input->drag_offer = NULL;
2844 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002845}
2846
2847static void
2848data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002849 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002850{
2851 struct input *input = data;
2852 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002853 float x = wl_fixed_to_double(x_w);
2854 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002855 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002856
2857 input->sx = x;
2858 input->sy = y;
2859
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002860 if (input->drag_offer)
2861 types_data = input->drag_offer->types.data;
2862 else
2863 types_data = NULL;
2864
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002865 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002866 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002867 window->user_data);
2868}
2869
2870static void
2871data_device_drop(void *data, struct wl_data_device *data_device)
2872{
2873 struct input *input = data;
2874 struct window *window = input->pointer_focus;
2875
2876 if (window->drop_handler)
2877 window->drop_handler(window, input,
2878 input->sx, input->sy, window->user_data);
2879}
2880
2881static void
2882data_device_selection(void *data,
2883 struct wl_data_device *wl_data_device,
2884 struct wl_data_offer *offer)
2885{
2886 struct input *input = data;
2887 char **p;
2888
2889 if (input->selection_offer)
2890 data_offer_destroy(input->selection_offer);
2891
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002892 if (offer) {
2893 input->selection_offer = wl_data_offer_get_user_data(offer);
2894 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2895 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002896 } else {
2897 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002898 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002899}
2900
2901static const struct wl_data_device_listener data_device_listener = {
2902 data_device_data_offer,
2903 data_device_enter,
2904 data_device_leave,
2905 data_device_motion,
2906 data_device_drop,
2907 data_device_selection
2908};
2909
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002910static void
2911input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002912{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002913 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002914 struct wl_cursor *cursor;
2915 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002916
Daniel Stone80972742012-11-07 17:51:39 +11002917 if (!input->pointer)
2918 return;
2919
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002920 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002921 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002922 return;
2923
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002924 if (index >= (int) cursor->image_count) {
2925 fprintf(stderr, "cursor index out of range\n");
2926 return;
2927 }
2928
2929 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002930 buffer = wl_cursor_image_get_buffer(image);
2931 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002932 return;
2933
Kristian Høgsbergae277372012-08-01 09:41:08 -04002934 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002935 input->pointer_surface,
2936 image->hotspot_x, image->hotspot_y);
2937 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2938 wl_surface_damage(input->pointer_surface, 0, 0,
2939 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002940 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002941}
2942
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002943static const struct wl_callback_listener pointer_surface_listener;
2944
2945static void
2946pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2947 uint32_t time)
2948{
2949 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002950 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002951 int i;
2952
2953 if (callback) {
2954 assert(callback == input->cursor_frame_cb);
2955 wl_callback_destroy(callback);
2956 input->cursor_frame_cb = NULL;
2957 }
2958
Daniel Stone80972742012-11-07 17:51:39 +11002959 if (!input->pointer)
2960 return;
2961
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002962 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002963 wl_pointer_set_cursor(input->pointer,
2964 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002965 NULL, 0, 0);
2966 return;
2967 }
2968
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002969 if (input->current_cursor == CURSOR_UNSET)
2970 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002971 cursor = input->display->cursors[input->current_cursor];
2972 if (!cursor)
2973 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002974
2975 /* FIXME We don't have the current time on the first call so we set
2976 * the animation start to the time of the first frame callback. */
2977 if (time == 0)
2978 input->cursor_anim_start = 0;
2979 else if (input->cursor_anim_start == 0)
2980 input->cursor_anim_start = time;
2981
2982 if (time == 0 || input->cursor_anim_start == 0)
2983 i = 0;
2984 else
2985 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2986
Pekka Paalanenbc106382012-10-10 12:49:31 +03002987 if (cursor->image_count > 1) {
2988 input->cursor_frame_cb =
2989 wl_surface_frame(input->pointer_surface);
2990 wl_callback_add_listener(input->cursor_frame_cb,
2991 &pointer_surface_listener, input);
2992 }
2993
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002994 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002995}
2996
2997static const struct wl_callback_listener pointer_surface_listener = {
2998 pointer_surface_frame_callback
2999};
3000
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003001void
3002input_set_pointer_image(struct input *input, int pointer)
3003{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003004 int force = 0;
3005
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003006 if (!input->pointer)
3007 return;
3008
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003009 if (input->pointer_enter_serial > input->cursor_serial)
3010 force = 1;
3011
3012 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003013 return;
3014
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003015 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003016 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003017 if (!input->cursor_frame_cb)
3018 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003019 else if (force) {
3020 /* The current frame callback may be stuck if, for instance,
3021 * the set cursor request was processed by the server after
3022 * this client lost the focus. In this case the cursor surface
3023 * might not be mapped and the frame callback wouldn't ever
3024 * complete. Send a set_cursor and attach to try to map the
3025 * cursor surface again so that the callback will finish */
3026 input_set_pointer_image_index(input, 0);
3027 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003028}
3029
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003030struct wl_data_device *
3031input_get_data_device(struct input *input)
3032{
3033 return input->data_device;
3034}
3035
3036void
3037input_set_selection(struct input *input,
3038 struct wl_data_source *source, uint32_t time)
3039{
3040 wl_data_device_set_selection(input->data_device, source, time);
3041}
3042
3043void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003044input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003045{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003046 wl_data_offer_accept(input->drag_offer->offer,
3047 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003048}
3049
3050static void
3051offer_io_func(struct task *task, uint32_t events)
3052{
3053 struct data_offer *offer =
3054 container_of(task, struct data_offer, io_task);
3055 unsigned int len;
3056 char buffer[4096];
3057
3058 len = read(offer->fd, buffer, sizeof buffer);
3059 offer->func(buffer, len,
3060 offer->x, offer->y, offer->user_data);
3061
3062 if (len == 0) {
3063 close(offer->fd);
3064 data_offer_destroy(offer);
3065 }
3066}
3067
3068static void
3069data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3070 data_func_t func, void *user_data)
3071{
3072 int p[2];
3073
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003074 if (pipe2(p, O_CLOEXEC) == -1)
3075 return;
3076
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003077 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3078 close(p[1]);
3079
3080 offer->io_task.run = offer_io_func;
3081 offer->fd = p[0];
3082 offer->func = func;
3083 offer->refcount++;
3084 offer->user_data = user_data;
3085
3086 display_watch_fd(offer->input->display,
3087 offer->fd, EPOLLIN, &offer->io_task);
3088}
3089
3090void
3091input_receive_drag_data(struct input *input, const char *mime_type,
3092 data_func_t func, void *data)
3093{
3094 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3095 input->drag_offer->x = input->sx;
3096 input->drag_offer->y = input->sy;
3097}
3098
3099int
3100input_receive_selection_data(struct input *input, const char *mime_type,
3101 data_func_t func, void *data)
3102{
3103 char **p;
3104
3105 if (input->selection_offer == NULL)
3106 return -1;
3107
3108 for (p = input->selection_offer->types.data; *p; p++)
3109 if (strcmp(mime_type, *p) == 0)
3110 break;
3111
3112 if (*p == NULL)
3113 return -1;
3114
3115 data_offer_receive_data(input->selection_offer,
3116 mime_type, func, data);
3117 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003118}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003119
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003120int
3121input_receive_selection_data_to_fd(struct input *input,
3122 const char *mime_type, int fd)
3123{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003124 if (input->selection_offer)
3125 wl_data_offer_receive(input->selection_offer->offer,
3126 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003127
3128 return 0;
3129}
3130
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003131void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003132window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003133{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003134 if (!window->shell_surface)
3135 return;
3136
Daniel Stone37816df2012-05-16 18:45:18 +01003137 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003138}
3139
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003140static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003141idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003142{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003143 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003144 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003145
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003146 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003147 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003148 widget_set_allocation(widget,
3149 window->pending_allocation.x,
3150 window->pending_allocation.y,
3151 window->pending_allocation.width,
3152 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003153
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003154 if (window->input_region) {
3155 wl_region_destroy(window->input_region);
3156 window->input_region = NULL;
3157 }
3158
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003159 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003160 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003161
3162 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003163
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003164 if (widget->resize_handler)
3165 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003166 widget->allocation.width,
3167 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003168 widget->user_data);
3169
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003170 if (window->allocation.width != widget->allocation.width ||
3171 window->allocation.height != widget->allocation.height) {
3172 window->allocation = widget->allocation;
3173 window_schedule_redraw(window);
3174 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003175
3176 if (widget->opaque)
3177 wl_region_add(window->opaque_region, 0, 0,
3178 widget->allocation.width,
3179 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003180}
3181
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003182void
3183window_schedule_resize(struct window *window, int width, int height)
3184{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003185 window->pending_allocation.x = 0;
3186 window->pending_allocation.y = 0;
3187 window->pending_allocation.width = width;
3188 window->pending_allocation.height = height;
3189
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003190 if (window->min_allocation.width == 0)
3191 window->min_allocation = window->pending_allocation;
3192 if (window->pending_allocation.width < window->min_allocation.width)
3193 window->pending_allocation.width = window->min_allocation.width;
3194 if (window->pending_allocation.height < window->min_allocation.height)
3195 window->pending_allocation.height = window->min_allocation.height;
3196
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003197 window->resize_needed = 1;
3198 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003199}
3200
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003201void
3202widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3203{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003204 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003205}
3206
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003207static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003208handle_ping(void *data, struct wl_shell_surface *shell_surface,
3209 uint32_t serial)
3210{
3211 wl_shell_surface_pong(shell_surface, serial);
3212}
3213
3214static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003215handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003216 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003217{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003218 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003219
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003220 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003221 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003222}
3223
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003224static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003225menu_destroy(struct menu *menu)
3226{
3227 widget_destroy(menu->widget);
3228 window_destroy(menu->window);
3229 free(menu);
3230}
3231
3232static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003233handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3234{
3235 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003236 struct menu *menu = window->widget->user_data;
3237
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003238 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003239 * device. Or just use wl_callback. And this really needs to
3240 * be a window vfunc that the menu can set. And we need the
3241 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003242
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003243 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003244 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003245 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003246}
3247
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003248static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003249 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003250 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003251 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003252};
3253
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003254void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003255window_get_allocation(struct window *window,
3256 struct rectangle *allocation)
3257{
3258 *allocation = window->allocation;
3259}
3260
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003261static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003262widget_redraw(struct widget *widget)
3263{
3264 struct widget *child;
3265
3266 if (widget->redraw_handler)
3267 widget->redraw_handler(widget, widget->user_data);
3268 wl_list_for_each(child, &widget->child_list, link)
3269 widget_redraw(child);
3270}
3271
3272static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003273frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3274{
3275 struct window *window = data;
3276
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003277 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003278 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003279 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003280 window->redraw_scheduled = 0;
3281 if (window->redraw_needed)
3282 window_schedule_redraw(window);
3283}
3284
3285static const struct wl_callback_listener listener = {
3286 frame_callback
3287};
3288
3289static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003290idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003291{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003292 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003293
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003294 if (window->resize_needed)
3295 idle_resize(window);
3296
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003297 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003298 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003299 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003300 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003301
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003302 window->frame_cb = wl_surface_frame(window->surface);
3303 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003304 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003305}
3306
3307void
3308window_schedule_redraw(struct window *window)
3309{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003310 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003311 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003312 window->redraw_task.run = idle_redraw;
3313 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003314 window->redraw_scheduled = 1;
3315 }
3316}
3317
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003318int
3319window_is_fullscreen(struct window *window)
3320{
3321 return window->type == TYPE_FULLSCREEN;
3322}
3323
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003324void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003325window_set_fullscreen(struct window *window, int fullscreen)
3326{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003327 if (!window->display->shell)
3328 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003329
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003330 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003331 return;
3332
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003333 if (fullscreen) {
3334 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003335 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003336 wl_shell_surface_set_fullscreen(window->shell_surface,
3337 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3338 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003339 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003340 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003341 wl_shell_surface_set_toplevel(window->shell_surface);
3342 window_schedule_resize(window,
3343 window->saved_allocation.width,
3344 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003345 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003346}
3347
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003348int
3349window_is_maximized(struct window *window)
3350{
3351 return window->type == TYPE_MAXIMIZED;
3352}
3353
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003354void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003355window_set_maximized(struct window *window, int maximized)
3356{
3357 if (!window->display->shell)
3358 return;
3359
3360 if ((window->type == TYPE_MAXIMIZED) == maximized)
3361 return;
3362
3363 if (window->type == TYPE_TOPLEVEL) {
3364 window->saved_allocation = window->allocation;
3365 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3366 window->type = TYPE_MAXIMIZED;
3367 } else {
3368 wl_shell_surface_set_toplevel(window->shell_surface);
3369 window->type = TYPE_TOPLEVEL;
3370 window_schedule_resize(window,
3371 window->saved_allocation.width,
3372 window->saved_allocation.height);
3373 }
3374}
3375
3376void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003377window_set_user_data(struct window *window, void *data)
3378{
3379 window->user_data = data;
3380}
3381
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003382void *
3383window_get_user_data(struct window *window)
3384{
3385 return window->user_data;
3386}
3387
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003388void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003389window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003390 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003391{
3392 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003393}
3394
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003395void
3396window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003397 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003398{
3399 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003400}
3401
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003402void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003403window_set_data_handler(struct window *window, window_data_handler_t handler)
3404{
3405 window->data_handler = handler;
3406}
3407
3408void
3409window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3410{
3411 window->drop_handler = handler;
3412}
3413
3414void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003415window_set_close_handler(struct window *window,
3416 window_close_handler_t handler)
3417{
3418 window->close_handler = handler;
3419}
3420
3421void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003422window_set_fullscreen_handler(struct window *window,
3423 window_fullscreen_handler_t handler)
3424{
3425 window->fullscreen_handler = handler;
3426}
3427
3428void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003429window_set_output_handler(struct window *window,
3430 window_output_handler_t handler)
3431{
3432 window->output_handler = handler;
3433}
3434
3435void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003436window_set_title(struct window *window, const char *title)
3437{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003438 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003439 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003440 if (window->shell_surface)
3441 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003442}
3443
3444const char *
3445window_get_title(struct window *window)
3446{
3447 return window->title;
3448}
3449
3450void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003451window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3452{
3453 struct text_cursor_position *text_cursor_position =
3454 window->display->text_cursor_position;
3455
Scott Moreau9295ce02012-06-01 12:46:10 -06003456 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003457 return;
3458
3459 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003460 window->surface,
3461 wl_fixed_from_int(x),
3462 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003463}
3464
3465void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003466window_damage(struct window *window, int32_t x, int32_t y,
3467 int32_t width, int32_t height)
3468{
3469 wl_surface_damage(window->surface, x, y, width, height);
3470}
3471
Casey Dahlin9074db52012-04-19 22:50:09 -04003472static void
3473surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003474 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003475{
Rob Bradford7507b572012-05-15 17:55:34 +01003476 struct window *window = data;
3477 struct output *output;
3478 struct output *output_found = NULL;
3479 struct window_output *window_output;
3480
3481 wl_list_for_each(output, &window->display->output_list, link) {
3482 if (output->output == wl_output) {
3483 output_found = output;
3484 break;
3485 }
3486 }
3487
3488 if (!output_found)
3489 return;
3490
3491 window_output = malloc (sizeof *window_output);
3492 window_output->output = output_found;
3493
3494 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003495
3496 if (window->output_handler)
3497 window->output_handler(window, output_found, 1,
3498 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003499}
3500
3501static void
3502surface_leave(void *data,
3503 struct wl_surface *wl_surface, struct wl_output *output)
3504{
Rob Bradford7507b572012-05-15 17:55:34 +01003505 struct window *window = data;
3506 struct window_output *window_output;
3507 struct window_output *window_output_found = NULL;
3508
3509 wl_list_for_each(window_output, &window->window_output_list, link) {
3510 if (window_output->output->output == output) {
3511 window_output_found = window_output;
3512 break;
3513 }
3514 }
3515
3516 if (window_output_found) {
3517 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003518
3519 if (window->output_handler)
3520 window->output_handler(window, window_output->output,
3521 0, window->user_data);
3522
Rob Bradford7507b572012-05-15 17:55:34 +01003523 free(window_output_found);
3524 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003525}
3526
3527static const struct wl_surface_listener surface_listener = {
3528 surface_enter,
3529 surface_leave
3530};
3531
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003532static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003533window_create_internal(struct display *display,
3534 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003535{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003536 struct window *window;
3537
3538 window = malloc(sizeof *window);
3539 if (window == NULL)
3540 return NULL;
3541
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003542 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003543 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003544 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003545 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003546 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003547 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003548 window->shell_surface =
3549 wl_shell_get_shell_surface(display->shell,
3550 window->surface);
3551 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003552 window->allocation.x = 0;
3553 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003554 window->allocation.width = 0;
3555 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003556 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003557 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003558 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003559 window->input_region = NULL;
3560 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003561
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003562 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003563#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003564 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003565#else
3566 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3567#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003568 else
3569 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003570
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003571 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003572 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003573 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003574
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003575 if (window->shell_surface) {
3576 wl_shell_surface_set_user_data(window->shell_surface, window);
3577 wl_shell_surface_add_listener(window->shell_surface,
3578 &shell_surface_listener, window);
3579 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003580
Rob Bradford7507b572012-05-15 17:55:34 +01003581 wl_list_init (&window->window_output_list);
3582
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003583 return window;
3584}
3585
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003586struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003587window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003588{
3589 struct window *window;
3590
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003591 window = window_create_internal(display, NULL, TYPE_NONE);
3592 if (!window)
3593 return NULL;
3594
3595 return window;
3596}
3597
3598struct window *
3599window_create_custom(struct display *display)
3600{
3601 struct window *window;
3602
3603 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003604 if (!window)
3605 return NULL;
3606
3607 return window;
3608}
3609
3610struct window *
3611window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003612 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003613{
3614 struct window *window;
3615
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003616 window = window_create_internal(parent->display,
3617 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003618 if (!window)
3619 return NULL;
3620
3621 window->x = x;
3622 window->y = y;
3623
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003624 if (display->shell)
3625 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003626 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003627 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003628
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003629 return window;
3630}
3631
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003632static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003633menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003634{
3635 int next;
3636
3637 next = (sy - 8) / 20;
3638 if (menu->current != next) {
3639 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003640 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003641 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003642}
3643
3644static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003645menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003646 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003647 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003648{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003649 struct menu *menu = data;
3650
3651 if (widget == menu->widget)
3652 menu_set_item(data, y);
3653
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003654 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003655}
3656
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003657static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003658menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003659 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003660{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003661 struct menu *menu = data;
3662
3663 if (widget == menu->widget)
3664 menu_set_item(data, y);
3665
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003666 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003667}
3668
3669static void
3670menu_leave_handler(struct widget *widget, struct input *input, void *data)
3671{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003672 struct menu *menu = data;
3673
3674 if (widget == menu->widget)
3675 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003676}
3677
3678static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003679menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003680 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003681 uint32_t button, enum wl_pointer_button_state state,
3682 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003683
3684{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003685 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003686
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003687 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3688 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003689 /* Either relase after press-drag-release or
3690 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003691 menu->func(menu->window->parent,
3692 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003693 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003694 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003695 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003696 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003697 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003698}
3699
3700static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003701menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003702{
3703 cairo_t *cr;
3704 const int32_t r = 3, margin = 3;
3705 struct menu *menu = data;
3706 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003707 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003708
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003709 cr = cairo_create(window->cairo_surface);
3710 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3711 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3712 cairo_paint(cr);
3713
3714 width = window->allocation.width;
3715 height = window->allocation.height;
3716 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003717
3718 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003719 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3720 cairo_fill(cr);
3721
3722 for (i = 0; i < menu->count; i++) {
3723 if (i == menu->current) {
3724 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3725 cairo_rectangle(cr, margin, i * 20 + margin,
3726 width - 2 * margin, 20);
3727 cairo_fill(cr);
3728 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3729 cairo_move_to(cr, 10, i * 20 + 16);
3730 cairo_show_text(cr, menu->entries[i]);
3731 } else {
3732 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3733 cairo_move_to(cr, 10, i * 20 + 16);
3734 cairo_show_text(cr, menu->entries[i]);
3735 }
3736 }
3737
3738 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003739}
3740
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003741void
3742window_show_menu(struct display *display,
3743 struct input *input, uint32_t time, struct window *parent,
3744 int32_t x, int32_t y,
3745 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003746{
3747 struct window *window;
3748 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003749 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003750
3751 menu = malloc(sizeof *menu);
3752 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003753 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003754
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003755 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003756 if (!window) {
3757 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003758 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003759 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003760
3761 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003762 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003763 menu->entries = entries;
3764 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003765 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003766 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003767 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003768 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003769 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003770 window->type = TYPE_MENU;
3771 window->x = x;
3772 window->y = y;
3773
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003774 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003775 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003776 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003777 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003778 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003779
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003780 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003781 widget_set_enter_handler(menu->widget, menu_enter_handler);
3782 widget_set_leave_handler(menu->widget, menu_leave_handler);
3783 widget_set_motion_handler(menu->widget, menu_motion_handler);
3784 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003785
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003786 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003787 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003788}
3789
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003790void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003791window_set_buffer_type(struct window *window, enum window_buffer_type type)
3792{
3793 window->buffer_type = type;
3794}
3795
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003796
3797static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003798display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003799 struct wl_output *wl_output,
3800 int x, int y,
3801 int physical_width,
3802 int physical_height,
3803 int subpixel,
3804 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003805 const char *model,
3806 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003807{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003808 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003809
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003810 output->allocation.x = x;
3811 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003812 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003813}
3814
3815static void
3816display_handle_mode(void *data,
3817 struct wl_output *wl_output,
3818 uint32_t flags,
3819 int width,
3820 int height,
3821 int refresh)
3822{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003823 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003824 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003825
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003826 if (flags & WL_OUTPUT_MODE_CURRENT) {
3827 output->allocation.width = width;
3828 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003829 if (display->output_configure_handler)
3830 (*display->output_configure_handler)(
3831 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003832 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003833}
3834
3835static const struct wl_output_listener output_listener = {
3836 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003837 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003838};
3839
3840static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003841display_add_output(struct display *d, uint32_t id)
3842{
3843 struct output *output;
3844
3845 output = malloc(sizeof *output);
3846 if (output == NULL)
3847 return;
3848
3849 memset(output, 0, sizeof *output);
3850 output->display = d;
3851 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003852 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003853 wl_list_insert(d->output_list.prev, &output->link);
3854
3855 wl_output_add_listener(output->output, &output_listener, output);
3856}
3857
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003858static void
3859output_destroy(struct output *output)
3860{
3861 if (output->destroy_handler)
3862 (*output->destroy_handler)(output, output->user_data);
3863
3864 wl_output_destroy(output->output);
3865 wl_list_remove(&output->link);
3866 free(output);
3867}
3868
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003869void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003870display_set_global_handler(struct display *display,
3871 display_global_handler_t handler)
3872{
3873 struct global *global;
3874
3875 display->global_handler = handler;
3876 if (!handler)
3877 return;
3878
3879 wl_list_for_each(global, &display->global_list, link)
3880 display->global_handler(display,
3881 global->name, global->interface,
3882 global->version, display->user_data);
3883}
3884
3885void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003886display_set_output_configure_handler(struct display *display,
3887 display_output_handler_t handler)
3888{
3889 struct output *output;
3890
3891 display->output_configure_handler = handler;
3892 if (!handler)
3893 return;
3894
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003895 wl_list_for_each(output, &display->output_list, link) {
3896 if (output->allocation.width == 0 &&
3897 output->allocation.height == 0)
3898 continue;
3899
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003900 (*display->output_configure_handler)(output,
3901 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003902 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003903}
3904
3905void
3906output_set_user_data(struct output *output, void *data)
3907{
3908 output->user_data = data;
3909}
3910
3911void *
3912output_get_user_data(struct output *output)
3913{
3914 return output->user_data;
3915}
3916
3917void
3918output_set_destroy_handler(struct output *output,
3919 display_output_handler_t handler)
3920{
3921 output->destroy_handler = handler;
3922 /* FIXME: implement this, once we have way to remove outputs */
3923}
3924
3925void
Scott Moreau4e072362012-09-29 02:03:11 -06003926output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003927{
Scott Moreau4e072362012-09-29 02:03:11 -06003928 struct rectangle allocation = output->allocation;
3929
3930 switch (output->transform) {
3931 case WL_OUTPUT_TRANSFORM_90:
3932 case WL_OUTPUT_TRANSFORM_270:
3933 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3934 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3935 /* Swap width and height */
3936 allocation.width = output->allocation.height;
3937 allocation.height = output->allocation.width;
3938 break;
3939 }
3940
3941 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003942}
3943
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003944struct wl_output *
3945output_get_wl_output(struct output *output)
3946{
3947 return output->output;
3948}
3949
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003950enum wl_output_transform
3951output_get_transform(struct output *output)
3952{
3953 return output->transform;
3954}
3955
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003956static void
Daniel Stone97f68542012-05-30 16:32:01 +01003957fini_xkb(struct input *input)
3958{
3959 xkb_state_unref(input->xkb.state);
3960 xkb_map_unref(input->xkb.keymap);
3961}
3962
3963static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003964display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003965{
3966 struct input *input;
3967
3968 input = malloc(sizeof *input);
3969 if (input == NULL)
3970 return;
3971
3972 memset(input, 0, sizeof *input);
3973 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003974 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003975 input->pointer_focus = NULL;
3976 input->keyboard_focus = NULL;
3977 wl_list_insert(d->input_list.prev, &input->link);
3978
Daniel Stone37816df2012-05-16 18:45:18 +01003979 wl_seat_add_listener(input->seat, &seat_listener, input);
3980 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003981
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003982 input->data_device =
3983 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003984 input->seat);
3985 wl_data_device_add_listener(input->data_device, &data_device_listener,
3986 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003987
3988 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003989
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003990 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3991 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003992 input->repeat_task.run = keyboard_repeat_func;
3993 display_watch_fd(d, input->repeat_timer_fd,
3994 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003995}
3996
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003997static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003998input_destroy(struct input *input)
3999{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004000 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004001 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004002
4003 if (input->drag_offer)
4004 data_offer_destroy(input->drag_offer);
4005
4006 if (input->selection_offer)
4007 data_offer_destroy(input->selection_offer);
4008
4009 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004010 fini_xkb(input);
4011
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004012 wl_surface_destroy(input->pointer_surface);
4013
Pekka Paalanene1207c72011-12-16 12:02:09 +02004014 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004015 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004016 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004017 free(input);
4018}
4019
4020static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004021init_workspace_manager(struct display *d, uint32_t id)
4022{
4023 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004024 wl_registry_bind(d->registry, id,
4025 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004026 if (d->workspace_manager != NULL)
4027 workspace_manager_add_listener(d->workspace_manager,
4028 &workspace_manager_listener,
4029 d);
4030}
4031
4032static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004033registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4034 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004035{
4036 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004037 struct global *global;
4038
4039 global = malloc(sizeof *global);
4040 global->name = id;
4041 global->interface = strdup(interface);
4042 global->version = version;
4043 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004044
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004045 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004046 d->compositor = wl_registry_bind(registry, id,
4047 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004048 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004049 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004050 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004051 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004052 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004053 d->shell = wl_registry_bind(registry,
4054 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004055 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004056 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004057 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4058 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004059 wl_registry_bind(registry, id,
4060 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004061 } else if (strcmp(interface, "text_cursor_position") == 0) {
4062 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004063 wl_registry_bind(registry, id,
4064 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004065 } else if (strcmp(interface, "workspace_manager") == 0) {
4066 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004067 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004068
4069 if (d->global_handler)
4070 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004071}
4072
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004073void *
4074display_bind(struct display *display, uint32_t name,
4075 const struct wl_interface *interface, uint32_t version)
4076{
4077 return wl_registry_bind(display->registry, name, interface, version);
4078}
4079
4080static const struct wl_registry_listener registry_listener = {
4081 registry_handle_global
4082};
4083
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004084#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004085static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004086init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004087{
4088 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004089 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004090
Rob Clark6396ed32012-03-11 19:48:41 -05004091#ifdef USE_CAIRO_GLESV2
4092# define GL_BIT EGL_OPENGL_ES2_BIT
4093#else
4094# define GL_BIT EGL_OPENGL_BIT
4095#endif
4096
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004097 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004098 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004099 EGL_RED_SIZE, 1,
4100 EGL_GREEN_SIZE, 1,
4101 EGL_BLUE_SIZE, 1,
4102 EGL_ALPHA_SIZE, 1,
4103 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004104 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004105 EGL_NONE
4106 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004107
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004108#ifdef USE_CAIRO_GLESV2
4109 static const EGLint context_attribs[] = {
4110 EGL_CONTEXT_CLIENT_VERSION, 2,
4111 EGL_NONE
4112 };
4113 EGLint api = EGL_OPENGL_ES_API;
4114#else
4115 EGLint *context_attribs = NULL;
4116 EGLint api = EGL_OPENGL_API;
4117#endif
4118
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004119 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004120 if (!eglInitialize(d->dpy, &major, &minor)) {
4121 fprintf(stderr, "failed to initialize display\n");
4122 return -1;
4123 }
4124
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004125 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004126 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
4127 return -1;
4128 }
4129
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004130 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4131 &d->argb_config, 1, &n) || n != 1) {
4132 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004133 return -1;
4134 }
4135
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004136 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004137 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004138 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004139 fprintf(stderr, "failed to create context\n");
4140 return -1;
4141 }
4142
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004143 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004144 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004145 return -1;
4146 }
4147
Benjamin Franzke0c991632011-09-27 21:57:31 +02004148 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4149 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4150 fprintf(stderr, "failed to get cairo egl argb device\n");
4151 return -1;
4152 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004153
4154 return 0;
4155}
4156
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004157static void
4158fini_egl(struct display *display)
4159{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004160 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004161
4162 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4163 EGL_NO_CONTEXT);
4164
4165 eglTerminate(display->dpy);
4166 eglReleaseThread();
4167}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004168#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004169
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004170static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004171init_dummy_surface(struct display *display)
4172{
4173 int len;
4174 void *data;
4175
4176 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4177 data = malloc(len);
4178 display->dummy_surface =
4179 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4180 1, 1, len);
4181 display->dummy_surface_data = data;
4182}
4183
4184static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004185handle_display_data(struct task *task, uint32_t events)
4186{
4187 struct display *display =
4188 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004189 struct epoll_event ep;
4190 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004191
4192 display->display_fd_events = events;
4193
4194 if (events & EPOLLERR || events & EPOLLHUP) {
4195 display_exit(display);
4196 return;
4197 }
4198
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004199 if (events & EPOLLIN) {
4200 ret = wl_display_dispatch(display->display);
4201 if (ret == -1) {
4202 display_exit(display);
4203 return;
4204 }
4205 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004206
4207 if (events & EPOLLOUT) {
4208 ret = wl_display_flush(display->display);
4209 if (ret == 0) {
4210 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4211 ep.data.ptr = &display->display_task;
4212 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4213 display->display_fd, &ep);
4214 } else if (ret == -1 && errno != EAGAIN) {
4215 display_exit(display);
4216 return;
4217 }
4218 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004219}
4220
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004221struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004222display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004223{
4224 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004225
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004226 d = malloc(sizeof *d);
4227 if (d == NULL)
4228 return NULL;
4229
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004230 memset(d, 0, sizeof *d);
4231
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004232 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004233 if (d->display == NULL) {
4234 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004235 return NULL;
4236 }
4237
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004238 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004239 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004240 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004241 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4242 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004243
4244 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004245 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004246 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004247 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004248
Daniel Stone97f68542012-05-30 16:32:01 +01004249 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004250 if (d->xkb_context == NULL) {
4251 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004252 return NULL;
4253 }
4254
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004255 d->workspace = 0;
4256 d->workspace_count = 1;
4257
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004258 d->registry = wl_display_get_registry(d->display);
4259 wl_registry_add_listener(d->registry, &registry_listener, d);
4260 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004261#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004262 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004263 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004264#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004265
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004266 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004267
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004268 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004269
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004270 wl_list_init(&d->window_list);
4271
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004272 init_dummy_surface(d);
4273
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004274 return d;
4275}
4276
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004277static void
4278display_destroy_outputs(struct display *display)
4279{
4280 struct output *tmp;
4281 struct output *output;
4282
4283 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4284 output_destroy(output);
4285}
4286
Pekka Paalanene1207c72011-12-16 12:02:09 +02004287static void
4288display_destroy_inputs(struct display *display)
4289{
4290 struct input *tmp;
4291 struct input *input;
4292
4293 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4294 input_destroy(input);
4295}
4296
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004297void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004298display_destroy(struct display *display)
4299{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004300 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004301 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4302 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004303
4304 if (!wl_list_empty(&display->deferred_list))
4305 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4306
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004307 cairo_surface_destroy(display->dummy_surface);
4308 free(display->dummy_surface_data);
4309
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004310 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004311 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004312
Daniel Stone97f68542012-05-30 16:32:01 +01004313 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004314
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004315 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004316 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004317
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004318#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004319 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004320#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004321
Pekka Paalanenc2052982011-12-16 11:41:32 +02004322 if (display->shell)
4323 wl_shell_destroy(display->shell);
4324
4325 if (display->shm)
4326 wl_shm_destroy(display->shm);
4327
4328 if (display->data_device_manager)
4329 wl_data_device_manager_destroy(display->data_device_manager);
4330
4331 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004332 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004333
4334 close(display->epoll_fd);
4335
U. Artie Eoff44874d92012-10-02 21:12:35 -07004336 if (!(display->display_fd_events & EPOLLERR) &&
4337 !(display->display_fd_events & EPOLLHUP))
4338 wl_display_flush(display->display);
4339
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004340 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004341 free(display);
4342}
4343
4344void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004345display_set_user_data(struct display *display, void *data)
4346{
4347 display->user_data = data;
4348}
4349
4350void *
4351display_get_user_data(struct display *display)
4352{
4353 return display->user_data;
4354}
4355
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004356struct wl_display *
4357display_get_display(struct display *display)
4358{
4359 return display->display;
4360}
4361
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004362struct output *
4363display_get_output(struct display *display)
4364{
4365 return container_of(display->output_list.next, struct output, link);
4366}
4367
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004368struct wl_compositor *
4369display_get_compositor(struct display *display)
4370{
4371 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004372}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004373
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004374uint32_t
4375display_get_serial(struct display *display)
4376{
4377 return display->serial;
4378}
4379
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004380EGLDisplay
4381display_get_egl_display(struct display *d)
4382{
4383 return d->dpy;
4384}
4385
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004386struct wl_data_source *
4387display_create_data_source(struct display *display)
4388{
4389 return wl_data_device_manager_create_data_source(display->data_device_manager);
4390}
4391
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004392EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004393display_get_argb_egl_config(struct display *d)
4394{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004395 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004396}
4397
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004398struct wl_shell *
4399display_get_shell(struct display *display)
4400{
4401 return display->shell;
4402}
4403
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004404int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004405display_acquire_window_surface(struct display *display,
4406 struct window *window,
4407 EGLContext ctx)
4408{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004409 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004410 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004411
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004412 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004413}
4414
4415void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004416display_release_window_surface(struct display *display,
4417 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004418{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004419 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004420 return;
4421
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004422 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004423}
4424
4425void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004426display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004427{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004428 wl_list_insert(&display->deferred_list, &task->link);
4429}
4430
4431void
4432display_watch_fd(struct display *display,
4433 int fd, uint32_t events, struct task *task)
4434{
4435 struct epoll_event ep;
4436
4437 ep.events = events;
4438 ep.data.ptr = task;
4439 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4440}
4441
4442void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004443display_unwatch_fd(struct display *display, int fd)
4444{
4445 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4446}
4447
4448void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004449display_run(struct display *display)
4450{
4451 struct task *task;
4452 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004453 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004454
Pekka Paalanen826d7952011-12-15 10:14:07 +02004455 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004456 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004457 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004458 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004459 struct task, link);
4460 wl_list_remove(&task->link);
4461 task->run(task, 0);
4462 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004463
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004464 wl_display_dispatch_pending(display->display);
4465
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004466 if (!display->running)
4467 break;
4468
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004469 ret = wl_display_flush(display->display);
4470 if (ret < 0 && errno == EAGAIN) {
4471 ep[0].events =
4472 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4473 ep[0].data.ptr = &display->display_task;
4474
4475 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4476 display->display_fd, &ep[0]);
4477 } else if (ret < 0) {
4478 break;
4479 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004480
4481 count = epoll_wait(display->epoll_fd,
4482 ep, ARRAY_LENGTH(ep), -1);
4483 for (i = 0; i < count; i++) {
4484 task = ep[i].data.ptr;
4485 task->run(task, ep[i].events);
4486 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004487 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004488}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004489
4490void
4491display_exit(struct display *display)
4492{
4493 display->running = 0;
4494}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004495
4496void
4497keysym_modifiers_add(struct wl_array *modifiers_map,
4498 const char *name)
4499{
4500 size_t len = strlen(name) + 1;
4501 char *p;
4502
4503 p = wl_array_add(modifiers_map, len);
4504
4505 if (p == NULL)
4506 return;
4507
4508 strncpy(p, name, len);
4509}
4510
4511static xkb_mod_index_t
4512keysym_modifiers_get_index(struct wl_array *modifiers_map,
4513 const char *name)
4514{
4515 xkb_mod_index_t index = 0;
4516 char *p = modifiers_map->data;
4517
4518 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4519 if (strcmp(p, name) == 0)
4520 return index;
4521
4522 index++;
4523 p += strlen(p) + 1;
4524 }
4525
4526 return XKB_MOD_INVALID;
4527}
4528
4529xkb_mod_mask_t
4530keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4531 const char *name)
4532{
4533 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4534
4535 if (index == XKB_MOD_INVALID)
4536 return XKB_MOD_INVALID;
4537
4538 return 1 << index;
4539}