blob: 20d09d5ab3e24a984dcced80747f93b629de8652 [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;
836}
837
838static const struct wl_buffer_listener shm_surface_buffer_listener = {
839 shm_surface_buffer_release
840};
841
Pekka Paalanen99436862012-11-19 17:15:59 +0200842static cairo_surface_t *
843shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200844 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200845{
Pekka Paalanenec076692012-11-30 13:37:27 +0200846 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200847 struct shm_surface *surface = to_shm_surface(base);
848 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200849 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200850
851 surface->dx = dx;
852 surface->dy = dy;
853
Pekka Paalanena4eda732012-11-19 17:16:02 +0200854 /* pick a free buffer from the two */
855 if (!surface->leaf[0].busy)
856 leaf = &surface->leaf[0];
857 else if (!surface->leaf[1].busy)
858 leaf = &surface->leaf[1];
859 else {
860 fprintf(stderr, "%s: both buffers are held by the server.\n",
861 __func__);
862 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200863 }
864
Pekka Paalanena4eda732012-11-19 17:16:02 +0200865 if (!resize_hint && leaf->resize_pool) {
866 cairo_surface_destroy(leaf->cairo_surface);
867 leaf->cairo_surface = NULL;
868 shm_pool_destroy(leaf->resize_pool);
869 leaf->resize_pool = NULL;
870 }
871
872 if (leaf->cairo_surface &&
873 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
874 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200875 goto out;
876
Pekka Paalanena4eda732012-11-19 17:16:02 +0200877 if (leaf->cairo_surface)
878 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200879
Pekka Paalanena4eda732012-11-19 17:16:02 +0200880 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200881 /* Create a big pool to allocate from, while continuously
882 * resizing. Mmapping a new pool in the server
883 * is relatively expensive, so reusing a pool performs
884 * better, but may temporarily reserve unneeded memory.
885 */
886 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200887 leaf->resize_pool = shm_pool_create(surface->display,
888 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200889 }
890
Pekka Paalanena4eda732012-11-19 17:16:02 +0200891 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200892 display_create_shm_surface(surface->display, &rect,
893 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200894 leaf->resize_pool,
895 &leaf->data);
896 wl_buffer_add_listener(leaf->data->buffer,
897 &shm_surface_buffer_listener, leaf);
Pekka Paalanen99436862012-11-19 17:15:59 +0200898
899out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200900 surface->current = leaf;
901
902 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200903}
904
905static void
906shm_surface_swap(struct toysurface *base,
907 struct rectangle *server_allocation)
908{
909 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200910 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200911
912 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200913 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200914 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200915 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200916
Pekka Paalanena4eda732012-11-19 17:16:02 +0200917 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200918 surface->dx, surface->dy);
919 wl_surface_damage(surface->surface, 0, 0,
920 server_allocation->width, server_allocation->height);
921 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200922
923 leaf->busy = 1;
924 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200925}
926
927static int
928shm_surface_acquire(struct toysurface *base, EGLContext ctx)
929{
930 return -1;
931}
932
933static void
934shm_surface_release(struct toysurface *base)
935{
936}
937
938static void
939shm_surface_destroy(struct toysurface *base)
940{
941 struct shm_surface *surface = to_shm_surface(base);
942
Pekka Paalanena4eda732012-11-19 17:16:02 +0200943 shm_surface_leaf_release(&surface->leaf[0]);
944 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200945
946 free(surface);
947}
948
949static struct toysurface *
950shm_surface_create(struct display *display, struct wl_surface *wl_surface,
951 uint32_t flags, struct rectangle *rectangle)
952{
953 struct shm_surface *surface;
954
955 surface = calloc(1, sizeof *surface);
956 if (!surface)
957 return NULL;
958
959 surface->base.prepare = shm_surface_prepare;
960 surface->base.swap = shm_surface_swap;
961 surface->base.acquire = shm_surface_acquire;
962 surface->base.release = shm_surface_release;
963 surface->base.destroy = shm_surface_destroy;
964
965 surface->display = display;
966 surface->surface = wl_surface;
967 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200968
969 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400970}
971
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200972/*
973 * The following correspondences between file names and cursors was copied
974 * from: https://bugs.kde.org/attachment.cgi?id=67313
975 */
976
977static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300978 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200979 "sw-resize"
980};
981
982static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300983 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200984 "se-resize"
985};
986
987static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300988 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200989 "s-resize"
990};
991
992static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300993 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200994 "closedhand",
995 "208530c400c041818281048008011002"
996};
997
998static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300999 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001000 "default",
1001 "top_left_arrow",
1002 "left-arrow"
1003};
1004
1005static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001006 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001007 "w-resize"
1008};
1009
1010static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001011 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001012 "e-resize"
1013};
1014
1015static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001016 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001017 "nw-resize"
1018};
1019
1020static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001021 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001022 "ne-resize"
1023};
1024
1025static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001026 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001027 "n-resize"
1028};
1029
1030static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001031 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001032 "ibeam",
1033 "text"
1034};
1035
1036static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001037 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001038 "pointer",
1039 "pointing_hand",
1040 "e29285e634086352946a0e7090d73106"
1041};
1042
1043static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001044 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001045 "wait",
1046 "0426c94ea35c87780ff01dc239897213"
1047};
1048
1049struct cursor_alternatives {
1050 const char **names;
1051 size_t count;
1052};
1053
1054static const struct cursor_alternatives cursors[] = {
1055 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1056 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1057 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1058 {grabbings, ARRAY_LENGTH(grabbings)},
1059 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1060 {left_sides, ARRAY_LENGTH(left_sides)},
1061 {right_sides, ARRAY_LENGTH(right_sides)},
1062 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1063 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1064 {top_sides, ARRAY_LENGTH(top_sides)},
1065 {xterms, ARRAY_LENGTH(xterms)},
1066 {hand1s, ARRAY_LENGTH(hand1s)},
1067 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001068};
1069
1070static void
1071create_cursors(struct display *display)
1072{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001073 char *config_file;
1074 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001075 unsigned int i, j;
1076 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001077 struct config_key shell_keys[] = {
1078 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1079 };
1080 struct config_section cs[] = {
1081 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1082 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001083
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001084 config_file = config_file_path("weston.ini");
1085 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1086 free(config_file);
1087
1088 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001089 display->cursors =
1090 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1091
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001092 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001093 cursor = NULL;
1094 for (j = 0; !cursor && j < cursors[i].count; ++j)
1095 cursor = wl_cursor_theme_get_cursor(
1096 display->cursor_theme, cursors[i].names[j]);
1097
1098 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001099 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001100 cursors[i].names[0]);
1101
1102 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001103 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001104}
1105
1106static void
1107destroy_cursors(struct display *display)
1108{
1109 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001110 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001111}
1112
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001113struct wl_cursor_image *
1114display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001115{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001116 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001117
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001118 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001119}
1120
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001121static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001122window_get_resize_dx_dy(struct window *window, int *x, int *y)
1123{
1124 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1125 *x = window->server_allocation.width - window->allocation.width;
1126 else
1127 *x = 0;
1128
1129 if (window->resize_edges & WINDOW_RESIZING_TOP)
1130 *y = window->server_allocation.height -
1131 window->allocation.height;
1132 else
1133 *y = 0;
1134
1135 window->resize_edges = 0;
1136}
1137
1138static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001139window_attach_surface(struct window *window)
1140{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001141 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001142
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001143 if (window->type == TYPE_NONE) {
1144 window->type = TYPE_TOPLEVEL;
1145 if (display->shell)
1146 wl_shell_surface_set_toplevel(window->shell_surface);
1147 }
1148
Pekka Paalanen512dde82012-10-10 12:49:27 +03001149 if (window->opaque_region) {
1150 wl_surface_set_opaque_region(window->surface,
1151 window->opaque_region);
1152 wl_region_destroy(window->opaque_region);
1153 window->opaque_region = NULL;
1154 }
1155
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001156 if (window->input_region) {
1157 wl_surface_set_input_region(window->surface,
1158 window->input_region);
1159 wl_region_destroy(window->input_region);
1160 window->input_region = NULL;
1161 }
1162
Pekka Paalanen99436862012-11-19 17:15:59 +02001163 window->toysurface->swap(window->toysurface,
1164 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001165}
1166
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001167int
1168window_has_focus(struct window *window)
1169{
1170 return window->focus_count > 0;
1171}
1172
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001173static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001174window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001175{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001176 if (!window->cairo_surface)
1177 return;
1178
1179 window_attach_surface(window);
1180 cairo_surface_destroy(window->cairo_surface);
1181 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001182}
1183
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001184struct display *
1185window_get_display(struct window *window)
1186{
1187 return window->display;
1188}
1189
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001190static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001191window_create_surface(struct window *window)
1192{
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001193 struct rectangle allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001194 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001195 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001196
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001197 if (!window->transparent)
1198 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001199
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001200 switch (window->buffer_transform) {
1201 case WL_OUTPUT_TRANSFORM_90:
1202 case WL_OUTPUT_TRANSFORM_270:
1203 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1204 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1205 allocation.width = window->allocation.height;
1206 allocation.height = window->allocation.width;
1207 break;
1208 default:
1209 break;
1210 }
1211
Pekka Paalanenb3627362012-11-19 17:16:00 +02001212 if (!window->toysurface &&
1213 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1214 window->display->dpy) {
1215 window->toysurface =
1216 egl_window_surface_create(window->display,
1217 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001218 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001219 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001220
Pekka Paalanenb3627362012-11-19 17:16:00 +02001221 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001222 window->toysurface = shm_surface_create(window->display,
1223 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001224 &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001225
Pekka Paalanenec076692012-11-30 13:37:27 +02001226 if (window->resizing)
1227 flags = SURFACE_HINT_RESIZE;
1228 else
1229 flags = 0;
1230
Pekka Paalanen99436862012-11-19 17:15:59 +02001231 window_get_resize_dx_dy(window, &dx, &dy);
1232 window->cairo_surface =
1233 window->toysurface->prepare(window->toysurface, dx, dy,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001234 allocation.width,
1235 allocation.height,
Pekka Paalanenec076692012-11-30 13:37:27 +02001236 flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001237}
1238
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001239int
1240window_get_buffer_transform(struct window *window)
1241{
1242 return window->buffer_transform;
1243}
1244
1245void
1246window_set_buffer_transform(struct window *window,
1247 enum wl_output_transform transform)
1248{
1249 window->buffer_transform = transform;
1250 wl_surface_set_buffer_transform(window->surface, transform);
1251}
1252
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001253static void frame_destroy(struct frame *frame);
1254
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001255void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001256window_destroy(struct window *window)
1257{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001258 struct display *display = window->display;
1259 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001260 struct window_output *window_output;
1261 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001262
1263 if (window->redraw_scheduled)
1264 wl_list_remove(&window->redraw_task.link);
1265
1266 wl_list_for_each(input, &display->input_list, link) {
1267 if (input->pointer_focus == window)
1268 input->pointer_focus = NULL;
1269 if (input->keyboard_focus == window)
1270 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001271 if (input->focus_widget &&
1272 input->focus_widget->window == window)
1273 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001274 }
1275
Rob Bradford7507b572012-05-15 17:55:34 +01001276 wl_list_for_each_safe(window_output, window_output_tmp,
1277 &window->window_output_list, link) {
1278 free (window_output);
1279 }
1280
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001281 if (window->input_region)
1282 wl_region_destroy(window->input_region);
1283 if (window->opaque_region)
1284 wl_region_destroy(window->opaque_region);
1285
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001286 if (window->frame)
1287 frame_destroy(window->frame);
1288
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001289 if (window->shell_surface)
1290 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001291 wl_surface_destroy(window->surface);
1292 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001293
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001294 if (window->toysurface)
1295 window->toysurface->destroy(window->toysurface);
1296
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001297 if (window->frame_cb)
1298 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001299 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001300 free(window);
1301}
1302
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001303static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001304widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001305{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001306 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001307
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001308 wl_list_for_each(child, &widget->child_list, link) {
1309 target = widget_find_widget(child, x, y);
1310 if (target)
1311 return target;
1312 }
1313
1314 if (widget->allocation.x <= x &&
1315 x < widget->allocation.x + widget->allocation.width &&
1316 widget->allocation.y <= y &&
1317 y < widget->allocation.y + widget->allocation.height) {
1318 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001319 }
1320
1321 return NULL;
1322}
1323
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001324static struct widget *
1325widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001326{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001327 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001328
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001329 widget = malloc(sizeof *widget);
1330 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001331 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001332 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001333 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001334 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001335 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001336 widget->tooltip = NULL;
1337 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001338 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001339
1340 return widget;
1341}
1342
1343struct widget *
1344window_add_widget(struct window *window, void *data)
1345{
1346 window->widget = widget_create(window, data);
1347 wl_list_init(&window->widget->link);
1348
1349 return window->widget;
1350}
1351
1352struct widget *
1353widget_add_widget(struct widget *parent, void *data)
1354{
1355 struct widget *widget;
1356
1357 widget = widget_create(parent->window, data);
1358 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001359
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001360 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001361}
1362
1363void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001364widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001365{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001366 struct display *display = widget->window->display;
1367 struct input *input;
1368
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001369 if (widget->tooltip) {
1370 free(widget->tooltip);
1371 widget->tooltip = NULL;
1372 }
1373
Pekka Paalanene156fb62012-01-19 13:51:38 +02001374 wl_list_for_each(input, &display->input_list, link) {
1375 if (input->focus_widget == widget)
1376 input->focus_widget = NULL;
1377 }
1378
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001379 wl_list_remove(&widget->link);
1380 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001381}
1382
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001383void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001384widget_set_default_cursor(struct widget *widget, int cursor)
1385{
1386 widget->default_cursor = cursor;
1387}
1388
1389void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001390widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001391{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001392 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001393}
1394
1395void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001396widget_set_size(struct widget *widget, int32_t width, int32_t height)
1397{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001398 widget->allocation.width = width;
1399 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001400}
1401
1402void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001403widget_set_allocation(struct widget *widget,
1404 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001405{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001406 widget->allocation.x = x;
1407 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001408 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001409}
1410
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001411void
1412widget_set_transparent(struct widget *widget, int transparent)
1413{
1414 widget->opaque = !transparent;
1415}
1416
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001417void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001418widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001419{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001420 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001421}
1422
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001423void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001424widget_set_resize_handler(struct widget *widget,
1425 widget_resize_handler_t handler)
1426{
1427 widget->resize_handler = handler;
1428}
1429
1430void
1431widget_set_redraw_handler(struct widget *widget,
1432 widget_redraw_handler_t handler)
1433{
1434 widget->redraw_handler = handler;
1435}
1436
1437void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001438widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001439{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001440 widget->enter_handler = handler;
1441}
1442
1443void
1444widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1445{
1446 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001447}
1448
1449void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001450widget_set_motion_handler(struct widget *widget,
1451 widget_motion_handler_t handler)
1452{
1453 widget->motion_handler = handler;
1454}
1455
1456void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001457widget_set_button_handler(struct widget *widget,
1458 widget_button_handler_t handler)
1459{
1460 widget->button_handler = handler;
1461}
1462
1463void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001464widget_set_axis_handler(struct widget *widget,
1465 widget_axis_handler_t handler)
1466{
1467 widget->axis_handler = handler;
1468}
1469
1470void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001471widget_schedule_redraw(struct widget *widget)
1472{
1473 window_schedule_redraw(widget->window);
1474}
1475
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001476cairo_surface_t *
1477window_get_surface(struct window *window)
1478{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001479 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001480}
1481
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001482struct wl_surface *
1483window_get_wl_surface(struct window *window)
1484{
1485 return window->surface;
1486}
1487
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001488struct wl_shell_surface *
1489window_get_wl_shell_surface(struct window *window)
1490{
1491 return window->shell_surface;
1492}
1493
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001494static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001495tooltip_redraw_handler(struct widget *widget, void *data)
1496{
1497 cairo_t *cr;
1498 const int32_t r = 3;
1499 struct tooltip *tooltip = data;
1500 int32_t width, height;
1501 struct window *window = widget->window;
1502
1503 cr = cairo_create(window->cairo_surface);
1504 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1505 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1506 cairo_paint(cr);
1507
1508 width = window->allocation.width;
1509 height = window->allocation.height;
1510 rounded_rect(cr, 0, 0, width, height, r);
1511
1512 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1513 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1514 cairo_fill(cr);
1515
1516 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1517 cairo_move_to(cr, 10, 16);
1518 cairo_show_text(cr, tooltip->entry);
1519 cairo_destroy(cr);
1520}
1521
1522static cairo_text_extents_t
1523get_text_extents(struct tooltip *tooltip)
1524{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001525 cairo_t *cr;
1526 cairo_text_extents_t extents;
1527
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001528 /* Use the dummy_surface because tooltip's surface was not
1529 * created yet, and parent does not have a valid surface
1530 * outside repaint, either.
1531 */
1532 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001533 cairo_text_extents(cr, tooltip->entry, &extents);
1534 cairo_destroy(cr);
1535
1536 return extents;
1537}
1538
1539static int
1540window_create_tooltip(struct tooltip *tooltip)
1541{
1542 struct widget *parent = tooltip->parent;
1543 struct display *display = parent->window->display;
1544 struct window *window;
1545 const int offset_y = 27;
1546 const int margin = 3;
1547 cairo_text_extents_t extents;
1548
1549 if (tooltip->widget)
1550 return 0;
1551
1552 window = window_create_transient(display, parent->window, tooltip->x,
1553 tooltip->y + offset_y,
1554 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1555 if (!window)
1556 return -1;
1557
1558 tooltip->window = window;
1559 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1560
1561 extents = get_text_extents(tooltip);
1562 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1563 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1564
1565 return 0;
1566}
1567
1568void
1569widget_destroy_tooltip(struct widget *parent)
1570{
1571 struct tooltip *tooltip = parent->tooltip;
1572
1573 parent->tooltip_count = 0;
1574 if (!tooltip)
1575 return;
1576
1577 if (tooltip->widget) {
1578 widget_destroy(tooltip->widget);
1579 window_destroy(tooltip->window);
1580 tooltip->widget = NULL;
1581 tooltip->window = NULL;
1582 }
1583
1584 close(tooltip->tooltip_fd);
1585 free(tooltip->entry);
1586 free(tooltip);
1587 parent->tooltip = NULL;
1588}
1589
1590static void
1591tooltip_func(struct task *task, uint32_t events)
1592{
1593 struct tooltip *tooltip =
1594 container_of(task, struct tooltip, tooltip_task);
1595 uint64_t exp;
1596
Martin Olsson8df662a2012-07-08 03:03:47 +02001597 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1598 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001599 window_create_tooltip(tooltip);
1600}
1601
1602#define TOOLTIP_TIMEOUT 500
1603static int
1604tooltip_timer_reset(struct tooltip *tooltip)
1605{
1606 struct itimerspec its;
1607
1608 its.it_interval.tv_sec = 0;
1609 its.it_interval.tv_nsec = 0;
1610 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1611 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1612 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1613 fprintf(stderr, "could not set timerfd\n: %m");
1614 return -1;
1615 }
1616
1617 return 0;
1618}
1619
1620int
1621widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1622{
1623 struct tooltip *tooltip = parent->tooltip;
1624
1625 parent->tooltip_count++;
1626 if (tooltip) {
1627 tooltip->x = x;
1628 tooltip->y = y;
1629 tooltip_timer_reset(tooltip);
1630 return 0;
1631 }
1632
1633 /* the handler might be triggered too fast via input device motion, so
1634 * we need this check here to make sure tooltip is fully initialized */
1635 if (parent->tooltip_count > 1)
1636 return 0;
1637
1638 tooltip = malloc(sizeof *tooltip);
1639 if (!tooltip)
1640 return -1;
1641
1642 parent->tooltip = tooltip;
1643 tooltip->parent = parent;
1644 tooltip->widget = NULL;
1645 tooltip->window = NULL;
1646 tooltip->x = x;
1647 tooltip->y = y;
1648 tooltip->entry = strdup(entry);
1649 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1650 if (tooltip->tooltip_fd < 0) {
1651 fprintf(stderr, "could not create timerfd\n: %m");
1652 return -1;
1653 }
1654
1655 tooltip->tooltip_task.run = tooltip_func;
1656 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1657 EPOLLIN, &tooltip->tooltip_task);
1658 tooltip_timer_reset(tooltip);
1659
1660 return 0;
1661}
1662
1663static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001664workspace_manager_state(void *data,
1665 struct workspace_manager *workspace_manager,
1666 uint32_t current,
1667 uint32_t count)
1668{
1669 struct display *display = data;
1670
1671 display->workspace = current;
1672 display->workspace_count = count;
1673}
1674
1675static const struct workspace_manager_listener workspace_manager_listener = {
1676 workspace_manager_state
1677};
1678
1679static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001680frame_resize_handler(struct widget *widget,
1681 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001682{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001683 struct frame *frame = data;
1684 struct widget *child = frame->child;
1685 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001686 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001687 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001688 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001689 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001690 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001691 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001692
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001693 switch (widget->window->type) {
1694 case TYPE_FULLSCREEN:
1695 decoration_width = 0;
1696 decoration_height = 0;
1697
1698 allocation.x = 0;
1699 allocation.y = 0;
1700 allocation.width = width;
1701 allocation.height = height;
1702 opaque_margin = 0;
1703
1704 wl_list_for_each(button, &frame->buttons_list, link)
1705 button->widget->opaque = 1;
1706 break;
1707 case TYPE_MAXIMIZED:
1708 decoration_width = t->width * 2;
1709 decoration_height = t->width + t->titlebar_height;
1710
1711 allocation.x = t->width;
1712 allocation.y = t->titlebar_height;
1713 allocation.width = width - decoration_width;
1714 allocation.height = height - decoration_height;
1715
1716 opaque_margin = 0;
1717
1718 wl_list_for_each(button, &frame->buttons_list, link)
1719 button->widget->opaque = 0;
1720 break;
1721 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001722 decoration_width = (t->width + t->margin) * 2;
1723 decoration_height = t->width +
1724 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001725
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001726 allocation.x = t->width + t->margin;
1727 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001728 allocation.width = width - decoration_width;
1729 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001730
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001731 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001732
1733 wl_list_for_each(button, &frame->buttons_list, link)
1734 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001735 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001736 }
1737
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001738 widget_set_allocation(child, allocation.x, allocation.y,
1739 allocation.width, allocation.height);
1740
1741 if (child->resize_handler)
1742 child->resize_handler(child,
1743 allocation.width,
1744 allocation.height,
1745 child->user_data);
1746
Scott Moreauf7e498c2012-05-14 11:39:29 -06001747 width = child->allocation.width + decoration_width;
1748 height = child->allocation.height + decoration_height;
1749
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001750 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1751
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001752 widget->window->input_region =
1753 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001754 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001755 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001756 shadow_margin, shadow_margin,
1757 width - 2 * shadow_margin,
1758 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001759 } else {
1760 wl_region_add(widget->window->input_region,
1761 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001762 }
1763
Scott Moreauf7e498c2012-05-14 11:39:29 -06001764 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001765
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001766 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001767 wl_region_add(widget->window->opaque_region,
1768 opaque_margin, opaque_margin,
1769 widget->allocation.width - 2 * opaque_margin,
1770 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001771
1772 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001773 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1774 x_l = t->width + shadow_margin;
1775 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001776 wl_list_for_each(button, &frame->buttons_list, link) {
1777 const int button_padding = 4;
1778 w = cairo_image_surface_get_width(button->icon);
1779 h = cairo_image_surface_get_height(button->icon);
1780
1781 if (button->decoration == FRAME_BUTTON_FANCY)
1782 w += 10;
1783
1784 if (button->align == FRAME_BUTTON_LEFT) {
1785 widget_set_allocation(button->widget,
1786 x_l, y , w + 1, h + 1);
1787 x_l += w;
1788 x_l += button_padding;
1789 } else {
1790 x_r -= w;
1791 widget_set_allocation(button->widget,
1792 x_r, y , w + 1, h + 1);
1793 x_r -= button_padding;
1794 }
1795 }
1796}
1797
1798static int
1799frame_button_enter_handler(struct widget *widget,
1800 struct input *input, float x, float y, void *data)
1801{
1802 struct frame_button *frame_button = data;
1803
1804 widget_schedule_redraw(frame_button->widget);
1805 frame_button->state = FRAME_BUTTON_OVER;
1806
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001807 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001808}
1809
1810static void
1811frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1812{
1813 struct frame_button *frame_button = data;
1814
1815 widget_schedule_redraw(frame_button->widget);
1816 frame_button->state = FRAME_BUTTON_DEFAULT;
1817}
1818
1819static void
1820frame_button_button_handler(struct widget *widget,
1821 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001822 uint32_t button,
1823 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001824{
1825 struct frame_button *frame_button = data;
1826 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001827 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001828
1829 if (button != BTN_LEFT)
1830 return;
1831
1832 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001833 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001834 frame_button->state = FRAME_BUTTON_ACTIVE;
1835 widget_schedule_redraw(frame_button->widget);
1836
1837 if (frame_button->type == FRAME_BUTTON_ICON)
1838 window_show_frame_menu(window, input, time);
1839 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001840 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001841 frame_button->state = FRAME_BUTTON_DEFAULT;
1842 widget_schedule_redraw(frame_button->widget);
1843 break;
1844 }
1845
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001846 if (!was_pressed)
1847 return;
1848
Martin Minarik1998b152012-05-10 02:04:35 +02001849 switch (frame_button->type) {
1850 case FRAME_BUTTON_CLOSE:
1851 if (window->close_handler)
1852 window->close_handler(window->parent,
1853 window->user_data);
1854 else
1855 display_exit(window->display);
1856 break;
1857 case FRAME_BUTTON_MINIMIZE:
1858 fprintf(stderr,"Minimize stub\n");
1859 break;
1860 case FRAME_BUTTON_MAXIMIZE:
1861 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1862 break;
1863 default:
1864 /* Unknown operation */
1865 break;
1866 }
1867}
1868
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001869static int
1870frame_button_motion_handler(struct widget *widget,
1871 struct input *input, uint32_t time,
1872 float x, float y, void *data)
1873{
1874 struct frame_button *frame_button = data;
1875 enum frame_button_pointer previous_button_state = frame_button->state;
1876
1877 /* only track state for a pressed button */
1878 if (input->grab != widget)
1879 return CURSOR_LEFT_PTR;
1880
1881 if (x > widget->allocation.x &&
1882 x < (widget->allocation.x + widget->allocation.width) &&
1883 y > widget->allocation.y &&
1884 y < (widget->allocation.y + widget->allocation.height)) {
1885 frame_button->state = FRAME_BUTTON_ACTIVE;
1886 } else {
1887 frame_button->state = FRAME_BUTTON_DEFAULT;
1888 }
1889
1890 if (frame_button->state != previous_button_state)
1891 widget_schedule_redraw(frame_button->widget);
1892
1893 return CURSOR_LEFT_PTR;
1894}
1895
Martin Minarik1998b152012-05-10 02:04:35 +02001896static void
1897frame_button_redraw_handler(struct widget *widget, void *data)
1898{
1899 struct frame_button *frame_button = data;
1900 cairo_t *cr;
1901 int width, height, x, y;
1902 struct window *window = widget->window;
1903
1904 x = widget->allocation.x;
1905 y = widget->allocation.y;
1906 width = widget->allocation.width;
1907 height = widget->allocation.height;
1908
1909 if (!width)
1910 return;
1911 if (!height)
1912 return;
1913 if (widget->opaque)
1914 return;
1915
1916 cr = cairo_create(window->cairo_surface);
1917
1918 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1919 cairo_set_line_width(cr, 1);
1920
1921 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1922 cairo_rectangle (cr, x, y, 25, 16);
1923
1924 cairo_stroke_preserve(cr);
1925
1926 switch (frame_button->state) {
1927 case FRAME_BUTTON_DEFAULT:
1928 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1929 break;
1930 case FRAME_BUTTON_OVER:
1931 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1932 break;
1933 case FRAME_BUTTON_ACTIVE:
1934 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1935 break;
1936 }
1937
1938 cairo_fill (cr);
1939
1940 x += 4;
1941 }
1942
1943 cairo_set_source_surface(cr, frame_button->icon, x, y);
1944 cairo_paint(cr);
1945
1946 cairo_destroy(cr);
1947}
1948
1949static struct widget *
1950frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1951 enum frame_button_align align, enum frame_button_decoration style)
1952{
1953 struct frame_button *frame_button;
1954 const char *icon = data;
1955
1956 frame_button = malloc (sizeof *frame_button);
1957 memset(frame_button, 0, sizeof *frame_button);
1958
1959 frame_button->icon = cairo_image_surface_create_from_png(icon);
1960 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1961 frame_button->frame = frame;
1962 frame_button->type = type;
1963 frame_button->align = align;
1964 frame_button->decoration = style;
1965
1966 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1967
1968 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1969 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1970 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1971 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001972 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001973 return frame_button->widget;
1974}
1975
1976static void
1977frame_button_destroy(struct frame_button *frame_button)
1978{
1979 widget_destroy(frame_button->widget);
1980 wl_list_remove(&frame_button->link);
1981 cairo_surface_destroy(frame_button->icon);
1982 free(frame_button);
1983
1984 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001985}
1986
1987static void
1988frame_redraw_handler(struct widget *widget, void *data)
1989{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001990 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001991 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001992 struct theme *t = window->display->theme;
1993 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001994
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001995 if (window->type == TYPE_FULLSCREEN)
1996 return;
1997
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001998 cr = cairo_create(window->cairo_surface);
1999
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002000 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002001 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002002 if (window->type == TYPE_MAXIMIZED)
2003 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002004 theme_render_frame(t, cr, widget->allocation.width,
2005 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002006
2007 cairo_destroy(cr);
2008}
2009
2010static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002011frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002012{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002013 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002014 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002015 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002016
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002017 location = theme_get_location(t, input->sx, input->sy,
2018 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002019 frame->widget->allocation.height,
2020 window->type == TYPE_MAXIMIZED ?
2021 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002022
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002023 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002024 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002025 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002026 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002027 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002028 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002029 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002030 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002031 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002032 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002033 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002034 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002035 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002036 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002037 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002038 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002039 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002040 case THEME_LOCATION_EXTERIOR:
2041 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002042 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002043 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002044 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002045}
2046
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002047static void
2048frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002049{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002050 struct display *display;
2051
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002052 switch (index) {
2053 case 0: /* close */
2054 if (window->close_handler)
2055 window->close_handler(window->parent,
2056 window->user_data);
2057 else
2058 display_exit(window->display);
2059 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002060 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002061 display = window->display;
2062 if (display->workspace > 0)
2063 workspace_manager_move_surface(display->workspace_manager,
2064 window->surface,
2065 display->workspace - 1);
2066 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002067 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002068 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002069 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002070 workspace_manager_move_surface(display->workspace_manager,
2071 window->surface,
2072 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002073 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002074 case 3: /* fullscreen */
2075 /* we don't have a way to get out of fullscreen for now */
2076 if (window->fullscreen_handler)
2077 window->fullscreen_handler(window, window->user_data);
2078 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002079 }
2080}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002081
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002082void
2083window_show_frame_menu(struct window *window,
2084 struct input *input, uint32_t time)
2085{
2086 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002087 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002088
2089 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002090 "Close",
2091 "Move to workspace above", "Move to workspace below",
2092 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002093 };
2094
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002095 if (window->fullscreen_handler)
2096 count = ARRAY_LENGTH(entries);
2097 else
2098 count = ARRAY_LENGTH(entries) - 1;
2099
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002100 input_get_position(input, &x, &y);
2101 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002102 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002103}
2104
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002105static int
2106frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002107 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002108{
2109 return frame_get_pointer_image_for_location(data, input);
2110}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002111
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002112static int
2113frame_motion_handler(struct widget *widget,
2114 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002115 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002116{
2117 return frame_get_pointer_image_for_location(data, input);
2118}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002119
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002121frame_button_handler(struct widget *widget,
2122 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002123 uint32_t button, enum wl_pointer_button_state state,
2124 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002125
2126{
2127 struct frame *frame = data;
2128 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002129 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002130 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002131
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002132 location = theme_get_location(display->theme, input->sx, input->sy,
2133 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002134 frame->widget->allocation.height,
2135 window->type == TYPE_MAXIMIZED ?
2136 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002137
Daniel Stone4dbadb12012-05-30 16:31:51 +01002138 if (window->display->shell && button == BTN_LEFT &&
2139 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002140 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002141 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002142 if (!window->shell_surface)
2143 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002144 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002145 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002146 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002147 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002148 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002149 case THEME_LOCATION_RESIZING_TOP:
2150 case THEME_LOCATION_RESIZING_BOTTOM:
2151 case THEME_LOCATION_RESIZING_LEFT:
2152 case THEME_LOCATION_RESIZING_RIGHT:
2153 case THEME_LOCATION_RESIZING_TOP_LEFT:
2154 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2155 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2156 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
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øgsberg1103a1a2012-04-03 12:00:48 -04002160
Pekka Paalanen99436862012-11-19 17:15:59 +02002161 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002163 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002164 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002165 break;
2166 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002167 } else if (button == BTN_RIGHT &&
2168 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002169 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002170 }
2171}
2172
2173struct widget *
2174frame_create(struct window *window, void *data)
2175{
2176 struct frame *frame;
2177
2178 frame = malloc(sizeof *frame);
2179 memset(frame, 0, sizeof *frame);
2180
2181 frame->widget = window_add_widget(window, frame);
2182 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002183
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002184 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2185 widget_set_resize_handler(frame->widget, frame_resize_handler);
2186 widget_set_enter_handler(frame->widget, frame_enter_handler);
2187 widget_set_motion_handler(frame->widget, frame_motion_handler);
2188 widget_set_button_handler(frame->widget, frame_button_handler);
2189
Martin Minarik1998b152012-05-10 02:04:35 +02002190 /* Create empty list for frame buttons */
2191 wl_list_init(&frame->buttons_list);
2192
2193 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2194 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2195
2196 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2197 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2198
2199 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2200 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2201
2202 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2203 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2204
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002205 window->frame = frame;
2206
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002207 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002208}
2209
Kristian Høgsberga1627922012-06-20 17:30:03 -04002210void
2211frame_set_child_size(struct widget *widget, int child_width, int child_height)
2212{
2213 struct display *display = widget->window->display;
2214 struct theme *t = display->theme;
2215 int decoration_width, decoration_height;
2216 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002217 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002218
2219 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002220 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002221 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002222 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002223
2224 width = child_width + decoration_width;
2225 height = child_height + decoration_height;
2226 } else {
2227 width = child_width;
2228 height = child_height;
2229 }
2230
2231 window_schedule_resize(widget->window, width, height);
2232}
2233
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002234static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002235frame_destroy(struct frame *frame)
2236{
Martin Minarik1998b152012-05-10 02:04:35 +02002237 struct frame_button *button, *tmp;
2238
2239 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2240 frame_button_destroy(button);
2241
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002242 /* frame->child must be destroyed by the application */
2243 widget_destroy(frame->widget);
2244 free(frame);
2245}
2246
2247static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002248input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002249 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002250{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002251 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002252 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002253
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002254 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002255 return;
2256
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002257 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002258 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002259 widget = old;
2260 if (input->grab)
2261 widget = input->grab;
2262 if (widget->leave_handler)
2263 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002264 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002265 }
2266
2267 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002268 widget = focus;
2269 if (input->grab)
2270 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002271 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002272 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002273 cursor = widget->enter_handler(focus, input, x, y,
2274 widget->user_data);
2275 else
2276 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002277
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002278 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002279 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002280}
2281
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002282void
2283input_grab(struct input *input, struct widget *widget, uint32_t button)
2284{
2285 input->grab = widget;
2286 input->grab_button = button;
2287}
2288
2289void
2290input_ungrab(struct input *input)
2291{
2292 struct widget *widget;
2293
2294 input->grab = NULL;
2295 if (input->pointer_focus) {
2296 widget = widget_find_widget(input->pointer_focus->widget,
2297 input->sx, input->sy);
2298 input_set_focus_widget(input, widget, input->sx, input->sy);
2299 }
2300}
2301
2302static void
2303input_remove_pointer_focus(struct input *input)
2304{
2305 struct window *window = input->pointer_focus;
2306
2307 if (!window)
2308 return;
2309
2310 input_set_focus_widget(input, NULL, 0, 0);
2311
2312 input->pointer_focus = NULL;
2313 input->current_cursor = CURSOR_UNSET;
2314}
2315
2316static void
2317pointer_handle_enter(void *data, struct wl_pointer *pointer,
2318 uint32_t serial, struct wl_surface *surface,
2319 wl_fixed_t sx_w, wl_fixed_t sy_w)
2320{
2321 struct input *input = data;
2322 struct window *window;
2323 struct widget *widget;
2324 float sx = wl_fixed_to_double(sx_w);
2325 float sy = wl_fixed_to_double(sy_w);
2326
2327 if (!surface) {
2328 /* enter event for a window we've just destroyed */
2329 return;
2330 }
2331
2332 input->display->serial = serial;
2333 input->pointer_enter_serial = serial;
2334 input->pointer_focus = wl_surface_get_user_data(surface);
2335 window = input->pointer_focus;
2336
Pekka Paalanen99436862012-11-19 17:15:59 +02002337 if (window->resizing) {
2338 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002339 /* Schedule a redraw to free the pool */
2340 window_schedule_redraw(window);
2341 }
2342
2343 input->sx = sx;
2344 input->sy = sy;
2345
2346 widget = widget_find_widget(window->widget, sx, sy);
2347 input_set_focus_widget(input, widget, sx, sy);
2348}
2349
2350static void
2351pointer_handle_leave(void *data, struct wl_pointer *pointer,
2352 uint32_t serial, struct wl_surface *surface)
2353{
2354 struct input *input = data;
2355
2356 input->display->serial = serial;
2357 input_remove_pointer_focus(input);
2358}
2359
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002360static void
Daniel Stone37816df2012-05-16 18:45:18 +01002361pointer_handle_motion(void *data, struct wl_pointer *pointer,
2362 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002363{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002364 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002365 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002366 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002367 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002368 float sx = wl_fixed_to_double(sx_w);
2369 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002370
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002371 input->sx = sx;
2372 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002373
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002374 if (!window)
2375 return;
2376
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002377 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002378 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002379 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002380 }
2381
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002382 if (input->grab)
2383 widget = input->grab;
2384 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002385 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002386 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002387 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002388 input, time, sx, sy,
2389 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002390 else
2391 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002392
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002393 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002394}
2395
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002396static void
Daniel Stone37816df2012-05-16 18:45:18 +01002397pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002398 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002399{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002400 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002401 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002402 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002403
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002404 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002405 if (input->focus_widget && input->grab == NULL &&
2406 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002407 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002408
Neil Roberts6b28aad2012-01-23 19:11:18 +00002409 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002410 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002411 (*widget->button_handler)(widget,
2412 input, time,
2413 button, state,
2414 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002415
Daniel Stone4dbadb12012-05-30 16:31:51 +01002416 if (input->grab && input->grab_button == button &&
2417 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002418 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002419}
2420
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002421static void
Daniel Stone37816df2012-05-16 18:45:18 +01002422pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002423 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002424{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002425 struct input *input = data;
2426 struct widget *widget;
2427
2428 widget = input->focus_widget;
2429 if (input->grab)
2430 widget = input->grab;
2431 if (widget && widget->axis_handler)
2432 (*widget->axis_handler)(widget,
2433 input, time,
2434 axis, value,
2435 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002436}
2437
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002438static const struct wl_pointer_listener pointer_listener = {
2439 pointer_handle_enter,
2440 pointer_handle_leave,
2441 pointer_handle_motion,
2442 pointer_handle_button,
2443 pointer_handle_axis,
2444};
2445
2446static void
2447input_remove_keyboard_focus(struct input *input)
2448{
2449 struct window *window = input->keyboard_focus;
2450 struct itimerspec its;
2451
2452 its.it_interval.tv_sec = 0;
2453 its.it_interval.tv_nsec = 0;
2454 its.it_value.tv_sec = 0;
2455 its.it_value.tv_nsec = 0;
2456 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2457
2458 if (!window)
2459 return;
2460
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002461 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002462 if (window->keyboard_focus_handler)
2463 (*window->keyboard_focus_handler)(window, NULL,
2464 window->user_data);
2465
2466 input->keyboard_focus = NULL;
2467}
2468
2469static void
2470keyboard_repeat_func(struct task *task, uint32_t events)
2471{
2472 struct input *input =
2473 container_of(task, struct input, repeat_task);
2474 struct window *window = input->keyboard_focus;
2475 uint64_t exp;
2476
2477 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2478 /* If we change the timer between the fd becoming
2479 * readable and getting here, there'll be nothing to
2480 * read and we get EAGAIN. */
2481 return;
2482
2483 if (window && window->key_handler) {
2484 (*window->key_handler)(window, input, input->repeat_time,
2485 input->repeat_key, input->repeat_sym,
2486 WL_KEYBOARD_KEY_STATE_PRESSED,
2487 window->user_data);
2488 }
2489}
2490
2491static void
2492keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2493 uint32_t format, int fd, uint32_t size)
2494{
2495 struct input *input = data;
2496 char *map_str;
2497
2498 if (!data) {
2499 close(fd);
2500 return;
2501 }
2502
2503 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2504 close(fd);
2505 return;
2506 }
2507
2508 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2509 if (map_str == MAP_FAILED) {
2510 close(fd);
2511 return;
2512 }
2513
2514 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2515 map_str,
2516 XKB_KEYMAP_FORMAT_TEXT_V1,
2517 0);
2518 munmap(map_str, size);
2519 close(fd);
2520
2521 if (!input->xkb.keymap) {
2522 fprintf(stderr, "failed to compile keymap\n");
2523 return;
2524 }
2525
2526 input->xkb.state = xkb_state_new(input->xkb.keymap);
2527 if (!input->xkb.state) {
2528 fprintf(stderr, "failed to create XKB state\n");
2529 xkb_map_unref(input->xkb.keymap);
2530 input->xkb.keymap = NULL;
2531 return;
2532 }
2533
2534 input->xkb.control_mask =
2535 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2536 input->xkb.alt_mask =
2537 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2538 input->xkb.shift_mask =
2539 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2540}
2541
2542static void
2543keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2544 uint32_t serial, struct wl_surface *surface,
2545 struct wl_array *keys)
2546{
2547 struct input *input = data;
2548 struct window *window;
2549
2550 input->display->serial = serial;
2551 input->keyboard_focus = wl_surface_get_user_data(surface);
2552
2553 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002554 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002555 if (window->keyboard_focus_handler)
2556 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002557 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002558}
2559
2560static void
2561keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2562 uint32_t serial, struct wl_surface *surface)
2563{
2564 struct input *input = data;
2565
2566 input->display->serial = serial;
2567 input_remove_keyboard_focus(input);
2568}
2569
Scott Moreau210d0792012-03-22 10:47:01 -06002570static void
Daniel Stone37816df2012-05-16 18:45:18 +01002571keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002572 uint32_t serial, uint32_t time, uint32_t key,
2573 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002574{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002575 struct input *input = data;
2576 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002577 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002578 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002579 const xkb_keysym_t *syms;
2580 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002581 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002582
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002583 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002584 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002585 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002586 return;
2587
Daniel Stone97f68542012-05-30 16:32:01 +01002588 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002589
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002590 sym = XKB_KEY_NoSymbol;
2591 if (num_syms == 1)
2592 sym = syms[0];
2593
2594 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002595 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002596 window_set_maximized(window,
2597 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002598 } else if (sym == XKB_KEY_F11 &&
2599 window->fullscreen_handler &&
2600 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2601 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002602 } else if (sym == XKB_KEY_F4 &&
2603 input->modifiers == MOD_ALT_MASK &&
2604 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2605 if (window->close_handler)
2606 window->close_handler(window->parent,
2607 window->user_data);
2608 else
2609 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002610 } else if (window->key_handler) {
2611 (*window->key_handler)(window, input, time, key,
2612 sym, state, window->user_data);
2613 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002614
2615 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2616 key == input->repeat_key) {
2617 its.it_interval.tv_sec = 0;
2618 its.it_interval.tv_nsec = 0;
2619 its.it_value.tv_sec = 0;
2620 its.it_value.tv_nsec = 0;
2621 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2622 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2623 input->repeat_sym = sym;
2624 input->repeat_key = key;
2625 input->repeat_time = time;
2626 its.it_interval.tv_sec = 0;
2627 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2628 its.it_value.tv_sec = 0;
2629 its.it_value.tv_nsec = 400 * 1000 * 1000;
2630 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2631 }
2632}
2633
2634static void
Daniel Stone351eb612012-05-31 15:27:47 -04002635keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2636 uint32_t serial, uint32_t mods_depressed,
2637 uint32_t mods_latched, uint32_t mods_locked,
2638 uint32_t group)
2639{
2640 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002641 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002642
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002643 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2644 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002645 mask = xkb_state_serialize_mods(input->xkb.state,
2646 XKB_STATE_DEPRESSED |
2647 XKB_STATE_LATCHED);
2648 input->modifiers = 0;
2649 if (mask & input->xkb.control_mask)
2650 input->modifiers |= MOD_CONTROL_MASK;
2651 if (mask & input->xkb.alt_mask)
2652 input->modifiers |= MOD_ALT_MASK;
2653 if (mask & input->xkb.shift_mask)
2654 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002655}
2656
Daniel Stone37816df2012-05-16 18:45:18 +01002657static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002658 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002659 keyboard_handle_enter,
2660 keyboard_handle_leave,
2661 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002662 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002663};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002664
2665static void
Daniel Stone37816df2012-05-16 18:45:18 +01002666seat_handle_capabilities(void *data, struct wl_seat *seat,
2667 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002668{
Daniel Stone37816df2012-05-16 18:45:18 +01002669 struct input *input = data;
2670
2671 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2672 input->pointer = wl_seat_get_pointer(seat);
2673 wl_pointer_set_user_data(input->pointer, input);
2674 wl_pointer_add_listener(input->pointer, &pointer_listener,
2675 input);
2676 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2677 wl_pointer_destroy(input->pointer);
2678 input->pointer = NULL;
2679 }
2680
2681 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2682 input->keyboard = wl_seat_get_keyboard(seat);
2683 wl_keyboard_set_user_data(input->keyboard, input);
2684 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2685 input);
2686 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2687 wl_keyboard_destroy(input->keyboard);
2688 input->keyboard = NULL;
2689 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002690}
2691
Daniel Stone37816df2012-05-16 18:45:18 +01002692static const struct wl_seat_listener seat_listener = {
2693 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002694};
2695
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002696void
2697input_get_position(struct input *input, int32_t *x, int32_t *y)
2698{
2699 *x = input->sx;
2700 *y = input->sy;
2701}
2702
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002703struct display *
2704input_get_display(struct input *input)
2705{
2706 return input->display;
2707}
2708
Daniel Stone37816df2012-05-16 18:45:18 +01002709struct wl_seat *
2710input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002711{
Daniel Stone37816df2012-05-16 18:45:18 +01002712 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002713}
2714
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002715uint32_t
2716input_get_modifiers(struct input *input)
2717{
2718 return input->modifiers;
2719}
2720
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002721struct widget *
2722input_get_focus_widget(struct input *input)
2723{
2724 return input->focus_widget;
2725}
2726
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002727struct data_offer {
2728 struct wl_data_offer *offer;
2729 struct input *input;
2730 struct wl_array types;
2731 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002732
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002733 struct task io_task;
2734 int fd;
2735 data_func_t func;
2736 int32_t x, y;
2737 void *user_data;
2738};
2739
2740static void
2741data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2742{
2743 struct data_offer *offer = data;
2744 char **p;
2745
2746 p = wl_array_add(&offer->types, sizeof *p);
2747 *p = strdup(type);
2748}
2749
2750static const struct wl_data_offer_listener data_offer_listener = {
2751 data_offer_offer,
2752};
2753
2754static void
2755data_offer_destroy(struct data_offer *offer)
2756{
2757 char **p;
2758
2759 offer->refcount--;
2760 if (offer->refcount == 0) {
2761 wl_data_offer_destroy(offer->offer);
2762 for (p = offer->types.data; *p; p++)
2763 free(*p);
2764 wl_array_release(&offer->types);
2765 free(offer);
2766 }
2767}
2768
2769static void
2770data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002771 struct wl_data_device *data_device,
2772 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002773{
2774 struct data_offer *offer;
2775
2776 offer = malloc(sizeof *offer);
2777
2778 wl_array_init(&offer->types);
2779 offer->refcount = 1;
2780 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002781 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002782 wl_data_offer_add_listener(offer->offer,
2783 &data_offer_listener, offer);
2784}
2785
2786static void
2787data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002788 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002789 wl_fixed_t x_w, wl_fixed_t y_w,
2790 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002791{
2792 struct input *input = data;
2793 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002794 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002795 float x = wl_fixed_to_double(x_w);
2796 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002797 char **p;
2798
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002799 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002800 window = wl_surface_get_user_data(surface);
2801 input->pointer_focus = window;
2802
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002803 if (offer) {
2804 input->drag_offer = wl_data_offer_get_user_data(offer);
2805
2806 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2807 *p = NULL;
2808
2809 types_data = input->drag_offer->types.data;
2810 } else {
2811 input->drag_offer = NULL;
2812 types_data = NULL;
2813 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002814
2815 window = input->pointer_focus;
2816 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002817 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002818 window->user_data);
2819}
2820
2821static void
2822data_device_leave(void *data, struct wl_data_device *data_device)
2823{
2824 struct input *input = data;
2825
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002826 if (input->drag_offer) {
2827 data_offer_destroy(input->drag_offer);
2828 input->drag_offer = NULL;
2829 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002830}
2831
2832static void
2833data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002834 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002835{
2836 struct input *input = data;
2837 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002838 float x = wl_fixed_to_double(x_w);
2839 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002840 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002841
2842 input->sx = x;
2843 input->sy = y;
2844
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002845 if (input->drag_offer)
2846 types_data = input->drag_offer->types.data;
2847 else
2848 types_data = NULL;
2849
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002850 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002851 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002852 window->user_data);
2853}
2854
2855static void
2856data_device_drop(void *data, struct wl_data_device *data_device)
2857{
2858 struct input *input = data;
2859 struct window *window = input->pointer_focus;
2860
2861 if (window->drop_handler)
2862 window->drop_handler(window, input,
2863 input->sx, input->sy, window->user_data);
2864}
2865
2866static void
2867data_device_selection(void *data,
2868 struct wl_data_device *wl_data_device,
2869 struct wl_data_offer *offer)
2870{
2871 struct input *input = data;
2872 char **p;
2873
2874 if (input->selection_offer)
2875 data_offer_destroy(input->selection_offer);
2876
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002877 if (offer) {
2878 input->selection_offer = wl_data_offer_get_user_data(offer);
2879 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2880 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002881 } else {
2882 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002883 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002884}
2885
2886static const struct wl_data_device_listener data_device_listener = {
2887 data_device_data_offer,
2888 data_device_enter,
2889 data_device_leave,
2890 data_device_motion,
2891 data_device_drop,
2892 data_device_selection
2893};
2894
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002895static void
2896input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002897{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002898 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002899 struct wl_cursor *cursor;
2900 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002901
Daniel Stone80972742012-11-07 17:51:39 +11002902 if (!input->pointer)
2903 return;
2904
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002905 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002906 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002907 return;
2908
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002909 if (index >= (int) cursor->image_count) {
2910 fprintf(stderr, "cursor index out of range\n");
2911 return;
2912 }
2913
2914 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002915 buffer = wl_cursor_image_get_buffer(image);
2916 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002917 return;
2918
Kristian Høgsbergae277372012-08-01 09:41:08 -04002919 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002920 input->pointer_surface,
2921 image->hotspot_x, image->hotspot_y);
2922 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2923 wl_surface_damage(input->pointer_surface, 0, 0,
2924 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002925 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002926}
2927
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002928static const struct wl_callback_listener pointer_surface_listener;
2929
2930static void
2931pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2932 uint32_t time)
2933{
2934 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002935 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002936 int i;
2937
2938 if (callback) {
2939 assert(callback == input->cursor_frame_cb);
2940 wl_callback_destroy(callback);
2941 input->cursor_frame_cb = NULL;
2942 }
2943
Daniel Stone80972742012-11-07 17:51:39 +11002944 if (!input->pointer)
2945 return;
2946
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002947 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002948 wl_pointer_set_cursor(input->pointer,
2949 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002950 NULL, 0, 0);
2951 return;
2952 }
2953
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002954 if (input->current_cursor == CURSOR_UNSET)
2955 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002956 cursor = input->display->cursors[input->current_cursor];
2957 if (!cursor)
2958 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002959
2960 /* FIXME We don't have the current time on the first call so we set
2961 * the animation start to the time of the first frame callback. */
2962 if (time == 0)
2963 input->cursor_anim_start = 0;
2964 else if (input->cursor_anim_start == 0)
2965 input->cursor_anim_start = time;
2966
2967 if (time == 0 || input->cursor_anim_start == 0)
2968 i = 0;
2969 else
2970 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2971
Pekka Paalanenbc106382012-10-10 12:49:31 +03002972 if (cursor->image_count > 1) {
2973 input->cursor_frame_cb =
2974 wl_surface_frame(input->pointer_surface);
2975 wl_callback_add_listener(input->cursor_frame_cb,
2976 &pointer_surface_listener, input);
2977 }
2978
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002979 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002980}
2981
2982static const struct wl_callback_listener pointer_surface_listener = {
2983 pointer_surface_frame_callback
2984};
2985
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002986void
2987input_set_pointer_image(struct input *input, int pointer)
2988{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002989 int force = 0;
2990
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002991 if (!input->pointer)
2992 return;
2993
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002994 if (input->pointer_enter_serial > input->cursor_serial)
2995 force = 1;
2996
2997 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002998 return;
2999
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003000 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003001 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003002 if (!input->cursor_frame_cb)
3003 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003004 else if (force) {
3005 /* The current frame callback may be stuck if, for instance,
3006 * the set cursor request was processed by the server after
3007 * this client lost the focus. In this case the cursor surface
3008 * might not be mapped and the frame callback wouldn't ever
3009 * complete. Send a set_cursor and attach to try to map the
3010 * cursor surface again so that the callback will finish */
3011 input_set_pointer_image_index(input, 0);
3012 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003013}
3014
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003015struct wl_data_device *
3016input_get_data_device(struct input *input)
3017{
3018 return input->data_device;
3019}
3020
3021void
3022input_set_selection(struct input *input,
3023 struct wl_data_source *source, uint32_t time)
3024{
3025 wl_data_device_set_selection(input->data_device, source, time);
3026}
3027
3028void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003029input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003030{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003031 wl_data_offer_accept(input->drag_offer->offer,
3032 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003033}
3034
3035static void
3036offer_io_func(struct task *task, uint32_t events)
3037{
3038 struct data_offer *offer =
3039 container_of(task, struct data_offer, io_task);
3040 unsigned int len;
3041 char buffer[4096];
3042
3043 len = read(offer->fd, buffer, sizeof buffer);
3044 offer->func(buffer, len,
3045 offer->x, offer->y, offer->user_data);
3046
3047 if (len == 0) {
3048 close(offer->fd);
3049 data_offer_destroy(offer);
3050 }
3051}
3052
3053static void
3054data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3055 data_func_t func, void *user_data)
3056{
3057 int p[2];
3058
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003059 if (pipe2(p, O_CLOEXEC) == -1)
3060 return;
3061
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003062 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3063 close(p[1]);
3064
3065 offer->io_task.run = offer_io_func;
3066 offer->fd = p[0];
3067 offer->func = func;
3068 offer->refcount++;
3069 offer->user_data = user_data;
3070
3071 display_watch_fd(offer->input->display,
3072 offer->fd, EPOLLIN, &offer->io_task);
3073}
3074
3075void
3076input_receive_drag_data(struct input *input, const char *mime_type,
3077 data_func_t func, void *data)
3078{
3079 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3080 input->drag_offer->x = input->sx;
3081 input->drag_offer->y = input->sy;
3082}
3083
3084int
3085input_receive_selection_data(struct input *input, const char *mime_type,
3086 data_func_t func, void *data)
3087{
3088 char **p;
3089
3090 if (input->selection_offer == NULL)
3091 return -1;
3092
3093 for (p = input->selection_offer->types.data; *p; p++)
3094 if (strcmp(mime_type, *p) == 0)
3095 break;
3096
3097 if (*p == NULL)
3098 return -1;
3099
3100 data_offer_receive_data(input->selection_offer,
3101 mime_type, func, data);
3102 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003103}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003104
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003105int
3106input_receive_selection_data_to_fd(struct input *input,
3107 const char *mime_type, int fd)
3108{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003109 if (input->selection_offer)
3110 wl_data_offer_receive(input->selection_offer->offer,
3111 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003112
3113 return 0;
3114}
3115
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003116void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003117window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003118{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003119 if (!window->shell_surface)
3120 return;
3121
Daniel Stone37816df2012-05-16 18:45:18 +01003122 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003123}
3124
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003125static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003126idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003127{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003128 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003129 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003130
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003131 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003132 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003133 widget_set_allocation(widget,
3134 window->pending_allocation.x,
3135 window->pending_allocation.y,
3136 window->pending_allocation.width,
3137 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003138
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003139 if (window->input_region) {
3140 wl_region_destroy(window->input_region);
3141 window->input_region = NULL;
3142 }
3143
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003144 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003145 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003146
3147 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003148
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003149 if (widget->resize_handler)
3150 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003151 widget->allocation.width,
3152 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003153 widget->user_data);
3154
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003155 if (window->allocation.width != widget->allocation.width ||
3156 window->allocation.height != widget->allocation.height) {
3157 window->allocation = widget->allocation;
3158 window_schedule_redraw(window);
3159 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003160
3161 if (widget->opaque)
3162 wl_region_add(window->opaque_region, 0, 0,
3163 widget->allocation.width,
3164 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003165}
3166
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003167void
3168window_schedule_resize(struct window *window, int width, int height)
3169{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003170 window->pending_allocation.x = 0;
3171 window->pending_allocation.y = 0;
3172 window->pending_allocation.width = width;
3173 window->pending_allocation.height = height;
3174
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003175 if (window->min_allocation.width == 0)
3176 window->min_allocation = window->pending_allocation;
3177 if (window->pending_allocation.width < window->min_allocation.width)
3178 window->pending_allocation.width = window->min_allocation.width;
3179 if (window->pending_allocation.height < window->min_allocation.height)
3180 window->pending_allocation.height = window->min_allocation.height;
3181
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003182 window->resize_needed = 1;
3183 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003184}
3185
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003186void
3187widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3188{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003189 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003190}
3191
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003192static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003193handle_ping(void *data, struct wl_shell_surface *shell_surface,
3194 uint32_t serial)
3195{
3196 wl_shell_surface_pong(shell_surface, serial);
3197}
3198
3199static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003200handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003201 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003202{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003203 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003204
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003205 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003206 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003207}
3208
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003209static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003210menu_destroy(struct menu *menu)
3211{
3212 widget_destroy(menu->widget);
3213 window_destroy(menu->window);
3214 free(menu);
3215}
3216
3217static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003218handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3219{
3220 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003221 struct menu *menu = window->widget->user_data;
3222
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003223 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003224 * device. Or just use wl_callback. And this really needs to
3225 * be a window vfunc that the menu can set. And we need the
3226 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003227
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003228 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003229 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003230 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003231}
3232
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003233static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003234 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003235 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003236 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003237};
3238
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003239void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003240window_get_allocation(struct window *window,
3241 struct rectangle *allocation)
3242{
3243 *allocation = window->allocation;
3244}
3245
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003246static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003247widget_redraw(struct widget *widget)
3248{
3249 struct widget *child;
3250
3251 if (widget->redraw_handler)
3252 widget->redraw_handler(widget, widget->user_data);
3253 wl_list_for_each(child, &widget->child_list, link)
3254 widget_redraw(child);
3255}
3256
3257static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003258frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3259{
3260 struct window *window = data;
3261
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003262 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003263 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003264 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003265 window->redraw_scheduled = 0;
3266 if (window->redraw_needed)
3267 window_schedule_redraw(window);
3268}
3269
3270static const struct wl_callback_listener listener = {
3271 frame_callback
3272};
3273
3274static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003275idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003276{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003277 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003278
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003279 if (window->resize_needed)
3280 idle_resize(window);
3281
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003282 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003283 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003284 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003285 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003286
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003287 window->frame_cb = wl_surface_frame(window->surface);
3288 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003289 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003290}
3291
3292void
3293window_schedule_redraw(struct window *window)
3294{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003295 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003296 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003297 window->redraw_task.run = idle_redraw;
3298 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003299 window->redraw_scheduled = 1;
3300 }
3301}
3302
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003303int
3304window_is_fullscreen(struct window *window)
3305{
3306 return window->type == TYPE_FULLSCREEN;
3307}
3308
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003309void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003310window_set_fullscreen(struct window *window, int fullscreen)
3311{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003312 if (!window->display->shell)
3313 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003314
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003315 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003316 return;
3317
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003318 if (fullscreen) {
3319 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003320 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003321 wl_shell_surface_set_fullscreen(window->shell_surface,
3322 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3323 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003324 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003325 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003326 wl_shell_surface_set_toplevel(window->shell_surface);
3327 window_schedule_resize(window,
3328 window->saved_allocation.width,
3329 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003330 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003331}
3332
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003333int
3334window_is_maximized(struct window *window)
3335{
3336 return window->type == TYPE_MAXIMIZED;
3337}
3338
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003339void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003340window_set_maximized(struct window *window, int maximized)
3341{
3342 if (!window->display->shell)
3343 return;
3344
3345 if ((window->type == TYPE_MAXIMIZED) == maximized)
3346 return;
3347
3348 if (window->type == TYPE_TOPLEVEL) {
3349 window->saved_allocation = window->allocation;
3350 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3351 window->type = TYPE_MAXIMIZED;
3352 } else {
3353 wl_shell_surface_set_toplevel(window->shell_surface);
3354 window->type = TYPE_TOPLEVEL;
3355 window_schedule_resize(window,
3356 window->saved_allocation.width,
3357 window->saved_allocation.height);
3358 }
3359}
3360
3361void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003362window_set_user_data(struct window *window, void *data)
3363{
3364 window->user_data = data;
3365}
3366
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003367void *
3368window_get_user_data(struct window *window)
3369{
3370 return window->user_data;
3371}
3372
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003373void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003374window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003375 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003376{
3377 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003378}
3379
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003380void
3381window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003382 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003383{
3384 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003385}
3386
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003387void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003388window_set_data_handler(struct window *window, window_data_handler_t handler)
3389{
3390 window->data_handler = handler;
3391}
3392
3393void
3394window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3395{
3396 window->drop_handler = handler;
3397}
3398
3399void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003400window_set_close_handler(struct window *window,
3401 window_close_handler_t handler)
3402{
3403 window->close_handler = handler;
3404}
3405
3406void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003407window_set_fullscreen_handler(struct window *window,
3408 window_fullscreen_handler_t handler)
3409{
3410 window->fullscreen_handler = handler;
3411}
3412
3413void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003414window_set_output_handler(struct window *window,
3415 window_output_handler_t handler)
3416{
3417 window->output_handler = handler;
3418}
3419
3420void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003421window_set_title(struct window *window, const char *title)
3422{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003423 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003424 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003425 if (window->shell_surface)
3426 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003427}
3428
3429const char *
3430window_get_title(struct window *window)
3431{
3432 return window->title;
3433}
3434
3435void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003436window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3437{
3438 struct text_cursor_position *text_cursor_position =
3439 window->display->text_cursor_position;
3440
Scott Moreau9295ce02012-06-01 12:46:10 -06003441 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003442 return;
3443
3444 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003445 window->surface,
3446 wl_fixed_from_int(x),
3447 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003448}
3449
3450void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003451window_damage(struct window *window, int32_t x, int32_t y,
3452 int32_t width, int32_t height)
3453{
3454 wl_surface_damage(window->surface, x, y, width, height);
3455}
3456
Casey Dahlin9074db52012-04-19 22:50:09 -04003457static void
3458surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003459 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003460{
Rob Bradford7507b572012-05-15 17:55:34 +01003461 struct window *window = data;
3462 struct output *output;
3463 struct output *output_found = NULL;
3464 struct window_output *window_output;
3465
3466 wl_list_for_each(output, &window->display->output_list, link) {
3467 if (output->output == wl_output) {
3468 output_found = output;
3469 break;
3470 }
3471 }
3472
3473 if (!output_found)
3474 return;
3475
3476 window_output = malloc (sizeof *window_output);
3477 window_output->output = output_found;
3478
3479 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003480
3481 if (window->output_handler)
3482 window->output_handler(window, output_found, 1,
3483 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003484}
3485
3486static void
3487surface_leave(void *data,
3488 struct wl_surface *wl_surface, struct wl_output *output)
3489{
Rob Bradford7507b572012-05-15 17:55:34 +01003490 struct window *window = data;
3491 struct window_output *window_output;
3492 struct window_output *window_output_found = NULL;
3493
3494 wl_list_for_each(window_output, &window->window_output_list, link) {
3495 if (window_output->output->output == output) {
3496 window_output_found = window_output;
3497 break;
3498 }
3499 }
3500
3501 if (window_output_found) {
3502 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003503
3504 if (window->output_handler)
3505 window->output_handler(window, window_output->output,
3506 0, window->user_data);
3507
Rob Bradford7507b572012-05-15 17:55:34 +01003508 free(window_output_found);
3509 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003510}
3511
3512static const struct wl_surface_listener surface_listener = {
3513 surface_enter,
3514 surface_leave
3515};
3516
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003517static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003518window_create_internal(struct display *display,
3519 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003520{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003521 struct window *window;
3522
3523 window = malloc(sizeof *window);
3524 if (window == NULL)
3525 return NULL;
3526
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003527 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003528 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003529 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003530 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003531 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003532 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003533 window->shell_surface =
3534 wl_shell_get_shell_surface(display->shell,
3535 window->surface);
3536 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003537 window->allocation.x = 0;
3538 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003539 window->allocation.width = 0;
3540 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003541 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003542 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003543 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003544 window->input_region = NULL;
3545 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003546
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003547 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003548#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003549 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003550#else
3551 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3552#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003553 else
3554 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003555
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003556 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003557 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003558 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003559
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003560 if (window->shell_surface) {
3561 wl_shell_surface_set_user_data(window->shell_surface, window);
3562 wl_shell_surface_add_listener(window->shell_surface,
3563 &shell_surface_listener, window);
3564 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003565
Rob Bradford7507b572012-05-15 17:55:34 +01003566 wl_list_init (&window->window_output_list);
3567
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003568 return window;
3569}
3570
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003571struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003572window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003573{
3574 struct window *window;
3575
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003576 window = window_create_internal(display, NULL, TYPE_NONE);
3577 if (!window)
3578 return NULL;
3579
3580 return window;
3581}
3582
3583struct window *
3584window_create_custom(struct display *display)
3585{
3586 struct window *window;
3587
3588 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003589 if (!window)
3590 return NULL;
3591
3592 return window;
3593}
3594
3595struct window *
3596window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003597 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003598{
3599 struct window *window;
3600
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003601 window = window_create_internal(parent->display,
3602 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003603 if (!window)
3604 return NULL;
3605
3606 window->x = x;
3607 window->y = y;
3608
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003609 if (display->shell)
3610 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003611 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003612 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003613
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003614 return window;
3615}
3616
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003617static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003618menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003619{
3620 int next;
3621
3622 next = (sy - 8) / 20;
3623 if (menu->current != next) {
3624 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003625 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003626 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003627}
3628
3629static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003630menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003631 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003632 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003633{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003634 struct menu *menu = data;
3635
3636 if (widget == menu->widget)
3637 menu_set_item(data, y);
3638
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003639 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003640}
3641
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003642static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003643menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003644 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003645{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003646 struct menu *menu = data;
3647
3648 if (widget == menu->widget)
3649 menu_set_item(data, y);
3650
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003651 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003652}
3653
3654static void
3655menu_leave_handler(struct widget *widget, struct input *input, void *data)
3656{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003657 struct menu *menu = data;
3658
3659 if (widget == menu->widget)
3660 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003661}
3662
3663static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003664menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003665 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003666 uint32_t button, enum wl_pointer_button_state state,
3667 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003668
3669{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003670 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003671
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003672 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3673 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003674 /* Either relase after press-drag-release or
3675 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003676 menu->func(menu->window->parent,
3677 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003678 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003679 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003680 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003681 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003682 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003683}
3684
3685static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003686menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003687{
3688 cairo_t *cr;
3689 const int32_t r = 3, margin = 3;
3690 struct menu *menu = data;
3691 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003692 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003693
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003694 cr = cairo_create(window->cairo_surface);
3695 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3696 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3697 cairo_paint(cr);
3698
3699 width = window->allocation.width;
3700 height = window->allocation.height;
3701 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003702
3703 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003704 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3705 cairo_fill(cr);
3706
3707 for (i = 0; i < menu->count; i++) {
3708 if (i == menu->current) {
3709 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3710 cairo_rectangle(cr, margin, i * 20 + margin,
3711 width - 2 * margin, 20);
3712 cairo_fill(cr);
3713 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3714 cairo_move_to(cr, 10, i * 20 + 16);
3715 cairo_show_text(cr, menu->entries[i]);
3716 } else {
3717 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3718 cairo_move_to(cr, 10, i * 20 + 16);
3719 cairo_show_text(cr, menu->entries[i]);
3720 }
3721 }
3722
3723 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003724}
3725
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003726void
3727window_show_menu(struct display *display,
3728 struct input *input, uint32_t time, struct window *parent,
3729 int32_t x, int32_t y,
3730 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003731{
3732 struct window *window;
3733 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003734 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003735
3736 menu = malloc(sizeof *menu);
3737 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003738 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003739
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003740 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003741 if (!window) {
3742 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003743 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003744 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003745
3746 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003747 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003748 menu->entries = entries;
3749 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003750 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003751 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003752 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003753 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003754 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003755 window->type = TYPE_MENU;
3756 window->x = x;
3757 window->y = y;
3758
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003759 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003760 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003761 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003762 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003763 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003764
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003765 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003766 widget_set_enter_handler(menu->widget, menu_enter_handler);
3767 widget_set_leave_handler(menu->widget, menu_leave_handler);
3768 widget_set_motion_handler(menu->widget, menu_motion_handler);
3769 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003770
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003771 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003772 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003773}
3774
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003775void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003776window_set_buffer_type(struct window *window, enum window_buffer_type type)
3777{
3778 window->buffer_type = type;
3779}
3780
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003781
3782static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003783display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003784 struct wl_output *wl_output,
3785 int x, int y,
3786 int physical_width,
3787 int physical_height,
3788 int subpixel,
3789 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003790 const char *model,
3791 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003792{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003793 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003794
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003795 output->allocation.x = x;
3796 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003797 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003798}
3799
3800static void
3801display_handle_mode(void *data,
3802 struct wl_output *wl_output,
3803 uint32_t flags,
3804 int width,
3805 int height,
3806 int refresh)
3807{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003808 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003809 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003810
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003811 if (flags & WL_OUTPUT_MODE_CURRENT) {
3812 output->allocation.width = width;
3813 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003814 if (display->output_configure_handler)
3815 (*display->output_configure_handler)(
3816 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003817 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003818}
3819
3820static const struct wl_output_listener output_listener = {
3821 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003822 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003823};
3824
3825static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003826display_add_output(struct display *d, uint32_t id)
3827{
3828 struct output *output;
3829
3830 output = malloc(sizeof *output);
3831 if (output == NULL)
3832 return;
3833
3834 memset(output, 0, sizeof *output);
3835 output->display = d;
3836 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003837 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003838 wl_list_insert(d->output_list.prev, &output->link);
3839
3840 wl_output_add_listener(output->output, &output_listener, output);
3841}
3842
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003843static void
3844output_destroy(struct output *output)
3845{
3846 if (output->destroy_handler)
3847 (*output->destroy_handler)(output, output->user_data);
3848
3849 wl_output_destroy(output->output);
3850 wl_list_remove(&output->link);
3851 free(output);
3852}
3853
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003854void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003855display_set_global_handler(struct display *display,
3856 display_global_handler_t handler)
3857{
3858 struct global *global;
3859
3860 display->global_handler = handler;
3861 if (!handler)
3862 return;
3863
3864 wl_list_for_each(global, &display->global_list, link)
3865 display->global_handler(display,
3866 global->name, global->interface,
3867 global->version, display->user_data);
3868}
3869
3870void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003871display_set_output_configure_handler(struct display *display,
3872 display_output_handler_t handler)
3873{
3874 struct output *output;
3875
3876 display->output_configure_handler = handler;
3877 if (!handler)
3878 return;
3879
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003880 wl_list_for_each(output, &display->output_list, link) {
3881 if (output->allocation.width == 0 &&
3882 output->allocation.height == 0)
3883 continue;
3884
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003885 (*display->output_configure_handler)(output,
3886 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003887 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003888}
3889
3890void
3891output_set_user_data(struct output *output, void *data)
3892{
3893 output->user_data = data;
3894}
3895
3896void *
3897output_get_user_data(struct output *output)
3898{
3899 return output->user_data;
3900}
3901
3902void
3903output_set_destroy_handler(struct output *output,
3904 display_output_handler_t handler)
3905{
3906 output->destroy_handler = handler;
3907 /* FIXME: implement this, once we have way to remove outputs */
3908}
3909
3910void
Scott Moreau4e072362012-09-29 02:03:11 -06003911output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003912{
Scott Moreau4e072362012-09-29 02:03:11 -06003913 struct rectangle allocation = output->allocation;
3914
3915 switch (output->transform) {
3916 case WL_OUTPUT_TRANSFORM_90:
3917 case WL_OUTPUT_TRANSFORM_270:
3918 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3919 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3920 /* Swap width and height */
3921 allocation.width = output->allocation.height;
3922 allocation.height = output->allocation.width;
3923 break;
3924 }
3925
3926 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003927}
3928
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003929struct wl_output *
3930output_get_wl_output(struct output *output)
3931{
3932 return output->output;
3933}
3934
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003935enum wl_output_transform
3936output_get_transform(struct output *output)
3937{
3938 return output->transform;
3939}
3940
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003941static void
Daniel Stone97f68542012-05-30 16:32:01 +01003942fini_xkb(struct input *input)
3943{
3944 xkb_state_unref(input->xkb.state);
3945 xkb_map_unref(input->xkb.keymap);
3946}
3947
3948static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003949display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003950{
3951 struct input *input;
3952
3953 input = malloc(sizeof *input);
3954 if (input == NULL)
3955 return;
3956
3957 memset(input, 0, sizeof *input);
3958 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003959 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003960 input->pointer_focus = NULL;
3961 input->keyboard_focus = NULL;
3962 wl_list_insert(d->input_list.prev, &input->link);
3963
Daniel Stone37816df2012-05-16 18:45:18 +01003964 wl_seat_add_listener(input->seat, &seat_listener, input);
3965 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003966
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003967 input->data_device =
3968 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003969 input->seat);
3970 wl_data_device_add_listener(input->data_device, &data_device_listener,
3971 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003972
3973 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003974
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003975 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3976 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003977 input->repeat_task.run = keyboard_repeat_func;
3978 display_watch_fd(d, input->repeat_timer_fd,
3979 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003980}
3981
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003982static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003983input_destroy(struct input *input)
3984{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003985 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003986 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003987
3988 if (input->drag_offer)
3989 data_offer_destroy(input->drag_offer);
3990
3991 if (input->selection_offer)
3992 data_offer_destroy(input->selection_offer);
3993
3994 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003995 fini_xkb(input);
3996
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003997 wl_surface_destroy(input->pointer_surface);
3998
Pekka Paalanene1207c72011-12-16 12:02:09 +02003999 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004000 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004001 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004002 free(input);
4003}
4004
4005static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004006init_workspace_manager(struct display *d, uint32_t id)
4007{
4008 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004009 wl_registry_bind(d->registry, id,
4010 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004011 if (d->workspace_manager != NULL)
4012 workspace_manager_add_listener(d->workspace_manager,
4013 &workspace_manager_listener,
4014 d);
4015}
4016
4017static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004018registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4019 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004020{
4021 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004022 struct global *global;
4023
4024 global = malloc(sizeof *global);
4025 global->name = id;
4026 global->interface = strdup(interface);
4027 global->version = version;
4028 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004029
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004030 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004031 d->compositor = wl_registry_bind(registry, id,
4032 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004033 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004034 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004035 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004036 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004037 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004038 d->shell = wl_registry_bind(registry,
4039 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004040 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004041 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004042 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4043 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004044 wl_registry_bind(registry, id,
4045 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004046 } else if (strcmp(interface, "text_cursor_position") == 0) {
4047 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004048 wl_registry_bind(registry, id,
4049 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004050 } else if (strcmp(interface, "workspace_manager") == 0) {
4051 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004052 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004053
4054 if (d->global_handler)
4055 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004056}
4057
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004058void *
4059display_bind(struct display *display, uint32_t name,
4060 const struct wl_interface *interface, uint32_t version)
4061{
4062 return wl_registry_bind(display->registry, name, interface, version);
4063}
4064
4065static const struct wl_registry_listener registry_listener = {
4066 registry_handle_global
4067};
4068
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004069#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004070static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004071init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004072{
4073 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004074 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004075
Rob Clark6396ed32012-03-11 19:48:41 -05004076#ifdef USE_CAIRO_GLESV2
4077# define GL_BIT EGL_OPENGL_ES2_BIT
4078#else
4079# define GL_BIT EGL_OPENGL_BIT
4080#endif
4081
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004082 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004083 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004084 EGL_RED_SIZE, 1,
4085 EGL_GREEN_SIZE, 1,
4086 EGL_BLUE_SIZE, 1,
4087 EGL_ALPHA_SIZE, 1,
4088 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004089 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004090 EGL_NONE
4091 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004092
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004093#ifdef USE_CAIRO_GLESV2
4094 static const EGLint context_attribs[] = {
4095 EGL_CONTEXT_CLIENT_VERSION, 2,
4096 EGL_NONE
4097 };
4098 EGLint api = EGL_OPENGL_ES_API;
4099#else
4100 EGLint *context_attribs = NULL;
4101 EGLint api = EGL_OPENGL_API;
4102#endif
4103
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004104 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004105 if (!eglInitialize(d->dpy, &major, &minor)) {
4106 fprintf(stderr, "failed to initialize display\n");
4107 return -1;
4108 }
4109
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004110 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004111 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
4112 return -1;
4113 }
4114
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004115 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4116 &d->argb_config, 1, &n) || n != 1) {
4117 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004118 return -1;
4119 }
4120
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004121 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004122 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004123 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004124 fprintf(stderr, "failed to create context\n");
4125 return -1;
4126 }
4127
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004128 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004129 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004130 return -1;
4131 }
4132
Benjamin Franzke0c991632011-09-27 21:57:31 +02004133 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4134 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4135 fprintf(stderr, "failed to get cairo egl argb device\n");
4136 return -1;
4137 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004138
4139 return 0;
4140}
4141
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004142static void
4143fini_egl(struct display *display)
4144{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004145 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004146
4147 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4148 EGL_NO_CONTEXT);
4149
4150 eglTerminate(display->dpy);
4151 eglReleaseThread();
4152}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004153#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004154
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004155static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004156init_dummy_surface(struct display *display)
4157{
4158 int len;
4159 void *data;
4160
4161 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4162 data = malloc(len);
4163 display->dummy_surface =
4164 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4165 1, 1, len);
4166 display->dummy_surface_data = data;
4167}
4168
4169static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004170handle_display_data(struct task *task, uint32_t events)
4171{
4172 struct display *display =
4173 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004174 struct epoll_event ep;
4175 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004176
4177 display->display_fd_events = events;
4178
4179 if (events & EPOLLERR || events & EPOLLHUP) {
4180 display_exit(display);
4181 return;
4182 }
4183
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004184 if (events & EPOLLIN) {
4185 ret = wl_display_dispatch(display->display);
4186 if (ret == -1) {
4187 display_exit(display);
4188 return;
4189 }
4190 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004191
4192 if (events & EPOLLOUT) {
4193 ret = wl_display_flush(display->display);
4194 if (ret == 0) {
4195 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4196 ep.data.ptr = &display->display_task;
4197 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4198 display->display_fd, &ep);
4199 } else if (ret == -1 && errno != EAGAIN) {
4200 display_exit(display);
4201 return;
4202 }
4203 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004204}
4205
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004206struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004207display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004208{
4209 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004210
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004211 d = malloc(sizeof *d);
4212 if (d == NULL)
4213 return NULL;
4214
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004215 memset(d, 0, sizeof *d);
4216
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004217 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004218 if (d->display == NULL) {
4219 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004220 return NULL;
4221 }
4222
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004223 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004224 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004225 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004226 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4227 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004228
4229 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004230 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004231 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004232 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004233
Daniel Stone97f68542012-05-30 16:32:01 +01004234 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004235 if (d->xkb_context == NULL) {
4236 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004237 return NULL;
4238 }
4239
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004240 d->workspace = 0;
4241 d->workspace_count = 1;
4242
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004243 d->registry = wl_display_get_registry(d->display);
4244 wl_registry_add_listener(d->registry, &registry_listener, d);
4245 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004246#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004247 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004248 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004249#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004250
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004251 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004252
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004253 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004254
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004255 wl_list_init(&d->window_list);
4256
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004257 init_dummy_surface(d);
4258
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004259 return d;
4260}
4261
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004262static void
4263display_destroy_outputs(struct display *display)
4264{
4265 struct output *tmp;
4266 struct output *output;
4267
4268 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4269 output_destroy(output);
4270}
4271
Pekka Paalanene1207c72011-12-16 12:02:09 +02004272static void
4273display_destroy_inputs(struct display *display)
4274{
4275 struct input *tmp;
4276 struct input *input;
4277
4278 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4279 input_destroy(input);
4280}
4281
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004282void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004283display_destroy(struct display *display)
4284{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004285 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004286 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4287 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004288
4289 if (!wl_list_empty(&display->deferred_list))
4290 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4291
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004292 cairo_surface_destroy(display->dummy_surface);
4293 free(display->dummy_surface_data);
4294
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004295 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004296 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004297
Daniel Stone97f68542012-05-30 16:32:01 +01004298 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004299
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004300 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004301 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004302
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004303#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004304 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004305#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004306
Pekka Paalanenc2052982011-12-16 11:41:32 +02004307 if (display->shell)
4308 wl_shell_destroy(display->shell);
4309
4310 if (display->shm)
4311 wl_shm_destroy(display->shm);
4312
4313 if (display->data_device_manager)
4314 wl_data_device_manager_destroy(display->data_device_manager);
4315
4316 wl_compositor_destroy(display->compositor);
4317
4318 close(display->epoll_fd);
4319
U. Artie Eoff44874d92012-10-02 21:12:35 -07004320 if (!(display->display_fd_events & EPOLLERR) &&
4321 !(display->display_fd_events & EPOLLHUP))
4322 wl_display_flush(display->display);
4323
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004324 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004325 free(display);
4326}
4327
4328void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004329display_set_user_data(struct display *display, void *data)
4330{
4331 display->user_data = data;
4332}
4333
4334void *
4335display_get_user_data(struct display *display)
4336{
4337 return display->user_data;
4338}
4339
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004340struct wl_display *
4341display_get_display(struct display *display)
4342{
4343 return display->display;
4344}
4345
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004346struct output *
4347display_get_output(struct display *display)
4348{
4349 return container_of(display->output_list.next, struct output, link);
4350}
4351
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004352struct wl_compositor *
4353display_get_compositor(struct display *display)
4354{
4355 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004356}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004357
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004358uint32_t
4359display_get_serial(struct display *display)
4360{
4361 return display->serial;
4362}
4363
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004364EGLDisplay
4365display_get_egl_display(struct display *d)
4366{
4367 return d->dpy;
4368}
4369
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004370struct wl_data_source *
4371display_create_data_source(struct display *display)
4372{
4373 return wl_data_device_manager_create_data_source(display->data_device_manager);
4374}
4375
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004376EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004377display_get_argb_egl_config(struct display *d)
4378{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004379 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004380}
4381
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004382struct wl_shell *
4383display_get_shell(struct display *display)
4384{
4385 return display->shell;
4386}
4387
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004388int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004389display_acquire_window_surface(struct display *display,
4390 struct window *window,
4391 EGLContext ctx)
4392{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004393 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004394 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004395
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004396 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004397}
4398
4399void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004400display_release_window_surface(struct display *display,
4401 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004402{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004403 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004404 return;
4405
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004406 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004407}
4408
4409void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004410display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004411{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004412 wl_list_insert(&display->deferred_list, &task->link);
4413}
4414
4415void
4416display_watch_fd(struct display *display,
4417 int fd, uint32_t events, struct task *task)
4418{
4419 struct epoll_event ep;
4420
4421 ep.events = events;
4422 ep.data.ptr = task;
4423 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4424}
4425
4426void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004427display_unwatch_fd(struct display *display, int fd)
4428{
4429 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4430}
4431
4432void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004433display_run(struct display *display)
4434{
4435 struct task *task;
4436 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004437 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004438
Pekka Paalanen826d7952011-12-15 10:14:07 +02004439 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004440 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004441 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004442 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004443 struct task, link);
4444 wl_list_remove(&task->link);
4445 task->run(task, 0);
4446 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004447
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004448 wl_display_dispatch_pending(display->display);
4449
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004450 if (!display->running)
4451 break;
4452
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004453 ret = wl_display_flush(display->display);
4454 if (ret < 0 && errno == EAGAIN) {
4455 ep[0].events =
4456 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4457 ep[0].data.ptr = &display->display_task;
4458
4459 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4460 display->display_fd, &ep[0]);
4461 } else if (ret < 0) {
4462 break;
4463 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004464
4465 count = epoll_wait(display->epoll_fd,
4466 ep, ARRAY_LENGTH(ep), -1);
4467 for (i = 0; i < count; i++) {
4468 task = ep[i].data.ptr;
4469 task->run(task, ep[i].events);
4470 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004471 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004472}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004473
4474void
4475display_exit(struct display *display)
4476{
4477 display->running = 0;
4478}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004479
4480void
4481keysym_modifiers_add(struct wl_array *modifiers_map,
4482 const char *name)
4483{
4484 size_t len = strlen(name) + 1;
4485 char *p;
4486
4487 p = wl_array_add(modifiers_map, len);
4488
4489 if (p == NULL)
4490 return;
4491
4492 strncpy(p, name, len);
4493}
4494
4495static xkb_mod_index_t
4496keysym_modifiers_get_index(struct wl_array *modifiers_map,
4497 const char *name)
4498{
4499 xkb_mod_index_t index = 0;
4500 char *p = modifiers_map->data;
4501
4502 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4503 if (strcmp(p, name) == 0)
4504 return index;
4505
4506 index++;
4507 p += strlen(p) + 1;
4508 }
4509
4510 return XKB_MOD_INVALID;
4511}
4512
4513xkb_mod_mask_t
4514keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4515 const char *name)
4516{
4517 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4518
4519 if (index == XKB_MOD_INVALID)
4520 return XKB_MOD_INVALID;
4521
4522 return 1 << index;
4523}