blob: c4a679b79c1b48eee55eee018e6dd20b958f2668 [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;
193
194 struct rectangle allocation;
195 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200196
197 enum window_buffer_type buffer_type;
198 enum wl_output_transform buffer_transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200199};
200
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500201struct window {
202 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500203 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100204 struct wl_list window_output_list;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500205 struct wl_region *input_region;
206 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500207 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200208 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400209 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500210 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500211 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400212 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400213 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400214 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400215 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400216 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400217 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400218 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400219 int focus_count;
220
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400221 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500222
Pekka Paalanen99436862012-11-19 17:15:59 +0200223 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200224 int fullscreen_method;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400225
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500226 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500227 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400228 window_data_handler_t data_handler;
229 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500230 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400231 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200232 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400233
Pekka Paalanen4e373742013-02-13 16:17:13 +0200234 struct surface *main_surface;
235 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300236 struct wl_callback *frame_cb;
237
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200238 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500239 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500240
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500241 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400242 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500243};
244
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500245struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500246 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300247 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500248 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400249 struct wl_list link;
250 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500251 widget_resize_handler_t resize_handler;
252 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500253 widget_enter_handler_t enter_handler;
254 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500255 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500256 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200257 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400258 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500259 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300260 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500261 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400262};
263
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400264struct input {
265 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100266 struct wl_seat *seat;
267 struct wl_pointer *pointer;
268 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400269 struct window *pointer_focus;
270 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300271 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300272 uint32_t cursor_anim_start;
273 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300274 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400275 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400276 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400277 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400278 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400279 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400280
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500281 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500282 struct widget *grab;
283 uint32_t grab_button;
284
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400285 struct wl_data_device *data_device;
286 struct data_offer *drag_offer;
287 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100288
289 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100290 struct xkb_keymap *keymap;
291 struct xkb_state *state;
292 xkb_mod_mask_t control_mask;
293 xkb_mod_mask_t alt_mask;
294 xkb_mod_mask_t shift_mask;
295 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400296
297 struct task repeat_task;
298 int repeat_timer_fd;
299 uint32_t repeat_sym;
300 uint32_t repeat_key;
301 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400302};
303
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500304struct output {
305 struct display *display;
306 struct wl_output *output;
307 struct rectangle allocation;
308 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600309 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200310
311 display_output_handler_t destroy_handler;
312 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500313};
314
Martin Minarik1998b152012-05-10 02:04:35 +0200315enum frame_button_action {
316 FRAME_BUTTON_NULL = 0,
317 FRAME_BUTTON_ICON = 1,
318 FRAME_BUTTON_CLOSE = 2,
319 FRAME_BUTTON_MINIMIZE = 3,
320 FRAME_BUTTON_MAXIMIZE = 4,
321};
322
323enum frame_button_pointer {
324 FRAME_BUTTON_DEFAULT = 0,
325 FRAME_BUTTON_OVER = 1,
326 FRAME_BUTTON_ACTIVE = 2,
327};
328
329enum frame_button_align {
330 FRAME_BUTTON_RIGHT = 0,
331 FRAME_BUTTON_LEFT = 1,
332};
333
334enum frame_button_decoration {
335 FRAME_BUTTON_NONE = 0,
336 FRAME_BUTTON_FANCY = 1,
337};
338
339struct frame_button {
340 struct widget *widget;
341 struct frame *frame;
342 cairo_surface_t *icon;
343 enum frame_button_action type;
344 enum frame_button_pointer state;
345 struct wl_list link; /* buttons_list */
346 enum frame_button_align align;
347 enum frame_button_decoration decoration;
348};
349
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500350struct frame {
351 struct widget *widget;
352 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200353 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500354};
355
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500356struct menu {
357 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500358 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500359 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500360 const char **entries;
361 uint32_t time;
362 int current;
363 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400364 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500365 menu_func_t func;
366};
367
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300368struct tooltip {
369 struct widget *parent;
370 struct window *window;
371 struct widget *widget;
372 char *entry;
373 struct task tooltip_task;
374 int tooltip_fd;
375 float x, y;
376};
377
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700378struct shm_pool {
379 struct wl_shm_pool *pool;
380 size_t size;
381 size_t used;
382 void *data;
383};
384
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400385enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300386 CURSOR_DEFAULT = 100,
387 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400388};
389
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500390enum window_location {
391 WINDOW_INTERIOR = 0,
392 WINDOW_RESIZING_TOP = 1,
393 WINDOW_RESIZING_BOTTOM = 2,
394 WINDOW_RESIZING_LEFT = 4,
395 WINDOW_RESIZING_TOP_LEFT = 5,
396 WINDOW_RESIZING_BOTTOM_LEFT = 6,
397 WINDOW_RESIZING_RIGHT = 8,
398 WINDOW_RESIZING_TOP_RIGHT = 9,
399 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
400 WINDOW_RESIZING_MASK = 15,
401 WINDOW_EXTERIOR = 16,
402 WINDOW_TITLEBAR = 17,
403 WINDOW_CLIENT_AREA = 18,
404};
405
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200406static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400407
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500408#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200410struct egl_window_surface {
411 struct toysurface base;
412 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100413 struct display *display;
414 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200415 struct wl_egl_window *egl_window;
416 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100417};
418
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200419static struct egl_window_surface *
420to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100421{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200422 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100423}
424
425static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200426egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200427 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100428{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200429 struct egl_window_surface *surface = to_egl_window_surface(base);
430
431 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
432 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
433
434 return cairo_surface_reference(surface->cairo_surface);
435}
436
437static void
438egl_window_surface_swap(struct toysurface *base,
439 struct rectangle *server_allocation)
440{
441 struct egl_window_surface *surface = to_egl_window_surface(base);
442
443 cairo_gl_surface_swapbuffers(surface->cairo_surface);
444 wl_egl_window_get_attached_size(surface->egl_window,
445 &server_allocation->width,
446 &server_allocation->height);
447}
448
449static int
450egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
451{
452 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200453 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100454
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200455 device = cairo_surface_get_device(surface->cairo_surface);
456 if (!device)
457 return -1;
458
459 if (!ctx) {
460 if (device == surface->display->argb_device)
461 ctx = surface->display->argb_ctx;
462 else
463 assert(0);
464 }
465
466 cairo_device_flush(device);
467 cairo_device_acquire(device);
468 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
469 surface->egl_surface, ctx))
470 fprintf(stderr, "failed to make surface current\n");
471
472 return 0;
473}
474
475static void
476egl_window_surface_release(struct toysurface *base)
477{
478 struct egl_window_surface *surface = to_egl_window_surface(base);
479 cairo_device_t *device;
480
481 device = cairo_surface_get_device(surface->cairo_surface);
482 if (!device)
483 return;
484
485 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
486 surface->display->argb_ctx))
487 fprintf(stderr, "failed to make context current\n");
488
489 cairo_device_release(device);
490}
491
492static void
493egl_window_surface_destroy(struct toysurface *base)
494{
495 struct egl_window_surface *surface = to_egl_window_surface(base);
496 struct display *d = surface->display;
497
498 cairo_surface_destroy(surface->cairo_surface);
499 eglDestroySurface(d->dpy, surface->egl_surface);
500 wl_egl_window_destroy(surface->egl_window);
501 surface->surface = NULL;
502
503 free(surface);
504}
505
506static struct toysurface *
507egl_window_surface_create(struct display *display,
508 struct wl_surface *wl_surface,
509 uint32_t flags,
510 struct rectangle *rectangle)
511{
512 struct egl_window_surface *surface;
513
Pekka Paalanenb3627362012-11-19 17:16:00 +0200514 if (display->dpy == EGL_NO_DISPLAY)
515 return NULL;
516
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517 surface = calloc(1, sizeof *surface);
518 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100519 return NULL;
520
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521 surface->base.prepare = egl_window_surface_prepare;
522 surface->base.swap = egl_window_surface_swap;
523 surface->base.acquire = egl_window_surface_acquire;
524 surface->base.release = egl_window_surface_release;
525 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100526
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527 surface->display = display;
528 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400529
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 surface->egl_window = wl_egl_window_create(surface->surface,
531 rectangle->width,
532 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100533
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534 surface->egl_surface = eglCreateWindowSurface(display->dpy,
535 display->argb_config,
536 surface->egl_window,
537 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100538
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200539 surface->cairo_surface =
540 cairo_gl_surface_create_for_egl(display->argb_device,
541 surface->egl_surface,
542 rectangle->width,
543 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100544
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200545 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100546}
547
Pekka Paalanenb3627362012-11-19 17:16:00 +0200548#else
549
550static struct toysurface *
551egl_window_surface_create(struct display *display,
552 struct wl_surface *wl_surface,
553 uint32_t flags,
554 struct rectangle *rectangle)
555{
556 return NULL;
557}
558
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559#endif
560
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200561struct shm_surface_data {
562 struct wl_buffer *buffer;
563 struct shm_pool *pool;
564};
565
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566struct wl_buffer *
567display_get_buffer_for_surface(struct display *display,
568 cairo_surface_t *surface)
569{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200570 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400571
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200572 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573
574 return data->buffer;
575}
576
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500577static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700578shm_pool_destroy(struct shm_pool *pool);
579
580static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581shm_surface_data_destroy(void *p)
582{
583 struct shm_surface_data *data = p;
584
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200585 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700586 if (data->pool)
587 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300588
589 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400590}
591
Kristian Høgsberg16626282012-04-03 11:21:27 -0400592static struct wl_shm_pool *
593make_shm_pool(struct display *display, int size, void **data)
594{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400595 struct wl_shm_pool *pool;
596 int fd;
597
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300598 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400599 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300600 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
601 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400602 return NULL;
603 }
604
605 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400606 if (*data == MAP_FAILED) {
607 fprintf(stderr, "mmap failed: %m\n");
608 close(fd);
609 return NULL;
610 }
611
612 pool = wl_shm_create_pool(display->shm, fd, size);
613
614 close(fd);
615
616 return pool;
617}
618
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700619static struct shm_pool *
620shm_pool_create(struct display *display, size_t size)
621{
622 struct shm_pool *pool = malloc(sizeof *pool);
623
624 if (!pool)
625 return NULL;
626
627 pool->pool = make_shm_pool(display, size, &pool->data);
628 if (!pool->pool) {
629 free(pool);
630 return NULL;
631 }
632
633 pool->size = size;
634 pool->used = 0;
635
636 return pool;
637}
638
639static void *
640shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
641{
642 if (pool->used + size > pool->size)
643 return NULL;
644
645 *offset = pool->used;
646 pool->used += size;
647
648 return (char *) pool->data + *offset;
649}
650
651/* destroy the pool. this does not unmap the memory though */
652static void
653shm_pool_destroy(struct shm_pool *pool)
654{
655 munmap(pool->data, pool->size);
656 wl_shm_pool_destroy(pool->pool);
657 free(pool);
658}
659
660/* Start allocating from the beginning of the pool again */
661static void
662shm_pool_reset(struct shm_pool *pool)
663{
664 pool->used = 0;
665}
666
667static int
668data_length_for_shm_surface(struct rectangle *rect)
669{
670 int stride;
671
672 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
673 rect->width);
674 return stride * rect->height;
675}
676
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500677static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700678display_create_shm_surface_from_pool(struct display *display,
679 struct rectangle *rectangle,
680 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681{
682 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400683 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400684 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700685 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400686 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400687
688 data = malloc(sizeof *data);
689 if (data == NULL)
690 return NULL;
691
692 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
693 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700694 length = stride * rectangle->height;
695 data->pool = NULL;
696 map = shm_pool_allocate(pool, length, &offset);
697
698 if (!map) {
699 free(data);
700 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400701 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400703 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704 CAIRO_FORMAT_ARGB32,
705 rectangle->width,
706 rectangle->height,
707 stride);
708
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200709 cairo_surface_set_user_data(surface, &shm_surface_data_key,
710 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400711
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400712 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500713 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400714 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500715 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400716
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200717 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
718 rectangle->width,
719 rectangle->height,
720 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400721
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700722 return surface;
723}
724
725static cairo_surface_t *
726display_create_shm_surface(struct display *display,
727 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200728 struct shm_pool *alternate_pool,
729 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700730{
731 struct shm_surface_data *data;
732 struct shm_pool *pool;
733 cairo_surface_t *surface;
734
Pekka Paalanen99436862012-11-19 17:15:59 +0200735 if (alternate_pool) {
736 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700737 surface = display_create_shm_surface_from_pool(display,
738 rectangle,
739 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200740 alternate_pool);
741 if (surface) {
742 data = cairo_surface_get_user_data(surface,
743 &shm_surface_data_key);
744 goto out;
745 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700746 }
747
748 pool = shm_pool_create(display,
749 data_length_for_shm_surface(rectangle));
750 if (!pool)
751 return NULL;
752
753 surface =
754 display_create_shm_surface_from_pool(display, rectangle,
755 flags, pool);
756
757 if (!surface) {
758 shm_pool_destroy(pool);
759 return NULL;
760 }
761
762 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200763 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700764 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400765
Pekka Paalanen99436862012-11-19 17:15:59 +0200766out:
767 if (data_ret)
768 *data_ret = data;
769
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400770 return surface;
771}
772
nobled7b87cb02011-02-01 18:51:47 +0000773static int
774check_size(struct rectangle *rect)
775{
776 if (rect->width && rect->height)
777 return 0;
778
779 fprintf(stderr, "tried to create surface of "
780 "width: %d, height: %d\n", rect->width, rect->height);
781 return -1;
782}
783
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400784cairo_surface_t *
785display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100786 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400787 struct rectangle *rectangle,
788 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400789{
nobled7b87cb02011-02-01 18:51:47 +0000790 if (check_size(rectangle) < 0)
791 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200792
793 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200794 return display_create_shm_surface(display, rectangle, flags,
795 NULL, NULL);
796}
797
Pekka Paalanena4eda732012-11-19 17:16:02 +0200798struct shm_surface_leaf {
799 cairo_surface_t *cairo_surface;
800 /* 'data' is automatically destroyed, when 'cairo_surface' is */
801 struct shm_surface_data *data;
802
803 struct shm_pool *resize_pool;
804 int busy;
805};
806
807static void
808shm_surface_leaf_release(struct shm_surface_leaf *leaf)
809{
810 if (leaf->cairo_surface)
811 cairo_surface_destroy(leaf->cairo_surface);
812 /* leaf->data already destroyed via cairo private */
813
814 if (leaf->resize_pool)
815 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200816
817 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200818}
819
Pekka Paalanen99436862012-11-19 17:15:59 +0200820struct shm_surface {
821 struct toysurface base;
822 struct display *display;
823 struct wl_surface *surface;
824 uint32_t flags;
825 int dx, dy;
826
Pekka Paalanena4eda732012-11-19 17:16:02 +0200827 struct shm_surface_leaf leaf[2];
828 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200829};
830
831static struct shm_surface *
832to_shm_surface(struct toysurface *base)
833{
834 return container_of(base, struct shm_surface, base);
835}
836
Pekka Paalanena4eda732012-11-19 17:16:02 +0200837static void
838shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
839{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200840 struct shm_surface *surface = data;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200841
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200842 if (surface->leaf[0].data->buffer == buffer)
843 surface->leaf[0].busy = 0;
844 else if (surface->leaf[1].data->buffer == buffer)
845 surface->leaf[1].busy = 0;
846 else
847 assert(0 && "shm_surface_buffer_release: unknown buffer");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200848
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200849 if (!surface->leaf[0].busy && !surface->leaf[1].busy)
850 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200851}
852
853static const struct wl_buffer_listener shm_surface_buffer_listener = {
854 shm_surface_buffer_release
855};
856
Pekka Paalanen99436862012-11-19 17:15:59 +0200857static cairo_surface_t *
858shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200859 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200860{
Pekka Paalanenec076692012-11-30 13:37:27 +0200861 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200862 struct shm_surface *surface = to_shm_surface(base);
863 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200864 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200865
866 surface->dx = dx;
867 surface->dy = dy;
868
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200869 /* See shm_surface_buffer_release() */
870 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
871 surface->leaf[1].cairo_surface) {
872 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
873 __func__);
874 }
875
Pekka Paalanena4eda732012-11-19 17:16:02 +0200876 /* pick a free buffer from the two */
877 if (!surface->leaf[0].busy)
878 leaf = &surface->leaf[0];
879 else if (!surface->leaf[1].busy)
880 leaf = &surface->leaf[1];
881 else {
882 fprintf(stderr, "%s: both buffers are held by the server.\n",
883 __func__);
884 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200885 }
886
Pekka Paalanena4eda732012-11-19 17:16:02 +0200887 if (!resize_hint && leaf->resize_pool) {
888 cairo_surface_destroy(leaf->cairo_surface);
889 leaf->cairo_surface = NULL;
890 shm_pool_destroy(leaf->resize_pool);
891 leaf->resize_pool = NULL;
892 }
893
894 if (leaf->cairo_surface &&
895 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
896 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200897 goto out;
898
Pekka Paalanena4eda732012-11-19 17:16:02 +0200899 if (leaf->cairo_surface)
900 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200901
Pekka Paalanena4eda732012-11-19 17:16:02 +0200902 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200903 /* Create a big pool to allocate from, while continuously
904 * resizing. Mmapping a new pool in the server
905 * is relatively expensive, so reusing a pool performs
906 * better, but may temporarily reserve unneeded memory.
907 */
908 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200909 leaf->resize_pool = shm_pool_create(surface->display,
910 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200911 }
912
Pekka Paalanena4eda732012-11-19 17:16:02 +0200913 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200914 display_create_shm_surface(surface->display, &rect,
915 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916 leaf->resize_pool,
917 &leaf->data);
918 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200919 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200920
921out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200922 surface->current = leaf;
923
924 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200925}
926
927static void
928shm_surface_swap(struct toysurface *base,
929 struct rectangle *server_allocation)
930{
931 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200932 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200933
934 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200935 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200936 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200937 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200938
Pekka Paalanena4eda732012-11-19 17:16:02 +0200939 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200940 surface->dx, surface->dy);
941 wl_surface_damage(surface->surface, 0, 0,
942 server_allocation->width, server_allocation->height);
943 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200944
945 leaf->busy = 1;
946 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200947}
948
949static int
950shm_surface_acquire(struct toysurface *base, EGLContext ctx)
951{
952 return -1;
953}
954
955static void
956shm_surface_release(struct toysurface *base)
957{
958}
959
960static void
961shm_surface_destroy(struct toysurface *base)
962{
963 struct shm_surface *surface = to_shm_surface(base);
964
Pekka Paalanena4eda732012-11-19 17:16:02 +0200965 shm_surface_leaf_release(&surface->leaf[0]);
966 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200967
968 free(surface);
969}
970
971static struct toysurface *
972shm_surface_create(struct display *display, struct wl_surface *wl_surface,
973 uint32_t flags, struct rectangle *rectangle)
974{
975 struct shm_surface *surface;
976
977 surface = calloc(1, sizeof *surface);
978 if (!surface)
979 return NULL;
980
981 surface->base.prepare = shm_surface_prepare;
982 surface->base.swap = shm_surface_swap;
983 surface->base.acquire = shm_surface_acquire;
984 surface->base.release = shm_surface_release;
985 surface->base.destroy = shm_surface_destroy;
986
987 surface->display = display;
988 surface->surface = wl_surface;
989 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200990
991 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400992}
993
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200994/*
995 * The following correspondences between file names and cursors was copied
996 * from: https://bugs.kde.org/attachment.cgi?id=67313
997 */
998
999static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001000 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001001 "sw-resize"
1002};
1003
1004static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001005 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001006 "se-resize"
1007};
1008
1009static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001010 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001011 "s-resize"
1012};
1013
1014static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001015 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001016 "closedhand",
1017 "208530c400c041818281048008011002"
1018};
1019
1020static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001021 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001022 "default",
1023 "top_left_arrow",
1024 "left-arrow"
1025};
1026
1027static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001028 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001029 "w-resize"
1030};
1031
1032static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001033 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001034 "e-resize"
1035};
1036
1037static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001038 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001039 "nw-resize"
1040};
1041
1042static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001043 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001044 "ne-resize"
1045};
1046
1047static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001048 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001049 "n-resize"
1050};
1051
1052static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001053 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001054 "ibeam",
1055 "text"
1056};
1057
1058static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001059 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001060 "pointer",
1061 "pointing_hand",
1062 "e29285e634086352946a0e7090d73106"
1063};
1064
1065static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001066 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001067 "wait",
1068 "0426c94ea35c87780ff01dc239897213"
1069};
1070
1071struct cursor_alternatives {
1072 const char **names;
1073 size_t count;
1074};
1075
1076static const struct cursor_alternatives cursors[] = {
1077 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1078 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1079 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1080 {grabbings, ARRAY_LENGTH(grabbings)},
1081 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1082 {left_sides, ARRAY_LENGTH(left_sides)},
1083 {right_sides, ARRAY_LENGTH(right_sides)},
1084 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1085 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1086 {top_sides, ARRAY_LENGTH(top_sides)},
1087 {xterms, ARRAY_LENGTH(xterms)},
1088 {hand1s, ARRAY_LENGTH(hand1s)},
1089 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001090};
1091
1092static void
1093create_cursors(struct display *display)
1094{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001095 char *config_file;
1096 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001097 unsigned int i, j;
1098 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001099 struct config_key shell_keys[] = {
1100 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1101 };
1102 struct config_section cs[] = {
1103 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1104 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001105
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001106 config_file = config_file_path("weston.ini");
1107 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1108 free(config_file);
1109
1110 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001111 display->cursors =
1112 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1113
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001114 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001115 cursor = NULL;
1116 for (j = 0; !cursor && j < cursors[i].count; ++j)
1117 cursor = wl_cursor_theme_get_cursor(
1118 display->cursor_theme, cursors[i].names[j]);
1119
1120 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001121 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001122 cursors[i].names[0]);
1123
1124 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001125 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001126}
1127
1128static void
1129destroy_cursors(struct display *display)
1130{
1131 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001132 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001133}
1134
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001135struct wl_cursor_image *
1136display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001137{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001138 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001140 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001141}
1142
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001143static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001144window_get_resize_dx_dy(struct window *window, int *x, int *y)
1145{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001146 struct surface *surface = window->main_surface;
1147
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001148 if (window->resize_edges & WINDOW_RESIZING_LEFT)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001149 *x = surface->server_allocation.width -
1150 surface->allocation.width;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001151 else
1152 *x = 0;
1153
1154 if (window->resize_edges & WINDOW_RESIZING_TOP)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001155 *y = surface->server_allocation.height -
1156 surface->allocation.height;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001157 else
1158 *y = 0;
1159
1160 window->resize_edges = 0;
1161}
1162
1163static void
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001164surface_attach_surface(struct surface *surface)
1165{
1166 surface->toysurface->swap(surface->toysurface,
1167 &surface->server_allocation);
1168}
1169
1170static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001171window_attach_surface(struct window *window)
1172{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001173 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001174
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001175 if (window->type == TYPE_NONE) {
1176 window->type = TYPE_TOPLEVEL;
1177 if (display->shell)
1178 wl_shell_surface_set_toplevel(window->shell_surface);
1179 }
1180
Pekka Paalanen512dde82012-10-10 12:49:27 +03001181 if (window->opaque_region) {
Pekka Paalanen4e373742013-02-13 16:17:13 +02001182 wl_surface_set_opaque_region(window->main_surface->surface,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001183 window->opaque_region);
1184 wl_region_destroy(window->opaque_region);
1185 window->opaque_region = NULL;
1186 }
1187
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001188 if (window->input_region) {
Pekka Paalanen4e373742013-02-13 16:17:13 +02001189 wl_surface_set_input_region(window->main_surface->surface,
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001190 window->input_region);
1191 wl_region_destroy(window->input_region);
1192 window->input_region = NULL;
1193 }
1194
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001195 surface_attach_surface(window->main_surface);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001196}
1197
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001198int
1199window_has_focus(struct window *window)
1200{
1201 return window->focus_count > 0;
1202}
1203
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001204static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001205window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001206{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001207 if (!window->cairo_surface)
1208 return;
1209
1210 window_attach_surface(window);
1211 cairo_surface_destroy(window->cairo_surface);
1212 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001213}
1214
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001215struct display *
1216window_get_display(struct window *window)
1217{
1218 return window->display;
1219}
1220
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001221static cairo_surface_t *
1222surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001223{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001224 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001225 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001226
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001227 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001228 case WL_OUTPUT_TRANSFORM_90:
1229 case WL_OUTPUT_TRANSFORM_270:
1230 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1231 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001232 allocation.width = surface->allocation.height;
1233 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001234 break;
1235 default:
1236 break;
1237 }
1238
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001239 if (!surface->toysurface && display->dpy &&
1240 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001241 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001242 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001243 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001244 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001245 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001246 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001247
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001248 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001249 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001250 surface->surface,
1251 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001252
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001253 return surface->toysurface->prepare(surface->toysurface, dx, dy,
1254 allocation.width,
1255 allocation.height,
1256 flags);
1257}
1258
1259static void
1260window_create_surface(struct window *window)
1261{
1262 uint32_t flags = 0;
1263 int dx, dy;
1264
1265 if (!window->transparent)
1266 flags |= SURFACE_OPAQUE;
1267
Pekka Paalanenec076692012-11-30 13:37:27 +02001268 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001269 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001270
Pekka Paalanen99436862012-11-19 17:15:59 +02001271 window_get_resize_dx_dy(window, &dx, &dy);
1272 window->cairo_surface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001273 surface_create_surface(window->main_surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001274}
1275
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001276int
1277window_get_buffer_transform(struct window *window)
1278{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001279 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001280}
1281
1282void
1283window_set_buffer_transform(struct window *window,
1284 enum wl_output_transform transform)
1285{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001286 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001287 wl_surface_set_buffer_transform(window->main_surface->surface,
1288 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001289}
1290
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001291static void frame_destroy(struct frame *frame);
1292
Pekka Paalanen4e373742013-02-13 16:17:13 +02001293static void
1294surface_destroy(struct surface *surface)
1295{
1296 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001297
1298 if (surface->toysurface)
1299 surface->toysurface->destroy(surface->toysurface);
1300
Pekka Paalanen4e373742013-02-13 16:17:13 +02001301 free(surface);
1302}
1303
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001304void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001305window_destroy(struct window *window)
1306{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001307 struct display *display = window->display;
1308 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001309 struct window_output *window_output;
1310 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001311
1312 if (window->redraw_scheduled)
1313 wl_list_remove(&window->redraw_task.link);
1314
1315 wl_list_for_each(input, &display->input_list, link) {
1316 if (input->pointer_focus == window)
1317 input->pointer_focus = NULL;
1318 if (input->keyboard_focus == window)
1319 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001320 if (input->focus_widget &&
1321 input->focus_widget->window == window)
1322 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001323 }
1324
Rob Bradford7507b572012-05-15 17:55:34 +01001325 wl_list_for_each_safe(window_output, window_output_tmp,
1326 &window->window_output_list, link) {
1327 free (window_output);
1328 }
1329
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001330 if (window->input_region)
1331 wl_region_destroy(window->input_region);
1332 if (window->opaque_region)
1333 wl_region_destroy(window->opaque_region);
1334
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001335 if (window->frame)
1336 frame_destroy(window->frame);
1337
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001338 if (window->shell_surface)
1339 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001340
1341 surface_destroy(window->main_surface);
1342
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001343 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001344
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001345 if (window->frame_cb)
1346 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001347 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001348 free(window);
1349}
1350
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001351static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001352widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001353{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001354 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001355
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001356 wl_list_for_each(child, &widget->child_list, link) {
1357 target = widget_find_widget(child, x, y);
1358 if (target)
1359 return target;
1360 }
1361
1362 if (widget->allocation.x <= x &&
1363 x < widget->allocation.x + widget->allocation.width &&
1364 widget->allocation.y <= y &&
1365 y < widget->allocation.y + widget->allocation.height) {
1366 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001367 }
1368
1369 return NULL;
1370}
1371
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001372static struct widget *
1373widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001374{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001375 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001376
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001377 widget = malloc(sizeof *widget);
1378 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001379 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001380 widget->user_data = data;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001381 widget->allocation = window->main_surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001382 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001383 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001384 widget->tooltip = NULL;
1385 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001386 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001387
1388 return widget;
1389}
1390
1391struct widget *
1392window_add_widget(struct window *window, void *data)
1393{
1394 window->widget = widget_create(window, data);
1395 wl_list_init(&window->widget->link);
1396
1397 return window->widget;
1398}
1399
1400struct widget *
1401widget_add_widget(struct widget *parent, void *data)
1402{
1403 struct widget *widget;
1404
1405 widget = widget_create(parent->window, data);
1406 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001407
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001408 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001409}
1410
1411void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001412widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001413{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001414 struct display *display = widget->window->display;
1415 struct input *input;
1416
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001417 if (widget->tooltip) {
1418 free(widget->tooltip);
1419 widget->tooltip = NULL;
1420 }
1421
Pekka Paalanene156fb62012-01-19 13:51:38 +02001422 wl_list_for_each(input, &display->input_list, link) {
1423 if (input->focus_widget == widget)
1424 input->focus_widget = NULL;
1425 }
1426
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001427 wl_list_remove(&widget->link);
1428 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001429}
1430
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001431void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001432widget_set_default_cursor(struct widget *widget, int cursor)
1433{
1434 widget->default_cursor = cursor;
1435}
1436
1437void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001438widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001439{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001440 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001441}
1442
1443void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001444widget_set_size(struct widget *widget, int32_t width, int32_t height)
1445{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001446 widget->allocation.width = width;
1447 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001448}
1449
1450void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001451widget_set_allocation(struct widget *widget,
1452 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001453{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001454 widget->allocation.x = x;
1455 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001456 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001457}
1458
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001459void
1460widget_set_transparent(struct widget *widget, int transparent)
1461{
1462 widget->opaque = !transparent;
1463}
1464
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001465void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001466widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001467{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001468 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001469}
1470
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001471void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001472widget_set_resize_handler(struct widget *widget,
1473 widget_resize_handler_t handler)
1474{
1475 widget->resize_handler = handler;
1476}
1477
1478void
1479widget_set_redraw_handler(struct widget *widget,
1480 widget_redraw_handler_t handler)
1481{
1482 widget->redraw_handler = handler;
1483}
1484
1485void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001486widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001487{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001488 widget->enter_handler = handler;
1489}
1490
1491void
1492widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1493{
1494 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001495}
1496
1497void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001498widget_set_motion_handler(struct widget *widget,
1499 widget_motion_handler_t handler)
1500{
1501 widget->motion_handler = handler;
1502}
1503
1504void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001505widget_set_button_handler(struct widget *widget,
1506 widget_button_handler_t handler)
1507{
1508 widget->button_handler = handler;
1509}
1510
1511void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001512widget_set_axis_handler(struct widget *widget,
1513 widget_axis_handler_t handler)
1514{
1515 widget->axis_handler = handler;
1516}
1517
1518void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001519widget_schedule_redraw(struct widget *widget)
1520{
1521 window_schedule_redraw(widget->window);
1522}
1523
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001524cairo_surface_t *
1525window_get_surface(struct window *window)
1526{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001527 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001528}
1529
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001530struct wl_surface *
1531window_get_wl_surface(struct window *window)
1532{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001533 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001534}
1535
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001536struct wl_shell_surface *
1537window_get_wl_shell_surface(struct window *window)
1538{
1539 return window->shell_surface;
1540}
1541
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001542static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001543tooltip_redraw_handler(struct widget *widget, void *data)
1544{
1545 cairo_t *cr;
1546 const int32_t r = 3;
1547 struct tooltip *tooltip = data;
1548 int32_t width, height;
1549 struct window *window = widget->window;
1550
1551 cr = cairo_create(window->cairo_surface);
1552 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1553 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1554 cairo_paint(cr);
1555
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001556 width = window->main_surface->allocation.width;
1557 height = window->main_surface->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001558 rounded_rect(cr, 0, 0, width, height, r);
1559
1560 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1561 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1562 cairo_fill(cr);
1563
1564 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1565 cairo_move_to(cr, 10, 16);
1566 cairo_show_text(cr, tooltip->entry);
1567 cairo_destroy(cr);
1568}
1569
1570static cairo_text_extents_t
1571get_text_extents(struct tooltip *tooltip)
1572{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001573 cairo_t *cr;
1574 cairo_text_extents_t extents;
1575
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001576 /* Use the dummy_surface because tooltip's surface was not
1577 * created yet, and parent does not have a valid surface
1578 * outside repaint, either.
1579 */
1580 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001581 cairo_text_extents(cr, tooltip->entry, &extents);
1582 cairo_destroy(cr);
1583
1584 return extents;
1585}
1586
1587static int
1588window_create_tooltip(struct tooltip *tooltip)
1589{
1590 struct widget *parent = tooltip->parent;
1591 struct display *display = parent->window->display;
1592 struct window *window;
1593 const int offset_y = 27;
1594 const int margin = 3;
1595 cairo_text_extents_t extents;
1596
1597 if (tooltip->widget)
1598 return 0;
1599
1600 window = window_create_transient(display, parent->window, tooltip->x,
1601 tooltip->y + offset_y,
1602 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1603 if (!window)
1604 return -1;
1605
1606 tooltip->window = window;
1607 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1608
1609 extents = get_text_extents(tooltip);
1610 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1611 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1612
1613 return 0;
1614}
1615
1616void
1617widget_destroy_tooltip(struct widget *parent)
1618{
1619 struct tooltip *tooltip = parent->tooltip;
1620
1621 parent->tooltip_count = 0;
1622 if (!tooltip)
1623 return;
1624
1625 if (tooltip->widget) {
1626 widget_destroy(tooltip->widget);
1627 window_destroy(tooltip->window);
1628 tooltip->widget = NULL;
1629 tooltip->window = NULL;
1630 }
1631
1632 close(tooltip->tooltip_fd);
1633 free(tooltip->entry);
1634 free(tooltip);
1635 parent->tooltip = NULL;
1636}
1637
1638static void
1639tooltip_func(struct task *task, uint32_t events)
1640{
1641 struct tooltip *tooltip =
1642 container_of(task, struct tooltip, tooltip_task);
1643 uint64_t exp;
1644
Martin Olsson8df662a2012-07-08 03:03:47 +02001645 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1646 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001647 window_create_tooltip(tooltip);
1648}
1649
1650#define TOOLTIP_TIMEOUT 500
1651static int
1652tooltip_timer_reset(struct tooltip *tooltip)
1653{
1654 struct itimerspec its;
1655
1656 its.it_interval.tv_sec = 0;
1657 its.it_interval.tv_nsec = 0;
1658 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1659 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1660 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1661 fprintf(stderr, "could not set timerfd\n: %m");
1662 return -1;
1663 }
1664
1665 return 0;
1666}
1667
1668int
1669widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1670{
1671 struct tooltip *tooltip = parent->tooltip;
1672
1673 parent->tooltip_count++;
1674 if (tooltip) {
1675 tooltip->x = x;
1676 tooltip->y = y;
1677 tooltip_timer_reset(tooltip);
1678 return 0;
1679 }
1680
1681 /* the handler might be triggered too fast via input device motion, so
1682 * we need this check here to make sure tooltip is fully initialized */
1683 if (parent->tooltip_count > 1)
1684 return 0;
1685
1686 tooltip = malloc(sizeof *tooltip);
1687 if (!tooltip)
1688 return -1;
1689
1690 parent->tooltip = tooltip;
1691 tooltip->parent = parent;
1692 tooltip->widget = NULL;
1693 tooltip->window = NULL;
1694 tooltip->x = x;
1695 tooltip->y = y;
1696 tooltip->entry = strdup(entry);
1697 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1698 if (tooltip->tooltip_fd < 0) {
1699 fprintf(stderr, "could not create timerfd\n: %m");
1700 return -1;
1701 }
1702
1703 tooltip->tooltip_task.run = tooltip_func;
1704 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1705 EPOLLIN, &tooltip->tooltip_task);
1706 tooltip_timer_reset(tooltip);
1707
1708 return 0;
1709}
1710
1711static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001712workspace_manager_state(void *data,
1713 struct workspace_manager *workspace_manager,
1714 uint32_t current,
1715 uint32_t count)
1716{
1717 struct display *display = data;
1718
1719 display->workspace = current;
1720 display->workspace_count = count;
1721}
1722
1723static const struct workspace_manager_listener workspace_manager_listener = {
1724 workspace_manager_state
1725};
1726
1727static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001728frame_resize_handler(struct widget *widget,
1729 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001730{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001731 struct frame *frame = data;
1732 struct widget *child = frame->child;
1733 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001734 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001735 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001736 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001737 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001738 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001739 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001740
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001741 switch (widget->window->type) {
1742 case TYPE_FULLSCREEN:
1743 decoration_width = 0;
1744 decoration_height = 0;
1745
1746 allocation.x = 0;
1747 allocation.y = 0;
1748 allocation.width = width;
1749 allocation.height = height;
1750 opaque_margin = 0;
1751
1752 wl_list_for_each(button, &frame->buttons_list, link)
1753 button->widget->opaque = 1;
1754 break;
1755 case TYPE_MAXIMIZED:
1756 decoration_width = t->width * 2;
1757 decoration_height = t->width + t->titlebar_height;
1758
1759 allocation.x = t->width;
1760 allocation.y = t->titlebar_height;
1761 allocation.width = width - decoration_width;
1762 allocation.height = height - decoration_height;
1763
1764 opaque_margin = 0;
1765
1766 wl_list_for_each(button, &frame->buttons_list, link)
1767 button->widget->opaque = 0;
1768 break;
1769 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001770 decoration_width = (t->width + t->margin) * 2;
1771 decoration_height = t->width +
1772 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001773
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001774 allocation.x = t->width + t->margin;
1775 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001776 allocation.width = width - decoration_width;
1777 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001778
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001779 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001780
1781 wl_list_for_each(button, &frame->buttons_list, link)
1782 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001783 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001784 }
1785
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001786 widget_set_allocation(child, allocation.x, allocation.y,
1787 allocation.width, allocation.height);
1788
1789 if (child->resize_handler)
1790 child->resize_handler(child,
1791 allocation.width,
1792 allocation.height,
1793 child->user_data);
1794
Scott Moreauf7e498c2012-05-14 11:39:29 -06001795 width = child->allocation.width + decoration_width;
1796 height = child->allocation.height + decoration_height;
1797
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001798 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1799
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001800 widget->window->input_region =
1801 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001802 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001803 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001804 shadow_margin, shadow_margin,
1805 width - 2 * shadow_margin,
1806 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001807 } else {
1808 wl_region_add(widget->window->input_region,
1809 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001810 }
1811
Scott Moreauf7e498c2012-05-14 11:39:29 -06001812 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001813
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001814 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001815 wl_region_add(widget->window->opaque_region,
1816 opaque_margin, opaque_margin,
1817 widget->allocation.width - 2 * opaque_margin,
1818 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001819
1820 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001821 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1822 x_l = t->width + shadow_margin;
1823 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001824 wl_list_for_each(button, &frame->buttons_list, link) {
1825 const int button_padding = 4;
1826 w = cairo_image_surface_get_width(button->icon);
1827 h = cairo_image_surface_get_height(button->icon);
1828
1829 if (button->decoration == FRAME_BUTTON_FANCY)
1830 w += 10;
1831
1832 if (button->align == FRAME_BUTTON_LEFT) {
1833 widget_set_allocation(button->widget,
1834 x_l, y , w + 1, h + 1);
1835 x_l += w;
1836 x_l += button_padding;
1837 } else {
1838 x_r -= w;
1839 widget_set_allocation(button->widget,
1840 x_r, y , w + 1, h + 1);
1841 x_r -= button_padding;
1842 }
1843 }
1844}
1845
1846static int
1847frame_button_enter_handler(struct widget *widget,
1848 struct input *input, float x, float y, void *data)
1849{
1850 struct frame_button *frame_button = data;
1851
1852 widget_schedule_redraw(frame_button->widget);
1853 frame_button->state = FRAME_BUTTON_OVER;
1854
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001855 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001856}
1857
1858static void
1859frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1860{
1861 struct frame_button *frame_button = data;
1862
1863 widget_schedule_redraw(frame_button->widget);
1864 frame_button->state = FRAME_BUTTON_DEFAULT;
1865}
1866
1867static void
1868frame_button_button_handler(struct widget *widget,
1869 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001870 uint32_t button,
1871 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001872{
1873 struct frame_button *frame_button = data;
1874 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001875 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001876
1877 if (button != BTN_LEFT)
1878 return;
1879
1880 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001881 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001882 frame_button->state = FRAME_BUTTON_ACTIVE;
1883 widget_schedule_redraw(frame_button->widget);
1884
1885 if (frame_button->type == FRAME_BUTTON_ICON)
1886 window_show_frame_menu(window, input, time);
1887 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001888 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001889 frame_button->state = FRAME_BUTTON_DEFAULT;
1890 widget_schedule_redraw(frame_button->widget);
1891 break;
1892 }
1893
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001894 if (!was_pressed)
1895 return;
1896
Martin Minarik1998b152012-05-10 02:04:35 +02001897 switch (frame_button->type) {
1898 case FRAME_BUTTON_CLOSE:
1899 if (window->close_handler)
1900 window->close_handler(window->parent,
1901 window->user_data);
1902 else
1903 display_exit(window->display);
1904 break;
1905 case FRAME_BUTTON_MINIMIZE:
1906 fprintf(stderr,"Minimize stub\n");
1907 break;
1908 case FRAME_BUTTON_MAXIMIZE:
1909 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1910 break;
1911 default:
1912 /* Unknown operation */
1913 break;
1914 }
1915}
1916
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001917static int
1918frame_button_motion_handler(struct widget *widget,
1919 struct input *input, uint32_t time,
1920 float x, float y, void *data)
1921{
1922 struct frame_button *frame_button = data;
1923 enum frame_button_pointer previous_button_state = frame_button->state;
1924
1925 /* only track state for a pressed button */
1926 if (input->grab != widget)
1927 return CURSOR_LEFT_PTR;
1928
1929 if (x > widget->allocation.x &&
1930 x < (widget->allocation.x + widget->allocation.width) &&
1931 y > widget->allocation.y &&
1932 y < (widget->allocation.y + widget->allocation.height)) {
1933 frame_button->state = FRAME_BUTTON_ACTIVE;
1934 } else {
1935 frame_button->state = FRAME_BUTTON_DEFAULT;
1936 }
1937
1938 if (frame_button->state != previous_button_state)
1939 widget_schedule_redraw(frame_button->widget);
1940
1941 return CURSOR_LEFT_PTR;
1942}
1943
Martin Minarik1998b152012-05-10 02:04:35 +02001944static void
1945frame_button_redraw_handler(struct widget *widget, void *data)
1946{
1947 struct frame_button *frame_button = data;
1948 cairo_t *cr;
1949 int width, height, x, y;
1950 struct window *window = widget->window;
1951
1952 x = widget->allocation.x;
1953 y = widget->allocation.y;
1954 width = widget->allocation.width;
1955 height = widget->allocation.height;
1956
1957 if (!width)
1958 return;
1959 if (!height)
1960 return;
1961 if (widget->opaque)
1962 return;
1963
1964 cr = cairo_create(window->cairo_surface);
1965
1966 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1967 cairo_set_line_width(cr, 1);
1968
1969 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1970 cairo_rectangle (cr, x, y, 25, 16);
1971
1972 cairo_stroke_preserve(cr);
1973
1974 switch (frame_button->state) {
1975 case FRAME_BUTTON_DEFAULT:
1976 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1977 break;
1978 case FRAME_BUTTON_OVER:
1979 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1980 break;
1981 case FRAME_BUTTON_ACTIVE:
1982 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1983 break;
1984 }
1985
1986 cairo_fill (cr);
1987
1988 x += 4;
1989 }
1990
1991 cairo_set_source_surface(cr, frame_button->icon, x, y);
1992 cairo_paint(cr);
1993
1994 cairo_destroy(cr);
1995}
1996
1997static struct widget *
1998frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1999 enum frame_button_align align, enum frame_button_decoration style)
2000{
2001 struct frame_button *frame_button;
2002 const char *icon = data;
2003
2004 frame_button = malloc (sizeof *frame_button);
2005 memset(frame_button, 0, sizeof *frame_button);
2006
2007 frame_button->icon = cairo_image_surface_create_from_png(icon);
2008 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2009 frame_button->frame = frame;
2010 frame_button->type = type;
2011 frame_button->align = align;
2012 frame_button->decoration = style;
2013
2014 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2015
2016 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2017 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2018 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2019 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002020 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002021 return frame_button->widget;
2022}
2023
2024static void
2025frame_button_destroy(struct frame_button *frame_button)
2026{
2027 widget_destroy(frame_button->widget);
2028 wl_list_remove(&frame_button->link);
2029 cairo_surface_destroy(frame_button->icon);
2030 free(frame_button);
2031
2032 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002033}
2034
2035static void
2036frame_redraw_handler(struct widget *widget, void *data)
2037{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002038 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002039 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002040 struct theme *t = window->display->theme;
2041 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002042
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002043 if (window->type == TYPE_FULLSCREEN)
2044 return;
2045
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002046 cr = cairo_create(window->cairo_surface);
2047
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002048 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002049 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002050 if (window->type == TYPE_MAXIMIZED)
2051 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002052 theme_render_frame(t, cr, widget->allocation.width,
2053 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002054
2055 cairo_destroy(cr);
2056}
2057
2058static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002059frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002060{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002061 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002062 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002063 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002064
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002065 location = theme_get_location(t, input->sx, input->sy,
2066 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002067 frame->widget->allocation.height,
2068 window->type == TYPE_MAXIMIZED ?
2069 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002070
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002071 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002072 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002073 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002074 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002075 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002076 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002077 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002078 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002079 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002080 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002081 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002082 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002083 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002084 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002085 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002086 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002087 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002088 case THEME_LOCATION_EXTERIOR:
2089 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002090 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002091 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002092 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002093}
2094
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002095static void
2096frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002097{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002098 struct display *display;
2099
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002100 switch (index) {
2101 case 0: /* close */
2102 if (window->close_handler)
2103 window->close_handler(window->parent,
2104 window->user_data);
2105 else
2106 display_exit(window->display);
2107 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002108 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002109 display = window->display;
2110 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002111 workspace_manager_move_surface(
2112 display->workspace_manager,
2113 window->main_surface->surface,
2114 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002115 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002116 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002117 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002118 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002119 workspace_manager_move_surface(
2120 display->workspace_manager,
2121 window->main_surface->surface,
2122 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002123 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002124 case 3: /* fullscreen */
2125 /* we don't have a way to get out of fullscreen for now */
2126 if (window->fullscreen_handler)
2127 window->fullscreen_handler(window, window->user_data);
2128 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002129 }
2130}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002131
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002132void
2133window_show_frame_menu(struct window *window,
2134 struct input *input, uint32_t time)
2135{
2136 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002137 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002138
2139 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002140 "Close",
2141 "Move to workspace above", "Move to workspace below",
2142 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002143 };
2144
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002145 if (window->fullscreen_handler)
2146 count = ARRAY_LENGTH(entries);
2147 else
2148 count = ARRAY_LENGTH(entries) - 1;
2149
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002150 input_get_position(input, &x, &y);
2151 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002152 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002153}
2154
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002155static int
2156frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002157 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002158{
2159 return frame_get_pointer_image_for_location(data, input);
2160}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002161
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162static int
2163frame_motion_handler(struct widget *widget,
2164 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002165 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002166{
2167 return frame_get_pointer_image_for_location(data, input);
2168}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002169
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002170static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002171frame_button_handler(struct widget *widget,
2172 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002173 uint32_t button, enum wl_pointer_button_state state,
2174 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002175
2176{
2177 struct frame *frame = data;
2178 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002179 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002180 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002181
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002182 location = theme_get_location(display->theme, input->sx, input->sy,
2183 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002184 frame->widget->allocation.height,
2185 window->type == TYPE_MAXIMIZED ?
2186 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002187
Daniel Stone4dbadb12012-05-30 16:31:51 +01002188 if (window->display->shell && button == BTN_LEFT &&
2189 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002190 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002191 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192 if (!window->shell_surface)
2193 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002194 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002195 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002196 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002197 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002198 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002199 case THEME_LOCATION_RESIZING_TOP:
2200 case THEME_LOCATION_RESIZING_BOTTOM:
2201 case THEME_LOCATION_RESIZING_LEFT:
2202 case THEME_LOCATION_RESIZING_RIGHT:
2203 case THEME_LOCATION_RESIZING_TOP_LEFT:
2204 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2205 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2206 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002207 if (!window->shell_surface)
2208 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002209 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002210
Pekka Paalanen99436862012-11-19 17:15:59 +02002211 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002213 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002214 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002215 break;
2216 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002217 } else if (button == BTN_RIGHT &&
2218 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002219 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002220 }
2221}
2222
2223struct widget *
2224frame_create(struct window *window, void *data)
2225{
2226 struct frame *frame;
2227
2228 frame = malloc(sizeof *frame);
2229 memset(frame, 0, sizeof *frame);
2230
2231 frame->widget = window_add_widget(window, frame);
2232 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002233
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002234 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2235 widget_set_resize_handler(frame->widget, frame_resize_handler);
2236 widget_set_enter_handler(frame->widget, frame_enter_handler);
2237 widget_set_motion_handler(frame->widget, frame_motion_handler);
2238 widget_set_button_handler(frame->widget, frame_button_handler);
2239
Martin Minarik1998b152012-05-10 02:04:35 +02002240 /* Create empty list for frame buttons */
2241 wl_list_init(&frame->buttons_list);
2242
2243 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2244 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2245
2246 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2247 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2248
2249 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2250 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2251
2252 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2253 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2254
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002255 window->frame = frame;
2256
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002257 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002258}
2259
Kristian Høgsberga1627922012-06-20 17:30:03 -04002260void
2261frame_set_child_size(struct widget *widget, int child_width, int child_height)
2262{
2263 struct display *display = widget->window->display;
2264 struct theme *t = display->theme;
2265 int decoration_width, decoration_height;
2266 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002267 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002268
2269 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002270 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002271 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002272 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002273
2274 width = child_width + decoration_width;
2275 height = child_height + decoration_height;
2276 } else {
2277 width = child_width;
2278 height = child_height;
2279 }
2280
2281 window_schedule_resize(widget->window, width, height);
2282}
2283
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002284static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002285frame_destroy(struct frame *frame)
2286{
Martin Minarik1998b152012-05-10 02:04:35 +02002287 struct frame_button *button, *tmp;
2288
2289 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2290 frame_button_destroy(button);
2291
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002292 /* frame->child must be destroyed by the application */
2293 widget_destroy(frame->widget);
2294 free(frame);
2295}
2296
2297static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002298input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002299 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002300{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002301 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002302 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002303
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002304 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002305 return;
2306
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002307 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002308 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002309 widget = old;
2310 if (input->grab)
2311 widget = input->grab;
2312 if (widget->leave_handler)
2313 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002314 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002315 }
2316
2317 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002318 widget = focus;
2319 if (input->grab)
2320 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002321 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002322 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002323 cursor = widget->enter_handler(focus, input, x, y,
2324 widget->user_data);
2325 else
2326 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002327
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002328 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002329 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002330}
2331
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002332void
2333input_grab(struct input *input, struct widget *widget, uint32_t button)
2334{
2335 input->grab = widget;
2336 input->grab_button = button;
2337}
2338
2339void
2340input_ungrab(struct input *input)
2341{
2342 struct widget *widget;
2343
2344 input->grab = NULL;
2345 if (input->pointer_focus) {
2346 widget = widget_find_widget(input->pointer_focus->widget,
2347 input->sx, input->sy);
2348 input_set_focus_widget(input, widget, input->sx, input->sy);
2349 }
2350}
2351
2352static void
2353input_remove_pointer_focus(struct input *input)
2354{
2355 struct window *window = input->pointer_focus;
2356
2357 if (!window)
2358 return;
2359
2360 input_set_focus_widget(input, NULL, 0, 0);
2361
2362 input->pointer_focus = NULL;
2363 input->current_cursor = CURSOR_UNSET;
2364}
2365
2366static void
2367pointer_handle_enter(void *data, struct wl_pointer *pointer,
2368 uint32_t serial, struct wl_surface *surface,
2369 wl_fixed_t sx_w, wl_fixed_t sy_w)
2370{
2371 struct input *input = data;
2372 struct window *window;
2373 struct widget *widget;
2374 float sx = wl_fixed_to_double(sx_w);
2375 float sy = wl_fixed_to_double(sy_w);
2376
2377 if (!surface) {
2378 /* enter event for a window we've just destroyed */
2379 return;
2380 }
2381
2382 input->display->serial = serial;
2383 input->pointer_enter_serial = serial;
2384 input->pointer_focus = wl_surface_get_user_data(surface);
2385 window = input->pointer_focus;
2386
Pekka Paalanen99436862012-11-19 17:15:59 +02002387 if (window->resizing) {
2388 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002389 /* Schedule a redraw to free the pool */
2390 window_schedule_redraw(window);
2391 }
2392
2393 input->sx = sx;
2394 input->sy = sy;
2395
2396 widget = widget_find_widget(window->widget, sx, sy);
2397 input_set_focus_widget(input, widget, sx, sy);
2398}
2399
2400static void
2401pointer_handle_leave(void *data, struct wl_pointer *pointer,
2402 uint32_t serial, struct wl_surface *surface)
2403{
2404 struct input *input = data;
2405
2406 input->display->serial = serial;
2407 input_remove_pointer_focus(input);
2408}
2409
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002410static void
Daniel Stone37816df2012-05-16 18:45:18 +01002411pointer_handle_motion(void *data, struct wl_pointer *pointer,
2412 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002413{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002414 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002415 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002416 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002417 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002418 float sx = wl_fixed_to_double(sx_w);
2419 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002420
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002421 input->sx = sx;
2422 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002423
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002424 if (!window)
2425 return;
2426
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002427 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002428 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002429 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002430 }
2431
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002432 if (input->grab)
2433 widget = input->grab;
2434 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002435 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002436 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002437 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002438 input, time, sx, sy,
2439 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002440 else
2441 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002442
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002443 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002444}
2445
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002446static void
Daniel Stone37816df2012-05-16 18:45:18 +01002447pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002448 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002449{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002450 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002451 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002452 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002453
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002454 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002455 if (input->focus_widget && input->grab == NULL &&
2456 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002457 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002458
Neil Roberts6b28aad2012-01-23 19:11:18 +00002459 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002460 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002461 (*widget->button_handler)(widget,
2462 input, time,
2463 button, state,
2464 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002465
Daniel Stone4dbadb12012-05-30 16:31:51 +01002466 if (input->grab && input->grab_button == button &&
2467 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002468 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002469}
2470
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002471static void
Daniel Stone37816df2012-05-16 18:45:18 +01002472pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002473 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002474{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002475 struct input *input = data;
2476 struct widget *widget;
2477
2478 widget = input->focus_widget;
2479 if (input->grab)
2480 widget = input->grab;
2481 if (widget && widget->axis_handler)
2482 (*widget->axis_handler)(widget,
2483 input, time,
2484 axis, value,
2485 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002486}
2487
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002488static const struct wl_pointer_listener pointer_listener = {
2489 pointer_handle_enter,
2490 pointer_handle_leave,
2491 pointer_handle_motion,
2492 pointer_handle_button,
2493 pointer_handle_axis,
2494};
2495
2496static void
2497input_remove_keyboard_focus(struct input *input)
2498{
2499 struct window *window = input->keyboard_focus;
2500 struct itimerspec its;
2501
2502 its.it_interval.tv_sec = 0;
2503 its.it_interval.tv_nsec = 0;
2504 its.it_value.tv_sec = 0;
2505 its.it_value.tv_nsec = 0;
2506 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2507
2508 if (!window)
2509 return;
2510
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002511 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002512 if (window->keyboard_focus_handler)
2513 (*window->keyboard_focus_handler)(window, NULL,
2514 window->user_data);
2515
2516 input->keyboard_focus = NULL;
2517}
2518
2519static void
2520keyboard_repeat_func(struct task *task, uint32_t events)
2521{
2522 struct input *input =
2523 container_of(task, struct input, repeat_task);
2524 struct window *window = input->keyboard_focus;
2525 uint64_t exp;
2526
2527 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2528 /* If we change the timer between the fd becoming
2529 * readable and getting here, there'll be nothing to
2530 * read and we get EAGAIN. */
2531 return;
2532
2533 if (window && window->key_handler) {
2534 (*window->key_handler)(window, input, input->repeat_time,
2535 input->repeat_key, input->repeat_sym,
2536 WL_KEYBOARD_KEY_STATE_PRESSED,
2537 window->user_data);
2538 }
2539}
2540
2541static void
2542keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2543 uint32_t format, int fd, uint32_t size)
2544{
2545 struct input *input = data;
2546 char *map_str;
2547
2548 if (!data) {
2549 close(fd);
2550 return;
2551 }
2552
2553 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2554 close(fd);
2555 return;
2556 }
2557
2558 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2559 if (map_str == MAP_FAILED) {
2560 close(fd);
2561 return;
2562 }
2563
2564 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2565 map_str,
2566 XKB_KEYMAP_FORMAT_TEXT_V1,
2567 0);
2568 munmap(map_str, size);
2569 close(fd);
2570
2571 if (!input->xkb.keymap) {
2572 fprintf(stderr, "failed to compile keymap\n");
2573 return;
2574 }
2575
2576 input->xkb.state = xkb_state_new(input->xkb.keymap);
2577 if (!input->xkb.state) {
2578 fprintf(stderr, "failed to create XKB state\n");
2579 xkb_map_unref(input->xkb.keymap);
2580 input->xkb.keymap = NULL;
2581 return;
2582 }
2583
2584 input->xkb.control_mask =
2585 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2586 input->xkb.alt_mask =
2587 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2588 input->xkb.shift_mask =
2589 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2590}
2591
2592static void
2593keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2594 uint32_t serial, struct wl_surface *surface,
2595 struct wl_array *keys)
2596{
2597 struct input *input = data;
2598 struct window *window;
2599
2600 input->display->serial = serial;
2601 input->keyboard_focus = wl_surface_get_user_data(surface);
2602
2603 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002604 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002605 if (window->keyboard_focus_handler)
2606 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002607 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002608}
2609
2610static void
2611keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2612 uint32_t serial, struct wl_surface *surface)
2613{
2614 struct input *input = data;
2615
2616 input->display->serial = serial;
2617 input_remove_keyboard_focus(input);
2618}
2619
Scott Moreau210d0792012-03-22 10:47:01 -06002620static void
Daniel Stone37816df2012-05-16 18:45:18 +01002621keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002622 uint32_t serial, uint32_t time, uint32_t key,
2623 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002624{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002625 struct input *input = data;
2626 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002627 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002628 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002629 const xkb_keysym_t *syms;
2630 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002631 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002632
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002633 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002634 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002635 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002636 return;
2637
Daniel Stone97f68542012-05-30 16:32:01 +01002638 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002639
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002640 sym = XKB_KEY_NoSymbol;
2641 if (num_syms == 1)
2642 sym = syms[0];
2643
2644 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002645 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002646 window_set_maximized(window,
2647 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002648 } else if (sym == XKB_KEY_F11 &&
2649 window->fullscreen_handler &&
2650 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2651 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002652 } else if (sym == XKB_KEY_F4 &&
2653 input->modifiers == MOD_ALT_MASK &&
2654 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2655 if (window->close_handler)
2656 window->close_handler(window->parent,
2657 window->user_data);
2658 else
2659 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002660 } else if (window->key_handler) {
2661 (*window->key_handler)(window, input, time, key,
2662 sym, state, window->user_data);
2663 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002664
2665 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2666 key == input->repeat_key) {
2667 its.it_interval.tv_sec = 0;
2668 its.it_interval.tv_nsec = 0;
2669 its.it_value.tv_sec = 0;
2670 its.it_value.tv_nsec = 0;
2671 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2672 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2673 input->repeat_sym = sym;
2674 input->repeat_key = key;
2675 input->repeat_time = time;
2676 its.it_interval.tv_sec = 0;
2677 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2678 its.it_value.tv_sec = 0;
2679 its.it_value.tv_nsec = 400 * 1000 * 1000;
2680 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2681 }
2682}
2683
2684static void
Daniel Stone351eb612012-05-31 15:27:47 -04002685keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2686 uint32_t serial, uint32_t mods_depressed,
2687 uint32_t mods_latched, uint32_t mods_locked,
2688 uint32_t group)
2689{
2690 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002691 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002692
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002693 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2694 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002695 mask = xkb_state_serialize_mods(input->xkb.state,
2696 XKB_STATE_DEPRESSED |
2697 XKB_STATE_LATCHED);
2698 input->modifiers = 0;
2699 if (mask & input->xkb.control_mask)
2700 input->modifiers |= MOD_CONTROL_MASK;
2701 if (mask & input->xkb.alt_mask)
2702 input->modifiers |= MOD_ALT_MASK;
2703 if (mask & input->xkb.shift_mask)
2704 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002705}
2706
Daniel Stone37816df2012-05-16 18:45:18 +01002707static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002708 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002709 keyboard_handle_enter,
2710 keyboard_handle_leave,
2711 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002712 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002713};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002714
2715static void
Daniel Stone37816df2012-05-16 18:45:18 +01002716seat_handle_capabilities(void *data, struct wl_seat *seat,
2717 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002718{
Daniel Stone37816df2012-05-16 18:45:18 +01002719 struct input *input = data;
2720
2721 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2722 input->pointer = wl_seat_get_pointer(seat);
2723 wl_pointer_set_user_data(input->pointer, input);
2724 wl_pointer_add_listener(input->pointer, &pointer_listener,
2725 input);
2726 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2727 wl_pointer_destroy(input->pointer);
2728 input->pointer = NULL;
2729 }
2730
2731 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2732 input->keyboard = wl_seat_get_keyboard(seat);
2733 wl_keyboard_set_user_data(input->keyboard, input);
2734 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2735 input);
2736 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2737 wl_keyboard_destroy(input->keyboard);
2738 input->keyboard = NULL;
2739 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002740}
2741
Daniel Stone37816df2012-05-16 18:45:18 +01002742static const struct wl_seat_listener seat_listener = {
2743 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002744};
2745
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002746void
2747input_get_position(struct input *input, int32_t *x, int32_t *y)
2748{
2749 *x = input->sx;
2750 *y = input->sy;
2751}
2752
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002753struct display *
2754input_get_display(struct input *input)
2755{
2756 return input->display;
2757}
2758
Daniel Stone37816df2012-05-16 18:45:18 +01002759struct wl_seat *
2760input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002761{
Daniel Stone37816df2012-05-16 18:45:18 +01002762 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002763}
2764
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002765uint32_t
2766input_get_modifiers(struct input *input)
2767{
2768 return input->modifiers;
2769}
2770
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002771struct widget *
2772input_get_focus_widget(struct input *input)
2773{
2774 return input->focus_widget;
2775}
2776
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002777struct data_offer {
2778 struct wl_data_offer *offer;
2779 struct input *input;
2780 struct wl_array types;
2781 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002782
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002783 struct task io_task;
2784 int fd;
2785 data_func_t func;
2786 int32_t x, y;
2787 void *user_data;
2788};
2789
2790static void
2791data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2792{
2793 struct data_offer *offer = data;
2794 char **p;
2795
2796 p = wl_array_add(&offer->types, sizeof *p);
2797 *p = strdup(type);
2798}
2799
2800static const struct wl_data_offer_listener data_offer_listener = {
2801 data_offer_offer,
2802};
2803
2804static void
2805data_offer_destroy(struct data_offer *offer)
2806{
2807 char **p;
2808
2809 offer->refcount--;
2810 if (offer->refcount == 0) {
2811 wl_data_offer_destroy(offer->offer);
2812 for (p = offer->types.data; *p; p++)
2813 free(*p);
2814 wl_array_release(&offer->types);
2815 free(offer);
2816 }
2817}
2818
2819static void
2820data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002821 struct wl_data_device *data_device,
2822 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002823{
2824 struct data_offer *offer;
2825
2826 offer = malloc(sizeof *offer);
2827
2828 wl_array_init(&offer->types);
2829 offer->refcount = 1;
2830 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002831 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002832 wl_data_offer_add_listener(offer->offer,
2833 &data_offer_listener, offer);
2834}
2835
2836static void
2837data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002838 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002839 wl_fixed_t x_w, wl_fixed_t y_w,
2840 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002841{
2842 struct input *input = data;
2843 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002844 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002845 float x = wl_fixed_to_double(x_w);
2846 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002847 char **p;
2848
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002849 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002850 window = wl_surface_get_user_data(surface);
2851 input->pointer_focus = window;
2852
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002853 if (offer) {
2854 input->drag_offer = wl_data_offer_get_user_data(offer);
2855
2856 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2857 *p = NULL;
2858
2859 types_data = input->drag_offer->types.data;
2860 } else {
2861 input->drag_offer = NULL;
2862 types_data = NULL;
2863 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002864
2865 window = input->pointer_focus;
2866 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002867 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002868 window->user_data);
2869}
2870
2871static void
2872data_device_leave(void *data, struct wl_data_device *data_device)
2873{
2874 struct input *input = data;
2875
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002876 if (input->drag_offer) {
2877 data_offer_destroy(input->drag_offer);
2878 input->drag_offer = NULL;
2879 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002880}
2881
2882static void
2883data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002884 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002885{
2886 struct input *input = data;
2887 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002888 float x = wl_fixed_to_double(x_w);
2889 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002890 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002891
2892 input->sx = x;
2893 input->sy = y;
2894
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002895 if (input->drag_offer)
2896 types_data = input->drag_offer->types.data;
2897 else
2898 types_data = NULL;
2899
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002900 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002901 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002902 window->user_data);
2903}
2904
2905static void
2906data_device_drop(void *data, struct wl_data_device *data_device)
2907{
2908 struct input *input = data;
2909 struct window *window = input->pointer_focus;
2910
2911 if (window->drop_handler)
2912 window->drop_handler(window, input,
2913 input->sx, input->sy, window->user_data);
2914}
2915
2916static void
2917data_device_selection(void *data,
2918 struct wl_data_device *wl_data_device,
2919 struct wl_data_offer *offer)
2920{
2921 struct input *input = data;
2922 char **p;
2923
2924 if (input->selection_offer)
2925 data_offer_destroy(input->selection_offer);
2926
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002927 if (offer) {
2928 input->selection_offer = wl_data_offer_get_user_data(offer);
2929 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2930 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002931 } else {
2932 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002933 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002934}
2935
2936static const struct wl_data_device_listener data_device_listener = {
2937 data_device_data_offer,
2938 data_device_enter,
2939 data_device_leave,
2940 data_device_motion,
2941 data_device_drop,
2942 data_device_selection
2943};
2944
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002945static void
2946input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002947{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002948 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002949 struct wl_cursor *cursor;
2950 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002951
Daniel Stone80972742012-11-07 17:51:39 +11002952 if (!input->pointer)
2953 return;
2954
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002955 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002956 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002957 return;
2958
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002959 if (index >= (int) cursor->image_count) {
2960 fprintf(stderr, "cursor index out of range\n");
2961 return;
2962 }
2963
2964 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002965 buffer = wl_cursor_image_get_buffer(image);
2966 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002967 return;
2968
Kristian Høgsbergae277372012-08-01 09:41:08 -04002969 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002970 input->pointer_surface,
2971 image->hotspot_x, image->hotspot_y);
2972 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2973 wl_surface_damage(input->pointer_surface, 0, 0,
2974 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002975 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002976}
2977
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002978static const struct wl_callback_listener pointer_surface_listener;
2979
2980static void
2981pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2982 uint32_t time)
2983{
2984 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002985 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002986 int i;
2987
2988 if (callback) {
2989 assert(callback == input->cursor_frame_cb);
2990 wl_callback_destroy(callback);
2991 input->cursor_frame_cb = NULL;
2992 }
2993
Daniel Stone80972742012-11-07 17:51:39 +11002994 if (!input->pointer)
2995 return;
2996
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002997 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002998 wl_pointer_set_cursor(input->pointer,
2999 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003000 NULL, 0, 0);
3001 return;
3002 }
3003
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003004 if (input->current_cursor == CURSOR_UNSET)
3005 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003006 cursor = input->display->cursors[input->current_cursor];
3007 if (!cursor)
3008 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003009
3010 /* FIXME We don't have the current time on the first call so we set
3011 * the animation start to the time of the first frame callback. */
3012 if (time == 0)
3013 input->cursor_anim_start = 0;
3014 else if (input->cursor_anim_start == 0)
3015 input->cursor_anim_start = time;
3016
3017 if (time == 0 || input->cursor_anim_start == 0)
3018 i = 0;
3019 else
3020 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3021
Pekka Paalanenbc106382012-10-10 12:49:31 +03003022 if (cursor->image_count > 1) {
3023 input->cursor_frame_cb =
3024 wl_surface_frame(input->pointer_surface);
3025 wl_callback_add_listener(input->cursor_frame_cb,
3026 &pointer_surface_listener, input);
3027 }
3028
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003029 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003030}
3031
3032static const struct wl_callback_listener pointer_surface_listener = {
3033 pointer_surface_frame_callback
3034};
3035
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003036void
3037input_set_pointer_image(struct input *input, int pointer)
3038{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003039 int force = 0;
3040
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003041 if (!input->pointer)
3042 return;
3043
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003044 if (input->pointer_enter_serial > input->cursor_serial)
3045 force = 1;
3046
3047 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003048 return;
3049
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003050 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003051 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003052 if (!input->cursor_frame_cb)
3053 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003054 else if (force) {
3055 /* The current frame callback may be stuck if, for instance,
3056 * the set cursor request was processed by the server after
3057 * this client lost the focus. In this case the cursor surface
3058 * might not be mapped and the frame callback wouldn't ever
3059 * complete. Send a set_cursor and attach to try to map the
3060 * cursor surface again so that the callback will finish */
3061 input_set_pointer_image_index(input, 0);
3062 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003063}
3064
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003065struct wl_data_device *
3066input_get_data_device(struct input *input)
3067{
3068 return input->data_device;
3069}
3070
3071void
3072input_set_selection(struct input *input,
3073 struct wl_data_source *source, uint32_t time)
3074{
3075 wl_data_device_set_selection(input->data_device, source, time);
3076}
3077
3078void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003079input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003080{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003081 wl_data_offer_accept(input->drag_offer->offer,
3082 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003083}
3084
3085static void
3086offer_io_func(struct task *task, uint32_t events)
3087{
3088 struct data_offer *offer =
3089 container_of(task, struct data_offer, io_task);
3090 unsigned int len;
3091 char buffer[4096];
3092
3093 len = read(offer->fd, buffer, sizeof buffer);
3094 offer->func(buffer, len,
3095 offer->x, offer->y, offer->user_data);
3096
3097 if (len == 0) {
3098 close(offer->fd);
3099 data_offer_destroy(offer);
3100 }
3101}
3102
3103static void
3104data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3105 data_func_t func, void *user_data)
3106{
3107 int p[2];
3108
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003109 if (pipe2(p, O_CLOEXEC) == -1)
3110 return;
3111
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003112 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3113 close(p[1]);
3114
3115 offer->io_task.run = offer_io_func;
3116 offer->fd = p[0];
3117 offer->func = func;
3118 offer->refcount++;
3119 offer->user_data = user_data;
3120
3121 display_watch_fd(offer->input->display,
3122 offer->fd, EPOLLIN, &offer->io_task);
3123}
3124
3125void
3126input_receive_drag_data(struct input *input, const char *mime_type,
3127 data_func_t func, void *data)
3128{
3129 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3130 input->drag_offer->x = input->sx;
3131 input->drag_offer->y = input->sy;
3132}
3133
3134int
3135input_receive_selection_data(struct input *input, const char *mime_type,
3136 data_func_t func, void *data)
3137{
3138 char **p;
3139
3140 if (input->selection_offer == NULL)
3141 return -1;
3142
3143 for (p = input->selection_offer->types.data; *p; p++)
3144 if (strcmp(mime_type, *p) == 0)
3145 break;
3146
3147 if (*p == NULL)
3148 return -1;
3149
3150 data_offer_receive_data(input->selection_offer,
3151 mime_type, func, data);
3152 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003153}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003154
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003155int
3156input_receive_selection_data_to_fd(struct input *input,
3157 const char *mime_type, int fd)
3158{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003159 if (input->selection_offer)
3160 wl_data_offer_receive(input->selection_offer->offer,
3161 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003162
3163 return 0;
3164}
3165
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003166void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003167window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003168{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003169 if (!window->shell_surface)
3170 return;
3171
Daniel Stone37816df2012-05-16 18:45:18 +01003172 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003173}
3174
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003175static void
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003176surface_resize(struct surface *surface, struct widget *widget)
3177{
3178 if (surface->allocation.width != widget->allocation.width ||
3179 surface->allocation.height != widget->allocation.height) {
3180 surface->allocation = widget->allocation;
3181 window_schedule_redraw(surface->window);
3182 }
3183}
3184
3185static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003186idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003187{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003188 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003189 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003190
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003191 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003192 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003193 widget_set_allocation(widget,
3194 window->pending_allocation.x,
3195 window->pending_allocation.y,
3196 window->pending_allocation.width,
3197 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003198
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003199 if (window->input_region) {
3200 wl_region_destroy(window->input_region);
3201 window->input_region = NULL;
3202 }
3203
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003204 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003205 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003206
3207 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003208
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003209 if (widget->resize_handler)
3210 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003211 widget->allocation.width,
3212 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003213 widget->user_data);
3214
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003215 surface_resize(window->main_surface, widget);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003216
3217 if (widget->opaque)
3218 wl_region_add(window->opaque_region, 0, 0,
3219 widget->allocation.width,
3220 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003221}
3222
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003223void
3224window_schedule_resize(struct window *window, int width, int height)
3225{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003226 window->pending_allocation.x = 0;
3227 window->pending_allocation.y = 0;
3228 window->pending_allocation.width = width;
3229 window->pending_allocation.height = height;
3230
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003231 if (window->min_allocation.width == 0)
3232 window->min_allocation = window->pending_allocation;
3233 if (window->pending_allocation.width < window->min_allocation.width)
3234 window->pending_allocation.width = window->min_allocation.width;
3235 if (window->pending_allocation.height < window->min_allocation.height)
3236 window->pending_allocation.height = window->min_allocation.height;
3237
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003238 window->resize_needed = 1;
3239 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003240}
3241
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003242void
3243widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3244{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003245 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003246}
3247
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003248static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003249handle_ping(void *data, struct wl_shell_surface *shell_surface,
3250 uint32_t serial)
3251{
3252 wl_shell_surface_pong(shell_surface, serial);
3253}
3254
3255static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003256handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003257 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003258{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003259 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003260
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003261 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003262 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003263}
3264
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003265static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003266menu_destroy(struct menu *menu)
3267{
3268 widget_destroy(menu->widget);
3269 window_destroy(menu->window);
3270 free(menu);
3271}
3272
3273static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003274handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3275{
3276 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003277 struct menu *menu = window->widget->user_data;
3278
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003279 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003280 * device. Or just use wl_callback. And this really needs to
3281 * be a window vfunc that the menu can set. And we need the
3282 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003283
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003284 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003285 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003286 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003287}
3288
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003289static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003290 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003291 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003292 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003293};
3294
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003295void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003296window_get_allocation(struct window *window,
3297 struct rectangle *allocation)
3298{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003299 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003300}
3301
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003302static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003303widget_redraw(struct widget *widget)
3304{
3305 struct widget *child;
3306
3307 if (widget->redraw_handler)
3308 widget->redraw_handler(widget, widget->user_data);
3309 wl_list_for_each(child, &widget->child_list, link)
3310 widget_redraw(child);
3311}
3312
3313static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003314frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3315{
3316 struct window *window = data;
3317
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003318 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003319 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003320 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003321 window->redraw_scheduled = 0;
3322 if (window->redraw_needed)
3323 window_schedule_redraw(window);
3324}
3325
3326static const struct wl_callback_listener listener = {
3327 frame_callback
3328};
3329
3330static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003331idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003332{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003333 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003334
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003335 if (window->resize_needed)
3336 idle_resize(window);
3337
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003338 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003339 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003340 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003341 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003342
Pekka Paalanen4e373742013-02-13 16:17:13 +02003343 window->frame_cb = wl_surface_frame(window->main_surface->surface);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003344 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003345 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003346}
3347
3348void
3349window_schedule_redraw(struct window *window)
3350{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003351 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003352 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003353 window->redraw_task.run = idle_redraw;
3354 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003355 window->redraw_scheduled = 1;
3356 }
3357}
3358
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003359int
3360window_is_fullscreen(struct window *window)
3361{
3362 return window->type == TYPE_FULLSCREEN;
3363}
3364
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003365void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003366window_set_fullscreen(struct window *window, int fullscreen)
3367{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003368 if (!window->display->shell)
3369 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003370
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003371 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003372 return;
3373
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003374 if (fullscreen) {
3375 window->type = TYPE_FULLSCREEN;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003376 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003377 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003378 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003379 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003380 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003381 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003382 wl_shell_surface_set_toplevel(window->shell_surface);
3383 window_schedule_resize(window,
3384 window->saved_allocation.width,
3385 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003386 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003387}
3388
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003389void
3390window_set_fullscreen_method(struct window *window,
3391 enum wl_shell_surface_fullscreen_method method)
3392{
3393 window->fullscreen_method = method;
3394}
3395
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003396int
3397window_is_maximized(struct window *window)
3398{
3399 return window->type == TYPE_MAXIMIZED;
3400}
3401
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003402void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003403window_set_maximized(struct window *window, int maximized)
3404{
3405 if (!window->display->shell)
3406 return;
3407
3408 if ((window->type == TYPE_MAXIMIZED) == maximized)
3409 return;
3410
3411 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003412 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003413 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3414 window->type = TYPE_MAXIMIZED;
3415 } else {
3416 wl_shell_surface_set_toplevel(window->shell_surface);
3417 window->type = TYPE_TOPLEVEL;
3418 window_schedule_resize(window,
3419 window->saved_allocation.width,
3420 window->saved_allocation.height);
3421 }
3422}
3423
3424void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003425window_set_user_data(struct window *window, void *data)
3426{
3427 window->user_data = data;
3428}
3429
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003430void *
3431window_get_user_data(struct window *window)
3432{
3433 return window->user_data;
3434}
3435
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003436void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003437window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003438 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003439{
3440 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003441}
3442
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003443void
3444window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003445 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003446{
3447 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003448}
3449
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003450void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003451window_set_data_handler(struct window *window, window_data_handler_t handler)
3452{
3453 window->data_handler = handler;
3454}
3455
3456void
3457window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3458{
3459 window->drop_handler = handler;
3460}
3461
3462void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003463window_set_close_handler(struct window *window,
3464 window_close_handler_t handler)
3465{
3466 window->close_handler = handler;
3467}
3468
3469void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003470window_set_fullscreen_handler(struct window *window,
3471 window_fullscreen_handler_t handler)
3472{
3473 window->fullscreen_handler = handler;
3474}
3475
3476void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003477window_set_output_handler(struct window *window,
3478 window_output_handler_t handler)
3479{
3480 window->output_handler = handler;
3481}
3482
3483void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003484window_set_title(struct window *window, const char *title)
3485{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003486 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003487 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003488 if (window->shell_surface)
3489 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003490}
3491
3492const char *
3493window_get_title(struct window *window)
3494{
3495 return window->title;
3496}
3497
3498void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003499window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3500{
3501 struct text_cursor_position *text_cursor_position =
3502 window->display->text_cursor_position;
3503
Scott Moreau9295ce02012-06-01 12:46:10 -06003504 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003505 return;
3506
3507 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003508 window->main_surface->surface,
3509 wl_fixed_from_int(x),
3510 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003511}
3512
3513void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003514window_damage(struct window *window, int32_t x, int32_t y,
3515 int32_t width, int32_t height)
3516{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003517 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003518}
3519
Casey Dahlin9074db52012-04-19 22:50:09 -04003520static void
3521surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003522 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003523{
Rob Bradford7507b572012-05-15 17:55:34 +01003524 struct window *window = data;
3525 struct output *output;
3526 struct output *output_found = NULL;
3527 struct window_output *window_output;
3528
3529 wl_list_for_each(output, &window->display->output_list, link) {
3530 if (output->output == wl_output) {
3531 output_found = output;
3532 break;
3533 }
3534 }
3535
3536 if (!output_found)
3537 return;
3538
3539 window_output = malloc (sizeof *window_output);
3540 window_output->output = output_found;
3541
3542 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003543
3544 if (window->output_handler)
3545 window->output_handler(window, output_found, 1,
3546 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003547}
3548
3549static void
3550surface_leave(void *data,
3551 struct wl_surface *wl_surface, struct wl_output *output)
3552{
Rob Bradford7507b572012-05-15 17:55:34 +01003553 struct window *window = data;
3554 struct window_output *window_output;
3555 struct window_output *window_output_found = NULL;
3556
3557 wl_list_for_each(window_output, &window->window_output_list, link) {
3558 if (window_output->output->output == output) {
3559 window_output_found = window_output;
3560 break;
3561 }
3562 }
3563
3564 if (window_output_found) {
3565 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003566
3567 if (window->output_handler)
3568 window->output_handler(window, window_output->output,
3569 0, window->user_data);
3570
Rob Bradford7507b572012-05-15 17:55:34 +01003571 free(window_output_found);
3572 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003573}
3574
3575static const struct wl_surface_listener surface_listener = {
3576 surface_enter,
3577 surface_leave
3578};
3579
Pekka Paalanen4e373742013-02-13 16:17:13 +02003580static struct surface *
3581surface_create(struct window *window)
3582{
3583 struct display *display = window->display;
3584 struct surface *surface;
3585
3586 surface = calloc(1, sizeof *surface);
3587 if (!surface)
3588 return NULL;
3589
3590 surface->window = window;
3591 surface->surface = wl_compositor_create_surface(display->compositor);
3592 wl_surface_add_listener(surface->surface, &surface_listener, window);
3593
3594 return surface;
3595}
3596
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003597static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003598window_create_internal(struct display *display,
3599 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003600{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003601 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003602 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003603
3604 window = malloc(sizeof *window);
3605 if (window == NULL)
3606 return NULL;
3607
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003608 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003609 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003610 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003611
3612 surface = surface_create(window);
3613 window->main_surface = surface;
3614
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003615 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003616 window->shell_surface =
3617 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003618 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003619 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003620
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003621 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003622 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003623 window->input_region = NULL;
3624 window->opaque_region = NULL;
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}