blob: f83ea013b78549f72594b3cb50b863c5bd568354 [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 Paalanen4e373742013-02-13 16:17:13 +0200203};
204
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500205struct window {
206 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500207 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100208 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500209 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200210 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400211 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500212 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500213 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400214 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400215 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400216 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400217 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400218 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400219 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400220 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400221 int focus_count;
222
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400223 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500224
Pekka Paalanen99436862012-11-19 17:15:59 +0200225 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200226 int fullscreen_method;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400227
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500228 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500229 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400230 window_data_handler_t data_handler;
231 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500232 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400233 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200234 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400235
Pekka Paalanen4e373742013-02-13 16:17:13 +0200236 struct surface *main_surface;
237 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300238 struct wl_callback *frame_cb;
239
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200240 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500241
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500242 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400243 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500244};
245
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500246struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500247 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200248 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300249 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500250 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400251 struct wl_list link;
252 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500253 widget_resize_handler_t resize_handler;
254 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500255 widget_enter_handler_t enter_handler;
256 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500257 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500258 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200259 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400260 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500261 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300262 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500263 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400264};
265
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400266struct input {
267 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100268 struct wl_seat *seat;
269 struct wl_pointer *pointer;
270 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400271 struct window *pointer_focus;
272 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300273 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300274 uint32_t cursor_anim_start;
275 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300276 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400277 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400278 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400279 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400280 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400281 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400282
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500283 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500284 struct widget *grab;
285 uint32_t grab_button;
286
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400287 struct wl_data_device *data_device;
288 struct data_offer *drag_offer;
289 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100290
291 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100292 struct xkb_keymap *keymap;
293 struct xkb_state *state;
294 xkb_mod_mask_t control_mask;
295 xkb_mod_mask_t alt_mask;
296 xkb_mod_mask_t shift_mask;
297 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400298
299 struct task repeat_task;
300 int repeat_timer_fd;
301 uint32_t repeat_sym;
302 uint32_t repeat_key;
303 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400304};
305
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500306struct output {
307 struct display *display;
308 struct wl_output *output;
309 struct rectangle allocation;
310 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600311 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200312
313 display_output_handler_t destroy_handler;
314 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500315};
316
Martin Minarik1998b152012-05-10 02:04:35 +0200317enum frame_button_action {
318 FRAME_BUTTON_NULL = 0,
319 FRAME_BUTTON_ICON = 1,
320 FRAME_BUTTON_CLOSE = 2,
321 FRAME_BUTTON_MINIMIZE = 3,
322 FRAME_BUTTON_MAXIMIZE = 4,
323};
324
325enum frame_button_pointer {
326 FRAME_BUTTON_DEFAULT = 0,
327 FRAME_BUTTON_OVER = 1,
328 FRAME_BUTTON_ACTIVE = 2,
329};
330
331enum frame_button_align {
332 FRAME_BUTTON_RIGHT = 0,
333 FRAME_BUTTON_LEFT = 1,
334};
335
336enum frame_button_decoration {
337 FRAME_BUTTON_NONE = 0,
338 FRAME_BUTTON_FANCY = 1,
339};
340
341struct frame_button {
342 struct widget *widget;
343 struct frame *frame;
344 cairo_surface_t *icon;
345 enum frame_button_action type;
346 enum frame_button_pointer state;
347 struct wl_list link; /* buttons_list */
348 enum frame_button_align align;
349 enum frame_button_decoration decoration;
350};
351
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500352struct frame {
353 struct widget *widget;
354 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200355 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500356};
357
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500358struct menu {
359 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500360 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500361 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500362 const char **entries;
363 uint32_t time;
364 int current;
365 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400366 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500367 menu_func_t func;
368};
369
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300370struct tooltip {
371 struct widget *parent;
372 struct window *window;
373 struct widget *widget;
374 char *entry;
375 struct task tooltip_task;
376 int tooltip_fd;
377 float x, y;
378};
379
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700380struct shm_pool {
381 struct wl_shm_pool *pool;
382 size_t size;
383 size_t used;
384 void *data;
385};
386
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400387enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300388 CURSOR_DEFAULT = 100,
389 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400390};
391
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500392enum window_location {
393 WINDOW_INTERIOR = 0,
394 WINDOW_RESIZING_TOP = 1,
395 WINDOW_RESIZING_BOTTOM = 2,
396 WINDOW_RESIZING_LEFT = 4,
397 WINDOW_RESIZING_TOP_LEFT = 5,
398 WINDOW_RESIZING_BOTTOM_LEFT = 6,
399 WINDOW_RESIZING_RIGHT = 8,
400 WINDOW_RESIZING_TOP_RIGHT = 9,
401 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
402 WINDOW_RESIZING_MASK = 15,
403 WINDOW_EXTERIOR = 16,
404 WINDOW_TITLEBAR = 17,
405 WINDOW_CLIENT_AREA = 18,
406};
407
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200408static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500410#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200412struct egl_window_surface {
413 struct toysurface base;
414 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100415 struct display *display;
416 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200417 struct wl_egl_window *egl_window;
418 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100419};
420
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200421static struct egl_window_surface *
422to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100423{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200424 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100425}
426
427static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200428egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200429 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100430{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200431 struct egl_window_surface *surface = to_egl_window_surface(base);
432
433 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
434 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
435
436 return cairo_surface_reference(surface->cairo_surface);
437}
438
439static void
440egl_window_surface_swap(struct toysurface *base,
441 struct rectangle *server_allocation)
442{
443 struct egl_window_surface *surface = to_egl_window_surface(base);
444
445 cairo_gl_surface_swapbuffers(surface->cairo_surface);
446 wl_egl_window_get_attached_size(surface->egl_window,
447 &server_allocation->width,
448 &server_allocation->height);
449}
450
451static int
452egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
453{
454 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200455 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100456
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200457 device = cairo_surface_get_device(surface->cairo_surface);
458 if (!device)
459 return -1;
460
461 if (!ctx) {
462 if (device == surface->display->argb_device)
463 ctx = surface->display->argb_ctx;
464 else
465 assert(0);
466 }
467
468 cairo_device_flush(device);
469 cairo_device_acquire(device);
470 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
471 surface->egl_surface, ctx))
472 fprintf(stderr, "failed to make surface current\n");
473
474 return 0;
475}
476
477static void
478egl_window_surface_release(struct toysurface *base)
479{
480 struct egl_window_surface *surface = to_egl_window_surface(base);
481 cairo_device_t *device;
482
483 device = cairo_surface_get_device(surface->cairo_surface);
484 if (!device)
485 return;
486
487 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
488 surface->display->argb_ctx))
489 fprintf(stderr, "failed to make context current\n");
490
491 cairo_device_release(device);
492}
493
494static void
495egl_window_surface_destroy(struct toysurface *base)
496{
497 struct egl_window_surface *surface = to_egl_window_surface(base);
498 struct display *d = surface->display;
499
500 cairo_surface_destroy(surface->cairo_surface);
501 eglDestroySurface(d->dpy, surface->egl_surface);
502 wl_egl_window_destroy(surface->egl_window);
503 surface->surface = NULL;
504
505 free(surface);
506}
507
508static struct toysurface *
509egl_window_surface_create(struct display *display,
510 struct wl_surface *wl_surface,
511 uint32_t flags,
512 struct rectangle *rectangle)
513{
514 struct egl_window_surface *surface;
515
Pekka Paalanenb3627362012-11-19 17:16:00 +0200516 if (display->dpy == EGL_NO_DISPLAY)
517 return NULL;
518
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519 surface = calloc(1, sizeof *surface);
520 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521 return NULL;
522
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 surface->base.prepare = egl_window_surface_prepare;
524 surface->base.swap = egl_window_surface_swap;
525 surface->base.acquire = egl_window_surface_acquire;
526 surface->base.release = egl_window_surface_release;
527 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100528
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200529 surface->display = display;
530 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400531
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200532 surface->egl_window = wl_egl_window_create(surface->surface,
533 rectangle->width,
534 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100535
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200536 surface->egl_surface = eglCreateWindowSurface(display->dpy,
537 display->argb_config,
538 surface->egl_window,
539 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100540
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200541 surface->cairo_surface =
542 cairo_gl_surface_create_for_egl(display->argb_device,
543 surface->egl_surface,
544 rectangle->width,
545 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100546
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200547 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100548}
549
Pekka Paalanenb3627362012-11-19 17:16:00 +0200550#else
551
552static struct toysurface *
553egl_window_surface_create(struct display *display,
554 struct wl_surface *wl_surface,
555 uint32_t flags,
556 struct rectangle *rectangle)
557{
558 return NULL;
559}
560
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561#endif
562
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200563struct shm_surface_data {
564 struct wl_buffer *buffer;
565 struct shm_pool *pool;
566};
567
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400568struct wl_buffer *
569display_get_buffer_for_surface(struct display *display,
570 cairo_surface_t *surface)
571{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200572 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200574 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400575
576 return data->buffer;
577}
578
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500579static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700580shm_pool_destroy(struct shm_pool *pool);
581
582static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400583shm_surface_data_destroy(void *p)
584{
585 struct shm_surface_data *data = p;
586
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200587 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700588 if (data->pool)
589 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300590
591 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400592}
593
Kristian Høgsberg16626282012-04-03 11:21:27 -0400594static struct wl_shm_pool *
595make_shm_pool(struct display *display, int size, void **data)
596{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400597 struct wl_shm_pool *pool;
598 int fd;
599
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300600 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400601 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300602 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
603 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400604 return NULL;
605 }
606
607 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400608 if (*data == MAP_FAILED) {
609 fprintf(stderr, "mmap failed: %m\n");
610 close(fd);
611 return NULL;
612 }
613
614 pool = wl_shm_create_pool(display->shm, fd, size);
615
616 close(fd);
617
618 return pool;
619}
620
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700621static struct shm_pool *
622shm_pool_create(struct display *display, size_t size)
623{
624 struct shm_pool *pool = malloc(sizeof *pool);
625
626 if (!pool)
627 return NULL;
628
629 pool->pool = make_shm_pool(display, size, &pool->data);
630 if (!pool->pool) {
631 free(pool);
632 return NULL;
633 }
634
635 pool->size = size;
636 pool->used = 0;
637
638 return pool;
639}
640
641static void *
642shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
643{
644 if (pool->used + size > pool->size)
645 return NULL;
646
647 *offset = pool->used;
648 pool->used += size;
649
650 return (char *) pool->data + *offset;
651}
652
653/* destroy the pool. this does not unmap the memory though */
654static void
655shm_pool_destroy(struct shm_pool *pool)
656{
657 munmap(pool->data, pool->size);
658 wl_shm_pool_destroy(pool->pool);
659 free(pool);
660}
661
662/* Start allocating from the beginning of the pool again */
663static void
664shm_pool_reset(struct shm_pool *pool)
665{
666 pool->used = 0;
667}
668
669static int
670data_length_for_shm_surface(struct rectangle *rect)
671{
672 int stride;
673
674 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
675 rect->width);
676 return stride * rect->height;
677}
678
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500679static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700680display_create_shm_surface_from_pool(struct display *display,
681 struct rectangle *rectangle,
682 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400683{
684 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400685 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400686 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700687 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400688 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689
690 data = malloc(sizeof *data);
691 if (data == NULL)
692 return NULL;
693
694 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
695 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700696 length = stride * rectangle->height;
697 data->pool = NULL;
698 map = shm_pool_allocate(pool, length, &offset);
699
700 if (!map) {
701 free(data);
702 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400703 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400705 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706 CAIRO_FORMAT_ARGB32,
707 rectangle->width,
708 rectangle->height,
709 stride);
710
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200711 cairo_surface_set_user_data(surface, &shm_surface_data_key,
712 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400713
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400714 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500715 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400716 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500717 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400718
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200719 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
720 rectangle->width,
721 rectangle->height,
722 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400723
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700724 return surface;
725}
726
727static cairo_surface_t *
728display_create_shm_surface(struct display *display,
729 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200730 struct shm_pool *alternate_pool,
731 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700732{
733 struct shm_surface_data *data;
734 struct shm_pool *pool;
735 cairo_surface_t *surface;
736
Pekka Paalanen99436862012-11-19 17:15:59 +0200737 if (alternate_pool) {
738 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700739 surface = display_create_shm_surface_from_pool(display,
740 rectangle,
741 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200742 alternate_pool);
743 if (surface) {
744 data = cairo_surface_get_user_data(surface,
745 &shm_surface_data_key);
746 goto out;
747 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700748 }
749
750 pool = shm_pool_create(display,
751 data_length_for_shm_surface(rectangle));
752 if (!pool)
753 return NULL;
754
755 surface =
756 display_create_shm_surface_from_pool(display, rectangle,
757 flags, pool);
758
759 if (!surface) {
760 shm_pool_destroy(pool);
761 return NULL;
762 }
763
764 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200765 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700766 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400767
Pekka Paalanen99436862012-11-19 17:15:59 +0200768out:
769 if (data_ret)
770 *data_ret = data;
771
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400772 return surface;
773}
774
nobled7b87cb02011-02-01 18:51:47 +0000775static int
776check_size(struct rectangle *rect)
777{
778 if (rect->width && rect->height)
779 return 0;
780
781 fprintf(stderr, "tried to create surface of "
782 "width: %d, height: %d\n", rect->width, rect->height);
783 return -1;
784}
785
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400786cairo_surface_t *
787display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400789 struct rectangle *rectangle,
790 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400791{
nobled7b87cb02011-02-01 18:51:47 +0000792 if (check_size(rectangle) < 0)
793 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200794
795 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200796 return display_create_shm_surface(display, rectangle, flags,
797 NULL, NULL);
798}
799
Pekka Paalanena4eda732012-11-19 17:16:02 +0200800struct shm_surface_leaf {
801 cairo_surface_t *cairo_surface;
802 /* 'data' is automatically destroyed, when 'cairo_surface' is */
803 struct shm_surface_data *data;
804
805 struct shm_pool *resize_pool;
806 int busy;
807};
808
809static void
810shm_surface_leaf_release(struct shm_surface_leaf *leaf)
811{
812 if (leaf->cairo_surface)
813 cairo_surface_destroy(leaf->cairo_surface);
814 /* leaf->data already destroyed via cairo private */
815
816 if (leaf->resize_pool)
817 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200818
819 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200820}
821
Pekka Paalanen99436862012-11-19 17:15:59 +0200822struct shm_surface {
823 struct toysurface base;
824 struct display *display;
825 struct wl_surface *surface;
826 uint32_t flags;
827 int dx, dy;
828
Pekka Paalanena4eda732012-11-19 17:16:02 +0200829 struct shm_surface_leaf leaf[2];
830 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200831};
832
833static struct shm_surface *
834to_shm_surface(struct toysurface *base)
835{
836 return container_of(base, struct shm_surface, base);
837}
838
Pekka Paalanena4eda732012-11-19 17:16:02 +0200839static void
840shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
841{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200842 struct shm_surface *surface = data;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200843
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200844 if (surface->leaf[0].data->buffer == buffer)
845 surface->leaf[0].busy = 0;
846 else if (surface->leaf[1].data->buffer == buffer)
847 surface->leaf[1].busy = 0;
848 else
849 assert(0 && "shm_surface_buffer_release: unknown buffer");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200850
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200851 if (!surface->leaf[0].busy && !surface->leaf[1].busy)
852 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200853}
854
855static const struct wl_buffer_listener shm_surface_buffer_listener = {
856 shm_surface_buffer_release
857};
858
Pekka Paalanen99436862012-11-19 17:15:59 +0200859static cairo_surface_t *
860shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200861 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200862{
Pekka Paalanenec076692012-11-30 13:37:27 +0200863 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200864 struct shm_surface *surface = to_shm_surface(base);
865 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200866 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200867
868 surface->dx = dx;
869 surface->dy = dy;
870
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200871 /* See shm_surface_buffer_release() */
872 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
873 surface->leaf[1].cairo_surface) {
874 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
875 __func__);
876 }
877
Pekka Paalanena4eda732012-11-19 17:16:02 +0200878 /* pick a free buffer from the two */
879 if (!surface->leaf[0].busy)
880 leaf = &surface->leaf[0];
881 else if (!surface->leaf[1].busy)
882 leaf = &surface->leaf[1];
883 else {
884 fprintf(stderr, "%s: both buffers are held by the server.\n",
885 __func__);
886 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200887 }
888
Pekka Paalanena4eda732012-11-19 17:16:02 +0200889 if (!resize_hint && leaf->resize_pool) {
890 cairo_surface_destroy(leaf->cairo_surface);
891 leaf->cairo_surface = NULL;
892 shm_pool_destroy(leaf->resize_pool);
893 leaf->resize_pool = NULL;
894 }
895
896 if (leaf->cairo_surface &&
897 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
898 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200899 goto out;
900
Pekka Paalanena4eda732012-11-19 17:16:02 +0200901 if (leaf->cairo_surface)
902 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200903
Pekka Paalanena4eda732012-11-19 17:16:02 +0200904 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200905 /* Create a big pool to allocate from, while continuously
906 * resizing. Mmapping a new pool in the server
907 * is relatively expensive, so reusing a pool performs
908 * better, but may temporarily reserve unneeded memory.
909 */
910 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200911 leaf->resize_pool = shm_pool_create(surface->display,
912 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200913 }
914
Pekka Paalanena4eda732012-11-19 17:16:02 +0200915 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200916 display_create_shm_surface(surface->display, &rect,
917 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200918 leaf->resize_pool,
919 &leaf->data);
920 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200921 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200922
923out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200924 surface->current = leaf;
925
926 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200927}
928
929static void
930shm_surface_swap(struct toysurface *base,
931 struct rectangle *server_allocation)
932{
933 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200934 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200935
936 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200937 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200938 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200939 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200940
Pekka Paalanena4eda732012-11-19 17:16:02 +0200941 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200942 surface->dx, surface->dy);
943 wl_surface_damage(surface->surface, 0, 0,
944 server_allocation->width, server_allocation->height);
945 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200946
947 leaf->busy = 1;
948 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200949}
950
951static int
952shm_surface_acquire(struct toysurface *base, EGLContext ctx)
953{
954 return -1;
955}
956
957static void
958shm_surface_release(struct toysurface *base)
959{
960}
961
962static void
963shm_surface_destroy(struct toysurface *base)
964{
965 struct shm_surface *surface = to_shm_surface(base);
966
Pekka Paalanena4eda732012-11-19 17:16:02 +0200967 shm_surface_leaf_release(&surface->leaf[0]);
968 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200969
970 free(surface);
971}
972
973static struct toysurface *
974shm_surface_create(struct display *display, struct wl_surface *wl_surface,
975 uint32_t flags, struct rectangle *rectangle)
976{
977 struct shm_surface *surface;
978
979 surface = calloc(1, sizeof *surface);
980 if (!surface)
981 return NULL;
982
983 surface->base.prepare = shm_surface_prepare;
984 surface->base.swap = shm_surface_swap;
985 surface->base.acquire = shm_surface_acquire;
986 surface->base.release = shm_surface_release;
987 surface->base.destroy = shm_surface_destroy;
988
989 surface->display = display;
990 surface->surface = wl_surface;
991 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200992
993 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400994}
995
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200996/*
997 * The following correspondences between file names and cursors was copied
998 * from: https://bugs.kde.org/attachment.cgi?id=67313
999 */
1000
1001static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001002 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001003 "sw-resize"
1004};
1005
1006static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001007 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001008 "se-resize"
1009};
1010
1011static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001012 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001013 "s-resize"
1014};
1015
1016static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001017 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001018 "closedhand",
1019 "208530c400c041818281048008011002"
1020};
1021
1022static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001023 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001024 "default",
1025 "top_left_arrow",
1026 "left-arrow"
1027};
1028
1029static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001030 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001031 "w-resize"
1032};
1033
1034static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001035 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001036 "e-resize"
1037};
1038
1039static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001040 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001041 "nw-resize"
1042};
1043
1044static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001045 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001046 "ne-resize"
1047};
1048
1049static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001050 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001051 "n-resize"
1052};
1053
1054static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001055 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001056 "ibeam",
1057 "text"
1058};
1059
1060static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001061 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001062 "pointer",
1063 "pointing_hand",
1064 "e29285e634086352946a0e7090d73106"
1065};
1066
1067static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001068 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001069 "wait",
1070 "0426c94ea35c87780ff01dc239897213"
1071};
1072
1073struct cursor_alternatives {
1074 const char **names;
1075 size_t count;
1076};
1077
1078static const struct cursor_alternatives cursors[] = {
1079 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1080 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1081 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1082 {grabbings, ARRAY_LENGTH(grabbings)},
1083 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1084 {left_sides, ARRAY_LENGTH(left_sides)},
1085 {right_sides, ARRAY_LENGTH(right_sides)},
1086 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1087 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1088 {top_sides, ARRAY_LENGTH(top_sides)},
1089 {xterms, ARRAY_LENGTH(xterms)},
1090 {hand1s, ARRAY_LENGTH(hand1s)},
1091 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001092};
1093
1094static void
1095create_cursors(struct display *display)
1096{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001097 char *config_file;
1098 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001099 unsigned int i, j;
1100 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001101 struct config_key shell_keys[] = {
1102 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1103 };
1104 struct config_section cs[] = {
1105 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1106 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001107
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001108 config_file = config_file_path("weston.ini");
1109 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1110 free(config_file);
1111
1112 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001113 display->cursors =
1114 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1115
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001116 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001117 cursor = NULL;
1118 for (j = 0; !cursor && j < cursors[i].count; ++j)
1119 cursor = wl_cursor_theme_get_cursor(
1120 display->cursor_theme, cursors[i].names[j]);
1121
1122 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001123 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001124 cursors[i].names[0]);
1125
1126 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001127 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001128}
1129
1130static void
1131destroy_cursors(struct display *display)
1132{
1133 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001134 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001135}
1136
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001137struct wl_cursor_image *
1138display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001140 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001141
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001142 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001143}
1144
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001145static void
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001146surface_attach_surface(struct surface *surface)
1147{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001148 if (surface->opaque_region) {
1149 wl_surface_set_opaque_region(surface->surface,
1150 surface->opaque_region);
1151 wl_region_destroy(surface->opaque_region);
1152 surface->opaque_region = NULL;
1153 }
1154
1155 if (surface->input_region) {
1156 wl_surface_set_input_region(surface->surface,
1157 surface->input_region);
1158 wl_region_destroy(surface->input_region);
1159 surface->input_region = NULL;
1160 }
1161
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001162 surface->toysurface->swap(surface->toysurface,
1163 &surface->server_allocation);
1164}
1165
1166static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001167window_attach_surface(struct window *window)
1168{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001169 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001170
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001171 if (window->type == TYPE_NONE) {
1172 window->type = TYPE_TOPLEVEL;
1173 if (display->shell)
1174 wl_shell_surface_set_toplevel(window->shell_surface);
1175 }
1176
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001177 surface_attach_surface(window->main_surface);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001178}
1179
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001180int
1181window_has_focus(struct window *window)
1182{
1183 return window->focus_count > 0;
1184}
1185
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001186static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001187window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001188{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001189 if (!window->cairo_surface)
1190 return;
1191
1192 window_attach_surface(window);
1193 cairo_surface_destroy(window->cairo_surface);
1194 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001195}
1196
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001197struct display *
1198window_get_display(struct window *window)
1199{
1200 return window->display;
1201}
1202
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001203static cairo_surface_t *
1204surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001205{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001206 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001207 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001208
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001209 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001210 case WL_OUTPUT_TRANSFORM_90:
1211 case WL_OUTPUT_TRANSFORM_270:
1212 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1213 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001214 allocation.width = surface->allocation.height;
1215 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001216 break;
1217 default:
1218 break;
1219 }
1220
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001221 if (!surface->toysurface && display->dpy &&
1222 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001223 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001224 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001225 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001226 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001227 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001228 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001229
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001230 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001231 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001232 surface->surface,
1233 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001234
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001235 return surface->toysurface->prepare(surface->toysurface, dx, dy,
1236 allocation.width,
1237 allocation.height,
1238 flags);
1239}
1240
1241static void
1242window_create_surface(struct window *window)
1243{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001244 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001245 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001246 int dx = 0;
1247 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001248
1249 if (!window->transparent)
1250 flags |= SURFACE_OPAQUE;
1251
Pekka Paalanenec076692012-11-30 13:37:27 +02001252 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001253 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001254
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001255 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1256 dx = surface->server_allocation.width -
1257 surface->allocation.width;
1258
1259 if (window->resize_edges & WINDOW_RESIZING_TOP)
1260 dy = surface->server_allocation.height -
1261 surface->allocation.height;
1262
1263 window->resize_edges = 0;
1264
1265 window->cairo_surface = surface_create_surface(window->main_surface,
1266 dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001267}
1268
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001269int
1270window_get_buffer_transform(struct window *window)
1271{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001272 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001273}
1274
1275void
1276window_set_buffer_transform(struct window *window,
1277 enum wl_output_transform transform)
1278{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001279 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001280 wl_surface_set_buffer_transform(window->main_surface->surface,
1281 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001282}
1283
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001284static void frame_destroy(struct frame *frame);
1285
Pekka Paalanen4e373742013-02-13 16:17:13 +02001286static void
1287surface_destroy(struct surface *surface)
1288{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001289 if (surface->input_region)
1290 wl_region_destroy(surface->input_region);
1291
1292 if (surface->opaque_region)
1293 wl_region_destroy(surface->opaque_region);
1294
Pekka Paalanen4e373742013-02-13 16:17:13 +02001295 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001296
1297 if (surface->toysurface)
1298 surface->toysurface->destroy(surface->toysurface);
1299
Pekka Paalanen4e373742013-02-13 16:17:13 +02001300 free(surface);
1301}
1302
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001303void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001304window_destroy(struct window *window)
1305{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001306 struct display *display = window->display;
1307 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001308 struct window_output *window_output;
1309 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001310
1311 if (window->redraw_scheduled)
1312 wl_list_remove(&window->redraw_task.link);
1313
1314 wl_list_for_each(input, &display->input_list, link) {
1315 if (input->pointer_focus == window)
1316 input->pointer_focus = NULL;
1317 if (input->keyboard_focus == window)
1318 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001319 if (input->focus_widget &&
1320 input->focus_widget->window == window)
1321 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001322 }
1323
Rob Bradford7507b572012-05-15 17:55:34 +01001324 wl_list_for_each_safe(window_output, window_output_tmp,
1325 &window->window_output_list, link) {
1326 free (window_output);
1327 }
1328
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001329 if (window->frame)
1330 frame_destroy(window->frame);
1331
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001332 if (window->shell_surface)
1333 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001334
1335 surface_destroy(window->main_surface);
1336
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001337 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001338
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001339 if (window->frame_cb)
1340 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001341 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001342 free(window);
1343}
1344
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001345static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001346widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001347{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001348 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001349
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001350 wl_list_for_each(child, &widget->child_list, link) {
1351 target = widget_find_widget(child, x, y);
1352 if (target)
1353 return target;
1354 }
1355
1356 if (widget->allocation.x <= x &&
1357 x < widget->allocation.x + widget->allocation.width &&
1358 widget->allocation.y <= y &&
1359 y < widget->allocation.y + widget->allocation.height) {
1360 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001361 }
1362
1363 return NULL;
1364}
1365
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001366static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001367window_find_widget(struct window *window, int32_t x, int32_t y)
1368{
1369 return widget_find_widget(window->main_surface->widget, x, y);
1370}
1371
1372static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001373widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001374{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001375 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001376
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001377 widget = malloc(sizeof *widget);
1378 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001379 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001380 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001381 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001382 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001383 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001384 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001385 widget->tooltip = NULL;
1386 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001387 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001388
1389 return widget;
1390}
1391
1392struct widget *
1393window_add_widget(struct window *window, void *data)
1394{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001395 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001396
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001397 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001398 wl_list_init(&widget->link);
1399 window->main_surface->widget = widget;
1400
1401 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001402}
1403
1404struct widget *
1405widget_add_widget(struct widget *parent, void *data)
1406{
1407 struct widget *widget;
1408
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001409 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001410 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001411
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001412 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001413}
1414
1415void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001416widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001417{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001418 struct display *display = widget->window->display;
1419 struct input *input;
1420
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001421 if (widget->tooltip) {
1422 free(widget->tooltip);
1423 widget->tooltip = NULL;
1424 }
1425
Pekka Paalanene156fb62012-01-19 13:51:38 +02001426 wl_list_for_each(input, &display->input_list, link) {
1427 if (input->focus_widget == widget)
1428 input->focus_widget = NULL;
1429 }
1430
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001431 wl_list_remove(&widget->link);
1432 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001433}
1434
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001435void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001436widget_set_default_cursor(struct widget *widget, int cursor)
1437{
1438 widget->default_cursor = cursor;
1439}
1440
1441void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001442widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001443{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001444 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001445}
1446
1447void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001448widget_set_size(struct widget *widget, int32_t width, int32_t height)
1449{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001450 widget->allocation.width = width;
1451 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001452}
1453
1454void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001455widget_set_allocation(struct widget *widget,
1456 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001457{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001458 widget->allocation.x = x;
1459 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001460 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001461}
1462
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001463void
1464widget_set_transparent(struct widget *widget, int transparent)
1465{
1466 widget->opaque = !transparent;
1467}
1468
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001469void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001470widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001471{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001472 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001473}
1474
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001475void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001476widget_set_resize_handler(struct widget *widget,
1477 widget_resize_handler_t handler)
1478{
1479 widget->resize_handler = handler;
1480}
1481
1482void
1483widget_set_redraw_handler(struct widget *widget,
1484 widget_redraw_handler_t handler)
1485{
1486 widget->redraw_handler = handler;
1487}
1488
1489void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001490widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001491{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001492 widget->enter_handler = handler;
1493}
1494
1495void
1496widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1497{
1498 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001499}
1500
1501void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001502widget_set_motion_handler(struct widget *widget,
1503 widget_motion_handler_t handler)
1504{
1505 widget->motion_handler = handler;
1506}
1507
1508void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001509widget_set_button_handler(struct widget *widget,
1510 widget_button_handler_t handler)
1511{
1512 widget->button_handler = handler;
1513}
1514
1515void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001516widget_set_axis_handler(struct widget *widget,
1517 widget_axis_handler_t handler)
1518{
1519 widget->axis_handler = handler;
1520}
1521
1522void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001523widget_schedule_redraw(struct widget *widget)
1524{
1525 window_schedule_redraw(widget->window);
1526}
1527
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001528cairo_surface_t *
1529window_get_surface(struct window *window)
1530{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001531 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001532}
1533
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001534struct wl_surface *
1535window_get_wl_surface(struct window *window)
1536{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001537 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001538}
1539
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001540struct wl_shell_surface *
1541window_get_wl_shell_surface(struct window *window)
1542{
1543 return window->shell_surface;
1544}
1545
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001546static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001547tooltip_redraw_handler(struct widget *widget, void *data)
1548{
1549 cairo_t *cr;
1550 const int32_t r = 3;
1551 struct tooltip *tooltip = data;
1552 int32_t width, height;
1553 struct window *window = widget->window;
1554
1555 cr = cairo_create(window->cairo_surface);
1556 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1557 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1558 cairo_paint(cr);
1559
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001560 width = window->main_surface->allocation.width;
1561 height = window->main_surface->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001562 rounded_rect(cr, 0, 0, width, height, r);
1563
1564 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1565 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1566 cairo_fill(cr);
1567
1568 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1569 cairo_move_to(cr, 10, 16);
1570 cairo_show_text(cr, tooltip->entry);
1571 cairo_destroy(cr);
1572}
1573
1574static cairo_text_extents_t
1575get_text_extents(struct tooltip *tooltip)
1576{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001577 cairo_t *cr;
1578 cairo_text_extents_t extents;
1579
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001580 /* Use the dummy_surface because tooltip's surface was not
1581 * created yet, and parent does not have a valid surface
1582 * outside repaint, either.
1583 */
1584 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001585 cairo_text_extents(cr, tooltip->entry, &extents);
1586 cairo_destroy(cr);
1587
1588 return extents;
1589}
1590
1591static int
1592window_create_tooltip(struct tooltip *tooltip)
1593{
1594 struct widget *parent = tooltip->parent;
1595 struct display *display = parent->window->display;
1596 struct window *window;
1597 const int offset_y = 27;
1598 const int margin = 3;
1599 cairo_text_extents_t extents;
1600
1601 if (tooltip->widget)
1602 return 0;
1603
1604 window = window_create_transient(display, parent->window, tooltip->x,
1605 tooltip->y + offset_y,
1606 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1607 if (!window)
1608 return -1;
1609
1610 tooltip->window = window;
1611 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1612
1613 extents = get_text_extents(tooltip);
1614 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1615 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1616
1617 return 0;
1618}
1619
1620void
1621widget_destroy_tooltip(struct widget *parent)
1622{
1623 struct tooltip *tooltip = parent->tooltip;
1624
1625 parent->tooltip_count = 0;
1626 if (!tooltip)
1627 return;
1628
1629 if (tooltip->widget) {
1630 widget_destroy(tooltip->widget);
1631 window_destroy(tooltip->window);
1632 tooltip->widget = NULL;
1633 tooltip->window = NULL;
1634 }
1635
1636 close(tooltip->tooltip_fd);
1637 free(tooltip->entry);
1638 free(tooltip);
1639 parent->tooltip = NULL;
1640}
1641
1642static void
1643tooltip_func(struct task *task, uint32_t events)
1644{
1645 struct tooltip *tooltip =
1646 container_of(task, struct tooltip, tooltip_task);
1647 uint64_t exp;
1648
Martin Olsson8df662a2012-07-08 03:03:47 +02001649 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1650 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001651 window_create_tooltip(tooltip);
1652}
1653
1654#define TOOLTIP_TIMEOUT 500
1655static int
1656tooltip_timer_reset(struct tooltip *tooltip)
1657{
1658 struct itimerspec its;
1659
1660 its.it_interval.tv_sec = 0;
1661 its.it_interval.tv_nsec = 0;
1662 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1663 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1664 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1665 fprintf(stderr, "could not set timerfd\n: %m");
1666 return -1;
1667 }
1668
1669 return 0;
1670}
1671
1672int
1673widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1674{
1675 struct tooltip *tooltip = parent->tooltip;
1676
1677 parent->tooltip_count++;
1678 if (tooltip) {
1679 tooltip->x = x;
1680 tooltip->y = y;
1681 tooltip_timer_reset(tooltip);
1682 return 0;
1683 }
1684
1685 /* the handler might be triggered too fast via input device motion, so
1686 * we need this check here to make sure tooltip is fully initialized */
1687 if (parent->tooltip_count > 1)
1688 return 0;
1689
1690 tooltip = malloc(sizeof *tooltip);
1691 if (!tooltip)
1692 return -1;
1693
1694 parent->tooltip = tooltip;
1695 tooltip->parent = parent;
1696 tooltip->widget = NULL;
1697 tooltip->window = NULL;
1698 tooltip->x = x;
1699 tooltip->y = y;
1700 tooltip->entry = strdup(entry);
1701 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1702 if (tooltip->tooltip_fd < 0) {
1703 fprintf(stderr, "could not create timerfd\n: %m");
1704 return -1;
1705 }
1706
1707 tooltip->tooltip_task.run = tooltip_func;
1708 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1709 EPOLLIN, &tooltip->tooltip_task);
1710 tooltip_timer_reset(tooltip);
1711
1712 return 0;
1713}
1714
1715static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001716workspace_manager_state(void *data,
1717 struct workspace_manager *workspace_manager,
1718 uint32_t current,
1719 uint32_t count)
1720{
1721 struct display *display = data;
1722
1723 display->workspace = current;
1724 display->workspace_count = count;
1725}
1726
1727static const struct workspace_manager_listener workspace_manager_listener = {
1728 workspace_manager_state
1729};
1730
1731static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001732frame_resize_handler(struct widget *widget,
1733 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001734{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001735 struct frame *frame = data;
1736 struct widget *child = frame->child;
1737 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001738 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001739 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001740 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001741 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001742 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001743 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001744 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001745
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001746 switch (widget->window->type) {
1747 case TYPE_FULLSCREEN:
1748 decoration_width = 0;
1749 decoration_height = 0;
1750
1751 allocation.x = 0;
1752 allocation.y = 0;
1753 allocation.width = width;
1754 allocation.height = height;
1755 opaque_margin = 0;
1756
1757 wl_list_for_each(button, &frame->buttons_list, link)
1758 button->widget->opaque = 1;
1759 break;
1760 case TYPE_MAXIMIZED:
1761 decoration_width = t->width * 2;
1762 decoration_height = t->width + t->titlebar_height;
1763
1764 allocation.x = t->width;
1765 allocation.y = t->titlebar_height;
1766 allocation.width = width - decoration_width;
1767 allocation.height = height - decoration_height;
1768
1769 opaque_margin = 0;
1770
1771 wl_list_for_each(button, &frame->buttons_list, link)
1772 button->widget->opaque = 0;
1773 break;
1774 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001775 decoration_width = (t->width + t->margin) * 2;
1776 decoration_height = t->width +
1777 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001778
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001779 allocation.x = t->width + t->margin;
1780 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001781 allocation.width = width - decoration_width;
1782 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001783
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001784 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001785
1786 wl_list_for_each(button, &frame->buttons_list, link)
1787 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001788 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001789 }
1790
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001791 widget_set_allocation(child, allocation.x, allocation.y,
1792 allocation.width, allocation.height);
1793
1794 if (child->resize_handler)
1795 child->resize_handler(child,
1796 allocation.width,
1797 allocation.height,
1798 child->user_data);
1799
Scott Moreauf7e498c2012-05-14 11:39:29 -06001800 width = child->allocation.width + decoration_width;
1801 height = child->allocation.height + decoration_height;
1802
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001803 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1804
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001805 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001806 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001807 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001808 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001809 shadow_margin, shadow_margin,
1810 width - 2 * shadow_margin,
1811 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001812 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001813 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001814 }
1815
Scott Moreauf7e498c2012-05-14 11:39:29 -06001816 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001817
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001818 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001819 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001820 opaque_margin, opaque_margin,
1821 widget->allocation.width - 2 * opaque_margin,
1822 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001823
1824 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001825 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1826 x_l = t->width + shadow_margin;
1827 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001828 wl_list_for_each(button, &frame->buttons_list, link) {
1829 const int button_padding = 4;
1830 w = cairo_image_surface_get_width(button->icon);
1831 h = cairo_image_surface_get_height(button->icon);
1832
1833 if (button->decoration == FRAME_BUTTON_FANCY)
1834 w += 10;
1835
1836 if (button->align == FRAME_BUTTON_LEFT) {
1837 widget_set_allocation(button->widget,
1838 x_l, y , w + 1, h + 1);
1839 x_l += w;
1840 x_l += button_padding;
1841 } else {
1842 x_r -= w;
1843 widget_set_allocation(button->widget,
1844 x_r, y , w + 1, h + 1);
1845 x_r -= button_padding;
1846 }
1847 }
1848}
1849
1850static int
1851frame_button_enter_handler(struct widget *widget,
1852 struct input *input, float x, float y, void *data)
1853{
1854 struct frame_button *frame_button = data;
1855
1856 widget_schedule_redraw(frame_button->widget);
1857 frame_button->state = FRAME_BUTTON_OVER;
1858
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001859 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001860}
1861
1862static void
1863frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1864{
1865 struct frame_button *frame_button = data;
1866
1867 widget_schedule_redraw(frame_button->widget);
1868 frame_button->state = FRAME_BUTTON_DEFAULT;
1869}
1870
1871static void
1872frame_button_button_handler(struct widget *widget,
1873 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001874 uint32_t button,
1875 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001876{
1877 struct frame_button *frame_button = data;
1878 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001879 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001880
1881 if (button != BTN_LEFT)
1882 return;
1883
1884 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001885 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001886 frame_button->state = FRAME_BUTTON_ACTIVE;
1887 widget_schedule_redraw(frame_button->widget);
1888
1889 if (frame_button->type == FRAME_BUTTON_ICON)
1890 window_show_frame_menu(window, input, time);
1891 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001892 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001893 frame_button->state = FRAME_BUTTON_DEFAULT;
1894 widget_schedule_redraw(frame_button->widget);
1895 break;
1896 }
1897
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001898 if (!was_pressed)
1899 return;
1900
Martin Minarik1998b152012-05-10 02:04:35 +02001901 switch (frame_button->type) {
1902 case FRAME_BUTTON_CLOSE:
1903 if (window->close_handler)
1904 window->close_handler(window->parent,
1905 window->user_data);
1906 else
1907 display_exit(window->display);
1908 break;
1909 case FRAME_BUTTON_MINIMIZE:
1910 fprintf(stderr,"Minimize stub\n");
1911 break;
1912 case FRAME_BUTTON_MAXIMIZE:
1913 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1914 break;
1915 default:
1916 /* Unknown operation */
1917 break;
1918 }
1919}
1920
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001921static int
1922frame_button_motion_handler(struct widget *widget,
1923 struct input *input, uint32_t time,
1924 float x, float y, void *data)
1925{
1926 struct frame_button *frame_button = data;
1927 enum frame_button_pointer previous_button_state = frame_button->state;
1928
1929 /* only track state for a pressed button */
1930 if (input->grab != widget)
1931 return CURSOR_LEFT_PTR;
1932
1933 if (x > widget->allocation.x &&
1934 x < (widget->allocation.x + widget->allocation.width) &&
1935 y > widget->allocation.y &&
1936 y < (widget->allocation.y + widget->allocation.height)) {
1937 frame_button->state = FRAME_BUTTON_ACTIVE;
1938 } else {
1939 frame_button->state = FRAME_BUTTON_DEFAULT;
1940 }
1941
1942 if (frame_button->state != previous_button_state)
1943 widget_schedule_redraw(frame_button->widget);
1944
1945 return CURSOR_LEFT_PTR;
1946}
1947
Martin Minarik1998b152012-05-10 02:04:35 +02001948static void
1949frame_button_redraw_handler(struct widget *widget, void *data)
1950{
1951 struct frame_button *frame_button = data;
1952 cairo_t *cr;
1953 int width, height, x, y;
1954 struct window *window = widget->window;
1955
1956 x = widget->allocation.x;
1957 y = widget->allocation.y;
1958 width = widget->allocation.width;
1959 height = widget->allocation.height;
1960
1961 if (!width)
1962 return;
1963 if (!height)
1964 return;
1965 if (widget->opaque)
1966 return;
1967
1968 cr = cairo_create(window->cairo_surface);
1969
1970 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1971 cairo_set_line_width(cr, 1);
1972
1973 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1974 cairo_rectangle (cr, x, y, 25, 16);
1975
1976 cairo_stroke_preserve(cr);
1977
1978 switch (frame_button->state) {
1979 case FRAME_BUTTON_DEFAULT:
1980 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1981 break;
1982 case FRAME_BUTTON_OVER:
1983 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1984 break;
1985 case FRAME_BUTTON_ACTIVE:
1986 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1987 break;
1988 }
1989
1990 cairo_fill (cr);
1991
1992 x += 4;
1993 }
1994
1995 cairo_set_source_surface(cr, frame_button->icon, x, y);
1996 cairo_paint(cr);
1997
1998 cairo_destroy(cr);
1999}
2000
2001static struct widget *
2002frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2003 enum frame_button_align align, enum frame_button_decoration style)
2004{
2005 struct frame_button *frame_button;
2006 const char *icon = data;
2007
2008 frame_button = malloc (sizeof *frame_button);
2009 memset(frame_button, 0, sizeof *frame_button);
2010
2011 frame_button->icon = cairo_image_surface_create_from_png(icon);
2012 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2013 frame_button->frame = frame;
2014 frame_button->type = type;
2015 frame_button->align = align;
2016 frame_button->decoration = style;
2017
2018 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2019
2020 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2021 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2022 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2023 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002024 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002025 return frame_button->widget;
2026}
2027
2028static void
2029frame_button_destroy(struct frame_button *frame_button)
2030{
2031 widget_destroy(frame_button->widget);
2032 wl_list_remove(&frame_button->link);
2033 cairo_surface_destroy(frame_button->icon);
2034 free(frame_button);
2035
2036 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002037}
2038
2039static void
2040frame_redraw_handler(struct widget *widget, void *data)
2041{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002042 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002043 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002044 struct theme *t = window->display->theme;
2045 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002046
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002047 if (window->type == TYPE_FULLSCREEN)
2048 return;
2049
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002050 cr = cairo_create(window->cairo_surface);
2051
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002052 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002053 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002054 if (window->type == TYPE_MAXIMIZED)
2055 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002056 theme_render_frame(t, cr, widget->allocation.width,
2057 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002058
2059 cairo_destroy(cr);
2060}
2061
2062static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002063frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002064{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002065 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002066 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002067 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002068
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002069 location = theme_get_location(t, input->sx, input->sy,
2070 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002071 frame->widget->allocation.height,
2072 window->type == TYPE_MAXIMIZED ?
2073 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002074
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002075 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002076 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002077 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002078 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002079 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002080 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002081 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002082 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002083 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002084 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002085 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002086 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002087 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002088 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002089 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002090 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002091 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002092 case THEME_LOCATION_EXTERIOR:
2093 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002094 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002095 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002096 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002097}
2098
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002099static void
2100frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002101{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002102 struct display *display;
2103
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002104 switch (index) {
2105 case 0: /* close */
2106 if (window->close_handler)
2107 window->close_handler(window->parent,
2108 window->user_data);
2109 else
2110 display_exit(window->display);
2111 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002112 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002113 display = window->display;
2114 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002115 workspace_manager_move_surface(
2116 display->workspace_manager,
2117 window->main_surface->surface,
2118 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002119 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002120 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002121 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002122 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002123 workspace_manager_move_surface(
2124 display->workspace_manager,
2125 window->main_surface->surface,
2126 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002127 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002128 case 3: /* fullscreen */
2129 /* we don't have a way to get out of fullscreen for now */
2130 if (window->fullscreen_handler)
2131 window->fullscreen_handler(window, window->user_data);
2132 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002133 }
2134}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002135
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002136void
2137window_show_frame_menu(struct window *window,
2138 struct input *input, uint32_t time)
2139{
2140 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002141 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002142
2143 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002144 "Close",
2145 "Move to workspace above", "Move to workspace below",
2146 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002147 };
2148
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002149 if (window->fullscreen_handler)
2150 count = ARRAY_LENGTH(entries);
2151 else
2152 count = ARRAY_LENGTH(entries) - 1;
2153
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002154 input_get_position(input, &x, &y);
2155 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002156 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002157}
2158
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159static int
2160frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002161 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162{
2163 return frame_get_pointer_image_for_location(data, input);
2164}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002165
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002166static int
2167frame_motion_handler(struct widget *widget,
2168 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002169 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002170{
2171 return frame_get_pointer_image_for_location(data, input);
2172}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002173
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002175frame_button_handler(struct widget *widget,
2176 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002177 uint32_t button, enum wl_pointer_button_state state,
2178 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002179
2180{
2181 struct frame *frame = data;
2182 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002183 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002184 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002185
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002186 location = theme_get_location(display->theme, input->sx, input->sy,
2187 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002188 frame->widget->allocation.height,
2189 window->type == TYPE_MAXIMIZED ?
2190 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191
Daniel Stone4dbadb12012-05-30 16:31:51 +01002192 if (window->display->shell && button == BTN_LEFT &&
2193 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002195 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002196 if (!window->shell_surface)
2197 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002198 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002199 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002200 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002201 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002202 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002203 case THEME_LOCATION_RESIZING_TOP:
2204 case THEME_LOCATION_RESIZING_BOTTOM:
2205 case THEME_LOCATION_RESIZING_LEFT:
2206 case THEME_LOCATION_RESIZING_RIGHT:
2207 case THEME_LOCATION_RESIZING_TOP_LEFT:
2208 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2209 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2210 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211 if (!window->shell_surface)
2212 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002213 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002214
Pekka Paalanen99436862012-11-19 17:15:59 +02002215 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002216 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002217 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002218 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219 break;
2220 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002221 } else if (button == BTN_RIGHT &&
2222 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002223 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 }
2225}
2226
2227struct widget *
2228frame_create(struct window *window, void *data)
2229{
2230 struct frame *frame;
2231
2232 frame = malloc(sizeof *frame);
2233 memset(frame, 0, sizeof *frame);
2234
2235 frame->widget = window_add_widget(window, frame);
2236 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002237
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002238 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2239 widget_set_resize_handler(frame->widget, frame_resize_handler);
2240 widget_set_enter_handler(frame->widget, frame_enter_handler);
2241 widget_set_motion_handler(frame->widget, frame_motion_handler);
2242 widget_set_button_handler(frame->widget, frame_button_handler);
2243
Martin Minarik1998b152012-05-10 02:04:35 +02002244 /* Create empty list for frame buttons */
2245 wl_list_init(&frame->buttons_list);
2246
2247 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2248 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2249
2250 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2251 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2252
2253 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2254 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2255
2256 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2257 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2258
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002259 window->frame = frame;
2260
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002261 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262}
2263
Kristian Høgsberga1627922012-06-20 17:30:03 -04002264void
2265frame_set_child_size(struct widget *widget, int child_width, int child_height)
2266{
2267 struct display *display = widget->window->display;
2268 struct theme *t = display->theme;
2269 int decoration_width, decoration_height;
2270 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002271 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002272
2273 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002274 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002275 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002276 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002277
2278 width = child_width + decoration_width;
2279 height = child_height + decoration_height;
2280 } else {
2281 width = child_width;
2282 height = child_height;
2283 }
2284
2285 window_schedule_resize(widget->window, width, height);
2286}
2287
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002288static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002289frame_destroy(struct frame *frame)
2290{
Martin Minarik1998b152012-05-10 02:04:35 +02002291 struct frame_button *button, *tmp;
2292
2293 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2294 frame_button_destroy(button);
2295
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002296 /* frame->child must be destroyed by the application */
2297 widget_destroy(frame->widget);
2298 free(frame);
2299}
2300
2301static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002302input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002303 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002304{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002305 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002306 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002307
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002308 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002309 return;
2310
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002311 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002312 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002313 widget = old;
2314 if (input->grab)
2315 widget = input->grab;
2316 if (widget->leave_handler)
2317 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002318 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002319 }
2320
2321 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002322 widget = focus;
2323 if (input->grab)
2324 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002325 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002326 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002327 cursor = widget->enter_handler(focus, input, x, y,
2328 widget->user_data);
2329 else
2330 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002331
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002332 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002333 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002334}
2335
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002336void
2337input_grab(struct input *input, struct widget *widget, uint32_t button)
2338{
2339 input->grab = widget;
2340 input->grab_button = button;
2341}
2342
2343void
2344input_ungrab(struct input *input)
2345{
2346 struct widget *widget;
2347
2348 input->grab = NULL;
2349 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002350 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002351 input->sx, input->sy);
2352 input_set_focus_widget(input, widget, input->sx, input->sy);
2353 }
2354}
2355
2356static void
2357input_remove_pointer_focus(struct input *input)
2358{
2359 struct window *window = input->pointer_focus;
2360
2361 if (!window)
2362 return;
2363
2364 input_set_focus_widget(input, NULL, 0, 0);
2365
2366 input->pointer_focus = NULL;
2367 input->current_cursor = CURSOR_UNSET;
2368}
2369
2370static void
2371pointer_handle_enter(void *data, struct wl_pointer *pointer,
2372 uint32_t serial, struct wl_surface *surface,
2373 wl_fixed_t sx_w, wl_fixed_t sy_w)
2374{
2375 struct input *input = data;
2376 struct window *window;
2377 struct widget *widget;
2378 float sx = wl_fixed_to_double(sx_w);
2379 float sy = wl_fixed_to_double(sy_w);
2380
2381 if (!surface) {
2382 /* enter event for a window we've just destroyed */
2383 return;
2384 }
2385
2386 input->display->serial = serial;
2387 input->pointer_enter_serial = serial;
2388 input->pointer_focus = wl_surface_get_user_data(surface);
2389 window = input->pointer_focus;
2390
Pekka Paalanen99436862012-11-19 17:15:59 +02002391 if (window->resizing) {
2392 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002393 /* Schedule a redraw to free the pool */
2394 window_schedule_redraw(window);
2395 }
2396
2397 input->sx = sx;
2398 input->sy = sy;
2399
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002400 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002401 input_set_focus_widget(input, widget, sx, sy);
2402}
2403
2404static void
2405pointer_handle_leave(void *data, struct wl_pointer *pointer,
2406 uint32_t serial, struct wl_surface *surface)
2407{
2408 struct input *input = data;
2409
2410 input->display->serial = serial;
2411 input_remove_pointer_focus(input);
2412}
2413
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002414static void
Daniel Stone37816df2012-05-16 18:45:18 +01002415pointer_handle_motion(void *data, struct wl_pointer *pointer,
2416 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002417{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002418 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002419 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002420 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002421 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002422 float sx = wl_fixed_to_double(sx_w);
2423 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002424
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002425 input->sx = sx;
2426 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002427
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002428 if (!window)
2429 return;
2430
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002431 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002432 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002433 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002434 }
2435
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002436 if (input->grab)
2437 widget = input->grab;
2438 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002439 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002440 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002441 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002442 input, time, sx, sy,
2443 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002444 else
2445 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002446
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002447 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002448}
2449
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002450static void
Daniel Stone37816df2012-05-16 18:45:18 +01002451pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002452 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002453{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002454 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002455 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002456 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002457
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002458 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002459 if (input->focus_widget && input->grab == NULL &&
2460 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002461 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002462
Neil Roberts6b28aad2012-01-23 19:11:18 +00002463 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002464 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002465 (*widget->button_handler)(widget,
2466 input, time,
2467 button, state,
2468 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002469
Daniel Stone4dbadb12012-05-30 16:31:51 +01002470 if (input->grab && input->grab_button == button &&
2471 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002472 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002473}
2474
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002475static void
Daniel Stone37816df2012-05-16 18:45:18 +01002476pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002477 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002478{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002479 struct input *input = data;
2480 struct widget *widget;
2481
2482 widget = input->focus_widget;
2483 if (input->grab)
2484 widget = input->grab;
2485 if (widget && widget->axis_handler)
2486 (*widget->axis_handler)(widget,
2487 input, time,
2488 axis, value,
2489 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002490}
2491
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002492static const struct wl_pointer_listener pointer_listener = {
2493 pointer_handle_enter,
2494 pointer_handle_leave,
2495 pointer_handle_motion,
2496 pointer_handle_button,
2497 pointer_handle_axis,
2498};
2499
2500static void
2501input_remove_keyboard_focus(struct input *input)
2502{
2503 struct window *window = input->keyboard_focus;
2504 struct itimerspec its;
2505
2506 its.it_interval.tv_sec = 0;
2507 its.it_interval.tv_nsec = 0;
2508 its.it_value.tv_sec = 0;
2509 its.it_value.tv_nsec = 0;
2510 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2511
2512 if (!window)
2513 return;
2514
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002515 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002516 if (window->keyboard_focus_handler)
2517 (*window->keyboard_focus_handler)(window, NULL,
2518 window->user_data);
2519
2520 input->keyboard_focus = NULL;
2521}
2522
2523static void
2524keyboard_repeat_func(struct task *task, uint32_t events)
2525{
2526 struct input *input =
2527 container_of(task, struct input, repeat_task);
2528 struct window *window = input->keyboard_focus;
2529 uint64_t exp;
2530
2531 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2532 /* If we change the timer between the fd becoming
2533 * readable and getting here, there'll be nothing to
2534 * read and we get EAGAIN. */
2535 return;
2536
2537 if (window && window->key_handler) {
2538 (*window->key_handler)(window, input, input->repeat_time,
2539 input->repeat_key, input->repeat_sym,
2540 WL_KEYBOARD_KEY_STATE_PRESSED,
2541 window->user_data);
2542 }
2543}
2544
2545static void
2546keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2547 uint32_t format, int fd, uint32_t size)
2548{
2549 struct input *input = data;
2550 char *map_str;
2551
2552 if (!data) {
2553 close(fd);
2554 return;
2555 }
2556
2557 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2558 close(fd);
2559 return;
2560 }
2561
2562 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2563 if (map_str == MAP_FAILED) {
2564 close(fd);
2565 return;
2566 }
2567
2568 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2569 map_str,
2570 XKB_KEYMAP_FORMAT_TEXT_V1,
2571 0);
2572 munmap(map_str, size);
2573 close(fd);
2574
2575 if (!input->xkb.keymap) {
2576 fprintf(stderr, "failed to compile keymap\n");
2577 return;
2578 }
2579
2580 input->xkb.state = xkb_state_new(input->xkb.keymap);
2581 if (!input->xkb.state) {
2582 fprintf(stderr, "failed to create XKB state\n");
2583 xkb_map_unref(input->xkb.keymap);
2584 input->xkb.keymap = NULL;
2585 return;
2586 }
2587
2588 input->xkb.control_mask =
2589 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2590 input->xkb.alt_mask =
2591 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2592 input->xkb.shift_mask =
2593 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2594}
2595
2596static void
2597keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2598 uint32_t serial, struct wl_surface *surface,
2599 struct wl_array *keys)
2600{
2601 struct input *input = data;
2602 struct window *window;
2603
2604 input->display->serial = serial;
2605 input->keyboard_focus = wl_surface_get_user_data(surface);
2606
2607 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002608 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002609 if (window->keyboard_focus_handler)
2610 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002611 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002612}
2613
2614static void
2615keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2616 uint32_t serial, struct wl_surface *surface)
2617{
2618 struct input *input = data;
2619
2620 input->display->serial = serial;
2621 input_remove_keyboard_focus(input);
2622}
2623
Scott Moreau210d0792012-03-22 10:47:01 -06002624static void
Daniel Stone37816df2012-05-16 18:45:18 +01002625keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002626 uint32_t serial, uint32_t time, uint32_t key,
2627 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002628{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002629 struct input *input = data;
2630 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002631 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002632 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002633 const xkb_keysym_t *syms;
2634 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002635 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002636
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002637 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002638 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002639 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002640 return;
2641
Daniel Stone97f68542012-05-30 16:32:01 +01002642 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002643
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002644 sym = XKB_KEY_NoSymbol;
2645 if (num_syms == 1)
2646 sym = syms[0];
2647
2648 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002649 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002650 window_set_maximized(window,
2651 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002652 } else if (sym == XKB_KEY_F11 &&
2653 window->fullscreen_handler &&
2654 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2655 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002656 } else if (sym == XKB_KEY_F4 &&
2657 input->modifiers == MOD_ALT_MASK &&
2658 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2659 if (window->close_handler)
2660 window->close_handler(window->parent,
2661 window->user_data);
2662 else
2663 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002664 } else if (window->key_handler) {
2665 (*window->key_handler)(window, input, time, key,
2666 sym, state, window->user_data);
2667 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002668
2669 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2670 key == input->repeat_key) {
2671 its.it_interval.tv_sec = 0;
2672 its.it_interval.tv_nsec = 0;
2673 its.it_value.tv_sec = 0;
2674 its.it_value.tv_nsec = 0;
2675 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2676 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2677 input->repeat_sym = sym;
2678 input->repeat_key = key;
2679 input->repeat_time = time;
2680 its.it_interval.tv_sec = 0;
2681 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2682 its.it_value.tv_sec = 0;
2683 its.it_value.tv_nsec = 400 * 1000 * 1000;
2684 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2685 }
2686}
2687
2688static void
Daniel Stone351eb612012-05-31 15:27:47 -04002689keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2690 uint32_t serial, uint32_t mods_depressed,
2691 uint32_t mods_latched, uint32_t mods_locked,
2692 uint32_t group)
2693{
2694 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002695 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002696
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002697 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2698 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002699 mask = xkb_state_serialize_mods(input->xkb.state,
2700 XKB_STATE_DEPRESSED |
2701 XKB_STATE_LATCHED);
2702 input->modifiers = 0;
2703 if (mask & input->xkb.control_mask)
2704 input->modifiers |= MOD_CONTROL_MASK;
2705 if (mask & input->xkb.alt_mask)
2706 input->modifiers |= MOD_ALT_MASK;
2707 if (mask & input->xkb.shift_mask)
2708 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002709}
2710
Daniel Stone37816df2012-05-16 18:45:18 +01002711static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002712 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002713 keyboard_handle_enter,
2714 keyboard_handle_leave,
2715 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002716 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002717};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002718
2719static void
Daniel Stone37816df2012-05-16 18:45:18 +01002720seat_handle_capabilities(void *data, struct wl_seat *seat,
2721 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002722{
Daniel Stone37816df2012-05-16 18:45:18 +01002723 struct input *input = data;
2724
2725 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2726 input->pointer = wl_seat_get_pointer(seat);
2727 wl_pointer_set_user_data(input->pointer, input);
2728 wl_pointer_add_listener(input->pointer, &pointer_listener,
2729 input);
2730 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2731 wl_pointer_destroy(input->pointer);
2732 input->pointer = NULL;
2733 }
2734
2735 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2736 input->keyboard = wl_seat_get_keyboard(seat);
2737 wl_keyboard_set_user_data(input->keyboard, input);
2738 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2739 input);
2740 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2741 wl_keyboard_destroy(input->keyboard);
2742 input->keyboard = NULL;
2743 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002744}
2745
Daniel Stone37816df2012-05-16 18:45:18 +01002746static const struct wl_seat_listener seat_listener = {
2747 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002748};
2749
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002750void
2751input_get_position(struct input *input, int32_t *x, int32_t *y)
2752{
2753 *x = input->sx;
2754 *y = input->sy;
2755}
2756
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002757struct display *
2758input_get_display(struct input *input)
2759{
2760 return input->display;
2761}
2762
Daniel Stone37816df2012-05-16 18:45:18 +01002763struct wl_seat *
2764input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002765{
Daniel Stone37816df2012-05-16 18:45:18 +01002766 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002767}
2768
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002769uint32_t
2770input_get_modifiers(struct input *input)
2771{
2772 return input->modifiers;
2773}
2774
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002775struct widget *
2776input_get_focus_widget(struct input *input)
2777{
2778 return input->focus_widget;
2779}
2780
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002781struct data_offer {
2782 struct wl_data_offer *offer;
2783 struct input *input;
2784 struct wl_array types;
2785 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002786
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002787 struct task io_task;
2788 int fd;
2789 data_func_t func;
2790 int32_t x, y;
2791 void *user_data;
2792};
2793
2794static void
2795data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2796{
2797 struct data_offer *offer = data;
2798 char **p;
2799
2800 p = wl_array_add(&offer->types, sizeof *p);
2801 *p = strdup(type);
2802}
2803
2804static const struct wl_data_offer_listener data_offer_listener = {
2805 data_offer_offer,
2806};
2807
2808static void
2809data_offer_destroy(struct data_offer *offer)
2810{
2811 char **p;
2812
2813 offer->refcount--;
2814 if (offer->refcount == 0) {
2815 wl_data_offer_destroy(offer->offer);
2816 for (p = offer->types.data; *p; p++)
2817 free(*p);
2818 wl_array_release(&offer->types);
2819 free(offer);
2820 }
2821}
2822
2823static void
2824data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002825 struct wl_data_device *data_device,
2826 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002827{
2828 struct data_offer *offer;
2829
2830 offer = malloc(sizeof *offer);
2831
2832 wl_array_init(&offer->types);
2833 offer->refcount = 1;
2834 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002835 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002836 wl_data_offer_add_listener(offer->offer,
2837 &data_offer_listener, offer);
2838}
2839
2840static void
2841data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002842 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002843 wl_fixed_t x_w, wl_fixed_t y_w,
2844 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002845{
2846 struct input *input = data;
2847 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002848 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002849 float x = wl_fixed_to_double(x_w);
2850 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002851 char **p;
2852
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002853 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002854 window = wl_surface_get_user_data(surface);
2855 input->pointer_focus = window;
2856
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002857 if (offer) {
2858 input->drag_offer = wl_data_offer_get_user_data(offer);
2859
2860 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2861 *p = NULL;
2862
2863 types_data = input->drag_offer->types.data;
2864 } else {
2865 input->drag_offer = NULL;
2866 types_data = NULL;
2867 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002868
2869 window = input->pointer_focus;
2870 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002871 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002872 window->user_data);
2873}
2874
2875static void
2876data_device_leave(void *data, struct wl_data_device *data_device)
2877{
2878 struct input *input = data;
2879
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002880 if (input->drag_offer) {
2881 data_offer_destroy(input->drag_offer);
2882 input->drag_offer = NULL;
2883 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002884}
2885
2886static void
2887data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002888 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002889{
2890 struct input *input = data;
2891 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002892 float x = wl_fixed_to_double(x_w);
2893 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002894 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002895
2896 input->sx = x;
2897 input->sy = y;
2898
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002899 if (input->drag_offer)
2900 types_data = input->drag_offer->types.data;
2901 else
2902 types_data = NULL;
2903
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002904 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002905 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002906 window->user_data);
2907}
2908
2909static void
2910data_device_drop(void *data, struct wl_data_device *data_device)
2911{
2912 struct input *input = data;
2913 struct window *window = input->pointer_focus;
2914
2915 if (window->drop_handler)
2916 window->drop_handler(window, input,
2917 input->sx, input->sy, window->user_data);
2918}
2919
2920static void
2921data_device_selection(void *data,
2922 struct wl_data_device *wl_data_device,
2923 struct wl_data_offer *offer)
2924{
2925 struct input *input = data;
2926 char **p;
2927
2928 if (input->selection_offer)
2929 data_offer_destroy(input->selection_offer);
2930
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002931 if (offer) {
2932 input->selection_offer = wl_data_offer_get_user_data(offer);
2933 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2934 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002935 } else {
2936 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002937 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002938}
2939
2940static const struct wl_data_device_listener data_device_listener = {
2941 data_device_data_offer,
2942 data_device_enter,
2943 data_device_leave,
2944 data_device_motion,
2945 data_device_drop,
2946 data_device_selection
2947};
2948
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002949static void
2950input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002951{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002952 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002953 struct wl_cursor *cursor;
2954 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002955
Daniel Stone80972742012-11-07 17:51:39 +11002956 if (!input->pointer)
2957 return;
2958
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002959 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002960 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002961 return;
2962
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002963 if (index >= (int) cursor->image_count) {
2964 fprintf(stderr, "cursor index out of range\n");
2965 return;
2966 }
2967
2968 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002969 buffer = wl_cursor_image_get_buffer(image);
2970 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002971 return;
2972
Kristian Høgsbergae277372012-08-01 09:41:08 -04002973 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002974 input->pointer_surface,
2975 image->hotspot_x, image->hotspot_y);
2976 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2977 wl_surface_damage(input->pointer_surface, 0, 0,
2978 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002979 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002980}
2981
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002982static const struct wl_callback_listener pointer_surface_listener;
2983
2984static void
2985pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2986 uint32_t time)
2987{
2988 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002989 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002990 int i;
2991
2992 if (callback) {
2993 assert(callback == input->cursor_frame_cb);
2994 wl_callback_destroy(callback);
2995 input->cursor_frame_cb = NULL;
2996 }
2997
Daniel Stone80972742012-11-07 17:51:39 +11002998 if (!input->pointer)
2999 return;
3000
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003001 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003002 wl_pointer_set_cursor(input->pointer,
3003 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003004 NULL, 0, 0);
3005 return;
3006 }
3007
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003008 if (input->current_cursor == CURSOR_UNSET)
3009 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003010 cursor = input->display->cursors[input->current_cursor];
3011 if (!cursor)
3012 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003013
3014 /* FIXME We don't have the current time on the first call so we set
3015 * the animation start to the time of the first frame callback. */
3016 if (time == 0)
3017 input->cursor_anim_start = 0;
3018 else if (input->cursor_anim_start == 0)
3019 input->cursor_anim_start = time;
3020
3021 if (time == 0 || input->cursor_anim_start == 0)
3022 i = 0;
3023 else
3024 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3025
Pekka Paalanenbc106382012-10-10 12:49:31 +03003026 if (cursor->image_count > 1) {
3027 input->cursor_frame_cb =
3028 wl_surface_frame(input->pointer_surface);
3029 wl_callback_add_listener(input->cursor_frame_cb,
3030 &pointer_surface_listener, input);
3031 }
3032
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003033 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003034}
3035
3036static const struct wl_callback_listener pointer_surface_listener = {
3037 pointer_surface_frame_callback
3038};
3039
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003040void
3041input_set_pointer_image(struct input *input, int pointer)
3042{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003043 int force = 0;
3044
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003045 if (!input->pointer)
3046 return;
3047
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003048 if (input->pointer_enter_serial > input->cursor_serial)
3049 force = 1;
3050
3051 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003052 return;
3053
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003054 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003055 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003056 if (!input->cursor_frame_cb)
3057 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003058 else if (force) {
3059 /* The current frame callback may be stuck if, for instance,
3060 * the set cursor request was processed by the server after
3061 * this client lost the focus. In this case the cursor surface
3062 * might not be mapped and the frame callback wouldn't ever
3063 * complete. Send a set_cursor and attach to try to map the
3064 * cursor surface again so that the callback will finish */
3065 input_set_pointer_image_index(input, 0);
3066 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003067}
3068
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003069struct wl_data_device *
3070input_get_data_device(struct input *input)
3071{
3072 return input->data_device;
3073}
3074
3075void
3076input_set_selection(struct input *input,
3077 struct wl_data_source *source, uint32_t time)
3078{
3079 wl_data_device_set_selection(input->data_device, source, time);
3080}
3081
3082void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003083input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003084{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003085 wl_data_offer_accept(input->drag_offer->offer,
3086 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003087}
3088
3089static void
3090offer_io_func(struct task *task, uint32_t events)
3091{
3092 struct data_offer *offer =
3093 container_of(task, struct data_offer, io_task);
3094 unsigned int len;
3095 char buffer[4096];
3096
3097 len = read(offer->fd, buffer, sizeof buffer);
3098 offer->func(buffer, len,
3099 offer->x, offer->y, offer->user_data);
3100
3101 if (len == 0) {
3102 close(offer->fd);
3103 data_offer_destroy(offer);
3104 }
3105}
3106
3107static void
3108data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3109 data_func_t func, void *user_data)
3110{
3111 int p[2];
3112
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003113 if (pipe2(p, O_CLOEXEC) == -1)
3114 return;
3115
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003116 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3117 close(p[1]);
3118
3119 offer->io_task.run = offer_io_func;
3120 offer->fd = p[0];
3121 offer->func = func;
3122 offer->refcount++;
3123 offer->user_data = user_data;
3124
3125 display_watch_fd(offer->input->display,
3126 offer->fd, EPOLLIN, &offer->io_task);
3127}
3128
3129void
3130input_receive_drag_data(struct input *input, const char *mime_type,
3131 data_func_t func, void *data)
3132{
3133 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3134 input->drag_offer->x = input->sx;
3135 input->drag_offer->y = input->sy;
3136}
3137
3138int
3139input_receive_selection_data(struct input *input, const char *mime_type,
3140 data_func_t func, void *data)
3141{
3142 char **p;
3143
3144 if (input->selection_offer == NULL)
3145 return -1;
3146
3147 for (p = input->selection_offer->types.data; *p; p++)
3148 if (strcmp(mime_type, *p) == 0)
3149 break;
3150
3151 if (*p == NULL)
3152 return -1;
3153
3154 data_offer_receive_data(input->selection_offer,
3155 mime_type, func, data);
3156 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003157}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003158
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003159int
3160input_receive_selection_data_to_fd(struct input *input,
3161 const char *mime_type, int fd)
3162{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003163 if (input->selection_offer)
3164 wl_data_offer_receive(input->selection_offer->offer,
3165 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003166
3167 return 0;
3168}
3169
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003170void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003171window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003172{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003173 if (!window->shell_surface)
3174 return;
3175
Daniel Stone37816df2012-05-16 18:45:18 +01003176 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003177}
3178
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003179static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003180surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003181{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003182 struct widget *widget = surface->widget;
3183 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003184
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003185 if (surface->input_region) {
3186 wl_region_destroy(surface->input_region);
3187 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003188 }
3189
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003190 if (surface->opaque_region)
3191 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003192
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003193 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003194
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003195 if (widget->resize_handler)
3196 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003197 widget->allocation.width,
3198 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003199 widget->user_data);
3200
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003201 if (surface->allocation.width != widget->allocation.width ||
3202 surface->allocation.height != widget->allocation.height) {
3203 surface->allocation = widget->allocation;
3204 window_schedule_redraw(widget->window);
3205 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003206
3207 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003208 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003209 widget->allocation.width,
3210 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003211}
3212
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003213static void
3214idle_resize(struct window *window)
3215{
3216 window->resize_needed = 0;
3217
3218 widget_set_allocation(window->main_surface->widget,
3219 window->pending_allocation.x,
3220 window->pending_allocation.y,
3221 window->pending_allocation.width,
3222 window->pending_allocation.height);
3223
3224 surface_resize(window->main_surface);
3225}
3226
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003227void
3228window_schedule_resize(struct window *window, int width, int height)
3229{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003230 window->pending_allocation.x = 0;
3231 window->pending_allocation.y = 0;
3232 window->pending_allocation.width = width;
3233 window->pending_allocation.height = height;
3234
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003235 if (window->min_allocation.width == 0)
3236 window->min_allocation = window->pending_allocation;
3237 if (window->pending_allocation.width < window->min_allocation.width)
3238 window->pending_allocation.width = window->min_allocation.width;
3239 if (window->pending_allocation.height < window->min_allocation.height)
3240 window->pending_allocation.height = window->min_allocation.height;
3241
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003242 window->resize_needed = 1;
3243 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003244}
3245
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003246void
3247widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3248{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003249 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003250}
3251
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003252static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003253handle_ping(void *data, struct wl_shell_surface *shell_surface,
3254 uint32_t serial)
3255{
3256 wl_shell_surface_pong(shell_surface, serial);
3257}
3258
3259static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003260handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003261 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003262{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003263 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003264
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003265 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003266 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003267}
3268
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003269static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003270menu_destroy(struct menu *menu)
3271{
3272 widget_destroy(menu->widget);
3273 window_destroy(menu->window);
3274 free(menu);
3275}
3276
3277static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003278handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3279{
3280 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003281 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003282
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003283 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003284 * device. Or just use wl_callback. And this really needs to
3285 * be a window vfunc that the menu can set. And we need the
3286 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003287
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003288 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003289 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003290 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003291}
3292
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003293static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003294 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003295 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003296 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003297};
3298
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003299void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003300window_get_allocation(struct window *window,
3301 struct rectangle *allocation)
3302{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003303 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003304}
3305
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003306static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003307widget_redraw(struct widget *widget)
3308{
3309 struct widget *child;
3310
3311 if (widget->redraw_handler)
3312 widget->redraw_handler(widget, widget->user_data);
3313 wl_list_for_each(child, &widget->child_list, link)
3314 widget_redraw(child);
3315}
3316
3317static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003318frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3319{
3320 struct window *window = data;
3321
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003322 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003323 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003324 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003325 window->redraw_scheduled = 0;
3326 if (window->redraw_needed)
3327 window_schedule_redraw(window);
3328}
3329
3330static const struct wl_callback_listener listener = {
3331 frame_callback
3332};
3333
3334static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003335idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003336{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003337 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003338
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003339 if (window->resize_needed)
3340 idle_resize(window);
3341
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003342 window_create_surface(window);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003343 widget_redraw(window->main_surface->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003344 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003345 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003346
Pekka Paalanen4e373742013-02-13 16:17:13 +02003347 window->frame_cb = wl_surface_frame(window->main_surface->surface);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003348 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003349 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003350}
3351
3352void
3353window_schedule_redraw(struct window *window)
3354{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003355 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003356 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003357 window->redraw_task.run = idle_redraw;
3358 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003359 window->redraw_scheduled = 1;
3360 }
3361}
3362
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003363int
3364window_is_fullscreen(struct window *window)
3365{
3366 return window->type == TYPE_FULLSCREEN;
3367}
3368
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003369void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003370window_set_fullscreen(struct window *window, int fullscreen)
3371{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003372 if (!window->display->shell)
3373 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003374
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003375 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003376 return;
3377
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003378 if (fullscreen) {
3379 window->type = TYPE_FULLSCREEN;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003380 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003381 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003382 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003383 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003384 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003385 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003386 wl_shell_surface_set_toplevel(window->shell_surface);
3387 window_schedule_resize(window,
3388 window->saved_allocation.width,
3389 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003390 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003391}
3392
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003393void
3394window_set_fullscreen_method(struct window *window,
3395 enum wl_shell_surface_fullscreen_method method)
3396{
3397 window->fullscreen_method = method;
3398}
3399
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003400int
3401window_is_maximized(struct window *window)
3402{
3403 return window->type == TYPE_MAXIMIZED;
3404}
3405
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003406void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003407window_set_maximized(struct window *window, int maximized)
3408{
3409 if (!window->display->shell)
3410 return;
3411
3412 if ((window->type == TYPE_MAXIMIZED) == maximized)
3413 return;
3414
3415 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003416 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003417 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3418 window->type = TYPE_MAXIMIZED;
3419 } else {
3420 wl_shell_surface_set_toplevel(window->shell_surface);
3421 window->type = TYPE_TOPLEVEL;
3422 window_schedule_resize(window,
3423 window->saved_allocation.width,
3424 window->saved_allocation.height);
3425 }
3426}
3427
3428void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003429window_set_user_data(struct window *window, void *data)
3430{
3431 window->user_data = data;
3432}
3433
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003434void *
3435window_get_user_data(struct window *window)
3436{
3437 return window->user_data;
3438}
3439
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003440void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003441window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003442 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003443{
3444 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003445}
3446
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003447void
3448window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003449 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003450{
3451 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003452}
3453
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003454void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003455window_set_data_handler(struct window *window, window_data_handler_t handler)
3456{
3457 window->data_handler = handler;
3458}
3459
3460void
3461window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3462{
3463 window->drop_handler = handler;
3464}
3465
3466void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003467window_set_close_handler(struct window *window,
3468 window_close_handler_t handler)
3469{
3470 window->close_handler = handler;
3471}
3472
3473void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003474window_set_fullscreen_handler(struct window *window,
3475 window_fullscreen_handler_t handler)
3476{
3477 window->fullscreen_handler = handler;
3478}
3479
3480void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003481window_set_output_handler(struct window *window,
3482 window_output_handler_t handler)
3483{
3484 window->output_handler = handler;
3485}
3486
3487void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003488window_set_title(struct window *window, const char *title)
3489{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003490 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003491 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003492 if (window->shell_surface)
3493 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003494}
3495
3496const char *
3497window_get_title(struct window *window)
3498{
3499 return window->title;
3500}
3501
3502void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003503window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3504{
3505 struct text_cursor_position *text_cursor_position =
3506 window->display->text_cursor_position;
3507
Scott Moreau9295ce02012-06-01 12:46:10 -06003508 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003509 return;
3510
3511 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003512 window->main_surface->surface,
3513 wl_fixed_from_int(x),
3514 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003515}
3516
3517void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003518window_damage(struct window *window, int32_t x, int32_t y,
3519 int32_t width, int32_t height)
3520{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003521 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003522}
3523
Casey Dahlin9074db52012-04-19 22:50:09 -04003524static void
3525surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003526 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003527{
Rob Bradford7507b572012-05-15 17:55:34 +01003528 struct window *window = data;
3529 struct output *output;
3530 struct output *output_found = NULL;
3531 struct window_output *window_output;
3532
3533 wl_list_for_each(output, &window->display->output_list, link) {
3534 if (output->output == wl_output) {
3535 output_found = output;
3536 break;
3537 }
3538 }
3539
3540 if (!output_found)
3541 return;
3542
3543 window_output = malloc (sizeof *window_output);
3544 window_output->output = output_found;
3545
3546 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003547
3548 if (window->output_handler)
3549 window->output_handler(window, output_found, 1,
3550 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003551}
3552
3553static void
3554surface_leave(void *data,
3555 struct wl_surface *wl_surface, struct wl_output *output)
3556{
Rob Bradford7507b572012-05-15 17:55:34 +01003557 struct window *window = data;
3558 struct window_output *window_output;
3559 struct window_output *window_output_found = NULL;
3560
3561 wl_list_for_each(window_output, &window->window_output_list, link) {
3562 if (window_output->output->output == output) {
3563 window_output_found = window_output;
3564 break;
3565 }
3566 }
3567
3568 if (window_output_found) {
3569 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003570
3571 if (window->output_handler)
3572 window->output_handler(window, window_output->output,
3573 0, window->user_data);
3574
Rob Bradford7507b572012-05-15 17:55:34 +01003575 free(window_output_found);
3576 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003577}
3578
3579static const struct wl_surface_listener surface_listener = {
3580 surface_enter,
3581 surface_leave
3582};
3583
Pekka Paalanen4e373742013-02-13 16:17:13 +02003584static struct surface *
3585surface_create(struct window *window)
3586{
3587 struct display *display = window->display;
3588 struct surface *surface;
3589
3590 surface = calloc(1, sizeof *surface);
3591 if (!surface)
3592 return NULL;
3593
3594 surface->window = window;
3595 surface->surface = wl_compositor_create_surface(display->compositor);
3596 wl_surface_add_listener(surface->surface, &surface_listener, window);
3597
3598 return surface;
3599}
3600
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003601static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003602window_create_internal(struct display *display,
3603 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003604{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003605 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003606 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003607
3608 window = malloc(sizeof *window);
3609 if (window == NULL)
3610 return NULL;
3611
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003612 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003613 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003614 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003615
3616 surface = surface_create(window);
3617 window->main_surface = surface;
3618
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003619 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003620 window->shell_surface =
3621 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003622 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003623 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003624
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003625 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003626 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003627 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003628
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003629 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003630#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003631 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003632#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003633 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003634#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003635 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003636 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003637
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003638 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003639 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003640 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003641
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003642 if (window->shell_surface) {
3643 wl_shell_surface_set_user_data(window->shell_surface, window);
3644 wl_shell_surface_add_listener(window->shell_surface,
3645 &shell_surface_listener, window);
3646 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003647
Rob Bradford7507b572012-05-15 17:55:34 +01003648 wl_list_init (&window->window_output_list);
3649
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003650 return window;
3651}
3652
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003653struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003654window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003655{
3656 struct window *window;
3657
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003658 window = window_create_internal(display, NULL, TYPE_NONE);
3659 if (!window)
3660 return NULL;
3661
3662 return window;
3663}
3664
3665struct window *
3666window_create_custom(struct display *display)
3667{
3668 struct window *window;
3669
3670 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003671 if (!window)
3672 return NULL;
3673
3674 return window;
3675}
3676
3677struct window *
3678window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003679 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003680{
3681 struct window *window;
3682
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003683 window = window_create_internal(parent->display,
3684 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003685 if (!window)
3686 return NULL;
3687
3688 window->x = x;
3689 window->y = y;
3690
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003691 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003692 wl_shell_surface_set_transient(
3693 window->shell_surface,
3694 window->parent->main_surface->surface,
3695 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003696
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003697 return window;
3698}
3699
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003700static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003701menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003702{
3703 int next;
3704
3705 next = (sy - 8) / 20;
3706 if (menu->current != next) {
3707 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003708 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003709 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003710}
3711
3712static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003713menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003714 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003715 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003716{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003717 struct menu *menu = data;
3718
3719 if (widget == menu->widget)
3720 menu_set_item(data, y);
3721
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003722 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003723}
3724
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003725static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003726menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003727 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003728{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003729 struct menu *menu = data;
3730
3731 if (widget == menu->widget)
3732 menu_set_item(data, y);
3733
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003734 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003735}
3736
3737static void
3738menu_leave_handler(struct widget *widget, struct input *input, void *data)
3739{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003740 struct menu *menu = data;
3741
3742 if (widget == menu->widget)
3743 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003744}
3745
3746static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003747menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003748 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003749 uint32_t button, enum wl_pointer_button_state state,
3750 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003751
3752{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003753 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003754
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003755 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3756 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003757 /* Either relase after press-drag-release or
3758 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003759 menu->func(menu->window->parent,
3760 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003761 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003762 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003763 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003764 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003765 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003766}
3767
3768static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003769menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003770{
3771 cairo_t *cr;
3772 const int32_t r = 3, margin = 3;
3773 struct menu *menu = data;
3774 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003775 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003776
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003777 cr = cairo_create(window->cairo_surface);
3778 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3779 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3780 cairo_paint(cr);
3781
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003782 width = window->main_surface->allocation.width;
3783 height = window->main_surface->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003784 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003785
3786 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003787 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3788 cairo_fill(cr);
3789
3790 for (i = 0; i < menu->count; i++) {
3791 if (i == menu->current) {
3792 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3793 cairo_rectangle(cr, margin, i * 20 + margin,
3794 width - 2 * margin, 20);
3795 cairo_fill(cr);
3796 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3797 cairo_move_to(cr, 10, i * 20 + 16);
3798 cairo_show_text(cr, menu->entries[i]);
3799 } else {
3800 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3801 cairo_move_to(cr, 10, i * 20 + 16);
3802 cairo_show_text(cr, menu->entries[i]);
3803 }
3804 }
3805
3806 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003807}
3808
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003809void
3810window_show_menu(struct display *display,
3811 struct input *input, uint32_t time, struct window *parent,
3812 int32_t x, int32_t y,
3813 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003814{
3815 struct window *window;
3816 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003817 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003818
3819 menu = malloc(sizeof *menu);
3820 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003821 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003822
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003823 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003824 if (!window) {
3825 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003826 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003827 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003828
3829 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003830 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003831 menu->entries = entries;
3832 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003833 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003834 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003835 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003836 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003837 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003838 window->type = TYPE_MENU;
3839 window->x = x;
3840 window->y = y;
3841
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003842 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003843 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003844 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02003845 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003846 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003847
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003848 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003849 widget_set_enter_handler(menu->widget, menu_enter_handler);
3850 widget_set_leave_handler(menu->widget, menu_leave_handler);
3851 widget_set_motion_handler(menu->widget, menu_motion_handler);
3852 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003853
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003854 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003855 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003856}
3857
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003858void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003859window_set_buffer_type(struct window *window, enum window_buffer_type type)
3860{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003861 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003862}
3863
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003864
3865static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003866display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003867 struct wl_output *wl_output,
3868 int x, int y,
3869 int physical_width,
3870 int physical_height,
3871 int subpixel,
3872 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003873 const char *model,
3874 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003875{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003876 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003877
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003878 output->allocation.x = x;
3879 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003880 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003881}
3882
3883static void
3884display_handle_mode(void *data,
3885 struct wl_output *wl_output,
3886 uint32_t flags,
3887 int width,
3888 int height,
3889 int refresh)
3890{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003891 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003892 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003893
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003894 if (flags & WL_OUTPUT_MODE_CURRENT) {
3895 output->allocation.width = width;
3896 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003897 if (display->output_configure_handler)
3898 (*display->output_configure_handler)(
3899 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003900 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003901}
3902
3903static const struct wl_output_listener output_listener = {
3904 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003905 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003906};
3907
3908static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003909display_add_output(struct display *d, uint32_t id)
3910{
3911 struct output *output;
3912
3913 output = malloc(sizeof *output);
3914 if (output == NULL)
3915 return;
3916
3917 memset(output, 0, sizeof *output);
3918 output->display = d;
3919 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003920 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003921 wl_list_insert(d->output_list.prev, &output->link);
3922
3923 wl_output_add_listener(output->output, &output_listener, output);
3924}
3925
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003926static void
3927output_destroy(struct output *output)
3928{
3929 if (output->destroy_handler)
3930 (*output->destroy_handler)(output, output->user_data);
3931
3932 wl_output_destroy(output->output);
3933 wl_list_remove(&output->link);
3934 free(output);
3935}
3936
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003937void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003938display_set_global_handler(struct display *display,
3939 display_global_handler_t handler)
3940{
3941 struct global *global;
3942
3943 display->global_handler = handler;
3944 if (!handler)
3945 return;
3946
3947 wl_list_for_each(global, &display->global_list, link)
3948 display->global_handler(display,
3949 global->name, global->interface,
3950 global->version, display->user_data);
3951}
3952
3953void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003954display_set_output_configure_handler(struct display *display,
3955 display_output_handler_t handler)
3956{
3957 struct output *output;
3958
3959 display->output_configure_handler = handler;
3960 if (!handler)
3961 return;
3962
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003963 wl_list_for_each(output, &display->output_list, link) {
3964 if (output->allocation.width == 0 &&
3965 output->allocation.height == 0)
3966 continue;
3967
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003968 (*display->output_configure_handler)(output,
3969 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003970 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003971}
3972
3973void
3974output_set_user_data(struct output *output, void *data)
3975{
3976 output->user_data = data;
3977}
3978
3979void *
3980output_get_user_data(struct output *output)
3981{
3982 return output->user_data;
3983}
3984
3985void
3986output_set_destroy_handler(struct output *output,
3987 display_output_handler_t handler)
3988{
3989 output->destroy_handler = handler;
3990 /* FIXME: implement this, once we have way to remove outputs */
3991}
3992
3993void
Scott Moreau4e072362012-09-29 02:03:11 -06003994output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003995{
Scott Moreau4e072362012-09-29 02:03:11 -06003996 struct rectangle allocation = output->allocation;
3997
3998 switch (output->transform) {
3999 case WL_OUTPUT_TRANSFORM_90:
4000 case WL_OUTPUT_TRANSFORM_270:
4001 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4002 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4003 /* Swap width and height */
4004 allocation.width = output->allocation.height;
4005 allocation.height = output->allocation.width;
4006 break;
4007 }
4008
4009 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004010}
4011
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004012struct wl_output *
4013output_get_wl_output(struct output *output)
4014{
4015 return output->output;
4016}
4017
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004018enum wl_output_transform
4019output_get_transform(struct output *output)
4020{
4021 return output->transform;
4022}
4023
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004024static void
Daniel Stone97f68542012-05-30 16:32:01 +01004025fini_xkb(struct input *input)
4026{
4027 xkb_state_unref(input->xkb.state);
4028 xkb_map_unref(input->xkb.keymap);
4029}
4030
4031static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004032display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004033{
4034 struct input *input;
4035
4036 input = malloc(sizeof *input);
4037 if (input == NULL)
4038 return;
4039
4040 memset(input, 0, sizeof *input);
4041 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004042 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004043 input->pointer_focus = NULL;
4044 input->keyboard_focus = NULL;
4045 wl_list_insert(d->input_list.prev, &input->link);
4046
Daniel Stone37816df2012-05-16 18:45:18 +01004047 wl_seat_add_listener(input->seat, &seat_listener, input);
4048 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004049
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004050 input->data_device =
4051 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004052 input->seat);
4053 wl_data_device_add_listener(input->data_device, &data_device_listener,
4054 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004055
4056 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004057
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004058 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4059 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004060 input->repeat_task.run = keyboard_repeat_func;
4061 display_watch_fd(d, input->repeat_timer_fd,
4062 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004063}
4064
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004065static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004066input_destroy(struct input *input)
4067{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004068 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004069 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004070
4071 if (input->drag_offer)
4072 data_offer_destroy(input->drag_offer);
4073
4074 if (input->selection_offer)
4075 data_offer_destroy(input->selection_offer);
4076
4077 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004078 fini_xkb(input);
4079
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004080 wl_surface_destroy(input->pointer_surface);
4081
Pekka Paalanene1207c72011-12-16 12:02:09 +02004082 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004083 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004084 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004085 free(input);
4086}
4087
4088static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004089init_workspace_manager(struct display *d, uint32_t id)
4090{
4091 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004092 wl_registry_bind(d->registry, id,
4093 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004094 if (d->workspace_manager != NULL)
4095 workspace_manager_add_listener(d->workspace_manager,
4096 &workspace_manager_listener,
4097 d);
4098}
4099
4100static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004101registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4102 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004103{
4104 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004105 struct global *global;
4106
4107 global = malloc(sizeof *global);
4108 global->name = id;
4109 global->interface = strdup(interface);
4110 global->version = version;
4111 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004112
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004113 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004114 d->compositor = wl_registry_bind(registry, id,
4115 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004116 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004117 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004118 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004119 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004120 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004121 d->shell = wl_registry_bind(registry,
4122 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004123 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004124 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004125 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4126 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004127 wl_registry_bind(registry, id,
4128 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004129 } else if (strcmp(interface, "text_cursor_position") == 0) {
4130 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004131 wl_registry_bind(registry, id,
4132 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004133 } else if (strcmp(interface, "workspace_manager") == 0) {
4134 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004135 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004136
4137 if (d->global_handler)
4138 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004139}
4140
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004141static void
4142registry_handle_global_remove(void *data, struct wl_registry *registry,
4143 uint32_t name)
4144{
4145 struct display *d = data;
4146 struct global *global;
4147 struct global *tmp;
4148
4149 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4150 if (global->name != name)
4151 continue;
4152
4153 /* XXX: Should destroy bound globals, and call
4154 * the counterpart of display::global_handler
4155 */
4156 wl_list_remove(&global->link);
4157 free(global->interface);
4158 free(global);
4159 }
4160}
4161
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004162void *
4163display_bind(struct display *display, uint32_t name,
4164 const struct wl_interface *interface, uint32_t version)
4165{
4166 return wl_registry_bind(display->registry, name, interface, version);
4167}
4168
4169static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004170 registry_handle_global,
4171 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004172};
4173
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004174#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004175static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004176init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004177{
4178 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004179 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004180
Rob Clark6396ed32012-03-11 19:48:41 -05004181#ifdef USE_CAIRO_GLESV2
4182# define GL_BIT EGL_OPENGL_ES2_BIT
4183#else
4184# define GL_BIT EGL_OPENGL_BIT
4185#endif
4186
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004187 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004188 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004189 EGL_RED_SIZE, 1,
4190 EGL_GREEN_SIZE, 1,
4191 EGL_BLUE_SIZE, 1,
4192 EGL_ALPHA_SIZE, 1,
4193 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004194 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004195 EGL_NONE
4196 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004197
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004198#ifdef USE_CAIRO_GLESV2
4199 static const EGLint context_attribs[] = {
4200 EGL_CONTEXT_CLIENT_VERSION, 2,
4201 EGL_NONE
4202 };
4203 EGLint api = EGL_OPENGL_ES_API;
4204#else
4205 EGLint *context_attribs = NULL;
4206 EGLint api = EGL_OPENGL_API;
4207#endif
4208
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004209 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004210 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004211 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004212 return -1;
4213 }
4214
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004215 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004216 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004217 return -1;
4218 }
4219
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004220 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4221 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004222 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004223 return -1;
4224 }
4225
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004226 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004227 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004228 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004229 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004230 return -1;
4231 }
4232
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004233 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004234 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004235 return -1;
4236 }
4237
Benjamin Franzke0c991632011-09-27 21:57:31 +02004238 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4239 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004240 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004241 return -1;
4242 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004243
4244 return 0;
4245}
4246
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004247static void
4248fini_egl(struct display *display)
4249{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004250 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004251
4252 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4253 EGL_NO_CONTEXT);
4254
4255 eglTerminate(display->dpy);
4256 eglReleaseThread();
4257}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004258#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004259
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004260static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004261init_dummy_surface(struct display *display)
4262{
4263 int len;
4264 void *data;
4265
4266 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4267 data = malloc(len);
4268 display->dummy_surface =
4269 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4270 1, 1, len);
4271 display->dummy_surface_data = data;
4272}
4273
4274static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004275handle_display_data(struct task *task, uint32_t events)
4276{
4277 struct display *display =
4278 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004279 struct epoll_event ep;
4280 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004281
4282 display->display_fd_events = events;
4283
4284 if (events & EPOLLERR || events & EPOLLHUP) {
4285 display_exit(display);
4286 return;
4287 }
4288
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004289 if (events & EPOLLIN) {
4290 ret = wl_display_dispatch(display->display);
4291 if (ret == -1) {
4292 display_exit(display);
4293 return;
4294 }
4295 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004296
4297 if (events & EPOLLOUT) {
4298 ret = wl_display_flush(display->display);
4299 if (ret == 0) {
4300 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4301 ep.data.ptr = &display->display_task;
4302 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4303 display->display_fd, &ep);
4304 } else if (ret == -1 && errno != EAGAIN) {
4305 display_exit(display);
4306 return;
4307 }
4308 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004309}
4310
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004311struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004312display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004313{
4314 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004315
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004316 d = malloc(sizeof *d);
4317 if (d == NULL)
4318 return NULL;
4319
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004320 memset(d, 0, sizeof *d);
4321
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004322 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004323 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004324 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004325 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004326 return NULL;
4327 }
4328
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004329 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004330 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004331 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004332 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4333 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004334
4335 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004336 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004337 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004338 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004339
Daniel Stone97f68542012-05-30 16:32:01 +01004340 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004341 if (d->xkb_context == NULL) {
4342 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004343 return NULL;
4344 }
4345
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004346 d->workspace = 0;
4347 d->workspace_count = 1;
4348
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004349 d->registry = wl_display_get_registry(d->display);
4350 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004351
4352 if (wl_display_dispatch(d->display) < 0) {
4353 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4354 return NULL;
4355 }
4356
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004357#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004358 if (init_egl(d) < 0)
4359 fprintf(stderr, "EGL does not seem to work, "
4360 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004361#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004362
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004363 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004364
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004365 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004366
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004367 wl_list_init(&d->window_list);
4368
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004369 init_dummy_surface(d);
4370
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004371 return d;
4372}
4373
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004374static void
4375display_destroy_outputs(struct display *display)
4376{
4377 struct output *tmp;
4378 struct output *output;
4379
4380 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4381 output_destroy(output);
4382}
4383
Pekka Paalanene1207c72011-12-16 12:02:09 +02004384static void
4385display_destroy_inputs(struct display *display)
4386{
4387 struct input *tmp;
4388 struct input *input;
4389
4390 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4391 input_destroy(input);
4392}
4393
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004394void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004395display_destroy(struct display *display)
4396{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004397 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004398 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4399 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004400
4401 if (!wl_list_empty(&display->deferred_list))
4402 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4403
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004404 cairo_surface_destroy(display->dummy_surface);
4405 free(display->dummy_surface_data);
4406
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004407 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004408 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004409
Daniel Stone97f68542012-05-30 16:32:01 +01004410 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004411
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004412 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004413 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004414
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004415#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004416 if (display->argb_device)
4417 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004418#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004419
Pekka Paalanenc2052982011-12-16 11:41:32 +02004420 if (display->shell)
4421 wl_shell_destroy(display->shell);
4422
4423 if (display->shm)
4424 wl_shm_destroy(display->shm);
4425
4426 if (display->data_device_manager)
4427 wl_data_device_manager_destroy(display->data_device_manager);
4428
4429 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004430 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004431
4432 close(display->epoll_fd);
4433
U. Artie Eoff44874d92012-10-02 21:12:35 -07004434 if (!(display->display_fd_events & EPOLLERR) &&
4435 !(display->display_fd_events & EPOLLHUP))
4436 wl_display_flush(display->display);
4437
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004438 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004439 free(display);
4440}
4441
4442void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004443display_set_user_data(struct display *display, void *data)
4444{
4445 display->user_data = data;
4446}
4447
4448void *
4449display_get_user_data(struct display *display)
4450{
4451 return display->user_data;
4452}
4453
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004454struct wl_display *
4455display_get_display(struct display *display)
4456{
4457 return display->display;
4458}
4459
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004460struct output *
4461display_get_output(struct display *display)
4462{
4463 return container_of(display->output_list.next, struct output, link);
4464}
4465
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004466struct wl_compositor *
4467display_get_compositor(struct display *display)
4468{
4469 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004470}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004471
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004472uint32_t
4473display_get_serial(struct display *display)
4474{
4475 return display->serial;
4476}
4477
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004478EGLDisplay
4479display_get_egl_display(struct display *d)
4480{
4481 return d->dpy;
4482}
4483
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004484struct wl_data_source *
4485display_create_data_source(struct display *display)
4486{
4487 return wl_data_device_manager_create_data_source(display->data_device_manager);
4488}
4489
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004490EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004491display_get_argb_egl_config(struct display *d)
4492{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004493 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004494}
4495
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004496struct wl_shell *
4497display_get_shell(struct display *display)
4498{
4499 return display->shell;
4500}
4501
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004502int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004503display_acquire_window_surface(struct display *display,
4504 struct window *window,
4505 EGLContext ctx)
4506{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004507 struct surface *surface = window->main_surface;
4508
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004509 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004510 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004511
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004512 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004513}
4514
4515void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004516display_release_window_surface(struct display *display,
4517 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004518{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004519 struct surface *surface = window->main_surface;
4520
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004521 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004522 return;
4523
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004524 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004525}
4526
4527void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004528display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004529{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004530 wl_list_insert(&display->deferred_list, &task->link);
4531}
4532
4533void
4534display_watch_fd(struct display *display,
4535 int fd, uint32_t events, struct task *task)
4536{
4537 struct epoll_event ep;
4538
4539 ep.events = events;
4540 ep.data.ptr = task;
4541 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4542}
4543
4544void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004545display_unwatch_fd(struct display *display, int fd)
4546{
4547 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4548}
4549
4550void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004551display_run(struct display *display)
4552{
4553 struct task *task;
4554 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004555 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004556
Pekka Paalanen826d7952011-12-15 10:14:07 +02004557 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004558 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004559 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004560 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004561 struct task, link);
4562 wl_list_remove(&task->link);
4563 task->run(task, 0);
4564 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004565
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004566 wl_display_dispatch_pending(display->display);
4567
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004568 if (!display->running)
4569 break;
4570
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004571 ret = wl_display_flush(display->display);
4572 if (ret < 0 && errno == EAGAIN) {
4573 ep[0].events =
4574 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4575 ep[0].data.ptr = &display->display_task;
4576
4577 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4578 display->display_fd, &ep[0]);
4579 } else if (ret < 0) {
4580 break;
4581 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004582
4583 count = epoll_wait(display->epoll_fd,
4584 ep, ARRAY_LENGTH(ep), -1);
4585 for (i = 0; i < count; i++) {
4586 task = ep[i].data.ptr;
4587 task->run(task, ep[i].events);
4588 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004589 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004590}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004591
4592void
4593display_exit(struct display *display)
4594{
4595 display->running = 0;
4596}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004597
4598void
4599keysym_modifiers_add(struct wl_array *modifiers_map,
4600 const char *name)
4601{
4602 size_t len = strlen(name) + 1;
4603 char *p;
4604
4605 p = wl_array_add(modifiers_map, len);
4606
4607 if (p == NULL)
4608 return;
4609
4610 strncpy(p, name, len);
4611}
4612
4613static xkb_mod_index_t
4614keysym_modifiers_get_index(struct wl_array *modifiers_map,
4615 const char *name)
4616{
4617 xkb_mod_index_t index = 0;
4618 char *p = modifiers_map->data;
4619
4620 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4621 if (strcmp(p, name) == 0)
4622 return index;
4623
4624 index++;
4625 p += strlen(p) + 1;
4626 }
4627
4628 return XKB_MOD_INVALID;
4629}
4630
4631xkb_mod_mask_t
4632keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4633 const char *name)
4634{
4635 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4636
4637 if (index == XKB_MOD_INVALID)
4638 return XKB_MOD_INVALID;
4639
4640 return 1 << index;
4641}