blob: 1bfbc78f812d79e673f02b33cc64ed0ff2e3aced [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;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300248 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500249 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400250 struct wl_list link;
251 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500252 widget_resize_handler_t resize_handler;
253 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500254 widget_enter_handler_t enter_handler;
255 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500256 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500257 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200258 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400259 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500260 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300261 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500262 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400263};
264
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400265struct input {
266 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100267 struct wl_seat *seat;
268 struct wl_pointer *pointer;
269 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400270 struct window *pointer_focus;
271 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300272 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300273 uint32_t cursor_anim_start;
274 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300275 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400276 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400277 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400278 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400279 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400280 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400281
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500282 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500283 struct widget *grab;
284 uint32_t grab_button;
285
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400286 struct wl_data_device *data_device;
287 struct data_offer *drag_offer;
288 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100289
290 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100291 struct xkb_keymap *keymap;
292 struct xkb_state *state;
293 xkb_mod_mask_t control_mask;
294 xkb_mod_mask_t alt_mask;
295 xkb_mod_mask_t shift_mask;
296 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400297
298 struct task repeat_task;
299 int repeat_timer_fd;
300 uint32_t repeat_sym;
301 uint32_t repeat_key;
302 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303};
304
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500305struct output {
306 struct display *display;
307 struct wl_output *output;
308 struct rectangle allocation;
309 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600310 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200311
312 display_output_handler_t destroy_handler;
313 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500314};
315
Martin Minarik1998b152012-05-10 02:04:35 +0200316enum frame_button_action {
317 FRAME_BUTTON_NULL = 0,
318 FRAME_BUTTON_ICON = 1,
319 FRAME_BUTTON_CLOSE = 2,
320 FRAME_BUTTON_MINIMIZE = 3,
321 FRAME_BUTTON_MAXIMIZE = 4,
322};
323
324enum frame_button_pointer {
325 FRAME_BUTTON_DEFAULT = 0,
326 FRAME_BUTTON_OVER = 1,
327 FRAME_BUTTON_ACTIVE = 2,
328};
329
330enum frame_button_align {
331 FRAME_BUTTON_RIGHT = 0,
332 FRAME_BUTTON_LEFT = 1,
333};
334
335enum frame_button_decoration {
336 FRAME_BUTTON_NONE = 0,
337 FRAME_BUTTON_FANCY = 1,
338};
339
340struct frame_button {
341 struct widget *widget;
342 struct frame *frame;
343 cairo_surface_t *icon;
344 enum frame_button_action type;
345 enum frame_button_pointer state;
346 struct wl_list link; /* buttons_list */
347 enum frame_button_align align;
348 enum frame_button_decoration decoration;
349};
350
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500351struct frame {
352 struct widget *widget;
353 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200354 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500355};
356
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500357struct menu {
358 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500359 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500360 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500361 const char **entries;
362 uint32_t time;
363 int current;
364 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400365 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500366 menu_func_t func;
367};
368
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300369struct tooltip {
370 struct widget *parent;
371 struct window *window;
372 struct widget *widget;
373 char *entry;
374 struct task tooltip_task;
375 int tooltip_fd;
376 float x, y;
377};
378
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700379struct shm_pool {
380 struct wl_shm_pool *pool;
381 size_t size;
382 size_t used;
383 void *data;
384};
385
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400386enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300387 CURSOR_DEFAULT = 100,
388 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400389};
390
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500391enum window_location {
392 WINDOW_INTERIOR = 0,
393 WINDOW_RESIZING_TOP = 1,
394 WINDOW_RESIZING_BOTTOM = 2,
395 WINDOW_RESIZING_LEFT = 4,
396 WINDOW_RESIZING_TOP_LEFT = 5,
397 WINDOW_RESIZING_BOTTOM_LEFT = 6,
398 WINDOW_RESIZING_RIGHT = 8,
399 WINDOW_RESIZING_TOP_RIGHT = 9,
400 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
401 WINDOW_RESIZING_MASK = 15,
402 WINDOW_EXTERIOR = 16,
403 WINDOW_TITLEBAR = 17,
404 WINDOW_CLIENT_AREA = 18,
405};
406
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200407static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400408
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500409#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200411struct egl_window_surface {
412 struct toysurface base;
413 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100414 struct display *display;
415 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200416 struct wl_egl_window *egl_window;
417 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100418};
419
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200420static struct egl_window_surface *
421to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100422{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200423 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100424}
425
426static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200427egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200428 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100429{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200430 struct egl_window_surface *surface = to_egl_window_surface(base);
431
432 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
433 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
434
435 return cairo_surface_reference(surface->cairo_surface);
436}
437
438static void
439egl_window_surface_swap(struct toysurface *base,
440 struct rectangle *server_allocation)
441{
442 struct egl_window_surface *surface = to_egl_window_surface(base);
443
444 cairo_gl_surface_swapbuffers(surface->cairo_surface);
445 wl_egl_window_get_attached_size(surface->egl_window,
446 &server_allocation->width,
447 &server_allocation->height);
448}
449
450static int
451egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
452{
453 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200454 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100455
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200456 device = cairo_surface_get_device(surface->cairo_surface);
457 if (!device)
458 return -1;
459
460 if (!ctx) {
461 if (device == surface->display->argb_device)
462 ctx = surface->display->argb_ctx;
463 else
464 assert(0);
465 }
466
467 cairo_device_flush(device);
468 cairo_device_acquire(device);
469 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
470 surface->egl_surface, ctx))
471 fprintf(stderr, "failed to make surface current\n");
472
473 return 0;
474}
475
476static void
477egl_window_surface_release(struct toysurface *base)
478{
479 struct egl_window_surface *surface = to_egl_window_surface(base);
480 cairo_device_t *device;
481
482 device = cairo_surface_get_device(surface->cairo_surface);
483 if (!device)
484 return;
485
486 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
487 surface->display->argb_ctx))
488 fprintf(stderr, "failed to make context current\n");
489
490 cairo_device_release(device);
491}
492
493static void
494egl_window_surface_destroy(struct toysurface *base)
495{
496 struct egl_window_surface *surface = to_egl_window_surface(base);
497 struct display *d = surface->display;
498
499 cairo_surface_destroy(surface->cairo_surface);
500 eglDestroySurface(d->dpy, surface->egl_surface);
501 wl_egl_window_destroy(surface->egl_window);
502 surface->surface = NULL;
503
504 free(surface);
505}
506
507static struct toysurface *
508egl_window_surface_create(struct display *display,
509 struct wl_surface *wl_surface,
510 uint32_t flags,
511 struct rectangle *rectangle)
512{
513 struct egl_window_surface *surface;
514
Pekka Paalanenb3627362012-11-19 17:16:00 +0200515 if (display->dpy == EGL_NO_DISPLAY)
516 return NULL;
517
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518 surface = calloc(1, sizeof *surface);
519 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520 return NULL;
521
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 surface->base.prepare = egl_window_surface_prepare;
523 surface->base.swap = egl_window_surface_swap;
524 surface->base.acquire = egl_window_surface_acquire;
525 surface->base.release = egl_window_surface_release;
526 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100527
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200528 surface->display = display;
529 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400530
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200531 surface->egl_window = wl_egl_window_create(surface->surface,
532 rectangle->width,
533 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100534
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200535 surface->egl_surface = eglCreateWindowSurface(display->dpy,
536 display->argb_config,
537 surface->egl_window,
538 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100539
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200540 surface->cairo_surface =
541 cairo_gl_surface_create_for_egl(display->argb_device,
542 surface->egl_surface,
543 rectangle->width,
544 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100545
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200546 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100547}
548
Pekka Paalanenb3627362012-11-19 17:16:00 +0200549#else
550
551static struct toysurface *
552egl_window_surface_create(struct display *display,
553 struct wl_surface *wl_surface,
554 uint32_t flags,
555 struct rectangle *rectangle)
556{
557 return NULL;
558}
559
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560#endif
561
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200562struct shm_surface_data {
563 struct wl_buffer *buffer;
564 struct shm_pool *pool;
565};
566
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400567struct wl_buffer *
568display_get_buffer_for_surface(struct display *display,
569 cairo_surface_t *surface)
570{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200571 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400572
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200573 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400574
575 return data->buffer;
576}
577
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500578static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700579shm_pool_destroy(struct shm_pool *pool);
580
581static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400582shm_surface_data_destroy(void *p)
583{
584 struct shm_surface_data *data = p;
585
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200586 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700587 if (data->pool)
588 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300589
590 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400591}
592
Kristian Høgsberg16626282012-04-03 11:21:27 -0400593static struct wl_shm_pool *
594make_shm_pool(struct display *display, int size, void **data)
595{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400596 struct wl_shm_pool *pool;
597 int fd;
598
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300599 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400600 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300601 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
602 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400603 return NULL;
604 }
605
606 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400607 if (*data == MAP_FAILED) {
608 fprintf(stderr, "mmap failed: %m\n");
609 close(fd);
610 return NULL;
611 }
612
613 pool = wl_shm_create_pool(display->shm, fd, size);
614
615 close(fd);
616
617 return pool;
618}
619
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700620static struct shm_pool *
621shm_pool_create(struct display *display, size_t size)
622{
623 struct shm_pool *pool = malloc(sizeof *pool);
624
625 if (!pool)
626 return NULL;
627
628 pool->pool = make_shm_pool(display, size, &pool->data);
629 if (!pool->pool) {
630 free(pool);
631 return NULL;
632 }
633
634 pool->size = size;
635 pool->used = 0;
636
637 return pool;
638}
639
640static void *
641shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
642{
643 if (pool->used + size > pool->size)
644 return NULL;
645
646 *offset = pool->used;
647 pool->used += size;
648
649 return (char *) pool->data + *offset;
650}
651
652/* destroy the pool. this does not unmap the memory though */
653static void
654shm_pool_destroy(struct shm_pool *pool)
655{
656 munmap(pool->data, pool->size);
657 wl_shm_pool_destroy(pool->pool);
658 free(pool);
659}
660
661/* Start allocating from the beginning of the pool again */
662static void
663shm_pool_reset(struct shm_pool *pool)
664{
665 pool->used = 0;
666}
667
668static int
669data_length_for_shm_surface(struct rectangle *rect)
670{
671 int stride;
672
673 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
674 rect->width);
675 return stride * rect->height;
676}
677
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500678static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700679display_create_shm_surface_from_pool(struct display *display,
680 struct rectangle *rectangle,
681 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682{
683 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400684 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400685 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700686 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400687 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400688
689 data = malloc(sizeof *data);
690 if (data == NULL)
691 return NULL;
692
693 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
694 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700695 length = stride * rectangle->height;
696 data->pool = NULL;
697 map = shm_pool_allocate(pool, length, &offset);
698
699 if (!map) {
700 free(data);
701 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400702 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400703
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400704 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705 CAIRO_FORMAT_ARGB32,
706 rectangle->width,
707 rectangle->height,
708 stride);
709
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200710 cairo_surface_set_user_data(surface, &shm_surface_data_key,
711 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400712
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400713 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500714 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400715 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500716 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400717
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200718 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
719 rectangle->width,
720 rectangle->height,
721 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400722
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723 return surface;
724}
725
726static cairo_surface_t *
727display_create_shm_surface(struct display *display,
728 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200729 struct shm_pool *alternate_pool,
730 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731{
732 struct shm_surface_data *data;
733 struct shm_pool *pool;
734 cairo_surface_t *surface;
735
Pekka Paalanen99436862012-11-19 17:15:59 +0200736 if (alternate_pool) {
737 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700738 surface = display_create_shm_surface_from_pool(display,
739 rectangle,
740 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200741 alternate_pool);
742 if (surface) {
743 data = cairo_surface_get_user_data(surface,
744 &shm_surface_data_key);
745 goto out;
746 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700747 }
748
749 pool = shm_pool_create(display,
750 data_length_for_shm_surface(rectangle));
751 if (!pool)
752 return NULL;
753
754 surface =
755 display_create_shm_surface_from_pool(display, rectangle,
756 flags, pool);
757
758 if (!surface) {
759 shm_pool_destroy(pool);
760 return NULL;
761 }
762
763 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200764 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700765 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400766
Pekka Paalanen99436862012-11-19 17:15:59 +0200767out:
768 if (data_ret)
769 *data_ret = data;
770
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400771 return surface;
772}
773
nobled7b87cb02011-02-01 18:51:47 +0000774static int
775check_size(struct rectangle *rect)
776{
777 if (rect->width && rect->height)
778 return 0;
779
780 fprintf(stderr, "tried to create surface of "
781 "width: %d, height: %d\n", rect->width, rect->height);
782 return -1;
783}
784
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400785cairo_surface_t *
786display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400788 struct rectangle *rectangle,
789 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400790{
nobled7b87cb02011-02-01 18:51:47 +0000791 if (check_size(rectangle) < 0)
792 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200793
794 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200795 return display_create_shm_surface(display, rectangle, flags,
796 NULL, NULL);
797}
798
Pekka Paalanena4eda732012-11-19 17:16:02 +0200799struct shm_surface_leaf {
800 cairo_surface_t *cairo_surface;
801 /* 'data' is automatically destroyed, when 'cairo_surface' is */
802 struct shm_surface_data *data;
803
804 struct shm_pool *resize_pool;
805 int busy;
806};
807
808static void
809shm_surface_leaf_release(struct shm_surface_leaf *leaf)
810{
811 if (leaf->cairo_surface)
812 cairo_surface_destroy(leaf->cairo_surface);
813 /* leaf->data already destroyed via cairo private */
814
815 if (leaf->resize_pool)
816 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200817
818 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200819}
820
Pekka Paalanen99436862012-11-19 17:15:59 +0200821struct shm_surface {
822 struct toysurface base;
823 struct display *display;
824 struct wl_surface *surface;
825 uint32_t flags;
826 int dx, dy;
827
Pekka Paalanena4eda732012-11-19 17:16:02 +0200828 struct shm_surface_leaf leaf[2];
829 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200830};
831
832static struct shm_surface *
833to_shm_surface(struct toysurface *base)
834{
835 return container_of(base, struct shm_surface, base);
836}
837
Pekka Paalanena4eda732012-11-19 17:16:02 +0200838static void
839shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
840{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200841 struct shm_surface *surface = data;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200842
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200843 if (surface->leaf[0].data->buffer == buffer)
844 surface->leaf[0].busy = 0;
845 else if (surface->leaf[1].data->buffer == buffer)
846 surface->leaf[1].busy = 0;
847 else
848 assert(0 && "shm_surface_buffer_release: unknown buffer");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200849
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200850 if (!surface->leaf[0].busy && !surface->leaf[1].busy)
851 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200852}
853
854static const struct wl_buffer_listener shm_surface_buffer_listener = {
855 shm_surface_buffer_release
856};
857
Pekka Paalanen99436862012-11-19 17:15:59 +0200858static cairo_surface_t *
859shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200860 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200861{
Pekka Paalanenec076692012-11-30 13:37:27 +0200862 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200863 struct shm_surface *surface = to_shm_surface(base);
864 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200865 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200866
867 surface->dx = dx;
868 surface->dy = dy;
869
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200870 /* See shm_surface_buffer_release() */
871 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
872 surface->leaf[1].cairo_surface) {
873 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
874 __func__);
875 }
876
Pekka Paalanena4eda732012-11-19 17:16:02 +0200877 /* pick a free buffer from the two */
878 if (!surface->leaf[0].busy)
879 leaf = &surface->leaf[0];
880 else if (!surface->leaf[1].busy)
881 leaf = &surface->leaf[1];
882 else {
883 fprintf(stderr, "%s: both buffers are held by the server.\n",
884 __func__);
885 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200886 }
887
Pekka Paalanena4eda732012-11-19 17:16:02 +0200888 if (!resize_hint && leaf->resize_pool) {
889 cairo_surface_destroy(leaf->cairo_surface);
890 leaf->cairo_surface = NULL;
891 shm_pool_destroy(leaf->resize_pool);
892 leaf->resize_pool = NULL;
893 }
894
895 if (leaf->cairo_surface &&
896 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
897 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200898 goto out;
899
Pekka Paalanena4eda732012-11-19 17:16:02 +0200900 if (leaf->cairo_surface)
901 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200902
Pekka Paalanena4eda732012-11-19 17:16:02 +0200903 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200904 /* Create a big pool to allocate from, while continuously
905 * resizing. Mmapping a new pool in the server
906 * is relatively expensive, so reusing a pool performs
907 * better, but may temporarily reserve unneeded memory.
908 */
909 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200910 leaf->resize_pool = shm_pool_create(surface->display,
911 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200912 }
913
Pekka Paalanena4eda732012-11-19 17:16:02 +0200914 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200915 display_create_shm_surface(surface->display, &rect,
916 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200917 leaf->resize_pool,
918 &leaf->data);
919 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200920 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200921
922out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200923 surface->current = leaf;
924
925 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200926}
927
928static void
929shm_surface_swap(struct toysurface *base,
930 struct rectangle *server_allocation)
931{
932 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200933 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200934
935 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200936 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200937 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200939
Pekka Paalanena4eda732012-11-19 17:16:02 +0200940 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200941 surface->dx, surface->dy);
942 wl_surface_damage(surface->surface, 0, 0,
943 server_allocation->width, server_allocation->height);
944 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200945
946 leaf->busy = 1;
947 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200948}
949
950static int
951shm_surface_acquire(struct toysurface *base, EGLContext ctx)
952{
953 return -1;
954}
955
956static void
957shm_surface_release(struct toysurface *base)
958{
959}
960
961static void
962shm_surface_destroy(struct toysurface *base)
963{
964 struct shm_surface *surface = to_shm_surface(base);
965
Pekka Paalanena4eda732012-11-19 17:16:02 +0200966 shm_surface_leaf_release(&surface->leaf[0]);
967 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200968
969 free(surface);
970}
971
972static struct toysurface *
973shm_surface_create(struct display *display, struct wl_surface *wl_surface,
974 uint32_t flags, struct rectangle *rectangle)
975{
976 struct shm_surface *surface;
977
978 surface = calloc(1, sizeof *surface);
979 if (!surface)
980 return NULL;
981
982 surface->base.prepare = shm_surface_prepare;
983 surface->base.swap = shm_surface_swap;
984 surface->base.acquire = shm_surface_acquire;
985 surface->base.release = shm_surface_release;
986 surface->base.destroy = shm_surface_destroy;
987
988 surface->display = display;
989 surface->surface = wl_surface;
990 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200991
992 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400993}
994
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200995/*
996 * The following correspondences between file names and cursors was copied
997 * from: https://bugs.kde.org/attachment.cgi?id=67313
998 */
999
1000static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001001 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001002 "sw-resize"
1003};
1004
1005static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001006 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001007 "se-resize"
1008};
1009
1010static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001011 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001012 "s-resize"
1013};
1014
1015static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001016 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001017 "closedhand",
1018 "208530c400c041818281048008011002"
1019};
1020
1021static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001022 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001023 "default",
1024 "top_left_arrow",
1025 "left-arrow"
1026};
1027
1028static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001029 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001030 "w-resize"
1031};
1032
1033static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001034 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001035 "e-resize"
1036};
1037
1038static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001039 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001040 "nw-resize"
1041};
1042
1043static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001044 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001045 "ne-resize"
1046};
1047
1048static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001049 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001050 "n-resize"
1051};
1052
1053static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001054 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001055 "ibeam",
1056 "text"
1057};
1058
1059static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001060 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001061 "pointer",
1062 "pointing_hand",
1063 "e29285e634086352946a0e7090d73106"
1064};
1065
1066static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001067 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001068 "wait",
1069 "0426c94ea35c87780ff01dc239897213"
1070};
1071
1072struct cursor_alternatives {
1073 const char **names;
1074 size_t count;
1075};
1076
1077static const struct cursor_alternatives cursors[] = {
1078 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1079 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1080 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1081 {grabbings, ARRAY_LENGTH(grabbings)},
1082 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1083 {left_sides, ARRAY_LENGTH(left_sides)},
1084 {right_sides, ARRAY_LENGTH(right_sides)},
1085 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1086 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1087 {top_sides, ARRAY_LENGTH(top_sides)},
1088 {xterms, ARRAY_LENGTH(xterms)},
1089 {hand1s, ARRAY_LENGTH(hand1s)},
1090 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001091};
1092
1093static void
1094create_cursors(struct display *display)
1095{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001096 char *config_file;
1097 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001098 unsigned int i, j;
1099 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001100 struct config_key shell_keys[] = {
1101 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1102 };
1103 struct config_section cs[] = {
1104 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1105 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001106
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001107 config_file = config_file_path("weston.ini");
1108 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1109 free(config_file);
1110
1111 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001112 display->cursors =
1113 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1114
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001115 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001116 cursor = NULL;
1117 for (j = 0; !cursor && j < cursors[i].count; ++j)
1118 cursor = wl_cursor_theme_get_cursor(
1119 display->cursor_theme, cursors[i].names[j]);
1120
1121 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001122 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001123 cursors[i].names[0]);
1124
1125 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001126 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001127}
1128
1129static void
1130destroy_cursors(struct display *display)
1131{
1132 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001133 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001134}
1135
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001136struct wl_cursor_image *
1137display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001138{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001139 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001140
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001141 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001142}
1143
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001144static void
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001145surface_attach_surface(struct surface *surface)
1146{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001147 if (surface->opaque_region) {
1148 wl_surface_set_opaque_region(surface->surface,
1149 surface->opaque_region);
1150 wl_region_destroy(surface->opaque_region);
1151 surface->opaque_region = NULL;
1152 }
1153
1154 if (surface->input_region) {
1155 wl_surface_set_input_region(surface->surface,
1156 surface->input_region);
1157 wl_region_destroy(surface->input_region);
1158 surface->input_region = NULL;
1159 }
1160
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001161 surface->toysurface->swap(surface->toysurface,
1162 &surface->server_allocation);
1163}
1164
1165static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001166window_attach_surface(struct window *window)
1167{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001168 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001169
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001170 if (window->type == TYPE_NONE) {
1171 window->type = TYPE_TOPLEVEL;
1172 if (display->shell)
1173 wl_shell_surface_set_toplevel(window->shell_surface);
1174 }
1175
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001176 surface_attach_surface(window->main_surface);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001177}
1178
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001179int
1180window_has_focus(struct window *window)
1181{
1182 return window->focus_count > 0;
1183}
1184
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001185static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001186window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001187{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001188 if (!window->cairo_surface)
1189 return;
1190
1191 window_attach_surface(window);
1192 cairo_surface_destroy(window->cairo_surface);
1193 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001194}
1195
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001196struct display *
1197window_get_display(struct window *window)
1198{
1199 return window->display;
1200}
1201
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001202static cairo_surface_t *
1203surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001204{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001205 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001206 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001207
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001208 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001209 case WL_OUTPUT_TRANSFORM_90:
1210 case WL_OUTPUT_TRANSFORM_270:
1211 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1212 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001213 allocation.width = surface->allocation.height;
1214 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001215 break;
1216 default:
1217 break;
1218 }
1219
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001220 if (!surface->toysurface && display->dpy &&
1221 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001222 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001223 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001224 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001225 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001226 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001227 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001228
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001229 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001230 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001231 surface->surface,
1232 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001233
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001234 return surface->toysurface->prepare(surface->toysurface, dx, dy,
1235 allocation.width,
1236 allocation.height,
1237 flags);
1238}
1239
1240static void
1241window_create_surface(struct window *window)
1242{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001243 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001244 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001245 int dx = 0;
1246 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001247
1248 if (!window->transparent)
1249 flags |= SURFACE_OPAQUE;
1250
Pekka Paalanenec076692012-11-30 13:37:27 +02001251 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001252 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001253
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001254 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1255 dx = surface->server_allocation.width -
1256 surface->allocation.width;
1257
1258 if (window->resize_edges & WINDOW_RESIZING_TOP)
1259 dy = surface->server_allocation.height -
1260 surface->allocation.height;
1261
1262 window->resize_edges = 0;
1263
1264 window->cairo_surface = surface_create_surface(window->main_surface,
1265 dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001266}
1267
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001268int
1269window_get_buffer_transform(struct window *window)
1270{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001271 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001272}
1273
1274void
1275window_set_buffer_transform(struct window *window,
1276 enum wl_output_transform transform)
1277{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001278 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001279 wl_surface_set_buffer_transform(window->main_surface->surface,
1280 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001281}
1282
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001283static void frame_destroy(struct frame *frame);
1284
Pekka Paalanen4e373742013-02-13 16:17:13 +02001285static void
1286surface_destroy(struct surface *surface)
1287{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001288 if (surface->input_region)
1289 wl_region_destroy(surface->input_region);
1290
1291 if (surface->opaque_region)
1292 wl_region_destroy(surface->opaque_region);
1293
Pekka Paalanen4e373742013-02-13 16:17:13 +02001294 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001295
1296 if (surface->toysurface)
1297 surface->toysurface->destroy(surface->toysurface);
1298
Pekka Paalanen4e373742013-02-13 16:17:13 +02001299 free(surface);
1300}
1301
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001302void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001303window_destroy(struct window *window)
1304{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001305 struct display *display = window->display;
1306 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001307 struct window_output *window_output;
1308 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001309
1310 if (window->redraw_scheduled)
1311 wl_list_remove(&window->redraw_task.link);
1312
1313 wl_list_for_each(input, &display->input_list, link) {
1314 if (input->pointer_focus == window)
1315 input->pointer_focus = NULL;
1316 if (input->keyboard_focus == window)
1317 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001318 if (input->focus_widget &&
1319 input->focus_widget->window == window)
1320 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001321 }
1322
Rob Bradford7507b572012-05-15 17:55:34 +01001323 wl_list_for_each_safe(window_output, window_output_tmp,
1324 &window->window_output_list, link) {
1325 free (window_output);
1326 }
1327
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001328 if (window->frame)
1329 frame_destroy(window->frame);
1330
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001331 if (window->shell_surface)
1332 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001333
1334 surface_destroy(window->main_surface);
1335
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001336 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001337
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001338 if (window->frame_cb)
1339 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001340 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001341 free(window);
1342}
1343
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001344static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001345widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001346{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001347 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001348
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001349 wl_list_for_each(child, &widget->child_list, link) {
1350 target = widget_find_widget(child, x, y);
1351 if (target)
1352 return target;
1353 }
1354
1355 if (widget->allocation.x <= x &&
1356 x < widget->allocation.x + widget->allocation.width &&
1357 widget->allocation.y <= y &&
1358 y < widget->allocation.y + widget->allocation.height) {
1359 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001360 }
1361
1362 return NULL;
1363}
1364
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001365static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001366window_find_widget(struct window *window, int32_t x, int32_t y)
1367{
1368 return widget_find_widget(window->main_surface->widget, x, y);
1369}
1370
1371static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001372widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001373{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001374 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001375
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001376 widget = malloc(sizeof *widget);
1377 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001378 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001379 widget->user_data = data;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001380 widget->allocation = window->main_surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001381 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001382 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001383 widget->tooltip = NULL;
1384 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001385 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001386
1387 return widget;
1388}
1389
1390struct widget *
1391window_add_widget(struct window *window, void *data)
1392{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001393 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001394
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001395 widget = widget_create(window, data);
1396 wl_list_init(&widget->link);
1397 window->main_surface->widget = widget;
1398
1399 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001400}
1401
1402struct widget *
1403widget_add_widget(struct widget *parent, void *data)
1404{
1405 struct widget *widget;
1406
1407 widget = widget_create(parent->window, data);
1408 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001409
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001410 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001411}
1412
1413void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001414widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001415{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001416 struct display *display = widget->window->display;
1417 struct input *input;
1418
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001419 if (widget->tooltip) {
1420 free(widget->tooltip);
1421 widget->tooltip = NULL;
1422 }
1423
Pekka Paalanene156fb62012-01-19 13:51:38 +02001424 wl_list_for_each(input, &display->input_list, link) {
1425 if (input->focus_widget == widget)
1426 input->focus_widget = NULL;
1427 }
1428
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001429 wl_list_remove(&widget->link);
1430 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001431}
1432
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001433void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001434widget_set_default_cursor(struct widget *widget, int cursor)
1435{
1436 widget->default_cursor = cursor;
1437}
1438
1439void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001440widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001441{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001442 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001443}
1444
1445void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001446widget_set_size(struct widget *widget, int32_t width, int32_t height)
1447{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001448 widget->allocation.width = width;
1449 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001450}
1451
1452void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001453widget_set_allocation(struct widget *widget,
1454 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001455{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001456 widget->allocation.x = x;
1457 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001458 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001459}
1460
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001461void
1462widget_set_transparent(struct widget *widget, int transparent)
1463{
1464 widget->opaque = !transparent;
1465}
1466
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001467void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001468widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001469{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001470 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001471}
1472
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001473void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001474widget_set_resize_handler(struct widget *widget,
1475 widget_resize_handler_t handler)
1476{
1477 widget->resize_handler = handler;
1478}
1479
1480void
1481widget_set_redraw_handler(struct widget *widget,
1482 widget_redraw_handler_t handler)
1483{
1484 widget->redraw_handler = handler;
1485}
1486
1487void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001488widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001489{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001490 widget->enter_handler = handler;
1491}
1492
1493void
1494widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1495{
1496 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001497}
1498
1499void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001500widget_set_motion_handler(struct widget *widget,
1501 widget_motion_handler_t handler)
1502{
1503 widget->motion_handler = handler;
1504}
1505
1506void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001507widget_set_button_handler(struct widget *widget,
1508 widget_button_handler_t handler)
1509{
1510 widget->button_handler = handler;
1511}
1512
1513void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001514widget_set_axis_handler(struct widget *widget,
1515 widget_axis_handler_t handler)
1516{
1517 widget->axis_handler = handler;
1518}
1519
1520void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001521widget_schedule_redraw(struct widget *widget)
1522{
1523 window_schedule_redraw(widget->window);
1524}
1525
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001526cairo_surface_t *
1527window_get_surface(struct window *window)
1528{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001529 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001530}
1531
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001532struct wl_surface *
1533window_get_wl_surface(struct window *window)
1534{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001535 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001536}
1537
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001538struct wl_shell_surface *
1539window_get_wl_shell_surface(struct window *window)
1540{
1541 return window->shell_surface;
1542}
1543
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001544static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001545tooltip_redraw_handler(struct widget *widget, void *data)
1546{
1547 cairo_t *cr;
1548 const int32_t r = 3;
1549 struct tooltip *tooltip = data;
1550 int32_t width, height;
1551 struct window *window = widget->window;
1552
1553 cr = cairo_create(window->cairo_surface);
1554 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1555 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1556 cairo_paint(cr);
1557
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001558 width = window->main_surface->allocation.width;
1559 height = window->main_surface->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001560 rounded_rect(cr, 0, 0, width, height, r);
1561
1562 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1563 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1564 cairo_fill(cr);
1565
1566 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1567 cairo_move_to(cr, 10, 16);
1568 cairo_show_text(cr, tooltip->entry);
1569 cairo_destroy(cr);
1570}
1571
1572static cairo_text_extents_t
1573get_text_extents(struct tooltip *tooltip)
1574{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001575 cairo_t *cr;
1576 cairo_text_extents_t extents;
1577
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001578 /* Use the dummy_surface because tooltip's surface was not
1579 * created yet, and parent does not have a valid surface
1580 * outside repaint, either.
1581 */
1582 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001583 cairo_text_extents(cr, tooltip->entry, &extents);
1584 cairo_destroy(cr);
1585
1586 return extents;
1587}
1588
1589static int
1590window_create_tooltip(struct tooltip *tooltip)
1591{
1592 struct widget *parent = tooltip->parent;
1593 struct display *display = parent->window->display;
1594 struct window *window;
1595 const int offset_y = 27;
1596 const int margin = 3;
1597 cairo_text_extents_t extents;
1598
1599 if (tooltip->widget)
1600 return 0;
1601
1602 window = window_create_transient(display, parent->window, tooltip->x,
1603 tooltip->y + offset_y,
1604 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1605 if (!window)
1606 return -1;
1607
1608 tooltip->window = window;
1609 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1610
1611 extents = get_text_extents(tooltip);
1612 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1613 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1614
1615 return 0;
1616}
1617
1618void
1619widget_destroy_tooltip(struct widget *parent)
1620{
1621 struct tooltip *tooltip = parent->tooltip;
1622
1623 parent->tooltip_count = 0;
1624 if (!tooltip)
1625 return;
1626
1627 if (tooltip->widget) {
1628 widget_destroy(tooltip->widget);
1629 window_destroy(tooltip->window);
1630 tooltip->widget = NULL;
1631 tooltip->window = NULL;
1632 }
1633
1634 close(tooltip->tooltip_fd);
1635 free(tooltip->entry);
1636 free(tooltip);
1637 parent->tooltip = NULL;
1638}
1639
1640static void
1641tooltip_func(struct task *task, uint32_t events)
1642{
1643 struct tooltip *tooltip =
1644 container_of(task, struct tooltip, tooltip_task);
1645 uint64_t exp;
1646
Martin Olsson8df662a2012-07-08 03:03:47 +02001647 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1648 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001649 window_create_tooltip(tooltip);
1650}
1651
1652#define TOOLTIP_TIMEOUT 500
1653static int
1654tooltip_timer_reset(struct tooltip *tooltip)
1655{
1656 struct itimerspec its;
1657
1658 its.it_interval.tv_sec = 0;
1659 its.it_interval.tv_nsec = 0;
1660 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1661 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1662 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1663 fprintf(stderr, "could not set timerfd\n: %m");
1664 return -1;
1665 }
1666
1667 return 0;
1668}
1669
1670int
1671widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1672{
1673 struct tooltip *tooltip = parent->tooltip;
1674
1675 parent->tooltip_count++;
1676 if (tooltip) {
1677 tooltip->x = x;
1678 tooltip->y = y;
1679 tooltip_timer_reset(tooltip);
1680 return 0;
1681 }
1682
1683 /* the handler might be triggered too fast via input device motion, so
1684 * we need this check here to make sure tooltip is fully initialized */
1685 if (parent->tooltip_count > 1)
1686 return 0;
1687
1688 tooltip = malloc(sizeof *tooltip);
1689 if (!tooltip)
1690 return -1;
1691
1692 parent->tooltip = tooltip;
1693 tooltip->parent = parent;
1694 tooltip->widget = NULL;
1695 tooltip->window = NULL;
1696 tooltip->x = x;
1697 tooltip->y = y;
1698 tooltip->entry = strdup(entry);
1699 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1700 if (tooltip->tooltip_fd < 0) {
1701 fprintf(stderr, "could not create timerfd\n: %m");
1702 return -1;
1703 }
1704
1705 tooltip->tooltip_task.run = tooltip_func;
1706 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1707 EPOLLIN, &tooltip->tooltip_task);
1708 tooltip_timer_reset(tooltip);
1709
1710 return 0;
1711}
1712
1713static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001714workspace_manager_state(void *data,
1715 struct workspace_manager *workspace_manager,
1716 uint32_t current,
1717 uint32_t count)
1718{
1719 struct display *display = data;
1720
1721 display->workspace = current;
1722 display->workspace_count = count;
1723}
1724
1725static const struct workspace_manager_listener workspace_manager_listener = {
1726 workspace_manager_state
1727};
1728
1729static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001730frame_resize_handler(struct widget *widget,
1731 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001732{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001733 struct frame *frame = data;
1734 struct widget *child = frame->child;
1735 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001736 struct display *display = widget->window->display;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001737 struct surface *surface = widget->window->main_surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001738 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001739 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001740 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001741 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001742 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001743
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001744 switch (widget->window->type) {
1745 case TYPE_FULLSCREEN:
1746 decoration_width = 0;
1747 decoration_height = 0;
1748
1749 allocation.x = 0;
1750 allocation.y = 0;
1751 allocation.width = width;
1752 allocation.height = height;
1753 opaque_margin = 0;
1754
1755 wl_list_for_each(button, &frame->buttons_list, link)
1756 button->widget->opaque = 1;
1757 break;
1758 case TYPE_MAXIMIZED:
1759 decoration_width = t->width * 2;
1760 decoration_height = t->width + t->titlebar_height;
1761
1762 allocation.x = t->width;
1763 allocation.y = t->titlebar_height;
1764 allocation.width = width - decoration_width;
1765 allocation.height = height - decoration_height;
1766
1767 opaque_margin = 0;
1768
1769 wl_list_for_each(button, &frame->buttons_list, link)
1770 button->widget->opaque = 0;
1771 break;
1772 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001773 decoration_width = (t->width + t->margin) * 2;
1774 decoration_height = t->width +
1775 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001776
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001777 allocation.x = t->width + t->margin;
1778 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001779 allocation.width = width - decoration_width;
1780 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001781
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001782 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001783
1784 wl_list_for_each(button, &frame->buttons_list, link)
1785 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001786 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001787 }
1788
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001789 widget_set_allocation(child, allocation.x, allocation.y,
1790 allocation.width, allocation.height);
1791
1792 if (child->resize_handler)
1793 child->resize_handler(child,
1794 allocation.width,
1795 allocation.height,
1796 child->user_data);
1797
Scott Moreauf7e498c2012-05-14 11:39:29 -06001798 width = child->allocation.width + decoration_width;
1799 height = child->allocation.height + decoration_height;
1800
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001801 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1802
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001803 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001804 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001805 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001806 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001807 shadow_margin, shadow_margin,
1808 width - 2 * shadow_margin,
1809 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001810 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001811 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001812 }
1813
Scott Moreauf7e498c2012-05-14 11:39:29 -06001814 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001815
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001816 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001817 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001818 opaque_margin, opaque_margin,
1819 widget->allocation.width - 2 * opaque_margin,
1820 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001821
1822 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001823 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1824 x_l = t->width + shadow_margin;
1825 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001826 wl_list_for_each(button, &frame->buttons_list, link) {
1827 const int button_padding = 4;
1828 w = cairo_image_surface_get_width(button->icon);
1829 h = cairo_image_surface_get_height(button->icon);
1830
1831 if (button->decoration == FRAME_BUTTON_FANCY)
1832 w += 10;
1833
1834 if (button->align == FRAME_BUTTON_LEFT) {
1835 widget_set_allocation(button->widget,
1836 x_l, y , w + 1, h + 1);
1837 x_l += w;
1838 x_l += button_padding;
1839 } else {
1840 x_r -= w;
1841 widget_set_allocation(button->widget,
1842 x_r, y , w + 1, h + 1);
1843 x_r -= button_padding;
1844 }
1845 }
1846}
1847
1848static int
1849frame_button_enter_handler(struct widget *widget,
1850 struct input *input, float x, float y, void *data)
1851{
1852 struct frame_button *frame_button = data;
1853
1854 widget_schedule_redraw(frame_button->widget);
1855 frame_button->state = FRAME_BUTTON_OVER;
1856
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001857 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001858}
1859
1860static void
1861frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1862{
1863 struct frame_button *frame_button = data;
1864
1865 widget_schedule_redraw(frame_button->widget);
1866 frame_button->state = FRAME_BUTTON_DEFAULT;
1867}
1868
1869static void
1870frame_button_button_handler(struct widget *widget,
1871 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001872 uint32_t button,
1873 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001874{
1875 struct frame_button *frame_button = data;
1876 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001877 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001878
1879 if (button != BTN_LEFT)
1880 return;
1881
1882 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001883 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001884 frame_button->state = FRAME_BUTTON_ACTIVE;
1885 widget_schedule_redraw(frame_button->widget);
1886
1887 if (frame_button->type == FRAME_BUTTON_ICON)
1888 window_show_frame_menu(window, input, time);
1889 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001890 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001891 frame_button->state = FRAME_BUTTON_DEFAULT;
1892 widget_schedule_redraw(frame_button->widget);
1893 break;
1894 }
1895
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001896 if (!was_pressed)
1897 return;
1898
Martin Minarik1998b152012-05-10 02:04:35 +02001899 switch (frame_button->type) {
1900 case FRAME_BUTTON_CLOSE:
1901 if (window->close_handler)
1902 window->close_handler(window->parent,
1903 window->user_data);
1904 else
1905 display_exit(window->display);
1906 break;
1907 case FRAME_BUTTON_MINIMIZE:
1908 fprintf(stderr,"Minimize stub\n");
1909 break;
1910 case FRAME_BUTTON_MAXIMIZE:
1911 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1912 break;
1913 default:
1914 /* Unknown operation */
1915 break;
1916 }
1917}
1918
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001919static int
1920frame_button_motion_handler(struct widget *widget,
1921 struct input *input, uint32_t time,
1922 float x, float y, void *data)
1923{
1924 struct frame_button *frame_button = data;
1925 enum frame_button_pointer previous_button_state = frame_button->state;
1926
1927 /* only track state for a pressed button */
1928 if (input->grab != widget)
1929 return CURSOR_LEFT_PTR;
1930
1931 if (x > widget->allocation.x &&
1932 x < (widget->allocation.x + widget->allocation.width) &&
1933 y > widget->allocation.y &&
1934 y < (widget->allocation.y + widget->allocation.height)) {
1935 frame_button->state = FRAME_BUTTON_ACTIVE;
1936 } else {
1937 frame_button->state = FRAME_BUTTON_DEFAULT;
1938 }
1939
1940 if (frame_button->state != previous_button_state)
1941 widget_schedule_redraw(frame_button->widget);
1942
1943 return CURSOR_LEFT_PTR;
1944}
1945
Martin Minarik1998b152012-05-10 02:04:35 +02001946static void
1947frame_button_redraw_handler(struct widget *widget, void *data)
1948{
1949 struct frame_button *frame_button = data;
1950 cairo_t *cr;
1951 int width, height, x, y;
1952 struct window *window = widget->window;
1953
1954 x = widget->allocation.x;
1955 y = widget->allocation.y;
1956 width = widget->allocation.width;
1957 height = widget->allocation.height;
1958
1959 if (!width)
1960 return;
1961 if (!height)
1962 return;
1963 if (widget->opaque)
1964 return;
1965
1966 cr = cairo_create(window->cairo_surface);
1967
1968 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1969 cairo_set_line_width(cr, 1);
1970
1971 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1972 cairo_rectangle (cr, x, y, 25, 16);
1973
1974 cairo_stroke_preserve(cr);
1975
1976 switch (frame_button->state) {
1977 case FRAME_BUTTON_DEFAULT:
1978 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1979 break;
1980 case FRAME_BUTTON_OVER:
1981 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1982 break;
1983 case FRAME_BUTTON_ACTIVE:
1984 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1985 break;
1986 }
1987
1988 cairo_fill (cr);
1989
1990 x += 4;
1991 }
1992
1993 cairo_set_source_surface(cr, frame_button->icon, x, y);
1994 cairo_paint(cr);
1995
1996 cairo_destroy(cr);
1997}
1998
1999static struct widget *
2000frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2001 enum frame_button_align align, enum frame_button_decoration style)
2002{
2003 struct frame_button *frame_button;
2004 const char *icon = data;
2005
2006 frame_button = malloc (sizeof *frame_button);
2007 memset(frame_button, 0, sizeof *frame_button);
2008
2009 frame_button->icon = cairo_image_surface_create_from_png(icon);
2010 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2011 frame_button->frame = frame;
2012 frame_button->type = type;
2013 frame_button->align = align;
2014 frame_button->decoration = style;
2015
2016 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2017
2018 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2019 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2020 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2021 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002022 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002023 return frame_button->widget;
2024}
2025
2026static void
2027frame_button_destroy(struct frame_button *frame_button)
2028{
2029 widget_destroy(frame_button->widget);
2030 wl_list_remove(&frame_button->link);
2031 cairo_surface_destroy(frame_button->icon);
2032 free(frame_button);
2033
2034 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002035}
2036
2037static void
2038frame_redraw_handler(struct widget *widget, void *data)
2039{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002040 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002041 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002042 struct theme *t = window->display->theme;
2043 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002044
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002045 if (window->type == TYPE_FULLSCREEN)
2046 return;
2047
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002048 cr = cairo_create(window->cairo_surface);
2049
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002050 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002051 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002052 if (window->type == TYPE_MAXIMIZED)
2053 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002054 theme_render_frame(t, cr, widget->allocation.width,
2055 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002056
2057 cairo_destroy(cr);
2058}
2059
2060static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002061frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002062{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002063 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002064 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002065 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002066
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002067 location = theme_get_location(t, input->sx, input->sy,
2068 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002069 frame->widget->allocation.height,
2070 window->type == TYPE_MAXIMIZED ?
2071 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002072
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002073 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002074 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002075 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002076 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002077 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002078 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002079 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002080 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002081 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002082 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002083 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002084 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002085 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002086 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002087 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002088 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002089 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002090 case THEME_LOCATION_EXTERIOR:
2091 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002092 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002093 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002094 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002095}
2096
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002097static void
2098frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002099{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002100 struct display *display;
2101
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002102 switch (index) {
2103 case 0: /* close */
2104 if (window->close_handler)
2105 window->close_handler(window->parent,
2106 window->user_data);
2107 else
2108 display_exit(window->display);
2109 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002110 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002111 display = window->display;
2112 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002113 workspace_manager_move_surface(
2114 display->workspace_manager,
2115 window->main_surface->surface,
2116 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002117 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002118 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002119 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002120 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002121 workspace_manager_move_surface(
2122 display->workspace_manager,
2123 window->main_surface->surface,
2124 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002125 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002126 case 3: /* fullscreen */
2127 /* we don't have a way to get out of fullscreen for now */
2128 if (window->fullscreen_handler)
2129 window->fullscreen_handler(window, window->user_data);
2130 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002131 }
2132}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002133
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002134void
2135window_show_frame_menu(struct window *window,
2136 struct input *input, uint32_t time)
2137{
2138 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002139 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002140
2141 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002142 "Close",
2143 "Move to workspace above", "Move to workspace below",
2144 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002145 };
2146
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002147 if (window->fullscreen_handler)
2148 count = ARRAY_LENGTH(entries);
2149 else
2150 count = ARRAY_LENGTH(entries) - 1;
2151
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002152 input_get_position(input, &x, &y);
2153 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002154 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002155}
2156
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002157static int
2158frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002159 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002160{
2161 return frame_get_pointer_image_for_location(data, input);
2162}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002163
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002164static int
2165frame_motion_handler(struct widget *widget,
2166 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002167 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002168{
2169 return frame_get_pointer_image_for_location(data, input);
2170}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002171
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002172static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002173frame_button_handler(struct widget *widget,
2174 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002175 uint32_t button, enum wl_pointer_button_state state,
2176 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177
2178{
2179 struct frame *frame = data;
2180 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002181 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002182 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002183
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002184 location = theme_get_location(display->theme, input->sx, input->sy,
2185 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002186 frame->widget->allocation.height,
2187 window->type == TYPE_MAXIMIZED ?
2188 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002189
Daniel Stone4dbadb12012-05-30 16:31:51 +01002190 if (window->display->shell && button == BTN_LEFT &&
2191 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002193 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194 if (!window->shell_surface)
2195 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002196 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002198 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002199 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002200 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002201 case THEME_LOCATION_RESIZING_TOP:
2202 case THEME_LOCATION_RESIZING_BOTTOM:
2203 case THEME_LOCATION_RESIZING_LEFT:
2204 case THEME_LOCATION_RESIZING_RIGHT:
2205 case THEME_LOCATION_RESIZING_TOP_LEFT:
2206 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2207 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2208 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002209 if (!window->shell_surface)
2210 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002211 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002212
Pekka Paalanen99436862012-11-19 17:15:59 +02002213 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002214 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002215 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002216 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002217 break;
2218 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002219 } else if (button == BTN_RIGHT &&
2220 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002221 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222 }
2223}
2224
2225struct widget *
2226frame_create(struct window *window, void *data)
2227{
2228 struct frame *frame;
2229
2230 frame = malloc(sizeof *frame);
2231 memset(frame, 0, sizeof *frame);
2232
2233 frame->widget = window_add_widget(window, frame);
2234 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002235
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002236 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2237 widget_set_resize_handler(frame->widget, frame_resize_handler);
2238 widget_set_enter_handler(frame->widget, frame_enter_handler);
2239 widget_set_motion_handler(frame->widget, frame_motion_handler);
2240 widget_set_button_handler(frame->widget, frame_button_handler);
2241
Martin Minarik1998b152012-05-10 02:04:35 +02002242 /* Create empty list for frame buttons */
2243 wl_list_init(&frame->buttons_list);
2244
2245 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2246 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2247
2248 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2249 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2250
2251 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2252 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2253
2254 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2255 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2256
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002257 window->frame = frame;
2258
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002259 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002260}
2261
Kristian Høgsberga1627922012-06-20 17:30:03 -04002262void
2263frame_set_child_size(struct widget *widget, int child_width, int child_height)
2264{
2265 struct display *display = widget->window->display;
2266 struct theme *t = display->theme;
2267 int decoration_width, decoration_height;
2268 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002269 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002270
2271 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002272 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002273 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002274 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002275
2276 width = child_width + decoration_width;
2277 height = child_height + decoration_height;
2278 } else {
2279 width = child_width;
2280 height = child_height;
2281 }
2282
2283 window_schedule_resize(widget->window, width, height);
2284}
2285
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002286static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002287frame_destroy(struct frame *frame)
2288{
Martin Minarik1998b152012-05-10 02:04:35 +02002289 struct frame_button *button, *tmp;
2290
2291 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2292 frame_button_destroy(button);
2293
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002294 /* frame->child must be destroyed by the application */
2295 widget_destroy(frame->widget);
2296 free(frame);
2297}
2298
2299static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002300input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002301 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002302{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002303 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002304 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002305
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002306 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002307 return;
2308
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002309 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002310 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002311 widget = old;
2312 if (input->grab)
2313 widget = input->grab;
2314 if (widget->leave_handler)
2315 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002316 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002317 }
2318
2319 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002320 widget = focus;
2321 if (input->grab)
2322 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002323 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002324 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002325 cursor = widget->enter_handler(focus, input, x, y,
2326 widget->user_data);
2327 else
2328 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002329
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002330 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002331 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002332}
2333
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002334void
2335input_grab(struct input *input, struct widget *widget, uint32_t button)
2336{
2337 input->grab = widget;
2338 input->grab_button = button;
2339}
2340
2341void
2342input_ungrab(struct input *input)
2343{
2344 struct widget *widget;
2345
2346 input->grab = NULL;
2347 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002348 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002349 input->sx, input->sy);
2350 input_set_focus_widget(input, widget, input->sx, input->sy);
2351 }
2352}
2353
2354static void
2355input_remove_pointer_focus(struct input *input)
2356{
2357 struct window *window = input->pointer_focus;
2358
2359 if (!window)
2360 return;
2361
2362 input_set_focus_widget(input, NULL, 0, 0);
2363
2364 input->pointer_focus = NULL;
2365 input->current_cursor = CURSOR_UNSET;
2366}
2367
2368static void
2369pointer_handle_enter(void *data, struct wl_pointer *pointer,
2370 uint32_t serial, struct wl_surface *surface,
2371 wl_fixed_t sx_w, wl_fixed_t sy_w)
2372{
2373 struct input *input = data;
2374 struct window *window;
2375 struct widget *widget;
2376 float sx = wl_fixed_to_double(sx_w);
2377 float sy = wl_fixed_to_double(sy_w);
2378
2379 if (!surface) {
2380 /* enter event for a window we've just destroyed */
2381 return;
2382 }
2383
2384 input->display->serial = serial;
2385 input->pointer_enter_serial = serial;
2386 input->pointer_focus = wl_surface_get_user_data(surface);
2387 window = input->pointer_focus;
2388
Pekka Paalanen99436862012-11-19 17:15:59 +02002389 if (window->resizing) {
2390 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002391 /* Schedule a redraw to free the pool */
2392 window_schedule_redraw(window);
2393 }
2394
2395 input->sx = sx;
2396 input->sy = sy;
2397
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002398 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002399 input_set_focus_widget(input, widget, sx, sy);
2400}
2401
2402static void
2403pointer_handle_leave(void *data, struct wl_pointer *pointer,
2404 uint32_t serial, struct wl_surface *surface)
2405{
2406 struct input *input = data;
2407
2408 input->display->serial = serial;
2409 input_remove_pointer_focus(input);
2410}
2411
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002412static void
Daniel Stone37816df2012-05-16 18:45:18 +01002413pointer_handle_motion(void *data, struct wl_pointer *pointer,
2414 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002415{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002416 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002417 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002418 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002419 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002420 float sx = wl_fixed_to_double(sx_w);
2421 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002422
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002423 input->sx = sx;
2424 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002425
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002426 if (!window)
2427 return;
2428
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002429 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002430 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002431 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002432 }
2433
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002434 if (input->grab)
2435 widget = input->grab;
2436 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002437 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002438 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002439 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002440 input, time, sx, sy,
2441 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002442 else
2443 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002444
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002445 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002446}
2447
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002448static void
Daniel Stone37816df2012-05-16 18:45:18 +01002449pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002450 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002451{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002452 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002453 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002454 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002455
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002456 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002457 if (input->focus_widget && input->grab == NULL &&
2458 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002459 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002460
Neil Roberts6b28aad2012-01-23 19:11:18 +00002461 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002462 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002463 (*widget->button_handler)(widget,
2464 input, time,
2465 button, state,
2466 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002467
Daniel Stone4dbadb12012-05-30 16:31:51 +01002468 if (input->grab && input->grab_button == button &&
2469 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002470 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002471}
2472
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002473static void
Daniel Stone37816df2012-05-16 18:45:18 +01002474pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002475 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002476{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002477 struct input *input = data;
2478 struct widget *widget;
2479
2480 widget = input->focus_widget;
2481 if (input->grab)
2482 widget = input->grab;
2483 if (widget && widget->axis_handler)
2484 (*widget->axis_handler)(widget,
2485 input, time,
2486 axis, value,
2487 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002488}
2489
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002490static const struct wl_pointer_listener pointer_listener = {
2491 pointer_handle_enter,
2492 pointer_handle_leave,
2493 pointer_handle_motion,
2494 pointer_handle_button,
2495 pointer_handle_axis,
2496};
2497
2498static void
2499input_remove_keyboard_focus(struct input *input)
2500{
2501 struct window *window = input->keyboard_focus;
2502 struct itimerspec its;
2503
2504 its.it_interval.tv_sec = 0;
2505 its.it_interval.tv_nsec = 0;
2506 its.it_value.tv_sec = 0;
2507 its.it_value.tv_nsec = 0;
2508 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2509
2510 if (!window)
2511 return;
2512
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002513 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002514 if (window->keyboard_focus_handler)
2515 (*window->keyboard_focus_handler)(window, NULL,
2516 window->user_data);
2517
2518 input->keyboard_focus = NULL;
2519}
2520
2521static void
2522keyboard_repeat_func(struct task *task, uint32_t events)
2523{
2524 struct input *input =
2525 container_of(task, struct input, repeat_task);
2526 struct window *window = input->keyboard_focus;
2527 uint64_t exp;
2528
2529 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2530 /* If we change the timer between the fd becoming
2531 * readable and getting here, there'll be nothing to
2532 * read and we get EAGAIN. */
2533 return;
2534
2535 if (window && window->key_handler) {
2536 (*window->key_handler)(window, input, input->repeat_time,
2537 input->repeat_key, input->repeat_sym,
2538 WL_KEYBOARD_KEY_STATE_PRESSED,
2539 window->user_data);
2540 }
2541}
2542
2543static void
2544keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2545 uint32_t format, int fd, uint32_t size)
2546{
2547 struct input *input = data;
2548 char *map_str;
2549
2550 if (!data) {
2551 close(fd);
2552 return;
2553 }
2554
2555 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2556 close(fd);
2557 return;
2558 }
2559
2560 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2561 if (map_str == MAP_FAILED) {
2562 close(fd);
2563 return;
2564 }
2565
2566 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2567 map_str,
2568 XKB_KEYMAP_FORMAT_TEXT_V1,
2569 0);
2570 munmap(map_str, size);
2571 close(fd);
2572
2573 if (!input->xkb.keymap) {
2574 fprintf(stderr, "failed to compile keymap\n");
2575 return;
2576 }
2577
2578 input->xkb.state = xkb_state_new(input->xkb.keymap);
2579 if (!input->xkb.state) {
2580 fprintf(stderr, "failed to create XKB state\n");
2581 xkb_map_unref(input->xkb.keymap);
2582 input->xkb.keymap = NULL;
2583 return;
2584 }
2585
2586 input->xkb.control_mask =
2587 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2588 input->xkb.alt_mask =
2589 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2590 input->xkb.shift_mask =
2591 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2592}
2593
2594static void
2595keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2596 uint32_t serial, struct wl_surface *surface,
2597 struct wl_array *keys)
2598{
2599 struct input *input = data;
2600 struct window *window;
2601
2602 input->display->serial = serial;
2603 input->keyboard_focus = wl_surface_get_user_data(surface);
2604
2605 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002606 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002607 if (window->keyboard_focus_handler)
2608 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002609 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002610}
2611
2612static void
2613keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2614 uint32_t serial, struct wl_surface *surface)
2615{
2616 struct input *input = data;
2617
2618 input->display->serial = serial;
2619 input_remove_keyboard_focus(input);
2620}
2621
Scott Moreau210d0792012-03-22 10:47:01 -06002622static void
Daniel Stone37816df2012-05-16 18:45:18 +01002623keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002624 uint32_t serial, uint32_t time, uint32_t key,
2625 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002626{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002627 struct input *input = data;
2628 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002629 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002630 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002631 const xkb_keysym_t *syms;
2632 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002633 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002634
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002635 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002636 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002637 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002638 return;
2639
Daniel Stone97f68542012-05-30 16:32:01 +01002640 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002641
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002642 sym = XKB_KEY_NoSymbol;
2643 if (num_syms == 1)
2644 sym = syms[0];
2645
2646 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002647 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002648 window_set_maximized(window,
2649 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002650 } else if (sym == XKB_KEY_F11 &&
2651 window->fullscreen_handler &&
2652 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2653 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002654 } else if (sym == XKB_KEY_F4 &&
2655 input->modifiers == MOD_ALT_MASK &&
2656 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2657 if (window->close_handler)
2658 window->close_handler(window->parent,
2659 window->user_data);
2660 else
2661 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002662 } else if (window->key_handler) {
2663 (*window->key_handler)(window, input, time, key,
2664 sym, state, window->user_data);
2665 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002666
2667 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2668 key == input->repeat_key) {
2669 its.it_interval.tv_sec = 0;
2670 its.it_interval.tv_nsec = 0;
2671 its.it_value.tv_sec = 0;
2672 its.it_value.tv_nsec = 0;
2673 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2674 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2675 input->repeat_sym = sym;
2676 input->repeat_key = key;
2677 input->repeat_time = time;
2678 its.it_interval.tv_sec = 0;
2679 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2680 its.it_value.tv_sec = 0;
2681 its.it_value.tv_nsec = 400 * 1000 * 1000;
2682 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2683 }
2684}
2685
2686static void
Daniel Stone351eb612012-05-31 15:27:47 -04002687keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2688 uint32_t serial, uint32_t mods_depressed,
2689 uint32_t mods_latched, uint32_t mods_locked,
2690 uint32_t group)
2691{
2692 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002693 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002694
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002695 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2696 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002697 mask = xkb_state_serialize_mods(input->xkb.state,
2698 XKB_STATE_DEPRESSED |
2699 XKB_STATE_LATCHED);
2700 input->modifiers = 0;
2701 if (mask & input->xkb.control_mask)
2702 input->modifiers |= MOD_CONTROL_MASK;
2703 if (mask & input->xkb.alt_mask)
2704 input->modifiers |= MOD_ALT_MASK;
2705 if (mask & input->xkb.shift_mask)
2706 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002707}
2708
Daniel Stone37816df2012-05-16 18:45:18 +01002709static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002710 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002711 keyboard_handle_enter,
2712 keyboard_handle_leave,
2713 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002714 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002715};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002716
2717static void
Daniel Stone37816df2012-05-16 18:45:18 +01002718seat_handle_capabilities(void *data, struct wl_seat *seat,
2719 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002720{
Daniel Stone37816df2012-05-16 18:45:18 +01002721 struct input *input = data;
2722
2723 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2724 input->pointer = wl_seat_get_pointer(seat);
2725 wl_pointer_set_user_data(input->pointer, input);
2726 wl_pointer_add_listener(input->pointer, &pointer_listener,
2727 input);
2728 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2729 wl_pointer_destroy(input->pointer);
2730 input->pointer = NULL;
2731 }
2732
2733 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2734 input->keyboard = wl_seat_get_keyboard(seat);
2735 wl_keyboard_set_user_data(input->keyboard, input);
2736 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2737 input);
2738 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2739 wl_keyboard_destroy(input->keyboard);
2740 input->keyboard = NULL;
2741 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002742}
2743
Daniel Stone37816df2012-05-16 18:45:18 +01002744static const struct wl_seat_listener seat_listener = {
2745 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002746};
2747
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002748void
2749input_get_position(struct input *input, int32_t *x, int32_t *y)
2750{
2751 *x = input->sx;
2752 *y = input->sy;
2753}
2754
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002755struct display *
2756input_get_display(struct input *input)
2757{
2758 return input->display;
2759}
2760
Daniel Stone37816df2012-05-16 18:45:18 +01002761struct wl_seat *
2762input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002763{
Daniel Stone37816df2012-05-16 18:45:18 +01002764 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002765}
2766
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002767uint32_t
2768input_get_modifiers(struct input *input)
2769{
2770 return input->modifiers;
2771}
2772
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002773struct widget *
2774input_get_focus_widget(struct input *input)
2775{
2776 return input->focus_widget;
2777}
2778
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002779struct data_offer {
2780 struct wl_data_offer *offer;
2781 struct input *input;
2782 struct wl_array types;
2783 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002784
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002785 struct task io_task;
2786 int fd;
2787 data_func_t func;
2788 int32_t x, y;
2789 void *user_data;
2790};
2791
2792static void
2793data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2794{
2795 struct data_offer *offer = data;
2796 char **p;
2797
2798 p = wl_array_add(&offer->types, sizeof *p);
2799 *p = strdup(type);
2800}
2801
2802static const struct wl_data_offer_listener data_offer_listener = {
2803 data_offer_offer,
2804};
2805
2806static void
2807data_offer_destroy(struct data_offer *offer)
2808{
2809 char **p;
2810
2811 offer->refcount--;
2812 if (offer->refcount == 0) {
2813 wl_data_offer_destroy(offer->offer);
2814 for (p = offer->types.data; *p; p++)
2815 free(*p);
2816 wl_array_release(&offer->types);
2817 free(offer);
2818 }
2819}
2820
2821static void
2822data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002823 struct wl_data_device *data_device,
2824 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002825{
2826 struct data_offer *offer;
2827
2828 offer = malloc(sizeof *offer);
2829
2830 wl_array_init(&offer->types);
2831 offer->refcount = 1;
2832 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002833 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002834 wl_data_offer_add_listener(offer->offer,
2835 &data_offer_listener, offer);
2836}
2837
2838static void
2839data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002840 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002841 wl_fixed_t x_w, wl_fixed_t y_w,
2842 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002843{
2844 struct input *input = data;
2845 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002846 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002847 float x = wl_fixed_to_double(x_w);
2848 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002849 char **p;
2850
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002851 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002852 window = wl_surface_get_user_data(surface);
2853 input->pointer_focus = window;
2854
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002855 if (offer) {
2856 input->drag_offer = wl_data_offer_get_user_data(offer);
2857
2858 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2859 *p = NULL;
2860
2861 types_data = input->drag_offer->types.data;
2862 } else {
2863 input->drag_offer = NULL;
2864 types_data = NULL;
2865 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002866
2867 window = input->pointer_focus;
2868 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002869 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002870 window->user_data);
2871}
2872
2873static void
2874data_device_leave(void *data, struct wl_data_device *data_device)
2875{
2876 struct input *input = data;
2877
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002878 if (input->drag_offer) {
2879 data_offer_destroy(input->drag_offer);
2880 input->drag_offer = NULL;
2881 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002882}
2883
2884static void
2885data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002886 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002887{
2888 struct input *input = data;
2889 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002890 float x = wl_fixed_to_double(x_w);
2891 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002892 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002893
2894 input->sx = x;
2895 input->sy = y;
2896
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002897 if (input->drag_offer)
2898 types_data = input->drag_offer->types.data;
2899 else
2900 types_data = NULL;
2901
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002902 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002903 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002904 window->user_data);
2905}
2906
2907static void
2908data_device_drop(void *data, struct wl_data_device *data_device)
2909{
2910 struct input *input = data;
2911 struct window *window = input->pointer_focus;
2912
2913 if (window->drop_handler)
2914 window->drop_handler(window, input,
2915 input->sx, input->sy, window->user_data);
2916}
2917
2918static void
2919data_device_selection(void *data,
2920 struct wl_data_device *wl_data_device,
2921 struct wl_data_offer *offer)
2922{
2923 struct input *input = data;
2924 char **p;
2925
2926 if (input->selection_offer)
2927 data_offer_destroy(input->selection_offer);
2928
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002929 if (offer) {
2930 input->selection_offer = wl_data_offer_get_user_data(offer);
2931 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2932 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002933 } else {
2934 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002935 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002936}
2937
2938static const struct wl_data_device_listener data_device_listener = {
2939 data_device_data_offer,
2940 data_device_enter,
2941 data_device_leave,
2942 data_device_motion,
2943 data_device_drop,
2944 data_device_selection
2945};
2946
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002947static void
2948input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002949{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002950 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002951 struct wl_cursor *cursor;
2952 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002953
Daniel Stone80972742012-11-07 17:51:39 +11002954 if (!input->pointer)
2955 return;
2956
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002957 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002958 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002959 return;
2960
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002961 if (index >= (int) cursor->image_count) {
2962 fprintf(stderr, "cursor index out of range\n");
2963 return;
2964 }
2965
2966 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002967 buffer = wl_cursor_image_get_buffer(image);
2968 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002969 return;
2970
Kristian Høgsbergae277372012-08-01 09:41:08 -04002971 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002972 input->pointer_surface,
2973 image->hotspot_x, image->hotspot_y);
2974 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2975 wl_surface_damage(input->pointer_surface, 0, 0,
2976 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002977 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002978}
2979
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002980static const struct wl_callback_listener pointer_surface_listener;
2981
2982static void
2983pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2984 uint32_t time)
2985{
2986 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002987 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002988 int i;
2989
2990 if (callback) {
2991 assert(callback == input->cursor_frame_cb);
2992 wl_callback_destroy(callback);
2993 input->cursor_frame_cb = NULL;
2994 }
2995
Daniel Stone80972742012-11-07 17:51:39 +11002996 if (!input->pointer)
2997 return;
2998
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002999 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003000 wl_pointer_set_cursor(input->pointer,
3001 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003002 NULL, 0, 0);
3003 return;
3004 }
3005
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003006 if (input->current_cursor == CURSOR_UNSET)
3007 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003008 cursor = input->display->cursors[input->current_cursor];
3009 if (!cursor)
3010 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003011
3012 /* FIXME We don't have the current time on the first call so we set
3013 * the animation start to the time of the first frame callback. */
3014 if (time == 0)
3015 input->cursor_anim_start = 0;
3016 else if (input->cursor_anim_start == 0)
3017 input->cursor_anim_start = time;
3018
3019 if (time == 0 || input->cursor_anim_start == 0)
3020 i = 0;
3021 else
3022 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3023
Pekka Paalanenbc106382012-10-10 12:49:31 +03003024 if (cursor->image_count > 1) {
3025 input->cursor_frame_cb =
3026 wl_surface_frame(input->pointer_surface);
3027 wl_callback_add_listener(input->cursor_frame_cb,
3028 &pointer_surface_listener, input);
3029 }
3030
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003031 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003032}
3033
3034static const struct wl_callback_listener pointer_surface_listener = {
3035 pointer_surface_frame_callback
3036};
3037
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003038void
3039input_set_pointer_image(struct input *input, int pointer)
3040{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003041 int force = 0;
3042
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003043 if (!input->pointer)
3044 return;
3045
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003046 if (input->pointer_enter_serial > input->cursor_serial)
3047 force = 1;
3048
3049 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003050 return;
3051
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003052 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003053 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003054 if (!input->cursor_frame_cb)
3055 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003056 else if (force) {
3057 /* The current frame callback may be stuck if, for instance,
3058 * the set cursor request was processed by the server after
3059 * this client lost the focus. In this case the cursor surface
3060 * might not be mapped and the frame callback wouldn't ever
3061 * complete. Send a set_cursor and attach to try to map the
3062 * cursor surface again so that the callback will finish */
3063 input_set_pointer_image_index(input, 0);
3064 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003065}
3066
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003067struct wl_data_device *
3068input_get_data_device(struct input *input)
3069{
3070 return input->data_device;
3071}
3072
3073void
3074input_set_selection(struct input *input,
3075 struct wl_data_source *source, uint32_t time)
3076{
3077 wl_data_device_set_selection(input->data_device, source, time);
3078}
3079
3080void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003081input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003082{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003083 wl_data_offer_accept(input->drag_offer->offer,
3084 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003085}
3086
3087static void
3088offer_io_func(struct task *task, uint32_t events)
3089{
3090 struct data_offer *offer =
3091 container_of(task, struct data_offer, io_task);
3092 unsigned int len;
3093 char buffer[4096];
3094
3095 len = read(offer->fd, buffer, sizeof buffer);
3096 offer->func(buffer, len,
3097 offer->x, offer->y, offer->user_data);
3098
3099 if (len == 0) {
3100 close(offer->fd);
3101 data_offer_destroy(offer);
3102 }
3103}
3104
3105static void
3106data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3107 data_func_t func, void *user_data)
3108{
3109 int p[2];
3110
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003111 if (pipe2(p, O_CLOEXEC) == -1)
3112 return;
3113
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003114 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3115 close(p[1]);
3116
3117 offer->io_task.run = offer_io_func;
3118 offer->fd = p[0];
3119 offer->func = func;
3120 offer->refcount++;
3121 offer->user_data = user_data;
3122
3123 display_watch_fd(offer->input->display,
3124 offer->fd, EPOLLIN, &offer->io_task);
3125}
3126
3127void
3128input_receive_drag_data(struct input *input, const char *mime_type,
3129 data_func_t func, void *data)
3130{
3131 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3132 input->drag_offer->x = input->sx;
3133 input->drag_offer->y = input->sy;
3134}
3135
3136int
3137input_receive_selection_data(struct input *input, const char *mime_type,
3138 data_func_t func, void *data)
3139{
3140 char **p;
3141
3142 if (input->selection_offer == NULL)
3143 return -1;
3144
3145 for (p = input->selection_offer->types.data; *p; p++)
3146 if (strcmp(mime_type, *p) == 0)
3147 break;
3148
3149 if (*p == NULL)
3150 return -1;
3151
3152 data_offer_receive_data(input->selection_offer,
3153 mime_type, func, data);
3154 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003155}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003156
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003157int
3158input_receive_selection_data_to_fd(struct input *input,
3159 const char *mime_type, int fd)
3160{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003161 if (input->selection_offer)
3162 wl_data_offer_receive(input->selection_offer->offer,
3163 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003164
3165 return 0;
3166}
3167
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003168void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003169window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003170{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003171 if (!window->shell_surface)
3172 return;
3173
Daniel Stone37816df2012-05-16 18:45:18 +01003174 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003175}
3176
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003177static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003178surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003179{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003180 struct widget *widget = surface->widget;
3181 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003182
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003183 if (surface->input_region) {
3184 wl_region_destroy(surface->input_region);
3185 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003186 }
3187
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003188 if (surface->opaque_region)
3189 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003190
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003191 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003192
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003193 if (widget->resize_handler)
3194 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003195 widget->allocation.width,
3196 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003197 widget->user_data);
3198
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003199 if (surface->allocation.width != widget->allocation.width ||
3200 surface->allocation.height != widget->allocation.height) {
3201 surface->allocation = widget->allocation;
3202 window_schedule_redraw(widget->window);
3203 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003204
3205 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003206 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003207 widget->allocation.width,
3208 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003209}
3210
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003211static void
3212idle_resize(struct window *window)
3213{
3214 window->resize_needed = 0;
3215
3216 widget_set_allocation(window->main_surface->widget,
3217 window->pending_allocation.x,
3218 window->pending_allocation.y,
3219 window->pending_allocation.width,
3220 window->pending_allocation.height);
3221
3222 surface_resize(window->main_surface);
3223}
3224
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003225void
3226window_schedule_resize(struct window *window, int width, int height)
3227{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003228 window->pending_allocation.x = 0;
3229 window->pending_allocation.y = 0;
3230 window->pending_allocation.width = width;
3231 window->pending_allocation.height = height;
3232
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003233 if (window->min_allocation.width == 0)
3234 window->min_allocation = window->pending_allocation;
3235 if (window->pending_allocation.width < window->min_allocation.width)
3236 window->pending_allocation.width = window->min_allocation.width;
3237 if (window->pending_allocation.height < window->min_allocation.height)
3238 window->pending_allocation.height = window->min_allocation.height;
3239
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003240 window->resize_needed = 1;
3241 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003242}
3243
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003244void
3245widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3246{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003247 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003248}
3249
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003250static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003251handle_ping(void *data, struct wl_shell_surface *shell_surface,
3252 uint32_t serial)
3253{
3254 wl_shell_surface_pong(shell_surface, serial);
3255}
3256
3257static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003258handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003259 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003260{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003261 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003262
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003263 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003264 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003265}
3266
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003267static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003268menu_destroy(struct menu *menu)
3269{
3270 widget_destroy(menu->widget);
3271 window_destroy(menu->window);
3272 free(menu);
3273}
3274
3275static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003276handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3277{
3278 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003279 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003280
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003281 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003282 * device. Or just use wl_callback. And this really needs to
3283 * be a window vfunc that the menu can set. And we need the
3284 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003285
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003286 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003287 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003288 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003289}
3290
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003291static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003292 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003293 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003294 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003295};
3296
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003297void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003298window_get_allocation(struct window *window,
3299 struct rectangle *allocation)
3300{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003301 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003302}
3303
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003304static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003305widget_redraw(struct widget *widget)
3306{
3307 struct widget *child;
3308
3309 if (widget->redraw_handler)
3310 widget->redraw_handler(widget, widget->user_data);
3311 wl_list_for_each(child, &widget->child_list, link)
3312 widget_redraw(child);
3313}
3314
3315static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003316frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3317{
3318 struct window *window = data;
3319
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003320 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003321 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003322 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003323 window->redraw_scheduled = 0;
3324 if (window->redraw_needed)
3325 window_schedule_redraw(window);
3326}
3327
3328static const struct wl_callback_listener listener = {
3329 frame_callback
3330};
3331
3332static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003333idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003334{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003335 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003336
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003337 if (window->resize_needed)
3338 idle_resize(window);
3339
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003340 window_create_surface(window);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003341 widget_redraw(window->main_surface->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003342 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003343 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003344
Pekka Paalanen4e373742013-02-13 16:17:13 +02003345 window->frame_cb = wl_surface_frame(window->main_surface->surface);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003346 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003347 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003348}
3349
3350void
3351window_schedule_redraw(struct window *window)
3352{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003353 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003354 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003355 window->redraw_task.run = idle_redraw;
3356 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003357 window->redraw_scheduled = 1;
3358 }
3359}
3360
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003361int
3362window_is_fullscreen(struct window *window)
3363{
3364 return window->type == TYPE_FULLSCREEN;
3365}
3366
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003367void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003368window_set_fullscreen(struct window *window, int fullscreen)
3369{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003370 if (!window->display->shell)
3371 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003372
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003373 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003374 return;
3375
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003376 if (fullscreen) {
3377 window->type = TYPE_FULLSCREEN;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003378 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003379 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003380 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003381 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003382 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003383 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003384 wl_shell_surface_set_toplevel(window->shell_surface);
3385 window_schedule_resize(window,
3386 window->saved_allocation.width,
3387 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003388 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003389}
3390
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003391void
3392window_set_fullscreen_method(struct window *window,
3393 enum wl_shell_surface_fullscreen_method method)
3394{
3395 window->fullscreen_method = method;
3396}
3397
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003398int
3399window_is_maximized(struct window *window)
3400{
3401 return window->type == TYPE_MAXIMIZED;
3402}
3403
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003404void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003405window_set_maximized(struct window *window, int maximized)
3406{
3407 if (!window->display->shell)
3408 return;
3409
3410 if ((window->type == TYPE_MAXIMIZED) == maximized)
3411 return;
3412
3413 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003414 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003415 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3416 window->type = TYPE_MAXIMIZED;
3417 } else {
3418 wl_shell_surface_set_toplevel(window->shell_surface);
3419 window->type = TYPE_TOPLEVEL;
3420 window_schedule_resize(window,
3421 window->saved_allocation.width,
3422 window->saved_allocation.height);
3423 }
3424}
3425
3426void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003427window_set_user_data(struct window *window, void *data)
3428{
3429 window->user_data = data;
3430}
3431
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003432void *
3433window_get_user_data(struct window *window)
3434{
3435 return window->user_data;
3436}
3437
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003438void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003439window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003440 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003441{
3442 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003443}
3444
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003445void
3446window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003447 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003448{
3449 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003450}
3451
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003452void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003453window_set_data_handler(struct window *window, window_data_handler_t handler)
3454{
3455 window->data_handler = handler;
3456}
3457
3458void
3459window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3460{
3461 window->drop_handler = handler;
3462}
3463
3464void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003465window_set_close_handler(struct window *window,
3466 window_close_handler_t handler)
3467{
3468 window->close_handler = handler;
3469}
3470
3471void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003472window_set_fullscreen_handler(struct window *window,
3473 window_fullscreen_handler_t handler)
3474{
3475 window->fullscreen_handler = handler;
3476}
3477
3478void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003479window_set_output_handler(struct window *window,
3480 window_output_handler_t handler)
3481{
3482 window->output_handler = handler;
3483}
3484
3485void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003486window_set_title(struct window *window, const char *title)
3487{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003488 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003489 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003490 if (window->shell_surface)
3491 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003492}
3493
3494const char *
3495window_get_title(struct window *window)
3496{
3497 return window->title;
3498}
3499
3500void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003501window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3502{
3503 struct text_cursor_position *text_cursor_position =
3504 window->display->text_cursor_position;
3505
Scott Moreau9295ce02012-06-01 12:46:10 -06003506 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003507 return;
3508
3509 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003510 window->main_surface->surface,
3511 wl_fixed_from_int(x),
3512 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003513}
3514
3515void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003516window_damage(struct window *window, int32_t x, int32_t y,
3517 int32_t width, int32_t height)
3518{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003519 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003520}
3521
Casey Dahlin9074db52012-04-19 22:50:09 -04003522static void
3523surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003524 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003525{
Rob Bradford7507b572012-05-15 17:55:34 +01003526 struct window *window = data;
3527 struct output *output;
3528 struct output *output_found = NULL;
3529 struct window_output *window_output;
3530
3531 wl_list_for_each(output, &window->display->output_list, link) {
3532 if (output->output == wl_output) {
3533 output_found = output;
3534 break;
3535 }
3536 }
3537
3538 if (!output_found)
3539 return;
3540
3541 window_output = malloc (sizeof *window_output);
3542 window_output->output = output_found;
3543
3544 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003545
3546 if (window->output_handler)
3547 window->output_handler(window, output_found, 1,
3548 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003549}
3550
3551static void
3552surface_leave(void *data,
3553 struct wl_surface *wl_surface, struct wl_output *output)
3554{
Rob Bradford7507b572012-05-15 17:55:34 +01003555 struct window *window = data;
3556 struct window_output *window_output;
3557 struct window_output *window_output_found = NULL;
3558
3559 wl_list_for_each(window_output, &window->window_output_list, link) {
3560 if (window_output->output->output == output) {
3561 window_output_found = window_output;
3562 break;
3563 }
3564 }
3565
3566 if (window_output_found) {
3567 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003568
3569 if (window->output_handler)
3570 window->output_handler(window, window_output->output,
3571 0, window->user_data);
3572
Rob Bradford7507b572012-05-15 17:55:34 +01003573 free(window_output_found);
3574 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003575}
3576
3577static const struct wl_surface_listener surface_listener = {
3578 surface_enter,
3579 surface_leave
3580};
3581
Pekka Paalanen4e373742013-02-13 16:17:13 +02003582static struct surface *
3583surface_create(struct window *window)
3584{
3585 struct display *display = window->display;
3586 struct surface *surface;
3587
3588 surface = calloc(1, sizeof *surface);
3589 if (!surface)
3590 return NULL;
3591
3592 surface->window = window;
3593 surface->surface = wl_compositor_create_surface(display->compositor);
3594 wl_surface_add_listener(surface->surface, &surface_listener, window);
3595
3596 return surface;
3597}
3598
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003599static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003600window_create_internal(struct display *display,
3601 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003602{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003603 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003604 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003605
3606 window = malloc(sizeof *window);
3607 if (window == NULL)
3608 return NULL;
3609
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003610 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003611 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003612 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003613
3614 surface = surface_create(window);
3615 window->main_surface = surface;
3616
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003617 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003618 window->shell_surface =
3619 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003620 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003621 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003622
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003623 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003624 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003625 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003626
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003627 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003628#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003629 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003630#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003631 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003632#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003633 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003634 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003635
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003636 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003637 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003638 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003639
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003640 if (window->shell_surface) {
3641 wl_shell_surface_set_user_data(window->shell_surface, window);
3642 wl_shell_surface_add_listener(window->shell_surface,
3643 &shell_surface_listener, window);
3644 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003645
Rob Bradford7507b572012-05-15 17:55:34 +01003646 wl_list_init (&window->window_output_list);
3647
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003648 return window;
3649}
3650
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003651struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003652window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003653{
3654 struct window *window;
3655
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003656 window = window_create_internal(display, NULL, TYPE_NONE);
3657 if (!window)
3658 return NULL;
3659
3660 return window;
3661}
3662
3663struct window *
3664window_create_custom(struct display *display)
3665{
3666 struct window *window;
3667
3668 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003669 if (!window)
3670 return NULL;
3671
3672 return window;
3673}
3674
3675struct window *
3676window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003677 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003678{
3679 struct window *window;
3680
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003681 window = window_create_internal(parent->display,
3682 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003683 if (!window)
3684 return NULL;
3685
3686 window->x = x;
3687 window->y = y;
3688
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003689 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003690 wl_shell_surface_set_transient(
3691 window->shell_surface,
3692 window->parent->main_surface->surface,
3693 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003694
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003695 return window;
3696}
3697
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003698static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003699menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003700{
3701 int next;
3702
3703 next = (sy - 8) / 20;
3704 if (menu->current != next) {
3705 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003706 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003707 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003708}
3709
3710static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003711menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003712 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003713 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003714{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003715 struct menu *menu = data;
3716
3717 if (widget == menu->widget)
3718 menu_set_item(data, y);
3719
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003720 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003721}
3722
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003723static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003724menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003725 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003726{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003727 struct menu *menu = data;
3728
3729 if (widget == menu->widget)
3730 menu_set_item(data, y);
3731
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003732 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003733}
3734
3735static void
3736menu_leave_handler(struct widget *widget, struct input *input, void *data)
3737{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003738 struct menu *menu = data;
3739
3740 if (widget == menu->widget)
3741 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003742}
3743
3744static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003745menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003746 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003747 uint32_t button, enum wl_pointer_button_state state,
3748 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003749
3750{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003751 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003752
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003753 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3754 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003755 /* Either relase after press-drag-release or
3756 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003757 menu->func(menu->window->parent,
3758 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003759 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003760 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003761 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003762 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003763 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003764}
3765
3766static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003767menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003768{
3769 cairo_t *cr;
3770 const int32_t r = 3, margin = 3;
3771 struct menu *menu = data;
3772 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003773 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003774
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003775 cr = cairo_create(window->cairo_surface);
3776 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3777 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3778 cairo_paint(cr);
3779
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003780 width = window->main_surface->allocation.width;
3781 height = window->main_surface->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003782 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003783
3784 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003785 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3786 cairo_fill(cr);
3787
3788 for (i = 0; i < menu->count; i++) {
3789 if (i == menu->current) {
3790 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3791 cairo_rectangle(cr, margin, i * 20 + margin,
3792 width - 2 * margin, 20);
3793 cairo_fill(cr);
3794 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3795 cairo_move_to(cr, 10, i * 20 + 16);
3796 cairo_show_text(cr, menu->entries[i]);
3797 } else {
3798 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3799 cairo_move_to(cr, 10, i * 20 + 16);
3800 cairo_show_text(cr, menu->entries[i]);
3801 }
3802 }
3803
3804 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003805}
3806
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003807void
3808window_show_menu(struct display *display,
3809 struct input *input, uint32_t time, struct window *parent,
3810 int32_t x, int32_t y,
3811 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003812{
3813 struct window *window;
3814 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003815 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003816
3817 menu = malloc(sizeof *menu);
3818 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003819 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003820
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003821 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003822 if (!window) {
3823 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003824 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003825 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003826
3827 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003828 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003829 menu->entries = entries;
3830 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003831 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003832 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003833 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003834 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003835 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003836 window->type = TYPE_MENU;
3837 window->x = x;
3838 window->y = y;
3839
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003840 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003841 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003842 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02003843 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003844 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003845
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003846 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003847 widget_set_enter_handler(menu->widget, menu_enter_handler);
3848 widget_set_leave_handler(menu->widget, menu_leave_handler);
3849 widget_set_motion_handler(menu->widget, menu_motion_handler);
3850 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003851
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003852 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003853 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003854}
3855
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003856void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003857window_set_buffer_type(struct window *window, enum window_buffer_type type)
3858{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003859 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003860}
3861
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003862
3863static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003864display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003865 struct wl_output *wl_output,
3866 int x, int y,
3867 int physical_width,
3868 int physical_height,
3869 int subpixel,
3870 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003871 const char *model,
3872 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003873{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003874 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003875
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003876 output->allocation.x = x;
3877 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003878 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003879}
3880
3881static void
3882display_handle_mode(void *data,
3883 struct wl_output *wl_output,
3884 uint32_t flags,
3885 int width,
3886 int height,
3887 int refresh)
3888{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003889 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003890 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003891
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003892 if (flags & WL_OUTPUT_MODE_CURRENT) {
3893 output->allocation.width = width;
3894 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003895 if (display->output_configure_handler)
3896 (*display->output_configure_handler)(
3897 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003898 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003899}
3900
3901static const struct wl_output_listener output_listener = {
3902 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003903 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003904};
3905
3906static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003907display_add_output(struct display *d, uint32_t id)
3908{
3909 struct output *output;
3910
3911 output = malloc(sizeof *output);
3912 if (output == NULL)
3913 return;
3914
3915 memset(output, 0, sizeof *output);
3916 output->display = d;
3917 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003918 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003919 wl_list_insert(d->output_list.prev, &output->link);
3920
3921 wl_output_add_listener(output->output, &output_listener, output);
3922}
3923
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003924static void
3925output_destroy(struct output *output)
3926{
3927 if (output->destroy_handler)
3928 (*output->destroy_handler)(output, output->user_data);
3929
3930 wl_output_destroy(output->output);
3931 wl_list_remove(&output->link);
3932 free(output);
3933}
3934
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003935void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003936display_set_global_handler(struct display *display,
3937 display_global_handler_t handler)
3938{
3939 struct global *global;
3940
3941 display->global_handler = handler;
3942 if (!handler)
3943 return;
3944
3945 wl_list_for_each(global, &display->global_list, link)
3946 display->global_handler(display,
3947 global->name, global->interface,
3948 global->version, display->user_data);
3949}
3950
3951void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003952display_set_output_configure_handler(struct display *display,
3953 display_output_handler_t handler)
3954{
3955 struct output *output;
3956
3957 display->output_configure_handler = handler;
3958 if (!handler)
3959 return;
3960
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003961 wl_list_for_each(output, &display->output_list, link) {
3962 if (output->allocation.width == 0 &&
3963 output->allocation.height == 0)
3964 continue;
3965
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003966 (*display->output_configure_handler)(output,
3967 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003968 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003969}
3970
3971void
3972output_set_user_data(struct output *output, void *data)
3973{
3974 output->user_data = data;
3975}
3976
3977void *
3978output_get_user_data(struct output *output)
3979{
3980 return output->user_data;
3981}
3982
3983void
3984output_set_destroy_handler(struct output *output,
3985 display_output_handler_t handler)
3986{
3987 output->destroy_handler = handler;
3988 /* FIXME: implement this, once we have way to remove outputs */
3989}
3990
3991void
Scott Moreau4e072362012-09-29 02:03:11 -06003992output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003993{
Scott Moreau4e072362012-09-29 02:03:11 -06003994 struct rectangle allocation = output->allocation;
3995
3996 switch (output->transform) {
3997 case WL_OUTPUT_TRANSFORM_90:
3998 case WL_OUTPUT_TRANSFORM_270:
3999 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4000 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4001 /* Swap width and height */
4002 allocation.width = output->allocation.height;
4003 allocation.height = output->allocation.width;
4004 break;
4005 }
4006
4007 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004008}
4009
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004010struct wl_output *
4011output_get_wl_output(struct output *output)
4012{
4013 return output->output;
4014}
4015
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004016enum wl_output_transform
4017output_get_transform(struct output *output)
4018{
4019 return output->transform;
4020}
4021
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004022static void
Daniel Stone97f68542012-05-30 16:32:01 +01004023fini_xkb(struct input *input)
4024{
4025 xkb_state_unref(input->xkb.state);
4026 xkb_map_unref(input->xkb.keymap);
4027}
4028
4029static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004030display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004031{
4032 struct input *input;
4033
4034 input = malloc(sizeof *input);
4035 if (input == NULL)
4036 return;
4037
4038 memset(input, 0, sizeof *input);
4039 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004040 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004041 input->pointer_focus = NULL;
4042 input->keyboard_focus = NULL;
4043 wl_list_insert(d->input_list.prev, &input->link);
4044
Daniel Stone37816df2012-05-16 18:45:18 +01004045 wl_seat_add_listener(input->seat, &seat_listener, input);
4046 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004047
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004048 input->data_device =
4049 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004050 input->seat);
4051 wl_data_device_add_listener(input->data_device, &data_device_listener,
4052 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004053
4054 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004055
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004056 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4057 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004058 input->repeat_task.run = keyboard_repeat_func;
4059 display_watch_fd(d, input->repeat_timer_fd,
4060 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004061}
4062
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004063static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004064input_destroy(struct input *input)
4065{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004066 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004067 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004068
4069 if (input->drag_offer)
4070 data_offer_destroy(input->drag_offer);
4071
4072 if (input->selection_offer)
4073 data_offer_destroy(input->selection_offer);
4074
4075 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004076 fini_xkb(input);
4077
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004078 wl_surface_destroy(input->pointer_surface);
4079
Pekka Paalanene1207c72011-12-16 12:02:09 +02004080 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004081 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004082 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004083 free(input);
4084}
4085
4086static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004087init_workspace_manager(struct display *d, uint32_t id)
4088{
4089 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004090 wl_registry_bind(d->registry, id,
4091 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004092 if (d->workspace_manager != NULL)
4093 workspace_manager_add_listener(d->workspace_manager,
4094 &workspace_manager_listener,
4095 d);
4096}
4097
4098static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004099registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4100 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004101{
4102 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004103 struct global *global;
4104
4105 global = malloc(sizeof *global);
4106 global->name = id;
4107 global->interface = strdup(interface);
4108 global->version = version;
4109 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004110
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004111 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004112 d->compositor = wl_registry_bind(registry, id,
4113 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004114 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004115 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004116 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004117 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004118 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004119 d->shell = wl_registry_bind(registry,
4120 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004121 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004122 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004123 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4124 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004125 wl_registry_bind(registry, id,
4126 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004127 } else if (strcmp(interface, "text_cursor_position") == 0) {
4128 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004129 wl_registry_bind(registry, id,
4130 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004131 } else if (strcmp(interface, "workspace_manager") == 0) {
4132 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004133 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004134
4135 if (d->global_handler)
4136 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004137}
4138
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004139static void
4140registry_handle_global_remove(void *data, struct wl_registry *registry,
4141 uint32_t name)
4142{
4143 struct display *d = data;
4144 struct global *global;
4145 struct global *tmp;
4146
4147 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4148 if (global->name != name)
4149 continue;
4150
4151 /* XXX: Should destroy bound globals, and call
4152 * the counterpart of display::global_handler
4153 */
4154 wl_list_remove(&global->link);
4155 free(global->interface);
4156 free(global);
4157 }
4158}
4159
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004160void *
4161display_bind(struct display *display, uint32_t name,
4162 const struct wl_interface *interface, uint32_t version)
4163{
4164 return wl_registry_bind(display->registry, name, interface, version);
4165}
4166
4167static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004168 registry_handle_global,
4169 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004170};
4171
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004172#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004173static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004174init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004175{
4176 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004177 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004178
Rob Clark6396ed32012-03-11 19:48:41 -05004179#ifdef USE_CAIRO_GLESV2
4180# define GL_BIT EGL_OPENGL_ES2_BIT
4181#else
4182# define GL_BIT EGL_OPENGL_BIT
4183#endif
4184
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004185 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004186 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004187 EGL_RED_SIZE, 1,
4188 EGL_GREEN_SIZE, 1,
4189 EGL_BLUE_SIZE, 1,
4190 EGL_ALPHA_SIZE, 1,
4191 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004192 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004193 EGL_NONE
4194 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004195
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004196#ifdef USE_CAIRO_GLESV2
4197 static const EGLint context_attribs[] = {
4198 EGL_CONTEXT_CLIENT_VERSION, 2,
4199 EGL_NONE
4200 };
4201 EGLint api = EGL_OPENGL_ES_API;
4202#else
4203 EGLint *context_attribs = NULL;
4204 EGLint api = EGL_OPENGL_API;
4205#endif
4206
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004207 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004208 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004209 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004210 return -1;
4211 }
4212
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004213 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004214 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004215 return -1;
4216 }
4217
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004218 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4219 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004220 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004221 return -1;
4222 }
4223
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004224 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004225 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004226 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004227 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004228 return -1;
4229 }
4230
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004231 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004232 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004233 return -1;
4234 }
4235
Benjamin Franzke0c991632011-09-27 21:57:31 +02004236 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4237 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004238 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004239 return -1;
4240 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004241
4242 return 0;
4243}
4244
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004245static void
4246fini_egl(struct display *display)
4247{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004248 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004249
4250 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4251 EGL_NO_CONTEXT);
4252
4253 eglTerminate(display->dpy);
4254 eglReleaseThread();
4255}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004256#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004257
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004258static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004259init_dummy_surface(struct display *display)
4260{
4261 int len;
4262 void *data;
4263
4264 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4265 data = malloc(len);
4266 display->dummy_surface =
4267 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4268 1, 1, len);
4269 display->dummy_surface_data = data;
4270}
4271
4272static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004273handle_display_data(struct task *task, uint32_t events)
4274{
4275 struct display *display =
4276 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004277 struct epoll_event ep;
4278 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004279
4280 display->display_fd_events = events;
4281
4282 if (events & EPOLLERR || events & EPOLLHUP) {
4283 display_exit(display);
4284 return;
4285 }
4286
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004287 if (events & EPOLLIN) {
4288 ret = wl_display_dispatch(display->display);
4289 if (ret == -1) {
4290 display_exit(display);
4291 return;
4292 }
4293 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004294
4295 if (events & EPOLLOUT) {
4296 ret = wl_display_flush(display->display);
4297 if (ret == 0) {
4298 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4299 ep.data.ptr = &display->display_task;
4300 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4301 display->display_fd, &ep);
4302 } else if (ret == -1 && errno != EAGAIN) {
4303 display_exit(display);
4304 return;
4305 }
4306 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004307}
4308
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004309struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004310display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004311{
4312 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004313
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004314 d = malloc(sizeof *d);
4315 if (d == NULL)
4316 return NULL;
4317
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004318 memset(d, 0, sizeof *d);
4319
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004320 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004321 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004322 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004323 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004324 return NULL;
4325 }
4326
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004327 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004328 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004329 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004330 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4331 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004332
4333 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004334 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004335 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004336 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004337
Daniel Stone97f68542012-05-30 16:32:01 +01004338 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004339 if (d->xkb_context == NULL) {
4340 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004341 return NULL;
4342 }
4343
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004344 d->workspace = 0;
4345 d->workspace_count = 1;
4346
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004347 d->registry = wl_display_get_registry(d->display);
4348 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004349
4350 if (wl_display_dispatch(d->display) < 0) {
4351 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4352 return NULL;
4353 }
4354
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004355#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004356 if (init_egl(d) < 0)
4357 fprintf(stderr, "EGL does not seem to work, "
4358 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004359#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004360
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004361 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004362
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004363 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004364
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004365 wl_list_init(&d->window_list);
4366
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004367 init_dummy_surface(d);
4368
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004369 return d;
4370}
4371
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004372static void
4373display_destroy_outputs(struct display *display)
4374{
4375 struct output *tmp;
4376 struct output *output;
4377
4378 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4379 output_destroy(output);
4380}
4381
Pekka Paalanene1207c72011-12-16 12:02:09 +02004382static void
4383display_destroy_inputs(struct display *display)
4384{
4385 struct input *tmp;
4386 struct input *input;
4387
4388 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4389 input_destroy(input);
4390}
4391
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004392void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004393display_destroy(struct display *display)
4394{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004395 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004396 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4397 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004398
4399 if (!wl_list_empty(&display->deferred_list))
4400 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4401
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004402 cairo_surface_destroy(display->dummy_surface);
4403 free(display->dummy_surface_data);
4404
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004405 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004406 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004407
Daniel Stone97f68542012-05-30 16:32:01 +01004408 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004409
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004410 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004411 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004412
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004413#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004414 if (display->argb_device)
4415 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004416#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004417
Pekka Paalanenc2052982011-12-16 11:41:32 +02004418 if (display->shell)
4419 wl_shell_destroy(display->shell);
4420
4421 if (display->shm)
4422 wl_shm_destroy(display->shm);
4423
4424 if (display->data_device_manager)
4425 wl_data_device_manager_destroy(display->data_device_manager);
4426
4427 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004428 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004429
4430 close(display->epoll_fd);
4431
U. Artie Eoff44874d92012-10-02 21:12:35 -07004432 if (!(display->display_fd_events & EPOLLERR) &&
4433 !(display->display_fd_events & EPOLLHUP))
4434 wl_display_flush(display->display);
4435
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004436 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004437 free(display);
4438}
4439
4440void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004441display_set_user_data(struct display *display, void *data)
4442{
4443 display->user_data = data;
4444}
4445
4446void *
4447display_get_user_data(struct display *display)
4448{
4449 return display->user_data;
4450}
4451
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004452struct wl_display *
4453display_get_display(struct display *display)
4454{
4455 return display->display;
4456}
4457
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004458struct output *
4459display_get_output(struct display *display)
4460{
4461 return container_of(display->output_list.next, struct output, link);
4462}
4463
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004464struct wl_compositor *
4465display_get_compositor(struct display *display)
4466{
4467 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004468}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004469
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004470uint32_t
4471display_get_serial(struct display *display)
4472{
4473 return display->serial;
4474}
4475
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004476EGLDisplay
4477display_get_egl_display(struct display *d)
4478{
4479 return d->dpy;
4480}
4481
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004482struct wl_data_source *
4483display_create_data_source(struct display *display)
4484{
4485 return wl_data_device_manager_create_data_source(display->data_device_manager);
4486}
4487
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004488EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004489display_get_argb_egl_config(struct display *d)
4490{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004491 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004492}
4493
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004494struct wl_shell *
4495display_get_shell(struct display *display)
4496{
4497 return display->shell;
4498}
4499
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004500int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004501display_acquire_window_surface(struct display *display,
4502 struct window *window,
4503 EGLContext ctx)
4504{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004505 struct surface *surface = window->main_surface;
4506
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004507 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004508 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004509
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004510 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004511}
4512
4513void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004514display_release_window_surface(struct display *display,
4515 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004516{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004517 struct surface *surface = window->main_surface;
4518
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004519 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004520 return;
4521
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004522 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004523}
4524
4525void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004526display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004527{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004528 wl_list_insert(&display->deferred_list, &task->link);
4529}
4530
4531void
4532display_watch_fd(struct display *display,
4533 int fd, uint32_t events, struct task *task)
4534{
4535 struct epoll_event ep;
4536
4537 ep.events = events;
4538 ep.data.ptr = task;
4539 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4540}
4541
4542void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004543display_unwatch_fd(struct display *display, int fd)
4544{
4545 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4546}
4547
4548void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004549display_run(struct display *display)
4550{
4551 struct task *task;
4552 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004553 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004554
Pekka Paalanen826d7952011-12-15 10:14:07 +02004555 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004556 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004557 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004558 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004559 struct task, link);
4560 wl_list_remove(&task->link);
4561 task->run(task, 0);
4562 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004563
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004564 wl_display_dispatch_pending(display->display);
4565
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004566 if (!display->running)
4567 break;
4568
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004569 ret = wl_display_flush(display->display);
4570 if (ret < 0 && errno == EAGAIN) {
4571 ep[0].events =
4572 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4573 ep[0].data.ptr = &display->display_task;
4574
4575 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4576 display->display_fd, &ep[0]);
4577 } else if (ret < 0) {
4578 break;
4579 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004580
4581 count = epoll_wait(display->epoll_fd,
4582 ep, ARRAY_LENGTH(ep), -1);
4583 for (i = 0; i < count; i++) {
4584 task = ep[i].data.ptr;
4585 task->run(task, ep[i].events);
4586 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004587 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004588}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004589
4590void
4591display_exit(struct display *display)
4592{
4593 display->running = 0;
4594}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004595
4596void
4597keysym_modifiers_add(struct wl_array *modifiers_map,
4598 const char *name)
4599{
4600 size_t len = strlen(name) + 1;
4601 char *p;
4602
4603 p = wl_array_add(modifiers_map, len);
4604
4605 if (p == NULL)
4606 return;
4607
4608 strncpy(p, name, len);
4609}
4610
4611static xkb_mod_index_t
4612keysym_modifiers_get_index(struct wl_array *modifiers_map,
4613 const char *name)
4614{
4615 xkb_mod_index_t index = 0;
4616 char *p = modifiers_map->data;
4617
4618 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4619 if (strcmp(p, name) == 0)
4620 return index;
4621
4622 index++;
4623 p += strlen(p) + 1;
4624 }
4625
4626 return XKB_MOD_INVALID;
4627}
4628
4629xkb_mod_mask_t
4630keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4631 const char *name)
4632{
4633 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4634
4635 if (index == XKB_MOD_INVALID)
4636 return XKB_MOD_INVALID;
4637
4638 return 1 << index;
4639}