blob: ef40c7b04d76c808327ee2dda4a40220fba410bb [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
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030043#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050044#include <wayland-egl.h>
45
Rob Clark6396ed32012-03-11 19:48:41 -050046#ifdef USE_CAIRO_GLESV2
47#include <GLES2/gl2.h>
48#include <GLES2/gl2ext.h>
49#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050051#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <EGL/egl.h>
53#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040054
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030056#else /* HAVE_CAIRO_EGL */
57typedef void *EGLDisplay;
58typedef void *EGLConfig;
59typedef void *EGLContext;
60#define EGL_NO_DISPLAY ((EGLDisplay)0)
61#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050062
Daniel Stone9d4f0302012-02-15 16:33:21 +000063#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030064#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040065
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020067#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040068#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070075struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030076
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040077struct global {
78 uint32_t name;
79 char *interface;
80 uint32_t version;
81 struct wl_list link;
82};
83
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040086 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050087 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040088 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050094 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020095 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040097 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040098
99 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700100 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400101 struct task display_task;
102
103 int epoll_fd;
104 struct wl_list deferred_list;
105
Pekka Paalanen826d7952011-12-15 10:14:07 +0200106 int running;
107
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400108 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400109 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400110 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500111 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400112
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400113 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400114
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300115 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300116 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400117
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200118 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120
121 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100122
123 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200124
125 uint32_t workspace;
126 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200127
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500131};
132
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400133enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400134 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400136 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500137 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500139 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_CUSTOM
141};
Rob Bradford7507b572012-05-15 17:55:34 +0100142
143struct window_output {
144 struct output *output;
145 struct wl_list link;
146};
147
Pekka Paalanenec076692012-11-30 13:37:27 +0200148enum toysurface_prepare_flags {
149 SURFACE_HINT_RESIZE = 0x01,
150};
151
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200152struct toysurface {
153 /*
154 * Prepare the surface for drawing. Makes sure there is a surface
155 * of the right size available for rendering, and returns it.
156 * dx,dy are the x,y of wl_surface.attach.
157 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200158 * If flags has SURFACE_HINT_RESIZE set, the user is
159 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160 * Returns the Cairo surface to draw to.
161 */
162 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200163 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164
165 /*
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
169 */
170 void (*swap)(struct toysurface *base,
171 struct rectangle *server_allocation);
172
173 /*
174 * Make the toysurface current with the given EGL context.
175 * Returns 0 on success, and negative of failure.
176 */
177 int (*acquire)(struct toysurface *base, EGLContext ctx);
178
179 /*
180 * Release the toysurface from the EGL context, returning control
181 * to Cairo.
182 */
183 void (*release)(struct toysurface *base);
184
185 /*
186 * Destroy the toysurface, including the Cairo surface, any
187 * backing storage, and the Wayland protocol objects.
188 */
189 void (*destroy)(struct toysurface *base);
190};
191
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500192struct window {
193 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500194 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100195 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500196 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200197 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500198 struct wl_region *input_region;
199 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500200 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500201 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400202 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500203 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500204 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400205 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400206 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400207 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400208 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400209 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400210 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400211 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400212 int focus_count;
213
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400214 enum window_buffer_type buffer_type;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +0200215 enum wl_output_transform buffer_transform;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200216 struct toysurface *toysurface;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400217 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500218
Pekka Paalanen99436862012-11-19 17:15:59 +0200219 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200220 int fullscreen_method;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400221
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500222 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500223 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400224 window_data_handler_t data_handler;
225 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500226 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400227 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200228 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400229
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300230 struct wl_callback *frame_cb;
231
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200232 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500233 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500234
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500235 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400236 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500237};
238
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500239struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500240 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300241 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500242 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400243 struct wl_list link;
244 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500245 widget_resize_handler_t resize_handler;
246 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500247 widget_enter_handler_t enter_handler;
248 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500249 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500250 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200251 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400252 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500253 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300254 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500255 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400256};
257
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400258struct input {
259 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100260 struct wl_seat *seat;
261 struct wl_pointer *pointer;
262 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400263 struct window *pointer_focus;
264 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300265 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300266 uint32_t cursor_anim_start;
267 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300268 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400269 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400270 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400271 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400272 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400273 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400274
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500275 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500276 struct widget *grab;
277 uint32_t grab_button;
278
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400279 struct wl_data_device *data_device;
280 struct data_offer *drag_offer;
281 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100282
283 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100284 struct xkb_keymap *keymap;
285 struct xkb_state *state;
286 xkb_mod_mask_t control_mask;
287 xkb_mod_mask_t alt_mask;
288 xkb_mod_mask_t shift_mask;
289 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400290
291 struct task repeat_task;
292 int repeat_timer_fd;
293 uint32_t repeat_sym;
294 uint32_t repeat_key;
295 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400296};
297
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500298struct output {
299 struct display *display;
300 struct wl_output *output;
301 struct rectangle allocation;
302 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600303 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200304
305 display_output_handler_t destroy_handler;
306 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500307};
308
Martin Minarik1998b152012-05-10 02:04:35 +0200309enum frame_button_action {
310 FRAME_BUTTON_NULL = 0,
311 FRAME_BUTTON_ICON = 1,
312 FRAME_BUTTON_CLOSE = 2,
313 FRAME_BUTTON_MINIMIZE = 3,
314 FRAME_BUTTON_MAXIMIZE = 4,
315};
316
317enum frame_button_pointer {
318 FRAME_BUTTON_DEFAULT = 0,
319 FRAME_BUTTON_OVER = 1,
320 FRAME_BUTTON_ACTIVE = 2,
321};
322
323enum frame_button_align {
324 FRAME_BUTTON_RIGHT = 0,
325 FRAME_BUTTON_LEFT = 1,
326};
327
328enum frame_button_decoration {
329 FRAME_BUTTON_NONE = 0,
330 FRAME_BUTTON_FANCY = 1,
331};
332
333struct frame_button {
334 struct widget *widget;
335 struct frame *frame;
336 cairo_surface_t *icon;
337 enum frame_button_action type;
338 enum frame_button_pointer state;
339 struct wl_list link; /* buttons_list */
340 enum frame_button_align align;
341 enum frame_button_decoration decoration;
342};
343
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500344struct frame {
345 struct widget *widget;
346 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200347 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500348};
349
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500350struct menu {
351 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500352 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500353 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500354 const char **entries;
355 uint32_t time;
356 int current;
357 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400358 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500359 menu_func_t func;
360};
361
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300362struct tooltip {
363 struct widget *parent;
364 struct window *window;
365 struct widget *widget;
366 char *entry;
367 struct task tooltip_task;
368 int tooltip_fd;
369 float x, y;
370};
371
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700372struct shm_pool {
373 struct wl_shm_pool *pool;
374 size_t size;
375 size_t used;
376 void *data;
377};
378
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400379enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300380 CURSOR_DEFAULT = 100,
381 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400382};
383
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500384enum window_location {
385 WINDOW_INTERIOR = 0,
386 WINDOW_RESIZING_TOP = 1,
387 WINDOW_RESIZING_BOTTOM = 2,
388 WINDOW_RESIZING_LEFT = 4,
389 WINDOW_RESIZING_TOP_LEFT = 5,
390 WINDOW_RESIZING_BOTTOM_LEFT = 6,
391 WINDOW_RESIZING_RIGHT = 8,
392 WINDOW_RESIZING_TOP_RIGHT = 9,
393 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
394 WINDOW_RESIZING_MASK = 15,
395 WINDOW_EXTERIOR = 16,
396 WINDOW_TITLEBAR = 17,
397 WINDOW_CLIENT_AREA = 18,
398};
399
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200400static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500402#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400403
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200404struct egl_window_surface {
405 struct toysurface base;
406 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100407 struct display *display;
408 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200409 struct wl_egl_window *egl_window;
410 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100411};
412
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200413static struct egl_window_surface *
414to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100415{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200416 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100417}
418
419static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200420egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200421 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100422{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200423 struct egl_window_surface *surface = to_egl_window_surface(base);
424
425 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
426 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
427
428 return cairo_surface_reference(surface->cairo_surface);
429}
430
431static void
432egl_window_surface_swap(struct toysurface *base,
433 struct rectangle *server_allocation)
434{
435 struct egl_window_surface *surface = to_egl_window_surface(base);
436
437 cairo_gl_surface_swapbuffers(surface->cairo_surface);
438 wl_egl_window_get_attached_size(surface->egl_window,
439 &server_allocation->width,
440 &server_allocation->height);
441}
442
443static int
444egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
445{
446 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200447 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100448
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200449 device = cairo_surface_get_device(surface->cairo_surface);
450 if (!device)
451 return -1;
452
453 if (!ctx) {
454 if (device == surface->display->argb_device)
455 ctx = surface->display->argb_ctx;
456 else
457 assert(0);
458 }
459
460 cairo_device_flush(device);
461 cairo_device_acquire(device);
462 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
463 surface->egl_surface, ctx))
464 fprintf(stderr, "failed to make surface current\n");
465
466 return 0;
467}
468
469static void
470egl_window_surface_release(struct toysurface *base)
471{
472 struct egl_window_surface *surface = to_egl_window_surface(base);
473 cairo_device_t *device;
474
475 device = cairo_surface_get_device(surface->cairo_surface);
476 if (!device)
477 return;
478
479 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
480 surface->display->argb_ctx))
481 fprintf(stderr, "failed to make context current\n");
482
483 cairo_device_release(device);
484}
485
486static void
487egl_window_surface_destroy(struct toysurface *base)
488{
489 struct egl_window_surface *surface = to_egl_window_surface(base);
490 struct display *d = surface->display;
491
492 cairo_surface_destroy(surface->cairo_surface);
493 eglDestroySurface(d->dpy, surface->egl_surface);
494 wl_egl_window_destroy(surface->egl_window);
495 surface->surface = NULL;
496
497 free(surface);
498}
499
500static struct toysurface *
501egl_window_surface_create(struct display *display,
502 struct wl_surface *wl_surface,
503 uint32_t flags,
504 struct rectangle *rectangle)
505{
506 struct egl_window_surface *surface;
507
Pekka Paalanenb3627362012-11-19 17:16:00 +0200508 if (display->dpy == EGL_NO_DISPLAY)
509 return NULL;
510
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511 surface = calloc(1, sizeof *surface);
512 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100513 return NULL;
514
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515 surface->base.prepare = egl_window_surface_prepare;
516 surface->base.swap = egl_window_surface_swap;
517 surface->base.acquire = egl_window_surface_acquire;
518 surface->base.release = egl_window_surface_release;
519 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521 surface->display = display;
522 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400523
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200524 surface->egl_window = wl_egl_window_create(surface->surface,
525 rectangle->width,
526 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100527
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200528 surface->egl_surface = eglCreateWindowSurface(display->dpy,
529 display->argb_config,
530 surface->egl_window,
531 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100532
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533 surface->cairo_surface =
534 cairo_gl_surface_create_for_egl(display->argb_device,
535 surface->egl_surface,
536 rectangle->width,
537 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100538
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200539 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100540}
541
Pekka Paalanenb3627362012-11-19 17:16:00 +0200542#else
543
544static struct toysurface *
545egl_window_surface_create(struct display *display,
546 struct wl_surface *wl_surface,
547 uint32_t flags,
548 struct rectangle *rectangle)
549{
550 return NULL;
551}
552
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400553#endif
554
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200555struct shm_surface_data {
556 struct wl_buffer *buffer;
557 struct shm_pool *pool;
558};
559
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560struct wl_buffer *
561display_get_buffer_for_surface(struct display *display,
562 cairo_surface_t *surface)
563{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200564 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400565
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200566 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400567
568 return data->buffer;
569}
570
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500571static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700572shm_pool_destroy(struct shm_pool *pool);
573
574static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400575shm_surface_data_destroy(void *p)
576{
577 struct shm_surface_data *data = p;
578
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200579 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700580 if (data->pool)
581 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300582
583 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584}
585
Kristian Høgsberg16626282012-04-03 11:21:27 -0400586static struct wl_shm_pool *
587make_shm_pool(struct display *display, int size, void **data)
588{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400589 struct wl_shm_pool *pool;
590 int fd;
591
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300592 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400593 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300594 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
595 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400596 return NULL;
597 }
598
599 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400600 if (*data == MAP_FAILED) {
601 fprintf(stderr, "mmap failed: %m\n");
602 close(fd);
603 return NULL;
604 }
605
606 pool = wl_shm_create_pool(display->shm, fd, size);
607
608 close(fd);
609
610 return pool;
611}
612
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700613static struct shm_pool *
614shm_pool_create(struct display *display, size_t size)
615{
616 struct shm_pool *pool = malloc(sizeof *pool);
617
618 if (!pool)
619 return NULL;
620
621 pool->pool = make_shm_pool(display, size, &pool->data);
622 if (!pool->pool) {
623 free(pool);
624 return NULL;
625 }
626
627 pool->size = size;
628 pool->used = 0;
629
630 return pool;
631}
632
633static void *
634shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
635{
636 if (pool->used + size > pool->size)
637 return NULL;
638
639 *offset = pool->used;
640 pool->used += size;
641
642 return (char *) pool->data + *offset;
643}
644
645/* destroy the pool. this does not unmap the memory though */
646static void
647shm_pool_destroy(struct shm_pool *pool)
648{
649 munmap(pool->data, pool->size);
650 wl_shm_pool_destroy(pool->pool);
651 free(pool);
652}
653
654/* Start allocating from the beginning of the pool again */
655static void
656shm_pool_reset(struct shm_pool *pool)
657{
658 pool->used = 0;
659}
660
661static int
662data_length_for_shm_surface(struct rectangle *rect)
663{
664 int stride;
665
666 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
667 rect->width);
668 return stride * rect->height;
669}
670
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500671static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700672display_create_shm_surface_from_pool(struct display *display,
673 struct rectangle *rectangle,
674 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675{
676 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400677 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400678 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700679 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400680 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681
682 data = malloc(sizeof *data);
683 if (data == NULL)
684 return NULL;
685
686 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
687 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700688 length = stride * rectangle->height;
689 data->pool = NULL;
690 map = shm_pool_allocate(pool, length, &offset);
691
692 if (!map) {
693 free(data);
694 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400695 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400696
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400697 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400698 CAIRO_FORMAT_ARGB32,
699 rectangle->width,
700 rectangle->height,
701 stride);
702
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200703 cairo_surface_set_user_data(surface, &shm_surface_data_key,
704 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400706 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500707 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400708 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500709 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400710
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200711 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
712 rectangle->width,
713 rectangle->height,
714 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400715
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700716 return surface;
717}
718
719static cairo_surface_t *
720display_create_shm_surface(struct display *display,
721 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200722 struct shm_pool *alternate_pool,
723 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700724{
725 struct shm_surface_data *data;
726 struct shm_pool *pool;
727 cairo_surface_t *surface;
728
Pekka Paalanen99436862012-11-19 17:15:59 +0200729 if (alternate_pool) {
730 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731 surface = display_create_shm_surface_from_pool(display,
732 rectangle,
733 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200734 alternate_pool);
735 if (surface) {
736 data = cairo_surface_get_user_data(surface,
737 &shm_surface_data_key);
738 goto out;
739 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700740 }
741
742 pool = shm_pool_create(display,
743 data_length_for_shm_surface(rectangle));
744 if (!pool)
745 return NULL;
746
747 surface =
748 display_create_shm_surface_from_pool(display, rectangle,
749 flags, pool);
750
751 if (!surface) {
752 shm_pool_destroy(pool);
753 return NULL;
754 }
755
756 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200757 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700758 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400759
Pekka Paalanen99436862012-11-19 17:15:59 +0200760out:
761 if (data_ret)
762 *data_ret = data;
763
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400764 return surface;
765}
766
nobled7b87cb02011-02-01 18:51:47 +0000767static int
768check_size(struct rectangle *rect)
769{
770 if (rect->width && rect->height)
771 return 0;
772
773 fprintf(stderr, "tried to create surface of "
774 "width: %d, height: %d\n", rect->width, rect->height);
775 return -1;
776}
777
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400778cairo_surface_t *
779display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100780 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400781 struct rectangle *rectangle,
782 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400783{
nobled7b87cb02011-02-01 18:51:47 +0000784 if (check_size(rectangle) < 0)
785 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200786
787 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200788 return display_create_shm_surface(display, rectangle, flags,
789 NULL, NULL);
790}
791
Pekka Paalanena4eda732012-11-19 17:16:02 +0200792struct shm_surface_leaf {
793 cairo_surface_t *cairo_surface;
794 /* 'data' is automatically destroyed, when 'cairo_surface' is */
795 struct shm_surface_data *data;
796
797 struct shm_pool *resize_pool;
798 int busy;
799};
800
801static void
802shm_surface_leaf_release(struct shm_surface_leaf *leaf)
803{
804 if (leaf->cairo_surface)
805 cairo_surface_destroy(leaf->cairo_surface);
806 /* leaf->data already destroyed via cairo private */
807
808 if (leaf->resize_pool)
809 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200810
811 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200812}
813
Pekka Paalanen99436862012-11-19 17:15:59 +0200814struct shm_surface {
815 struct toysurface base;
816 struct display *display;
817 struct wl_surface *surface;
818 uint32_t flags;
819 int dx, dy;
820
Pekka Paalanena4eda732012-11-19 17:16:02 +0200821 struct shm_surface_leaf leaf[2];
822 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200823};
824
825static struct shm_surface *
826to_shm_surface(struct toysurface *base)
827{
828 return container_of(base, struct shm_surface, base);
829}
830
Pekka Paalanena4eda732012-11-19 17:16:02 +0200831static void
832shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
833{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200834 struct shm_surface *surface = data;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200835
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200836 if (surface->leaf[0].data->buffer == buffer)
837 surface->leaf[0].busy = 0;
838 else if (surface->leaf[1].data->buffer == buffer)
839 surface->leaf[1].busy = 0;
840 else
841 assert(0 && "shm_surface_buffer_release: unknown buffer");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200842
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200843 if (!surface->leaf[0].busy && !surface->leaf[1].busy)
844 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200845}
846
847static const struct wl_buffer_listener shm_surface_buffer_listener = {
848 shm_surface_buffer_release
849};
850
Pekka Paalanen99436862012-11-19 17:15:59 +0200851static cairo_surface_t *
852shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200853 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200854{
Pekka Paalanenec076692012-11-30 13:37:27 +0200855 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200856 struct shm_surface *surface = to_shm_surface(base);
857 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200858 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200859
860 surface->dx = dx;
861 surface->dy = dy;
862
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200863 /* See shm_surface_buffer_release() */
864 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
865 surface->leaf[1].cairo_surface) {
866 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
867 __func__);
868 }
869
Pekka Paalanena4eda732012-11-19 17:16:02 +0200870 /* pick a free buffer from the two */
871 if (!surface->leaf[0].busy)
872 leaf = &surface->leaf[0];
873 else if (!surface->leaf[1].busy)
874 leaf = &surface->leaf[1];
875 else {
876 fprintf(stderr, "%s: both buffers are held by the server.\n",
877 __func__);
878 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200879 }
880
Pekka Paalanena4eda732012-11-19 17:16:02 +0200881 if (!resize_hint && leaf->resize_pool) {
882 cairo_surface_destroy(leaf->cairo_surface);
883 leaf->cairo_surface = NULL;
884 shm_pool_destroy(leaf->resize_pool);
885 leaf->resize_pool = NULL;
886 }
887
888 if (leaf->cairo_surface &&
889 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
890 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200891 goto out;
892
Pekka Paalanena4eda732012-11-19 17:16:02 +0200893 if (leaf->cairo_surface)
894 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200895
Pekka Paalanena4eda732012-11-19 17:16:02 +0200896 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200897 /* Create a big pool to allocate from, while continuously
898 * resizing. Mmapping a new pool in the server
899 * is relatively expensive, so reusing a pool performs
900 * better, but may temporarily reserve unneeded memory.
901 */
902 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200903 leaf->resize_pool = shm_pool_create(surface->display,
904 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200905 }
906
Pekka Paalanena4eda732012-11-19 17:16:02 +0200907 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200908 display_create_shm_surface(surface->display, &rect,
909 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200910 leaf->resize_pool,
911 &leaf->data);
912 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200913 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200914
915out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916 surface->current = leaf;
917
918 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200919}
920
921static void
922shm_surface_swap(struct toysurface *base,
923 struct rectangle *server_allocation)
924{
925 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200926 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200927
928 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200929 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200930 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200931 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200932
Pekka Paalanena4eda732012-11-19 17:16:02 +0200933 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200934 surface->dx, surface->dy);
935 wl_surface_damage(surface->surface, 0, 0,
936 server_allocation->width, server_allocation->height);
937 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938
939 leaf->busy = 1;
940 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200941}
942
943static int
944shm_surface_acquire(struct toysurface *base, EGLContext ctx)
945{
946 return -1;
947}
948
949static void
950shm_surface_release(struct toysurface *base)
951{
952}
953
954static void
955shm_surface_destroy(struct toysurface *base)
956{
957 struct shm_surface *surface = to_shm_surface(base);
958
Pekka Paalanena4eda732012-11-19 17:16:02 +0200959 shm_surface_leaf_release(&surface->leaf[0]);
960 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200961
962 free(surface);
963}
964
965static struct toysurface *
966shm_surface_create(struct display *display, struct wl_surface *wl_surface,
967 uint32_t flags, struct rectangle *rectangle)
968{
969 struct shm_surface *surface;
970
971 surface = calloc(1, sizeof *surface);
972 if (!surface)
973 return NULL;
974
975 surface->base.prepare = shm_surface_prepare;
976 surface->base.swap = shm_surface_swap;
977 surface->base.acquire = shm_surface_acquire;
978 surface->base.release = shm_surface_release;
979 surface->base.destroy = shm_surface_destroy;
980
981 surface->display = display;
982 surface->surface = wl_surface;
983 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200984
985 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400986}
987
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200988/*
989 * The following correspondences between file names and cursors was copied
990 * from: https://bugs.kde.org/attachment.cgi?id=67313
991 */
992
993static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300994 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200995 "sw-resize"
996};
997
998static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300999 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001000 "se-resize"
1001};
1002
1003static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001004 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001005 "s-resize"
1006};
1007
1008static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001009 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001010 "closedhand",
1011 "208530c400c041818281048008011002"
1012};
1013
1014static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001015 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001016 "default",
1017 "top_left_arrow",
1018 "left-arrow"
1019};
1020
1021static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001022 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001023 "w-resize"
1024};
1025
1026static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001027 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001028 "e-resize"
1029};
1030
1031static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001032 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001033 "nw-resize"
1034};
1035
1036static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001037 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001038 "ne-resize"
1039};
1040
1041static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001042 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001043 "n-resize"
1044};
1045
1046static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001047 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001048 "ibeam",
1049 "text"
1050};
1051
1052static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001053 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001054 "pointer",
1055 "pointing_hand",
1056 "e29285e634086352946a0e7090d73106"
1057};
1058
1059static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001060 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001061 "wait",
1062 "0426c94ea35c87780ff01dc239897213"
1063};
1064
1065struct cursor_alternatives {
1066 const char **names;
1067 size_t count;
1068};
1069
1070static const struct cursor_alternatives cursors[] = {
1071 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1072 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1073 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1074 {grabbings, ARRAY_LENGTH(grabbings)},
1075 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1076 {left_sides, ARRAY_LENGTH(left_sides)},
1077 {right_sides, ARRAY_LENGTH(right_sides)},
1078 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1079 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1080 {top_sides, ARRAY_LENGTH(top_sides)},
1081 {xterms, ARRAY_LENGTH(xterms)},
1082 {hand1s, ARRAY_LENGTH(hand1s)},
1083 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001084};
1085
1086static void
1087create_cursors(struct display *display)
1088{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001089 char *config_file;
1090 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001091 unsigned int i, j;
1092 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001093 struct config_key shell_keys[] = {
1094 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1095 };
1096 struct config_section cs[] = {
1097 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1098 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001099
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001100 config_file = config_file_path("weston.ini");
1101 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1102 free(config_file);
1103
1104 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001105 display->cursors =
1106 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1107
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001108 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001109 cursor = NULL;
1110 for (j = 0; !cursor && j < cursors[i].count; ++j)
1111 cursor = wl_cursor_theme_get_cursor(
1112 display->cursor_theme, cursors[i].names[j]);
1113
1114 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001115 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001116 cursors[i].names[0]);
1117
1118 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001119 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001120}
1121
1122static void
1123destroy_cursors(struct display *display)
1124{
1125 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001126 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001127}
1128
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001129struct wl_cursor_image *
1130display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001131{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001132 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001133
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001134 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001135}
1136
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001137static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001138window_get_resize_dx_dy(struct window *window, int *x, int *y)
1139{
1140 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1141 *x = window->server_allocation.width - window->allocation.width;
1142 else
1143 *x = 0;
1144
1145 if (window->resize_edges & WINDOW_RESIZING_TOP)
1146 *y = window->server_allocation.height -
1147 window->allocation.height;
1148 else
1149 *y = 0;
1150
1151 window->resize_edges = 0;
1152}
1153
1154static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001155window_attach_surface(struct window *window)
1156{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001157 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001158
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001159 if (window->type == TYPE_NONE) {
1160 window->type = TYPE_TOPLEVEL;
1161 if (display->shell)
1162 wl_shell_surface_set_toplevel(window->shell_surface);
1163 }
1164
Pekka Paalanen512dde82012-10-10 12:49:27 +03001165 if (window->opaque_region) {
1166 wl_surface_set_opaque_region(window->surface,
1167 window->opaque_region);
1168 wl_region_destroy(window->opaque_region);
1169 window->opaque_region = NULL;
1170 }
1171
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001172 if (window->input_region) {
1173 wl_surface_set_input_region(window->surface,
1174 window->input_region);
1175 wl_region_destroy(window->input_region);
1176 window->input_region = NULL;
1177 }
1178
Pekka Paalanen99436862012-11-19 17:15:59 +02001179 window->toysurface->swap(window->toysurface,
1180 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001181}
1182
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001183int
1184window_has_focus(struct window *window)
1185{
1186 return window->focus_count > 0;
1187}
1188
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001189static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001190window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001191{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001192 if (!window->cairo_surface)
1193 return;
1194
1195 window_attach_surface(window);
1196 cairo_surface_destroy(window->cairo_surface);
1197 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001198}
1199
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001200struct display *
1201window_get_display(struct window *window)
1202{
1203 return window->display;
1204}
1205
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001206static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001207window_create_surface(struct window *window)
1208{
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001209 struct rectangle allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001210 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001211 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001212
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001213 if (!window->transparent)
1214 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001215
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001216 switch (window->buffer_transform) {
1217 case WL_OUTPUT_TRANSFORM_90:
1218 case WL_OUTPUT_TRANSFORM_270:
1219 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1220 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1221 allocation.width = window->allocation.height;
1222 allocation.height = window->allocation.width;
1223 break;
1224 default:
1225 break;
1226 }
1227
Pekka Paalanenb3627362012-11-19 17:16:00 +02001228 if (!window->toysurface &&
1229 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1230 window->display->dpy) {
1231 window->toysurface =
1232 egl_window_surface_create(window->display,
1233 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001234 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001235 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001236
Pekka Paalanenb3627362012-11-19 17:16:00 +02001237 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001238 window->toysurface = shm_surface_create(window->display,
1239 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001240 &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001241
Pekka Paalanenec076692012-11-30 13:37:27 +02001242 if (window->resizing)
1243 flags = SURFACE_HINT_RESIZE;
1244 else
1245 flags = 0;
1246
Pekka Paalanen99436862012-11-19 17:15:59 +02001247 window_get_resize_dx_dy(window, &dx, &dy);
1248 window->cairo_surface =
1249 window->toysurface->prepare(window->toysurface, dx, dy,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001250 allocation.width,
1251 allocation.height,
Pekka Paalanenec076692012-11-30 13:37:27 +02001252 flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001253}
1254
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001255int
1256window_get_buffer_transform(struct window *window)
1257{
1258 return window->buffer_transform;
1259}
1260
1261void
1262window_set_buffer_transform(struct window *window,
1263 enum wl_output_transform transform)
1264{
1265 window->buffer_transform = transform;
1266 wl_surface_set_buffer_transform(window->surface, transform);
1267}
1268
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001269static void frame_destroy(struct frame *frame);
1270
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001271void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001272window_destroy(struct window *window)
1273{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001274 struct display *display = window->display;
1275 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001276 struct window_output *window_output;
1277 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001278
1279 if (window->redraw_scheduled)
1280 wl_list_remove(&window->redraw_task.link);
1281
1282 wl_list_for_each(input, &display->input_list, link) {
1283 if (input->pointer_focus == window)
1284 input->pointer_focus = NULL;
1285 if (input->keyboard_focus == window)
1286 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001287 if (input->focus_widget &&
1288 input->focus_widget->window == window)
1289 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001290 }
1291
Rob Bradford7507b572012-05-15 17:55:34 +01001292 wl_list_for_each_safe(window_output, window_output_tmp,
1293 &window->window_output_list, link) {
1294 free (window_output);
1295 }
1296
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001297 if (window->input_region)
1298 wl_region_destroy(window->input_region);
1299 if (window->opaque_region)
1300 wl_region_destroy(window->opaque_region);
1301
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001302 if (window->frame)
1303 frame_destroy(window->frame);
1304
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001305 if (window->shell_surface)
1306 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001307 wl_surface_destroy(window->surface);
1308 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001309
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001310 if (window->toysurface)
1311 window->toysurface->destroy(window->toysurface);
1312
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001313 if (window->frame_cb)
1314 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001315 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001316 free(window);
1317}
1318
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001319static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001320widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001321{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001322 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001323
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001324 wl_list_for_each(child, &widget->child_list, link) {
1325 target = widget_find_widget(child, x, y);
1326 if (target)
1327 return target;
1328 }
1329
1330 if (widget->allocation.x <= x &&
1331 x < widget->allocation.x + widget->allocation.width &&
1332 widget->allocation.y <= y &&
1333 y < widget->allocation.y + widget->allocation.height) {
1334 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001335 }
1336
1337 return NULL;
1338}
1339
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001340static struct widget *
1341widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001342{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001343 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001344
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001345 widget = malloc(sizeof *widget);
1346 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001347 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001348 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001349 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001350 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001351 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001352 widget->tooltip = NULL;
1353 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001354 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001355
1356 return widget;
1357}
1358
1359struct widget *
1360window_add_widget(struct window *window, void *data)
1361{
1362 window->widget = widget_create(window, data);
1363 wl_list_init(&window->widget->link);
1364
1365 return window->widget;
1366}
1367
1368struct widget *
1369widget_add_widget(struct widget *parent, void *data)
1370{
1371 struct widget *widget;
1372
1373 widget = widget_create(parent->window, data);
1374 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001375
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001376 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001377}
1378
1379void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001380widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001381{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001382 struct display *display = widget->window->display;
1383 struct input *input;
1384
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001385 if (widget->tooltip) {
1386 free(widget->tooltip);
1387 widget->tooltip = NULL;
1388 }
1389
Pekka Paalanene156fb62012-01-19 13:51:38 +02001390 wl_list_for_each(input, &display->input_list, link) {
1391 if (input->focus_widget == widget)
1392 input->focus_widget = NULL;
1393 }
1394
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001395 wl_list_remove(&widget->link);
1396 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001397}
1398
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001399void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001400widget_set_default_cursor(struct widget *widget, int cursor)
1401{
1402 widget->default_cursor = cursor;
1403}
1404
1405void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001406widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001407{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001408 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001409}
1410
1411void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001412widget_set_size(struct widget *widget, int32_t width, int32_t height)
1413{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001414 widget->allocation.width = width;
1415 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001416}
1417
1418void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001419widget_set_allocation(struct widget *widget,
1420 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001421{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001422 widget->allocation.x = x;
1423 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001424 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001425}
1426
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001427void
1428widget_set_transparent(struct widget *widget, int transparent)
1429{
1430 widget->opaque = !transparent;
1431}
1432
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001433void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001434widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001435{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001436 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001437}
1438
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001439void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001440widget_set_resize_handler(struct widget *widget,
1441 widget_resize_handler_t handler)
1442{
1443 widget->resize_handler = handler;
1444}
1445
1446void
1447widget_set_redraw_handler(struct widget *widget,
1448 widget_redraw_handler_t handler)
1449{
1450 widget->redraw_handler = handler;
1451}
1452
1453void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001454widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001455{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001456 widget->enter_handler = handler;
1457}
1458
1459void
1460widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1461{
1462 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001463}
1464
1465void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001466widget_set_motion_handler(struct widget *widget,
1467 widget_motion_handler_t handler)
1468{
1469 widget->motion_handler = handler;
1470}
1471
1472void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001473widget_set_button_handler(struct widget *widget,
1474 widget_button_handler_t handler)
1475{
1476 widget->button_handler = handler;
1477}
1478
1479void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001480widget_set_axis_handler(struct widget *widget,
1481 widget_axis_handler_t handler)
1482{
1483 widget->axis_handler = handler;
1484}
1485
1486void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001487widget_schedule_redraw(struct widget *widget)
1488{
1489 window_schedule_redraw(widget->window);
1490}
1491
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001492cairo_surface_t *
1493window_get_surface(struct window *window)
1494{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001495 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001496}
1497
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001498struct wl_surface *
1499window_get_wl_surface(struct window *window)
1500{
1501 return window->surface;
1502}
1503
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001504struct wl_shell_surface *
1505window_get_wl_shell_surface(struct window *window)
1506{
1507 return window->shell_surface;
1508}
1509
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001510static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001511tooltip_redraw_handler(struct widget *widget, void *data)
1512{
1513 cairo_t *cr;
1514 const int32_t r = 3;
1515 struct tooltip *tooltip = data;
1516 int32_t width, height;
1517 struct window *window = widget->window;
1518
1519 cr = cairo_create(window->cairo_surface);
1520 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1521 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1522 cairo_paint(cr);
1523
1524 width = window->allocation.width;
1525 height = window->allocation.height;
1526 rounded_rect(cr, 0, 0, width, height, r);
1527
1528 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1529 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1530 cairo_fill(cr);
1531
1532 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1533 cairo_move_to(cr, 10, 16);
1534 cairo_show_text(cr, tooltip->entry);
1535 cairo_destroy(cr);
1536}
1537
1538static cairo_text_extents_t
1539get_text_extents(struct tooltip *tooltip)
1540{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001541 cairo_t *cr;
1542 cairo_text_extents_t extents;
1543
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001544 /* Use the dummy_surface because tooltip's surface was not
1545 * created yet, and parent does not have a valid surface
1546 * outside repaint, either.
1547 */
1548 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001549 cairo_text_extents(cr, tooltip->entry, &extents);
1550 cairo_destroy(cr);
1551
1552 return extents;
1553}
1554
1555static int
1556window_create_tooltip(struct tooltip *tooltip)
1557{
1558 struct widget *parent = tooltip->parent;
1559 struct display *display = parent->window->display;
1560 struct window *window;
1561 const int offset_y = 27;
1562 const int margin = 3;
1563 cairo_text_extents_t extents;
1564
1565 if (tooltip->widget)
1566 return 0;
1567
1568 window = window_create_transient(display, parent->window, tooltip->x,
1569 tooltip->y + offset_y,
1570 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1571 if (!window)
1572 return -1;
1573
1574 tooltip->window = window;
1575 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1576
1577 extents = get_text_extents(tooltip);
1578 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1579 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1580
1581 return 0;
1582}
1583
1584void
1585widget_destroy_tooltip(struct widget *parent)
1586{
1587 struct tooltip *tooltip = parent->tooltip;
1588
1589 parent->tooltip_count = 0;
1590 if (!tooltip)
1591 return;
1592
1593 if (tooltip->widget) {
1594 widget_destroy(tooltip->widget);
1595 window_destroy(tooltip->window);
1596 tooltip->widget = NULL;
1597 tooltip->window = NULL;
1598 }
1599
1600 close(tooltip->tooltip_fd);
1601 free(tooltip->entry);
1602 free(tooltip);
1603 parent->tooltip = NULL;
1604}
1605
1606static void
1607tooltip_func(struct task *task, uint32_t events)
1608{
1609 struct tooltip *tooltip =
1610 container_of(task, struct tooltip, tooltip_task);
1611 uint64_t exp;
1612
Martin Olsson8df662a2012-07-08 03:03:47 +02001613 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1614 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001615 window_create_tooltip(tooltip);
1616}
1617
1618#define TOOLTIP_TIMEOUT 500
1619static int
1620tooltip_timer_reset(struct tooltip *tooltip)
1621{
1622 struct itimerspec its;
1623
1624 its.it_interval.tv_sec = 0;
1625 its.it_interval.tv_nsec = 0;
1626 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1627 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1628 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1629 fprintf(stderr, "could not set timerfd\n: %m");
1630 return -1;
1631 }
1632
1633 return 0;
1634}
1635
1636int
1637widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1638{
1639 struct tooltip *tooltip = parent->tooltip;
1640
1641 parent->tooltip_count++;
1642 if (tooltip) {
1643 tooltip->x = x;
1644 tooltip->y = y;
1645 tooltip_timer_reset(tooltip);
1646 return 0;
1647 }
1648
1649 /* the handler might be triggered too fast via input device motion, so
1650 * we need this check here to make sure tooltip is fully initialized */
1651 if (parent->tooltip_count > 1)
1652 return 0;
1653
1654 tooltip = malloc(sizeof *tooltip);
1655 if (!tooltip)
1656 return -1;
1657
1658 parent->tooltip = tooltip;
1659 tooltip->parent = parent;
1660 tooltip->widget = NULL;
1661 tooltip->window = NULL;
1662 tooltip->x = x;
1663 tooltip->y = y;
1664 tooltip->entry = strdup(entry);
1665 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1666 if (tooltip->tooltip_fd < 0) {
1667 fprintf(stderr, "could not create timerfd\n: %m");
1668 return -1;
1669 }
1670
1671 tooltip->tooltip_task.run = tooltip_func;
1672 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1673 EPOLLIN, &tooltip->tooltip_task);
1674 tooltip_timer_reset(tooltip);
1675
1676 return 0;
1677}
1678
1679static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001680workspace_manager_state(void *data,
1681 struct workspace_manager *workspace_manager,
1682 uint32_t current,
1683 uint32_t count)
1684{
1685 struct display *display = data;
1686
1687 display->workspace = current;
1688 display->workspace_count = count;
1689}
1690
1691static const struct workspace_manager_listener workspace_manager_listener = {
1692 workspace_manager_state
1693};
1694
1695static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001696frame_resize_handler(struct widget *widget,
1697 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001698{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001699 struct frame *frame = data;
1700 struct widget *child = frame->child;
1701 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001702 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001703 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001704 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001705 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001706 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001707 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001708
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001709 switch (widget->window->type) {
1710 case TYPE_FULLSCREEN:
1711 decoration_width = 0;
1712 decoration_height = 0;
1713
1714 allocation.x = 0;
1715 allocation.y = 0;
1716 allocation.width = width;
1717 allocation.height = height;
1718 opaque_margin = 0;
1719
1720 wl_list_for_each(button, &frame->buttons_list, link)
1721 button->widget->opaque = 1;
1722 break;
1723 case TYPE_MAXIMIZED:
1724 decoration_width = t->width * 2;
1725 decoration_height = t->width + t->titlebar_height;
1726
1727 allocation.x = t->width;
1728 allocation.y = t->titlebar_height;
1729 allocation.width = width - decoration_width;
1730 allocation.height = height - decoration_height;
1731
1732 opaque_margin = 0;
1733
1734 wl_list_for_each(button, &frame->buttons_list, link)
1735 button->widget->opaque = 0;
1736 break;
1737 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001738 decoration_width = (t->width + t->margin) * 2;
1739 decoration_height = t->width +
1740 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001741
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001742 allocation.x = t->width + t->margin;
1743 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001744 allocation.width = width - decoration_width;
1745 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001746
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001747 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001748
1749 wl_list_for_each(button, &frame->buttons_list, link)
1750 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001751 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001752 }
1753
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001754 widget_set_allocation(child, allocation.x, allocation.y,
1755 allocation.width, allocation.height);
1756
1757 if (child->resize_handler)
1758 child->resize_handler(child,
1759 allocation.width,
1760 allocation.height,
1761 child->user_data);
1762
Scott Moreauf7e498c2012-05-14 11:39:29 -06001763 width = child->allocation.width + decoration_width;
1764 height = child->allocation.height + decoration_height;
1765
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001766 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1767
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001768 widget->window->input_region =
1769 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001770 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001771 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001772 shadow_margin, shadow_margin,
1773 width - 2 * shadow_margin,
1774 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001775 } else {
1776 wl_region_add(widget->window->input_region,
1777 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001778 }
1779
Scott Moreauf7e498c2012-05-14 11:39:29 -06001780 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001781
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001782 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001783 wl_region_add(widget->window->opaque_region,
1784 opaque_margin, opaque_margin,
1785 widget->allocation.width - 2 * opaque_margin,
1786 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001787
1788 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001789 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1790 x_l = t->width + shadow_margin;
1791 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001792 wl_list_for_each(button, &frame->buttons_list, link) {
1793 const int button_padding = 4;
1794 w = cairo_image_surface_get_width(button->icon);
1795 h = cairo_image_surface_get_height(button->icon);
1796
1797 if (button->decoration == FRAME_BUTTON_FANCY)
1798 w += 10;
1799
1800 if (button->align == FRAME_BUTTON_LEFT) {
1801 widget_set_allocation(button->widget,
1802 x_l, y , w + 1, h + 1);
1803 x_l += w;
1804 x_l += button_padding;
1805 } else {
1806 x_r -= w;
1807 widget_set_allocation(button->widget,
1808 x_r, y , w + 1, h + 1);
1809 x_r -= button_padding;
1810 }
1811 }
1812}
1813
1814static int
1815frame_button_enter_handler(struct widget *widget,
1816 struct input *input, float x, float y, void *data)
1817{
1818 struct frame_button *frame_button = data;
1819
1820 widget_schedule_redraw(frame_button->widget);
1821 frame_button->state = FRAME_BUTTON_OVER;
1822
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001823 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001824}
1825
1826static void
1827frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1828{
1829 struct frame_button *frame_button = data;
1830
1831 widget_schedule_redraw(frame_button->widget);
1832 frame_button->state = FRAME_BUTTON_DEFAULT;
1833}
1834
1835static void
1836frame_button_button_handler(struct widget *widget,
1837 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001838 uint32_t button,
1839 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001840{
1841 struct frame_button *frame_button = data;
1842 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001843 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001844
1845 if (button != BTN_LEFT)
1846 return;
1847
1848 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001849 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001850 frame_button->state = FRAME_BUTTON_ACTIVE;
1851 widget_schedule_redraw(frame_button->widget);
1852
1853 if (frame_button->type == FRAME_BUTTON_ICON)
1854 window_show_frame_menu(window, input, time);
1855 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001856 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001857 frame_button->state = FRAME_BUTTON_DEFAULT;
1858 widget_schedule_redraw(frame_button->widget);
1859 break;
1860 }
1861
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001862 if (!was_pressed)
1863 return;
1864
Martin Minarik1998b152012-05-10 02:04:35 +02001865 switch (frame_button->type) {
1866 case FRAME_BUTTON_CLOSE:
1867 if (window->close_handler)
1868 window->close_handler(window->parent,
1869 window->user_data);
1870 else
1871 display_exit(window->display);
1872 break;
1873 case FRAME_BUTTON_MINIMIZE:
1874 fprintf(stderr,"Minimize stub\n");
1875 break;
1876 case FRAME_BUTTON_MAXIMIZE:
1877 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1878 break;
1879 default:
1880 /* Unknown operation */
1881 break;
1882 }
1883}
1884
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001885static int
1886frame_button_motion_handler(struct widget *widget,
1887 struct input *input, uint32_t time,
1888 float x, float y, void *data)
1889{
1890 struct frame_button *frame_button = data;
1891 enum frame_button_pointer previous_button_state = frame_button->state;
1892
1893 /* only track state for a pressed button */
1894 if (input->grab != widget)
1895 return CURSOR_LEFT_PTR;
1896
1897 if (x > widget->allocation.x &&
1898 x < (widget->allocation.x + widget->allocation.width) &&
1899 y > widget->allocation.y &&
1900 y < (widget->allocation.y + widget->allocation.height)) {
1901 frame_button->state = FRAME_BUTTON_ACTIVE;
1902 } else {
1903 frame_button->state = FRAME_BUTTON_DEFAULT;
1904 }
1905
1906 if (frame_button->state != previous_button_state)
1907 widget_schedule_redraw(frame_button->widget);
1908
1909 return CURSOR_LEFT_PTR;
1910}
1911
Martin Minarik1998b152012-05-10 02:04:35 +02001912static void
1913frame_button_redraw_handler(struct widget *widget, void *data)
1914{
1915 struct frame_button *frame_button = data;
1916 cairo_t *cr;
1917 int width, height, x, y;
1918 struct window *window = widget->window;
1919
1920 x = widget->allocation.x;
1921 y = widget->allocation.y;
1922 width = widget->allocation.width;
1923 height = widget->allocation.height;
1924
1925 if (!width)
1926 return;
1927 if (!height)
1928 return;
1929 if (widget->opaque)
1930 return;
1931
1932 cr = cairo_create(window->cairo_surface);
1933
1934 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1935 cairo_set_line_width(cr, 1);
1936
1937 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1938 cairo_rectangle (cr, x, y, 25, 16);
1939
1940 cairo_stroke_preserve(cr);
1941
1942 switch (frame_button->state) {
1943 case FRAME_BUTTON_DEFAULT:
1944 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1945 break;
1946 case FRAME_BUTTON_OVER:
1947 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1948 break;
1949 case FRAME_BUTTON_ACTIVE:
1950 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1951 break;
1952 }
1953
1954 cairo_fill (cr);
1955
1956 x += 4;
1957 }
1958
1959 cairo_set_source_surface(cr, frame_button->icon, x, y);
1960 cairo_paint(cr);
1961
1962 cairo_destroy(cr);
1963}
1964
1965static struct widget *
1966frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1967 enum frame_button_align align, enum frame_button_decoration style)
1968{
1969 struct frame_button *frame_button;
1970 const char *icon = data;
1971
1972 frame_button = malloc (sizeof *frame_button);
1973 memset(frame_button, 0, sizeof *frame_button);
1974
1975 frame_button->icon = cairo_image_surface_create_from_png(icon);
1976 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1977 frame_button->frame = frame;
1978 frame_button->type = type;
1979 frame_button->align = align;
1980 frame_button->decoration = style;
1981
1982 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1983
1984 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1985 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1986 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1987 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001988 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001989 return frame_button->widget;
1990}
1991
1992static void
1993frame_button_destroy(struct frame_button *frame_button)
1994{
1995 widget_destroy(frame_button->widget);
1996 wl_list_remove(&frame_button->link);
1997 cairo_surface_destroy(frame_button->icon);
1998 free(frame_button);
1999
2000 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002001}
2002
2003static void
2004frame_redraw_handler(struct widget *widget, void *data)
2005{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002006 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002007 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002008 struct theme *t = window->display->theme;
2009 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002010
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002011 if (window->type == TYPE_FULLSCREEN)
2012 return;
2013
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002014 cr = cairo_create(window->cairo_surface);
2015
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002016 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002017 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002018 if (window->type == TYPE_MAXIMIZED)
2019 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002020 theme_render_frame(t, cr, widget->allocation.width,
2021 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002022
2023 cairo_destroy(cr);
2024}
2025
2026static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002027frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002028{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002029 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002030 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002031 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002032
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002033 location = theme_get_location(t, input->sx, input->sy,
2034 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002035 frame->widget->allocation.height,
2036 window->type == TYPE_MAXIMIZED ?
2037 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002038
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002039 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002040 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002041 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002042 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002043 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002044 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002045 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002046 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002047 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002048 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002049 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002050 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002051 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002052 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002053 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002054 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002055 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002056 case THEME_LOCATION_EXTERIOR:
2057 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002058 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002059 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002060 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002061}
2062
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002063static void
2064frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002065{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002066 struct display *display;
2067
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002068 switch (index) {
2069 case 0: /* close */
2070 if (window->close_handler)
2071 window->close_handler(window->parent,
2072 window->user_data);
2073 else
2074 display_exit(window->display);
2075 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002076 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002077 display = window->display;
2078 if (display->workspace > 0)
2079 workspace_manager_move_surface(display->workspace_manager,
2080 window->surface,
2081 display->workspace - 1);
2082 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002083 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002084 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002085 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002086 workspace_manager_move_surface(display->workspace_manager,
2087 window->surface,
2088 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002089 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002090 case 3: /* fullscreen */
2091 /* we don't have a way to get out of fullscreen for now */
2092 if (window->fullscreen_handler)
2093 window->fullscreen_handler(window, window->user_data);
2094 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002095 }
2096}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002097
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002098void
2099window_show_frame_menu(struct window *window,
2100 struct input *input, uint32_t time)
2101{
2102 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002103 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002104
2105 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002106 "Close",
2107 "Move to workspace above", "Move to workspace below",
2108 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002109 };
2110
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002111 if (window->fullscreen_handler)
2112 count = ARRAY_LENGTH(entries);
2113 else
2114 count = ARRAY_LENGTH(entries) - 1;
2115
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002116 input_get_position(input, &x, &y);
2117 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002118 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002119}
2120
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002121static int
2122frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002123 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002124{
2125 return frame_get_pointer_image_for_location(data, input);
2126}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002127
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002128static int
2129frame_motion_handler(struct widget *widget,
2130 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002131 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002132{
2133 return frame_get_pointer_image_for_location(data, input);
2134}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002135
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002136static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002137frame_button_handler(struct widget *widget,
2138 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002139 uint32_t button, enum wl_pointer_button_state state,
2140 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002141
2142{
2143 struct frame *frame = data;
2144 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002145 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002146 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002147
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002148 location = theme_get_location(display->theme, input->sx, input->sy,
2149 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002150 frame->widget->allocation.height,
2151 window->type == TYPE_MAXIMIZED ?
2152 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002153
Daniel Stone4dbadb12012-05-30 16:31:51 +01002154 if (window->display->shell && button == BTN_LEFT &&
2155 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002156 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002157 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002158 if (!window->shell_surface)
2159 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002160 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002161 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002162 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002163 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002164 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002165 case THEME_LOCATION_RESIZING_TOP:
2166 case THEME_LOCATION_RESIZING_BOTTOM:
2167 case THEME_LOCATION_RESIZING_LEFT:
2168 case THEME_LOCATION_RESIZING_RIGHT:
2169 case THEME_LOCATION_RESIZING_TOP_LEFT:
2170 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2171 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2172 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002173 if (!window->shell_surface)
2174 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002175 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002176
Pekka Paalanen99436862012-11-19 17:15:59 +02002177 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002178 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002179 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002180 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002181 break;
2182 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002183 } else if (button == BTN_RIGHT &&
2184 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002185 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002186 }
2187}
2188
2189struct widget *
2190frame_create(struct window *window, void *data)
2191{
2192 struct frame *frame;
2193
2194 frame = malloc(sizeof *frame);
2195 memset(frame, 0, sizeof *frame);
2196
2197 frame->widget = window_add_widget(window, frame);
2198 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002199
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002200 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2201 widget_set_resize_handler(frame->widget, frame_resize_handler);
2202 widget_set_enter_handler(frame->widget, frame_enter_handler);
2203 widget_set_motion_handler(frame->widget, frame_motion_handler);
2204 widget_set_button_handler(frame->widget, frame_button_handler);
2205
Martin Minarik1998b152012-05-10 02:04:35 +02002206 /* Create empty list for frame buttons */
2207 wl_list_init(&frame->buttons_list);
2208
2209 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2210 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2211
2212 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2213 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2214
2215 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2216 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2217
2218 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2219 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2220
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002221 window->frame = frame;
2222
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002223 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002224}
2225
Kristian Høgsberga1627922012-06-20 17:30:03 -04002226void
2227frame_set_child_size(struct widget *widget, int child_width, int child_height)
2228{
2229 struct display *display = widget->window->display;
2230 struct theme *t = display->theme;
2231 int decoration_width, decoration_height;
2232 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002233 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002234
2235 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002236 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002237 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002238 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002239
2240 width = child_width + decoration_width;
2241 height = child_height + decoration_height;
2242 } else {
2243 width = child_width;
2244 height = child_height;
2245 }
2246
2247 window_schedule_resize(widget->window, width, height);
2248}
2249
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002250static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002251frame_destroy(struct frame *frame)
2252{
Martin Minarik1998b152012-05-10 02:04:35 +02002253 struct frame_button *button, *tmp;
2254
2255 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2256 frame_button_destroy(button);
2257
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002258 /* frame->child must be destroyed by the application */
2259 widget_destroy(frame->widget);
2260 free(frame);
2261}
2262
2263static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002264input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002265 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002266{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002267 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002268 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002269
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002270 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002271 return;
2272
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002273 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002274 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002275 widget = old;
2276 if (input->grab)
2277 widget = input->grab;
2278 if (widget->leave_handler)
2279 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002280 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002281 }
2282
2283 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002284 widget = focus;
2285 if (input->grab)
2286 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002287 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002288 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002289 cursor = widget->enter_handler(focus, input, x, y,
2290 widget->user_data);
2291 else
2292 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002293
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002294 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002295 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002296}
2297
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002298void
2299input_grab(struct input *input, struct widget *widget, uint32_t button)
2300{
2301 input->grab = widget;
2302 input->grab_button = button;
2303}
2304
2305void
2306input_ungrab(struct input *input)
2307{
2308 struct widget *widget;
2309
2310 input->grab = NULL;
2311 if (input->pointer_focus) {
2312 widget = widget_find_widget(input->pointer_focus->widget,
2313 input->sx, input->sy);
2314 input_set_focus_widget(input, widget, input->sx, input->sy);
2315 }
2316}
2317
2318static void
2319input_remove_pointer_focus(struct input *input)
2320{
2321 struct window *window = input->pointer_focus;
2322
2323 if (!window)
2324 return;
2325
2326 input_set_focus_widget(input, NULL, 0, 0);
2327
2328 input->pointer_focus = NULL;
2329 input->current_cursor = CURSOR_UNSET;
2330}
2331
2332static void
2333pointer_handle_enter(void *data, struct wl_pointer *pointer,
2334 uint32_t serial, struct wl_surface *surface,
2335 wl_fixed_t sx_w, wl_fixed_t sy_w)
2336{
2337 struct input *input = data;
2338 struct window *window;
2339 struct widget *widget;
2340 float sx = wl_fixed_to_double(sx_w);
2341 float sy = wl_fixed_to_double(sy_w);
2342
2343 if (!surface) {
2344 /* enter event for a window we've just destroyed */
2345 return;
2346 }
2347
2348 input->display->serial = serial;
2349 input->pointer_enter_serial = serial;
2350 input->pointer_focus = wl_surface_get_user_data(surface);
2351 window = input->pointer_focus;
2352
Pekka Paalanen99436862012-11-19 17:15:59 +02002353 if (window->resizing) {
2354 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002355 /* Schedule a redraw to free the pool */
2356 window_schedule_redraw(window);
2357 }
2358
2359 input->sx = sx;
2360 input->sy = sy;
2361
2362 widget = widget_find_widget(window->widget, sx, sy);
2363 input_set_focus_widget(input, widget, sx, sy);
2364}
2365
2366static void
2367pointer_handle_leave(void *data, struct wl_pointer *pointer,
2368 uint32_t serial, struct wl_surface *surface)
2369{
2370 struct input *input = data;
2371
2372 input->display->serial = serial;
2373 input_remove_pointer_focus(input);
2374}
2375
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002376static void
Daniel Stone37816df2012-05-16 18:45:18 +01002377pointer_handle_motion(void *data, struct wl_pointer *pointer,
2378 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002379{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002380 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002381 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002382 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002383 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002384 float sx = wl_fixed_to_double(sx_w);
2385 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002386
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002387 input->sx = sx;
2388 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002389
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002390 if (!window)
2391 return;
2392
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002393 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002394 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002395 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002396 }
2397
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002398 if (input->grab)
2399 widget = input->grab;
2400 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002401 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002402 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002403 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002404 input, time, sx, sy,
2405 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002406 else
2407 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002408
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002409 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002410}
2411
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002412static void
Daniel Stone37816df2012-05-16 18:45:18 +01002413pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002414 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002415{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002416 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002417 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002418 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002419
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002420 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002421 if (input->focus_widget && input->grab == NULL &&
2422 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002423 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002424
Neil Roberts6b28aad2012-01-23 19:11:18 +00002425 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002426 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002427 (*widget->button_handler)(widget,
2428 input, time,
2429 button, state,
2430 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002431
Daniel Stone4dbadb12012-05-30 16:31:51 +01002432 if (input->grab && input->grab_button == button &&
2433 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002434 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002435}
2436
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002437static void
Daniel Stone37816df2012-05-16 18:45:18 +01002438pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002439 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002440{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002441 struct input *input = data;
2442 struct widget *widget;
2443
2444 widget = input->focus_widget;
2445 if (input->grab)
2446 widget = input->grab;
2447 if (widget && widget->axis_handler)
2448 (*widget->axis_handler)(widget,
2449 input, time,
2450 axis, value,
2451 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002452}
2453
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002454static const struct wl_pointer_listener pointer_listener = {
2455 pointer_handle_enter,
2456 pointer_handle_leave,
2457 pointer_handle_motion,
2458 pointer_handle_button,
2459 pointer_handle_axis,
2460};
2461
2462static void
2463input_remove_keyboard_focus(struct input *input)
2464{
2465 struct window *window = input->keyboard_focus;
2466 struct itimerspec its;
2467
2468 its.it_interval.tv_sec = 0;
2469 its.it_interval.tv_nsec = 0;
2470 its.it_value.tv_sec = 0;
2471 its.it_value.tv_nsec = 0;
2472 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2473
2474 if (!window)
2475 return;
2476
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002477 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002478 if (window->keyboard_focus_handler)
2479 (*window->keyboard_focus_handler)(window, NULL,
2480 window->user_data);
2481
2482 input->keyboard_focus = NULL;
2483}
2484
2485static void
2486keyboard_repeat_func(struct task *task, uint32_t events)
2487{
2488 struct input *input =
2489 container_of(task, struct input, repeat_task);
2490 struct window *window = input->keyboard_focus;
2491 uint64_t exp;
2492
2493 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2494 /* If we change the timer between the fd becoming
2495 * readable and getting here, there'll be nothing to
2496 * read and we get EAGAIN. */
2497 return;
2498
2499 if (window && window->key_handler) {
2500 (*window->key_handler)(window, input, input->repeat_time,
2501 input->repeat_key, input->repeat_sym,
2502 WL_KEYBOARD_KEY_STATE_PRESSED,
2503 window->user_data);
2504 }
2505}
2506
2507static void
2508keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2509 uint32_t format, int fd, uint32_t size)
2510{
2511 struct input *input = data;
2512 char *map_str;
2513
2514 if (!data) {
2515 close(fd);
2516 return;
2517 }
2518
2519 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2520 close(fd);
2521 return;
2522 }
2523
2524 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2525 if (map_str == MAP_FAILED) {
2526 close(fd);
2527 return;
2528 }
2529
2530 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2531 map_str,
2532 XKB_KEYMAP_FORMAT_TEXT_V1,
2533 0);
2534 munmap(map_str, size);
2535 close(fd);
2536
2537 if (!input->xkb.keymap) {
2538 fprintf(stderr, "failed to compile keymap\n");
2539 return;
2540 }
2541
2542 input->xkb.state = xkb_state_new(input->xkb.keymap);
2543 if (!input->xkb.state) {
2544 fprintf(stderr, "failed to create XKB state\n");
2545 xkb_map_unref(input->xkb.keymap);
2546 input->xkb.keymap = NULL;
2547 return;
2548 }
2549
2550 input->xkb.control_mask =
2551 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2552 input->xkb.alt_mask =
2553 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2554 input->xkb.shift_mask =
2555 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2556}
2557
2558static void
2559keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2560 uint32_t serial, struct wl_surface *surface,
2561 struct wl_array *keys)
2562{
2563 struct input *input = data;
2564 struct window *window;
2565
2566 input->display->serial = serial;
2567 input->keyboard_focus = wl_surface_get_user_data(surface);
2568
2569 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002570 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002571 if (window->keyboard_focus_handler)
2572 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002573 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002574}
2575
2576static void
2577keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2578 uint32_t serial, struct wl_surface *surface)
2579{
2580 struct input *input = data;
2581
2582 input->display->serial = serial;
2583 input_remove_keyboard_focus(input);
2584}
2585
Scott Moreau210d0792012-03-22 10:47:01 -06002586static void
Daniel Stone37816df2012-05-16 18:45:18 +01002587keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002588 uint32_t serial, uint32_t time, uint32_t key,
2589 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002590{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002591 struct input *input = data;
2592 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002593 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002594 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002595 const xkb_keysym_t *syms;
2596 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002597 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002598
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002599 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002600 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002601 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002602 return;
2603
Daniel Stone97f68542012-05-30 16:32:01 +01002604 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002605
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002606 sym = XKB_KEY_NoSymbol;
2607 if (num_syms == 1)
2608 sym = syms[0];
2609
2610 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002611 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002612 window_set_maximized(window,
2613 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002614 } else if (sym == XKB_KEY_F11 &&
2615 window->fullscreen_handler &&
2616 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2617 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002618 } else if (sym == XKB_KEY_F4 &&
2619 input->modifiers == MOD_ALT_MASK &&
2620 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2621 if (window->close_handler)
2622 window->close_handler(window->parent,
2623 window->user_data);
2624 else
2625 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002626 } else if (window->key_handler) {
2627 (*window->key_handler)(window, input, time, key,
2628 sym, state, window->user_data);
2629 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002630
2631 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2632 key == input->repeat_key) {
2633 its.it_interval.tv_sec = 0;
2634 its.it_interval.tv_nsec = 0;
2635 its.it_value.tv_sec = 0;
2636 its.it_value.tv_nsec = 0;
2637 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2638 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2639 input->repeat_sym = sym;
2640 input->repeat_key = key;
2641 input->repeat_time = time;
2642 its.it_interval.tv_sec = 0;
2643 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2644 its.it_value.tv_sec = 0;
2645 its.it_value.tv_nsec = 400 * 1000 * 1000;
2646 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2647 }
2648}
2649
2650static void
Daniel Stone351eb612012-05-31 15:27:47 -04002651keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2652 uint32_t serial, uint32_t mods_depressed,
2653 uint32_t mods_latched, uint32_t mods_locked,
2654 uint32_t group)
2655{
2656 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002657 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002658
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002659 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2660 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002661 mask = xkb_state_serialize_mods(input->xkb.state,
2662 XKB_STATE_DEPRESSED |
2663 XKB_STATE_LATCHED);
2664 input->modifiers = 0;
2665 if (mask & input->xkb.control_mask)
2666 input->modifiers |= MOD_CONTROL_MASK;
2667 if (mask & input->xkb.alt_mask)
2668 input->modifiers |= MOD_ALT_MASK;
2669 if (mask & input->xkb.shift_mask)
2670 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002671}
2672
Daniel Stone37816df2012-05-16 18:45:18 +01002673static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002674 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002675 keyboard_handle_enter,
2676 keyboard_handle_leave,
2677 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002678 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002679};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002680
2681static void
Daniel Stone37816df2012-05-16 18:45:18 +01002682seat_handle_capabilities(void *data, struct wl_seat *seat,
2683 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002684{
Daniel Stone37816df2012-05-16 18:45:18 +01002685 struct input *input = data;
2686
2687 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2688 input->pointer = wl_seat_get_pointer(seat);
2689 wl_pointer_set_user_data(input->pointer, input);
2690 wl_pointer_add_listener(input->pointer, &pointer_listener,
2691 input);
2692 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2693 wl_pointer_destroy(input->pointer);
2694 input->pointer = NULL;
2695 }
2696
2697 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2698 input->keyboard = wl_seat_get_keyboard(seat);
2699 wl_keyboard_set_user_data(input->keyboard, input);
2700 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2701 input);
2702 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2703 wl_keyboard_destroy(input->keyboard);
2704 input->keyboard = NULL;
2705 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002706}
2707
Daniel Stone37816df2012-05-16 18:45:18 +01002708static const struct wl_seat_listener seat_listener = {
2709 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002710};
2711
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002712void
2713input_get_position(struct input *input, int32_t *x, int32_t *y)
2714{
2715 *x = input->sx;
2716 *y = input->sy;
2717}
2718
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002719struct display *
2720input_get_display(struct input *input)
2721{
2722 return input->display;
2723}
2724
Daniel Stone37816df2012-05-16 18:45:18 +01002725struct wl_seat *
2726input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002727{
Daniel Stone37816df2012-05-16 18:45:18 +01002728 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002729}
2730
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002731uint32_t
2732input_get_modifiers(struct input *input)
2733{
2734 return input->modifiers;
2735}
2736
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002737struct widget *
2738input_get_focus_widget(struct input *input)
2739{
2740 return input->focus_widget;
2741}
2742
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002743struct data_offer {
2744 struct wl_data_offer *offer;
2745 struct input *input;
2746 struct wl_array types;
2747 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002748
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002749 struct task io_task;
2750 int fd;
2751 data_func_t func;
2752 int32_t x, y;
2753 void *user_data;
2754};
2755
2756static void
2757data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2758{
2759 struct data_offer *offer = data;
2760 char **p;
2761
2762 p = wl_array_add(&offer->types, sizeof *p);
2763 *p = strdup(type);
2764}
2765
2766static const struct wl_data_offer_listener data_offer_listener = {
2767 data_offer_offer,
2768};
2769
2770static void
2771data_offer_destroy(struct data_offer *offer)
2772{
2773 char **p;
2774
2775 offer->refcount--;
2776 if (offer->refcount == 0) {
2777 wl_data_offer_destroy(offer->offer);
2778 for (p = offer->types.data; *p; p++)
2779 free(*p);
2780 wl_array_release(&offer->types);
2781 free(offer);
2782 }
2783}
2784
2785static void
2786data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002787 struct wl_data_device *data_device,
2788 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002789{
2790 struct data_offer *offer;
2791
2792 offer = malloc(sizeof *offer);
2793
2794 wl_array_init(&offer->types);
2795 offer->refcount = 1;
2796 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002797 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002798 wl_data_offer_add_listener(offer->offer,
2799 &data_offer_listener, offer);
2800}
2801
2802static void
2803data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002804 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002805 wl_fixed_t x_w, wl_fixed_t y_w,
2806 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002807{
2808 struct input *input = data;
2809 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002810 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002811 float x = wl_fixed_to_double(x_w);
2812 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002813 char **p;
2814
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002815 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002816 window = wl_surface_get_user_data(surface);
2817 input->pointer_focus = window;
2818
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002819 if (offer) {
2820 input->drag_offer = wl_data_offer_get_user_data(offer);
2821
2822 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2823 *p = NULL;
2824
2825 types_data = input->drag_offer->types.data;
2826 } else {
2827 input->drag_offer = NULL;
2828 types_data = NULL;
2829 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002830
2831 window = input->pointer_focus;
2832 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002833 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002834 window->user_data);
2835}
2836
2837static void
2838data_device_leave(void *data, struct wl_data_device *data_device)
2839{
2840 struct input *input = data;
2841
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002842 if (input->drag_offer) {
2843 data_offer_destroy(input->drag_offer);
2844 input->drag_offer = NULL;
2845 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002846}
2847
2848static void
2849data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002850 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002851{
2852 struct input *input = data;
2853 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002854 float x = wl_fixed_to_double(x_w);
2855 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002856 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002857
2858 input->sx = x;
2859 input->sy = y;
2860
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002861 if (input->drag_offer)
2862 types_data = input->drag_offer->types.data;
2863 else
2864 types_data = NULL;
2865
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002866 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002867 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002868 window->user_data);
2869}
2870
2871static void
2872data_device_drop(void *data, struct wl_data_device *data_device)
2873{
2874 struct input *input = data;
2875 struct window *window = input->pointer_focus;
2876
2877 if (window->drop_handler)
2878 window->drop_handler(window, input,
2879 input->sx, input->sy, window->user_data);
2880}
2881
2882static void
2883data_device_selection(void *data,
2884 struct wl_data_device *wl_data_device,
2885 struct wl_data_offer *offer)
2886{
2887 struct input *input = data;
2888 char **p;
2889
2890 if (input->selection_offer)
2891 data_offer_destroy(input->selection_offer);
2892
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002893 if (offer) {
2894 input->selection_offer = wl_data_offer_get_user_data(offer);
2895 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2896 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002897 } else {
2898 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002899 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002900}
2901
2902static const struct wl_data_device_listener data_device_listener = {
2903 data_device_data_offer,
2904 data_device_enter,
2905 data_device_leave,
2906 data_device_motion,
2907 data_device_drop,
2908 data_device_selection
2909};
2910
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002911static void
2912input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002913{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002914 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002915 struct wl_cursor *cursor;
2916 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002917
Daniel Stone80972742012-11-07 17:51:39 +11002918 if (!input->pointer)
2919 return;
2920
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002921 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002922 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002923 return;
2924
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002925 if (index >= (int) cursor->image_count) {
2926 fprintf(stderr, "cursor index out of range\n");
2927 return;
2928 }
2929
2930 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002931 buffer = wl_cursor_image_get_buffer(image);
2932 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002933 return;
2934
Kristian Høgsbergae277372012-08-01 09:41:08 -04002935 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002936 input->pointer_surface,
2937 image->hotspot_x, image->hotspot_y);
2938 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2939 wl_surface_damage(input->pointer_surface, 0, 0,
2940 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002941 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002942}
2943
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002944static const struct wl_callback_listener pointer_surface_listener;
2945
2946static void
2947pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2948 uint32_t time)
2949{
2950 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002951 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002952 int i;
2953
2954 if (callback) {
2955 assert(callback == input->cursor_frame_cb);
2956 wl_callback_destroy(callback);
2957 input->cursor_frame_cb = NULL;
2958 }
2959
Daniel Stone80972742012-11-07 17:51:39 +11002960 if (!input->pointer)
2961 return;
2962
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002963 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002964 wl_pointer_set_cursor(input->pointer,
2965 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002966 NULL, 0, 0);
2967 return;
2968 }
2969
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002970 if (input->current_cursor == CURSOR_UNSET)
2971 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002972 cursor = input->display->cursors[input->current_cursor];
2973 if (!cursor)
2974 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002975
2976 /* FIXME We don't have the current time on the first call so we set
2977 * the animation start to the time of the first frame callback. */
2978 if (time == 0)
2979 input->cursor_anim_start = 0;
2980 else if (input->cursor_anim_start == 0)
2981 input->cursor_anim_start = time;
2982
2983 if (time == 0 || input->cursor_anim_start == 0)
2984 i = 0;
2985 else
2986 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2987
Pekka Paalanenbc106382012-10-10 12:49:31 +03002988 if (cursor->image_count > 1) {
2989 input->cursor_frame_cb =
2990 wl_surface_frame(input->pointer_surface);
2991 wl_callback_add_listener(input->cursor_frame_cb,
2992 &pointer_surface_listener, input);
2993 }
2994
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002995 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002996}
2997
2998static const struct wl_callback_listener pointer_surface_listener = {
2999 pointer_surface_frame_callback
3000};
3001
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003002void
3003input_set_pointer_image(struct input *input, int pointer)
3004{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003005 int force = 0;
3006
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003007 if (!input->pointer)
3008 return;
3009
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003010 if (input->pointer_enter_serial > input->cursor_serial)
3011 force = 1;
3012
3013 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003014 return;
3015
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003016 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003017 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003018 if (!input->cursor_frame_cb)
3019 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003020 else if (force) {
3021 /* The current frame callback may be stuck if, for instance,
3022 * the set cursor request was processed by the server after
3023 * this client lost the focus. In this case the cursor surface
3024 * might not be mapped and the frame callback wouldn't ever
3025 * complete. Send a set_cursor and attach to try to map the
3026 * cursor surface again so that the callback will finish */
3027 input_set_pointer_image_index(input, 0);
3028 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003029}
3030
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003031struct wl_data_device *
3032input_get_data_device(struct input *input)
3033{
3034 return input->data_device;
3035}
3036
3037void
3038input_set_selection(struct input *input,
3039 struct wl_data_source *source, uint32_t time)
3040{
3041 wl_data_device_set_selection(input->data_device, source, time);
3042}
3043
3044void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003045input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003046{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003047 wl_data_offer_accept(input->drag_offer->offer,
3048 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003049}
3050
3051static void
3052offer_io_func(struct task *task, uint32_t events)
3053{
3054 struct data_offer *offer =
3055 container_of(task, struct data_offer, io_task);
3056 unsigned int len;
3057 char buffer[4096];
3058
3059 len = read(offer->fd, buffer, sizeof buffer);
3060 offer->func(buffer, len,
3061 offer->x, offer->y, offer->user_data);
3062
3063 if (len == 0) {
3064 close(offer->fd);
3065 data_offer_destroy(offer);
3066 }
3067}
3068
3069static void
3070data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3071 data_func_t func, void *user_data)
3072{
3073 int p[2];
3074
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003075 if (pipe2(p, O_CLOEXEC) == -1)
3076 return;
3077
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003078 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3079 close(p[1]);
3080
3081 offer->io_task.run = offer_io_func;
3082 offer->fd = p[0];
3083 offer->func = func;
3084 offer->refcount++;
3085 offer->user_data = user_data;
3086
3087 display_watch_fd(offer->input->display,
3088 offer->fd, EPOLLIN, &offer->io_task);
3089}
3090
3091void
3092input_receive_drag_data(struct input *input, const char *mime_type,
3093 data_func_t func, void *data)
3094{
3095 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3096 input->drag_offer->x = input->sx;
3097 input->drag_offer->y = input->sy;
3098}
3099
3100int
3101input_receive_selection_data(struct input *input, const char *mime_type,
3102 data_func_t func, void *data)
3103{
3104 char **p;
3105
3106 if (input->selection_offer == NULL)
3107 return -1;
3108
3109 for (p = input->selection_offer->types.data; *p; p++)
3110 if (strcmp(mime_type, *p) == 0)
3111 break;
3112
3113 if (*p == NULL)
3114 return -1;
3115
3116 data_offer_receive_data(input->selection_offer,
3117 mime_type, func, data);
3118 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003119}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003120
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003121int
3122input_receive_selection_data_to_fd(struct input *input,
3123 const char *mime_type, int fd)
3124{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003125 if (input->selection_offer)
3126 wl_data_offer_receive(input->selection_offer->offer,
3127 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003128
3129 return 0;
3130}
3131
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003132void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003133window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003134{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003135 if (!window->shell_surface)
3136 return;
3137
Daniel Stone37816df2012-05-16 18:45:18 +01003138 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003139}
3140
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003141static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003142idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003143{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003144 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003145 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003146
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003147 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003148 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003149 widget_set_allocation(widget,
3150 window->pending_allocation.x,
3151 window->pending_allocation.y,
3152 window->pending_allocation.width,
3153 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003154
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003155 if (window->input_region) {
3156 wl_region_destroy(window->input_region);
3157 window->input_region = NULL;
3158 }
3159
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003160 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003161 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003162
3163 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003164
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003165 if (widget->resize_handler)
3166 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003167 widget->allocation.width,
3168 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003169 widget->user_data);
3170
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003171 if (window->allocation.width != widget->allocation.width ||
3172 window->allocation.height != widget->allocation.height) {
3173 window->allocation = widget->allocation;
3174 window_schedule_redraw(window);
3175 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003176
3177 if (widget->opaque)
3178 wl_region_add(window->opaque_region, 0, 0,
3179 widget->allocation.width,
3180 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003181}
3182
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003183void
3184window_schedule_resize(struct window *window, int width, int height)
3185{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003186 window->pending_allocation.x = 0;
3187 window->pending_allocation.y = 0;
3188 window->pending_allocation.width = width;
3189 window->pending_allocation.height = height;
3190
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003191 if (window->min_allocation.width == 0)
3192 window->min_allocation = window->pending_allocation;
3193 if (window->pending_allocation.width < window->min_allocation.width)
3194 window->pending_allocation.width = window->min_allocation.width;
3195 if (window->pending_allocation.height < window->min_allocation.height)
3196 window->pending_allocation.height = window->min_allocation.height;
3197
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003198 window->resize_needed = 1;
3199 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003200}
3201
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003202void
3203widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3204{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003205 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003206}
3207
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003208static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003209handle_ping(void *data, struct wl_shell_surface *shell_surface,
3210 uint32_t serial)
3211{
3212 wl_shell_surface_pong(shell_surface, serial);
3213}
3214
3215static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003216handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003217 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003218{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003219 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003220
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003221 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003222 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003223}
3224
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003225static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003226menu_destroy(struct menu *menu)
3227{
3228 widget_destroy(menu->widget);
3229 window_destroy(menu->window);
3230 free(menu);
3231}
3232
3233static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003234handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3235{
3236 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003237 struct menu *menu = window->widget->user_data;
3238
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003239 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003240 * device. Or just use wl_callback. And this really needs to
3241 * be a window vfunc that the menu can set. And we need the
3242 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003243
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003244 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003245 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003246 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003247}
3248
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003249static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003250 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003251 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003252 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003253};
3254
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003255void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003256window_get_allocation(struct window *window,
3257 struct rectangle *allocation)
3258{
3259 *allocation = window->allocation;
3260}
3261
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003262static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003263widget_redraw(struct widget *widget)
3264{
3265 struct widget *child;
3266
3267 if (widget->redraw_handler)
3268 widget->redraw_handler(widget, widget->user_data);
3269 wl_list_for_each(child, &widget->child_list, link)
3270 widget_redraw(child);
3271}
3272
3273static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003274frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3275{
3276 struct window *window = data;
3277
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003278 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003279 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003280 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003281 window->redraw_scheduled = 0;
3282 if (window->redraw_needed)
3283 window_schedule_redraw(window);
3284}
3285
3286static const struct wl_callback_listener listener = {
3287 frame_callback
3288};
3289
3290static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003291idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003292{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003293 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003294
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003295 if (window->resize_needed)
3296 idle_resize(window);
3297
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003298 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003299 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003300 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003301 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003302
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003303 window->frame_cb = wl_surface_frame(window->surface);
3304 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003305 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003306}
3307
3308void
3309window_schedule_redraw(struct window *window)
3310{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003311 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003312 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003313 window->redraw_task.run = idle_redraw;
3314 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003315 window->redraw_scheduled = 1;
3316 }
3317}
3318
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003319int
3320window_is_fullscreen(struct window *window)
3321{
3322 return window->type == TYPE_FULLSCREEN;
3323}
3324
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003325void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003326window_set_fullscreen(struct window *window, int fullscreen)
3327{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003328 if (!window->display->shell)
3329 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003330
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003331 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003332 return;
3333
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003334 if (fullscreen) {
3335 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003336 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003337 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003338 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003339 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003340 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003341 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003342 wl_shell_surface_set_toplevel(window->shell_surface);
3343 window_schedule_resize(window,
3344 window->saved_allocation.width,
3345 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003346 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003347}
3348
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003349void
3350window_set_fullscreen_method(struct window *window,
3351 enum wl_shell_surface_fullscreen_method method)
3352{
3353 window->fullscreen_method = method;
3354}
3355
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003356int
3357window_is_maximized(struct window *window)
3358{
3359 return window->type == TYPE_MAXIMIZED;
3360}
3361
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003362void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003363window_set_maximized(struct window *window, int maximized)
3364{
3365 if (!window->display->shell)
3366 return;
3367
3368 if ((window->type == TYPE_MAXIMIZED) == maximized)
3369 return;
3370
3371 if (window->type == TYPE_TOPLEVEL) {
3372 window->saved_allocation = window->allocation;
3373 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3374 window->type = TYPE_MAXIMIZED;
3375 } else {
3376 wl_shell_surface_set_toplevel(window->shell_surface);
3377 window->type = TYPE_TOPLEVEL;
3378 window_schedule_resize(window,
3379 window->saved_allocation.width,
3380 window->saved_allocation.height);
3381 }
3382}
3383
3384void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003385window_set_user_data(struct window *window, void *data)
3386{
3387 window->user_data = data;
3388}
3389
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003390void *
3391window_get_user_data(struct window *window)
3392{
3393 return window->user_data;
3394}
3395
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003396void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003397window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003398 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003399{
3400 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003401}
3402
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003403void
3404window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003405 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003406{
3407 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003408}
3409
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003410void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003411window_set_data_handler(struct window *window, window_data_handler_t handler)
3412{
3413 window->data_handler = handler;
3414}
3415
3416void
3417window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3418{
3419 window->drop_handler = handler;
3420}
3421
3422void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003423window_set_close_handler(struct window *window,
3424 window_close_handler_t handler)
3425{
3426 window->close_handler = handler;
3427}
3428
3429void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003430window_set_fullscreen_handler(struct window *window,
3431 window_fullscreen_handler_t handler)
3432{
3433 window->fullscreen_handler = handler;
3434}
3435
3436void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003437window_set_output_handler(struct window *window,
3438 window_output_handler_t handler)
3439{
3440 window->output_handler = handler;
3441}
3442
3443void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003444window_set_title(struct window *window, const char *title)
3445{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003446 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003447 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003448 if (window->shell_surface)
3449 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003450}
3451
3452const char *
3453window_get_title(struct window *window)
3454{
3455 return window->title;
3456}
3457
3458void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003459window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3460{
3461 struct text_cursor_position *text_cursor_position =
3462 window->display->text_cursor_position;
3463
Scott Moreau9295ce02012-06-01 12:46:10 -06003464 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003465 return;
3466
3467 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003468 window->surface,
3469 wl_fixed_from_int(x),
3470 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003471}
3472
3473void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003474window_damage(struct window *window, int32_t x, int32_t y,
3475 int32_t width, int32_t height)
3476{
3477 wl_surface_damage(window->surface, x, y, width, height);
3478}
3479
Casey Dahlin9074db52012-04-19 22:50:09 -04003480static void
3481surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003482 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003483{
Rob Bradford7507b572012-05-15 17:55:34 +01003484 struct window *window = data;
3485 struct output *output;
3486 struct output *output_found = NULL;
3487 struct window_output *window_output;
3488
3489 wl_list_for_each(output, &window->display->output_list, link) {
3490 if (output->output == wl_output) {
3491 output_found = output;
3492 break;
3493 }
3494 }
3495
3496 if (!output_found)
3497 return;
3498
3499 window_output = malloc (sizeof *window_output);
3500 window_output->output = output_found;
3501
3502 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003503
3504 if (window->output_handler)
3505 window->output_handler(window, output_found, 1,
3506 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003507}
3508
3509static void
3510surface_leave(void *data,
3511 struct wl_surface *wl_surface, struct wl_output *output)
3512{
Rob Bradford7507b572012-05-15 17:55:34 +01003513 struct window *window = data;
3514 struct window_output *window_output;
3515 struct window_output *window_output_found = NULL;
3516
3517 wl_list_for_each(window_output, &window->window_output_list, link) {
3518 if (window_output->output->output == output) {
3519 window_output_found = window_output;
3520 break;
3521 }
3522 }
3523
3524 if (window_output_found) {
3525 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003526
3527 if (window->output_handler)
3528 window->output_handler(window, window_output->output,
3529 0, window->user_data);
3530
Rob Bradford7507b572012-05-15 17:55:34 +01003531 free(window_output_found);
3532 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003533}
3534
3535static const struct wl_surface_listener surface_listener = {
3536 surface_enter,
3537 surface_leave
3538};
3539
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003540static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003541window_create_internal(struct display *display,
3542 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003543{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003544 struct window *window;
3545
3546 window = malloc(sizeof *window);
3547 if (window == NULL)
3548 return NULL;
3549
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003550 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003551 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003552 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003553 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003554 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003555 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003556 window->shell_surface =
3557 wl_shell_get_shell_surface(display->shell,
3558 window->surface);
3559 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003560 window->allocation.x = 0;
3561 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003562 window->allocation.width = 0;
3563 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003564 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003565 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003566 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003567 window->input_region = NULL;
3568 window->opaque_region = NULL;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003569 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003570
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003571 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003572#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003573 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003574#else
3575 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3576#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003577 else
3578 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003579
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003580 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003581 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003582 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003583
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003584 if (window->shell_surface) {
3585 wl_shell_surface_set_user_data(window->shell_surface, window);
3586 wl_shell_surface_add_listener(window->shell_surface,
3587 &shell_surface_listener, window);
3588 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003589
Rob Bradford7507b572012-05-15 17:55:34 +01003590 wl_list_init (&window->window_output_list);
3591
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003592 return window;
3593}
3594
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003595struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003596window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003597{
3598 struct window *window;
3599
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003600 window = window_create_internal(display, NULL, TYPE_NONE);
3601 if (!window)
3602 return NULL;
3603
3604 return window;
3605}
3606
3607struct window *
3608window_create_custom(struct display *display)
3609{
3610 struct window *window;
3611
3612 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003613 if (!window)
3614 return NULL;
3615
3616 return window;
3617}
3618
3619struct window *
3620window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003621 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003622{
3623 struct window *window;
3624
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003625 window = window_create_internal(parent->display,
3626 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003627 if (!window)
3628 return NULL;
3629
3630 window->x = x;
3631 window->y = y;
3632
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003633 if (display->shell)
3634 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003635 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003636 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003637
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003638 return window;
3639}
3640
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003641static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003642menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003643{
3644 int next;
3645
3646 next = (sy - 8) / 20;
3647 if (menu->current != next) {
3648 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003649 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003650 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003651}
3652
3653static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003654menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003655 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003656 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003657{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003658 struct menu *menu = data;
3659
3660 if (widget == menu->widget)
3661 menu_set_item(data, y);
3662
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003663 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003664}
3665
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003666static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003667menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003668 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003669{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003670 struct menu *menu = data;
3671
3672 if (widget == menu->widget)
3673 menu_set_item(data, y);
3674
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003675 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003676}
3677
3678static void
3679menu_leave_handler(struct widget *widget, struct input *input, void *data)
3680{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003681 struct menu *menu = data;
3682
3683 if (widget == menu->widget)
3684 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003685}
3686
3687static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003688menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003689 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003690 uint32_t button, enum wl_pointer_button_state state,
3691 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003692
3693{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003694 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003695
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003696 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3697 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003698 /* Either relase after press-drag-release or
3699 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003700 menu->func(menu->window->parent,
3701 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003702 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003703 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003704 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003705 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003706 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003707}
3708
3709static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003710menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003711{
3712 cairo_t *cr;
3713 const int32_t r = 3, margin = 3;
3714 struct menu *menu = data;
3715 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003716 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003717
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003718 cr = cairo_create(window->cairo_surface);
3719 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3720 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3721 cairo_paint(cr);
3722
3723 width = window->allocation.width;
3724 height = window->allocation.height;
3725 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003726
3727 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003728 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3729 cairo_fill(cr);
3730
3731 for (i = 0; i < menu->count; i++) {
3732 if (i == menu->current) {
3733 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3734 cairo_rectangle(cr, margin, i * 20 + margin,
3735 width - 2 * margin, 20);
3736 cairo_fill(cr);
3737 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3738 cairo_move_to(cr, 10, i * 20 + 16);
3739 cairo_show_text(cr, menu->entries[i]);
3740 } else {
3741 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3742 cairo_move_to(cr, 10, i * 20 + 16);
3743 cairo_show_text(cr, menu->entries[i]);
3744 }
3745 }
3746
3747 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003748}
3749
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003750void
3751window_show_menu(struct display *display,
3752 struct input *input, uint32_t time, struct window *parent,
3753 int32_t x, int32_t y,
3754 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003755{
3756 struct window *window;
3757 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003758 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003759
3760 menu = malloc(sizeof *menu);
3761 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003762 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003763
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003764 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003765 if (!window) {
3766 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003767 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003768 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003769
3770 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003771 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003772 menu->entries = entries;
3773 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003774 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003775 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003776 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003777 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003778 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003779 window->type = TYPE_MENU;
3780 window->x = x;
3781 window->y = y;
3782
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003783 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003784 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003785 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003786 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003787 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003788
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003789 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003790 widget_set_enter_handler(menu->widget, menu_enter_handler);
3791 widget_set_leave_handler(menu->widget, menu_leave_handler);
3792 widget_set_motion_handler(menu->widget, menu_motion_handler);
3793 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003794
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003795 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003796 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003797}
3798
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003799void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003800window_set_buffer_type(struct window *window, enum window_buffer_type type)
3801{
3802 window->buffer_type = type;
3803}
3804
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003805
3806static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003807display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003808 struct wl_output *wl_output,
3809 int x, int y,
3810 int physical_width,
3811 int physical_height,
3812 int subpixel,
3813 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003814 const char *model,
3815 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003816{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003817 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003818
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003819 output->allocation.x = x;
3820 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003821 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003822}
3823
3824static void
3825display_handle_mode(void *data,
3826 struct wl_output *wl_output,
3827 uint32_t flags,
3828 int width,
3829 int height,
3830 int refresh)
3831{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003832 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003833 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003834
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003835 if (flags & WL_OUTPUT_MODE_CURRENT) {
3836 output->allocation.width = width;
3837 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003838 if (display->output_configure_handler)
3839 (*display->output_configure_handler)(
3840 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003841 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003842}
3843
3844static const struct wl_output_listener output_listener = {
3845 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003846 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003847};
3848
3849static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003850display_add_output(struct display *d, uint32_t id)
3851{
3852 struct output *output;
3853
3854 output = malloc(sizeof *output);
3855 if (output == NULL)
3856 return;
3857
3858 memset(output, 0, sizeof *output);
3859 output->display = d;
3860 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003861 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003862 wl_list_insert(d->output_list.prev, &output->link);
3863
3864 wl_output_add_listener(output->output, &output_listener, output);
3865}
3866
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003867static void
3868output_destroy(struct output *output)
3869{
3870 if (output->destroy_handler)
3871 (*output->destroy_handler)(output, output->user_data);
3872
3873 wl_output_destroy(output->output);
3874 wl_list_remove(&output->link);
3875 free(output);
3876}
3877
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003878void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003879display_set_global_handler(struct display *display,
3880 display_global_handler_t handler)
3881{
3882 struct global *global;
3883
3884 display->global_handler = handler;
3885 if (!handler)
3886 return;
3887
3888 wl_list_for_each(global, &display->global_list, link)
3889 display->global_handler(display,
3890 global->name, global->interface,
3891 global->version, display->user_data);
3892}
3893
3894void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003895display_set_output_configure_handler(struct display *display,
3896 display_output_handler_t handler)
3897{
3898 struct output *output;
3899
3900 display->output_configure_handler = handler;
3901 if (!handler)
3902 return;
3903
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003904 wl_list_for_each(output, &display->output_list, link) {
3905 if (output->allocation.width == 0 &&
3906 output->allocation.height == 0)
3907 continue;
3908
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003909 (*display->output_configure_handler)(output,
3910 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003911 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003912}
3913
3914void
3915output_set_user_data(struct output *output, void *data)
3916{
3917 output->user_data = data;
3918}
3919
3920void *
3921output_get_user_data(struct output *output)
3922{
3923 return output->user_data;
3924}
3925
3926void
3927output_set_destroy_handler(struct output *output,
3928 display_output_handler_t handler)
3929{
3930 output->destroy_handler = handler;
3931 /* FIXME: implement this, once we have way to remove outputs */
3932}
3933
3934void
Scott Moreau4e072362012-09-29 02:03:11 -06003935output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003936{
Scott Moreau4e072362012-09-29 02:03:11 -06003937 struct rectangle allocation = output->allocation;
3938
3939 switch (output->transform) {
3940 case WL_OUTPUT_TRANSFORM_90:
3941 case WL_OUTPUT_TRANSFORM_270:
3942 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3943 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3944 /* Swap width and height */
3945 allocation.width = output->allocation.height;
3946 allocation.height = output->allocation.width;
3947 break;
3948 }
3949
3950 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003951}
3952
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003953struct wl_output *
3954output_get_wl_output(struct output *output)
3955{
3956 return output->output;
3957}
3958
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003959enum wl_output_transform
3960output_get_transform(struct output *output)
3961{
3962 return output->transform;
3963}
3964
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003965static void
Daniel Stone97f68542012-05-30 16:32:01 +01003966fini_xkb(struct input *input)
3967{
3968 xkb_state_unref(input->xkb.state);
3969 xkb_map_unref(input->xkb.keymap);
3970}
3971
3972static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003973display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003974{
3975 struct input *input;
3976
3977 input = malloc(sizeof *input);
3978 if (input == NULL)
3979 return;
3980
3981 memset(input, 0, sizeof *input);
3982 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003983 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003984 input->pointer_focus = NULL;
3985 input->keyboard_focus = NULL;
3986 wl_list_insert(d->input_list.prev, &input->link);
3987
Daniel Stone37816df2012-05-16 18:45:18 +01003988 wl_seat_add_listener(input->seat, &seat_listener, input);
3989 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003990
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003991 input->data_device =
3992 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003993 input->seat);
3994 wl_data_device_add_listener(input->data_device, &data_device_listener,
3995 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003996
3997 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003998
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003999 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4000 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004001 input->repeat_task.run = keyboard_repeat_func;
4002 display_watch_fd(d, input->repeat_timer_fd,
4003 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004004}
4005
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004006static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004007input_destroy(struct input *input)
4008{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004009 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004010 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004011
4012 if (input->drag_offer)
4013 data_offer_destroy(input->drag_offer);
4014
4015 if (input->selection_offer)
4016 data_offer_destroy(input->selection_offer);
4017
4018 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004019 fini_xkb(input);
4020
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004021 wl_surface_destroy(input->pointer_surface);
4022
Pekka Paalanene1207c72011-12-16 12:02:09 +02004023 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004024 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004025 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004026 free(input);
4027}
4028
4029static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004030init_workspace_manager(struct display *d, uint32_t id)
4031{
4032 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004033 wl_registry_bind(d->registry, id,
4034 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004035 if (d->workspace_manager != NULL)
4036 workspace_manager_add_listener(d->workspace_manager,
4037 &workspace_manager_listener,
4038 d);
4039}
4040
4041static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004042registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4043 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004044{
4045 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004046 struct global *global;
4047
4048 global = malloc(sizeof *global);
4049 global->name = id;
4050 global->interface = strdup(interface);
4051 global->version = version;
4052 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004053
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004054 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004055 d->compositor = wl_registry_bind(registry, id,
4056 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004057 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004058 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004059 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004060 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004061 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004062 d->shell = wl_registry_bind(registry,
4063 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004064 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004065 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004066 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4067 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004068 wl_registry_bind(registry, id,
4069 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004070 } else if (strcmp(interface, "text_cursor_position") == 0) {
4071 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004072 wl_registry_bind(registry, id,
4073 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004074 } else if (strcmp(interface, "workspace_manager") == 0) {
4075 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004076 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004077
4078 if (d->global_handler)
4079 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004080}
4081
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004082static void
4083registry_handle_global_remove(void *data, struct wl_registry *registry,
4084 uint32_t name)
4085{
4086 struct display *d = data;
4087 struct global *global;
4088 struct global *tmp;
4089
4090 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4091 if (global->name != name)
4092 continue;
4093
4094 /* XXX: Should destroy bound globals, and call
4095 * the counterpart of display::global_handler
4096 */
4097 wl_list_remove(&global->link);
4098 free(global->interface);
4099 free(global);
4100 }
4101}
4102
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004103void *
4104display_bind(struct display *display, uint32_t name,
4105 const struct wl_interface *interface, uint32_t version)
4106{
4107 return wl_registry_bind(display->registry, name, interface, version);
4108}
4109
4110static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004111 registry_handle_global,
4112 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004113};
4114
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004115#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004116static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004117init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004118{
4119 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004120 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004121
Rob Clark6396ed32012-03-11 19:48:41 -05004122#ifdef USE_CAIRO_GLESV2
4123# define GL_BIT EGL_OPENGL_ES2_BIT
4124#else
4125# define GL_BIT EGL_OPENGL_BIT
4126#endif
4127
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004128 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004129 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004130 EGL_RED_SIZE, 1,
4131 EGL_GREEN_SIZE, 1,
4132 EGL_BLUE_SIZE, 1,
4133 EGL_ALPHA_SIZE, 1,
4134 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004135 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004136 EGL_NONE
4137 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004138
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004139#ifdef USE_CAIRO_GLESV2
4140 static const EGLint context_attribs[] = {
4141 EGL_CONTEXT_CLIENT_VERSION, 2,
4142 EGL_NONE
4143 };
4144 EGLint api = EGL_OPENGL_ES_API;
4145#else
4146 EGLint *context_attribs = NULL;
4147 EGLint api = EGL_OPENGL_API;
4148#endif
4149
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004150 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004151 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004152 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004153 return -1;
4154 }
4155
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004156 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004157 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004158 return -1;
4159 }
4160
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004161 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4162 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004163 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004164 return -1;
4165 }
4166
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004167 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004168 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004169 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004170 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004171 return -1;
4172 }
4173
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004174 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004175 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004176 return -1;
4177 }
4178
Benjamin Franzke0c991632011-09-27 21:57:31 +02004179 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4180 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004181 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004182 return -1;
4183 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004184
4185 return 0;
4186}
4187
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004188static void
4189fini_egl(struct display *display)
4190{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004191 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004192
4193 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4194 EGL_NO_CONTEXT);
4195
4196 eglTerminate(display->dpy);
4197 eglReleaseThread();
4198}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004199#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004200
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004201static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004202init_dummy_surface(struct display *display)
4203{
4204 int len;
4205 void *data;
4206
4207 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4208 data = malloc(len);
4209 display->dummy_surface =
4210 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4211 1, 1, len);
4212 display->dummy_surface_data = data;
4213}
4214
4215static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004216handle_display_data(struct task *task, uint32_t events)
4217{
4218 struct display *display =
4219 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004220 struct epoll_event ep;
4221 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004222
4223 display->display_fd_events = events;
4224
4225 if (events & EPOLLERR || events & EPOLLHUP) {
4226 display_exit(display);
4227 return;
4228 }
4229
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004230 if (events & EPOLLIN) {
4231 ret = wl_display_dispatch(display->display);
4232 if (ret == -1) {
4233 display_exit(display);
4234 return;
4235 }
4236 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004237
4238 if (events & EPOLLOUT) {
4239 ret = wl_display_flush(display->display);
4240 if (ret == 0) {
4241 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4242 ep.data.ptr = &display->display_task;
4243 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4244 display->display_fd, &ep);
4245 } else if (ret == -1 && errno != EAGAIN) {
4246 display_exit(display);
4247 return;
4248 }
4249 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004250}
4251
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004252struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004253display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004254{
4255 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004256
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004257 d = malloc(sizeof *d);
4258 if (d == NULL)
4259 return NULL;
4260
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004261 memset(d, 0, sizeof *d);
4262
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004263 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004264 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004265 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004266 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004267 return NULL;
4268 }
4269
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004270 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004271 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004272 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004273 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4274 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004275
4276 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004277 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004278 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004279 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004280
Daniel Stone97f68542012-05-30 16:32:01 +01004281 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004282 if (d->xkb_context == NULL) {
4283 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004284 return NULL;
4285 }
4286
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004287 d->workspace = 0;
4288 d->workspace_count = 1;
4289
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004290 d->registry = wl_display_get_registry(d->display);
4291 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004292
4293 if (wl_display_dispatch(d->display) < 0) {
4294 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4295 return NULL;
4296 }
4297
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004298#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004299 if (init_egl(d) < 0)
4300 fprintf(stderr, "EGL does not seem to work, "
4301 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004302#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004303
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004304 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004305
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004306 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004307
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004308 wl_list_init(&d->window_list);
4309
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004310 init_dummy_surface(d);
4311
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004312 return d;
4313}
4314
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004315static void
4316display_destroy_outputs(struct display *display)
4317{
4318 struct output *tmp;
4319 struct output *output;
4320
4321 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4322 output_destroy(output);
4323}
4324
Pekka Paalanene1207c72011-12-16 12:02:09 +02004325static void
4326display_destroy_inputs(struct display *display)
4327{
4328 struct input *tmp;
4329 struct input *input;
4330
4331 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4332 input_destroy(input);
4333}
4334
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004335void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004336display_destroy(struct display *display)
4337{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004338 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004339 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4340 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004341
4342 if (!wl_list_empty(&display->deferred_list))
4343 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4344
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004345 cairo_surface_destroy(display->dummy_surface);
4346 free(display->dummy_surface_data);
4347
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004348 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004349 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004350
Daniel Stone97f68542012-05-30 16:32:01 +01004351 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004352
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004353 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004354 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004355
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004356#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004357 if (display->argb_device)
4358 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004359#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004360
Pekka Paalanenc2052982011-12-16 11:41:32 +02004361 if (display->shell)
4362 wl_shell_destroy(display->shell);
4363
4364 if (display->shm)
4365 wl_shm_destroy(display->shm);
4366
4367 if (display->data_device_manager)
4368 wl_data_device_manager_destroy(display->data_device_manager);
4369
4370 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004371 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004372
4373 close(display->epoll_fd);
4374
U. Artie Eoff44874d92012-10-02 21:12:35 -07004375 if (!(display->display_fd_events & EPOLLERR) &&
4376 !(display->display_fd_events & EPOLLHUP))
4377 wl_display_flush(display->display);
4378
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004379 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004380 free(display);
4381}
4382
4383void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004384display_set_user_data(struct display *display, void *data)
4385{
4386 display->user_data = data;
4387}
4388
4389void *
4390display_get_user_data(struct display *display)
4391{
4392 return display->user_data;
4393}
4394
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004395struct wl_display *
4396display_get_display(struct display *display)
4397{
4398 return display->display;
4399}
4400
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004401struct output *
4402display_get_output(struct display *display)
4403{
4404 return container_of(display->output_list.next, struct output, link);
4405}
4406
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004407struct wl_compositor *
4408display_get_compositor(struct display *display)
4409{
4410 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004411}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004412
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004413uint32_t
4414display_get_serial(struct display *display)
4415{
4416 return display->serial;
4417}
4418
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004419EGLDisplay
4420display_get_egl_display(struct display *d)
4421{
4422 return d->dpy;
4423}
4424
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004425struct wl_data_source *
4426display_create_data_source(struct display *display)
4427{
4428 return wl_data_device_manager_create_data_source(display->data_device_manager);
4429}
4430
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004431EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004432display_get_argb_egl_config(struct display *d)
4433{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004434 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004435}
4436
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004437struct wl_shell *
4438display_get_shell(struct display *display)
4439{
4440 return display->shell;
4441}
4442
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004443int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004444display_acquire_window_surface(struct display *display,
4445 struct window *window,
4446 EGLContext ctx)
4447{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004448 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004449 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004450
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004451 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004452}
4453
4454void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004455display_release_window_surface(struct display *display,
4456 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004457{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004458 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004459 return;
4460
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004461 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004462}
4463
4464void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004465display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004466{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004467 wl_list_insert(&display->deferred_list, &task->link);
4468}
4469
4470void
4471display_watch_fd(struct display *display,
4472 int fd, uint32_t events, struct task *task)
4473{
4474 struct epoll_event ep;
4475
4476 ep.events = events;
4477 ep.data.ptr = task;
4478 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4479}
4480
4481void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004482display_unwatch_fd(struct display *display, int fd)
4483{
4484 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4485}
4486
4487void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004488display_run(struct display *display)
4489{
4490 struct task *task;
4491 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004492 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004493
Pekka Paalanen826d7952011-12-15 10:14:07 +02004494 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004495 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004496 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004497 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004498 struct task, link);
4499 wl_list_remove(&task->link);
4500 task->run(task, 0);
4501 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004502
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004503 wl_display_dispatch_pending(display->display);
4504
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004505 if (!display->running)
4506 break;
4507
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004508 ret = wl_display_flush(display->display);
4509 if (ret < 0 && errno == EAGAIN) {
4510 ep[0].events =
4511 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4512 ep[0].data.ptr = &display->display_task;
4513
4514 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4515 display->display_fd, &ep[0]);
4516 } else if (ret < 0) {
4517 break;
4518 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004519
4520 count = epoll_wait(display->epoll_fd,
4521 ep, ARRAY_LENGTH(ep), -1);
4522 for (i = 0; i < count; i++) {
4523 task = ep[i].data.ptr;
4524 task->run(task, ep[i].events);
4525 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004526 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004527}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004528
4529void
4530display_exit(struct display *display)
4531{
4532 display->running = 0;
4533}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004534
4535void
4536keysym_modifiers_add(struct wl_array *modifiers_map,
4537 const char *name)
4538{
4539 size_t len = strlen(name) + 1;
4540 char *p;
4541
4542 p = wl_array_add(modifiers_map, len);
4543
4544 if (p == NULL)
4545 return;
4546
4547 strncpy(p, name, len);
4548}
4549
4550static xkb_mod_index_t
4551keysym_modifiers_get_index(struct wl_array *modifiers_map,
4552 const char *name)
4553{
4554 xkb_mod_index_t index = 0;
4555 char *p = modifiers_map->data;
4556
4557 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4558 if (strcmp(p, name) == 0)
4559 return index;
4560
4561 index++;
4562 p += strlen(p) + 1;
4563 }
4564
4565 return XKB_MOD_INVALID;
4566}
4567
4568xkb_mod_mask_t
4569keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4570 const char *name)
4571{
4572 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4573
4574 if (index == XKB_MOD_INVALID)
4575 return XKB_MOD_INVALID;
4576
4577 return 1 << index;
4578}