blob: 073ce53326c7789e761ab6ebc3046b360f9a4008 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
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 Paalanen03fc3162012-11-19 17:15:58 +0200148struct toysurface {
149 /*
150 * Prepare the surface for drawing. Makes sure there is a surface
151 * of the right size available for rendering, and returns it.
152 * dx,dy are the x,y of wl_surface.attach.
153 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200154 * If flags has SURFACE_HINT_RESIZE set, the user is
155 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156 * Returns the Cairo surface to draw to.
157 */
158 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200159 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160
161 /*
162 * Post the surface to the server, returning the server allocation
163 * rectangle. The Cairo surface from prepare() must be destroyed
164 * after calling this.
165 */
166 void (*swap)(struct toysurface *base,
167 struct rectangle *server_allocation);
168
169 /*
170 * Make the toysurface current with the given EGL context.
171 * Returns 0 on success, and negative of failure.
172 */
173 int (*acquire)(struct toysurface *base, EGLContext ctx);
174
175 /*
176 * Release the toysurface from the EGL context, returning control
177 * to Cairo.
178 */
179 void (*release)(struct toysurface *base);
180
181 /*
182 * Destroy the toysurface, including the Cairo surface, any
183 * backing storage, and the Wayland protocol objects.
184 */
185 void (*destroy)(struct toysurface *base);
186};
187
Pekka Paalanen4e373742013-02-13 16:17:13 +0200188struct surface {
189 struct window *window;
190
191 struct wl_surface *surface;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200192 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200193 struct widget *widget;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200194
195 struct rectangle allocation;
196 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200197
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200198 struct wl_region *input_region;
199 struct wl_region *opaque_region;
200
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200201 enum window_buffer_type buffer_type;
202 enum wl_output_transform buffer_transform;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200203
204 cairo_surface_t *cairo_surface;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200205};
206
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500207struct window {
208 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500209 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100210 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500211 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200212 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400213 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500214 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500215 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400216 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400217 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400218 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400219 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400220 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100221 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400222 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400223 int focus_count;
224
Pekka Paalanen99436862012-11-19 17:15:59 +0200225 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200226 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000227 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400228
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500229 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500230 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400231 window_data_handler_t data_handler;
232 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500233 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400234 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200235 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400236
Pekka Paalanen4e373742013-02-13 16:17:13 +0200237 struct surface *main_surface;
238 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300239 struct wl_callback *frame_cb;
240
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200241 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500242
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500243 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400244 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500245};
246
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500247struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500248 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200249 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300250 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500251 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400252 struct wl_list link;
253 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500254 widget_resize_handler_t resize_handler;
255 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500256 widget_enter_handler_t enter_handler;
257 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500258 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500259 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200260 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400261 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500262 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300263 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500264 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400265};
266
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400267struct input {
268 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100269 struct wl_seat *seat;
270 struct wl_pointer *pointer;
271 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400272 struct window *pointer_focus;
273 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300274 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300275 uint32_t cursor_anim_start;
276 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300277 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400278 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400279 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400280 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400281 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400282 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400283
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500284 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500285 struct widget *grab;
286 uint32_t grab_button;
287
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400288 struct wl_data_device *data_device;
289 struct data_offer *drag_offer;
290 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100291
292 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100293 struct xkb_keymap *keymap;
294 struct xkb_state *state;
295 xkb_mod_mask_t control_mask;
296 xkb_mod_mask_t alt_mask;
297 xkb_mod_mask_t shift_mask;
298 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400299
300 struct task repeat_task;
301 int repeat_timer_fd;
302 uint32_t repeat_sym;
303 uint32_t repeat_key;
304 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400305};
306
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500307struct output {
308 struct display *display;
309 struct wl_output *output;
310 struct rectangle allocation;
311 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600312 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200313
314 display_output_handler_t destroy_handler;
315 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500316};
317
Martin Minarik1998b152012-05-10 02:04:35 +0200318enum frame_button_action {
319 FRAME_BUTTON_NULL = 0,
320 FRAME_BUTTON_ICON = 1,
321 FRAME_BUTTON_CLOSE = 2,
322 FRAME_BUTTON_MINIMIZE = 3,
323 FRAME_BUTTON_MAXIMIZE = 4,
324};
325
326enum frame_button_pointer {
327 FRAME_BUTTON_DEFAULT = 0,
328 FRAME_BUTTON_OVER = 1,
329 FRAME_BUTTON_ACTIVE = 2,
330};
331
332enum frame_button_align {
333 FRAME_BUTTON_RIGHT = 0,
334 FRAME_BUTTON_LEFT = 1,
335};
336
337enum frame_button_decoration {
338 FRAME_BUTTON_NONE = 0,
339 FRAME_BUTTON_FANCY = 1,
340};
341
342struct frame_button {
343 struct widget *widget;
344 struct frame *frame;
345 cairo_surface_t *icon;
346 enum frame_button_action type;
347 enum frame_button_pointer state;
348 struct wl_list link; /* buttons_list */
349 enum frame_button_align align;
350 enum frame_button_decoration decoration;
351};
352
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500353struct frame {
354 struct widget *widget;
355 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200356 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500357};
358
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500359struct menu {
360 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500361 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500362 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500363 const char **entries;
364 uint32_t time;
365 int current;
366 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400367 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500368 menu_func_t func;
369};
370
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300371struct tooltip {
372 struct widget *parent;
373 struct window *window;
374 struct widget *widget;
375 char *entry;
376 struct task tooltip_task;
377 int tooltip_fd;
378 float x, y;
379};
380
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700381struct shm_pool {
382 struct wl_shm_pool *pool;
383 size_t size;
384 size_t used;
385 void *data;
386};
387
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400388enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300389 CURSOR_DEFAULT = 100,
390 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400391};
392
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500393enum window_location {
394 WINDOW_INTERIOR = 0,
395 WINDOW_RESIZING_TOP = 1,
396 WINDOW_RESIZING_BOTTOM = 2,
397 WINDOW_RESIZING_LEFT = 4,
398 WINDOW_RESIZING_TOP_LEFT = 5,
399 WINDOW_RESIZING_BOTTOM_LEFT = 6,
400 WINDOW_RESIZING_RIGHT = 8,
401 WINDOW_RESIZING_TOP_RIGHT = 9,
402 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
403 WINDOW_RESIZING_MASK = 15,
404 WINDOW_EXTERIOR = 16,
405 WINDOW_TITLEBAR = 17,
406 WINDOW_CLIENT_AREA = 18,
407};
408
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200409static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500411#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200413struct egl_window_surface {
414 struct toysurface base;
415 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100416 struct display *display;
417 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200418 struct wl_egl_window *egl_window;
419 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100420};
421
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200422static struct egl_window_surface *
423to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100424{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200425 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100426}
427
428static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200429egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200430 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100431{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200432 struct egl_window_surface *surface = to_egl_window_surface(base);
433
434 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
435 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
436
437 return cairo_surface_reference(surface->cairo_surface);
438}
439
440static void
441egl_window_surface_swap(struct toysurface *base,
442 struct rectangle *server_allocation)
443{
444 struct egl_window_surface *surface = to_egl_window_surface(base);
445
446 cairo_gl_surface_swapbuffers(surface->cairo_surface);
447 wl_egl_window_get_attached_size(surface->egl_window,
448 &server_allocation->width,
449 &server_allocation->height);
450}
451
452static int
453egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
454{
455 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200456 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100457
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200458 device = cairo_surface_get_device(surface->cairo_surface);
459 if (!device)
460 return -1;
461
462 if (!ctx) {
463 if (device == surface->display->argb_device)
464 ctx = surface->display->argb_ctx;
465 else
466 assert(0);
467 }
468
469 cairo_device_flush(device);
470 cairo_device_acquire(device);
471 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
472 surface->egl_surface, ctx))
473 fprintf(stderr, "failed to make surface current\n");
474
475 return 0;
476}
477
478static void
479egl_window_surface_release(struct toysurface *base)
480{
481 struct egl_window_surface *surface = to_egl_window_surface(base);
482 cairo_device_t *device;
483
484 device = cairo_surface_get_device(surface->cairo_surface);
485 if (!device)
486 return;
487
488 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
489 surface->display->argb_ctx))
490 fprintf(stderr, "failed to make context current\n");
491
492 cairo_device_release(device);
493}
494
495static void
496egl_window_surface_destroy(struct toysurface *base)
497{
498 struct egl_window_surface *surface = to_egl_window_surface(base);
499 struct display *d = surface->display;
500
501 cairo_surface_destroy(surface->cairo_surface);
502 eglDestroySurface(d->dpy, surface->egl_surface);
503 wl_egl_window_destroy(surface->egl_window);
504 surface->surface = NULL;
505
506 free(surface);
507}
508
509static struct toysurface *
510egl_window_surface_create(struct display *display,
511 struct wl_surface *wl_surface,
512 uint32_t flags,
513 struct rectangle *rectangle)
514{
515 struct egl_window_surface *surface;
516
Pekka Paalanenb3627362012-11-19 17:16:00 +0200517 if (display->dpy == EGL_NO_DISPLAY)
518 return NULL;
519
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200520 surface = calloc(1, sizeof *surface);
521 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100522 return NULL;
523
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200524 surface->base.prepare = egl_window_surface_prepare;
525 surface->base.swap = egl_window_surface_swap;
526 surface->base.acquire = egl_window_surface_acquire;
527 surface->base.release = egl_window_surface_release;
528 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100529
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 surface->display = display;
531 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400532
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533 surface->egl_window = wl_egl_window_create(surface->surface,
534 rectangle->width,
535 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100536
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200537 surface->egl_surface = eglCreateWindowSurface(display->dpy,
538 display->argb_config,
539 surface->egl_window,
540 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100541
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200542 surface->cairo_surface =
543 cairo_gl_surface_create_for_egl(display->argb_device,
544 surface->egl_surface,
545 rectangle->width,
546 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100547
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200548 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100549}
550
Pekka Paalanenb3627362012-11-19 17:16:00 +0200551#else
552
553static struct toysurface *
554egl_window_surface_create(struct display *display,
555 struct wl_surface *wl_surface,
556 uint32_t flags,
557 struct rectangle *rectangle)
558{
559 return NULL;
560}
561
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562#endif
563
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200564struct shm_surface_data {
565 struct wl_buffer *buffer;
566 struct shm_pool *pool;
567};
568
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400569struct wl_buffer *
570display_get_buffer_for_surface(struct display *display,
571 cairo_surface_t *surface)
572{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200573 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400574
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200575 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576
577 return data->buffer;
578}
579
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500580static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700581shm_pool_destroy(struct shm_pool *pool);
582
583static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584shm_surface_data_destroy(void *p)
585{
586 struct shm_surface_data *data = p;
587
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200588 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700589 if (data->pool)
590 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300591
592 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593}
594
Kristian Høgsberg16626282012-04-03 11:21:27 -0400595static struct wl_shm_pool *
596make_shm_pool(struct display *display, int size, void **data)
597{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400598 struct wl_shm_pool *pool;
599 int fd;
600
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300601 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400602 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300603 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
604 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400605 return NULL;
606 }
607
608 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400609 if (*data == MAP_FAILED) {
610 fprintf(stderr, "mmap failed: %m\n");
611 close(fd);
612 return NULL;
613 }
614
615 pool = wl_shm_create_pool(display->shm, fd, size);
616
617 close(fd);
618
619 return pool;
620}
621
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700622static struct shm_pool *
623shm_pool_create(struct display *display, size_t size)
624{
625 struct shm_pool *pool = malloc(sizeof *pool);
626
627 if (!pool)
628 return NULL;
629
630 pool->pool = make_shm_pool(display, size, &pool->data);
631 if (!pool->pool) {
632 free(pool);
633 return NULL;
634 }
635
636 pool->size = size;
637 pool->used = 0;
638
639 return pool;
640}
641
642static void *
643shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
644{
645 if (pool->used + size > pool->size)
646 return NULL;
647
648 *offset = pool->used;
649 pool->used += size;
650
651 return (char *) pool->data + *offset;
652}
653
654/* destroy the pool. this does not unmap the memory though */
655static void
656shm_pool_destroy(struct shm_pool *pool)
657{
658 munmap(pool->data, pool->size);
659 wl_shm_pool_destroy(pool->pool);
660 free(pool);
661}
662
663/* Start allocating from the beginning of the pool again */
664static void
665shm_pool_reset(struct shm_pool *pool)
666{
667 pool->used = 0;
668}
669
670static int
671data_length_for_shm_surface(struct rectangle *rect)
672{
673 int stride;
674
675 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
676 rect->width);
677 return stride * rect->height;
678}
679
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500680static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700681display_create_shm_surface_from_pool(struct display *display,
682 struct rectangle *rectangle,
683 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400684{
685 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400686 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400687 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700688 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400689 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690
691 data = malloc(sizeof *data);
692 if (data == NULL)
693 return NULL;
694
695 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
696 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700697 length = stride * rectangle->height;
698 data->pool = NULL;
699 map = shm_pool_allocate(pool, length, &offset);
700
701 if (!map) {
702 free(data);
703 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400704 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400706 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400707 CAIRO_FORMAT_ARGB32,
708 rectangle->width,
709 rectangle->height,
710 stride);
711
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200712 cairo_surface_set_user_data(surface, &shm_surface_data_key,
713 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400714
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400715 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500716 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400717 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500718 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400719
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200720 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
721 rectangle->width,
722 rectangle->height,
723 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400724
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700725 return surface;
726}
727
728static cairo_surface_t *
729display_create_shm_surface(struct display *display,
730 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200731 struct shm_pool *alternate_pool,
732 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700733{
734 struct shm_surface_data *data;
735 struct shm_pool *pool;
736 cairo_surface_t *surface;
737
Pekka Paalanen99436862012-11-19 17:15:59 +0200738 if (alternate_pool) {
739 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700740 surface = display_create_shm_surface_from_pool(display,
741 rectangle,
742 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200743 alternate_pool);
744 if (surface) {
745 data = cairo_surface_get_user_data(surface,
746 &shm_surface_data_key);
747 goto out;
748 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700749 }
750
751 pool = shm_pool_create(display,
752 data_length_for_shm_surface(rectangle));
753 if (!pool)
754 return NULL;
755
756 surface =
757 display_create_shm_surface_from_pool(display, rectangle,
758 flags, pool);
759
760 if (!surface) {
761 shm_pool_destroy(pool);
762 return NULL;
763 }
764
765 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200766 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700767 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400768
Pekka Paalanen99436862012-11-19 17:15:59 +0200769out:
770 if (data_ret)
771 *data_ret = data;
772
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400773 return surface;
774}
775
nobled7b87cb02011-02-01 18:51:47 +0000776static int
777check_size(struct rectangle *rect)
778{
779 if (rect->width && rect->height)
780 return 0;
781
782 fprintf(stderr, "tried to create surface of "
783 "width: %d, height: %d\n", rect->width, rect->height);
784 return -1;
785}
786
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400787cairo_surface_t *
788display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400790 struct rectangle *rectangle,
791 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400792{
nobled7b87cb02011-02-01 18:51:47 +0000793 if (check_size(rectangle) < 0)
794 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200795
796 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200797 return display_create_shm_surface(display, rectangle, flags,
798 NULL, NULL);
799}
800
Pekka Paalanena4eda732012-11-19 17:16:02 +0200801struct shm_surface_leaf {
802 cairo_surface_t *cairo_surface;
803 /* 'data' is automatically destroyed, when 'cairo_surface' is */
804 struct shm_surface_data *data;
805
806 struct shm_pool *resize_pool;
807 int busy;
808};
809
810static void
811shm_surface_leaf_release(struct shm_surface_leaf *leaf)
812{
813 if (leaf->cairo_surface)
814 cairo_surface_destroy(leaf->cairo_surface);
815 /* leaf->data already destroyed via cairo private */
816
817 if (leaf->resize_pool)
818 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200819
820 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200821}
822
Pekka Paalanen99436862012-11-19 17:15:59 +0200823struct shm_surface {
824 struct toysurface base;
825 struct display *display;
826 struct wl_surface *surface;
827 uint32_t flags;
828 int dx, dy;
829
Pekka Paalanena4eda732012-11-19 17:16:02 +0200830 struct shm_surface_leaf leaf[2];
831 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200832};
833
834static struct shm_surface *
835to_shm_surface(struct toysurface *base)
836{
837 return container_of(base, struct shm_surface, base);
838}
839
Pekka Paalanena4eda732012-11-19 17:16:02 +0200840static void
841shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
842{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200843 struct shm_surface *surface = data;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200844
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200845 if (surface->leaf[0].data->buffer == buffer)
846 surface->leaf[0].busy = 0;
847 else if (surface->leaf[1].data->buffer == buffer)
848 surface->leaf[1].busy = 0;
849 else
850 assert(0 && "shm_surface_buffer_release: unknown buffer");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200851
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200852 if (!surface->leaf[0].busy && !surface->leaf[1].busy)
853 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200854}
855
856static const struct wl_buffer_listener shm_surface_buffer_listener = {
857 shm_surface_buffer_release
858};
859
Pekka Paalanen99436862012-11-19 17:15:59 +0200860static cairo_surface_t *
861shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200862 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200863{
Pekka Paalanenec076692012-11-30 13:37:27 +0200864 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200865 struct shm_surface *surface = to_shm_surface(base);
866 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200867 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200868
869 surface->dx = dx;
870 surface->dy = dy;
871
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200872 /* See shm_surface_buffer_release() */
873 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
874 surface->leaf[1].cairo_surface) {
875 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
876 __func__);
877 }
878
Pekka Paalanena4eda732012-11-19 17:16:02 +0200879 /* pick a free buffer from the two */
880 if (!surface->leaf[0].busy)
881 leaf = &surface->leaf[0];
882 else if (!surface->leaf[1].busy)
883 leaf = &surface->leaf[1];
884 else {
885 fprintf(stderr, "%s: both buffers are held by the server.\n",
886 __func__);
887 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200888 }
889
Pekka Paalanena4eda732012-11-19 17:16:02 +0200890 if (!resize_hint && leaf->resize_pool) {
891 cairo_surface_destroy(leaf->cairo_surface);
892 leaf->cairo_surface = NULL;
893 shm_pool_destroy(leaf->resize_pool);
894 leaf->resize_pool = NULL;
895 }
896
897 if (leaf->cairo_surface &&
898 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
899 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200900 goto out;
901
Pekka Paalanena4eda732012-11-19 17:16:02 +0200902 if (leaf->cairo_surface)
903 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200904
Pekka Paalanena4eda732012-11-19 17:16:02 +0200905 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200906 /* Create a big pool to allocate from, while continuously
907 * resizing. Mmapping a new pool in the server
908 * is relatively expensive, so reusing a pool performs
909 * better, but may temporarily reserve unneeded memory.
910 */
911 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200912 leaf->resize_pool = shm_pool_create(surface->display,
913 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200914 }
915
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200917 display_create_shm_surface(surface->display, &rect,
918 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200919 leaf->resize_pool,
920 &leaf->data);
921 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200922 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200923
924out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200925 surface->current = leaf;
926
927 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200928}
929
930static void
931shm_surface_swap(struct toysurface *base,
932 struct rectangle *server_allocation)
933{
934 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200935 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200936
937 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200939 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200940 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200941
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200943 surface->dx, surface->dy);
944 wl_surface_damage(surface->surface, 0, 0,
945 server_allocation->width, server_allocation->height);
946 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200947
948 leaf->busy = 1;
949 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200950}
951
952static int
953shm_surface_acquire(struct toysurface *base, EGLContext ctx)
954{
955 return -1;
956}
957
958static void
959shm_surface_release(struct toysurface *base)
960{
961}
962
963static void
964shm_surface_destroy(struct toysurface *base)
965{
966 struct shm_surface *surface = to_shm_surface(base);
967
Pekka Paalanena4eda732012-11-19 17:16:02 +0200968 shm_surface_leaf_release(&surface->leaf[0]);
969 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200970
971 free(surface);
972}
973
974static struct toysurface *
975shm_surface_create(struct display *display, struct wl_surface *wl_surface,
976 uint32_t flags, struct rectangle *rectangle)
977{
978 struct shm_surface *surface;
979
980 surface = calloc(1, sizeof *surface);
981 if (!surface)
982 return NULL;
983
984 surface->base.prepare = shm_surface_prepare;
985 surface->base.swap = shm_surface_swap;
986 surface->base.acquire = shm_surface_acquire;
987 surface->base.release = shm_surface_release;
988 surface->base.destroy = shm_surface_destroy;
989
990 surface->display = display;
991 surface->surface = wl_surface;
992 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200993
994 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400995}
996
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200997/*
998 * The following correspondences between file names and cursors was copied
999 * from: https://bugs.kde.org/attachment.cgi?id=67313
1000 */
1001
1002static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001003 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001004 "sw-resize"
1005};
1006
1007static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001008 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001009 "se-resize"
1010};
1011
1012static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001013 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001014 "s-resize"
1015};
1016
1017static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001018 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001019 "closedhand",
1020 "208530c400c041818281048008011002"
1021};
1022
1023static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001024 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001025 "default",
1026 "top_left_arrow",
1027 "left-arrow"
1028};
1029
1030static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001031 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001032 "w-resize"
1033};
1034
1035static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001036 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001037 "e-resize"
1038};
1039
1040static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001041 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001042 "nw-resize"
1043};
1044
1045static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001046 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001047 "ne-resize"
1048};
1049
1050static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001051 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001052 "n-resize"
1053};
1054
1055static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001056 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001057 "ibeam",
1058 "text"
1059};
1060
1061static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001062 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001063 "pointer",
1064 "pointing_hand",
1065 "e29285e634086352946a0e7090d73106"
1066};
1067
1068static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001069 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001070 "wait",
1071 "0426c94ea35c87780ff01dc239897213"
1072};
1073
1074struct cursor_alternatives {
1075 const char **names;
1076 size_t count;
1077};
1078
1079static const struct cursor_alternatives cursors[] = {
1080 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1081 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1082 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1083 {grabbings, ARRAY_LENGTH(grabbings)},
1084 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1085 {left_sides, ARRAY_LENGTH(left_sides)},
1086 {right_sides, ARRAY_LENGTH(right_sides)},
1087 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1088 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1089 {top_sides, ARRAY_LENGTH(top_sides)},
1090 {xterms, ARRAY_LENGTH(xterms)},
1091 {hand1s, ARRAY_LENGTH(hand1s)},
1092 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001093};
1094
1095static void
1096create_cursors(struct display *display)
1097{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001098 char *config_file;
1099 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001100 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001101 unsigned int i, j;
1102 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001103 struct config_key shell_keys[] = {
1104 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001105 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001106 };
1107 struct config_section cs[] = {
1108 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1109 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001110
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001111 config_file = config_file_path("weston.ini");
1112 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1113 free(config_file);
1114
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001115 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001116 display->cursors =
1117 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1118
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001119 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001120 cursor = NULL;
1121 for (j = 0; !cursor && j < cursors[i].count; ++j)
1122 cursor = wl_cursor_theme_get_cursor(
1123 display->cursor_theme, cursors[i].names[j]);
1124
1125 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001126 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001127 cursors[i].names[0]);
1128
1129 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001130 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001131}
1132
1133static void
1134destroy_cursors(struct display *display)
1135{
1136 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001137 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001138}
1139
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001140struct wl_cursor_image *
1141display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001142{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001143 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001144
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001145 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001146}
1147
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001148static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001149surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001150{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001151 if (!surface->cairo_surface)
1152 return;
1153
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001154 if (surface->opaque_region) {
1155 wl_surface_set_opaque_region(surface->surface,
1156 surface->opaque_region);
1157 wl_region_destroy(surface->opaque_region);
1158 surface->opaque_region = NULL;
1159 }
1160
1161 if (surface->input_region) {
1162 wl_surface_set_input_region(surface->surface,
1163 surface->input_region);
1164 wl_region_destroy(surface->input_region);
1165 surface->input_region = NULL;
1166 }
1167
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001168 surface->toysurface->swap(surface->toysurface,
1169 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001170
Pekka Paalanen89dee002013-02-13 16:17:20 +02001171 cairo_surface_destroy(surface->cairo_surface);
1172 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001173}
1174
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001175int
1176window_has_focus(struct window *window)
1177{
1178 return window->focus_count > 0;
1179}
1180
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001181static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001182window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001183{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001184 if (window->type == TYPE_NONE) {
1185 window->type = TYPE_TOPLEVEL;
1186 if (window->shell_surface)
1187 wl_shell_surface_set_toplevel(window->shell_surface);
1188 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001189
Pekka Paalanen89dee002013-02-13 16:17:20 +02001190 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001191}
1192
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001193struct display *
1194window_get_display(struct window *window)
1195{
1196 return window->display;
1197}
1198
Pekka Paalanen89dee002013-02-13 16:17:20 +02001199static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001200surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001201{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001202 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001203 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001204
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001205 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001206 case WL_OUTPUT_TRANSFORM_90:
1207 case WL_OUTPUT_TRANSFORM_270:
1208 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1209 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001210 allocation.width = surface->allocation.height;
1211 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001212 break;
1213 default:
1214 break;
1215 }
1216
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001217 if (!surface->toysurface && display->dpy &&
1218 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001219 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001220 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001221 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001222 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001223 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001224 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001225
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001226 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001227 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001228 surface->surface,
1229 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001230
Pekka Paalanen89dee002013-02-13 16:17:20 +02001231 surface->cairo_surface = surface->toysurface->prepare(
1232 surface->toysurface, dx, dy,
1233 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001234}
1235
1236static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001237window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001238{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001239 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001240 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001241 int dx = 0;
1242 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001243
Pekka Paalanenec076692012-11-30 13:37:27 +02001244 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001245 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001246
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001247 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1248 dx = surface->server_allocation.width -
1249 surface->allocation.width;
1250
1251 if (window->resize_edges & WINDOW_RESIZING_TOP)
1252 dy = surface->server_allocation.height -
1253 surface->allocation.height;
1254
1255 window->resize_edges = 0;
1256
Pekka Paalanen89dee002013-02-13 16:17:20 +02001257 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001258}
1259
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001260int
1261window_get_buffer_transform(struct window *window)
1262{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001263 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001264}
1265
1266void
1267window_set_buffer_transform(struct window *window,
1268 enum wl_output_transform transform)
1269{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001270 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001271 wl_surface_set_buffer_transform(window->main_surface->surface,
1272 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001273}
1274
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001275static void frame_destroy(struct frame *frame);
1276
Pekka Paalanen4e373742013-02-13 16:17:13 +02001277static void
1278surface_destroy(struct surface *surface)
1279{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001280 if (surface->input_region)
1281 wl_region_destroy(surface->input_region);
1282
1283 if (surface->opaque_region)
1284 wl_region_destroy(surface->opaque_region);
1285
Pekka Paalanen4e373742013-02-13 16:17:13 +02001286 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001287
1288 if (surface->toysurface)
1289 surface->toysurface->destroy(surface->toysurface);
1290
Pekka Paalanen4e373742013-02-13 16:17:13 +02001291 free(surface);
1292}
1293
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001294void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001295window_destroy(struct window *window)
1296{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001297 struct display *display = window->display;
1298 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001299 struct window_output *window_output;
1300 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001301
1302 if (window->redraw_scheduled)
1303 wl_list_remove(&window->redraw_task.link);
1304
1305 wl_list_for_each(input, &display->input_list, link) {
1306 if (input->pointer_focus == window)
1307 input->pointer_focus = NULL;
1308 if (input->keyboard_focus == window)
1309 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001310 if (input->focus_widget &&
1311 input->focus_widget->window == window)
1312 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001313 }
1314
Rob Bradford7507b572012-05-15 17:55:34 +01001315 wl_list_for_each_safe(window_output, window_output_tmp,
1316 &window->window_output_list, link) {
1317 free (window_output);
1318 }
1319
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001320 if (window->frame)
1321 frame_destroy(window->frame);
1322
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001323 if (window->shell_surface)
1324 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001325
1326 surface_destroy(window->main_surface);
1327
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001328 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001329
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001330 if (window->frame_cb)
1331 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001332 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001333 free(window);
1334}
1335
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001336static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001337widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001338{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001339 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001340
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001341 wl_list_for_each(child, &widget->child_list, link) {
1342 target = widget_find_widget(child, x, y);
1343 if (target)
1344 return target;
1345 }
1346
1347 if (widget->allocation.x <= x &&
1348 x < widget->allocation.x + widget->allocation.width &&
1349 widget->allocation.y <= y &&
1350 y < widget->allocation.y + widget->allocation.height) {
1351 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001352 }
1353
1354 return NULL;
1355}
1356
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001357static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001358window_find_widget(struct window *window, int32_t x, int32_t y)
1359{
1360 return widget_find_widget(window->main_surface->widget, x, y);
1361}
1362
1363static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001364widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001365{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001366 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001367
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001368 widget = malloc(sizeof *widget);
1369 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001370 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001371 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001372 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001373 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001374 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001375 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001376 widget->tooltip = NULL;
1377 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001378 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001379
1380 return widget;
1381}
1382
1383struct widget *
1384window_add_widget(struct window *window, void *data)
1385{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001386 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001387
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001388 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001389 wl_list_init(&widget->link);
1390 window->main_surface->widget = widget;
1391
1392 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001393}
1394
1395struct widget *
1396widget_add_widget(struct widget *parent, void *data)
1397{
1398 struct widget *widget;
1399
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001400 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001401 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001402
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001403 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001404}
1405
1406void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001407widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001408{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001409 struct display *display = widget->window->display;
1410 struct input *input;
1411
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001412 if (widget->tooltip) {
1413 free(widget->tooltip);
1414 widget->tooltip = NULL;
1415 }
1416
Pekka Paalanene156fb62012-01-19 13:51:38 +02001417 wl_list_for_each(input, &display->input_list, link) {
1418 if (input->focus_widget == widget)
1419 input->focus_widget = NULL;
1420 }
1421
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001422 wl_list_remove(&widget->link);
1423 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001424}
1425
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001426void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001427widget_set_default_cursor(struct widget *widget, int cursor)
1428{
1429 widget->default_cursor = cursor;
1430}
1431
1432void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001433widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001434{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001435 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001436}
1437
1438void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001439widget_set_size(struct widget *widget, int32_t width, int32_t height)
1440{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001441 widget->allocation.width = width;
1442 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001443}
1444
1445void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001446widget_set_allocation(struct widget *widget,
1447 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001448{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001449 widget->allocation.x = x;
1450 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001451 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001452}
1453
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001454void
1455widget_set_transparent(struct widget *widget, int transparent)
1456{
1457 widget->opaque = !transparent;
1458}
1459
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001460void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001461widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001462{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001463 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001464}
1465
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001466static cairo_surface_t *
1467widget_get_cairo_surface(struct widget *widget)
1468{
1469 struct surface *surface = widget->surface;
1470 struct window *window = widget->window;
1471
1472 if (!surface->cairo_surface) {
1473 if (surface == window->main_surface)
1474 window_create_main_surface(window);
1475 else
1476 surface_create_surface(surface, 0, 0, 0);
1477 }
1478
1479 return surface->cairo_surface;
1480}
1481
1482cairo_t *
1483widget_cairo_create(struct widget *widget)
1484{
1485 cairo_surface_t *cairo_surface;
1486 cairo_t *cr;
1487
1488 cairo_surface = widget_get_cairo_surface(widget);
1489 cr = cairo_create(cairo_surface);
1490
1491 return cr;
1492}
1493
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001494void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001495widget_set_resize_handler(struct widget *widget,
1496 widget_resize_handler_t handler)
1497{
1498 widget->resize_handler = handler;
1499}
1500
1501void
1502widget_set_redraw_handler(struct widget *widget,
1503 widget_redraw_handler_t handler)
1504{
1505 widget->redraw_handler = handler;
1506}
1507
1508void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001509widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001510{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001511 widget->enter_handler = handler;
1512}
1513
1514void
1515widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1516{
1517 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001518}
1519
1520void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001521widget_set_motion_handler(struct widget *widget,
1522 widget_motion_handler_t handler)
1523{
1524 widget->motion_handler = handler;
1525}
1526
1527void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001528widget_set_button_handler(struct widget *widget,
1529 widget_button_handler_t handler)
1530{
1531 widget->button_handler = handler;
1532}
1533
1534void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001535widget_set_axis_handler(struct widget *widget,
1536 widget_axis_handler_t handler)
1537{
1538 widget->axis_handler = handler;
1539}
1540
1541void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001542widget_schedule_redraw(struct widget *widget)
1543{
1544 window_schedule_redraw(widget->window);
1545}
1546
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001547cairo_surface_t *
1548window_get_surface(struct window *window)
1549{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001550 cairo_surface_t *cairo_surface;
1551
1552 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1553
1554 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001555}
1556
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001557struct wl_surface *
1558window_get_wl_surface(struct window *window)
1559{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001560 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001561}
1562
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001563struct wl_shell_surface *
1564window_get_wl_shell_surface(struct window *window)
1565{
1566 return window->shell_surface;
1567}
1568
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001569static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001570tooltip_redraw_handler(struct widget *widget, void *data)
1571{
1572 cairo_t *cr;
1573 const int32_t r = 3;
1574 struct tooltip *tooltip = data;
1575 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001576
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001577 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001578 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1579 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1580 cairo_paint(cr);
1581
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001582 width = widget->allocation.width;
1583 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001584 rounded_rect(cr, 0, 0, width, height, r);
1585
1586 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1587 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1588 cairo_fill(cr);
1589
1590 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1591 cairo_move_to(cr, 10, 16);
1592 cairo_show_text(cr, tooltip->entry);
1593 cairo_destroy(cr);
1594}
1595
1596static cairo_text_extents_t
1597get_text_extents(struct tooltip *tooltip)
1598{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001599 cairo_t *cr;
1600 cairo_text_extents_t extents;
1601
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001602 /* Use the dummy_surface because tooltip's surface was not
1603 * created yet, and parent does not have a valid surface
1604 * outside repaint, either.
1605 */
1606 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001607 cairo_text_extents(cr, tooltip->entry, &extents);
1608 cairo_destroy(cr);
1609
1610 return extents;
1611}
1612
1613static int
1614window_create_tooltip(struct tooltip *tooltip)
1615{
1616 struct widget *parent = tooltip->parent;
1617 struct display *display = parent->window->display;
1618 struct window *window;
1619 const int offset_y = 27;
1620 const int margin = 3;
1621 cairo_text_extents_t extents;
1622
1623 if (tooltip->widget)
1624 return 0;
1625
1626 window = window_create_transient(display, parent->window, tooltip->x,
1627 tooltip->y + offset_y,
1628 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1629 if (!window)
1630 return -1;
1631
1632 tooltip->window = window;
1633 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1634
1635 extents = get_text_extents(tooltip);
1636 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1637 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1638
1639 return 0;
1640}
1641
1642void
1643widget_destroy_tooltip(struct widget *parent)
1644{
1645 struct tooltip *tooltip = parent->tooltip;
1646
1647 parent->tooltip_count = 0;
1648 if (!tooltip)
1649 return;
1650
1651 if (tooltip->widget) {
1652 widget_destroy(tooltip->widget);
1653 window_destroy(tooltip->window);
1654 tooltip->widget = NULL;
1655 tooltip->window = NULL;
1656 }
1657
1658 close(tooltip->tooltip_fd);
1659 free(tooltip->entry);
1660 free(tooltip);
1661 parent->tooltip = NULL;
1662}
1663
1664static void
1665tooltip_func(struct task *task, uint32_t events)
1666{
1667 struct tooltip *tooltip =
1668 container_of(task, struct tooltip, tooltip_task);
1669 uint64_t exp;
1670
Martin Olsson8df662a2012-07-08 03:03:47 +02001671 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1672 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001673 window_create_tooltip(tooltip);
1674}
1675
1676#define TOOLTIP_TIMEOUT 500
1677static int
1678tooltip_timer_reset(struct tooltip *tooltip)
1679{
1680 struct itimerspec its;
1681
1682 its.it_interval.tv_sec = 0;
1683 its.it_interval.tv_nsec = 0;
1684 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1685 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1686 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1687 fprintf(stderr, "could not set timerfd\n: %m");
1688 return -1;
1689 }
1690
1691 return 0;
1692}
1693
1694int
1695widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1696{
1697 struct tooltip *tooltip = parent->tooltip;
1698
1699 parent->tooltip_count++;
1700 if (tooltip) {
1701 tooltip->x = x;
1702 tooltip->y = y;
1703 tooltip_timer_reset(tooltip);
1704 return 0;
1705 }
1706
1707 /* the handler might be triggered too fast via input device motion, so
1708 * we need this check here to make sure tooltip is fully initialized */
1709 if (parent->tooltip_count > 1)
1710 return 0;
1711
1712 tooltip = malloc(sizeof *tooltip);
1713 if (!tooltip)
1714 return -1;
1715
1716 parent->tooltip = tooltip;
1717 tooltip->parent = parent;
1718 tooltip->widget = NULL;
1719 tooltip->window = NULL;
1720 tooltip->x = x;
1721 tooltip->y = y;
1722 tooltip->entry = strdup(entry);
1723 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1724 if (tooltip->tooltip_fd < 0) {
1725 fprintf(stderr, "could not create timerfd\n: %m");
1726 return -1;
1727 }
1728
1729 tooltip->tooltip_task.run = tooltip_func;
1730 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1731 EPOLLIN, &tooltip->tooltip_task);
1732 tooltip_timer_reset(tooltip);
1733
1734 return 0;
1735}
1736
1737static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001738workspace_manager_state(void *data,
1739 struct workspace_manager *workspace_manager,
1740 uint32_t current,
1741 uint32_t count)
1742{
1743 struct display *display = data;
1744
1745 display->workspace = current;
1746 display->workspace_count = count;
1747}
1748
1749static const struct workspace_manager_listener workspace_manager_listener = {
1750 workspace_manager_state
1751};
1752
1753static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001754frame_resize_handler(struct widget *widget,
1755 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001756{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001757 struct frame *frame = data;
1758 struct widget *child = frame->child;
1759 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001760 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001761 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001762 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001763 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001764 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001765 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001766 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001767
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001768 switch (widget->window->type) {
1769 case TYPE_FULLSCREEN:
1770 decoration_width = 0;
1771 decoration_height = 0;
1772
1773 allocation.x = 0;
1774 allocation.y = 0;
1775 allocation.width = width;
1776 allocation.height = height;
1777 opaque_margin = 0;
1778
1779 wl_list_for_each(button, &frame->buttons_list, link)
1780 button->widget->opaque = 1;
1781 break;
1782 case TYPE_MAXIMIZED:
1783 decoration_width = t->width * 2;
1784 decoration_height = t->width + t->titlebar_height;
1785
1786 allocation.x = t->width;
1787 allocation.y = t->titlebar_height;
1788 allocation.width = width - decoration_width;
1789 allocation.height = height - decoration_height;
1790
1791 opaque_margin = 0;
1792
1793 wl_list_for_each(button, &frame->buttons_list, link)
1794 button->widget->opaque = 0;
1795 break;
1796 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001797 decoration_width = (t->width + t->margin) * 2;
1798 decoration_height = t->width +
1799 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001800
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001801 allocation.x = t->width + t->margin;
1802 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001803 allocation.width = width - decoration_width;
1804 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001805
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001806 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001807
1808 wl_list_for_each(button, &frame->buttons_list, link)
1809 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001810 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001811 }
1812
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001813 widget_set_allocation(child, allocation.x, allocation.y,
1814 allocation.width, allocation.height);
1815
1816 if (child->resize_handler)
1817 child->resize_handler(child,
1818 allocation.width,
1819 allocation.height,
1820 child->user_data);
1821
Scott Moreauf7e498c2012-05-14 11:39:29 -06001822 width = child->allocation.width + decoration_width;
1823 height = child->allocation.height + decoration_height;
1824
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001825 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1826
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001827 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001828 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001829 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001830 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001831 shadow_margin, shadow_margin,
1832 width - 2 * shadow_margin,
1833 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001834 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001835 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001836 }
1837
Scott Moreauf7e498c2012-05-14 11:39:29 -06001838 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001839
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001840 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001841 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001842 opaque_margin, opaque_margin,
1843 widget->allocation.width - 2 * opaque_margin,
1844 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001845
1846 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001847 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1848 x_l = t->width + shadow_margin;
1849 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001850 wl_list_for_each(button, &frame->buttons_list, link) {
1851 const int button_padding = 4;
1852 w = cairo_image_surface_get_width(button->icon);
1853 h = cairo_image_surface_get_height(button->icon);
1854
1855 if (button->decoration == FRAME_BUTTON_FANCY)
1856 w += 10;
1857
1858 if (button->align == FRAME_BUTTON_LEFT) {
1859 widget_set_allocation(button->widget,
1860 x_l, y , w + 1, h + 1);
1861 x_l += w;
1862 x_l += button_padding;
1863 } else {
1864 x_r -= w;
1865 widget_set_allocation(button->widget,
1866 x_r, y , w + 1, h + 1);
1867 x_r -= button_padding;
1868 }
1869 }
1870}
1871
1872static int
1873frame_button_enter_handler(struct widget *widget,
1874 struct input *input, float x, float y, void *data)
1875{
1876 struct frame_button *frame_button = data;
1877
1878 widget_schedule_redraw(frame_button->widget);
1879 frame_button->state = FRAME_BUTTON_OVER;
1880
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001881 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001882}
1883
1884static void
1885frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1886{
1887 struct frame_button *frame_button = data;
1888
1889 widget_schedule_redraw(frame_button->widget);
1890 frame_button->state = FRAME_BUTTON_DEFAULT;
1891}
1892
1893static void
1894frame_button_button_handler(struct widget *widget,
1895 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001896 uint32_t button,
1897 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001898{
1899 struct frame_button *frame_button = data;
1900 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001901 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001902
1903 if (button != BTN_LEFT)
1904 return;
1905
1906 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001907 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001908 frame_button->state = FRAME_BUTTON_ACTIVE;
1909 widget_schedule_redraw(frame_button->widget);
1910
1911 if (frame_button->type == FRAME_BUTTON_ICON)
1912 window_show_frame_menu(window, input, time);
1913 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001914 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001915 frame_button->state = FRAME_BUTTON_DEFAULT;
1916 widget_schedule_redraw(frame_button->widget);
1917 break;
1918 }
1919
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001920 if (!was_pressed)
1921 return;
1922
Martin Minarik1998b152012-05-10 02:04:35 +02001923 switch (frame_button->type) {
1924 case FRAME_BUTTON_CLOSE:
1925 if (window->close_handler)
1926 window->close_handler(window->parent,
1927 window->user_data);
1928 else
1929 display_exit(window->display);
1930 break;
1931 case FRAME_BUTTON_MINIMIZE:
1932 fprintf(stderr,"Minimize stub\n");
1933 break;
1934 case FRAME_BUTTON_MAXIMIZE:
1935 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1936 break;
1937 default:
1938 /* Unknown operation */
1939 break;
1940 }
1941}
1942
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001943static int
1944frame_button_motion_handler(struct widget *widget,
1945 struct input *input, uint32_t time,
1946 float x, float y, void *data)
1947{
1948 struct frame_button *frame_button = data;
1949 enum frame_button_pointer previous_button_state = frame_button->state;
1950
1951 /* only track state for a pressed button */
1952 if (input->grab != widget)
1953 return CURSOR_LEFT_PTR;
1954
1955 if (x > widget->allocation.x &&
1956 x < (widget->allocation.x + widget->allocation.width) &&
1957 y > widget->allocation.y &&
1958 y < (widget->allocation.y + widget->allocation.height)) {
1959 frame_button->state = FRAME_BUTTON_ACTIVE;
1960 } else {
1961 frame_button->state = FRAME_BUTTON_DEFAULT;
1962 }
1963
1964 if (frame_button->state != previous_button_state)
1965 widget_schedule_redraw(frame_button->widget);
1966
1967 return CURSOR_LEFT_PTR;
1968}
1969
Martin Minarik1998b152012-05-10 02:04:35 +02001970static void
1971frame_button_redraw_handler(struct widget *widget, void *data)
1972{
1973 struct frame_button *frame_button = data;
1974 cairo_t *cr;
1975 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02001976
1977 x = widget->allocation.x;
1978 y = widget->allocation.y;
1979 width = widget->allocation.width;
1980 height = widget->allocation.height;
1981
1982 if (!width)
1983 return;
1984 if (!height)
1985 return;
1986 if (widget->opaque)
1987 return;
1988
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001989 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02001990
1991 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1992 cairo_set_line_width(cr, 1);
1993
1994 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1995 cairo_rectangle (cr, x, y, 25, 16);
1996
1997 cairo_stroke_preserve(cr);
1998
1999 switch (frame_button->state) {
2000 case FRAME_BUTTON_DEFAULT:
2001 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2002 break;
2003 case FRAME_BUTTON_OVER:
2004 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2005 break;
2006 case FRAME_BUTTON_ACTIVE:
2007 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2008 break;
2009 }
2010
2011 cairo_fill (cr);
2012
2013 x += 4;
2014 }
2015
2016 cairo_set_source_surface(cr, frame_button->icon, x, y);
2017 cairo_paint(cr);
2018
2019 cairo_destroy(cr);
2020}
2021
2022static struct widget *
2023frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2024 enum frame_button_align align, enum frame_button_decoration style)
2025{
2026 struct frame_button *frame_button;
2027 const char *icon = data;
2028
2029 frame_button = malloc (sizeof *frame_button);
2030 memset(frame_button, 0, sizeof *frame_button);
2031
2032 frame_button->icon = cairo_image_surface_create_from_png(icon);
2033 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2034 frame_button->frame = frame;
2035 frame_button->type = type;
2036 frame_button->align = align;
2037 frame_button->decoration = style;
2038
2039 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2040
2041 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2042 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2043 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2044 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002045 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002046 return frame_button->widget;
2047}
2048
2049static void
2050frame_button_destroy(struct frame_button *frame_button)
2051{
2052 widget_destroy(frame_button->widget);
2053 wl_list_remove(&frame_button->link);
2054 cairo_surface_destroy(frame_button->icon);
2055 free(frame_button);
2056
2057 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002058}
2059
2060static void
2061frame_redraw_handler(struct widget *widget, void *data)
2062{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002063 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002064 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002065 struct theme *t = window->display->theme;
2066 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002067
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002068 if (window->type == TYPE_FULLSCREEN)
2069 return;
2070
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002071 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002072
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002073 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002074 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002075 if (window->type == TYPE_MAXIMIZED)
2076 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002077 theme_render_frame(t, cr, widget->allocation.width,
2078 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002079
2080 cairo_destroy(cr);
2081}
2082
2083static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002084frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002085{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002086 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002087 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002088 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002089
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002090 if (window->type != TYPE_TOPLEVEL)
2091 return CURSOR_LEFT_PTR;
2092
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002093 location = theme_get_location(t, input->sx, input->sy,
2094 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002095 frame->widget->allocation.height,
2096 window->type == TYPE_MAXIMIZED ?
2097 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002098
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002099 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002100 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002101 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002102 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002103 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002104 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002105 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002106 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002107 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002108 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002109 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002110 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002111 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002112 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002113 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002114 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002115 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002116 case THEME_LOCATION_EXTERIOR:
2117 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002118 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002119 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002120 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002121}
2122
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002123static void
2124frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002125{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002126 struct display *display;
2127
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002128 switch (index) {
2129 case 0: /* close */
2130 if (window->close_handler)
2131 window->close_handler(window->parent,
2132 window->user_data);
2133 else
2134 display_exit(window->display);
2135 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002136 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002137 display = window->display;
2138 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002139 workspace_manager_move_surface(
2140 display->workspace_manager,
2141 window->main_surface->surface,
2142 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002143 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002144 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002145 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002146 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002147 workspace_manager_move_surface(
2148 display->workspace_manager,
2149 window->main_surface->surface,
2150 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002151 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002152 case 3: /* fullscreen */
2153 /* we don't have a way to get out of fullscreen for now */
2154 if (window->fullscreen_handler)
2155 window->fullscreen_handler(window, window->user_data);
2156 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002157 }
2158}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002159
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002160void
2161window_show_frame_menu(struct window *window,
2162 struct input *input, uint32_t time)
2163{
2164 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002165 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002166
2167 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002168 "Close",
2169 "Move to workspace above", "Move to workspace below",
2170 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002171 };
2172
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002173 if (window->fullscreen_handler)
2174 count = ARRAY_LENGTH(entries);
2175 else
2176 count = ARRAY_LENGTH(entries) - 1;
2177
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002178 input_get_position(input, &x, &y);
2179 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002180 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002181}
2182
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002183static int
2184frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002185 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002186{
2187 return frame_get_pointer_image_for_location(data, input);
2188}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002189
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002190static int
2191frame_motion_handler(struct widget *widget,
2192 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002193 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194{
2195 return frame_get_pointer_image_for_location(data, input);
2196}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002197
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002198static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002199frame_button_handler(struct widget *widget,
2200 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002201 uint32_t button, enum wl_pointer_button_state state,
2202 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002203
2204{
2205 struct frame *frame = data;
2206 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002207 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002208 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002209
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002210 if (window->type != TYPE_TOPLEVEL)
2211 return;
2212
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002213 location = theme_get_location(display->theme, input->sx, input->sy,
2214 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002215 frame->widget->allocation.height,
2216 window->type == TYPE_MAXIMIZED ?
2217 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002218
Daniel Stone4dbadb12012-05-30 16:31:51 +01002219 if (window->display->shell && button == BTN_LEFT &&
2220 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002221 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002222 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002223 if (!window->shell_surface)
2224 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002225 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002227 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002228 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002229 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002230 case THEME_LOCATION_RESIZING_TOP:
2231 case THEME_LOCATION_RESIZING_BOTTOM:
2232 case THEME_LOCATION_RESIZING_LEFT:
2233 case THEME_LOCATION_RESIZING_RIGHT:
2234 case THEME_LOCATION_RESIZING_TOP_LEFT:
2235 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2236 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2237 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002238 if (!window->shell_surface)
2239 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002240 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002241
Pekka Paalanen99436862012-11-19 17:15:59 +02002242 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002243 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002244 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002245 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002246 break;
2247 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002248 } else if (button == BTN_RIGHT &&
2249 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002250 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002251 }
2252}
2253
2254struct widget *
2255frame_create(struct window *window, void *data)
2256{
2257 struct frame *frame;
2258
2259 frame = malloc(sizeof *frame);
2260 memset(frame, 0, sizeof *frame);
2261
2262 frame->widget = window_add_widget(window, frame);
2263 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002264
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002265 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2266 widget_set_resize_handler(frame->widget, frame_resize_handler);
2267 widget_set_enter_handler(frame->widget, frame_enter_handler);
2268 widget_set_motion_handler(frame->widget, frame_motion_handler);
2269 widget_set_button_handler(frame->widget, frame_button_handler);
2270
Martin Minarik1998b152012-05-10 02:04:35 +02002271 /* Create empty list for frame buttons */
2272 wl_list_init(&frame->buttons_list);
2273
2274 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2275 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2276
2277 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2278 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2279
2280 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2281 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2282
2283 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2284 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2285
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002286 window->frame = frame;
2287
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002288 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002289}
2290
Kristian Høgsberga1627922012-06-20 17:30:03 -04002291void
2292frame_set_child_size(struct widget *widget, int child_width, int child_height)
2293{
2294 struct display *display = widget->window->display;
2295 struct theme *t = display->theme;
2296 int decoration_width, decoration_height;
2297 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002298 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002299
2300 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002301 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002302 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002303 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002304
2305 width = child_width + decoration_width;
2306 height = child_height + decoration_height;
2307 } else {
2308 width = child_width;
2309 height = child_height;
2310 }
2311
2312 window_schedule_resize(widget->window, width, height);
2313}
2314
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002315static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002316frame_destroy(struct frame *frame)
2317{
Martin Minarik1998b152012-05-10 02:04:35 +02002318 struct frame_button *button, *tmp;
2319
2320 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2321 frame_button_destroy(button);
2322
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002323 /* frame->child must be destroyed by the application */
2324 widget_destroy(frame->widget);
2325 free(frame);
2326}
2327
2328static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002329input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002330 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002331{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002332 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002333 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002334
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002335 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002336 return;
2337
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002338 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002339 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002340 widget = old;
2341 if (input->grab)
2342 widget = input->grab;
2343 if (widget->leave_handler)
2344 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002345 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002346 }
2347
2348 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002349 widget = focus;
2350 if (input->grab)
2351 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002352 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002353 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002354 cursor = widget->enter_handler(focus, input, x, y,
2355 widget->user_data);
2356 else
2357 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002358
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002359 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002360 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002361}
2362
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002363void
2364input_grab(struct input *input, struct widget *widget, uint32_t button)
2365{
2366 input->grab = widget;
2367 input->grab_button = button;
2368}
2369
2370void
2371input_ungrab(struct input *input)
2372{
2373 struct widget *widget;
2374
2375 input->grab = NULL;
2376 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002377 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002378 input->sx, input->sy);
2379 input_set_focus_widget(input, widget, input->sx, input->sy);
2380 }
2381}
2382
2383static void
2384input_remove_pointer_focus(struct input *input)
2385{
2386 struct window *window = input->pointer_focus;
2387
2388 if (!window)
2389 return;
2390
2391 input_set_focus_widget(input, NULL, 0, 0);
2392
2393 input->pointer_focus = NULL;
2394 input->current_cursor = CURSOR_UNSET;
2395}
2396
2397static void
2398pointer_handle_enter(void *data, struct wl_pointer *pointer,
2399 uint32_t serial, struct wl_surface *surface,
2400 wl_fixed_t sx_w, wl_fixed_t sy_w)
2401{
2402 struct input *input = data;
2403 struct window *window;
2404 struct widget *widget;
2405 float sx = wl_fixed_to_double(sx_w);
2406 float sy = wl_fixed_to_double(sy_w);
2407
2408 if (!surface) {
2409 /* enter event for a window we've just destroyed */
2410 return;
2411 }
2412
2413 input->display->serial = serial;
2414 input->pointer_enter_serial = serial;
2415 input->pointer_focus = wl_surface_get_user_data(surface);
2416 window = input->pointer_focus;
2417
Pekka Paalanen99436862012-11-19 17:15:59 +02002418 if (window->resizing) {
2419 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002420 /* Schedule a redraw to free the pool */
2421 window_schedule_redraw(window);
2422 }
2423
2424 input->sx = sx;
2425 input->sy = sy;
2426
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002427 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002428 input_set_focus_widget(input, widget, sx, sy);
2429}
2430
2431static void
2432pointer_handle_leave(void *data, struct wl_pointer *pointer,
2433 uint32_t serial, struct wl_surface *surface)
2434{
2435 struct input *input = data;
2436
2437 input->display->serial = serial;
2438 input_remove_pointer_focus(input);
2439}
2440
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002441static void
Daniel Stone37816df2012-05-16 18:45:18 +01002442pointer_handle_motion(void *data, struct wl_pointer *pointer,
2443 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002444{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002445 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002446 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002447 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002448 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002449 float sx = wl_fixed_to_double(sx_w);
2450 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002451
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002452 input->sx = sx;
2453 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002454
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002455 if (!window)
2456 return;
2457
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002458 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002459 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002460 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002461 }
2462
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002463 if (input->grab)
2464 widget = input->grab;
2465 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002466 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002467 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002468 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002469 input, time, sx, sy,
2470 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002471 else
2472 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002473
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002474 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002475}
2476
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002477static void
Daniel Stone37816df2012-05-16 18:45:18 +01002478pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002479 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002480{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002481 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002482 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002483 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002484
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002485 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002486 if (input->focus_widget && input->grab == NULL &&
2487 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002488 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002489
Neil Roberts6b28aad2012-01-23 19:11:18 +00002490 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002491 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002492 (*widget->button_handler)(widget,
2493 input, time,
2494 button, state,
2495 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002496
Daniel Stone4dbadb12012-05-30 16:31:51 +01002497 if (input->grab && input->grab_button == button &&
2498 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002499 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002500}
2501
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002502static void
Daniel Stone37816df2012-05-16 18:45:18 +01002503pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002504 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002505{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002506 struct input *input = data;
2507 struct widget *widget;
2508
2509 widget = input->focus_widget;
2510 if (input->grab)
2511 widget = input->grab;
2512 if (widget && widget->axis_handler)
2513 (*widget->axis_handler)(widget,
2514 input, time,
2515 axis, value,
2516 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002517}
2518
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002519static const struct wl_pointer_listener pointer_listener = {
2520 pointer_handle_enter,
2521 pointer_handle_leave,
2522 pointer_handle_motion,
2523 pointer_handle_button,
2524 pointer_handle_axis,
2525};
2526
2527static void
2528input_remove_keyboard_focus(struct input *input)
2529{
2530 struct window *window = input->keyboard_focus;
2531 struct itimerspec its;
2532
2533 its.it_interval.tv_sec = 0;
2534 its.it_interval.tv_nsec = 0;
2535 its.it_value.tv_sec = 0;
2536 its.it_value.tv_nsec = 0;
2537 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2538
2539 if (!window)
2540 return;
2541
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002542 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002543 if (window->keyboard_focus_handler)
2544 (*window->keyboard_focus_handler)(window, NULL,
2545 window->user_data);
2546
2547 input->keyboard_focus = NULL;
2548}
2549
2550static void
2551keyboard_repeat_func(struct task *task, uint32_t events)
2552{
2553 struct input *input =
2554 container_of(task, struct input, repeat_task);
2555 struct window *window = input->keyboard_focus;
2556 uint64_t exp;
2557
2558 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2559 /* If we change the timer between the fd becoming
2560 * readable and getting here, there'll be nothing to
2561 * read and we get EAGAIN. */
2562 return;
2563
2564 if (window && window->key_handler) {
2565 (*window->key_handler)(window, input, input->repeat_time,
2566 input->repeat_key, input->repeat_sym,
2567 WL_KEYBOARD_KEY_STATE_PRESSED,
2568 window->user_data);
2569 }
2570}
2571
2572static void
2573keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2574 uint32_t format, int fd, uint32_t size)
2575{
2576 struct input *input = data;
2577 char *map_str;
2578
2579 if (!data) {
2580 close(fd);
2581 return;
2582 }
2583
2584 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2585 close(fd);
2586 return;
2587 }
2588
2589 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2590 if (map_str == MAP_FAILED) {
2591 close(fd);
2592 return;
2593 }
2594
2595 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2596 map_str,
2597 XKB_KEYMAP_FORMAT_TEXT_V1,
2598 0);
2599 munmap(map_str, size);
2600 close(fd);
2601
2602 if (!input->xkb.keymap) {
2603 fprintf(stderr, "failed to compile keymap\n");
2604 return;
2605 }
2606
2607 input->xkb.state = xkb_state_new(input->xkb.keymap);
2608 if (!input->xkb.state) {
2609 fprintf(stderr, "failed to create XKB state\n");
2610 xkb_map_unref(input->xkb.keymap);
2611 input->xkb.keymap = NULL;
2612 return;
2613 }
2614
2615 input->xkb.control_mask =
2616 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2617 input->xkb.alt_mask =
2618 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2619 input->xkb.shift_mask =
2620 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2621}
2622
2623static void
2624keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2625 uint32_t serial, struct wl_surface *surface,
2626 struct wl_array *keys)
2627{
2628 struct input *input = data;
2629 struct window *window;
2630
2631 input->display->serial = serial;
2632 input->keyboard_focus = wl_surface_get_user_data(surface);
2633
2634 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002635 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002636 if (window->keyboard_focus_handler)
2637 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002638 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002639}
2640
2641static void
2642keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2643 uint32_t serial, struct wl_surface *surface)
2644{
2645 struct input *input = data;
2646
2647 input->display->serial = serial;
2648 input_remove_keyboard_focus(input);
2649}
2650
Scott Moreau210d0792012-03-22 10:47:01 -06002651static void
Daniel Stone37816df2012-05-16 18:45:18 +01002652keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002653 uint32_t serial, uint32_t time, uint32_t key,
2654 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002655{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002656 struct input *input = data;
2657 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002658 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002659 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002660 const xkb_keysym_t *syms;
2661 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002662 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002663
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002664 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002665 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002666 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002667 return;
2668
Daniel Stone97f68542012-05-30 16:32:01 +01002669 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002670
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002671 sym = XKB_KEY_NoSymbol;
2672 if (num_syms == 1)
2673 sym = syms[0];
2674
2675 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002676 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002677 window_set_maximized(window,
2678 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002679 } else if (sym == XKB_KEY_F11 &&
2680 window->fullscreen_handler &&
2681 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2682 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002683 } else if (sym == XKB_KEY_F4 &&
2684 input->modifiers == MOD_ALT_MASK &&
2685 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2686 if (window->close_handler)
2687 window->close_handler(window->parent,
2688 window->user_data);
2689 else
2690 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002691 } else if (window->key_handler) {
2692 (*window->key_handler)(window, input, time, key,
2693 sym, state, window->user_data);
2694 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002695
2696 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2697 key == input->repeat_key) {
2698 its.it_interval.tv_sec = 0;
2699 its.it_interval.tv_nsec = 0;
2700 its.it_value.tv_sec = 0;
2701 its.it_value.tv_nsec = 0;
2702 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2703 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2704 input->repeat_sym = sym;
2705 input->repeat_key = key;
2706 input->repeat_time = time;
2707 its.it_interval.tv_sec = 0;
2708 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2709 its.it_value.tv_sec = 0;
2710 its.it_value.tv_nsec = 400 * 1000 * 1000;
2711 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2712 }
2713}
2714
2715static void
Daniel Stone351eb612012-05-31 15:27:47 -04002716keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2717 uint32_t serial, uint32_t mods_depressed,
2718 uint32_t mods_latched, uint32_t mods_locked,
2719 uint32_t group)
2720{
2721 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002722 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002723
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002724 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2725 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002726 mask = xkb_state_serialize_mods(input->xkb.state,
2727 XKB_STATE_DEPRESSED |
2728 XKB_STATE_LATCHED);
2729 input->modifiers = 0;
2730 if (mask & input->xkb.control_mask)
2731 input->modifiers |= MOD_CONTROL_MASK;
2732 if (mask & input->xkb.alt_mask)
2733 input->modifiers |= MOD_ALT_MASK;
2734 if (mask & input->xkb.shift_mask)
2735 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002736}
2737
Daniel Stone37816df2012-05-16 18:45:18 +01002738static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002739 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002740 keyboard_handle_enter,
2741 keyboard_handle_leave,
2742 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002743 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002744};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002745
2746static void
Daniel Stone37816df2012-05-16 18:45:18 +01002747seat_handle_capabilities(void *data, struct wl_seat *seat,
2748 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002749{
Daniel Stone37816df2012-05-16 18:45:18 +01002750 struct input *input = data;
2751
2752 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2753 input->pointer = wl_seat_get_pointer(seat);
2754 wl_pointer_set_user_data(input->pointer, input);
2755 wl_pointer_add_listener(input->pointer, &pointer_listener,
2756 input);
2757 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2758 wl_pointer_destroy(input->pointer);
2759 input->pointer = NULL;
2760 }
2761
2762 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2763 input->keyboard = wl_seat_get_keyboard(seat);
2764 wl_keyboard_set_user_data(input->keyboard, input);
2765 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2766 input);
2767 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2768 wl_keyboard_destroy(input->keyboard);
2769 input->keyboard = NULL;
2770 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002771}
2772
Daniel Stone37816df2012-05-16 18:45:18 +01002773static const struct wl_seat_listener seat_listener = {
2774 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002775};
2776
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002777void
2778input_get_position(struct input *input, int32_t *x, int32_t *y)
2779{
2780 *x = input->sx;
2781 *y = input->sy;
2782}
2783
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002784struct display *
2785input_get_display(struct input *input)
2786{
2787 return input->display;
2788}
2789
Daniel Stone37816df2012-05-16 18:45:18 +01002790struct wl_seat *
2791input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002792{
Daniel Stone37816df2012-05-16 18:45:18 +01002793 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002794}
2795
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002796uint32_t
2797input_get_modifiers(struct input *input)
2798{
2799 return input->modifiers;
2800}
2801
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002802struct widget *
2803input_get_focus_widget(struct input *input)
2804{
2805 return input->focus_widget;
2806}
2807
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002808struct data_offer {
2809 struct wl_data_offer *offer;
2810 struct input *input;
2811 struct wl_array types;
2812 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002813
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002814 struct task io_task;
2815 int fd;
2816 data_func_t func;
2817 int32_t x, y;
2818 void *user_data;
2819};
2820
2821static void
2822data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2823{
2824 struct data_offer *offer = data;
2825 char **p;
2826
2827 p = wl_array_add(&offer->types, sizeof *p);
2828 *p = strdup(type);
2829}
2830
2831static const struct wl_data_offer_listener data_offer_listener = {
2832 data_offer_offer,
2833};
2834
2835static void
2836data_offer_destroy(struct data_offer *offer)
2837{
2838 char **p;
2839
2840 offer->refcount--;
2841 if (offer->refcount == 0) {
2842 wl_data_offer_destroy(offer->offer);
2843 for (p = offer->types.data; *p; p++)
2844 free(*p);
2845 wl_array_release(&offer->types);
2846 free(offer);
2847 }
2848}
2849
2850static void
2851data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002852 struct wl_data_device *data_device,
2853 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002854{
2855 struct data_offer *offer;
2856
2857 offer = malloc(sizeof *offer);
2858
2859 wl_array_init(&offer->types);
2860 offer->refcount = 1;
2861 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002862 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002863 wl_data_offer_add_listener(offer->offer,
2864 &data_offer_listener, offer);
2865}
2866
2867static void
2868data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002869 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002870 wl_fixed_t x_w, wl_fixed_t y_w,
2871 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002872{
2873 struct input *input = data;
2874 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002875 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002876 float x = wl_fixed_to_double(x_w);
2877 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002878 char **p;
2879
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002880 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002881 window = wl_surface_get_user_data(surface);
2882 input->pointer_focus = window;
2883
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002884 if (offer) {
2885 input->drag_offer = wl_data_offer_get_user_data(offer);
2886
2887 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2888 *p = NULL;
2889
2890 types_data = input->drag_offer->types.data;
2891 } else {
2892 input->drag_offer = NULL;
2893 types_data = NULL;
2894 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002895
2896 window = input->pointer_focus;
2897 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002898 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002899 window->user_data);
2900}
2901
2902static void
2903data_device_leave(void *data, struct wl_data_device *data_device)
2904{
2905 struct input *input = data;
2906
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002907 if (input->drag_offer) {
2908 data_offer_destroy(input->drag_offer);
2909 input->drag_offer = NULL;
2910 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002911}
2912
2913static void
2914data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002915 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002916{
2917 struct input *input = data;
2918 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002919 float x = wl_fixed_to_double(x_w);
2920 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002921 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002922
2923 input->sx = x;
2924 input->sy = y;
2925
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002926 if (input->drag_offer)
2927 types_data = input->drag_offer->types.data;
2928 else
2929 types_data = NULL;
2930
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002931 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002932 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002933 window->user_data);
2934}
2935
2936static void
2937data_device_drop(void *data, struct wl_data_device *data_device)
2938{
2939 struct input *input = data;
2940 struct window *window = input->pointer_focus;
2941
2942 if (window->drop_handler)
2943 window->drop_handler(window, input,
2944 input->sx, input->sy, window->user_data);
2945}
2946
2947static void
2948data_device_selection(void *data,
2949 struct wl_data_device *wl_data_device,
2950 struct wl_data_offer *offer)
2951{
2952 struct input *input = data;
2953 char **p;
2954
2955 if (input->selection_offer)
2956 data_offer_destroy(input->selection_offer);
2957
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002958 if (offer) {
2959 input->selection_offer = wl_data_offer_get_user_data(offer);
2960 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2961 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002962 } else {
2963 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002964 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002965}
2966
2967static const struct wl_data_device_listener data_device_listener = {
2968 data_device_data_offer,
2969 data_device_enter,
2970 data_device_leave,
2971 data_device_motion,
2972 data_device_drop,
2973 data_device_selection
2974};
2975
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002976static void
2977input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002978{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002979 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002980 struct wl_cursor *cursor;
2981 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002982
Daniel Stone80972742012-11-07 17:51:39 +11002983 if (!input->pointer)
2984 return;
2985
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002986 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002987 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002988 return;
2989
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002990 if (index >= (int) cursor->image_count) {
2991 fprintf(stderr, "cursor index out of range\n");
2992 return;
2993 }
2994
2995 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002996 buffer = wl_cursor_image_get_buffer(image);
2997 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002998 return;
2999
Kristian Høgsbergae277372012-08-01 09:41:08 -04003000 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003001 input->pointer_surface,
3002 image->hotspot_x, image->hotspot_y);
3003 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3004 wl_surface_damage(input->pointer_surface, 0, 0,
3005 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003006 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003007}
3008
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003009static const struct wl_callback_listener pointer_surface_listener;
3010
3011static void
3012pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3013 uint32_t time)
3014{
3015 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003016 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003017 int i;
3018
3019 if (callback) {
3020 assert(callback == input->cursor_frame_cb);
3021 wl_callback_destroy(callback);
3022 input->cursor_frame_cb = NULL;
3023 }
3024
Daniel Stone80972742012-11-07 17:51:39 +11003025 if (!input->pointer)
3026 return;
3027
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003028 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003029 wl_pointer_set_cursor(input->pointer,
3030 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003031 NULL, 0, 0);
3032 return;
3033 }
3034
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003035 if (input->current_cursor == CURSOR_UNSET)
3036 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003037 cursor = input->display->cursors[input->current_cursor];
3038 if (!cursor)
3039 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003040
3041 /* FIXME We don't have the current time on the first call so we set
3042 * the animation start to the time of the first frame callback. */
3043 if (time == 0)
3044 input->cursor_anim_start = 0;
3045 else if (input->cursor_anim_start == 0)
3046 input->cursor_anim_start = time;
3047
3048 if (time == 0 || input->cursor_anim_start == 0)
3049 i = 0;
3050 else
3051 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3052
Pekka Paalanenbc106382012-10-10 12:49:31 +03003053 if (cursor->image_count > 1) {
3054 input->cursor_frame_cb =
3055 wl_surface_frame(input->pointer_surface);
3056 wl_callback_add_listener(input->cursor_frame_cb,
3057 &pointer_surface_listener, input);
3058 }
3059
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003060 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003061}
3062
3063static const struct wl_callback_listener pointer_surface_listener = {
3064 pointer_surface_frame_callback
3065};
3066
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003067void
3068input_set_pointer_image(struct input *input, int pointer)
3069{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003070 int force = 0;
3071
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003072 if (!input->pointer)
3073 return;
3074
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003075 if (input->pointer_enter_serial > input->cursor_serial)
3076 force = 1;
3077
3078 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003079 return;
3080
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003081 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003082 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003083 if (!input->cursor_frame_cb)
3084 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003085 else if (force) {
3086 /* The current frame callback may be stuck if, for instance,
3087 * the set cursor request was processed by the server after
3088 * this client lost the focus. In this case the cursor surface
3089 * might not be mapped and the frame callback wouldn't ever
3090 * complete. Send a set_cursor and attach to try to map the
3091 * cursor surface again so that the callback will finish */
3092 input_set_pointer_image_index(input, 0);
3093 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003094}
3095
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003096struct wl_data_device *
3097input_get_data_device(struct input *input)
3098{
3099 return input->data_device;
3100}
3101
3102void
3103input_set_selection(struct input *input,
3104 struct wl_data_source *source, uint32_t time)
3105{
3106 wl_data_device_set_selection(input->data_device, source, time);
3107}
3108
3109void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003110input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003111{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003112 wl_data_offer_accept(input->drag_offer->offer,
3113 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003114}
3115
3116static void
3117offer_io_func(struct task *task, uint32_t events)
3118{
3119 struct data_offer *offer =
3120 container_of(task, struct data_offer, io_task);
3121 unsigned int len;
3122 char buffer[4096];
3123
3124 len = read(offer->fd, buffer, sizeof buffer);
3125 offer->func(buffer, len,
3126 offer->x, offer->y, offer->user_data);
3127
3128 if (len == 0) {
3129 close(offer->fd);
3130 data_offer_destroy(offer);
3131 }
3132}
3133
3134static void
3135data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3136 data_func_t func, void *user_data)
3137{
3138 int p[2];
3139
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003140 if (pipe2(p, O_CLOEXEC) == -1)
3141 return;
3142
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003143 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3144 close(p[1]);
3145
3146 offer->io_task.run = offer_io_func;
3147 offer->fd = p[0];
3148 offer->func = func;
3149 offer->refcount++;
3150 offer->user_data = user_data;
3151
3152 display_watch_fd(offer->input->display,
3153 offer->fd, EPOLLIN, &offer->io_task);
3154}
3155
3156void
3157input_receive_drag_data(struct input *input, const char *mime_type,
3158 data_func_t func, void *data)
3159{
3160 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3161 input->drag_offer->x = input->sx;
3162 input->drag_offer->y = input->sy;
3163}
3164
3165int
3166input_receive_selection_data(struct input *input, const char *mime_type,
3167 data_func_t func, void *data)
3168{
3169 char **p;
3170
3171 if (input->selection_offer == NULL)
3172 return -1;
3173
3174 for (p = input->selection_offer->types.data; *p; p++)
3175 if (strcmp(mime_type, *p) == 0)
3176 break;
3177
3178 if (*p == NULL)
3179 return -1;
3180
3181 data_offer_receive_data(input->selection_offer,
3182 mime_type, func, data);
3183 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003184}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003185
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003186int
3187input_receive_selection_data_to_fd(struct input *input,
3188 const char *mime_type, int fd)
3189{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003190 if (input->selection_offer)
3191 wl_data_offer_receive(input->selection_offer->offer,
3192 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003193
3194 return 0;
3195}
3196
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003197void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003198window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003199{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003200 if (!window->shell_surface)
3201 return;
3202
Daniel Stone37816df2012-05-16 18:45:18 +01003203 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003204}
3205
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003206static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003207surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003208{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003209 struct widget *widget = surface->widget;
3210 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003211
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003212 if (surface->input_region) {
3213 wl_region_destroy(surface->input_region);
3214 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003215 }
3216
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003217 if (surface->opaque_region)
3218 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003219
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003220 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003221
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003222 if (widget->resize_handler)
3223 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003224 widget->allocation.width,
3225 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003226 widget->user_data);
3227
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003228 if (surface->allocation.width != widget->allocation.width ||
3229 surface->allocation.height != widget->allocation.height) {
3230 surface->allocation = widget->allocation;
3231 window_schedule_redraw(widget->window);
3232 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003233
3234 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003235 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003236 widget->allocation.width,
3237 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003238}
3239
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003240static void
3241idle_resize(struct window *window)
3242{
3243 window->resize_needed = 0;
3244
3245 widget_set_allocation(window->main_surface->widget,
3246 window->pending_allocation.x,
3247 window->pending_allocation.y,
3248 window->pending_allocation.width,
3249 window->pending_allocation.height);
3250
3251 surface_resize(window->main_surface);
3252}
3253
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003254void
3255window_schedule_resize(struct window *window, int width, int height)
3256{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003257 window->pending_allocation.x = 0;
3258 window->pending_allocation.y = 0;
3259 window->pending_allocation.width = width;
3260 window->pending_allocation.height = height;
3261
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003262 if (window->min_allocation.width == 0)
3263 window->min_allocation = window->pending_allocation;
3264 if (window->pending_allocation.width < window->min_allocation.width)
3265 window->pending_allocation.width = window->min_allocation.width;
3266 if (window->pending_allocation.height < window->min_allocation.height)
3267 window->pending_allocation.height = window->min_allocation.height;
3268
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003269 window->resize_needed = 1;
3270 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003271}
3272
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003273void
3274widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3275{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003276 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003277}
3278
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003279static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003280handle_ping(void *data, struct wl_shell_surface *shell_surface,
3281 uint32_t serial)
3282{
3283 wl_shell_surface_pong(shell_surface, serial);
3284}
3285
3286static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003287handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003288 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003289{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003290 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003291
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003292 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003293 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003294}
3295
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003296static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003297menu_destroy(struct menu *menu)
3298{
3299 widget_destroy(menu->widget);
3300 window_destroy(menu->window);
3301 free(menu);
3302}
3303
3304static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003305handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3306{
3307 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003308 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003309
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003310 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003311 * device. Or just use wl_callback. And this really needs to
3312 * be a window vfunc that the menu can set. And we need the
3313 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003314
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003315 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003316 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003317}
3318
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003319static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003320 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003321 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003322 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003323};
3324
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003325void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003326window_get_allocation(struct window *window,
3327 struct rectangle *allocation)
3328{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003329 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003330}
3331
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003332static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003333widget_redraw(struct widget *widget)
3334{
3335 struct widget *child;
3336
3337 if (widget->redraw_handler)
3338 widget->redraw_handler(widget, widget->user_data);
3339 wl_list_for_each(child, &widget->child_list, link)
3340 widget_redraw(child);
3341}
3342
3343static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003344frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3345{
3346 struct window *window = data;
3347
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003348 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003349 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003350 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003351 window->redraw_scheduled = 0;
3352 if (window->redraw_needed)
3353 window_schedule_redraw(window);
3354}
3355
3356static const struct wl_callback_listener listener = {
3357 frame_callback
3358};
3359
3360static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003361idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003362{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003363 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003364
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003365 if (window->resize_needed)
3366 idle_resize(window);
3367
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003368 widget_redraw(window->main_surface->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003369 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003370 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003371
Pekka Paalanen4e373742013-02-13 16:17:13 +02003372 window->frame_cb = wl_surface_frame(window->main_surface->surface);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003373 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003374 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003375}
3376
3377void
3378window_schedule_redraw(struct window *window)
3379{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003380 window->redraw_needed = 1;
MoD063a8822013-03-02 23:43:57 +00003381 if (window->configure_requests)
3382 return;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003383 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003384 window->redraw_task.run = idle_redraw;
3385 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003386 window->redraw_scheduled = 1;
3387 }
3388}
3389
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003390int
3391window_is_fullscreen(struct window *window)
3392{
3393 return window->type == TYPE_FULLSCREEN;
3394}
3395
MoD063a8822013-03-02 23:43:57 +00003396static void
3397configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3398{
3399 struct window *window = data;
3400
3401 wl_callback_destroy(callback);
3402 window->configure_requests--;
3403
3404 if (!window->configure_requests)
3405 window_schedule_redraw(window);
3406}
3407
3408static struct wl_callback_listener configure_request_listener = {
3409 configure_request_completed,
3410};
3411
3412static void
3413window_defer_redraw_until_configure(struct window* window)
3414{
3415 struct wl_callback *callback;
3416
3417 if (window->redraw_scheduled) {
3418 wl_list_remove(&window->redraw_task.link);
3419 window->redraw_scheduled = 0;
3420 }
3421 if (window->frame_cb) {
3422 wl_callback_destroy(window->frame_cb);
3423 window->frame_cb = 0;
3424 }
3425
3426 callback = wl_display_sync(window->display->display);
3427 wl_callback_add_listener(callback, &configure_request_listener, window);
3428 window->configure_requests++;
3429}
3430
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003431void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003432window_set_fullscreen(struct window *window, int fullscreen)
3433{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003434 if (!window->display->shell)
3435 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003436
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003437 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003438 return;
3439
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003440 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003441 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003442 if (window->type == TYPE_TOPLEVEL) {
3443 window->saved_allocation = window->main_surface->allocation;
3444 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003445 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003446 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003447 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003448 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003449 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003450 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003451 if (window->saved_type == TYPE_MAXIMIZED) {
3452 window_set_maximized(window, 1);
3453 } else {
3454 window->type = TYPE_TOPLEVEL;
3455 wl_shell_surface_set_toplevel(window->shell_surface);
3456 window_schedule_resize(window,
3457 window->saved_allocation.width,
3458 window->saved_allocation.height);
3459 }
3460
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003461 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003462}
3463
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003464void
3465window_set_fullscreen_method(struct window *window,
3466 enum wl_shell_surface_fullscreen_method method)
3467{
3468 window->fullscreen_method = method;
3469}
3470
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003471int
3472window_is_maximized(struct window *window)
3473{
3474 return window->type == TYPE_MAXIMIZED;
3475}
3476
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003477void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003478window_set_maximized(struct window *window, int maximized)
3479{
3480 if (!window->display->shell)
3481 return;
3482
3483 if ((window->type == TYPE_MAXIMIZED) == maximized)
3484 return;
3485
3486 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003487 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003488 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3489 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003490 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003491 } else if (window->type == TYPE_FULLSCREEN) {
3492 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3493 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003494 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003495 } else {
3496 wl_shell_surface_set_toplevel(window->shell_surface);
3497 window->type = TYPE_TOPLEVEL;
3498 window_schedule_resize(window,
3499 window->saved_allocation.width,
3500 window->saved_allocation.height);
3501 }
3502}
3503
3504void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003505window_set_user_data(struct window *window, void *data)
3506{
3507 window->user_data = data;
3508}
3509
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003510void *
3511window_get_user_data(struct window *window)
3512{
3513 return window->user_data;
3514}
3515
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003516void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003517window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003518 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003519{
3520 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003521}
3522
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003523void
3524window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003525 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003526{
3527 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003528}
3529
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003530void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003531window_set_data_handler(struct window *window, window_data_handler_t handler)
3532{
3533 window->data_handler = handler;
3534}
3535
3536void
3537window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3538{
3539 window->drop_handler = handler;
3540}
3541
3542void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003543window_set_close_handler(struct window *window,
3544 window_close_handler_t handler)
3545{
3546 window->close_handler = handler;
3547}
3548
3549void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003550window_set_fullscreen_handler(struct window *window,
3551 window_fullscreen_handler_t handler)
3552{
3553 window->fullscreen_handler = handler;
3554}
3555
3556void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003557window_set_output_handler(struct window *window,
3558 window_output_handler_t handler)
3559{
3560 window->output_handler = handler;
3561}
3562
3563void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003564window_set_title(struct window *window, const char *title)
3565{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003566 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003567 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003568 if (window->shell_surface)
3569 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003570}
3571
3572const char *
3573window_get_title(struct window *window)
3574{
3575 return window->title;
3576}
3577
3578void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003579window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3580{
3581 struct text_cursor_position *text_cursor_position =
3582 window->display->text_cursor_position;
3583
Scott Moreau9295ce02012-06-01 12:46:10 -06003584 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003585 return;
3586
3587 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003588 window->main_surface->surface,
3589 wl_fixed_from_int(x),
3590 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003591}
3592
3593void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003594window_damage(struct window *window, int32_t x, int32_t y,
3595 int32_t width, int32_t height)
3596{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003597 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003598}
3599
Casey Dahlin9074db52012-04-19 22:50:09 -04003600static void
3601surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003602 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003603{
Rob Bradford7507b572012-05-15 17:55:34 +01003604 struct window *window = data;
3605 struct output *output;
3606 struct output *output_found = NULL;
3607 struct window_output *window_output;
3608
3609 wl_list_for_each(output, &window->display->output_list, link) {
3610 if (output->output == wl_output) {
3611 output_found = output;
3612 break;
3613 }
3614 }
3615
3616 if (!output_found)
3617 return;
3618
3619 window_output = malloc (sizeof *window_output);
3620 window_output->output = output_found;
3621
3622 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003623
3624 if (window->output_handler)
3625 window->output_handler(window, output_found, 1,
3626 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003627}
3628
3629static void
3630surface_leave(void *data,
3631 struct wl_surface *wl_surface, struct wl_output *output)
3632{
Rob Bradford7507b572012-05-15 17:55:34 +01003633 struct window *window = data;
3634 struct window_output *window_output;
3635 struct window_output *window_output_found = NULL;
3636
3637 wl_list_for_each(window_output, &window->window_output_list, link) {
3638 if (window_output->output->output == output) {
3639 window_output_found = window_output;
3640 break;
3641 }
3642 }
3643
3644 if (window_output_found) {
3645 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003646
3647 if (window->output_handler)
3648 window->output_handler(window, window_output->output,
3649 0, window->user_data);
3650
Rob Bradford7507b572012-05-15 17:55:34 +01003651 free(window_output_found);
3652 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003653}
3654
3655static const struct wl_surface_listener surface_listener = {
3656 surface_enter,
3657 surface_leave
3658};
3659
Pekka Paalanen4e373742013-02-13 16:17:13 +02003660static struct surface *
3661surface_create(struct window *window)
3662{
3663 struct display *display = window->display;
3664 struct surface *surface;
3665
3666 surface = calloc(1, sizeof *surface);
3667 if (!surface)
3668 return NULL;
3669
3670 surface->window = window;
3671 surface->surface = wl_compositor_create_surface(display->compositor);
3672 wl_surface_add_listener(surface->surface, &surface_listener, window);
3673
3674 return surface;
3675}
3676
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003677static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003678window_create_internal(struct display *display,
3679 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003680{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003681 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003682 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003683
3684 window = malloc(sizeof *window);
3685 if (window == NULL)
3686 return NULL;
3687
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003688 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003689 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003690 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003691
3692 surface = surface_create(window);
3693 window->main_surface = surface;
3694
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003695 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003696 window->shell_surface =
3697 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003698 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003699 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003700
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003701 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003702 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00003703 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003704
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003705 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003706#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003707 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003708#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003709 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003710#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003711 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003712 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003713
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003714 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003715 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003716 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003717
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003718 if (window->shell_surface) {
3719 wl_shell_surface_set_user_data(window->shell_surface, window);
3720 wl_shell_surface_add_listener(window->shell_surface,
3721 &shell_surface_listener, window);
3722 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003723
Rob Bradford7507b572012-05-15 17:55:34 +01003724 wl_list_init (&window->window_output_list);
3725
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003726 return window;
3727}
3728
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003729struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003730window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003731{
3732 struct window *window;
3733
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003734 window = window_create_internal(display, NULL, TYPE_NONE);
3735 if (!window)
3736 return NULL;
3737
3738 return window;
3739}
3740
3741struct window *
3742window_create_custom(struct display *display)
3743{
3744 struct window *window;
3745
3746 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003747 if (!window)
3748 return NULL;
3749
3750 return window;
3751}
3752
3753struct window *
3754window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003755 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003756{
3757 struct window *window;
3758
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003759 window = window_create_internal(parent->display,
3760 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003761 if (!window)
3762 return NULL;
3763
3764 window->x = x;
3765 window->y = y;
3766
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003767 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003768 wl_shell_surface_set_transient(
3769 window->shell_surface,
3770 window->parent->main_surface->surface,
3771 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003772
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003773 return window;
3774}
3775
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003776static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003777menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003778{
3779 int next;
3780
3781 next = (sy - 8) / 20;
3782 if (menu->current != next) {
3783 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003784 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003785 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003786}
3787
3788static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003789menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003790 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003791 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003792{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003793 struct menu *menu = data;
3794
3795 if (widget == menu->widget)
3796 menu_set_item(data, y);
3797
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003798 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003799}
3800
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003801static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003802menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003803 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003804{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003805 struct menu *menu = data;
3806
3807 if (widget == menu->widget)
3808 menu_set_item(data, y);
3809
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003810 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003811}
3812
3813static void
3814menu_leave_handler(struct widget *widget, struct input *input, void *data)
3815{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003816 struct menu *menu = data;
3817
3818 if (widget == menu->widget)
3819 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003820}
3821
3822static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003823menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003824 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003825 uint32_t button, enum wl_pointer_button_state state,
3826 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003827
3828{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003829 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003830
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003831 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3832 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003833 /* Either relase after press-drag-release or
3834 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003835 menu->func(menu->window->parent,
3836 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003837 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003838 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003839 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003840 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003841 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003842}
3843
3844static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003845menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003846{
3847 cairo_t *cr;
3848 const int32_t r = 3, margin = 3;
3849 struct menu *menu = data;
3850 int32_t width, height, i;
3851
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02003852 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003853 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3854 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3855 cairo_paint(cr);
3856
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02003857 width = widget->allocation.width;
3858 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003859 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003860
3861 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003862 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3863 cairo_fill(cr);
3864
3865 for (i = 0; i < menu->count; i++) {
3866 if (i == menu->current) {
3867 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3868 cairo_rectangle(cr, margin, i * 20 + margin,
3869 width - 2 * margin, 20);
3870 cairo_fill(cr);
3871 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3872 cairo_move_to(cr, 10, i * 20 + 16);
3873 cairo_show_text(cr, menu->entries[i]);
3874 } else {
3875 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3876 cairo_move_to(cr, 10, i * 20 + 16);
3877 cairo_show_text(cr, menu->entries[i]);
3878 }
3879 }
3880
3881 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003882}
3883
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003884void
3885window_show_menu(struct display *display,
3886 struct input *input, uint32_t time, struct window *parent,
3887 int32_t x, int32_t y,
3888 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003889{
3890 struct window *window;
3891 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003892 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003893
3894 menu = malloc(sizeof *menu);
3895 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003896 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003897
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003898 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003899 if (!window) {
3900 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003901 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003902 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003903
3904 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003905 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003906 menu->entries = entries;
3907 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003908 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003909 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003910 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003911 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003912 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003913 window->type = TYPE_MENU;
3914 window->x = x;
3915 window->y = y;
3916
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003917 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003918 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003919 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02003920 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003921 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003922
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003923 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003924 widget_set_enter_handler(menu->widget, menu_enter_handler);
3925 widget_set_leave_handler(menu->widget, menu_leave_handler);
3926 widget_set_motion_handler(menu->widget, menu_motion_handler);
3927 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003928
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003929 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003930 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003931}
3932
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003933void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003934window_set_buffer_type(struct window *window, enum window_buffer_type type)
3935{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003936 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003937}
3938
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003939
3940static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003941display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003942 struct wl_output *wl_output,
3943 int x, int y,
3944 int physical_width,
3945 int physical_height,
3946 int subpixel,
3947 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003948 const char *model,
3949 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003950{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003951 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003952
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003953 output->allocation.x = x;
3954 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003955 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003956}
3957
3958static void
3959display_handle_mode(void *data,
3960 struct wl_output *wl_output,
3961 uint32_t flags,
3962 int width,
3963 int height,
3964 int refresh)
3965{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003966 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003967 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003968
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003969 if (flags & WL_OUTPUT_MODE_CURRENT) {
3970 output->allocation.width = width;
3971 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003972 if (display->output_configure_handler)
3973 (*display->output_configure_handler)(
3974 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003975 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003976}
3977
3978static const struct wl_output_listener output_listener = {
3979 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003980 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003981};
3982
3983static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003984display_add_output(struct display *d, uint32_t id)
3985{
3986 struct output *output;
3987
3988 output = malloc(sizeof *output);
3989 if (output == NULL)
3990 return;
3991
3992 memset(output, 0, sizeof *output);
3993 output->display = d;
3994 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003995 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003996 wl_list_insert(d->output_list.prev, &output->link);
3997
3998 wl_output_add_listener(output->output, &output_listener, output);
3999}
4000
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004001static void
4002output_destroy(struct output *output)
4003{
4004 if (output->destroy_handler)
4005 (*output->destroy_handler)(output, output->user_data);
4006
4007 wl_output_destroy(output->output);
4008 wl_list_remove(&output->link);
4009 free(output);
4010}
4011
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004012void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004013display_set_global_handler(struct display *display,
4014 display_global_handler_t handler)
4015{
4016 struct global *global;
4017
4018 display->global_handler = handler;
4019 if (!handler)
4020 return;
4021
4022 wl_list_for_each(global, &display->global_list, link)
4023 display->global_handler(display,
4024 global->name, global->interface,
4025 global->version, display->user_data);
4026}
4027
4028void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004029display_set_output_configure_handler(struct display *display,
4030 display_output_handler_t handler)
4031{
4032 struct output *output;
4033
4034 display->output_configure_handler = handler;
4035 if (!handler)
4036 return;
4037
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004038 wl_list_for_each(output, &display->output_list, link) {
4039 if (output->allocation.width == 0 &&
4040 output->allocation.height == 0)
4041 continue;
4042
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004043 (*display->output_configure_handler)(output,
4044 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004045 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004046}
4047
4048void
4049output_set_user_data(struct output *output, void *data)
4050{
4051 output->user_data = data;
4052}
4053
4054void *
4055output_get_user_data(struct output *output)
4056{
4057 return output->user_data;
4058}
4059
4060void
4061output_set_destroy_handler(struct output *output,
4062 display_output_handler_t handler)
4063{
4064 output->destroy_handler = handler;
4065 /* FIXME: implement this, once we have way to remove outputs */
4066}
4067
4068void
Scott Moreau4e072362012-09-29 02:03:11 -06004069output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004070{
Scott Moreau4e072362012-09-29 02:03:11 -06004071 struct rectangle allocation = output->allocation;
4072
4073 switch (output->transform) {
4074 case WL_OUTPUT_TRANSFORM_90:
4075 case WL_OUTPUT_TRANSFORM_270:
4076 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4077 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4078 /* Swap width and height */
4079 allocation.width = output->allocation.height;
4080 allocation.height = output->allocation.width;
4081 break;
4082 }
4083
4084 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004085}
4086
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004087struct wl_output *
4088output_get_wl_output(struct output *output)
4089{
4090 return output->output;
4091}
4092
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004093enum wl_output_transform
4094output_get_transform(struct output *output)
4095{
4096 return output->transform;
4097}
4098
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004099static void
Daniel Stone97f68542012-05-30 16:32:01 +01004100fini_xkb(struct input *input)
4101{
4102 xkb_state_unref(input->xkb.state);
4103 xkb_map_unref(input->xkb.keymap);
4104}
4105
4106static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004107display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004108{
4109 struct input *input;
4110
4111 input = malloc(sizeof *input);
4112 if (input == NULL)
4113 return;
4114
4115 memset(input, 0, sizeof *input);
4116 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004117 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004118 input->pointer_focus = NULL;
4119 input->keyboard_focus = NULL;
4120 wl_list_insert(d->input_list.prev, &input->link);
4121
Daniel Stone37816df2012-05-16 18:45:18 +01004122 wl_seat_add_listener(input->seat, &seat_listener, input);
4123 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004124
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004125 input->data_device =
4126 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004127 input->seat);
4128 wl_data_device_add_listener(input->data_device, &data_device_listener,
4129 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004130
4131 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004132
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004133 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4134 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004135 input->repeat_task.run = keyboard_repeat_func;
4136 display_watch_fd(d, input->repeat_timer_fd,
4137 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004138}
4139
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004140static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004141input_destroy(struct input *input)
4142{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004143 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004144 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004145
4146 if (input->drag_offer)
4147 data_offer_destroy(input->drag_offer);
4148
4149 if (input->selection_offer)
4150 data_offer_destroy(input->selection_offer);
4151
4152 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004153 fini_xkb(input);
4154
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004155 wl_surface_destroy(input->pointer_surface);
4156
Pekka Paalanene1207c72011-12-16 12:02:09 +02004157 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004158 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004159 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004160 free(input);
4161}
4162
4163static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004164init_workspace_manager(struct display *d, uint32_t id)
4165{
4166 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004167 wl_registry_bind(d->registry, id,
4168 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004169 if (d->workspace_manager != NULL)
4170 workspace_manager_add_listener(d->workspace_manager,
4171 &workspace_manager_listener,
4172 d);
4173}
4174
4175static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004176registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4177 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004178{
4179 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004180 struct global *global;
4181
4182 global = malloc(sizeof *global);
4183 global->name = id;
4184 global->interface = strdup(interface);
4185 global->version = version;
4186 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004187
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004188 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004189 d->compositor = wl_registry_bind(registry, id,
4190 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004191 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004192 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004193 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004194 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004195 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004196 d->shell = wl_registry_bind(registry,
4197 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004198 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004199 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004200 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4201 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004202 wl_registry_bind(registry, id,
4203 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004204 } else if (strcmp(interface, "text_cursor_position") == 0) {
4205 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004206 wl_registry_bind(registry, id,
4207 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004208 } else if (strcmp(interface, "workspace_manager") == 0) {
4209 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004210 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004211
4212 if (d->global_handler)
4213 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004214}
4215
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004216static void
4217registry_handle_global_remove(void *data, struct wl_registry *registry,
4218 uint32_t name)
4219{
4220 struct display *d = data;
4221 struct global *global;
4222 struct global *tmp;
4223
4224 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4225 if (global->name != name)
4226 continue;
4227
4228 /* XXX: Should destroy bound globals, and call
4229 * the counterpart of display::global_handler
4230 */
4231 wl_list_remove(&global->link);
4232 free(global->interface);
4233 free(global);
4234 }
4235}
4236
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004237void *
4238display_bind(struct display *display, uint32_t name,
4239 const struct wl_interface *interface, uint32_t version)
4240{
4241 return wl_registry_bind(display->registry, name, interface, version);
4242}
4243
4244static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004245 registry_handle_global,
4246 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004247};
4248
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004249#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004250static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004251init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004252{
4253 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004254 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004255
Rob Clark6396ed32012-03-11 19:48:41 -05004256#ifdef USE_CAIRO_GLESV2
4257# define GL_BIT EGL_OPENGL_ES2_BIT
4258#else
4259# define GL_BIT EGL_OPENGL_BIT
4260#endif
4261
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004262 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004263 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004264 EGL_RED_SIZE, 1,
4265 EGL_GREEN_SIZE, 1,
4266 EGL_BLUE_SIZE, 1,
4267 EGL_ALPHA_SIZE, 1,
4268 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004269 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004270 EGL_NONE
4271 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004272
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004273#ifdef USE_CAIRO_GLESV2
4274 static const EGLint context_attribs[] = {
4275 EGL_CONTEXT_CLIENT_VERSION, 2,
4276 EGL_NONE
4277 };
4278 EGLint api = EGL_OPENGL_ES_API;
4279#else
4280 EGLint *context_attribs = NULL;
4281 EGLint api = EGL_OPENGL_API;
4282#endif
4283
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004284 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004285 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004286 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004287 return -1;
4288 }
4289
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004290 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004291 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004292 return -1;
4293 }
4294
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004295 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4296 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004297 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004298 return -1;
4299 }
4300
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004301 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004302 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004303 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004304 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004305 return -1;
4306 }
4307
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004308 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004309 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004310 return -1;
4311 }
4312
Benjamin Franzke0c991632011-09-27 21:57:31 +02004313 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4314 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004315 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004316 return -1;
4317 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004318
4319 return 0;
4320}
4321
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004322static void
4323fini_egl(struct display *display)
4324{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004325 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004326
4327 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4328 EGL_NO_CONTEXT);
4329
4330 eglTerminate(display->dpy);
4331 eglReleaseThread();
4332}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004333#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004334
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004335static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004336init_dummy_surface(struct display *display)
4337{
4338 int len;
4339 void *data;
4340
4341 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4342 data = malloc(len);
4343 display->dummy_surface =
4344 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4345 1, 1, len);
4346 display->dummy_surface_data = data;
4347}
4348
4349static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004350handle_display_data(struct task *task, uint32_t events)
4351{
4352 struct display *display =
4353 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004354 struct epoll_event ep;
4355 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004356
4357 display->display_fd_events = events;
4358
4359 if (events & EPOLLERR || events & EPOLLHUP) {
4360 display_exit(display);
4361 return;
4362 }
4363
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004364 if (events & EPOLLIN) {
4365 ret = wl_display_dispatch(display->display);
4366 if (ret == -1) {
4367 display_exit(display);
4368 return;
4369 }
4370 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004371
4372 if (events & EPOLLOUT) {
4373 ret = wl_display_flush(display->display);
4374 if (ret == 0) {
4375 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4376 ep.data.ptr = &display->display_task;
4377 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4378 display->display_fd, &ep);
4379 } else if (ret == -1 && errno != EAGAIN) {
4380 display_exit(display);
4381 return;
4382 }
4383 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004384}
4385
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004386struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004387display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004388{
4389 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004390
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004391 d = malloc(sizeof *d);
4392 if (d == NULL)
4393 return NULL;
4394
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004395 memset(d, 0, sizeof *d);
4396
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004397 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004398 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004399 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004400 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004401 return NULL;
4402 }
4403
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004404 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004405 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004406 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004407 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4408 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004409
4410 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004411 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004412 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004413 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004414
Daniel Stone97f68542012-05-30 16:32:01 +01004415 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004416 if (d->xkb_context == NULL) {
4417 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004418 return NULL;
4419 }
4420
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004421 d->workspace = 0;
4422 d->workspace_count = 1;
4423
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004424 d->registry = wl_display_get_registry(d->display);
4425 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004426
4427 if (wl_display_dispatch(d->display) < 0) {
4428 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4429 return NULL;
4430 }
4431
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004432#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004433 if (init_egl(d) < 0)
4434 fprintf(stderr, "EGL does not seem to work, "
4435 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004436#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004437
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004438 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004439
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004440 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004441
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004442 wl_list_init(&d->window_list);
4443
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004444 init_dummy_surface(d);
4445
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004446 return d;
4447}
4448
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004449static void
4450display_destroy_outputs(struct display *display)
4451{
4452 struct output *tmp;
4453 struct output *output;
4454
4455 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4456 output_destroy(output);
4457}
4458
Pekka Paalanene1207c72011-12-16 12:02:09 +02004459static void
4460display_destroy_inputs(struct display *display)
4461{
4462 struct input *tmp;
4463 struct input *input;
4464
4465 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4466 input_destroy(input);
4467}
4468
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004469void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004470display_destroy(struct display *display)
4471{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004472 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004473 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4474 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004475
4476 if (!wl_list_empty(&display->deferred_list))
4477 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4478
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004479 cairo_surface_destroy(display->dummy_surface);
4480 free(display->dummy_surface_data);
4481
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004482 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004483 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004484
Daniel Stone97f68542012-05-30 16:32:01 +01004485 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004486
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004487 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004488 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004489
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004490#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004491 if (display->argb_device)
4492 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004493#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004494
Pekka Paalanenc2052982011-12-16 11:41:32 +02004495 if (display->shell)
4496 wl_shell_destroy(display->shell);
4497
4498 if (display->shm)
4499 wl_shm_destroy(display->shm);
4500
4501 if (display->data_device_manager)
4502 wl_data_device_manager_destroy(display->data_device_manager);
4503
4504 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004505 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004506
4507 close(display->epoll_fd);
4508
U. Artie Eoff44874d92012-10-02 21:12:35 -07004509 if (!(display->display_fd_events & EPOLLERR) &&
4510 !(display->display_fd_events & EPOLLHUP))
4511 wl_display_flush(display->display);
4512
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004513 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004514 free(display);
4515}
4516
4517void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004518display_set_user_data(struct display *display, void *data)
4519{
4520 display->user_data = data;
4521}
4522
4523void *
4524display_get_user_data(struct display *display)
4525{
4526 return display->user_data;
4527}
4528
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004529struct wl_display *
4530display_get_display(struct display *display)
4531{
4532 return display->display;
4533}
4534
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004535struct output *
4536display_get_output(struct display *display)
4537{
4538 return container_of(display->output_list.next, struct output, link);
4539}
4540
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004541struct wl_compositor *
4542display_get_compositor(struct display *display)
4543{
4544 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004545}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004546
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004547uint32_t
4548display_get_serial(struct display *display)
4549{
4550 return display->serial;
4551}
4552
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004553EGLDisplay
4554display_get_egl_display(struct display *d)
4555{
4556 return d->dpy;
4557}
4558
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004559struct wl_data_source *
4560display_create_data_source(struct display *display)
4561{
4562 return wl_data_device_manager_create_data_source(display->data_device_manager);
4563}
4564
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004565EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004566display_get_argb_egl_config(struct display *d)
4567{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004568 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004569}
4570
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004571struct wl_shell *
4572display_get_shell(struct display *display)
4573{
4574 return display->shell;
4575}
4576
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004577int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004578display_acquire_window_surface(struct display *display,
4579 struct window *window,
4580 EGLContext ctx)
4581{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004582 struct surface *surface = window->main_surface;
4583
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004584 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004585 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004586
Pekka Paalanen6f41b072013-02-20 13:39:17 +02004587 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004588 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004589}
4590
4591void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004592display_release_window_surface(struct display *display,
4593 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004594{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004595 struct surface *surface = window->main_surface;
4596
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004597 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004598 return;
4599
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004600 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004601}
4602
4603void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004604display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004605{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004606 wl_list_insert(&display->deferred_list, &task->link);
4607}
4608
4609void
4610display_watch_fd(struct display *display,
4611 int fd, uint32_t events, struct task *task)
4612{
4613 struct epoll_event ep;
4614
4615 ep.events = events;
4616 ep.data.ptr = task;
4617 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4618}
4619
4620void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004621display_unwatch_fd(struct display *display, int fd)
4622{
4623 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4624}
4625
4626void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004627display_run(struct display *display)
4628{
4629 struct task *task;
4630 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004631 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004632
Pekka Paalanen826d7952011-12-15 10:14:07 +02004633 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004634 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004635 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004636 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004637 struct task, link);
4638 wl_list_remove(&task->link);
4639 task->run(task, 0);
4640 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004641
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004642 wl_display_dispatch_pending(display->display);
4643
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004644 if (!display->running)
4645 break;
4646
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004647 ret = wl_display_flush(display->display);
4648 if (ret < 0 && errno == EAGAIN) {
4649 ep[0].events =
4650 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4651 ep[0].data.ptr = &display->display_task;
4652
4653 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4654 display->display_fd, &ep[0]);
4655 } else if (ret < 0) {
4656 break;
4657 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004658
4659 count = epoll_wait(display->epoll_fd,
4660 ep, ARRAY_LENGTH(ep), -1);
4661 for (i = 0; i < count; i++) {
4662 task = ep[i].data.ptr;
4663 task->run(task, ep[i].events);
4664 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004665 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004666}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004667
4668void
4669display_exit(struct display *display)
4670{
4671 display->running = 0;
4672}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004673
4674void
4675keysym_modifiers_add(struct wl_array *modifiers_map,
4676 const char *name)
4677{
4678 size_t len = strlen(name) + 1;
4679 char *p;
4680
4681 p = wl_array_add(modifiers_map, len);
4682
4683 if (p == NULL)
4684 return;
4685
4686 strncpy(p, name, len);
4687}
4688
4689static xkb_mod_index_t
4690keysym_modifiers_get_index(struct wl_array *modifiers_map,
4691 const char *name)
4692{
4693 xkb_mod_index_t index = 0;
4694 char *p = modifiers_map->data;
4695
4696 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4697 if (strcmp(p, name) == 0)
4698 return index;
4699
4700 index++;
4701 p += strlen(p) + 1;
4702 }
4703
4704 return XKB_MOD_INVALID;
4705}
4706
4707xkb_mod_mask_t
4708keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4709 const char *name)
4710{
4711 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4712
4713 if (index == XKB_MOD_INVALID)
4714 return XKB_MOD_INVALID;
4715
4716 return 1 << index;
4717}