blob: d9c4e43591997e1d86368d09c202fdc7c6a157c5 [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;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001100 unsigned int i, j;
1101 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001102 struct config_key shell_keys[] = {
1103 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1104 };
1105 struct config_section cs[] = {
1106 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1107 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001108
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001109 config_file = config_file_path("weston.ini");
1110 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1111 free(config_file);
1112
1113 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001114 display->cursors =
1115 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1116
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001117 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001118 cursor = NULL;
1119 for (j = 0; !cursor && j < cursors[i].count; ++j)
1120 cursor = wl_cursor_theme_get_cursor(
1121 display->cursor_theme, cursors[i].names[j]);
1122
1123 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001124 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001125 cursors[i].names[0]);
1126
1127 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001128 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001129}
1130
1131static void
1132destroy_cursors(struct display *display)
1133{
1134 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001135 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001136}
1137
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001138struct wl_cursor_image *
1139display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001140{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001141 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001142
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001143 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001144}
1145
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001146static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001147surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001148{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001149 if (!surface->cairo_surface)
1150 return;
1151
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001152 if (surface->opaque_region) {
1153 wl_surface_set_opaque_region(surface->surface,
1154 surface->opaque_region);
1155 wl_region_destroy(surface->opaque_region);
1156 surface->opaque_region = NULL;
1157 }
1158
1159 if (surface->input_region) {
1160 wl_surface_set_input_region(surface->surface,
1161 surface->input_region);
1162 wl_region_destroy(surface->input_region);
1163 surface->input_region = NULL;
1164 }
1165
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001166 surface->toysurface->swap(surface->toysurface,
1167 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001168
Pekka Paalanen89dee002013-02-13 16:17:20 +02001169 cairo_surface_destroy(surface->cairo_surface);
1170 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001171}
1172
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001173int
1174window_has_focus(struct window *window)
1175{
1176 return window->focus_count > 0;
1177}
1178
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001179static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001180window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001181{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001182 if (window->type == TYPE_NONE) {
1183 window->type = TYPE_TOPLEVEL;
1184 if (window->shell_surface)
1185 wl_shell_surface_set_toplevel(window->shell_surface);
1186 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001187
Pekka Paalanen89dee002013-02-13 16:17:20 +02001188 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001189}
1190
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001191struct display *
1192window_get_display(struct window *window)
1193{
1194 return window->display;
1195}
1196
Pekka Paalanen89dee002013-02-13 16:17:20 +02001197static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001198surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001199{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001200 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001201 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001202
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001203 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001204 case WL_OUTPUT_TRANSFORM_90:
1205 case WL_OUTPUT_TRANSFORM_270:
1206 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1207 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001208 allocation.width = surface->allocation.height;
1209 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001210 break;
1211 default:
1212 break;
1213 }
1214
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001215 if (!surface->toysurface && display->dpy &&
1216 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001217 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001218 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001219 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001220 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001221 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001222 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001223
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001224 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001225 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001226 surface->surface,
1227 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001228
Pekka Paalanen89dee002013-02-13 16:17:20 +02001229 surface->cairo_surface = surface->toysurface->prepare(
1230 surface->toysurface, dx, dy,
1231 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001232}
1233
1234static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001235window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001236{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001237 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001238 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001239 int dx = 0;
1240 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001241
Pekka Paalanenec076692012-11-30 13:37:27 +02001242 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001243 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001244
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001245 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1246 dx = surface->server_allocation.width -
1247 surface->allocation.width;
1248
1249 if (window->resize_edges & WINDOW_RESIZING_TOP)
1250 dy = surface->server_allocation.height -
1251 surface->allocation.height;
1252
1253 window->resize_edges = 0;
1254
Pekka Paalanen89dee002013-02-13 16:17:20 +02001255 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001256}
1257
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001258int
1259window_get_buffer_transform(struct window *window)
1260{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001261 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001262}
1263
1264void
1265window_set_buffer_transform(struct window *window,
1266 enum wl_output_transform transform)
1267{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001268 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001269 wl_surface_set_buffer_transform(window->main_surface->surface,
1270 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001271}
1272
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001273static void frame_destroy(struct frame *frame);
1274
Pekka Paalanen4e373742013-02-13 16:17:13 +02001275static void
1276surface_destroy(struct surface *surface)
1277{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001278 if (surface->input_region)
1279 wl_region_destroy(surface->input_region);
1280
1281 if (surface->opaque_region)
1282 wl_region_destroy(surface->opaque_region);
1283
Pekka Paalanen4e373742013-02-13 16:17:13 +02001284 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001285
1286 if (surface->toysurface)
1287 surface->toysurface->destroy(surface->toysurface);
1288
Pekka Paalanen4e373742013-02-13 16:17:13 +02001289 free(surface);
1290}
1291
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001292void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001293window_destroy(struct window *window)
1294{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001295 struct display *display = window->display;
1296 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001297 struct window_output *window_output;
1298 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001299
1300 if (window->redraw_scheduled)
1301 wl_list_remove(&window->redraw_task.link);
1302
1303 wl_list_for_each(input, &display->input_list, link) {
1304 if (input->pointer_focus == window)
1305 input->pointer_focus = NULL;
1306 if (input->keyboard_focus == window)
1307 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001308 if (input->focus_widget &&
1309 input->focus_widget->window == window)
1310 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001311 }
1312
Rob Bradford7507b572012-05-15 17:55:34 +01001313 wl_list_for_each_safe(window_output, window_output_tmp,
1314 &window->window_output_list, link) {
1315 free (window_output);
1316 }
1317
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001318 if (window->frame)
1319 frame_destroy(window->frame);
1320
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001321 if (window->shell_surface)
1322 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001323
1324 surface_destroy(window->main_surface);
1325
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001326 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001327
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001328 if (window->frame_cb)
1329 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001330 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001331 free(window);
1332}
1333
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001334static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001335widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001336{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001337 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001338
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001339 wl_list_for_each(child, &widget->child_list, link) {
1340 target = widget_find_widget(child, x, y);
1341 if (target)
1342 return target;
1343 }
1344
1345 if (widget->allocation.x <= x &&
1346 x < widget->allocation.x + widget->allocation.width &&
1347 widget->allocation.y <= y &&
1348 y < widget->allocation.y + widget->allocation.height) {
1349 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001350 }
1351
1352 return NULL;
1353}
1354
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001355static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001356window_find_widget(struct window *window, int32_t x, int32_t y)
1357{
1358 return widget_find_widget(window->main_surface->widget, x, y);
1359}
1360
1361static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001362widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001363{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001364 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001365
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001366 widget = malloc(sizeof *widget);
1367 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001368 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001369 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001370 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001371 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001372 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001373 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001374 widget->tooltip = NULL;
1375 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001376 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001377
1378 return widget;
1379}
1380
1381struct widget *
1382window_add_widget(struct window *window, void *data)
1383{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001384 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001385
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001386 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001387 wl_list_init(&widget->link);
1388 window->main_surface->widget = widget;
1389
1390 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001391}
1392
1393struct widget *
1394widget_add_widget(struct widget *parent, void *data)
1395{
1396 struct widget *widget;
1397
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001398 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001399 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001400
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001401 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001402}
1403
1404void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001405widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001406{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001407 struct display *display = widget->window->display;
1408 struct input *input;
1409
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001410 if (widget->tooltip) {
1411 free(widget->tooltip);
1412 widget->tooltip = NULL;
1413 }
1414
Pekka Paalanene156fb62012-01-19 13:51:38 +02001415 wl_list_for_each(input, &display->input_list, link) {
1416 if (input->focus_widget == widget)
1417 input->focus_widget = NULL;
1418 }
1419
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001420 wl_list_remove(&widget->link);
1421 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001422}
1423
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001424void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001425widget_set_default_cursor(struct widget *widget, int cursor)
1426{
1427 widget->default_cursor = cursor;
1428}
1429
1430void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001431widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001432{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001433 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001434}
1435
1436void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001437widget_set_size(struct widget *widget, int32_t width, int32_t height)
1438{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001439 widget->allocation.width = width;
1440 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001441}
1442
1443void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001444widget_set_allocation(struct widget *widget,
1445 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001446{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001447 widget->allocation.x = x;
1448 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001449 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001450}
1451
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001452void
1453widget_set_transparent(struct widget *widget, int transparent)
1454{
1455 widget->opaque = !transparent;
1456}
1457
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001458void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001459widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001460{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001461 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001462}
1463
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001464static cairo_surface_t *
1465widget_get_cairo_surface(struct widget *widget)
1466{
1467 struct surface *surface = widget->surface;
1468 struct window *window = widget->window;
1469
1470 if (!surface->cairo_surface) {
1471 if (surface == window->main_surface)
1472 window_create_main_surface(window);
1473 else
1474 surface_create_surface(surface, 0, 0, 0);
1475 }
1476
1477 return surface->cairo_surface;
1478}
1479
1480cairo_t *
1481widget_cairo_create(struct widget *widget)
1482{
1483 cairo_surface_t *cairo_surface;
1484 cairo_t *cr;
1485
1486 cairo_surface = widget_get_cairo_surface(widget);
1487 cr = cairo_create(cairo_surface);
1488
1489 return cr;
1490}
1491
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001492void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001493widget_set_resize_handler(struct widget *widget,
1494 widget_resize_handler_t handler)
1495{
1496 widget->resize_handler = handler;
1497}
1498
1499void
1500widget_set_redraw_handler(struct widget *widget,
1501 widget_redraw_handler_t handler)
1502{
1503 widget->redraw_handler = handler;
1504}
1505
1506void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001507widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001508{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001509 widget->enter_handler = handler;
1510}
1511
1512void
1513widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1514{
1515 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001516}
1517
1518void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001519widget_set_motion_handler(struct widget *widget,
1520 widget_motion_handler_t handler)
1521{
1522 widget->motion_handler = handler;
1523}
1524
1525void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001526widget_set_button_handler(struct widget *widget,
1527 widget_button_handler_t handler)
1528{
1529 widget->button_handler = handler;
1530}
1531
1532void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001533widget_set_axis_handler(struct widget *widget,
1534 widget_axis_handler_t handler)
1535{
1536 widget->axis_handler = handler;
1537}
1538
1539void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001540widget_schedule_redraw(struct widget *widget)
1541{
1542 window_schedule_redraw(widget->window);
1543}
1544
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001545cairo_surface_t *
1546window_get_surface(struct window *window)
1547{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001548 cairo_surface_t *cairo_surface;
1549
1550 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1551
1552 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001553}
1554
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001555struct wl_surface *
1556window_get_wl_surface(struct window *window)
1557{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001558 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001559}
1560
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001561struct wl_shell_surface *
1562window_get_wl_shell_surface(struct window *window)
1563{
1564 return window->shell_surface;
1565}
1566
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001567static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001568tooltip_redraw_handler(struct widget *widget, void *data)
1569{
1570 cairo_t *cr;
1571 const int32_t r = 3;
1572 struct tooltip *tooltip = data;
1573 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001574
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001575 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001576 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1577 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1578 cairo_paint(cr);
1579
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001580 width = widget->allocation.width;
1581 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001582 rounded_rect(cr, 0, 0, width, height, r);
1583
1584 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1585 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1586 cairo_fill(cr);
1587
1588 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1589 cairo_move_to(cr, 10, 16);
1590 cairo_show_text(cr, tooltip->entry);
1591 cairo_destroy(cr);
1592}
1593
1594static cairo_text_extents_t
1595get_text_extents(struct tooltip *tooltip)
1596{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001597 cairo_t *cr;
1598 cairo_text_extents_t extents;
1599
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001600 /* Use the dummy_surface because tooltip's surface was not
1601 * created yet, and parent does not have a valid surface
1602 * outside repaint, either.
1603 */
1604 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001605 cairo_text_extents(cr, tooltip->entry, &extents);
1606 cairo_destroy(cr);
1607
1608 return extents;
1609}
1610
1611static int
1612window_create_tooltip(struct tooltip *tooltip)
1613{
1614 struct widget *parent = tooltip->parent;
1615 struct display *display = parent->window->display;
1616 struct window *window;
1617 const int offset_y = 27;
1618 const int margin = 3;
1619 cairo_text_extents_t extents;
1620
1621 if (tooltip->widget)
1622 return 0;
1623
1624 window = window_create_transient(display, parent->window, tooltip->x,
1625 tooltip->y + offset_y,
1626 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1627 if (!window)
1628 return -1;
1629
1630 tooltip->window = window;
1631 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1632
1633 extents = get_text_extents(tooltip);
1634 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1635 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1636
1637 return 0;
1638}
1639
1640void
1641widget_destroy_tooltip(struct widget *parent)
1642{
1643 struct tooltip *tooltip = parent->tooltip;
1644
1645 parent->tooltip_count = 0;
1646 if (!tooltip)
1647 return;
1648
1649 if (tooltip->widget) {
1650 widget_destroy(tooltip->widget);
1651 window_destroy(tooltip->window);
1652 tooltip->widget = NULL;
1653 tooltip->window = NULL;
1654 }
1655
1656 close(tooltip->tooltip_fd);
1657 free(tooltip->entry);
1658 free(tooltip);
1659 parent->tooltip = NULL;
1660}
1661
1662static void
1663tooltip_func(struct task *task, uint32_t events)
1664{
1665 struct tooltip *tooltip =
1666 container_of(task, struct tooltip, tooltip_task);
1667 uint64_t exp;
1668
Martin Olsson8df662a2012-07-08 03:03:47 +02001669 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1670 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001671 window_create_tooltip(tooltip);
1672}
1673
1674#define TOOLTIP_TIMEOUT 500
1675static int
1676tooltip_timer_reset(struct tooltip *tooltip)
1677{
1678 struct itimerspec its;
1679
1680 its.it_interval.tv_sec = 0;
1681 its.it_interval.tv_nsec = 0;
1682 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1683 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1684 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1685 fprintf(stderr, "could not set timerfd\n: %m");
1686 return -1;
1687 }
1688
1689 return 0;
1690}
1691
1692int
1693widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1694{
1695 struct tooltip *tooltip = parent->tooltip;
1696
1697 parent->tooltip_count++;
1698 if (tooltip) {
1699 tooltip->x = x;
1700 tooltip->y = y;
1701 tooltip_timer_reset(tooltip);
1702 return 0;
1703 }
1704
1705 /* the handler might be triggered too fast via input device motion, so
1706 * we need this check here to make sure tooltip is fully initialized */
1707 if (parent->tooltip_count > 1)
1708 return 0;
1709
1710 tooltip = malloc(sizeof *tooltip);
1711 if (!tooltip)
1712 return -1;
1713
1714 parent->tooltip = tooltip;
1715 tooltip->parent = parent;
1716 tooltip->widget = NULL;
1717 tooltip->window = NULL;
1718 tooltip->x = x;
1719 tooltip->y = y;
1720 tooltip->entry = strdup(entry);
1721 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1722 if (tooltip->tooltip_fd < 0) {
1723 fprintf(stderr, "could not create timerfd\n: %m");
1724 return -1;
1725 }
1726
1727 tooltip->tooltip_task.run = tooltip_func;
1728 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1729 EPOLLIN, &tooltip->tooltip_task);
1730 tooltip_timer_reset(tooltip);
1731
1732 return 0;
1733}
1734
1735static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001736workspace_manager_state(void *data,
1737 struct workspace_manager *workspace_manager,
1738 uint32_t current,
1739 uint32_t count)
1740{
1741 struct display *display = data;
1742
1743 display->workspace = current;
1744 display->workspace_count = count;
1745}
1746
1747static const struct workspace_manager_listener workspace_manager_listener = {
1748 workspace_manager_state
1749};
1750
1751static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001752frame_resize_handler(struct widget *widget,
1753 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001754{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001755 struct frame *frame = data;
1756 struct widget *child = frame->child;
1757 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001758 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001759 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001760 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001761 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001762 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001763 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001764 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001765
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001766 switch (widget->window->type) {
1767 case TYPE_FULLSCREEN:
1768 decoration_width = 0;
1769 decoration_height = 0;
1770
1771 allocation.x = 0;
1772 allocation.y = 0;
1773 allocation.width = width;
1774 allocation.height = height;
1775 opaque_margin = 0;
1776
1777 wl_list_for_each(button, &frame->buttons_list, link)
1778 button->widget->opaque = 1;
1779 break;
1780 case TYPE_MAXIMIZED:
1781 decoration_width = t->width * 2;
1782 decoration_height = t->width + t->titlebar_height;
1783
1784 allocation.x = t->width;
1785 allocation.y = t->titlebar_height;
1786 allocation.width = width - decoration_width;
1787 allocation.height = height - decoration_height;
1788
1789 opaque_margin = 0;
1790
1791 wl_list_for_each(button, &frame->buttons_list, link)
1792 button->widget->opaque = 0;
1793 break;
1794 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001795 decoration_width = (t->width + t->margin) * 2;
1796 decoration_height = t->width +
1797 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001798
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001799 allocation.x = t->width + t->margin;
1800 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001801 allocation.width = width - decoration_width;
1802 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001803
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001804 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001805
1806 wl_list_for_each(button, &frame->buttons_list, link)
1807 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001808 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001809 }
1810
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001811 widget_set_allocation(child, allocation.x, allocation.y,
1812 allocation.width, allocation.height);
1813
1814 if (child->resize_handler)
1815 child->resize_handler(child,
1816 allocation.width,
1817 allocation.height,
1818 child->user_data);
1819
Scott Moreauf7e498c2012-05-14 11:39:29 -06001820 width = child->allocation.width + decoration_width;
1821 height = child->allocation.height + decoration_height;
1822
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001823 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1824
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001825 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001826 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001827 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001828 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001829 shadow_margin, shadow_margin,
1830 width - 2 * shadow_margin,
1831 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001832 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001833 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001834 }
1835
Scott Moreauf7e498c2012-05-14 11:39:29 -06001836 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001837
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001838 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001839 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001840 opaque_margin, opaque_margin,
1841 widget->allocation.width - 2 * opaque_margin,
1842 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001843
1844 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001845 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1846 x_l = t->width + shadow_margin;
1847 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001848 wl_list_for_each(button, &frame->buttons_list, link) {
1849 const int button_padding = 4;
1850 w = cairo_image_surface_get_width(button->icon);
1851 h = cairo_image_surface_get_height(button->icon);
1852
1853 if (button->decoration == FRAME_BUTTON_FANCY)
1854 w += 10;
1855
1856 if (button->align == FRAME_BUTTON_LEFT) {
1857 widget_set_allocation(button->widget,
1858 x_l, y , w + 1, h + 1);
1859 x_l += w;
1860 x_l += button_padding;
1861 } else {
1862 x_r -= w;
1863 widget_set_allocation(button->widget,
1864 x_r, y , w + 1, h + 1);
1865 x_r -= button_padding;
1866 }
1867 }
1868}
1869
1870static int
1871frame_button_enter_handler(struct widget *widget,
1872 struct input *input, float x, float y, void *data)
1873{
1874 struct frame_button *frame_button = data;
1875
1876 widget_schedule_redraw(frame_button->widget);
1877 frame_button->state = FRAME_BUTTON_OVER;
1878
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001879 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001880}
1881
1882static void
1883frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1884{
1885 struct frame_button *frame_button = data;
1886
1887 widget_schedule_redraw(frame_button->widget);
1888 frame_button->state = FRAME_BUTTON_DEFAULT;
1889}
1890
1891static void
1892frame_button_button_handler(struct widget *widget,
1893 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001894 uint32_t button,
1895 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001896{
1897 struct frame_button *frame_button = data;
1898 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001899 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001900
1901 if (button != BTN_LEFT)
1902 return;
1903
1904 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001905 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001906 frame_button->state = FRAME_BUTTON_ACTIVE;
1907 widget_schedule_redraw(frame_button->widget);
1908
1909 if (frame_button->type == FRAME_BUTTON_ICON)
1910 window_show_frame_menu(window, input, time);
1911 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001912 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001913 frame_button->state = FRAME_BUTTON_DEFAULT;
1914 widget_schedule_redraw(frame_button->widget);
1915 break;
1916 }
1917
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001918 if (!was_pressed)
1919 return;
1920
Martin Minarik1998b152012-05-10 02:04:35 +02001921 switch (frame_button->type) {
1922 case FRAME_BUTTON_CLOSE:
1923 if (window->close_handler)
1924 window->close_handler(window->parent,
1925 window->user_data);
1926 else
1927 display_exit(window->display);
1928 break;
1929 case FRAME_BUTTON_MINIMIZE:
1930 fprintf(stderr,"Minimize stub\n");
1931 break;
1932 case FRAME_BUTTON_MAXIMIZE:
1933 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1934 break;
1935 default:
1936 /* Unknown operation */
1937 break;
1938 }
1939}
1940
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001941static int
1942frame_button_motion_handler(struct widget *widget,
1943 struct input *input, uint32_t time,
1944 float x, float y, void *data)
1945{
1946 struct frame_button *frame_button = data;
1947 enum frame_button_pointer previous_button_state = frame_button->state;
1948
1949 /* only track state for a pressed button */
1950 if (input->grab != widget)
1951 return CURSOR_LEFT_PTR;
1952
1953 if (x > widget->allocation.x &&
1954 x < (widget->allocation.x + widget->allocation.width) &&
1955 y > widget->allocation.y &&
1956 y < (widget->allocation.y + widget->allocation.height)) {
1957 frame_button->state = FRAME_BUTTON_ACTIVE;
1958 } else {
1959 frame_button->state = FRAME_BUTTON_DEFAULT;
1960 }
1961
1962 if (frame_button->state != previous_button_state)
1963 widget_schedule_redraw(frame_button->widget);
1964
1965 return CURSOR_LEFT_PTR;
1966}
1967
Martin Minarik1998b152012-05-10 02:04:35 +02001968static void
1969frame_button_redraw_handler(struct widget *widget, void *data)
1970{
1971 struct frame_button *frame_button = data;
1972 cairo_t *cr;
1973 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02001974
1975 x = widget->allocation.x;
1976 y = widget->allocation.y;
1977 width = widget->allocation.width;
1978 height = widget->allocation.height;
1979
1980 if (!width)
1981 return;
1982 if (!height)
1983 return;
1984 if (widget->opaque)
1985 return;
1986
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001987 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02001988
1989 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1990 cairo_set_line_width(cr, 1);
1991
1992 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1993 cairo_rectangle (cr, x, y, 25, 16);
1994
1995 cairo_stroke_preserve(cr);
1996
1997 switch (frame_button->state) {
1998 case FRAME_BUTTON_DEFAULT:
1999 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2000 break;
2001 case FRAME_BUTTON_OVER:
2002 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2003 break;
2004 case FRAME_BUTTON_ACTIVE:
2005 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2006 break;
2007 }
2008
2009 cairo_fill (cr);
2010
2011 x += 4;
2012 }
2013
2014 cairo_set_source_surface(cr, frame_button->icon, x, y);
2015 cairo_paint(cr);
2016
2017 cairo_destroy(cr);
2018}
2019
2020static struct widget *
2021frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2022 enum frame_button_align align, enum frame_button_decoration style)
2023{
2024 struct frame_button *frame_button;
2025 const char *icon = data;
2026
2027 frame_button = malloc (sizeof *frame_button);
2028 memset(frame_button, 0, sizeof *frame_button);
2029
2030 frame_button->icon = cairo_image_surface_create_from_png(icon);
2031 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2032 frame_button->frame = frame;
2033 frame_button->type = type;
2034 frame_button->align = align;
2035 frame_button->decoration = style;
2036
2037 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2038
2039 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2040 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2041 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2042 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002043 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002044 return frame_button->widget;
2045}
2046
2047static void
2048frame_button_destroy(struct frame_button *frame_button)
2049{
2050 widget_destroy(frame_button->widget);
2051 wl_list_remove(&frame_button->link);
2052 cairo_surface_destroy(frame_button->icon);
2053 free(frame_button);
2054
2055 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002056}
2057
2058static void
2059frame_redraw_handler(struct widget *widget, void *data)
2060{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002061 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002062 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002063 struct theme *t = window->display->theme;
2064 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002065
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002066 if (window->type == TYPE_FULLSCREEN)
2067 return;
2068
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002069 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002070
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002071 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002072 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002073 if (window->type == TYPE_MAXIMIZED)
2074 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002075 theme_render_frame(t, cr, widget->allocation.width,
2076 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002077
2078 cairo_destroy(cr);
2079}
2080
2081static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002082frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002083{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002084 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002085 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002086 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002087
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002088 if (window->type != TYPE_TOPLEVEL)
2089 return CURSOR_LEFT_PTR;
2090
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002091 location = theme_get_location(t, input->sx, input->sy,
2092 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002093 frame->widget->allocation.height,
2094 window->type == TYPE_MAXIMIZED ?
2095 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002096
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002097 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002098 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002099 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002100 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002101 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002102 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002103 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002104 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002105 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002106 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002107 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002108 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002109 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002110 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002111 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002112 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002113 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002114 case THEME_LOCATION_EXTERIOR:
2115 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002116 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002117 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002118 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002119}
2120
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002121static void
2122frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002123{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002124 struct display *display;
2125
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002126 switch (index) {
2127 case 0: /* close */
2128 if (window->close_handler)
2129 window->close_handler(window->parent,
2130 window->user_data);
2131 else
2132 display_exit(window->display);
2133 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002134 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002135 display = window->display;
2136 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002137 workspace_manager_move_surface(
2138 display->workspace_manager,
2139 window->main_surface->surface,
2140 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002141 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002142 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002143 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002144 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002145 workspace_manager_move_surface(
2146 display->workspace_manager,
2147 window->main_surface->surface,
2148 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002149 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002150 case 3: /* fullscreen */
2151 /* we don't have a way to get out of fullscreen for now */
2152 if (window->fullscreen_handler)
2153 window->fullscreen_handler(window, window->user_data);
2154 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002155 }
2156}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002157
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002158void
2159window_show_frame_menu(struct window *window,
2160 struct input *input, uint32_t time)
2161{
2162 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002163 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002164
2165 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002166 "Close",
2167 "Move to workspace above", "Move to workspace below",
2168 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002169 };
2170
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002171 if (window->fullscreen_handler)
2172 count = ARRAY_LENGTH(entries);
2173 else
2174 count = ARRAY_LENGTH(entries) - 1;
2175
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002176 input_get_position(input, &x, &y);
2177 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002178 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002179}
2180
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002181static int
2182frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002183 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002184{
2185 return frame_get_pointer_image_for_location(data, input);
2186}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002187
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002188static int
2189frame_motion_handler(struct widget *widget,
2190 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002191 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192{
2193 return frame_get_pointer_image_for_location(data, input);
2194}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002195
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002196static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197frame_button_handler(struct widget *widget,
2198 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002199 uint32_t button, enum wl_pointer_button_state state,
2200 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002201
2202{
2203 struct frame *frame = data;
2204 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002205 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002206 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002207
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002208 if (window->type != TYPE_TOPLEVEL)
2209 return;
2210
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002211 location = theme_get_location(display->theme, input->sx, input->sy,
2212 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002213 frame->widget->allocation.height,
2214 window->type == TYPE_MAXIMIZED ?
2215 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002216
Daniel Stone4dbadb12012-05-30 16:31:51 +01002217 if (window->display->shell && button == BTN_LEFT &&
2218 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002220 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002221 if (!window->shell_surface)
2222 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002223 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002225 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002226 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002227 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002228 case THEME_LOCATION_RESIZING_TOP:
2229 case THEME_LOCATION_RESIZING_BOTTOM:
2230 case THEME_LOCATION_RESIZING_LEFT:
2231 case THEME_LOCATION_RESIZING_RIGHT:
2232 case THEME_LOCATION_RESIZING_TOP_LEFT:
2233 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2234 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2235 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002236 if (!window->shell_surface)
2237 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002238 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002239
Pekka Paalanen99436862012-11-19 17:15:59 +02002240 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002241 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002242 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002243 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002244 break;
2245 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002246 } else if (button == BTN_RIGHT &&
2247 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002248 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002249 }
2250}
2251
2252struct widget *
2253frame_create(struct window *window, void *data)
2254{
2255 struct frame *frame;
2256
2257 frame = malloc(sizeof *frame);
2258 memset(frame, 0, sizeof *frame);
2259
2260 frame->widget = window_add_widget(window, frame);
2261 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002262
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002263 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2264 widget_set_resize_handler(frame->widget, frame_resize_handler);
2265 widget_set_enter_handler(frame->widget, frame_enter_handler);
2266 widget_set_motion_handler(frame->widget, frame_motion_handler);
2267 widget_set_button_handler(frame->widget, frame_button_handler);
2268
Martin Minarik1998b152012-05-10 02:04:35 +02002269 /* Create empty list for frame buttons */
2270 wl_list_init(&frame->buttons_list);
2271
2272 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2273 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2274
2275 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2276 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2277
2278 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2279 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2280
2281 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2282 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2283
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002284 window->frame = frame;
2285
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002286 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002287}
2288
Kristian Høgsberga1627922012-06-20 17:30:03 -04002289void
2290frame_set_child_size(struct widget *widget, int child_width, int child_height)
2291{
2292 struct display *display = widget->window->display;
2293 struct theme *t = display->theme;
2294 int decoration_width, decoration_height;
2295 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002296 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002297
2298 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002299 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002300 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002301 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002302
2303 width = child_width + decoration_width;
2304 height = child_height + decoration_height;
2305 } else {
2306 width = child_width;
2307 height = child_height;
2308 }
2309
2310 window_schedule_resize(widget->window, width, height);
2311}
2312
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002313static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002314frame_destroy(struct frame *frame)
2315{
Martin Minarik1998b152012-05-10 02:04:35 +02002316 struct frame_button *button, *tmp;
2317
2318 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2319 frame_button_destroy(button);
2320
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002321 /* frame->child must be destroyed by the application */
2322 widget_destroy(frame->widget);
2323 free(frame);
2324}
2325
2326static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002327input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002328 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002329{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002330 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002331 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002332
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002333 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002334 return;
2335
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002336 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002337 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002338 widget = old;
2339 if (input->grab)
2340 widget = input->grab;
2341 if (widget->leave_handler)
2342 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002343 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002344 }
2345
2346 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002347 widget = focus;
2348 if (input->grab)
2349 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002350 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002351 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002352 cursor = widget->enter_handler(focus, input, x, y,
2353 widget->user_data);
2354 else
2355 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002356
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002357 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002358 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002359}
2360
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002361void
2362input_grab(struct input *input, struct widget *widget, uint32_t button)
2363{
2364 input->grab = widget;
2365 input->grab_button = button;
2366}
2367
2368void
2369input_ungrab(struct input *input)
2370{
2371 struct widget *widget;
2372
2373 input->grab = NULL;
2374 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002375 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002376 input->sx, input->sy);
2377 input_set_focus_widget(input, widget, input->sx, input->sy);
2378 }
2379}
2380
2381static void
2382input_remove_pointer_focus(struct input *input)
2383{
2384 struct window *window = input->pointer_focus;
2385
2386 if (!window)
2387 return;
2388
2389 input_set_focus_widget(input, NULL, 0, 0);
2390
2391 input->pointer_focus = NULL;
2392 input->current_cursor = CURSOR_UNSET;
2393}
2394
2395static void
2396pointer_handle_enter(void *data, struct wl_pointer *pointer,
2397 uint32_t serial, struct wl_surface *surface,
2398 wl_fixed_t sx_w, wl_fixed_t sy_w)
2399{
2400 struct input *input = data;
2401 struct window *window;
2402 struct widget *widget;
2403 float sx = wl_fixed_to_double(sx_w);
2404 float sy = wl_fixed_to_double(sy_w);
2405
2406 if (!surface) {
2407 /* enter event for a window we've just destroyed */
2408 return;
2409 }
2410
2411 input->display->serial = serial;
2412 input->pointer_enter_serial = serial;
2413 input->pointer_focus = wl_surface_get_user_data(surface);
2414 window = input->pointer_focus;
2415
Pekka Paalanen99436862012-11-19 17:15:59 +02002416 if (window->resizing) {
2417 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002418 /* Schedule a redraw to free the pool */
2419 window_schedule_redraw(window);
2420 }
2421
2422 input->sx = sx;
2423 input->sy = sy;
2424
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002425 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002426 input_set_focus_widget(input, widget, sx, sy);
2427}
2428
2429static void
2430pointer_handle_leave(void *data, struct wl_pointer *pointer,
2431 uint32_t serial, struct wl_surface *surface)
2432{
2433 struct input *input = data;
2434
2435 input->display->serial = serial;
2436 input_remove_pointer_focus(input);
2437}
2438
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002439static void
Daniel Stone37816df2012-05-16 18:45:18 +01002440pointer_handle_motion(void *data, struct wl_pointer *pointer,
2441 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002442{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002443 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002444 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002445 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002446 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002447 float sx = wl_fixed_to_double(sx_w);
2448 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002449
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002450 input->sx = sx;
2451 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002452
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002453 if (!window)
2454 return;
2455
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002456 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002457 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002458 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002459 }
2460
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002461 if (input->grab)
2462 widget = input->grab;
2463 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002464 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002465 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002466 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002467 input, time, sx, sy,
2468 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002469 else
2470 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002471
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002472 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002473}
2474
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002475static void
Daniel Stone37816df2012-05-16 18:45:18 +01002476pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002477 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002478{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002479 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002480 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002481 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002482
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002483 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002484 if (input->focus_widget && input->grab == NULL &&
2485 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002486 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002487
Neil Roberts6b28aad2012-01-23 19:11:18 +00002488 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002489 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002490 (*widget->button_handler)(widget,
2491 input, time,
2492 button, state,
2493 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002494
Daniel Stone4dbadb12012-05-30 16:31:51 +01002495 if (input->grab && input->grab_button == button &&
2496 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002497 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002498}
2499
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002500static void
Daniel Stone37816df2012-05-16 18:45:18 +01002501pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002502 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002503{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002504 struct input *input = data;
2505 struct widget *widget;
2506
2507 widget = input->focus_widget;
2508 if (input->grab)
2509 widget = input->grab;
2510 if (widget && widget->axis_handler)
2511 (*widget->axis_handler)(widget,
2512 input, time,
2513 axis, value,
2514 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002515}
2516
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002517static const struct wl_pointer_listener pointer_listener = {
2518 pointer_handle_enter,
2519 pointer_handle_leave,
2520 pointer_handle_motion,
2521 pointer_handle_button,
2522 pointer_handle_axis,
2523};
2524
2525static void
2526input_remove_keyboard_focus(struct input *input)
2527{
2528 struct window *window = input->keyboard_focus;
2529 struct itimerspec its;
2530
2531 its.it_interval.tv_sec = 0;
2532 its.it_interval.tv_nsec = 0;
2533 its.it_value.tv_sec = 0;
2534 its.it_value.tv_nsec = 0;
2535 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2536
2537 if (!window)
2538 return;
2539
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002540 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002541 if (window->keyboard_focus_handler)
2542 (*window->keyboard_focus_handler)(window, NULL,
2543 window->user_data);
2544
2545 input->keyboard_focus = NULL;
2546}
2547
2548static void
2549keyboard_repeat_func(struct task *task, uint32_t events)
2550{
2551 struct input *input =
2552 container_of(task, struct input, repeat_task);
2553 struct window *window = input->keyboard_focus;
2554 uint64_t exp;
2555
2556 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2557 /* If we change the timer between the fd becoming
2558 * readable and getting here, there'll be nothing to
2559 * read and we get EAGAIN. */
2560 return;
2561
2562 if (window && window->key_handler) {
2563 (*window->key_handler)(window, input, input->repeat_time,
2564 input->repeat_key, input->repeat_sym,
2565 WL_KEYBOARD_KEY_STATE_PRESSED,
2566 window->user_data);
2567 }
2568}
2569
2570static void
2571keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2572 uint32_t format, int fd, uint32_t size)
2573{
2574 struct input *input = data;
2575 char *map_str;
2576
2577 if (!data) {
2578 close(fd);
2579 return;
2580 }
2581
2582 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2583 close(fd);
2584 return;
2585 }
2586
2587 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2588 if (map_str == MAP_FAILED) {
2589 close(fd);
2590 return;
2591 }
2592
2593 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2594 map_str,
2595 XKB_KEYMAP_FORMAT_TEXT_V1,
2596 0);
2597 munmap(map_str, size);
2598 close(fd);
2599
2600 if (!input->xkb.keymap) {
2601 fprintf(stderr, "failed to compile keymap\n");
2602 return;
2603 }
2604
2605 input->xkb.state = xkb_state_new(input->xkb.keymap);
2606 if (!input->xkb.state) {
2607 fprintf(stderr, "failed to create XKB state\n");
2608 xkb_map_unref(input->xkb.keymap);
2609 input->xkb.keymap = NULL;
2610 return;
2611 }
2612
2613 input->xkb.control_mask =
2614 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2615 input->xkb.alt_mask =
2616 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2617 input->xkb.shift_mask =
2618 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2619}
2620
2621static void
2622keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2623 uint32_t serial, struct wl_surface *surface,
2624 struct wl_array *keys)
2625{
2626 struct input *input = data;
2627 struct window *window;
2628
2629 input->display->serial = serial;
2630 input->keyboard_focus = wl_surface_get_user_data(surface);
2631
2632 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002633 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002634 if (window->keyboard_focus_handler)
2635 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002636 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002637}
2638
2639static void
2640keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2641 uint32_t serial, struct wl_surface *surface)
2642{
2643 struct input *input = data;
2644
2645 input->display->serial = serial;
2646 input_remove_keyboard_focus(input);
2647}
2648
Scott Moreau210d0792012-03-22 10:47:01 -06002649static void
Daniel Stone37816df2012-05-16 18:45:18 +01002650keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002651 uint32_t serial, uint32_t time, uint32_t key,
2652 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002653{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002654 struct input *input = data;
2655 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002656 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002657 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002658 const xkb_keysym_t *syms;
2659 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002660 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002661
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002662 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002663 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002664 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002665 return;
2666
Daniel Stone97f68542012-05-30 16:32:01 +01002667 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002668
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002669 sym = XKB_KEY_NoSymbol;
2670 if (num_syms == 1)
2671 sym = syms[0];
2672
2673 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002674 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002675 window_set_maximized(window,
2676 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002677 } else if (sym == XKB_KEY_F11 &&
2678 window->fullscreen_handler &&
2679 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2680 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002681 } else if (sym == XKB_KEY_F4 &&
2682 input->modifiers == MOD_ALT_MASK &&
2683 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2684 if (window->close_handler)
2685 window->close_handler(window->parent,
2686 window->user_data);
2687 else
2688 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002689 } else if (window->key_handler) {
2690 (*window->key_handler)(window, input, time, key,
2691 sym, state, window->user_data);
2692 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002693
2694 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2695 key == input->repeat_key) {
2696 its.it_interval.tv_sec = 0;
2697 its.it_interval.tv_nsec = 0;
2698 its.it_value.tv_sec = 0;
2699 its.it_value.tv_nsec = 0;
2700 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2701 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2702 input->repeat_sym = sym;
2703 input->repeat_key = key;
2704 input->repeat_time = time;
2705 its.it_interval.tv_sec = 0;
2706 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2707 its.it_value.tv_sec = 0;
2708 its.it_value.tv_nsec = 400 * 1000 * 1000;
2709 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2710 }
2711}
2712
2713static void
Daniel Stone351eb612012-05-31 15:27:47 -04002714keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2715 uint32_t serial, uint32_t mods_depressed,
2716 uint32_t mods_latched, uint32_t mods_locked,
2717 uint32_t group)
2718{
2719 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002720 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002721
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002722 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2723 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002724 mask = xkb_state_serialize_mods(input->xkb.state,
2725 XKB_STATE_DEPRESSED |
2726 XKB_STATE_LATCHED);
2727 input->modifiers = 0;
2728 if (mask & input->xkb.control_mask)
2729 input->modifiers |= MOD_CONTROL_MASK;
2730 if (mask & input->xkb.alt_mask)
2731 input->modifiers |= MOD_ALT_MASK;
2732 if (mask & input->xkb.shift_mask)
2733 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002734}
2735
Daniel Stone37816df2012-05-16 18:45:18 +01002736static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002737 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002738 keyboard_handle_enter,
2739 keyboard_handle_leave,
2740 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002741 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002742};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002743
2744static void
Daniel Stone37816df2012-05-16 18:45:18 +01002745seat_handle_capabilities(void *data, struct wl_seat *seat,
2746 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002747{
Daniel Stone37816df2012-05-16 18:45:18 +01002748 struct input *input = data;
2749
2750 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2751 input->pointer = wl_seat_get_pointer(seat);
2752 wl_pointer_set_user_data(input->pointer, input);
2753 wl_pointer_add_listener(input->pointer, &pointer_listener,
2754 input);
2755 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2756 wl_pointer_destroy(input->pointer);
2757 input->pointer = NULL;
2758 }
2759
2760 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2761 input->keyboard = wl_seat_get_keyboard(seat);
2762 wl_keyboard_set_user_data(input->keyboard, input);
2763 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2764 input);
2765 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2766 wl_keyboard_destroy(input->keyboard);
2767 input->keyboard = NULL;
2768 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002769}
2770
Daniel Stone37816df2012-05-16 18:45:18 +01002771static const struct wl_seat_listener seat_listener = {
2772 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002773};
2774
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002775void
2776input_get_position(struct input *input, int32_t *x, int32_t *y)
2777{
2778 *x = input->sx;
2779 *y = input->sy;
2780}
2781
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002782struct display *
2783input_get_display(struct input *input)
2784{
2785 return input->display;
2786}
2787
Daniel Stone37816df2012-05-16 18:45:18 +01002788struct wl_seat *
2789input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002790{
Daniel Stone37816df2012-05-16 18:45:18 +01002791 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002792}
2793
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002794uint32_t
2795input_get_modifiers(struct input *input)
2796{
2797 return input->modifiers;
2798}
2799
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002800struct widget *
2801input_get_focus_widget(struct input *input)
2802{
2803 return input->focus_widget;
2804}
2805
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002806struct data_offer {
2807 struct wl_data_offer *offer;
2808 struct input *input;
2809 struct wl_array types;
2810 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002811
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002812 struct task io_task;
2813 int fd;
2814 data_func_t func;
2815 int32_t x, y;
2816 void *user_data;
2817};
2818
2819static void
2820data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2821{
2822 struct data_offer *offer = data;
2823 char **p;
2824
2825 p = wl_array_add(&offer->types, sizeof *p);
2826 *p = strdup(type);
2827}
2828
2829static const struct wl_data_offer_listener data_offer_listener = {
2830 data_offer_offer,
2831};
2832
2833static void
2834data_offer_destroy(struct data_offer *offer)
2835{
2836 char **p;
2837
2838 offer->refcount--;
2839 if (offer->refcount == 0) {
2840 wl_data_offer_destroy(offer->offer);
2841 for (p = offer->types.data; *p; p++)
2842 free(*p);
2843 wl_array_release(&offer->types);
2844 free(offer);
2845 }
2846}
2847
2848static void
2849data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002850 struct wl_data_device *data_device,
2851 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002852{
2853 struct data_offer *offer;
2854
2855 offer = malloc(sizeof *offer);
2856
2857 wl_array_init(&offer->types);
2858 offer->refcount = 1;
2859 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002860 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002861 wl_data_offer_add_listener(offer->offer,
2862 &data_offer_listener, offer);
2863}
2864
2865static void
2866data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002867 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002868 wl_fixed_t x_w, wl_fixed_t y_w,
2869 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002870{
2871 struct input *input = data;
2872 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002873 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002874 float x = wl_fixed_to_double(x_w);
2875 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002876 char **p;
2877
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002878 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002879 window = wl_surface_get_user_data(surface);
2880 input->pointer_focus = window;
2881
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002882 if (offer) {
2883 input->drag_offer = wl_data_offer_get_user_data(offer);
2884
2885 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2886 *p = NULL;
2887
2888 types_data = input->drag_offer->types.data;
2889 } else {
2890 input->drag_offer = NULL;
2891 types_data = NULL;
2892 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002893
2894 window = input->pointer_focus;
2895 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002896 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002897 window->user_data);
2898}
2899
2900static void
2901data_device_leave(void *data, struct wl_data_device *data_device)
2902{
2903 struct input *input = data;
2904
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002905 if (input->drag_offer) {
2906 data_offer_destroy(input->drag_offer);
2907 input->drag_offer = NULL;
2908 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002909}
2910
2911static void
2912data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002913 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002914{
2915 struct input *input = data;
2916 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002917 float x = wl_fixed_to_double(x_w);
2918 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002919 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002920
2921 input->sx = x;
2922 input->sy = y;
2923
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002924 if (input->drag_offer)
2925 types_data = input->drag_offer->types.data;
2926 else
2927 types_data = NULL;
2928
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002929 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002930 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002931 window->user_data);
2932}
2933
2934static void
2935data_device_drop(void *data, struct wl_data_device *data_device)
2936{
2937 struct input *input = data;
2938 struct window *window = input->pointer_focus;
2939
2940 if (window->drop_handler)
2941 window->drop_handler(window, input,
2942 input->sx, input->sy, window->user_data);
2943}
2944
2945static void
2946data_device_selection(void *data,
2947 struct wl_data_device *wl_data_device,
2948 struct wl_data_offer *offer)
2949{
2950 struct input *input = data;
2951 char **p;
2952
2953 if (input->selection_offer)
2954 data_offer_destroy(input->selection_offer);
2955
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002956 if (offer) {
2957 input->selection_offer = wl_data_offer_get_user_data(offer);
2958 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2959 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002960 } else {
2961 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002962 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002963}
2964
2965static const struct wl_data_device_listener data_device_listener = {
2966 data_device_data_offer,
2967 data_device_enter,
2968 data_device_leave,
2969 data_device_motion,
2970 data_device_drop,
2971 data_device_selection
2972};
2973
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002974static void
2975input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002976{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002977 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002978 struct wl_cursor *cursor;
2979 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002980
Daniel Stone80972742012-11-07 17:51:39 +11002981 if (!input->pointer)
2982 return;
2983
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002984 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002985 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002986 return;
2987
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002988 if (index >= (int) cursor->image_count) {
2989 fprintf(stderr, "cursor index out of range\n");
2990 return;
2991 }
2992
2993 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002994 buffer = wl_cursor_image_get_buffer(image);
2995 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002996 return;
2997
Kristian Høgsbergae277372012-08-01 09:41:08 -04002998 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002999 input->pointer_surface,
3000 image->hotspot_x, image->hotspot_y);
3001 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3002 wl_surface_damage(input->pointer_surface, 0, 0,
3003 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003004 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003005}
3006
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003007static const struct wl_callback_listener pointer_surface_listener;
3008
3009static void
3010pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3011 uint32_t time)
3012{
3013 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003014 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003015 int i;
3016
3017 if (callback) {
3018 assert(callback == input->cursor_frame_cb);
3019 wl_callback_destroy(callback);
3020 input->cursor_frame_cb = NULL;
3021 }
3022
Daniel Stone80972742012-11-07 17:51:39 +11003023 if (!input->pointer)
3024 return;
3025
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003026 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003027 wl_pointer_set_cursor(input->pointer,
3028 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003029 NULL, 0, 0);
3030 return;
3031 }
3032
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003033 if (input->current_cursor == CURSOR_UNSET)
3034 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003035 cursor = input->display->cursors[input->current_cursor];
3036 if (!cursor)
3037 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003038
3039 /* FIXME We don't have the current time on the first call so we set
3040 * the animation start to the time of the first frame callback. */
3041 if (time == 0)
3042 input->cursor_anim_start = 0;
3043 else if (input->cursor_anim_start == 0)
3044 input->cursor_anim_start = time;
3045
3046 if (time == 0 || input->cursor_anim_start == 0)
3047 i = 0;
3048 else
3049 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3050
Pekka Paalanenbc106382012-10-10 12:49:31 +03003051 if (cursor->image_count > 1) {
3052 input->cursor_frame_cb =
3053 wl_surface_frame(input->pointer_surface);
3054 wl_callback_add_listener(input->cursor_frame_cb,
3055 &pointer_surface_listener, input);
3056 }
3057
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003058 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003059}
3060
3061static const struct wl_callback_listener pointer_surface_listener = {
3062 pointer_surface_frame_callback
3063};
3064
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003065void
3066input_set_pointer_image(struct input *input, int pointer)
3067{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003068 int force = 0;
3069
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003070 if (!input->pointer)
3071 return;
3072
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003073 if (input->pointer_enter_serial > input->cursor_serial)
3074 force = 1;
3075
3076 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003077 return;
3078
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003079 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003080 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003081 if (!input->cursor_frame_cb)
3082 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003083 else if (force) {
3084 /* The current frame callback may be stuck if, for instance,
3085 * the set cursor request was processed by the server after
3086 * this client lost the focus. In this case the cursor surface
3087 * might not be mapped and the frame callback wouldn't ever
3088 * complete. Send a set_cursor and attach to try to map the
3089 * cursor surface again so that the callback will finish */
3090 input_set_pointer_image_index(input, 0);
3091 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003092}
3093
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003094struct wl_data_device *
3095input_get_data_device(struct input *input)
3096{
3097 return input->data_device;
3098}
3099
3100void
3101input_set_selection(struct input *input,
3102 struct wl_data_source *source, uint32_t time)
3103{
3104 wl_data_device_set_selection(input->data_device, source, time);
3105}
3106
3107void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003108input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003109{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003110 wl_data_offer_accept(input->drag_offer->offer,
3111 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003112}
3113
3114static void
3115offer_io_func(struct task *task, uint32_t events)
3116{
3117 struct data_offer *offer =
3118 container_of(task, struct data_offer, io_task);
3119 unsigned int len;
3120 char buffer[4096];
3121
3122 len = read(offer->fd, buffer, sizeof buffer);
3123 offer->func(buffer, len,
3124 offer->x, offer->y, offer->user_data);
3125
3126 if (len == 0) {
3127 close(offer->fd);
3128 data_offer_destroy(offer);
3129 }
3130}
3131
3132static void
3133data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3134 data_func_t func, void *user_data)
3135{
3136 int p[2];
3137
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003138 if (pipe2(p, O_CLOEXEC) == -1)
3139 return;
3140
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003141 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3142 close(p[1]);
3143
3144 offer->io_task.run = offer_io_func;
3145 offer->fd = p[0];
3146 offer->func = func;
3147 offer->refcount++;
3148 offer->user_data = user_data;
3149
3150 display_watch_fd(offer->input->display,
3151 offer->fd, EPOLLIN, &offer->io_task);
3152}
3153
3154void
3155input_receive_drag_data(struct input *input, const char *mime_type,
3156 data_func_t func, void *data)
3157{
3158 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3159 input->drag_offer->x = input->sx;
3160 input->drag_offer->y = input->sy;
3161}
3162
3163int
3164input_receive_selection_data(struct input *input, const char *mime_type,
3165 data_func_t func, void *data)
3166{
3167 char **p;
3168
3169 if (input->selection_offer == NULL)
3170 return -1;
3171
3172 for (p = input->selection_offer->types.data; *p; p++)
3173 if (strcmp(mime_type, *p) == 0)
3174 break;
3175
3176 if (*p == NULL)
3177 return -1;
3178
3179 data_offer_receive_data(input->selection_offer,
3180 mime_type, func, data);
3181 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003182}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003183
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003184int
3185input_receive_selection_data_to_fd(struct input *input,
3186 const char *mime_type, int fd)
3187{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003188 if (input->selection_offer)
3189 wl_data_offer_receive(input->selection_offer->offer,
3190 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003191
3192 return 0;
3193}
3194
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003195void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003196window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003197{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003198 if (!window->shell_surface)
3199 return;
3200
Daniel Stone37816df2012-05-16 18:45:18 +01003201 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003202}
3203
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003204static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003205surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003206{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003207 struct widget *widget = surface->widget;
3208 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003209
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003210 if (surface->input_region) {
3211 wl_region_destroy(surface->input_region);
3212 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003213 }
3214
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003215 if (surface->opaque_region)
3216 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003217
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003218 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003219
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003220 if (widget->resize_handler)
3221 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003222 widget->allocation.width,
3223 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003224 widget->user_data);
3225
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003226 if (surface->allocation.width != widget->allocation.width ||
3227 surface->allocation.height != widget->allocation.height) {
3228 surface->allocation = widget->allocation;
3229 window_schedule_redraw(widget->window);
3230 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003231
3232 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003233 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003234 widget->allocation.width,
3235 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003236}
3237
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003238static void
3239idle_resize(struct window *window)
3240{
3241 window->resize_needed = 0;
3242
3243 widget_set_allocation(window->main_surface->widget,
3244 window->pending_allocation.x,
3245 window->pending_allocation.y,
3246 window->pending_allocation.width,
3247 window->pending_allocation.height);
3248
3249 surface_resize(window->main_surface);
3250}
3251
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003252void
3253window_schedule_resize(struct window *window, int width, int height)
3254{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003255 window->pending_allocation.x = 0;
3256 window->pending_allocation.y = 0;
3257 window->pending_allocation.width = width;
3258 window->pending_allocation.height = height;
3259
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003260 if (window->min_allocation.width == 0)
3261 window->min_allocation = window->pending_allocation;
3262 if (window->pending_allocation.width < window->min_allocation.width)
3263 window->pending_allocation.width = window->min_allocation.width;
3264 if (window->pending_allocation.height < window->min_allocation.height)
3265 window->pending_allocation.height = window->min_allocation.height;
3266
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003267 window->resize_needed = 1;
3268 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003269}
3270
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003271void
3272widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3273{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003274 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003275}
3276
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003277static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003278handle_ping(void *data, struct wl_shell_surface *shell_surface,
3279 uint32_t serial)
3280{
3281 wl_shell_surface_pong(shell_surface, serial);
3282}
3283
3284static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003285handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003286 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003287{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003288 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003289
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003290 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003291 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003292}
3293
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003294static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003295menu_destroy(struct menu *menu)
3296{
3297 widget_destroy(menu->widget);
3298 window_destroy(menu->window);
3299 free(menu);
3300}
3301
3302static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003303handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3304{
3305 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003306 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003307
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003308 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003309 * device. Or just use wl_callback. And this really needs to
3310 * be a window vfunc that the menu can set. And we need the
3311 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003312
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003313 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003314 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003315 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003316}
3317
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003318static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003319 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003320 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003321 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003322};
3323
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003324void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003325window_get_allocation(struct window *window,
3326 struct rectangle *allocation)
3327{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003328 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003329}
3330
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003331static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003332widget_redraw(struct widget *widget)
3333{
3334 struct widget *child;
3335
3336 if (widget->redraw_handler)
3337 widget->redraw_handler(widget, widget->user_data);
3338 wl_list_for_each(child, &widget->child_list, link)
3339 widget_redraw(child);
3340}
3341
3342static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003343frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3344{
3345 struct window *window = data;
3346
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003347 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003348 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003349 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003350 window->redraw_scheduled = 0;
3351 if (window->redraw_needed)
3352 window_schedule_redraw(window);
3353}
3354
3355static const struct wl_callback_listener listener = {
3356 frame_callback
3357};
3358
3359static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003360idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003361{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003362 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003363
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003364 if (window->resize_needed)
3365 idle_resize(window);
3366
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003367 widget_redraw(window->main_surface->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003368 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003369 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003370
Pekka Paalanen4e373742013-02-13 16:17:13 +02003371 window->frame_cb = wl_surface_frame(window->main_surface->surface);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003372 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003373 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003374}
3375
3376void
3377window_schedule_redraw(struct window *window)
3378{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003379 window->redraw_needed = 1;
MoD063a8822013-03-02 23:43:57 +00003380 if (window->configure_requests)
3381 return;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003382 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003383 window->redraw_task.run = idle_redraw;
3384 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003385 window->redraw_scheduled = 1;
3386 }
3387}
3388
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003389int
3390window_is_fullscreen(struct window *window)
3391{
3392 return window->type == TYPE_FULLSCREEN;
3393}
3394
MoD063a8822013-03-02 23:43:57 +00003395static void
3396configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3397{
3398 struct window *window = data;
3399
3400 wl_callback_destroy(callback);
3401 window->configure_requests--;
3402
3403 if (!window->configure_requests)
3404 window_schedule_redraw(window);
3405}
3406
3407static struct wl_callback_listener configure_request_listener = {
3408 configure_request_completed,
3409};
3410
3411static void
3412window_defer_redraw_until_configure(struct window* window)
3413{
3414 struct wl_callback *callback;
3415
3416 if (window->redraw_scheduled) {
3417 wl_list_remove(&window->redraw_task.link);
3418 window->redraw_scheduled = 0;
3419 }
3420 if (window->frame_cb) {
3421 wl_callback_destroy(window->frame_cb);
3422 window->frame_cb = 0;
3423 }
3424
3425 callback = wl_display_sync(window->display->display);
3426 wl_callback_add_listener(callback, &configure_request_listener, window);
3427 window->configure_requests++;
3428}
3429
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003430void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003431window_set_fullscreen(struct window *window, int fullscreen)
3432{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003433 if (!window->display->shell)
3434 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003435
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003436 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003437 return;
3438
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003439 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003440 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003441 if (window->type == TYPE_TOPLEVEL) {
3442 window->saved_allocation = window->main_surface->allocation;
3443 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003444 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003445 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003446 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003447 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003448 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003449 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003450 if (window->saved_type == TYPE_MAXIMIZED) {
3451 window_set_maximized(window, 1);
3452 } else {
3453 window->type = TYPE_TOPLEVEL;
3454 wl_shell_surface_set_toplevel(window->shell_surface);
3455 window_schedule_resize(window,
3456 window->saved_allocation.width,
3457 window->saved_allocation.height);
3458 }
3459
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003460 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003461}
3462
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003463void
3464window_set_fullscreen_method(struct window *window,
3465 enum wl_shell_surface_fullscreen_method method)
3466{
3467 window->fullscreen_method = method;
3468}
3469
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003470int
3471window_is_maximized(struct window *window)
3472{
3473 return window->type == TYPE_MAXIMIZED;
3474}
3475
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003476void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003477window_set_maximized(struct window *window, int maximized)
3478{
3479 if (!window->display->shell)
3480 return;
3481
3482 if ((window->type == TYPE_MAXIMIZED) == maximized)
3483 return;
3484
3485 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003486 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003487 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3488 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003489 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003490 } else if (window->type == TYPE_FULLSCREEN) {
3491 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3492 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003493 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003494 } else {
3495 wl_shell_surface_set_toplevel(window->shell_surface);
3496 window->type = TYPE_TOPLEVEL;
3497 window_schedule_resize(window,
3498 window->saved_allocation.width,
3499 window->saved_allocation.height);
3500 }
3501}
3502
3503void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003504window_set_user_data(struct window *window, void *data)
3505{
3506 window->user_data = data;
3507}
3508
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003509void *
3510window_get_user_data(struct window *window)
3511{
3512 return window->user_data;
3513}
3514
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003515void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003516window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003517 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003518{
3519 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003520}
3521
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003522void
3523window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003524 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003525{
3526 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003527}
3528
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003529void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003530window_set_data_handler(struct window *window, window_data_handler_t handler)
3531{
3532 window->data_handler = handler;
3533}
3534
3535void
3536window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3537{
3538 window->drop_handler = handler;
3539}
3540
3541void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003542window_set_close_handler(struct window *window,
3543 window_close_handler_t handler)
3544{
3545 window->close_handler = handler;
3546}
3547
3548void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003549window_set_fullscreen_handler(struct window *window,
3550 window_fullscreen_handler_t handler)
3551{
3552 window->fullscreen_handler = handler;
3553}
3554
3555void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003556window_set_output_handler(struct window *window,
3557 window_output_handler_t handler)
3558{
3559 window->output_handler = handler;
3560}
3561
3562void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003563window_set_title(struct window *window, const char *title)
3564{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003565 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003566 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003567 if (window->shell_surface)
3568 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003569}
3570
3571const char *
3572window_get_title(struct window *window)
3573{
3574 return window->title;
3575}
3576
3577void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003578window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3579{
3580 struct text_cursor_position *text_cursor_position =
3581 window->display->text_cursor_position;
3582
Scott Moreau9295ce02012-06-01 12:46:10 -06003583 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003584 return;
3585
3586 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003587 window->main_surface->surface,
3588 wl_fixed_from_int(x),
3589 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003590}
3591
3592void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003593window_damage(struct window *window, int32_t x, int32_t y,
3594 int32_t width, int32_t height)
3595{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003596 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003597}
3598
Casey Dahlin9074db52012-04-19 22:50:09 -04003599static void
3600surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003601 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003602{
Rob Bradford7507b572012-05-15 17:55:34 +01003603 struct window *window = data;
3604 struct output *output;
3605 struct output *output_found = NULL;
3606 struct window_output *window_output;
3607
3608 wl_list_for_each(output, &window->display->output_list, link) {
3609 if (output->output == wl_output) {
3610 output_found = output;
3611 break;
3612 }
3613 }
3614
3615 if (!output_found)
3616 return;
3617
3618 window_output = malloc (sizeof *window_output);
3619 window_output->output = output_found;
3620
3621 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003622
3623 if (window->output_handler)
3624 window->output_handler(window, output_found, 1,
3625 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003626}
3627
3628static void
3629surface_leave(void *data,
3630 struct wl_surface *wl_surface, struct wl_output *output)
3631{
Rob Bradford7507b572012-05-15 17:55:34 +01003632 struct window *window = data;
3633 struct window_output *window_output;
3634 struct window_output *window_output_found = NULL;
3635
3636 wl_list_for_each(window_output, &window->window_output_list, link) {
3637 if (window_output->output->output == output) {
3638 window_output_found = window_output;
3639 break;
3640 }
3641 }
3642
3643 if (window_output_found) {
3644 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003645
3646 if (window->output_handler)
3647 window->output_handler(window, window_output->output,
3648 0, window->user_data);
3649
Rob Bradford7507b572012-05-15 17:55:34 +01003650 free(window_output_found);
3651 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003652}
3653
3654static const struct wl_surface_listener surface_listener = {
3655 surface_enter,
3656 surface_leave
3657};
3658
Pekka Paalanen4e373742013-02-13 16:17:13 +02003659static struct surface *
3660surface_create(struct window *window)
3661{
3662 struct display *display = window->display;
3663 struct surface *surface;
3664
3665 surface = calloc(1, sizeof *surface);
3666 if (!surface)
3667 return NULL;
3668
3669 surface->window = window;
3670 surface->surface = wl_compositor_create_surface(display->compositor);
3671 wl_surface_add_listener(surface->surface, &surface_listener, window);
3672
3673 return surface;
3674}
3675
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003676static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003677window_create_internal(struct display *display,
3678 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003679{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003680 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003681 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003682
3683 window = malloc(sizeof *window);
3684 if (window == NULL)
3685 return NULL;
3686
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003687 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003688 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003689 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003690
3691 surface = surface_create(window);
3692 window->main_surface = surface;
3693
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003694 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003695 window->shell_surface =
3696 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003697 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003698 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003699
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003700 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003701 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00003702 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003703
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003704 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003705#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003706 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003707#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003708 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003709#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003710 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003711 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003712
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003713 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003714 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003715 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003716
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003717 if (window->shell_surface) {
3718 wl_shell_surface_set_user_data(window->shell_surface, window);
3719 wl_shell_surface_add_listener(window->shell_surface,
3720 &shell_surface_listener, window);
3721 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003722
Rob Bradford7507b572012-05-15 17:55:34 +01003723 wl_list_init (&window->window_output_list);
3724
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003725 return window;
3726}
3727
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003728struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003729window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003730{
3731 struct window *window;
3732
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003733 window = window_create_internal(display, NULL, TYPE_NONE);
3734 if (!window)
3735 return NULL;
3736
3737 return window;
3738}
3739
3740struct window *
3741window_create_custom(struct display *display)
3742{
3743 struct window *window;
3744
3745 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003746 if (!window)
3747 return NULL;
3748
3749 return window;
3750}
3751
3752struct window *
3753window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003754 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003755{
3756 struct window *window;
3757
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003758 window = window_create_internal(parent->display,
3759 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003760 if (!window)
3761 return NULL;
3762
3763 window->x = x;
3764 window->y = y;
3765
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003766 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003767 wl_shell_surface_set_transient(
3768 window->shell_surface,
3769 window->parent->main_surface->surface,
3770 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003771
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003772 return window;
3773}
3774
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003775static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003776menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003777{
3778 int next;
3779
3780 next = (sy - 8) / 20;
3781 if (menu->current != next) {
3782 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003783 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003784 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003785}
3786
3787static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003788menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003789 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003790 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003791{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003792 struct menu *menu = data;
3793
3794 if (widget == menu->widget)
3795 menu_set_item(data, y);
3796
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003797 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003798}
3799
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003800static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003801menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003802 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003803{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003804 struct menu *menu = data;
3805
3806 if (widget == menu->widget)
3807 menu_set_item(data, y);
3808
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003809 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003810}
3811
3812static void
3813menu_leave_handler(struct widget *widget, struct input *input, void *data)
3814{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003815 struct menu *menu = data;
3816
3817 if (widget == menu->widget)
3818 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003819}
3820
3821static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003822menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003823 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003824 uint32_t button, enum wl_pointer_button_state state,
3825 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003826
3827{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003828 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003829
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003830 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3831 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003832 /* Either relase after press-drag-release or
3833 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003834 menu->func(menu->window->parent,
3835 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003836 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003837 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003838 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003839 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003840 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003841}
3842
3843static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003844menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003845{
3846 cairo_t *cr;
3847 const int32_t r = 3, margin = 3;
3848 struct menu *menu = data;
3849 int32_t width, height, i;
3850
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02003851 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003852 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3853 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3854 cairo_paint(cr);
3855
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02003856 width = widget->allocation.width;
3857 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003858 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003859
3860 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003861 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3862 cairo_fill(cr);
3863
3864 for (i = 0; i < menu->count; i++) {
3865 if (i == menu->current) {
3866 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3867 cairo_rectangle(cr, margin, i * 20 + margin,
3868 width - 2 * margin, 20);
3869 cairo_fill(cr);
3870 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3871 cairo_move_to(cr, 10, i * 20 + 16);
3872 cairo_show_text(cr, menu->entries[i]);
3873 } else {
3874 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3875 cairo_move_to(cr, 10, i * 20 + 16);
3876 cairo_show_text(cr, menu->entries[i]);
3877 }
3878 }
3879
3880 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003881}
3882
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003883void
3884window_show_menu(struct display *display,
3885 struct input *input, uint32_t time, struct window *parent,
3886 int32_t x, int32_t y,
3887 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003888{
3889 struct window *window;
3890 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003891 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003892
3893 menu = malloc(sizeof *menu);
3894 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003895 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003896
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003897 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003898 if (!window) {
3899 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003900 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003901 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003902
3903 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003904 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003905 menu->entries = entries;
3906 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003907 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003908 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003909 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003910 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003911 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003912 window->type = TYPE_MENU;
3913 window->x = x;
3914 window->y = y;
3915
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003916 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003917 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003918 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02003919 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003920 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003921
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003922 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003923 widget_set_enter_handler(menu->widget, menu_enter_handler);
3924 widget_set_leave_handler(menu->widget, menu_leave_handler);
3925 widget_set_motion_handler(menu->widget, menu_motion_handler);
3926 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003927
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003928 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003929 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003930}
3931
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003932void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003933window_set_buffer_type(struct window *window, enum window_buffer_type type)
3934{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003935 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003936}
3937
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003938
3939static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003940display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003941 struct wl_output *wl_output,
3942 int x, int y,
3943 int physical_width,
3944 int physical_height,
3945 int subpixel,
3946 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003947 const char *model,
3948 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003949{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003950 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003951
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003952 output->allocation.x = x;
3953 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003954 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003955}
3956
3957static void
3958display_handle_mode(void *data,
3959 struct wl_output *wl_output,
3960 uint32_t flags,
3961 int width,
3962 int height,
3963 int refresh)
3964{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003965 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003966 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003967
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003968 if (flags & WL_OUTPUT_MODE_CURRENT) {
3969 output->allocation.width = width;
3970 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003971 if (display->output_configure_handler)
3972 (*display->output_configure_handler)(
3973 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003974 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003975}
3976
3977static const struct wl_output_listener output_listener = {
3978 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003979 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003980};
3981
3982static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003983display_add_output(struct display *d, uint32_t id)
3984{
3985 struct output *output;
3986
3987 output = malloc(sizeof *output);
3988 if (output == NULL)
3989 return;
3990
3991 memset(output, 0, sizeof *output);
3992 output->display = d;
3993 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003994 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003995 wl_list_insert(d->output_list.prev, &output->link);
3996
3997 wl_output_add_listener(output->output, &output_listener, output);
3998}
3999
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004000static void
4001output_destroy(struct output *output)
4002{
4003 if (output->destroy_handler)
4004 (*output->destroy_handler)(output, output->user_data);
4005
4006 wl_output_destroy(output->output);
4007 wl_list_remove(&output->link);
4008 free(output);
4009}
4010
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004011void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004012display_set_global_handler(struct display *display,
4013 display_global_handler_t handler)
4014{
4015 struct global *global;
4016
4017 display->global_handler = handler;
4018 if (!handler)
4019 return;
4020
4021 wl_list_for_each(global, &display->global_list, link)
4022 display->global_handler(display,
4023 global->name, global->interface,
4024 global->version, display->user_data);
4025}
4026
4027void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004028display_set_output_configure_handler(struct display *display,
4029 display_output_handler_t handler)
4030{
4031 struct output *output;
4032
4033 display->output_configure_handler = handler;
4034 if (!handler)
4035 return;
4036
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004037 wl_list_for_each(output, &display->output_list, link) {
4038 if (output->allocation.width == 0 &&
4039 output->allocation.height == 0)
4040 continue;
4041
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004042 (*display->output_configure_handler)(output,
4043 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004044 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004045}
4046
4047void
4048output_set_user_data(struct output *output, void *data)
4049{
4050 output->user_data = data;
4051}
4052
4053void *
4054output_get_user_data(struct output *output)
4055{
4056 return output->user_data;
4057}
4058
4059void
4060output_set_destroy_handler(struct output *output,
4061 display_output_handler_t handler)
4062{
4063 output->destroy_handler = handler;
4064 /* FIXME: implement this, once we have way to remove outputs */
4065}
4066
4067void
Scott Moreau4e072362012-09-29 02:03:11 -06004068output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004069{
Scott Moreau4e072362012-09-29 02:03:11 -06004070 struct rectangle allocation = output->allocation;
4071
4072 switch (output->transform) {
4073 case WL_OUTPUT_TRANSFORM_90:
4074 case WL_OUTPUT_TRANSFORM_270:
4075 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4076 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4077 /* Swap width and height */
4078 allocation.width = output->allocation.height;
4079 allocation.height = output->allocation.width;
4080 break;
4081 }
4082
4083 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004084}
4085
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004086struct wl_output *
4087output_get_wl_output(struct output *output)
4088{
4089 return output->output;
4090}
4091
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004092enum wl_output_transform
4093output_get_transform(struct output *output)
4094{
4095 return output->transform;
4096}
4097
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004098static void
Daniel Stone97f68542012-05-30 16:32:01 +01004099fini_xkb(struct input *input)
4100{
4101 xkb_state_unref(input->xkb.state);
4102 xkb_map_unref(input->xkb.keymap);
4103}
4104
4105static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004106display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004107{
4108 struct input *input;
4109
4110 input = malloc(sizeof *input);
4111 if (input == NULL)
4112 return;
4113
4114 memset(input, 0, sizeof *input);
4115 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004116 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004117 input->pointer_focus = NULL;
4118 input->keyboard_focus = NULL;
4119 wl_list_insert(d->input_list.prev, &input->link);
4120
Daniel Stone37816df2012-05-16 18:45:18 +01004121 wl_seat_add_listener(input->seat, &seat_listener, input);
4122 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004123
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004124 input->data_device =
4125 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004126 input->seat);
4127 wl_data_device_add_listener(input->data_device, &data_device_listener,
4128 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004129
4130 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004131
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004132 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4133 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004134 input->repeat_task.run = keyboard_repeat_func;
4135 display_watch_fd(d, input->repeat_timer_fd,
4136 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004137}
4138
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004139static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004140input_destroy(struct input *input)
4141{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004142 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004143 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004144
4145 if (input->drag_offer)
4146 data_offer_destroy(input->drag_offer);
4147
4148 if (input->selection_offer)
4149 data_offer_destroy(input->selection_offer);
4150
4151 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004152 fini_xkb(input);
4153
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004154 wl_surface_destroy(input->pointer_surface);
4155
Pekka Paalanene1207c72011-12-16 12:02:09 +02004156 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004157 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004158 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004159 free(input);
4160}
4161
4162static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004163init_workspace_manager(struct display *d, uint32_t id)
4164{
4165 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004166 wl_registry_bind(d->registry, id,
4167 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004168 if (d->workspace_manager != NULL)
4169 workspace_manager_add_listener(d->workspace_manager,
4170 &workspace_manager_listener,
4171 d);
4172}
4173
4174static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004175registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4176 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004177{
4178 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004179 struct global *global;
4180
4181 global = malloc(sizeof *global);
4182 global->name = id;
4183 global->interface = strdup(interface);
4184 global->version = version;
4185 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004186
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004187 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004188 d->compositor = wl_registry_bind(registry, id,
4189 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004190 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004191 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004192 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004193 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004194 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004195 d->shell = wl_registry_bind(registry,
4196 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004197 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004198 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004199 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4200 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004201 wl_registry_bind(registry, id,
4202 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004203 } else if (strcmp(interface, "text_cursor_position") == 0) {
4204 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004205 wl_registry_bind(registry, id,
4206 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004207 } else if (strcmp(interface, "workspace_manager") == 0) {
4208 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004209 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004210
4211 if (d->global_handler)
4212 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004213}
4214
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004215static void
4216registry_handle_global_remove(void *data, struct wl_registry *registry,
4217 uint32_t name)
4218{
4219 struct display *d = data;
4220 struct global *global;
4221 struct global *tmp;
4222
4223 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4224 if (global->name != name)
4225 continue;
4226
4227 /* XXX: Should destroy bound globals, and call
4228 * the counterpart of display::global_handler
4229 */
4230 wl_list_remove(&global->link);
4231 free(global->interface);
4232 free(global);
4233 }
4234}
4235
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004236void *
4237display_bind(struct display *display, uint32_t name,
4238 const struct wl_interface *interface, uint32_t version)
4239{
4240 return wl_registry_bind(display->registry, name, interface, version);
4241}
4242
4243static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004244 registry_handle_global,
4245 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004246};
4247
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004248#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004249static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004250init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004251{
4252 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004253 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004254
Rob Clark6396ed32012-03-11 19:48:41 -05004255#ifdef USE_CAIRO_GLESV2
4256# define GL_BIT EGL_OPENGL_ES2_BIT
4257#else
4258# define GL_BIT EGL_OPENGL_BIT
4259#endif
4260
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004261 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004262 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004263 EGL_RED_SIZE, 1,
4264 EGL_GREEN_SIZE, 1,
4265 EGL_BLUE_SIZE, 1,
4266 EGL_ALPHA_SIZE, 1,
4267 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004268 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004269 EGL_NONE
4270 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004271
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004272#ifdef USE_CAIRO_GLESV2
4273 static const EGLint context_attribs[] = {
4274 EGL_CONTEXT_CLIENT_VERSION, 2,
4275 EGL_NONE
4276 };
4277 EGLint api = EGL_OPENGL_ES_API;
4278#else
4279 EGLint *context_attribs = NULL;
4280 EGLint api = EGL_OPENGL_API;
4281#endif
4282
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004283 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004284 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004285 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004286 return -1;
4287 }
4288
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004289 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004290 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004291 return -1;
4292 }
4293
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004294 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4295 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004296 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004297 return -1;
4298 }
4299
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004300 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004301 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004302 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004303 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004304 return -1;
4305 }
4306
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004307 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004308 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004309 return -1;
4310 }
4311
Benjamin Franzke0c991632011-09-27 21:57:31 +02004312 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4313 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004314 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004315 return -1;
4316 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004317
4318 return 0;
4319}
4320
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004321static void
4322fini_egl(struct display *display)
4323{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004324 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004325
4326 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4327 EGL_NO_CONTEXT);
4328
4329 eglTerminate(display->dpy);
4330 eglReleaseThread();
4331}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004332#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004333
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004334static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004335init_dummy_surface(struct display *display)
4336{
4337 int len;
4338 void *data;
4339
4340 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4341 data = malloc(len);
4342 display->dummy_surface =
4343 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4344 1, 1, len);
4345 display->dummy_surface_data = data;
4346}
4347
4348static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004349handle_display_data(struct task *task, uint32_t events)
4350{
4351 struct display *display =
4352 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004353 struct epoll_event ep;
4354 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004355
4356 display->display_fd_events = events;
4357
4358 if (events & EPOLLERR || events & EPOLLHUP) {
4359 display_exit(display);
4360 return;
4361 }
4362
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004363 if (events & EPOLLIN) {
4364 ret = wl_display_dispatch(display->display);
4365 if (ret == -1) {
4366 display_exit(display);
4367 return;
4368 }
4369 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004370
4371 if (events & EPOLLOUT) {
4372 ret = wl_display_flush(display->display);
4373 if (ret == 0) {
4374 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4375 ep.data.ptr = &display->display_task;
4376 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4377 display->display_fd, &ep);
4378 } else if (ret == -1 && errno != EAGAIN) {
4379 display_exit(display);
4380 return;
4381 }
4382 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004383}
4384
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004385struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004386display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004387{
4388 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004389
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004390 d = malloc(sizeof *d);
4391 if (d == NULL)
4392 return NULL;
4393
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004394 memset(d, 0, sizeof *d);
4395
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004396 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004397 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004398 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004399 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004400 return NULL;
4401 }
4402
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004403 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004404 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004405 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004406 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4407 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004408
4409 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004410 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004411 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004412 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004413
Daniel Stone97f68542012-05-30 16:32:01 +01004414 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004415 if (d->xkb_context == NULL) {
4416 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004417 return NULL;
4418 }
4419
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004420 d->workspace = 0;
4421 d->workspace_count = 1;
4422
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004423 d->registry = wl_display_get_registry(d->display);
4424 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004425
4426 if (wl_display_dispatch(d->display) < 0) {
4427 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4428 return NULL;
4429 }
4430
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004431#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004432 if (init_egl(d) < 0)
4433 fprintf(stderr, "EGL does not seem to work, "
4434 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004435#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004436
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004437 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004438
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004439 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004440
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004441 wl_list_init(&d->window_list);
4442
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004443 init_dummy_surface(d);
4444
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004445 return d;
4446}
4447
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004448static void
4449display_destroy_outputs(struct display *display)
4450{
4451 struct output *tmp;
4452 struct output *output;
4453
4454 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4455 output_destroy(output);
4456}
4457
Pekka Paalanene1207c72011-12-16 12:02:09 +02004458static void
4459display_destroy_inputs(struct display *display)
4460{
4461 struct input *tmp;
4462 struct input *input;
4463
4464 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4465 input_destroy(input);
4466}
4467
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004468void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004469display_destroy(struct display *display)
4470{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004471 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004472 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4473 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004474
4475 if (!wl_list_empty(&display->deferred_list))
4476 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4477
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004478 cairo_surface_destroy(display->dummy_surface);
4479 free(display->dummy_surface_data);
4480
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004481 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004482 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004483
Daniel Stone97f68542012-05-30 16:32:01 +01004484 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004485
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004486 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004487 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004488
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004489#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004490 if (display->argb_device)
4491 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004492#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004493
Pekka Paalanenc2052982011-12-16 11:41:32 +02004494 if (display->shell)
4495 wl_shell_destroy(display->shell);
4496
4497 if (display->shm)
4498 wl_shm_destroy(display->shm);
4499
4500 if (display->data_device_manager)
4501 wl_data_device_manager_destroy(display->data_device_manager);
4502
4503 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004504 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004505
4506 close(display->epoll_fd);
4507
U. Artie Eoff44874d92012-10-02 21:12:35 -07004508 if (!(display->display_fd_events & EPOLLERR) &&
4509 !(display->display_fd_events & EPOLLHUP))
4510 wl_display_flush(display->display);
4511
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004512 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004513 free(display);
4514}
4515
4516void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004517display_set_user_data(struct display *display, void *data)
4518{
4519 display->user_data = data;
4520}
4521
4522void *
4523display_get_user_data(struct display *display)
4524{
4525 return display->user_data;
4526}
4527
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004528struct wl_display *
4529display_get_display(struct display *display)
4530{
4531 return display->display;
4532}
4533
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004534struct output *
4535display_get_output(struct display *display)
4536{
4537 return container_of(display->output_list.next, struct output, link);
4538}
4539
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004540struct wl_compositor *
4541display_get_compositor(struct display *display)
4542{
4543 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004544}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004545
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004546uint32_t
4547display_get_serial(struct display *display)
4548{
4549 return display->serial;
4550}
4551
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004552EGLDisplay
4553display_get_egl_display(struct display *d)
4554{
4555 return d->dpy;
4556}
4557
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004558struct wl_data_source *
4559display_create_data_source(struct display *display)
4560{
4561 return wl_data_device_manager_create_data_source(display->data_device_manager);
4562}
4563
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004564EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004565display_get_argb_egl_config(struct display *d)
4566{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004567 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004568}
4569
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004570struct wl_shell *
4571display_get_shell(struct display *display)
4572{
4573 return display->shell;
4574}
4575
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004576int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004577display_acquire_window_surface(struct display *display,
4578 struct window *window,
4579 EGLContext ctx)
4580{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004581 struct surface *surface = window->main_surface;
4582
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004583 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004584 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004585
Pekka Paalanen6f41b072013-02-20 13:39:17 +02004586 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004587 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004588}
4589
4590void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004591display_release_window_surface(struct display *display,
4592 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004593{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004594 struct surface *surface = window->main_surface;
4595
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004596 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004597 return;
4598
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004599 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004600}
4601
4602void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004603display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004604{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004605 wl_list_insert(&display->deferred_list, &task->link);
4606}
4607
4608void
4609display_watch_fd(struct display *display,
4610 int fd, uint32_t events, struct task *task)
4611{
4612 struct epoll_event ep;
4613
4614 ep.events = events;
4615 ep.data.ptr = task;
4616 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4617}
4618
4619void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004620display_unwatch_fd(struct display *display, int fd)
4621{
4622 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4623}
4624
4625void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004626display_run(struct display *display)
4627{
4628 struct task *task;
4629 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004630 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004631
Pekka Paalanen826d7952011-12-15 10:14:07 +02004632 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004633 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004634 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004635 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004636 struct task, link);
4637 wl_list_remove(&task->link);
4638 task->run(task, 0);
4639 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004640
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004641 wl_display_dispatch_pending(display->display);
4642
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004643 if (!display->running)
4644 break;
4645
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004646 ret = wl_display_flush(display->display);
4647 if (ret < 0 && errno == EAGAIN) {
4648 ep[0].events =
4649 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4650 ep[0].data.ptr = &display->display_task;
4651
4652 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4653 display->display_fd, &ep[0]);
4654 } else if (ret < 0) {
4655 break;
4656 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004657
4658 count = epoll_wait(display->epoll_fd,
4659 ep, ARRAY_LENGTH(ep), -1);
4660 for (i = 0; i < count; i++) {
4661 task = ep[i].data.ptr;
4662 task->run(task, ep[i].events);
4663 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004664 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004665}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004666
4667void
4668display_exit(struct display *display)
4669{
4670 display->running = 0;
4671}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004672
4673void
4674keysym_modifiers_add(struct wl_array *modifiers_map,
4675 const char *name)
4676{
4677 size_t len = strlen(name) + 1;
4678 char *p;
4679
4680 p = wl_array_add(modifiers_map, len);
4681
4682 if (p == NULL)
4683 return;
4684
4685 strncpy(p, name, len);
4686}
4687
4688static xkb_mod_index_t
4689keysym_modifiers_get_index(struct wl_array *modifiers_map,
4690 const char *name)
4691{
4692 xkb_mod_index_t index = 0;
4693 char *p = modifiers_map->data;
4694
4695 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4696 if (strcmp(p, name) == 0)
4697 return index;
4698
4699 index++;
4700 p += strlen(p) + 1;
4701 }
4702
4703 return XKB_MOD_INVALID;
4704}
4705
4706xkb_mod_mask_t
4707keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4708 const char *name)
4709{
4710 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4711
4712 if (index == XKB_MOD_INVALID)
4713 return XKB_MOD_INVALID;
4714
4715 return 1 << index;
4716}