blob: 404c92f165d77884ce4cedbef58335ccb6b8de9f [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);
810}
811
Pekka Paalanen99436862012-11-19 17:15:59 +0200812struct shm_surface {
813 struct toysurface base;
814 struct display *display;
815 struct wl_surface *surface;
816 uint32_t flags;
817 int dx, dy;
818
Pekka Paalanena4eda732012-11-19 17:16:02 +0200819 struct shm_surface_leaf leaf[2];
820 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200821};
822
823static struct shm_surface *
824to_shm_surface(struct toysurface *base)
825{
826 return container_of(base, struct shm_surface, base);
827}
828
Pekka Paalanena4eda732012-11-19 17:16:02 +0200829static void
830shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
831{
832 struct shm_surface_leaf *leaf = data;
833
834 leaf->busy = 0;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200835
836 /* If both leaves are now free, we should call
837 * shm_surface_leaf_release(shm_surface::leaf[1]).
838 * However, none of Weston's backends switch dynamically
839 * between early buffer release and requiring double-buffering,
840 * so if both leaves are free, we never used the second
841 * leaf to begin with.
842 */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200843}
844
845static const struct wl_buffer_listener shm_surface_buffer_listener = {
846 shm_surface_buffer_release
847};
848
Pekka Paalanen99436862012-11-19 17:15:59 +0200849static cairo_surface_t *
850shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200851 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200852{
Pekka Paalanenec076692012-11-30 13:37:27 +0200853 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200854 struct shm_surface *surface = to_shm_surface(base);
855 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200856 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200857
858 surface->dx = dx;
859 surface->dy = dy;
860
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200861 /* See shm_surface_buffer_release() */
862 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
863 surface->leaf[1].cairo_surface) {
864 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
865 __func__);
866 }
867
Pekka Paalanena4eda732012-11-19 17:16:02 +0200868 /* pick a free buffer from the two */
869 if (!surface->leaf[0].busy)
870 leaf = &surface->leaf[0];
871 else if (!surface->leaf[1].busy)
872 leaf = &surface->leaf[1];
873 else {
874 fprintf(stderr, "%s: both buffers are held by the server.\n",
875 __func__);
876 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200877 }
878
Pekka Paalanena4eda732012-11-19 17:16:02 +0200879 if (!resize_hint && leaf->resize_pool) {
880 cairo_surface_destroy(leaf->cairo_surface);
881 leaf->cairo_surface = NULL;
882 shm_pool_destroy(leaf->resize_pool);
883 leaf->resize_pool = NULL;
884 }
885
886 if (leaf->cairo_surface &&
887 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
888 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200889 goto out;
890
Pekka Paalanena4eda732012-11-19 17:16:02 +0200891 if (leaf->cairo_surface)
892 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200893
Pekka Paalanena4eda732012-11-19 17:16:02 +0200894 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200895 /* Create a big pool to allocate from, while continuously
896 * resizing. Mmapping a new pool in the server
897 * is relatively expensive, so reusing a pool performs
898 * better, but may temporarily reserve unneeded memory.
899 */
900 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200901 leaf->resize_pool = shm_pool_create(surface->display,
902 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200903 }
904
Pekka Paalanena4eda732012-11-19 17:16:02 +0200905 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200906 display_create_shm_surface(surface->display, &rect,
907 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200908 leaf->resize_pool,
909 &leaf->data);
910 wl_buffer_add_listener(leaf->data->buffer,
911 &shm_surface_buffer_listener, leaf);
Pekka Paalanen99436862012-11-19 17:15:59 +0200912
913out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200914 surface->current = leaf;
915
916 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200917}
918
919static void
920shm_surface_swap(struct toysurface *base,
921 struct rectangle *server_allocation)
922{
923 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200924 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200925
926 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200927 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200928 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200929 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200930
Pekka Paalanena4eda732012-11-19 17:16:02 +0200931 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200932 surface->dx, surface->dy);
933 wl_surface_damage(surface->surface, 0, 0,
934 server_allocation->width, server_allocation->height);
935 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200936
937 leaf->busy = 1;
938 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200939}
940
941static int
942shm_surface_acquire(struct toysurface *base, EGLContext ctx)
943{
944 return -1;
945}
946
947static void
948shm_surface_release(struct toysurface *base)
949{
950}
951
952static void
953shm_surface_destroy(struct toysurface *base)
954{
955 struct shm_surface *surface = to_shm_surface(base);
956
Pekka Paalanena4eda732012-11-19 17:16:02 +0200957 shm_surface_leaf_release(&surface->leaf[0]);
958 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200959
960 free(surface);
961}
962
963static struct toysurface *
964shm_surface_create(struct display *display, struct wl_surface *wl_surface,
965 uint32_t flags, struct rectangle *rectangle)
966{
967 struct shm_surface *surface;
968
969 surface = calloc(1, sizeof *surface);
970 if (!surface)
971 return NULL;
972
973 surface->base.prepare = shm_surface_prepare;
974 surface->base.swap = shm_surface_swap;
975 surface->base.acquire = shm_surface_acquire;
976 surface->base.release = shm_surface_release;
977 surface->base.destroy = shm_surface_destroy;
978
979 surface->display = display;
980 surface->surface = wl_surface;
981 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200982
983 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400984}
985
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200986/*
987 * The following correspondences between file names and cursors was copied
988 * from: https://bugs.kde.org/attachment.cgi?id=67313
989 */
990
991static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300992 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200993 "sw-resize"
994};
995
996static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300997 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200998 "se-resize"
999};
1000
1001static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001002 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001003 "s-resize"
1004};
1005
1006static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001007 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001008 "closedhand",
1009 "208530c400c041818281048008011002"
1010};
1011
1012static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001013 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001014 "default",
1015 "top_left_arrow",
1016 "left-arrow"
1017};
1018
1019static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001020 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001021 "w-resize"
1022};
1023
1024static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001025 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001026 "e-resize"
1027};
1028
1029static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001030 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001031 "nw-resize"
1032};
1033
1034static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001035 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001036 "ne-resize"
1037};
1038
1039static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001040 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001041 "n-resize"
1042};
1043
1044static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001045 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001046 "ibeam",
1047 "text"
1048};
1049
1050static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001051 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001052 "pointer",
1053 "pointing_hand",
1054 "e29285e634086352946a0e7090d73106"
1055};
1056
1057static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001058 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001059 "wait",
1060 "0426c94ea35c87780ff01dc239897213"
1061};
1062
1063struct cursor_alternatives {
1064 const char **names;
1065 size_t count;
1066};
1067
1068static const struct cursor_alternatives cursors[] = {
1069 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1070 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1071 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1072 {grabbings, ARRAY_LENGTH(grabbings)},
1073 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1074 {left_sides, ARRAY_LENGTH(left_sides)},
1075 {right_sides, ARRAY_LENGTH(right_sides)},
1076 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1077 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1078 {top_sides, ARRAY_LENGTH(top_sides)},
1079 {xterms, ARRAY_LENGTH(xterms)},
1080 {hand1s, ARRAY_LENGTH(hand1s)},
1081 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001082};
1083
1084static void
1085create_cursors(struct display *display)
1086{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001087 char *config_file;
1088 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001089 unsigned int i, j;
1090 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001091 struct config_key shell_keys[] = {
1092 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1093 };
1094 struct config_section cs[] = {
1095 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1096 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001097
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001098 config_file = config_file_path("weston.ini");
1099 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1100 free(config_file);
1101
1102 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001103 display->cursors =
1104 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1105
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001106 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001107 cursor = NULL;
1108 for (j = 0; !cursor && j < cursors[i].count; ++j)
1109 cursor = wl_cursor_theme_get_cursor(
1110 display->cursor_theme, cursors[i].names[j]);
1111
1112 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001113 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001114 cursors[i].names[0]);
1115
1116 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001117 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001118}
1119
1120static void
1121destroy_cursors(struct display *display)
1122{
1123 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001124 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001125}
1126
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001127struct wl_cursor_image *
1128display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001129{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001130 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001131
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001132 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001133}
1134
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001135static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001136window_get_resize_dx_dy(struct window *window, int *x, int *y)
1137{
1138 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1139 *x = window->server_allocation.width - window->allocation.width;
1140 else
1141 *x = 0;
1142
1143 if (window->resize_edges & WINDOW_RESIZING_TOP)
1144 *y = window->server_allocation.height -
1145 window->allocation.height;
1146 else
1147 *y = 0;
1148
1149 window->resize_edges = 0;
1150}
1151
1152static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001153window_attach_surface(struct window *window)
1154{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001155 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001156
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001157 if (window->type == TYPE_NONE) {
1158 window->type = TYPE_TOPLEVEL;
1159 if (display->shell)
1160 wl_shell_surface_set_toplevel(window->shell_surface);
1161 }
1162
Pekka Paalanen512dde82012-10-10 12:49:27 +03001163 if (window->opaque_region) {
1164 wl_surface_set_opaque_region(window->surface,
1165 window->opaque_region);
1166 wl_region_destroy(window->opaque_region);
1167 window->opaque_region = NULL;
1168 }
1169
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001170 if (window->input_region) {
1171 wl_surface_set_input_region(window->surface,
1172 window->input_region);
1173 wl_region_destroy(window->input_region);
1174 window->input_region = NULL;
1175 }
1176
Pekka Paalanen99436862012-11-19 17:15:59 +02001177 window->toysurface->swap(window->toysurface,
1178 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001179}
1180
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001181int
1182window_has_focus(struct window *window)
1183{
1184 return window->focus_count > 0;
1185}
1186
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001187static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001188window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001189{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001190 if (!window->cairo_surface)
1191 return;
1192
1193 window_attach_surface(window);
1194 cairo_surface_destroy(window->cairo_surface);
1195 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001196}
1197
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001198struct display *
1199window_get_display(struct window *window)
1200{
1201 return window->display;
1202}
1203
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001204static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001205window_create_surface(struct window *window)
1206{
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001207 struct rectangle allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001208 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001209 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001210
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001211 if (!window->transparent)
1212 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001213
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001214 switch (window->buffer_transform) {
1215 case WL_OUTPUT_TRANSFORM_90:
1216 case WL_OUTPUT_TRANSFORM_270:
1217 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1218 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1219 allocation.width = window->allocation.height;
1220 allocation.height = window->allocation.width;
1221 break;
1222 default:
1223 break;
1224 }
1225
Pekka Paalanenb3627362012-11-19 17:16:00 +02001226 if (!window->toysurface &&
1227 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1228 window->display->dpy) {
1229 window->toysurface =
1230 egl_window_surface_create(window->display,
1231 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001232 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001233 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001234
Pekka Paalanenb3627362012-11-19 17:16:00 +02001235 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001236 window->toysurface = shm_surface_create(window->display,
1237 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001238 &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001239
Pekka Paalanenec076692012-11-30 13:37:27 +02001240 if (window->resizing)
1241 flags = SURFACE_HINT_RESIZE;
1242 else
1243 flags = 0;
1244
Pekka Paalanen99436862012-11-19 17:15:59 +02001245 window_get_resize_dx_dy(window, &dx, &dy);
1246 window->cairo_surface =
1247 window->toysurface->prepare(window->toysurface, dx, dy,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001248 allocation.width,
1249 allocation.height,
Pekka Paalanenec076692012-11-30 13:37:27 +02001250 flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001251}
1252
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001253int
1254window_get_buffer_transform(struct window *window)
1255{
1256 return window->buffer_transform;
1257}
1258
1259void
1260window_set_buffer_transform(struct window *window,
1261 enum wl_output_transform transform)
1262{
1263 window->buffer_transform = transform;
1264 wl_surface_set_buffer_transform(window->surface, transform);
1265}
1266
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001267static void frame_destroy(struct frame *frame);
1268
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001269void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001270window_destroy(struct window *window)
1271{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001272 struct display *display = window->display;
1273 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001274 struct window_output *window_output;
1275 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001276
1277 if (window->redraw_scheduled)
1278 wl_list_remove(&window->redraw_task.link);
1279
1280 wl_list_for_each(input, &display->input_list, link) {
1281 if (input->pointer_focus == window)
1282 input->pointer_focus = NULL;
1283 if (input->keyboard_focus == window)
1284 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001285 if (input->focus_widget &&
1286 input->focus_widget->window == window)
1287 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001288 }
1289
Rob Bradford7507b572012-05-15 17:55:34 +01001290 wl_list_for_each_safe(window_output, window_output_tmp,
1291 &window->window_output_list, link) {
1292 free (window_output);
1293 }
1294
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001295 if (window->input_region)
1296 wl_region_destroy(window->input_region);
1297 if (window->opaque_region)
1298 wl_region_destroy(window->opaque_region);
1299
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001300 if (window->frame)
1301 frame_destroy(window->frame);
1302
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001303 if (window->shell_surface)
1304 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001305 wl_surface_destroy(window->surface);
1306 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001307
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001308 if (window->toysurface)
1309 window->toysurface->destroy(window->toysurface);
1310
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001311 if (window->frame_cb)
1312 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001313 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001314 free(window);
1315}
1316
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001317static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001318widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001319{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001320 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001321
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001322 wl_list_for_each(child, &widget->child_list, link) {
1323 target = widget_find_widget(child, x, y);
1324 if (target)
1325 return target;
1326 }
1327
1328 if (widget->allocation.x <= x &&
1329 x < widget->allocation.x + widget->allocation.width &&
1330 widget->allocation.y <= y &&
1331 y < widget->allocation.y + widget->allocation.height) {
1332 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001333 }
1334
1335 return NULL;
1336}
1337
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001338static struct widget *
1339widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001340{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001341 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001342
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001343 widget = malloc(sizeof *widget);
1344 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001345 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001346 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001347 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001348 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001349 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001350 widget->tooltip = NULL;
1351 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001352 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001353
1354 return widget;
1355}
1356
1357struct widget *
1358window_add_widget(struct window *window, void *data)
1359{
1360 window->widget = widget_create(window, data);
1361 wl_list_init(&window->widget->link);
1362
1363 return window->widget;
1364}
1365
1366struct widget *
1367widget_add_widget(struct widget *parent, void *data)
1368{
1369 struct widget *widget;
1370
1371 widget = widget_create(parent->window, data);
1372 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001373
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001374 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001375}
1376
1377void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001378widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001379{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001380 struct display *display = widget->window->display;
1381 struct input *input;
1382
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001383 if (widget->tooltip) {
1384 free(widget->tooltip);
1385 widget->tooltip = NULL;
1386 }
1387
Pekka Paalanene156fb62012-01-19 13:51:38 +02001388 wl_list_for_each(input, &display->input_list, link) {
1389 if (input->focus_widget == widget)
1390 input->focus_widget = NULL;
1391 }
1392
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001393 wl_list_remove(&widget->link);
1394 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001395}
1396
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001397void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001398widget_set_default_cursor(struct widget *widget, int cursor)
1399{
1400 widget->default_cursor = cursor;
1401}
1402
1403void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001404widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001405{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001406 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001407}
1408
1409void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001410widget_set_size(struct widget *widget, int32_t width, int32_t height)
1411{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001412 widget->allocation.width = width;
1413 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001414}
1415
1416void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001417widget_set_allocation(struct widget *widget,
1418 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001419{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001420 widget->allocation.x = x;
1421 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001422 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001423}
1424
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001425void
1426widget_set_transparent(struct widget *widget, int transparent)
1427{
1428 widget->opaque = !transparent;
1429}
1430
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001431void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001432widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001433{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001434 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001435}
1436
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001437void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001438widget_set_resize_handler(struct widget *widget,
1439 widget_resize_handler_t handler)
1440{
1441 widget->resize_handler = handler;
1442}
1443
1444void
1445widget_set_redraw_handler(struct widget *widget,
1446 widget_redraw_handler_t handler)
1447{
1448 widget->redraw_handler = handler;
1449}
1450
1451void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001452widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001453{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001454 widget->enter_handler = handler;
1455}
1456
1457void
1458widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1459{
1460 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001461}
1462
1463void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001464widget_set_motion_handler(struct widget *widget,
1465 widget_motion_handler_t handler)
1466{
1467 widget->motion_handler = handler;
1468}
1469
1470void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001471widget_set_button_handler(struct widget *widget,
1472 widget_button_handler_t handler)
1473{
1474 widget->button_handler = handler;
1475}
1476
1477void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001478widget_set_axis_handler(struct widget *widget,
1479 widget_axis_handler_t handler)
1480{
1481 widget->axis_handler = handler;
1482}
1483
1484void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001485widget_schedule_redraw(struct widget *widget)
1486{
1487 window_schedule_redraw(widget->window);
1488}
1489
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001490cairo_surface_t *
1491window_get_surface(struct window *window)
1492{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001493 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001494}
1495
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001496struct wl_surface *
1497window_get_wl_surface(struct window *window)
1498{
1499 return window->surface;
1500}
1501
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001502struct wl_shell_surface *
1503window_get_wl_shell_surface(struct window *window)
1504{
1505 return window->shell_surface;
1506}
1507
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001508static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001509tooltip_redraw_handler(struct widget *widget, void *data)
1510{
1511 cairo_t *cr;
1512 const int32_t r = 3;
1513 struct tooltip *tooltip = data;
1514 int32_t width, height;
1515 struct window *window = widget->window;
1516
1517 cr = cairo_create(window->cairo_surface);
1518 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1519 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1520 cairo_paint(cr);
1521
1522 width = window->allocation.width;
1523 height = window->allocation.height;
1524 rounded_rect(cr, 0, 0, width, height, r);
1525
1526 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1527 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1528 cairo_fill(cr);
1529
1530 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1531 cairo_move_to(cr, 10, 16);
1532 cairo_show_text(cr, tooltip->entry);
1533 cairo_destroy(cr);
1534}
1535
1536static cairo_text_extents_t
1537get_text_extents(struct tooltip *tooltip)
1538{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001539 cairo_t *cr;
1540 cairo_text_extents_t extents;
1541
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001542 /* Use the dummy_surface because tooltip's surface was not
1543 * created yet, and parent does not have a valid surface
1544 * outside repaint, either.
1545 */
1546 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001547 cairo_text_extents(cr, tooltip->entry, &extents);
1548 cairo_destroy(cr);
1549
1550 return extents;
1551}
1552
1553static int
1554window_create_tooltip(struct tooltip *tooltip)
1555{
1556 struct widget *parent = tooltip->parent;
1557 struct display *display = parent->window->display;
1558 struct window *window;
1559 const int offset_y = 27;
1560 const int margin = 3;
1561 cairo_text_extents_t extents;
1562
1563 if (tooltip->widget)
1564 return 0;
1565
1566 window = window_create_transient(display, parent->window, tooltip->x,
1567 tooltip->y + offset_y,
1568 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1569 if (!window)
1570 return -1;
1571
1572 tooltip->window = window;
1573 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1574
1575 extents = get_text_extents(tooltip);
1576 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1577 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1578
1579 return 0;
1580}
1581
1582void
1583widget_destroy_tooltip(struct widget *parent)
1584{
1585 struct tooltip *tooltip = parent->tooltip;
1586
1587 parent->tooltip_count = 0;
1588 if (!tooltip)
1589 return;
1590
1591 if (tooltip->widget) {
1592 widget_destroy(tooltip->widget);
1593 window_destroy(tooltip->window);
1594 tooltip->widget = NULL;
1595 tooltip->window = NULL;
1596 }
1597
1598 close(tooltip->tooltip_fd);
1599 free(tooltip->entry);
1600 free(tooltip);
1601 parent->tooltip = NULL;
1602}
1603
1604static void
1605tooltip_func(struct task *task, uint32_t events)
1606{
1607 struct tooltip *tooltip =
1608 container_of(task, struct tooltip, tooltip_task);
1609 uint64_t exp;
1610
Martin Olsson8df662a2012-07-08 03:03:47 +02001611 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1612 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001613 window_create_tooltip(tooltip);
1614}
1615
1616#define TOOLTIP_TIMEOUT 500
1617static int
1618tooltip_timer_reset(struct tooltip *tooltip)
1619{
1620 struct itimerspec its;
1621
1622 its.it_interval.tv_sec = 0;
1623 its.it_interval.tv_nsec = 0;
1624 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1625 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1626 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1627 fprintf(stderr, "could not set timerfd\n: %m");
1628 return -1;
1629 }
1630
1631 return 0;
1632}
1633
1634int
1635widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1636{
1637 struct tooltip *tooltip = parent->tooltip;
1638
1639 parent->tooltip_count++;
1640 if (tooltip) {
1641 tooltip->x = x;
1642 tooltip->y = y;
1643 tooltip_timer_reset(tooltip);
1644 return 0;
1645 }
1646
1647 /* the handler might be triggered too fast via input device motion, so
1648 * we need this check here to make sure tooltip is fully initialized */
1649 if (parent->tooltip_count > 1)
1650 return 0;
1651
1652 tooltip = malloc(sizeof *tooltip);
1653 if (!tooltip)
1654 return -1;
1655
1656 parent->tooltip = tooltip;
1657 tooltip->parent = parent;
1658 tooltip->widget = NULL;
1659 tooltip->window = NULL;
1660 tooltip->x = x;
1661 tooltip->y = y;
1662 tooltip->entry = strdup(entry);
1663 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1664 if (tooltip->tooltip_fd < 0) {
1665 fprintf(stderr, "could not create timerfd\n: %m");
1666 return -1;
1667 }
1668
1669 tooltip->tooltip_task.run = tooltip_func;
1670 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1671 EPOLLIN, &tooltip->tooltip_task);
1672 tooltip_timer_reset(tooltip);
1673
1674 return 0;
1675}
1676
1677static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001678workspace_manager_state(void *data,
1679 struct workspace_manager *workspace_manager,
1680 uint32_t current,
1681 uint32_t count)
1682{
1683 struct display *display = data;
1684
1685 display->workspace = current;
1686 display->workspace_count = count;
1687}
1688
1689static const struct workspace_manager_listener workspace_manager_listener = {
1690 workspace_manager_state
1691};
1692
1693static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001694frame_resize_handler(struct widget *widget,
1695 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001696{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001697 struct frame *frame = data;
1698 struct widget *child = frame->child;
1699 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001700 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001701 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001702 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001703 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001704 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001705 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001706
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001707 switch (widget->window->type) {
1708 case TYPE_FULLSCREEN:
1709 decoration_width = 0;
1710 decoration_height = 0;
1711
1712 allocation.x = 0;
1713 allocation.y = 0;
1714 allocation.width = width;
1715 allocation.height = height;
1716 opaque_margin = 0;
1717
1718 wl_list_for_each(button, &frame->buttons_list, link)
1719 button->widget->opaque = 1;
1720 break;
1721 case TYPE_MAXIMIZED:
1722 decoration_width = t->width * 2;
1723 decoration_height = t->width + t->titlebar_height;
1724
1725 allocation.x = t->width;
1726 allocation.y = t->titlebar_height;
1727 allocation.width = width - decoration_width;
1728 allocation.height = height - decoration_height;
1729
1730 opaque_margin = 0;
1731
1732 wl_list_for_each(button, &frame->buttons_list, link)
1733 button->widget->opaque = 0;
1734 break;
1735 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001736 decoration_width = (t->width + t->margin) * 2;
1737 decoration_height = t->width +
1738 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001739
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001740 allocation.x = t->width + t->margin;
1741 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001742 allocation.width = width - decoration_width;
1743 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001744
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001745 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001746
1747 wl_list_for_each(button, &frame->buttons_list, link)
1748 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001749 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001750 }
1751
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001752 widget_set_allocation(child, allocation.x, allocation.y,
1753 allocation.width, allocation.height);
1754
1755 if (child->resize_handler)
1756 child->resize_handler(child,
1757 allocation.width,
1758 allocation.height,
1759 child->user_data);
1760
Scott Moreauf7e498c2012-05-14 11:39:29 -06001761 width = child->allocation.width + decoration_width;
1762 height = child->allocation.height + decoration_height;
1763
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001764 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1765
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001766 widget->window->input_region =
1767 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001768 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001769 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001770 shadow_margin, shadow_margin,
1771 width - 2 * shadow_margin,
1772 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001773 } else {
1774 wl_region_add(widget->window->input_region,
1775 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001776 }
1777
Scott Moreauf7e498c2012-05-14 11:39:29 -06001778 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001779
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001780 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001781 wl_region_add(widget->window->opaque_region,
1782 opaque_margin, opaque_margin,
1783 widget->allocation.width - 2 * opaque_margin,
1784 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001785
1786 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001787 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1788 x_l = t->width + shadow_margin;
1789 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001790 wl_list_for_each(button, &frame->buttons_list, link) {
1791 const int button_padding = 4;
1792 w = cairo_image_surface_get_width(button->icon);
1793 h = cairo_image_surface_get_height(button->icon);
1794
1795 if (button->decoration == FRAME_BUTTON_FANCY)
1796 w += 10;
1797
1798 if (button->align == FRAME_BUTTON_LEFT) {
1799 widget_set_allocation(button->widget,
1800 x_l, y , w + 1, h + 1);
1801 x_l += w;
1802 x_l += button_padding;
1803 } else {
1804 x_r -= w;
1805 widget_set_allocation(button->widget,
1806 x_r, y , w + 1, h + 1);
1807 x_r -= button_padding;
1808 }
1809 }
1810}
1811
1812static int
1813frame_button_enter_handler(struct widget *widget,
1814 struct input *input, float x, float y, void *data)
1815{
1816 struct frame_button *frame_button = data;
1817
1818 widget_schedule_redraw(frame_button->widget);
1819 frame_button->state = FRAME_BUTTON_OVER;
1820
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001821 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001822}
1823
1824static void
1825frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1826{
1827 struct frame_button *frame_button = data;
1828
1829 widget_schedule_redraw(frame_button->widget);
1830 frame_button->state = FRAME_BUTTON_DEFAULT;
1831}
1832
1833static void
1834frame_button_button_handler(struct widget *widget,
1835 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001836 uint32_t button,
1837 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001838{
1839 struct frame_button *frame_button = data;
1840 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001841 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001842
1843 if (button != BTN_LEFT)
1844 return;
1845
1846 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001847 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001848 frame_button->state = FRAME_BUTTON_ACTIVE;
1849 widget_schedule_redraw(frame_button->widget);
1850
1851 if (frame_button->type == FRAME_BUTTON_ICON)
1852 window_show_frame_menu(window, input, time);
1853 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001854 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001855 frame_button->state = FRAME_BUTTON_DEFAULT;
1856 widget_schedule_redraw(frame_button->widget);
1857 break;
1858 }
1859
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001860 if (!was_pressed)
1861 return;
1862
Martin Minarik1998b152012-05-10 02:04:35 +02001863 switch (frame_button->type) {
1864 case FRAME_BUTTON_CLOSE:
1865 if (window->close_handler)
1866 window->close_handler(window->parent,
1867 window->user_data);
1868 else
1869 display_exit(window->display);
1870 break;
1871 case FRAME_BUTTON_MINIMIZE:
1872 fprintf(stderr,"Minimize stub\n");
1873 break;
1874 case FRAME_BUTTON_MAXIMIZE:
1875 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1876 break;
1877 default:
1878 /* Unknown operation */
1879 break;
1880 }
1881}
1882
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001883static int
1884frame_button_motion_handler(struct widget *widget,
1885 struct input *input, uint32_t time,
1886 float x, float y, void *data)
1887{
1888 struct frame_button *frame_button = data;
1889 enum frame_button_pointer previous_button_state = frame_button->state;
1890
1891 /* only track state for a pressed button */
1892 if (input->grab != widget)
1893 return CURSOR_LEFT_PTR;
1894
1895 if (x > widget->allocation.x &&
1896 x < (widget->allocation.x + widget->allocation.width) &&
1897 y > widget->allocation.y &&
1898 y < (widget->allocation.y + widget->allocation.height)) {
1899 frame_button->state = FRAME_BUTTON_ACTIVE;
1900 } else {
1901 frame_button->state = FRAME_BUTTON_DEFAULT;
1902 }
1903
1904 if (frame_button->state != previous_button_state)
1905 widget_schedule_redraw(frame_button->widget);
1906
1907 return CURSOR_LEFT_PTR;
1908}
1909
Martin Minarik1998b152012-05-10 02:04:35 +02001910static void
1911frame_button_redraw_handler(struct widget *widget, void *data)
1912{
1913 struct frame_button *frame_button = data;
1914 cairo_t *cr;
1915 int width, height, x, y;
1916 struct window *window = widget->window;
1917
1918 x = widget->allocation.x;
1919 y = widget->allocation.y;
1920 width = widget->allocation.width;
1921 height = widget->allocation.height;
1922
1923 if (!width)
1924 return;
1925 if (!height)
1926 return;
1927 if (widget->opaque)
1928 return;
1929
1930 cr = cairo_create(window->cairo_surface);
1931
1932 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1933 cairo_set_line_width(cr, 1);
1934
1935 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1936 cairo_rectangle (cr, x, y, 25, 16);
1937
1938 cairo_stroke_preserve(cr);
1939
1940 switch (frame_button->state) {
1941 case FRAME_BUTTON_DEFAULT:
1942 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1943 break;
1944 case FRAME_BUTTON_OVER:
1945 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1946 break;
1947 case FRAME_BUTTON_ACTIVE:
1948 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1949 break;
1950 }
1951
1952 cairo_fill (cr);
1953
1954 x += 4;
1955 }
1956
1957 cairo_set_source_surface(cr, frame_button->icon, x, y);
1958 cairo_paint(cr);
1959
1960 cairo_destroy(cr);
1961}
1962
1963static struct widget *
1964frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1965 enum frame_button_align align, enum frame_button_decoration style)
1966{
1967 struct frame_button *frame_button;
1968 const char *icon = data;
1969
1970 frame_button = malloc (sizeof *frame_button);
1971 memset(frame_button, 0, sizeof *frame_button);
1972
1973 frame_button->icon = cairo_image_surface_create_from_png(icon);
1974 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1975 frame_button->frame = frame;
1976 frame_button->type = type;
1977 frame_button->align = align;
1978 frame_button->decoration = style;
1979
1980 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1981
1982 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1983 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1984 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1985 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001986 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001987 return frame_button->widget;
1988}
1989
1990static void
1991frame_button_destroy(struct frame_button *frame_button)
1992{
1993 widget_destroy(frame_button->widget);
1994 wl_list_remove(&frame_button->link);
1995 cairo_surface_destroy(frame_button->icon);
1996 free(frame_button);
1997
1998 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001999}
2000
2001static void
2002frame_redraw_handler(struct widget *widget, void *data)
2003{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002004 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002005 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002006 struct theme *t = window->display->theme;
2007 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002008
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002009 if (window->type == TYPE_FULLSCREEN)
2010 return;
2011
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002012 cr = cairo_create(window->cairo_surface);
2013
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002014 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002015 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002016 if (window->type == TYPE_MAXIMIZED)
2017 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002018 theme_render_frame(t, cr, widget->allocation.width,
2019 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002020
2021 cairo_destroy(cr);
2022}
2023
2024static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002025frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002026{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002027 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002028 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002029 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002030
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002031 location = theme_get_location(t, input->sx, input->sy,
2032 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002033 frame->widget->allocation.height,
2034 window->type == TYPE_MAXIMIZED ?
2035 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002036
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002037 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002038 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002039 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002040 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002041 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002042 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002043 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002044 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002045 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002046 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002047 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002048 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002049 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002050 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002051 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002052 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002053 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002054 case THEME_LOCATION_EXTERIOR:
2055 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002056 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002057 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002058 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002059}
2060
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002061static void
2062frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002063{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002064 struct display *display;
2065
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002066 switch (index) {
2067 case 0: /* close */
2068 if (window->close_handler)
2069 window->close_handler(window->parent,
2070 window->user_data);
2071 else
2072 display_exit(window->display);
2073 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002074 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002075 display = window->display;
2076 if (display->workspace > 0)
2077 workspace_manager_move_surface(display->workspace_manager,
2078 window->surface,
2079 display->workspace - 1);
2080 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002081 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002082 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002083 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002084 workspace_manager_move_surface(display->workspace_manager,
2085 window->surface,
2086 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002087 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002088 case 3: /* fullscreen */
2089 /* we don't have a way to get out of fullscreen for now */
2090 if (window->fullscreen_handler)
2091 window->fullscreen_handler(window, window->user_data);
2092 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002093 }
2094}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002095
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002096void
2097window_show_frame_menu(struct window *window,
2098 struct input *input, uint32_t time)
2099{
2100 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002101 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002102
2103 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002104 "Close",
2105 "Move to workspace above", "Move to workspace below",
2106 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002107 };
2108
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002109 if (window->fullscreen_handler)
2110 count = ARRAY_LENGTH(entries);
2111 else
2112 count = ARRAY_LENGTH(entries) - 1;
2113
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002114 input_get_position(input, &x, &y);
2115 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002116 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002117}
2118
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002119static int
2120frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002121 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002122{
2123 return frame_get_pointer_image_for_location(data, input);
2124}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002125
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002126static int
2127frame_motion_handler(struct widget *widget,
2128 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002129 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002130{
2131 return frame_get_pointer_image_for_location(data, input);
2132}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002133
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002134static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002135frame_button_handler(struct widget *widget,
2136 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002137 uint32_t button, enum wl_pointer_button_state state,
2138 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002139
2140{
2141 struct frame *frame = data;
2142 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002143 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002144 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002145
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002146 location = theme_get_location(display->theme, input->sx, input->sy,
2147 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002148 frame->widget->allocation.height,
2149 window->type == TYPE_MAXIMIZED ?
2150 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002151
Daniel Stone4dbadb12012-05-30 16:31:51 +01002152 if (window->display->shell && button == BTN_LEFT &&
2153 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002154 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002155 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002156 if (!window->shell_surface)
2157 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002158 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002160 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002161 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002163 case THEME_LOCATION_RESIZING_TOP:
2164 case THEME_LOCATION_RESIZING_BOTTOM:
2165 case THEME_LOCATION_RESIZING_LEFT:
2166 case THEME_LOCATION_RESIZING_RIGHT:
2167 case THEME_LOCATION_RESIZING_TOP_LEFT:
2168 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2169 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2170 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002171 if (!window->shell_surface)
2172 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002173 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002174
Pekka Paalanen99436862012-11-19 17:15:59 +02002175 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002176 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002177 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002178 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002179 break;
2180 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002181 } else if (button == BTN_RIGHT &&
2182 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002183 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002184 }
2185}
2186
2187struct widget *
2188frame_create(struct window *window, void *data)
2189{
2190 struct frame *frame;
2191
2192 frame = malloc(sizeof *frame);
2193 memset(frame, 0, sizeof *frame);
2194
2195 frame->widget = window_add_widget(window, frame);
2196 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002197
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002198 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2199 widget_set_resize_handler(frame->widget, frame_resize_handler);
2200 widget_set_enter_handler(frame->widget, frame_enter_handler);
2201 widget_set_motion_handler(frame->widget, frame_motion_handler);
2202 widget_set_button_handler(frame->widget, frame_button_handler);
2203
Martin Minarik1998b152012-05-10 02:04:35 +02002204 /* Create empty list for frame buttons */
2205 wl_list_init(&frame->buttons_list);
2206
2207 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2208 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2209
2210 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2211 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2212
2213 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2214 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2215
2216 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2217 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2218
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002219 window->frame = frame;
2220
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002221 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002222}
2223
Kristian Høgsberga1627922012-06-20 17:30:03 -04002224void
2225frame_set_child_size(struct widget *widget, int child_width, int child_height)
2226{
2227 struct display *display = widget->window->display;
2228 struct theme *t = display->theme;
2229 int decoration_width, decoration_height;
2230 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002231 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002232
2233 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002234 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002235 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002236 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002237
2238 width = child_width + decoration_width;
2239 height = child_height + decoration_height;
2240 } else {
2241 width = child_width;
2242 height = child_height;
2243 }
2244
2245 window_schedule_resize(widget->window, width, height);
2246}
2247
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002248static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002249frame_destroy(struct frame *frame)
2250{
Martin Minarik1998b152012-05-10 02:04:35 +02002251 struct frame_button *button, *tmp;
2252
2253 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2254 frame_button_destroy(button);
2255
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002256 /* frame->child must be destroyed by the application */
2257 widget_destroy(frame->widget);
2258 free(frame);
2259}
2260
2261static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002262input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002263 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002264{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002265 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002266 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002267
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002268 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002269 return;
2270
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002271 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002272 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002273 widget = old;
2274 if (input->grab)
2275 widget = input->grab;
2276 if (widget->leave_handler)
2277 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002278 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002279 }
2280
2281 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002282 widget = focus;
2283 if (input->grab)
2284 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002285 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002286 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002287 cursor = widget->enter_handler(focus, input, x, y,
2288 widget->user_data);
2289 else
2290 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002291
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002292 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002293 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002294}
2295
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002296void
2297input_grab(struct input *input, struct widget *widget, uint32_t button)
2298{
2299 input->grab = widget;
2300 input->grab_button = button;
2301}
2302
2303void
2304input_ungrab(struct input *input)
2305{
2306 struct widget *widget;
2307
2308 input->grab = NULL;
2309 if (input->pointer_focus) {
2310 widget = widget_find_widget(input->pointer_focus->widget,
2311 input->sx, input->sy);
2312 input_set_focus_widget(input, widget, input->sx, input->sy);
2313 }
2314}
2315
2316static void
2317input_remove_pointer_focus(struct input *input)
2318{
2319 struct window *window = input->pointer_focus;
2320
2321 if (!window)
2322 return;
2323
2324 input_set_focus_widget(input, NULL, 0, 0);
2325
2326 input->pointer_focus = NULL;
2327 input->current_cursor = CURSOR_UNSET;
2328}
2329
2330static void
2331pointer_handle_enter(void *data, struct wl_pointer *pointer,
2332 uint32_t serial, struct wl_surface *surface,
2333 wl_fixed_t sx_w, wl_fixed_t sy_w)
2334{
2335 struct input *input = data;
2336 struct window *window;
2337 struct widget *widget;
2338 float sx = wl_fixed_to_double(sx_w);
2339 float sy = wl_fixed_to_double(sy_w);
2340
2341 if (!surface) {
2342 /* enter event for a window we've just destroyed */
2343 return;
2344 }
2345
2346 input->display->serial = serial;
2347 input->pointer_enter_serial = serial;
2348 input->pointer_focus = wl_surface_get_user_data(surface);
2349 window = input->pointer_focus;
2350
Pekka Paalanen99436862012-11-19 17:15:59 +02002351 if (window->resizing) {
2352 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002353 /* Schedule a redraw to free the pool */
2354 window_schedule_redraw(window);
2355 }
2356
2357 input->sx = sx;
2358 input->sy = sy;
2359
2360 widget = widget_find_widget(window->widget, sx, sy);
2361 input_set_focus_widget(input, widget, sx, sy);
2362}
2363
2364static void
2365pointer_handle_leave(void *data, struct wl_pointer *pointer,
2366 uint32_t serial, struct wl_surface *surface)
2367{
2368 struct input *input = data;
2369
2370 input->display->serial = serial;
2371 input_remove_pointer_focus(input);
2372}
2373
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002374static void
Daniel Stone37816df2012-05-16 18:45:18 +01002375pointer_handle_motion(void *data, struct wl_pointer *pointer,
2376 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002377{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002378 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002379 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002380 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002381 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002382 float sx = wl_fixed_to_double(sx_w);
2383 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002384
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002385 input->sx = sx;
2386 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002387
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002388 if (!window)
2389 return;
2390
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002391 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002392 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002393 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002394 }
2395
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002396 if (input->grab)
2397 widget = input->grab;
2398 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002399 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002400 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002401 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002402 input, time, sx, sy,
2403 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002404 else
2405 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002406
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002407 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002408}
2409
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002410static void
Daniel Stone37816df2012-05-16 18:45:18 +01002411pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002412 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002413{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002414 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002415 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002416 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002417
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002418 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002419 if (input->focus_widget && input->grab == NULL &&
2420 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002421 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002422
Neil Roberts6b28aad2012-01-23 19:11:18 +00002423 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002424 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002425 (*widget->button_handler)(widget,
2426 input, time,
2427 button, state,
2428 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002429
Daniel Stone4dbadb12012-05-30 16:31:51 +01002430 if (input->grab && input->grab_button == button &&
2431 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002432 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002433}
2434
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002435static void
Daniel Stone37816df2012-05-16 18:45:18 +01002436pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002437 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002438{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002439 struct input *input = data;
2440 struct widget *widget;
2441
2442 widget = input->focus_widget;
2443 if (input->grab)
2444 widget = input->grab;
2445 if (widget && widget->axis_handler)
2446 (*widget->axis_handler)(widget,
2447 input, time,
2448 axis, value,
2449 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002450}
2451
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002452static const struct wl_pointer_listener pointer_listener = {
2453 pointer_handle_enter,
2454 pointer_handle_leave,
2455 pointer_handle_motion,
2456 pointer_handle_button,
2457 pointer_handle_axis,
2458};
2459
2460static void
2461input_remove_keyboard_focus(struct input *input)
2462{
2463 struct window *window = input->keyboard_focus;
2464 struct itimerspec its;
2465
2466 its.it_interval.tv_sec = 0;
2467 its.it_interval.tv_nsec = 0;
2468 its.it_value.tv_sec = 0;
2469 its.it_value.tv_nsec = 0;
2470 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2471
2472 if (!window)
2473 return;
2474
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002475 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002476 if (window->keyboard_focus_handler)
2477 (*window->keyboard_focus_handler)(window, NULL,
2478 window->user_data);
2479
2480 input->keyboard_focus = NULL;
2481}
2482
2483static void
2484keyboard_repeat_func(struct task *task, uint32_t events)
2485{
2486 struct input *input =
2487 container_of(task, struct input, repeat_task);
2488 struct window *window = input->keyboard_focus;
2489 uint64_t exp;
2490
2491 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2492 /* If we change the timer between the fd becoming
2493 * readable and getting here, there'll be nothing to
2494 * read and we get EAGAIN. */
2495 return;
2496
2497 if (window && window->key_handler) {
2498 (*window->key_handler)(window, input, input->repeat_time,
2499 input->repeat_key, input->repeat_sym,
2500 WL_KEYBOARD_KEY_STATE_PRESSED,
2501 window->user_data);
2502 }
2503}
2504
2505static void
2506keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2507 uint32_t format, int fd, uint32_t size)
2508{
2509 struct input *input = data;
2510 char *map_str;
2511
2512 if (!data) {
2513 close(fd);
2514 return;
2515 }
2516
2517 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2518 close(fd);
2519 return;
2520 }
2521
2522 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2523 if (map_str == MAP_FAILED) {
2524 close(fd);
2525 return;
2526 }
2527
2528 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2529 map_str,
2530 XKB_KEYMAP_FORMAT_TEXT_V1,
2531 0);
2532 munmap(map_str, size);
2533 close(fd);
2534
2535 if (!input->xkb.keymap) {
2536 fprintf(stderr, "failed to compile keymap\n");
2537 return;
2538 }
2539
2540 input->xkb.state = xkb_state_new(input->xkb.keymap);
2541 if (!input->xkb.state) {
2542 fprintf(stderr, "failed to create XKB state\n");
2543 xkb_map_unref(input->xkb.keymap);
2544 input->xkb.keymap = NULL;
2545 return;
2546 }
2547
2548 input->xkb.control_mask =
2549 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2550 input->xkb.alt_mask =
2551 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2552 input->xkb.shift_mask =
2553 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2554}
2555
2556static void
2557keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2558 uint32_t serial, struct wl_surface *surface,
2559 struct wl_array *keys)
2560{
2561 struct input *input = data;
2562 struct window *window;
2563
2564 input->display->serial = serial;
2565 input->keyboard_focus = wl_surface_get_user_data(surface);
2566
2567 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002568 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002569 if (window->keyboard_focus_handler)
2570 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002571 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002572}
2573
2574static void
2575keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2576 uint32_t serial, struct wl_surface *surface)
2577{
2578 struct input *input = data;
2579
2580 input->display->serial = serial;
2581 input_remove_keyboard_focus(input);
2582}
2583
Scott Moreau210d0792012-03-22 10:47:01 -06002584static void
Daniel Stone37816df2012-05-16 18:45:18 +01002585keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002586 uint32_t serial, uint32_t time, uint32_t key,
2587 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002588{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002589 struct input *input = data;
2590 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002591 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002592 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002593 const xkb_keysym_t *syms;
2594 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002595 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002596
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002597 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002598 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002599 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002600 return;
2601
Daniel Stone97f68542012-05-30 16:32:01 +01002602 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002603
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002604 sym = XKB_KEY_NoSymbol;
2605 if (num_syms == 1)
2606 sym = syms[0];
2607
2608 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002609 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002610 window_set_maximized(window,
2611 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002612 } else if (sym == XKB_KEY_F11 &&
2613 window->fullscreen_handler &&
2614 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2615 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002616 } else if (sym == XKB_KEY_F4 &&
2617 input->modifiers == MOD_ALT_MASK &&
2618 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2619 if (window->close_handler)
2620 window->close_handler(window->parent,
2621 window->user_data);
2622 else
2623 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002624 } else if (window->key_handler) {
2625 (*window->key_handler)(window, input, time, key,
2626 sym, state, window->user_data);
2627 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002628
2629 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2630 key == input->repeat_key) {
2631 its.it_interval.tv_sec = 0;
2632 its.it_interval.tv_nsec = 0;
2633 its.it_value.tv_sec = 0;
2634 its.it_value.tv_nsec = 0;
2635 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2636 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2637 input->repeat_sym = sym;
2638 input->repeat_key = key;
2639 input->repeat_time = time;
2640 its.it_interval.tv_sec = 0;
2641 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2642 its.it_value.tv_sec = 0;
2643 its.it_value.tv_nsec = 400 * 1000 * 1000;
2644 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2645 }
2646}
2647
2648static void
Daniel Stone351eb612012-05-31 15:27:47 -04002649keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2650 uint32_t serial, uint32_t mods_depressed,
2651 uint32_t mods_latched, uint32_t mods_locked,
2652 uint32_t group)
2653{
2654 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002655 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002656
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002657 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2658 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002659 mask = xkb_state_serialize_mods(input->xkb.state,
2660 XKB_STATE_DEPRESSED |
2661 XKB_STATE_LATCHED);
2662 input->modifiers = 0;
2663 if (mask & input->xkb.control_mask)
2664 input->modifiers |= MOD_CONTROL_MASK;
2665 if (mask & input->xkb.alt_mask)
2666 input->modifiers |= MOD_ALT_MASK;
2667 if (mask & input->xkb.shift_mask)
2668 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002669}
2670
Daniel Stone37816df2012-05-16 18:45:18 +01002671static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002672 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002673 keyboard_handle_enter,
2674 keyboard_handle_leave,
2675 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002676 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002677};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002678
2679static void
Daniel Stone37816df2012-05-16 18:45:18 +01002680seat_handle_capabilities(void *data, struct wl_seat *seat,
2681 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002682{
Daniel Stone37816df2012-05-16 18:45:18 +01002683 struct input *input = data;
2684
2685 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2686 input->pointer = wl_seat_get_pointer(seat);
2687 wl_pointer_set_user_data(input->pointer, input);
2688 wl_pointer_add_listener(input->pointer, &pointer_listener,
2689 input);
2690 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2691 wl_pointer_destroy(input->pointer);
2692 input->pointer = NULL;
2693 }
2694
2695 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2696 input->keyboard = wl_seat_get_keyboard(seat);
2697 wl_keyboard_set_user_data(input->keyboard, input);
2698 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2699 input);
2700 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2701 wl_keyboard_destroy(input->keyboard);
2702 input->keyboard = NULL;
2703 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002704}
2705
Daniel Stone37816df2012-05-16 18:45:18 +01002706static const struct wl_seat_listener seat_listener = {
2707 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002708};
2709
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002710void
2711input_get_position(struct input *input, int32_t *x, int32_t *y)
2712{
2713 *x = input->sx;
2714 *y = input->sy;
2715}
2716
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002717struct display *
2718input_get_display(struct input *input)
2719{
2720 return input->display;
2721}
2722
Daniel Stone37816df2012-05-16 18:45:18 +01002723struct wl_seat *
2724input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002725{
Daniel Stone37816df2012-05-16 18:45:18 +01002726 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002727}
2728
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002729uint32_t
2730input_get_modifiers(struct input *input)
2731{
2732 return input->modifiers;
2733}
2734
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002735struct widget *
2736input_get_focus_widget(struct input *input)
2737{
2738 return input->focus_widget;
2739}
2740
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002741struct data_offer {
2742 struct wl_data_offer *offer;
2743 struct input *input;
2744 struct wl_array types;
2745 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002746
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002747 struct task io_task;
2748 int fd;
2749 data_func_t func;
2750 int32_t x, y;
2751 void *user_data;
2752};
2753
2754static void
2755data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2756{
2757 struct data_offer *offer = data;
2758 char **p;
2759
2760 p = wl_array_add(&offer->types, sizeof *p);
2761 *p = strdup(type);
2762}
2763
2764static const struct wl_data_offer_listener data_offer_listener = {
2765 data_offer_offer,
2766};
2767
2768static void
2769data_offer_destroy(struct data_offer *offer)
2770{
2771 char **p;
2772
2773 offer->refcount--;
2774 if (offer->refcount == 0) {
2775 wl_data_offer_destroy(offer->offer);
2776 for (p = offer->types.data; *p; p++)
2777 free(*p);
2778 wl_array_release(&offer->types);
2779 free(offer);
2780 }
2781}
2782
2783static void
2784data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002785 struct wl_data_device *data_device,
2786 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002787{
2788 struct data_offer *offer;
2789
2790 offer = malloc(sizeof *offer);
2791
2792 wl_array_init(&offer->types);
2793 offer->refcount = 1;
2794 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002795 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002796 wl_data_offer_add_listener(offer->offer,
2797 &data_offer_listener, offer);
2798}
2799
2800static void
2801data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002802 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002803 wl_fixed_t x_w, wl_fixed_t y_w,
2804 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002805{
2806 struct input *input = data;
2807 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002808 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002809 float x = wl_fixed_to_double(x_w);
2810 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002811 char **p;
2812
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002813 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002814 window = wl_surface_get_user_data(surface);
2815 input->pointer_focus = window;
2816
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002817 if (offer) {
2818 input->drag_offer = wl_data_offer_get_user_data(offer);
2819
2820 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2821 *p = NULL;
2822
2823 types_data = input->drag_offer->types.data;
2824 } else {
2825 input->drag_offer = NULL;
2826 types_data = NULL;
2827 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002828
2829 window = input->pointer_focus;
2830 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002831 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002832 window->user_data);
2833}
2834
2835static void
2836data_device_leave(void *data, struct wl_data_device *data_device)
2837{
2838 struct input *input = data;
2839
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002840 if (input->drag_offer) {
2841 data_offer_destroy(input->drag_offer);
2842 input->drag_offer = NULL;
2843 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002844}
2845
2846static void
2847data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002848 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002849{
2850 struct input *input = data;
2851 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002852 float x = wl_fixed_to_double(x_w);
2853 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002854 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002855
2856 input->sx = x;
2857 input->sy = y;
2858
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002859 if (input->drag_offer)
2860 types_data = input->drag_offer->types.data;
2861 else
2862 types_data = NULL;
2863
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002864 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002865 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002866 window->user_data);
2867}
2868
2869static void
2870data_device_drop(void *data, struct wl_data_device *data_device)
2871{
2872 struct input *input = data;
2873 struct window *window = input->pointer_focus;
2874
2875 if (window->drop_handler)
2876 window->drop_handler(window, input,
2877 input->sx, input->sy, window->user_data);
2878}
2879
2880static void
2881data_device_selection(void *data,
2882 struct wl_data_device *wl_data_device,
2883 struct wl_data_offer *offer)
2884{
2885 struct input *input = data;
2886 char **p;
2887
2888 if (input->selection_offer)
2889 data_offer_destroy(input->selection_offer);
2890
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002891 if (offer) {
2892 input->selection_offer = wl_data_offer_get_user_data(offer);
2893 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2894 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002895 } else {
2896 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002897 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002898}
2899
2900static const struct wl_data_device_listener data_device_listener = {
2901 data_device_data_offer,
2902 data_device_enter,
2903 data_device_leave,
2904 data_device_motion,
2905 data_device_drop,
2906 data_device_selection
2907};
2908
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002909static void
2910input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002911{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002912 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002913 struct wl_cursor *cursor;
2914 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002915
Daniel Stone80972742012-11-07 17:51:39 +11002916 if (!input->pointer)
2917 return;
2918
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002919 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002920 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002921 return;
2922
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002923 if (index >= (int) cursor->image_count) {
2924 fprintf(stderr, "cursor index out of range\n");
2925 return;
2926 }
2927
2928 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002929 buffer = wl_cursor_image_get_buffer(image);
2930 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002931 return;
2932
Kristian Høgsbergae277372012-08-01 09:41:08 -04002933 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002934 input->pointer_surface,
2935 image->hotspot_x, image->hotspot_y);
2936 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2937 wl_surface_damage(input->pointer_surface, 0, 0,
2938 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002939 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002940}
2941
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002942static const struct wl_callback_listener pointer_surface_listener;
2943
2944static void
2945pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2946 uint32_t time)
2947{
2948 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002949 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002950 int i;
2951
2952 if (callback) {
2953 assert(callback == input->cursor_frame_cb);
2954 wl_callback_destroy(callback);
2955 input->cursor_frame_cb = NULL;
2956 }
2957
Daniel Stone80972742012-11-07 17:51:39 +11002958 if (!input->pointer)
2959 return;
2960
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002961 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002962 wl_pointer_set_cursor(input->pointer,
2963 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002964 NULL, 0, 0);
2965 return;
2966 }
2967
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002968 if (input->current_cursor == CURSOR_UNSET)
2969 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002970 cursor = input->display->cursors[input->current_cursor];
2971 if (!cursor)
2972 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002973
2974 /* FIXME We don't have the current time on the first call so we set
2975 * the animation start to the time of the first frame callback. */
2976 if (time == 0)
2977 input->cursor_anim_start = 0;
2978 else if (input->cursor_anim_start == 0)
2979 input->cursor_anim_start = time;
2980
2981 if (time == 0 || input->cursor_anim_start == 0)
2982 i = 0;
2983 else
2984 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2985
Pekka Paalanenbc106382012-10-10 12:49:31 +03002986 if (cursor->image_count > 1) {
2987 input->cursor_frame_cb =
2988 wl_surface_frame(input->pointer_surface);
2989 wl_callback_add_listener(input->cursor_frame_cb,
2990 &pointer_surface_listener, input);
2991 }
2992
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002993 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002994}
2995
2996static const struct wl_callback_listener pointer_surface_listener = {
2997 pointer_surface_frame_callback
2998};
2999
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003000void
3001input_set_pointer_image(struct input *input, int pointer)
3002{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003003 int force = 0;
3004
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003005 if (!input->pointer)
3006 return;
3007
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003008 if (input->pointer_enter_serial > input->cursor_serial)
3009 force = 1;
3010
3011 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003012 return;
3013
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003014 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003015 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003016 if (!input->cursor_frame_cb)
3017 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003018 else if (force) {
3019 /* The current frame callback may be stuck if, for instance,
3020 * the set cursor request was processed by the server after
3021 * this client lost the focus. In this case the cursor surface
3022 * might not be mapped and the frame callback wouldn't ever
3023 * complete. Send a set_cursor and attach to try to map the
3024 * cursor surface again so that the callback will finish */
3025 input_set_pointer_image_index(input, 0);
3026 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003027}
3028
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003029struct wl_data_device *
3030input_get_data_device(struct input *input)
3031{
3032 return input->data_device;
3033}
3034
3035void
3036input_set_selection(struct input *input,
3037 struct wl_data_source *source, uint32_t time)
3038{
3039 wl_data_device_set_selection(input->data_device, source, time);
3040}
3041
3042void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003043input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003044{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003045 wl_data_offer_accept(input->drag_offer->offer,
3046 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003047}
3048
3049static void
3050offer_io_func(struct task *task, uint32_t events)
3051{
3052 struct data_offer *offer =
3053 container_of(task, struct data_offer, io_task);
3054 unsigned int len;
3055 char buffer[4096];
3056
3057 len = read(offer->fd, buffer, sizeof buffer);
3058 offer->func(buffer, len,
3059 offer->x, offer->y, offer->user_data);
3060
3061 if (len == 0) {
3062 close(offer->fd);
3063 data_offer_destroy(offer);
3064 }
3065}
3066
3067static void
3068data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3069 data_func_t func, void *user_data)
3070{
3071 int p[2];
3072
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003073 if (pipe2(p, O_CLOEXEC) == -1)
3074 return;
3075
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003076 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3077 close(p[1]);
3078
3079 offer->io_task.run = offer_io_func;
3080 offer->fd = p[0];
3081 offer->func = func;
3082 offer->refcount++;
3083 offer->user_data = user_data;
3084
3085 display_watch_fd(offer->input->display,
3086 offer->fd, EPOLLIN, &offer->io_task);
3087}
3088
3089void
3090input_receive_drag_data(struct input *input, const char *mime_type,
3091 data_func_t func, void *data)
3092{
3093 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3094 input->drag_offer->x = input->sx;
3095 input->drag_offer->y = input->sy;
3096}
3097
3098int
3099input_receive_selection_data(struct input *input, const char *mime_type,
3100 data_func_t func, void *data)
3101{
3102 char **p;
3103
3104 if (input->selection_offer == NULL)
3105 return -1;
3106
3107 for (p = input->selection_offer->types.data; *p; p++)
3108 if (strcmp(mime_type, *p) == 0)
3109 break;
3110
3111 if (*p == NULL)
3112 return -1;
3113
3114 data_offer_receive_data(input->selection_offer,
3115 mime_type, func, data);
3116 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003117}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003118
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003119int
3120input_receive_selection_data_to_fd(struct input *input,
3121 const char *mime_type, int fd)
3122{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003123 if (input->selection_offer)
3124 wl_data_offer_receive(input->selection_offer->offer,
3125 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003126
3127 return 0;
3128}
3129
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003130void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003131window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003132{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003133 if (!window->shell_surface)
3134 return;
3135
Daniel Stone37816df2012-05-16 18:45:18 +01003136 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003137}
3138
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003139static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003140idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003141{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003142 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003143 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003144
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003145 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003146 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003147 widget_set_allocation(widget,
3148 window->pending_allocation.x,
3149 window->pending_allocation.y,
3150 window->pending_allocation.width,
3151 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003152
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003153 if (window->input_region) {
3154 wl_region_destroy(window->input_region);
3155 window->input_region = NULL;
3156 }
3157
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003158 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003159 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003160
3161 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003162
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003163 if (widget->resize_handler)
3164 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003165 widget->allocation.width,
3166 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003167 widget->user_data);
3168
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003169 if (window->allocation.width != widget->allocation.width ||
3170 window->allocation.height != widget->allocation.height) {
3171 window->allocation = widget->allocation;
3172 window_schedule_redraw(window);
3173 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003174
3175 if (widget->opaque)
3176 wl_region_add(window->opaque_region, 0, 0,
3177 widget->allocation.width,
3178 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003179}
3180
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003181void
3182window_schedule_resize(struct window *window, int width, int height)
3183{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003184 window->pending_allocation.x = 0;
3185 window->pending_allocation.y = 0;
3186 window->pending_allocation.width = width;
3187 window->pending_allocation.height = height;
3188
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003189 if (window->min_allocation.width == 0)
3190 window->min_allocation = window->pending_allocation;
3191 if (window->pending_allocation.width < window->min_allocation.width)
3192 window->pending_allocation.width = window->min_allocation.width;
3193 if (window->pending_allocation.height < window->min_allocation.height)
3194 window->pending_allocation.height = window->min_allocation.height;
3195
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003196 window->resize_needed = 1;
3197 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003198}
3199
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003200void
3201widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3202{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003203 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003204}
3205
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003206static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003207handle_ping(void *data, struct wl_shell_surface *shell_surface,
3208 uint32_t serial)
3209{
3210 wl_shell_surface_pong(shell_surface, serial);
3211}
3212
3213static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003214handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003215 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003216{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003217 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003218
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003219 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003220 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003221}
3222
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003223static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003224menu_destroy(struct menu *menu)
3225{
3226 widget_destroy(menu->widget);
3227 window_destroy(menu->window);
3228 free(menu);
3229}
3230
3231static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003232handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3233{
3234 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003235 struct menu *menu = window->widget->user_data;
3236
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003237 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003238 * device. Or just use wl_callback. And this really needs to
3239 * be a window vfunc that the menu can set. And we need the
3240 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003241
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003242 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003243 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003244 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003245}
3246
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003247static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003248 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003249 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003250 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003251};
3252
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003253void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003254window_get_allocation(struct window *window,
3255 struct rectangle *allocation)
3256{
3257 *allocation = window->allocation;
3258}
3259
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003260static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003261widget_redraw(struct widget *widget)
3262{
3263 struct widget *child;
3264
3265 if (widget->redraw_handler)
3266 widget->redraw_handler(widget, widget->user_data);
3267 wl_list_for_each(child, &widget->child_list, link)
3268 widget_redraw(child);
3269}
3270
3271static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003272frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3273{
3274 struct window *window = data;
3275
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003276 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003277 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003278 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003279 window->redraw_scheduled = 0;
3280 if (window->redraw_needed)
3281 window_schedule_redraw(window);
3282}
3283
3284static const struct wl_callback_listener listener = {
3285 frame_callback
3286};
3287
3288static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003289idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003290{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003291 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003292
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003293 if (window->resize_needed)
3294 idle_resize(window);
3295
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003296 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003297 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003298 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003299 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003300
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003301 window->frame_cb = wl_surface_frame(window->surface);
3302 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003303 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003304}
3305
3306void
3307window_schedule_redraw(struct window *window)
3308{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003309 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003310 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003311 window->redraw_task.run = idle_redraw;
3312 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003313 window->redraw_scheduled = 1;
3314 }
3315}
3316
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003317int
3318window_is_fullscreen(struct window *window)
3319{
3320 return window->type == TYPE_FULLSCREEN;
3321}
3322
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003323void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003324window_set_fullscreen(struct window *window, int fullscreen)
3325{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003326 if (!window->display->shell)
3327 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003328
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003329 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003330 return;
3331
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003332 if (fullscreen) {
3333 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003334 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003335 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003336 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003337 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003338 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003339 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003340 wl_shell_surface_set_toplevel(window->shell_surface);
3341 window_schedule_resize(window,
3342 window->saved_allocation.width,
3343 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003344 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003345}
3346
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003347void
3348window_set_fullscreen_method(struct window *window,
3349 enum wl_shell_surface_fullscreen_method method)
3350{
3351 window->fullscreen_method = method;
3352}
3353
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003354int
3355window_is_maximized(struct window *window)
3356{
3357 return window->type == TYPE_MAXIMIZED;
3358}
3359
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003360void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003361window_set_maximized(struct window *window, int maximized)
3362{
3363 if (!window->display->shell)
3364 return;
3365
3366 if ((window->type == TYPE_MAXIMIZED) == maximized)
3367 return;
3368
3369 if (window->type == TYPE_TOPLEVEL) {
3370 window->saved_allocation = window->allocation;
3371 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3372 window->type = TYPE_MAXIMIZED;
3373 } else {
3374 wl_shell_surface_set_toplevel(window->shell_surface);
3375 window->type = TYPE_TOPLEVEL;
3376 window_schedule_resize(window,
3377 window->saved_allocation.width,
3378 window->saved_allocation.height);
3379 }
3380}
3381
3382void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003383window_set_user_data(struct window *window, void *data)
3384{
3385 window->user_data = data;
3386}
3387
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003388void *
3389window_get_user_data(struct window *window)
3390{
3391 return window->user_data;
3392}
3393
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003394void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003395window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003396 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003397{
3398 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003399}
3400
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003401void
3402window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003403 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003404{
3405 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003406}
3407
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003408void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003409window_set_data_handler(struct window *window, window_data_handler_t handler)
3410{
3411 window->data_handler = handler;
3412}
3413
3414void
3415window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3416{
3417 window->drop_handler = handler;
3418}
3419
3420void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003421window_set_close_handler(struct window *window,
3422 window_close_handler_t handler)
3423{
3424 window->close_handler = handler;
3425}
3426
3427void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003428window_set_fullscreen_handler(struct window *window,
3429 window_fullscreen_handler_t handler)
3430{
3431 window->fullscreen_handler = handler;
3432}
3433
3434void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003435window_set_output_handler(struct window *window,
3436 window_output_handler_t handler)
3437{
3438 window->output_handler = handler;
3439}
3440
3441void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003442window_set_title(struct window *window, const char *title)
3443{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003444 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003445 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003446 if (window->shell_surface)
3447 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003448}
3449
3450const char *
3451window_get_title(struct window *window)
3452{
3453 return window->title;
3454}
3455
3456void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003457window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3458{
3459 struct text_cursor_position *text_cursor_position =
3460 window->display->text_cursor_position;
3461
Scott Moreau9295ce02012-06-01 12:46:10 -06003462 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003463 return;
3464
3465 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003466 window->surface,
3467 wl_fixed_from_int(x),
3468 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003469}
3470
3471void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003472window_damage(struct window *window, int32_t x, int32_t y,
3473 int32_t width, int32_t height)
3474{
3475 wl_surface_damage(window->surface, x, y, width, height);
3476}
3477
Casey Dahlin9074db52012-04-19 22:50:09 -04003478static void
3479surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003480 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003481{
Rob Bradford7507b572012-05-15 17:55:34 +01003482 struct window *window = data;
3483 struct output *output;
3484 struct output *output_found = NULL;
3485 struct window_output *window_output;
3486
3487 wl_list_for_each(output, &window->display->output_list, link) {
3488 if (output->output == wl_output) {
3489 output_found = output;
3490 break;
3491 }
3492 }
3493
3494 if (!output_found)
3495 return;
3496
3497 window_output = malloc (sizeof *window_output);
3498 window_output->output = output_found;
3499
3500 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003501
3502 if (window->output_handler)
3503 window->output_handler(window, output_found, 1,
3504 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003505}
3506
3507static void
3508surface_leave(void *data,
3509 struct wl_surface *wl_surface, struct wl_output *output)
3510{
Rob Bradford7507b572012-05-15 17:55:34 +01003511 struct window *window = data;
3512 struct window_output *window_output;
3513 struct window_output *window_output_found = NULL;
3514
3515 wl_list_for_each(window_output, &window->window_output_list, link) {
3516 if (window_output->output->output == output) {
3517 window_output_found = window_output;
3518 break;
3519 }
3520 }
3521
3522 if (window_output_found) {
3523 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003524
3525 if (window->output_handler)
3526 window->output_handler(window, window_output->output,
3527 0, window->user_data);
3528
Rob Bradford7507b572012-05-15 17:55:34 +01003529 free(window_output_found);
3530 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003531}
3532
3533static const struct wl_surface_listener surface_listener = {
3534 surface_enter,
3535 surface_leave
3536};
3537
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003538static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003539window_create_internal(struct display *display,
3540 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003541{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003542 struct window *window;
3543
3544 window = malloc(sizeof *window);
3545 if (window == NULL)
3546 return NULL;
3547
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003548 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003549 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003550 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003551 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003552 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003553 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003554 window->shell_surface =
3555 wl_shell_get_shell_surface(display->shell,
3556 window->surface);
3557 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003558 window->allocation.x = 0;
3559 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003560 window->allocation.width = 0;
3561 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003562 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003563 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003564 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003565 window->input_region = NULL;
3566 window->opaque_region = NULL;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003567 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003568
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003569 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003570#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003571 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003572#else
3573 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3574#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003575 else
3576 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003577
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003578 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003579 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003580 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003581
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003582 if (window->shell_surface) {
3583 wl_shell_surface_set_user_data(window->shell_surface, window);
3584 wl_shell_surface_add_listener(window->shell_surface,
3585 &shell_surface_listener, window);
3586 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003587
Rob Bradford7507b572012-05-15 17:55:34 +01003588 wl_list_init (&window->window_output_list);
3589
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003590 return window;
3591}
3592
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003593struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003594window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003595{
3596 struct window *window;
3597
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003598 window = window_create_internal(display, NULL, TYPE_NONE);
3599 if (!window)
3600 return NULL;
3601
3602 return window;
3603}
3604
3605struct window *
3606window_create_custom(struct display *display)
3607{
3608 struct window *window;
3609
3610 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003611 if (!window)
3612 return NULL;
3613
3614 return window;
3615}
3616
3617struct window *
3618window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003619 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003620{
3621 struct window *window;
3622
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003623 window = window_create_internal(parent->display,
3624 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003625 if (!window)
3626 return NULL;
3627
3628 window->x = x;
3629 window->y = y;
3630
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003631 if (display->shell)
3632 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003633 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003634 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003635
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003636 return window;
3637}
3638
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003639static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003640menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003641{
3642 int next;
3643
3644 next = (sy - 8) / 20;
3645 if (menu->current != next) {
3646 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003647 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003648 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003649}
3650
3651static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003652menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003653 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003654 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003655{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003656 struct menu *menu = data;
3657
3658 if (widget == menu->widget)
3659 menu_set_item(data, y);
3660
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003661 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003662}
3663
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003664static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003665menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003666 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003667{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003668 struct menu *menu = data;
3669
3670 if (widget == menu->widget)
3671 menu_set_item(data, y);
3672
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003673 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003674}
3675
3676static void
3677menu_leave_handler(struct widget *widget, struct input *input, void *data)
3678{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003679 struct menu *menu = data;
3680
3681 if (widget == menu->widget)
3682 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003683}
3684
3685static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003686menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003687 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003688 uint32_t button, enum wl_pointer_button_state state,
3689 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003690
3691{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003692 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003693
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003694 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3695 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003696 /* Either relase after press-drag-release or
3697 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003698 menu->func(menu->window->parent,
3699 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003700 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003701 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003702 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003703 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003704 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003705}
3706
3707static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003708menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003709{
3710 cairo_t *cr;
3711 const int32_t r = 3, margin = 3;
3712 struct menu *menu = data;
3713 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003714 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003715
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003716 cr = cairo_create(window->cairo_surface);
3717 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3718 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3719 cairo_paint(cr);
3720
3721 width = window->allocation.width;
3722 height = window->allocation.height;
3723 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003724
3725 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003726 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3727 cairo_fill(cr);
3728
3729 for (i = 0; i < menu->count; i++) {
3730 if (i == menu->current) {
3731 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3732 cairo_rectangle(cr, margin, i * 20 + margin,
3733 width - 2 * margin, 20);
3734 cairo_fill(cr);
3735 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3736 cairo_move_to(cr, 10, i * 20 + 16);
3737 cairo_show_text(cr, menu->entries[i]);
3738 } else {
3739 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3740 cairo_move_to(cr, 10, i * 20 + 16);
3741 cairo_show_text(cr, menu->entries[i]);
3742 }
3743 }
3744
3745 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003746}
3747
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003748void
3749window_show_menu(struct display *display,
3750 struct input *input, uint32_t time, struct window *parent,
3751 int32_t x, int32_t y,
3752 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003753{
3754 struct window *window;
3755 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003756 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003757
3758 menu = malloc(sizeof *menu);
3759 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003760 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003761
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003762 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003763 if (!window) {
3764 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003765 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003766 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003767
3768 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003769 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003770 menu->entries = entries;
3771 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003772 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003773 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003774 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003775 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003776 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003777 window->type = TYPE_MENU;
3778 window->x = x;
3779 window->y = y;
3780
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003781 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003782 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003783 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003784 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003785 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003786
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003787 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003788 widget_set_enter_handler(menu->widget, menu_enter_handler);
3789 widget_set_leave_handler(menu->widget, menu_leave_handler);
3790 widget_set_motion_handler(menu->widget, menu_motion_handler);
3791 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003792
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003793 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003794 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003795}
3796
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003797void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003798window_set_buffer_type(struct window *window, enum window_buffer_type type)
3799{
3800 window->buffer_type = type;
3801}
3802
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003803
3804static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003805display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003806 struct wl_output *wl_output,
3807 int x, int y,
3808 int physical_width,
3809 int physical_height,
3810 int subpixel,
3811 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003812 const char *model,
3813 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003814{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003815 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003816
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003817 output->allocation.x = x;
3818 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003819 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003820}
3821
3822static void
3823display_handle_mode(void *data,
3824 struct wl_output *wl_output,
3825 uint32_t flags,
3826 int width,
3827 int height,
3828 int refresh)
3829{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003830 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003831 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003832
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003833 if (flags & WL_OUTPUT_MODE_CURRENT) {
3834 output->allocation.width = width;
3835 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003836 if (display->output_configure_handler)
3837 (*display->output_configure_handler)(
3838 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003839 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003840}
3841
3842static const struct wl_output_listener output_listener = {
3843 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003844 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003845};
3846
3847static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003848display_add_output(struct display *d, uint32_t id)
3849{
3850 struct output *output;
3851
3852 output = malloc(sizeof *output);
3853 if (output == NULL)
3854 return;
3855
3856 memset(output, 0, sizeof *output);
3857 output->display = d;
3858 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003859 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003860 wl_list_insert(d->output_list.prev, &output->link);
3861
3862 wl_output_add_listener(output->output, &output_listener, output);
3863}
3864
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003865static void
3866output_destroy(struct output *output)
3867{
3868 if (output->destroy_handler)
3869 (*output->destroy_handler)(output, output->user_data);
3870
3871 wl_output_destroy(output->output);
3872 wl_list_remove(&output->link);
3873 free(output);
3874}
3875
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003876void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003877display_set_global_handler(struct display *display,
3878 display_global_handler_t handler)
3879{
3880 struct global *global;
3881
3882 display->global_handler = handler;
3883 if (!handler)
3884 return;
3885
3886 wl_list_for_each(global, &display->global_list, link)
3887 display->global_handler(display,
3888 global->name, global->interface,
3889 global->version, display->user_data);
3890}
3891
3892void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003893display_set_output_configure_handler(struct display *display,
3894 display_output_handler_t handler)
3895{
3896 struct output *output;
3897
3898 display->output_configure_handler = handler;
3899 if (!handler)
3900 return;
3901
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003902 wl_list_for_each(output, &display->output_list, link) {
3903 if (output->allocation.width == 0 &&
3904 output->allocation.height == 0)
3905 continue;
3906
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003907 (*display->output_configure_handler)(output,
3908 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003909 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003910}
3911
3912void
3913output_set_user_data(struct output *output, void *data)
3914{
3915 output->user_data = data;
3916}
3917
3918void *
3919output_get_user_data(struct output *output)
3920{
3921 return output->user_data;
3922}
3923
3924void
3925output_set_destroy_handler(struct output *output,
3926 display_output_handler_t handler)
3927{
3928 output->destroy_handler = handler;
3929 /* FIXME: implement this, once we have way to remove outputs */
3930}
3931
3932void
Scott Moreau4e072362012-09-29 02:03:11 -06003933output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003934{
Scott Moreau4e072362012-09-29 02:03:11 -06003935 struct rectangle allocation = output->allocation;
3936
3937 switch (output->transform) {
3938 case WL_OUTPUT_TRANSFORM_90:
3939 case WL_OUTPUT_TRANSFORM_270:
3940 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3941 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3942 /* Swap width and height */
3943 allocation.width = output->allocation.height;
3944 allocation.height = output->allocation.width;
3945 break;
3946 }
3947
3948 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003949}
3950
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003951struct wl_output *
3952output_get_wl_output(struct output *output)
3953{
3954 return output->output;
3955}
3956
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003957enum wl_output_transform
3958output_get_transform(struct output *output)
3959{
3960 return output->transform;
3961}
3962
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003963static void
Daniel Stone97f68542012-05-30 16:32:01 +01003964fini_xkb(struct input *input)
3965{
3966 xkb_state_unref(input->xkb.state);
3967 xkb_map_unref(input->xkb.keymap);
3968}
3969
3970static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003971display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003972{
3973 struct input *input;
3974
3975 input = malloc(sizeof *input);
3976 if (input == NULL)
3977 return;
3978
3979 memset(input, 0, sizeof *input);
3980 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003981 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003982 input->pointer_focus = NULL;
3983 input->keyboard_focus = NULL;
3984 wl_list_insert(d->input_list.prev, &input->link);
3985
Daniel Stone37816df2012-05-16 18:45:18 +01003986 wl_seat_add_listener(input->seat, &seat_listener, input);
3987 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003988
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003989 input->data_device =
3990 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003991 input->seat);
3992 wl_data_device_add_listener(input->data_device, &data_device_listener,
3993 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003994
3995 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003996
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003997 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3998 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003999 input->repeat_task.run = keyboard_repeat_func;
4000 display_watch_fd(d, input->repeat_timer_fd,
4001 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004002}
4003
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004004static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004005input_destroy(struct input *input)
4006{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004007 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004008 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004009
4010 if (input->drag_offer)
4011 data_offer_destroy(input->drag_offer);
4012
4013 if (input->selection_offer)
4014 data_offer_destroy(input->selection_offer);
4015
4016 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004017 fini_xkb(input);
4018
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004019 wl_surface_destroy(input->pointer_surface);
4020
Pekka Paalanene1207c72011-12-16 12:02:09 +02004021 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004022 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004023 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004024 free(input);
4025}
4026
4027static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004028init_workspace_manager(struct display *d, uint32_t id)
4029{
4030 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004031 wl_registry_bind(d->registry, id,
4032 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004033 if (d->workspace_manager != NULL)
4034 workspace_manager_add_listener(d->workspace_manager,
4035 &workspace_manager_listener,
4036 d);
4037}
4038
4039static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004040registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4041 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004042{
4043 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004044 struct global *global;
4045
4046 global = malloc(sizeof *global);
4047 global->name = id;
4048 global->interface = strdup(interface);
4049 global->version = version;
4050 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004051
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004052 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004053 d->compositor = wl_registry_bind(registry, id,
4054 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004055 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004056 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004057 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004058 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004059 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004060 d->shell = wl_registry_bind(registry,
4061 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004062 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004063 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004064 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4065 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004066 wl_registry_bind(registry, id,
4067 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004068 } else if (strcmp(interface, "text_cursor_position") == 0) {
4069 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004070 wl_registry_bind(registry, id,
4071 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004072 } else if (strcmp(interface, "workspace_manager") == 0) {
4073 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004074 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004075
4076 if (d->global_handler)
4077 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004078}
4079
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004080static void
4081registry_handle_global_remove(void *data, struct wl_registry *registry,
4082 uint32_t name)
4083{
4084 struct display *d = data;
4085 struct global *global;
4086 struct global *tmp;
4087
4088 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4089 if (global->name != name)
4090 continue;
4091
4092 /* XXX: Should destroy bound globals, and call
4093 * the counterpart of display::global_handler
4094 */
4095 wl_list_remove(&global->link);
4096 free(global->interface);
4097 free(global);
4098 }
4099}
4100
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004101void *
4102display_bind(struct display *display, uint32_t name,
4103 const struct wl_interface *interface, uint32_t version)
4104{
4105 return wl_registry_bind(display->registry, name, interface, version);
4106}
4107
4108static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004109 registry_handle_global,
4110 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004111};
4112
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004113#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004114static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004115init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004116{
4117 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004118 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004119
Rob Clark6396ed32012-03-11 19:48:41 -05004120#ifdef USE_CAIRO_GLESV2
4121# define GL_BIT EGL_OPENGL_ES2_BIT
4122#else
4123# define GL_BIT EGL_OPENGL_BIT
4124#endif
4125
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004126 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004127 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004128 EGL_RED_SIZE, 1,
4129 EGL_GREEN_SIZE, 1,
4130 EGL_BLUE_SIZE, 1,
4131 EGL_ALPHA_SIZE, 1,
4132 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004133 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004134 EGL_NONE
4135 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004136
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004137#ifdef USE_CAIRO_GLESV2
4138 static const EGLint context_attribs[] = {
4139 EGL_CONTEXT_CLIENT_VERSION, 2,
4140 EGL_NONE
4141 };
4142 EGLint api = EGL_OPENGL_ES_API;
4143#else
4144 EGLint *context_attribs = NULL;
4145 EGLint api = EGL_OPENGL_API;
4146#endif
4147
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004148 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004149 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004150 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004151 return -1;
4152 }
4153
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004154 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004155 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004156 return -1;
4157 }
4158
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004159 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4160 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004161 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004162 return -1;
4163 }
4164
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004165 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004166 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004167 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004168 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004169 return -1;
4170 }
4171
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004172 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004173 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004174 return -1;
4175 }
4176
Benjamin Franzke0c991632011-09-27 21:57:31 +02004177 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4178 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004179 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004180 return -1;
4181 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004182
4183 return 0;
4184}
4185
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004186static void
4187fini_egl(struct display *display)
4188{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004189 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004190
4191 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4192 EGL_NO_CONTEXT);
4193
4194 eglTerminate(display->dpy);
4195 eglReleaseThread();
4196}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004197#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004198
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004199static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004200init_dummy_surface(struct display *display)
4201{
4202 int len;
4203 void *data;
4204
4205 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4206 data = malloc(len);
4207 display->dummy_surface =
4208 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4209 1, 1, len);
4210 display->dummy_surface_data = data;
4211}
4212
4213static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004214handle_display_data(struct task *task, uint32_t events)
4215{
4216 struct display *display =
4217 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004218 struct epoll_event ep;
4219 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004220
4221 display->display_fd_events = events;
4222
4223 if (events & EPOLLERR || events & EPOLLHUP) {
4224 display_exit(display);
4225 return;
4226 }
4227
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004228 if (events & EPOLLIN) {
4229 ret = wl_display_dispatch(display->display);
4230 if (ret == -1) {
4231 display_exit(display);
4232 return;
4233 }
4234 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004235
4236 if (events & EPOLLOUT) {
4237 ret = wl_display_flush(display->display);
4238 if (ret == 0) {
4239 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4240 ep.data.ptr = &display->display_task;
4241 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4242 display->display_fd, &ep);
4243 } else if (ret == -1 && errno != EAGAIN) {
4244 display_exit(display);
4245 return;
4246 }
4247 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004248}
4249
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004250struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004251display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004252{
4253 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004254
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004255 d = malloc(sizeof *d);
4256 if (d == NULL)
4257 return NULL;
4258
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004259 memset(d, 0, sizeof *d);
4260
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004261 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004262 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004263 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004264 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004265 return NULL;
4266 }
4267
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004268 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004269 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004270 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004271 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4272 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004273
4274 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004275 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004276 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004277 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004278
Daniel Stone97f68542012-05-30 16:32:01 +01004279 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004280 if (d->xkb_context == NULL) {
4281 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004282 return NULL;
4283 }
4284
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004285 d->workspace = 0;
4286 d->workspace_count = 1;
4287
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004288 d->registry = wl_display_get_registry(d->display);
4289 wl_registry_add_listener(d->registry, &registry_listener, d);
4290 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004291#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004292 if (init_egl(d) < 0)
4293 fprintf(stderr, "EGL does not seem to work, "
4294 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004295#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004296
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004297 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004298
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004299 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004300
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004301 wl_list_init(&d->window_list);
4302
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004303 init_dummy_surface(d);
4304
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004305 return d;
4306}
4307
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004308static void
4309display_destroy_outputs(struct display *display)
4310{
4311 struct output *tmp;
4312 struct output *output;
4313
4314 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4315 output_destroy(output);
4316}
4317
Pekka Paalanene1207c72011-12-16 12:02:09 +02004318static void
4319display_destroy_inputs(struct display *display)
4320{
4321 struct input *tmp;
4322 struct input *input;
4323
4324 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4325 input_destroy(input);
4326}
4327
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004328void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004329display_destroy(struct display *display)
4330{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004331 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004332 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4333 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004334
4335 if (!wl_list_empty(&display->deferred_list))
4336 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4337
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004338 cairo_surface_destroy(display->dummy_surface);
4339 free(display->dummy_surface_data);
4340
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004341 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004342 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004343
Daniel Stone97f68542012-05-30 16:32:01 +01004344 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004345
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004346 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004347 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004348
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004349#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004350 if (display->argb_device)
4351 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004352#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004353
Pekka Paalanenc2052982011-12-16 11:41:32 +02004354 if (display->shell)
4355 wl_shell_destroy(display->shell);
4356
4357 if (display->shm)
4358 wl_shm_destroy(display->shm);
4359
4360 if (display->data_device_manager)
4361 wl_data_device_manager_destroy(display->data_device_manager);
4362
4363 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004364 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004365
4366 close(display->epoll_fd);
4367
U. Artie Eoff44874d92012-10-02 21:12:35 -07004368 if (!(display->display_fd_events & EPOLLERR) &&
4369 !(display->display_fd_events & EPOLLHUP))
4370 wl_display_flush(display->display);
4371
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004372 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004373 free(display);
4374}
4375
4376void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004377display_set_user_data(struct display *display, void *data)
4378{
4379 display->user_data = data;
4380}
4381
4382void *
4383display_get_user_data(struct display *display)
4384{
4385 return display->user_data;
4386}
4387
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004388struct wl_display *
4389display_get_display(struct display *display)
4390{
4391 return display->display;
4392}
4393
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004394struct output *
4395display_get_output(struct display *display)
4396{
4397 return container_of(display->output_list.next, struct output, link);
4398}
4399
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004400struct wl_compositor *
4401display_get_compositor(struct display *display)
4402{
4403 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004404}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004405
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004406uint32_t
4407display_get_serial(struct display *display)
4408{
4409 return display->serial;
4410}
4411
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004412EGLDisplay
4413display_get_egl_display(struct display *d)
4414{
4415 return d->dpy;
4416}
4417
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004418struct wl_data_source *
4419display_create_data_source(struct display *display)
4420{
4421 return wl_data_device_manager_create_data_source(display->data_device_manager);
4422}
4423
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004424EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004425display_get_argb_egl_config(struct display *d)
4426{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004427 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004428}
4429
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004430struct wl_shell *
4431display_get_shell(struct display *display)
4432{
4433 return display->shell;
4434}
4435
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004436int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004437display_acquire_window_surface(struct display *display,
4438 struct window *window,
4439 EGLContext ctx)
4440{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004441 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004442 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004443
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004444 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004445}
4446
4447void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004448display_release_window_surface(struct display *display,
4449 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004450{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004451 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004452 return;
4453
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004454 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004455}
4456
4457void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004458display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004459{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004460 wl_list_insert(&display->deferred_list, &task->link);
4461}
4462
4463void
4464display_watch_fd(struct display *display,
4465 int fd, uint32_t events, struct task *task)
4466{
4467 struct epoll_event ep;
4468
4469 ep.events = events;
4470 ep.data.ptr = task;
4471 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4472}
4473
4474void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004475display_unwatch_fd(struct display *display, int fd)
4476{
4477 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4478}
4479
4480void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004481display_run(struct display *display)
4482{
4483 struct task *task;
4484 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004485 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004486
Pekka Paalanen826d7952011-12-15 10:14:07 +02004487 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004488 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004489 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004490 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004491 struct task, link);
4492 wl_list_remove(&task->link);
4493 task->run(task, 0);
4494 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004495
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004496 wl_display_dispatch_pending(display->display);
4497
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004498 if (!display->running)
4499 break;
4500
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004501 ret = wl_display_flush(display->display);
4502 if (ret < 0 && errno == EAGAIN) {
4503 ep[0].events =
4504 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4505 ep[0].data.ptr = &display->display_task;
4506
4507 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4508 display->display_fd, &ep[0]);
4509 } else if (ret < 0) {
4510 break;
4511 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004512
4513 count = epoll_wait(display->epoll_fd,
4514 ep, ARRAY_LENGTH(ep), -1);
4515 for (i = 0; i < count; i++) {
4516 task = ep[i].data.ptr;
4517 task->run(task, ep[i].events);
4518 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004519 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004520}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004521
4522void
4523display_exit(struct display *display)
4524{
4525 display->running = 0;
4526}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004527
4528void
4529keysym_modifiers_add(struct wl_array *modifiers_map,
4530 const char *name)
4531{
4532 size_t len = strlen(name) + 1;
4533 char *p;
4534
4535 p = wl_array_add(modifiers_map, len);
4536
4537 if (p == NULL)
4538 return;
4539
4540 strncpy(p, name, len);
4541}
4542
4543static xkb_mod_index_t
4544keysym_modifiers_get_index(struct wl_array *modifiers_map,
4545 const char *name)
4546{
4547 xkb_mod_index_t index = 0;
4548 char *p = modifiers_map->data;
4549
4550 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4551 if (strcmp(p, name) == 0)
4552 return index;
4553
4554 index++;
4555 p += strlen(p) + 1;
4556 }
4557
4558 return XKB_MOD_INVALID;
4559}
4560
4561xkb_mod_mask_t
4562keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4563 const char *name)
4564{
4565 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4566
4567 if (index == XKB_MOD_INVALID)
4568 return XKB_MOD_INVALID;
4569
4570 return 1 << index;
4571}