blob: b72c2cac027fa06ea056c1528f88a5d3bd590b9e [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;
Pekka Paalanen35e82632013-04-25 13:57:48 +030088 struct wl_subcompositor *subcompositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040089 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040090 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040091 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060092 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020093 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200121
122 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100123
124 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200125
126 uint32_t workspace;
127 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200128
129 /* A hack to get text extents for tooltips */
130 cairo_surface_t *dummy_surface;
131 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500132};
133
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400134enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400135 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400136 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400137 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500138 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400139 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500140 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400141 TYPE_CUSTOM
142};
Rob Bradford7507b572012-05-15 17:55:34 +0100143
144struct window_output {
145 struct output *output;
146 struct wl_list link;
147};
148
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200149struct toysurface {
150 /*
151 * Prepare the surface for drawing. Makes sure there is a surface
152 * of the right size available for rendering, and returns it.
153 * dx,dy are the x,y of wl_surface.attach.
154 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200155 * If flags has SURFACE_HINT_RESIZE set, the user is
156 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157 * Returns the Cairo surface to draw to.
158 */
159 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200160 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161
162 /*
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
166 */
167 void (*swap)(struct toysurface *base,
168 struct rectangle *server_allocation);
169
170 /*
171 * Make the toysurface current with the given EGL context.
172 * Returns 0 on success, and negative of failure.
173 */
174 int (*acquire)(struct toysurface *base, EGLContext ctx);
175
176 /*
177 * Release the toysurface from the EGL context, returning control
178 * to Cairo.
179 */
180 void (*release)(struct toysurface *base);
181
182 /*
183 * Destroy the toysurface, including the Cairo surface, any
184 * backing storage, and the Wayland protocol objects.
185 */
186 void (*destroy)(struct toysurface *base);
187};
188
Pekka Paalanen4e373742013-02-13 16:17:13 +0200189struct surface {
190 struct window *window;
191
192 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300193 struct wl_subsurface *subsurface;
194 int synchronized;
195 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200196 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200197 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300198 int redraw_needed;
199 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300200 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200201
202 struct rectangle allocation;
203 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200204
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200205 struct wl_region *input_region;
206 struct wl_region *opaque_region;
207
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200208 enum window_buffer_type buffer_type;
209 enum wl_output_transform buffer_transform;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200210
211 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300212
213 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200214};
215
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500216struct window {
217 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500218 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100219 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500220 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200221 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400222 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500223 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500224 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400225 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400226 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300227 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400228 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400229 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100230 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400231 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400232 int focus_count;
233
Pekka Paalanen99436862012-11-19 17:15:59 +0200234 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200235 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000236 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400237
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500238 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500239 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400240 window_data_handler_t data_handler;
241 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500242 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400243 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200244 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400245
Pekka Paalanen4e373742013-02-13 16:17:13 +0200246 struct surface *main_surface;
247 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300248
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200249 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500250
Pekka Paalanen35e82632013-04-25 13:57:48 +0300251 /* struct surface::link, contains also main_surface */
252 struct wl_list subsurface_list;
253
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500254 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400255 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500256};
257
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500258struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500259 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200260 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300261 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500262 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400263 struct wl_list link;
264 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500265 widget_resize_handler_t resize_handler;
266 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500267 widget_enter_handler_t enter_handler;
268 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500269 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500270 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200271 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400272 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500273 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300274 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500275 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400276};
277
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400278struct input {
279 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100280 struct wl_seat *seat;
281 struct wl_pointer *pointer;
282 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400283 struct window *pointer_focus;
284 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300285 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300286 uint32_t cursor_anim_start;
287 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300288 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400289 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400290 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400291 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400292 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400293 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400294
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500295 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500296 struct widget *grab;
297 uint32_t grab_button;
298
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400299 struct wl_data_device *data_device;
300 struct data_offer *drag_offer;
301 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100302
303 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100304 struct xkb_keymap *keymap;
305 struct xkb_state *state;
306 xkb_mod_mask_t control_mask;
307 xkb_mod_mask_t alt_mask;
308 xkb_mod_mask_t shift_mask;
309 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400310
311 struct task repeat_task;
312 int repeat_timer_fd;
313 uint32_t repeat_sym;
314 uint32_t repeat_key;
315 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400316};
317
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500318struct output {
319 struct display *display;
320 struct wl_output *output;
321 struct rectangle allocation;
322 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600323 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200324
325 display_output_handler_t destroy_handler;
326 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500327};
328
Martin Minarik1998b152012-05-10 02:04:35 +0200329enum frame_button_action {
330 FRAME_BUTTON_NULL = 0,
331 FRAME_BUTTON_ICON = 1,
332 FRAME_BUTTON_CLOSE = 2,
333 FRAME_BUTTON_MINIMIZE = 3,
334 FRAME_BUTTON_MAXIMIZE = 4,
335};
336
337enum frame_button_pointer {
338 FRAME_BUTTON_DEFAULT = 0,
339 FRAME_BUTTON_OVER = 1,
340 FRAME_BUTTON_ACTIVE = 2,
341};
342
343enum frame_button_align {
344 FRAME_BUTTON_RIGHT = 0,
345 FRAME_BUTTON_LEFT = 1,
346};
347
348enum frame_button_decoration {
349 FRAME_BUTTON_NONE = 0,
350 FRAME_BUTTON_FANCY = 1,
351};
352
353struct frame_button {
354 struct widget *widget;
355 struct frame *frame;
356 cairo_surface_t *icon;
357 enum frame_button_action type;
358 enum frame_button_pointer state;
359 struct wl_list link; /* buttons_list */
360 enum frame_button_align align;
361 enum frame_button_decoration decoration;
362};
363
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500364struct frame {
365 struct widget *widget;
366 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200367 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500368};
369
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500370struct menu {
371 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500372 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500373 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500374 const char **entries;
375 uint32_t time;
376 int current;
377 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400378 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500379 menu_func_t func;
380};
381
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300382struct tooltip {
383 struct widget *parent;
384 struct window *window;
385 struct widget *widget;
386 char *entry;
387 struct task tooltip_task;
388 int tooltip_fd;
389 float x, y;
390};
391
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700392struct shm_pool {
393 struct wl_shm_pool *pool;
394 size_t size;
395 size_t used;
396 void *data;
397};
398
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400399enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300400 CURSOR_DEFAULT = 100,
401 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400402};
403
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500404enum window_location {
405 WINDOW_INTERIOR = 0,
406 WINDOW_RESIZING_TOP = 1,
407 WINDOW_RESIZING_BOTTOM = 2,
408 WINDOW_RESIZING_LEFT = 4,
409 WINDOW_RESIZING_TOP_LEFT = 5,
410 WINDOW_RESIZING_BOTTOM_LEFT = 6,
411 WINDOW_RESIZING_RIGHT = 8,
412 WINDOW_RESIZING_TOP_RIGHT = 9,
413 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
414 WINDOW_RESIZING_MASK = 15,
415 WINDOW_EXTERIOR = 16,
416 WINDOW_TITLEBAR = 17,
417 WINDOW_CLIENT_AREA = 18,
418};
419
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200420static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500422#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200424struct egl_window_surface {
425 struct toysurface base;
426 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100427 struct display *display;
428 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200429 struct wl_egl_window *egl_window;
430 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100431};
432
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200433static struct egl_window_surface *
434to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100435{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200436 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100437}
438
439static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200440egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200441 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100442{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200443 struct egl_window_surface *surface = to_egl_window_surface(base);
444
445 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
446 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
447
448 return cairo_surface_reference(surface->cairo_surface);
449}
450
451static void
452egl_window_surface_swap(struct toysurface *base,
453 struct rectangle *server_allocation)
454{
455 struct egl_window_surface *surface = to_egl_window_surface(base);
456
457 cairo_gl_surface_swapbuffers(surface->cairo_surface);
458 wl_egl_window_get_attached_size(surface->egl_window,
459 &server_allocation->width,
460 &server_allocation->height);
461}
462
463static int
464egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
465{
466 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200467 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100468
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200469 device = cairo_surface_get_device(surface->cairo_surface);
470 if (!device)
471 return -1;
472
473 if (!ctx) {
474 if (device == surface->display->argb_device)
475 ctx = surface->display->argb_ctx;
476 else
477 assert(0);
478 }
479
480 cairo_device_flush(device);
481 cairo_device_acquire(device);
482 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
483 surface->egl_surface, ctx))
484 fprintf(stderr, "failed to make surface current\n");
485
486 return 0;
487}
488
489static void
490egl_window_surface_release(struct toysurface *base)
491{
492 struct egl_window_surface *surface = to_egl_window_surface(base);
493 cairo_device_t *device;
494
495 device = cairo_surface_get_device(surface->cairo_surface);
496 if (!device)
497 return;
498
499 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
500 surface->display->argb_ctx))
501 fprintf(stderr, "failed to make context current\n");
502
503 cairo_device_release(device);
504}
505
506static void
507egl_window_surface_destroy(struct toysurface *base)
508{
509 struct egl_window_surface *surface = to_egl_window_surface(base);
510 struct display *d = surface->display;
511
512 cairo_surface_destroy(surface->cairo_surface);
513 eglDestroySurface(d->dpy, surface->egl_surface);
514 wl_egl_window_destroy(surface->egl_window);
515 surface->surface = NULL;
516
517 free(surface);
518}
519
520static struct toysurface *
521egl_window_surface_create(struct display *display,
522 struct wl_surface *wl_surface,
523 uint32_t flags,
524 struct rectangle *rectangle)
525{
526 struct egl_window_surface *surface;
527
Pekka Paalanenb3627362012-11-19 17:16:00 +0200528 if (display->dpy == EGL_NO_DISPLAY)
529 return NULL;
530
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200531 surface = calloc(1, sizeof *surface);
532 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100533 return NULL;
534
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200535 surface->base.prepare = egl_window_surface_prepare;
536 surface->base.swap = egl_window_surface_swap;
537 surface->base.acquire = egl_window_surface_acquire;
538 surface->base.release = egl_window_surface_release;
539 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100540
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200541 surface->display = display;
542 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400543
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200544 surface->egl_window = wl_egl_window_create(surface->surface,
545 rectangle->width,
546 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100547
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200548 surface->egl_surface = eglCreateWindowSurface(display->dpy,
549 display->argb_config,
550 surface->egl_window,
551 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100552
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200553 surface->cairo_surface =
554 cairo_gl_surface_create_for_egl(display->argb_device,
555 surface->egl_surface,
556 rectangle->width,
557 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100560}
561
Pekka Paalanenb3627362012-11-19 17:16:00 +0200562#else
563
564static struct toysurface *
565egl_window_surface_create(struct display *display,
566 struct wl_surface *wl_surface,
567 uint32_t flags,
568 struct rectangle *rectangle)
569{
570 return NULL;
571}
572
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573#endif
574
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200575struct shm_surface_data {
576 struct wl_buffer *buffer;
577 struct shm_pool *pool;
578};
579
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400580struct wl_buffer *
581display_get_buffer_for_surface(struct display *display,
582 cairo_surface_t *surface)
583{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200584 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200586 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400587
588 return data->buffer;
589}
590
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500591static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700592shm_pool_destroy(struct shm_pool *pool);
593
594static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400595shm_surface_data_destroy(void *p)
596{
597 struct shm_surface_data *data = p;
598
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200599 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700600 if (data->pool)
601 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300602
603 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604}
605
Kristian Høgsberg16626282012-04-03 11:21:27 -0400606static struct wl_shm_pool *
607make_shm_pool(struct display *display, int size, void **data)
608{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400609 struct wl_shm_pool *pool;
610 int fd;
611
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300612 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400613 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300614 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
615 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400616 return NULL;
617 }
618
619 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400620 if (*data == MAP_FAILED) {
621 fprintf(stderr, "mmap failed: %m\n");
622 close(fd);
623 return NULL;
624 }
625
626 pool = wl_shm_create_pool(display->shm, fd, size);
627
628 close(fd);
629
630 return pool;
631}
632
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700633static struct shm_pool *
634shm_pool_create(struct display *display, size_t size)
635{
636 struct shm_pool *pool = malloc(sizeof *pool);
637
638 if (!pool)
639 return NULL;
640
641 pool->pool = make_shm_pool(display, size, &pool->data);
642 if (!pool->pool) {
643 free(pool);
644 return NULL;
645 }
646
647 pool->size = size;
648 pool->used = 0;
649
650 return pool;
651}
652
653static void *
654shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
655{
656 if (pool->used + size > pool->size)
657 return NULL;
658
659 *offset = pool->used;
660 pool->used += size;
661
662 return (char *) pool->data + *offset;
663}
664
665/* destroy the pool. this does not unmap the memory though */
666static void
667shm_pool_destroy(struct shm_pool *pool)
668{
669 munmap(pool->data, pool->size);
670 wl_shm_pool_destroy(pool->pool);
671 free(pool);
672}
673
674/* Start allocating from the beginning of the pool again */
675static void
676shm_pool_reset(struct shm_pool *pool)
677{
678 pool->used = 0;
679}
680
681static int
682data_length_for_shm_surface(struct rectangle *rect)
683{
684 int stride;
685
686 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
687 rect->width);
688 return stride * rect->height;
689}
690
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500691static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700692display_create_shm_surface_from_pool(struct display *display,
693 struct rectangle *rectangle,
694 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400695{
696 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400697 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400698 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700699 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400700 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400701
702 data = malloc(sizeof *data);
703 if (data == NULL)
704 return NULL;
705
706 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
707 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700708 length = stride * rectangle->height;
709 data->pool = NULL;
710 map = shm_pool_allocate(pool, length, &offset);
711
712 if (!map) {
713 free(data);
714 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400715 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400716
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400717 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400718 CAIRO_FORMAT_ARGB32,
719 rectangle->width,
720 rectangle->height,
721 stride);
722
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200723 cairo_surface_set_user_data(surface, &shm_surface_data_key,
724 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400725
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400726 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500727 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400728 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500729 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400730
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200731 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
732 rectangle->width,
733 rectangle->height,
734 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400735
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700736 return surface;
737}
738
739static cairo_surface_t *
740display_create_shm_surface(struct display *display,
741 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200742 struct shm_pool *alternate_pool,
743 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700744{
745 struct shm_surface_data *data;
746 struct shm_pool *pool;
747 cairo_surface_t *surface;
748
Pekka Paalanen99436862012-11-19 17:15:59 +0200749 if (alternate_pool) {
750 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700751 surface = display_create_shm_surface_from_pool(display,
752 rectangle,
753 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200754 alternate_pool);
755 if (surface) {
756 data = cairo_surface_get_user_data(surface,
757 &shm_surface_data_key);
758 goto out;
759 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700760 }
761
762 pool = shm_pool_create(display,
763 data_length_for_shm_surface(rectangle));
764 if (!pool)
765 return NULL;
766
767 surface =
768 display_create_shm_surface_from_pool(display, rectangle,
769 flags, pool);
770
771 if (!surface) {
772 shm_pool_destroy(pool);
773 return NULL;
774 }
775
776 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200777 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700778 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400779
Pekka Paalanen99436862012-11-19 17:15:59 +0200780out:
781 if (data_ret)
782 *data_ret = data;
783
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400784 return surface;
785}
786
nobled7b87cb02011-02-01 18:51:47 +0000787static int
788check_size(struct rectangle *rect)
789{
790 if (rect->width && rect->height)
791 return 0;
792
793 fprintf(stderr, "tried to create surface of "
794 "width: %d, height: %d\n", rect->width, rect->height);
795 return -1;
796}
797
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400798cairo_surface_t *
799display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100800 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400801 struct rectangle *rectangle,
802 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400803{
nobled7b87cb02011-02-01 18:51:47 +0000804 if (check_size(rectangle) < 0)
805 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200806
807 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200808 return display_create_shm_surface(display, rectangle, flags,
809 NULL, NULL);
810}
811
Pekka Paalanena4eda732012-11-19 17:16:02 +0200812struct shm_surface_leaf {
813 cairo_surface_t *cairo_surface;
814 /* 'data' is automatically destroyed, when 'cairo_surface' is */
815 struct shm_surface_data *data;
816
817 struct shm_pool *resize_pool;
818 int busy;
819};
820
821static void
822shm_surface_leaf_release(struct shm_surface_leaf *leaf)
823{
824 if (leaf->cairo_surface)
825 cairo_surface_destroy(leaf->cairo_surface);
826 /* leaf->data already destroyed via cairo private */
827
828 if (leaf->resize_pool)
829 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200830
831 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200832}
833
Pekka Paalanenaef02542013-04-25 13:57:47 +0300834#define MAX_LEAVES 3
835
Pekka Paalanen99436862012-11-19 17:15:59 +0200836struct shm_surface {
837 struct toysurface base;
838 struct display *display;
839 struct wl_surface *surface;
840 uint32_t flags;
841 int dx, dy;
842
Pekka Paalanenaef02542013-04-25 13:57:47 +0300843 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200844 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200845};
846
847static struct shm_surface *
848to_shm_surface(struct toysurface *base)
849{
850 return container_of(base, struct shm_surface, base);
851}
852
Pekka Paalanena4eda732012-11-19 17:16:02 +0200853static void
854shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
855{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200856 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300857 struct shm_surface_leaf *leaf;
858 int i;
859 int free_found;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200860
Pekka Paalanenaef02542013-04-25 13:57:47 +0300861 for (i = 0; i < MAX_LEAVES; i++) {
862 leaf = &surface->leaf[i];
863 if (leaf->data && leaf->data->buffer == buffer) {
864 leaf->busy = 0;
865 break;
866 }
867 }
868 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200869
Pekka Paalanenaef02542013-04-25 13:57:47 +0300870 /* Leave one free leaf with storage, release others */
871 free_found = 0;
872 for (i = 0; i < MAX_LEAVES; i++) {
873 leaf = &surface->leaf[i];
874
875 if (!leaf->cairo_surface || leaf->busy)
876 continue;
877
878 if (!free_found)
879 free_found = 1;
880 else
881 shm_surface_leaf_release(leaf);
882 }
Pekka Paalanena4eda732012-11-19 17:16:02 +0200883}
884
885static const struct wl_buffer_listener shm_surface_buffer_listener = {
886 shm_surface_buffer_release
887};
888
Pekka Paalanen99436862012-11-19 17:15:59 +0200889static cairo_surface_t *
890shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200891 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200892{
Pekka Paalanenec076692012-11-30 13:37:27 +0200893 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200894 struct shm_surface *surface = to_shm_surface(base);
895 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanenaef02542013-04-25 13:57:47 +0300896 struct shm_surface_leaf *leaf = NULL;
897 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200898
899 surface->dx = dx;
900 surface->dy = dy;
901
Pekka Paalanenaef02542013-04-25 13:57:47 +0300902 /* pick a free buffer, preferrably one that already has storage */
903 for (i = 0; i < MAX_LEAVES; i++) {
904 if (surface->leaf[i].busy)
905 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200906
Pekka Paalanenaef02542013-04-25 13:57:47 +0300907 if (!leaf || surface->leaf[i].cairo_surface)
908 leaf = &surface->leaf[i];
909 }
910 if (!leaf) {
911 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +0200912 __func__);
913 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200914 }
915
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916 if (!resize_hint && leaf->resize_pool) {
917 cairo_surface_destroy(leaf->cairo_surface);
918 leaf->cairo_surface = NULL;
919 shm_pool_destroy(leaf->resize_pool);
920 leaf->resize_pool = NULL;
921 }
922
923 if (leaf->cairo_surface &&
924 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
925 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200926 goto out;
927
Pekka Paalanena4eda732012-11-19 17:16:02 +0200928 if (leaf->cairo_surface)
929 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200930
Pekka Paalanena4eda732012-11-19 17:16:02 +0200931 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200932 /* Create a big pool to allocate from, while continuously
933 * resizing. Mmapping a new pool in the server
934 * is relatively expensive, so reusing a pool performs
935 * better, but may temporarily reserve unneeded memory.
936 */
937 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938 leaf->resize_pool = shm_pool_create(surface->display,
939 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200940 }
941
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200943 display_create_shm_surface(surface->display, &rect,
944 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200945 leaf->resize_pool,
946 &leaf->data);
947 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200948 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200949
950out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200951 surface->current = leaf;
952
953 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200954}
955
956static void
957shm_surface_swap(struct toysurface *base,
958 struct rectangle *server_allocation)
959{
960 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200961 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200962
963 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200964 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200965 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200966 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200967
Pekka Paalanena4eda732012-11-19 17:16:02 +0200968 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200969 surface->dx, surface->dy);
970 wl_surface_damage(surface->surface, 0, 0,
971 server_allocation->width, server_allocation->height);
972 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200973
974 leaf->busy = 1;
975 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200976}
977
978static int
979shm_surface_acquire(struct toysurface *base, EGLContext ctx)
980{
981 return -1;
982}
983
984static void
985shm_surface_release(struct toysurface *base)
986{
987}
988
989static void
990shm_surface_destroy(struct toysurface *base)
991{
992 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +0300993 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200994
Pekka Paalanenaef02542013-04-25 13:57:47 +0300995 for (i = 0; i < MAX_LEAVES; i++)
996 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200997
998 free(surface);
999}
1000
1001static struct toysurface *
1002shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1003 uint32_t flags, struct rectangle *rectangle)
1004{
1005 struct shm_surface *surface;
1006
1007 surface = calloc(1, sizeof *surface);
1008 if (!surface)
1009 return NULL;
1010
1011 surface->base.prepare = shm_surface_prepare;
1012 surface->base.swap = shm_surface_swap;
1013 surface->base.acquire = shm_surface_acquire;
1014 surface->base.release = shm_surface_release;
1015 surface->base.destroy = shm_surface_destroy;
1016
1017 surface->display = display;
1018 surface->surface = wl_surface;
1019 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001020
1021 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001022}
1023
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001024/*
1025 * The following correspondences between file names and cursors was copied
1026 * from: https://bugs.kde.org/attachment.cgi?id=67313
1027 */
1028
1029static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001030 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001031 "sw-resize"
1032};
1033
1034static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001035 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001036 "se-resize"
1037};
1038
1039static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001040 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001041 "s-resize"
1042};
1043
1044static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001045 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001046 "closedhand",
1047 "208530c400c041818281048008011002"
1048};
1049
1050static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001051 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001052 "default",
1053 "top_left_arrow",
1054 "left-arrow"
1055};
1056
1057static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001058 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001059 "w-resize"
1060};
1061
1062static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001063 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001064 "e-resize"
1065};
1066
1067static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001068 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001069 "nw-resize"
1070};
1071
1072static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001073 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001074 "ne-resize"
1075};
1076
1077static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001078 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001079 "n-resize"
1080};
1081
1082static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001083 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001084 "ibeam",
1085 "text"
1086};
1087
1088static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001089 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001090 "pointer",
1091 "pointing_hand",
1092 "e29285e634086352946a0e7090d73106"
1093};
1094
1095static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001096 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001097 "wait",
1098 "0426c94ea35c87780ff01dc239897213"
1099};
1100
1101struct cursor_alternatives {
1102 const char **names;
1103 size_t count;
1104};
1105
1106static const struct cursor_alternatives cursors[] = {
1107 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1108 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1109 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1110 {grabbings, ARRAY_LENGTH(grabbings)},
1111 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1112 {left_sides, ARRAY_LENGTH(left_sides)},
1113 {right_sides, ARRAY_LENGTH(right_sides)},
1114 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1115 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1116 {top_sides, ARRAY_LENGTH(top_sides)},
1117 {xterms, ARRAY_LENGTH(xterms)},
1118 {hand1s, ARRAY_LENGTH(hand1s)},
1119 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001120};
1121
1122static void
1123create_cursors(struct display *display)
1124{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001125 char *config_file;
1126 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001127 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001128 unsigned int i, j;
1129 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001130 struct config_key shell_keys[] = {
1131 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001132 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001133 };
1134 struct config_section cs[] = {
1135 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1136 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001137
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001138 config_file = config_file_path("weston.ini");
1139 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1140 free(config_file);
1141
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001142 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001143 display->cursors =
1144 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1145
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001146 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001147 cursor = NULL;
1148 for (j = 0; !cursor && j < cursors[i].count; ++j)
1149 cursor = wl_cursor_theme_get_cursor(
1150 display->cursor_theme, cursors[i].names[j]);
1151
1152 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001153 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001154 cursors[i].names[0]);
1155
1156 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001157 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001158}
1159
1160static void
1161destroy_cursors(struct display *display)
1162{
1163 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001164 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001165}
1166
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001167struct wl_cursor_image *
1168display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001170 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001172 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001173}
1174
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001175static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001176surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001177{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001178 if (!surface->cairo_surface)
1179 return;
1180
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001181 if (surface->opaque_region) {
1182 wl_surface_set_opaque_region(surface->surface,
1183 surface->opaque_region);
1184 wl_region_destroy(surface->opaque_region);
1185 surface->opaque_region = NULL;
1186 }
1187
1188 if (surface->input_region) {
1189 wl_surface_set_input_region(surface->surface,
1190 surface->input_region);
1191 wl_region_destroy(surface->input_region);
1192 surface->input_region = NULL;
1193 }
1194
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001195 surface->toysurface->swap(surface->toysurface,
1196 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001197
Pekka Paalanen89dee002013-02-13 16:17:20 +02001198 cairo_surface_destroy(surface->cairo_surface);
1199 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001200}
1201
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001202int
1203window_has_focus(struct window *window)
1204{
1205 return window->focus_count > 0;
1206}
1207
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001208static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001209window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001210{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001211 struct surface *surface;
1212
Pekka Paalanen89dee002013-02-13 16:17:20 +02001213 if (window->type == TYPE_NONE) {
1214 window->type = TYPE_TOPLEVEL;
1215 if (window->shell_surface)
1216 wl_shell_surface_set_toplevel(window->shell_surface);
1217 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001218
Pekka Paalanen35e82632013-04-25 13:57:48 +03001219 wl_list_for_each(surface, &window->subsurface_list, link) {
1220 if (surface == window->main_surface)
1221 continue;
1222
1223 surface_flush(surface);
1224 }
1225
Pekka Paalanen89dee002013-02-13 16:17:20 +02001226 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001227}
1228
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001229struct display *
1230window_get_display(struct window *window)
1231{
1232 return window->display;
1233}
1234
Pekka Paalanen89dee002013-02-13 16:17:20 +02001235static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001236surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001237{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001238 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001239 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001240
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001241 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001242 case WL_OUTPUT_TRANSFORM_90:
1243 case WL_OUTPUT_TRANSFORM_270:
1244 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1245 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001246 allocation.width = surface->allocation.height;
1247 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001248 break;
1249 default:
1250 break;
1251 }
1252
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001253 if (!surface->toysurface && display->dpy &&
1254 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001255 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001256 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001257 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001258 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001259 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001260 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001261
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001262 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001263 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001264 surface->surface,
1265 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001266
Pekka Paalanen89dee002013-02-13 16:17:20 +02001267 surface->cairo_surface = surface->toysurface->prepare(
1268 surface->toysurface, dx, dy,
1269 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001270}
1271
1272static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001273window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001274{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001275 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001276 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001277 int dx = 0;
1278 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001279
Pekka Paalanenec076692012-11-30 13:37:27 +02001280 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001281 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001282
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001283 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1284 dx = surface->server_allocation.width -
1285 surface->allocation.width;
1286
1287 if (window->resize_edges & WINDOW_RESIZING_TOP)
1288 dy = surface->server_allocation.height -
1289 surface->allocation.height;
1290
1291 window->resize_edges = 0;
1292
Pekka Paalanen89dee002013-02-13 16:17:20 +02001293 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001294}
1295
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001296int
1297window_get_buffer_transform(struct window *window)
1298{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001299 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001300}
1301
1302void
1303window_set_buffer_transform(struct window *window,
1304 enum wl_output_transform transform)
1305{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001306 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001307 wl_surface_set_buffer_transform(window->main_surface->surface,
1308 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001309}
1310
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001311static void frame_destroy(struct frame *frame);
1312
Pekka Paalanen4e373742013-02-13 16:17:13 +02001313static void
1314surface_destroy(struct surface *surface)
1315{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001316 if (surface->frame_cb)
1317 wl_callback_destroy(surface->frame_cb);
1318
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001319 if (surface->input_region)
1320 wl_region_destroy(surface->input_region);
1321
1322 if (surface->opaque_region)
1323 wl_region_destroy(surface->opaque_region);
1324
Pekka Paalanen35e82632013-04-25 13:57:48 +03001325 if (surface->subsurface)
1326 wl_subsurface_destroy(surface->subsurface);
1327
Pekka Paalanen4e373742013-02-13 16:17:13 +02001328 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001329
1330 if (surface->toysurface)
1331 surface->toysurface->destroy(surface->toysurface);
1332
Pekka Paalanen35e82632013-04-25 13:57:48 +03001333 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001334 free(surface);
1335}
1336
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001337void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001338window_destroy(struct window *window)
1339{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001340 struct display *display = window->display;
1341 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001342 struct window_output *window_output;
1343 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001344
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001345 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001346
1347 wl_list_for_each(input, &display->input_list, link) {
1348 if (input->pointer_focus == window)
1349 input->pointer_focus = NULL;
1350 if (input->keyboard_focus == window)
1351 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001352 if (input->focus_widget &&
1353 input->focus_widget->window == window)
1354 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001355 }
1356
Rob Bradford7507b572012-05-15 17:55:34 +01001357 wl_list_for_each_safe(window_output, window_output_tmp,
1358 &window->window_output_list, link) {
1359 free (window_output);
1360 }
1361
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001362 if (window->frame)
1363 frame_destroy(window->frame);
1364
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001365 if (window->shell_surface)
1366 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001367
1368 surface_destroy(window->main_surface);
1369
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001370 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001371
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001372 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001373 free(window);
1374}
1375
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001376static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001377widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001378{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001379 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001380
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001381 wl_list_for_each(child, &widget->child_list, link) {
1382 target = widget_find_widget(child, x, y);
1383 if (target)
1384 return target;
1385 }
1386
1387 if (widget->allocation.x <= x &&
1388 x < widget->allocation.x + widget->allocation.width &&
1389 widget->allocation.y <= y &&
1390 y < widget->allocation.y + widget->allocation.height) {
1391 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001392 }
1393
1394 return NULL;
1395}
1396
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001397static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001398window_find_widget(struct window *window, int32_t x, int32_t y)
1399{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001400 struct surface *surface;
1401 struct widget *widget;
1402
1403 wl_list_for_each(surface, &window->subsurface_list, link) {
1404 widget = widget_find_widget(surface->widget, x, y);
1405 if (widget)
1406 return widget;
1407 }
1408
1409 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001410}
1411
1412static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001413widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001414{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001415 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001416
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001417 widget = malloc(sizeof *widget);
1418 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001419 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001420 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001421 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001422 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001423 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001424 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001425 widget->tooltip = NULL;
1426 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001427 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001428
1429 return widget;
1430}
1431
1432struct widget *
1433window_add_widget(struct window *window, void *data)
1434{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001435 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001436
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001437 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001438 wl_list_init(&widget->link);
1439 window->main_surface->widget = widget;
1440
1441 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001442}
1443
1444struct widget *
1445widget_add_widget(struct widget *parent, void *data)
1446{
1447 struct widget *widget;
1448
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001449 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001450 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001451
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001452 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001453}
1454
1455void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001456widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001457{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001458 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001459 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001460 struct input *input;
1461
Pekka Paalanen35e82632013-04-25 13:57:48 +03001462 /* Destroy the sub-surface along with the root widget */
1463 if (surface->widget == widget && surface->subsurface)
1464 surface_destroy(widget->surface);
1465
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001466 if (widget->tooltip) {
1467 free(widget->tooltip);
1468 widget->tooltip = NULL;
1469 }
1470
Pekka Paalanene156fb62012-01-19 13:51:38 +02001471 wl_list_for_each(input, &display->input_list, link) {
1472 if (input->focus_widget == widget)
1473 input->focus_widget = NULL;
1474 }
1475
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001476 wl_list_remove(&widget->link);
1477 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001478}
1479
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001480void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001481widget_set_default_cursor(struct widget *widget, int cursor)
1482{
1483 widget->default_cursor = cursor;
1484}
1485
1486void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001487widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001488{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001489 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001490}
1491
1492void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001493widget_set_size(struct widget *widget, int32_t width, int32_t height)
1494{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001495 widget->allocation.width = width;
1496 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001497}
1498
1499void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001500widget_set_allocation(struct widget *widget,
1501 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001502{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001503 widget->allocation.x = x;
1504 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001505 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001506}
1507
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001508void
1509widget_set_transparent(struct widget *widget, int transparent)
1510{
1511 widget->opaque = !transparent;
1512}
1513
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001514void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001515widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001516{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001517 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001518}
1519
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001520static cairo_surface_t *
1521widget_get_cairo_surface(struct widget *widget)
1522{
1523 struct surface *surface = widget->surface;
1524 struct window *window = widget->window;
1525
1526 if (!surface->cairo_surface) {
1527 if (surface == window->main_surface)
1528 window_create_main_surface(window);
1529 else
1530 surface_create_surface(surface, 0, 0, 0);
1531 }
1532
1533 return surface->cairo_surface;
1534}
1535
1536cairo_t *
1537widget_cairo_create(struct widget *widget)
1538{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001539 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001540 cairo_surface_t *cairo_surface;
1541 cairo_t *cr;
1542
1543 cairo_surface = widget_get_cairo_surface(widget);
1544 cr = cairo_create(cairo_surface);
1545
Pekka Paalanen35e82632013-04-25 13:57:48 +03001546 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1547
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001548 return cr;
1549}
1550
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001551struct wl_surface *
1552widget_get_wl_surface(struct widget *widget)
1553{
1554 return widget->surface->surface;
1555}
1556
1557uint32_t
1558widget_get_last_time(struct widget *widget)
1559{
1560 return widget->surface->last_time;
1561}
1562
1563void
1564widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1565{
1566 struct wl_compositor *comp = widget->window->display->compositor;
1567 struct surface *surface = widget->surface;
1568
1569 if (!surface->input_region)
1570 surface->input_region = wl_compositor_create_region(comp);
1571
1572 if (rect) {
1573 wl_region_add(surface->input_region,
1574 rect->x, rect->y, rect->width, rect->height);
1575 }
1576}
1577
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001578void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001579widget_set_resize_handler(struct widget *widget,
1580 widget_resize_handler_t handler)
1581{
1582 widget->resize_handler = handler;
1583}
1584
1585void
1586widget_set_redraw_handler(struct widget *widget,
1587 widget_redraw_handler_t handler)
1588{
1589 widget->redraw_handler = handler;
1590}
1591
1592void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001593widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001594{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001595 widget->enter_handler = handler;
1596}
1597
1598void
1599widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1600{
1601 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001602}
1603
1604void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001605widget_set_motion_handler(struct widget *widget,
1606 widget_motion_handler_t handler)
1607{
1608 widget->motion_handler = handler;
1609}
1610
1611void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001612widget_set_button_handler(struct widget *widget,
1613 widget_button_handler_t handler)
1614{
1615 widget->button_handler = handler;
1616}
1617
1618void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001619widget_set_axis_handler(struct widget *widget,
1620 widget_axis_handler_t handler)
1621{
1622 widget->axis_handler = handler;
1623}
1624
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001625static void
1626window_schedule_redraw_task(struct window *window);
1627
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001628void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001629widget_schedule_redraw(struct widget *widget)
1630{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001631 widget->surface->redraw_needed = 1;
1632 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001633}
1634
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001635cairo_surface_t *
1636window_get_surface(struct window *window)
1637{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001638 cairo_surface_t *cairo_surface;
1639
1640 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1641
1642 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001643}
1644
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001645struct wl_surface *
1646window_get_wl_surface(struct window *window)
1647{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001648 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001649}
1650
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001651struct wl_shell_surface *
1652window_get_wl_shell_surface(struct window *window)
1653{
1654 return window->shell_surface;
1655}
1656
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001657static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001658tooltip_redraw_handler(struct widget *widget, void *data)
1659{
1660 cairo_t *cr;
1661 const int32_t r = 3;
1662 struct tooltip *tooltip = data;
1663 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001664
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001665 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001666 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1667 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1668 cairo_paint(cr);
1669
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001670 width = widget->allocation.width;
1671 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001672 rounded_rect(cr, 0, 0, width, height, r);
1673
1674 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1675 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1676 cairo_fill(cr);
1677
1678 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1679 cairo_move_to(cr, 10, 16);
1680 cairo_show_text(cr, tooltip->entry);
1681 cairo_destroy(cr);
1682}
1683
1684static cairo_text_extents_t
1685get_text_extents(struct tooltip *tooltip)
1686{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001687 cairo_t *cr;
1688 cairo_text_extents_t extents;
1689
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001690 /* Use the dummy_surface because tooltip's surface was not
1691 * created yet, and parent does not have a valid surface
1692 * outside repaint, either.
1693 */
1694 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001695 cairo_text_extents(cr, tooltip->entry, &extents);
1696 cairo_destroy(cr);
1697
1698 return extents;
1699}
1700
1701static int
1702window_create_tooltip(struct tooltip *tooltip)
1703{
1704 struct widget *parent = tooltip->parent;
1705 struct display *display = parent->window->display;
1706 struct window *window;
1707 const int offset_y = 27;
1708 const int margin = 3;
1709 cairo_text_extents_t extents;
1710
1711 if (tooltip->widget)
1712 return 0;
1713
1714 window = window_create_transient(display, parent->window, tooltip->x,
1715 tooltip->y + offset_y,
1716 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1717 if (!window)
1718 return -1;
1719
1720 tooltip->window = window;
1721 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1722
1723 extents = get_text_extents(tooltip);
1724 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1725 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1726
1727 return 0;
1728}
1729
1730void
1731widget_destroy_tooltip(struct widget *parent)
1732{
1733 struct tooltip *tooltip = parent->tooltip;
1734
1735 parent->tooltip_count = 0;
1736 if (!tooltip)
1737 return;
1738
1739 if (tooltip->widget) {
1740 widget_destroy(tooltip->widget);
1741 window_destroy(tooltip->window);
1742 tooltip->widget = NULL;
1743 tooltip->window = NULL;
1744 }
1745
1746 close(tooltip->tooltip_fd);
1747 free(tooltip->entry);
1748 free(tooltip);
1749 parent->tooltip = NULL;
1750}
1751
1752static void
1753tooltip_func(struct task *task, uint32_t events)
1754{
1755 struct tooltip *tooltip =
1756 container_of(task, struct tooltip, tooltip_task);
1757 uint64_t exp;
1758
Martin Olsson8df662a2012-07-08 03:03:47 +02001759 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1760 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001761 window_create_tooltip(tooltip);
1762}
1763
1764#define TOOLTIP_TIMEOUT 500
1765static int
1766tooltip_timer_reset(struct tooltip *tooltip)
1767{
1768 struct itimerspec its;
1769
1770 its.it_interval.tv_sec = 0;
1771 its.it_interval.tv_nsec = 0;
1772 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1773 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1774 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1775 fprintf(stderr, "could not set timerfd\n: %m");
1776 return -1;
1777 }
1778
1779 return 0;
1780}
1781
1782int
1783widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1784{
1785 struct tooltip *tooltip = parent->tooltip;
1786
1787 parent->tooltip_count++;
1788 if (tooltip) {
1789 tooltip->x = x;
1790 tooltip->y = y;
1791 tooltip_timer_reset(tooltip);
1792 return 0;
1793 }
1794
1795 /* the handler might be triggered too fast via input device motion, so
1796 * we need this check here to make sure tooltip is fully initialized */
1797 if (parent->tooltip_count > 1)
1798 return 0;
1799
1800 tooltip = malloc(sizeof *tooltip);
1801 if (!tooltip)
1802 return -1;
1803
1804 parent->tooltip = tooltip;
1805 tooltip->parent = parent;
1806 tooltip->widget = NULL;
1807 tooltip->window = NULL;
1808 tooltip->x = x;
1809 tooltip->y = y;
1810 tooltip->entry = strdup(entry);
1811 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1812 if (tooltip->tooltip_fd < 0) {
1813 fprintf(stderr, "could not create timerfd\n: %m");
1814 return -1;
1815 }
1816
1817 tooltip->tooltip_task.run = tooltip_func;
1818 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1819 EPOLLIN, &tooltip->tooltip_task);
1820 tooltip_timer_reset(tooltip);
1821
1822 return 0;
1823}
1824
1825static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001826workspace_manager_state(void *data,
1827 struct workspace_manager *workspace_manager,
1828 uint32_t current,
1829 uint32_t count)
1830{
1831 struct display *display = data;
1832
1833 display->workspace = current;
1834 display->workspace_count = count;
1835}
1836
1837static const struct workspace_manager_listener workspace_manager_listener = {
1838 workspace_manager_state
1839};
1840
1841static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001842frame_resize_handler(struct widget *widget,
1843 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001844{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001845 struct frame *frame = data;
1846 struct widget *child = frame->child;
1847 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001848 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001849 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001850 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001851 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001852 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001853 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001854 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001855
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001856 switch (widget->window->type) {
1857 case TYPE_FULLSCREEN:
1858 decoration_width = 0;
1859 decoration_height = 0;
1860
1861 allocation.x = 0;
1862 allocation.y = 0;
1863 allocation.width = width;
1864 allocation.height = height;
1865 opaque_margin = 0;
1866
1867 wl_list_for_each(button, &frame->buttons_list, link)
1868 button->widget->opaque = 1;
1869 break;
1870 case TYPE_MAXIMIZED:
1871 decoration_width = t->width * 2;
1872 decoration_height = t->width + t->titlebar_height;
1873
1874 allocation.x = t->width;
1875 allocation.y = t->titlebar_height;
1876 allocation.width = width - decoration_width;
1877 allocation.height = height - decoration_height;
1878
1879 opaque_margin = 0;
1880
1881 wl_list_for_each(button, &frame->buttons_list, link)
1882 button->widget->opaque = 0;
1883 break;
1884 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001885 decoration_width = (t->width + t->margin) * 2;
1886 decoration_height = t->width +
1887 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001888
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001889 allocation.x = t->width + t->margin;
1890 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001891 allocation.width = width - decoration_width;
1892 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001893
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001894 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001895
1896 wl_list_for_each(button, &frame->buttons_list, link)
1897 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001898 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001899 }
1900
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001901 widget_set_allocation(child, allocation.x, allocation.y,
1902 allocation.width, allocation.height);
1903
1904 if (child->resize_handler)
1905 child->resize_handler(child,
1906 allocation.width,
1907 allocation.height,
1908 child->user_data);
1909
Scott Moreauf7e498c2012-05-14 11:39:29 -06001910 width = child->allocation.width + decoration_width;
1911 height = child->allocation.height + decoration_height;
1912
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001913 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1914
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001915 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001916 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001917 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001918 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001919 shadow_margin, shadow_margin,
1920 width - 2 * shadow_margin,
1921 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001922 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001923 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001924 }
1925
Scott Moreauf7e498c2012-05-14 11:39:29 -06001926 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001927
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001928 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001929 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001930 opaque_margin, opaque_margin,
1931 widget->allocation.width - 2 * opaque_margin,
1932 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001933
1934 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001935 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1936 x_l = t->width + shadow_margin;
1937 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001938 wl_list_for_each(button, &frame->buttons_list, link) {
1939 const int button_padding = 4;
1940 w = cairo_image_surface_get_width(button->icon);
1941 h = cairo_image_surface_get_height(button->icon);
1942
1943 if (button->decoration == FRAME_BUTTON_FANCY)
1944 w += 10;
1945
1946 if (button->align == FRAME_BUTTON_LEFT) {
1947 widget_set_allocation(button->widget,
1948 x_l, y , w + 1, h + 1);
1949 x_l += w;
1950 x_l += button_padding;
1951 } else {
1952 x_r -= w;
1953 widget_set_allocation(button->widget,
1954 x_r, y , w + 1, h + 1);
1955 x_r -= button_padding;
1956 }
1957 }
1958}
1959
1960static int
1961frame_button_enter_handler(struct widget *widget,
1962 struct input *input, float x, float y, void *data)
1963{
1964 struct frame_button *frame_button = data;
1965
1966 widget_schedule_redraw(frame_button->widget);
1967 frame_button->state = FRAME_BUTTON_OVER;
1968
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001969 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001970}
1971
1972static void
1973frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1974{
1975 struct frame_button *frame_button = data;
1976
1977 widget_schedule_redraw(frame_button->widget);
1978 frame_button->state = FRAME_BUTTON_DEFAULT;
1979}
1980
1981static void
1982frame_button_button_handler(struct widget *widget,
1983 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001984 uint32_t button,
1985 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001986{
1987 struct frame_button *frame_button = data;
1988 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001989 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001990
1991 if (button != BTN_LEFT)
1992 return;
1993
1994 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001995 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001996 frame_button->state = FRAME_BUTTON_ACTIVE;
1997 widget_schedule_redraw(frame_button->widget);
1998
1999 if (frame_button->type == FRAME_BUTTON_ICON)
2000 window_show_frame_menu(window, input, time);
2001 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002002 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002003 frame_button->state = FRAME_BUTTON_DEFAULT;
2004 widget_schedule_redraw(frame_button->widget);
2005 break;
2006 }
2007
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002008 if (!was_pressed)
2009 return;
2010
Martin Minarik1998b152012-05-10 02:04:35 +02002011 switch (frame_button->type) {
2012 case FRAME_BUTTON_CLOSE:
2013 if (window->close_handler)
2014 window->close_handler(window->parent,
2015 window->user_data);
2016 else
2017 display_exit(window->display);
2018 break;
2019 case FRAME_BUTTON_MINIMIZE:
2020 fprintf(stderr,"Minimize stub\n");
2021 break;
2022 case FRAME_BUTTON_MAXIMIZE:
2023 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2024 break;
2025 default:
2026 /* Unknown operation */
2027 break;
2028 }
2029}
2030
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002031static int
2032frame_button_motion_handler(struct widget *widget,
2033 struct input *input, uint32_t time,
2034 float x, float y, void *data)
2035{
2036 struct frame_button *frame_button = data;
2037 enum frame_button_pointer previous_button_state = frame_button->state;
2038
2039 /* only track state for a pressed button */
2040 if (input->grab != widget)
2041 return CURSOR_LEFT_PTR;
2042
2043 if (x > widget->allocation.x &&
2044 x < (widget->allocation.x + widget->allocation.width) &&
2045 y > widget->allocation.y &&
2046 y < (widget->allocation.y + widget->allocation.height)) {
2047 frame_button->state = FRAME_BUTTON_ACTIVE;
2048 } else {
2049 frame_button->state = FRAME_BUTTON_DEFAULT;
2050 }
2051
2052 if (frame_button->state != previous_button_state)
2053 widget_schedule_redraw(frame_button->widget);
2054
2055 return CURSOR_LEFT_PTR;
2056}
2057
Martin Minarik1998b152012-05-10 02:04:35 +02002058static void
2059frame_button_redraw_handler(struct widget *widget, void *data)
2060{
2061 struct frame_button *frame_button = data;
2062 cairo_t *cr;
2063 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002064
2065 x = widget->allocation.x;
2066 y = widget->allocation.y;
2067 width = widget->allocation.width;
2068 height = widget->allocation.height;
2069
2070 if (!width)
2071 return;
2072 if (!height)
2073 return;
2074 if (widget->opaque)
2075 return;
2076
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002077 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002078
2079 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2080 cairo_set_line_width(cr, 1);
2081
2082 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2083 cairo_rectangle (cr, x, y, 25, 16);
2084
2085 cairo_stroke_preserve(cr);
2086
2087 switch (frame_button->state) {
2088 case FRAME_BUTTON_DEFAULT:
2089 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2090 break;
2091 case FRAME_BUTTON_OVER:
2092 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2093 break;
2094 case FRAME_BUTTON_ACTIVE:
2095 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2096 break;
2097 }
2098
2099 cairo_fill (cr);
2100
2101 x += 4;
2102 }
2103
2104 cairo_set_source_surface(cr, frame_button->icon, x, y);
2105 cairo_paint(cr);
2106
2107 cairo_destroy(cr);
2108}
2109
2110static struct widget *
2111frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2112 enum frame_button_align align, enum frame_button_decoration style)
2113{
2114 struct frame_button *frame_button;
2115 const char *icon = data;
2116
2117 frame_button = malloc (sizeof *frame_button);
2118 memset(frame_button, 0, sizeof *frame_button);
2119
2120 frame_button->icon = cairo_image_surface_create_from_png(icon);
2121 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2122 frame_button->frame = frame;
2123 frame_button->type = type;
2124 frame_button->align = align;
2125 frame_button->decoration = style;
2126
2127 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2128
2129 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2130 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2131 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2132 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002133 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002134 return frame_button->widget;
2135}
2136
2137static void
2138frame_button_destroy(struct frame_button *frame_button)
2139{
2140 widget_destroy(frame_button->widget);
2141 wl_list_remove(&frame_button->link);
2142 cairo_surface_destroy(frame_button->icon);
2143 free(frame_button);
2144
2145 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002146}
2147
2148static void
2149frame_redraw_handler(struct widget *widget, void *data)
2150{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002151 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002152 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002153 struct theme *t = window->display->theme;
2154 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002155
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002156 if (window->type == TYPE_FULLSCREEN)
2157 return;
2158
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002159 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002160
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002161 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002162 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002163 if (window->type == TYPE_MAXIMIZED)
2164 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002165 theme_render_frame(t, cr, widget->allocation.width,
2166 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002167
2168 cairo_destroy(cr);
2169}
2170
2171static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002172frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002173{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002174 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002175 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002176 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002177
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002178 if (window->type != TYPE_TOPLEVEL)
2179 return CURSOR_LEFT_PTR;
2180
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002181 location = theme_get_location(t, input->sx, input->sy,
2182 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002183 frame->widget->allocation.height,
2184 window->type == TYPE_MAXIMIZED ?
2185 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002186
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002187 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002188 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002189 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002190 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002191 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002192 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002193 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002194 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002195 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002196 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002197 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002198 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002199 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002200 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002201 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002202 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002203 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002204 case THEME_LOCATION_EXTERIOR:
2205 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002206 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002207 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002208 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002209}
2210
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211static void
2212frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002213{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002214 struct display *display;
2215
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002216 switch (index) {
2217 case 0: /* close */
2218 if (window->close_handler)
2219 window->close_handler(window->parent,
2220 window->user_data);
2221 else
2222 display_exit(window->display);
2223 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002224 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002225 display = window->display;
2226 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002227 workspace_manager_move_surface(
2228 display->workspace_manager,
2229 window->main_surface->surface,
2230 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002231 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002232 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002233 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002234 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002235 workspace_manager_move_surface(
2236 display->workspace_manager,
2237 window->main_surface->surface,
2238 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002239 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002240 case 3: /* fullscreen */
2241 /* we don't have a way to get out of fullscreen for now */
2242 if (window->fullscreen_handler)
2243 window->fullscreen_handler(window, window->user_data);
2244 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002245 }
2246}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002247
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002248void
2249window_show_frame_menu(struct window *window,
2250 struct input *input, uint32_t time)
2251{
2252 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002253 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002254
2255 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002256 "Close",
2257 "Move to workspace above", "Move to workspace below",
2258 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002259 };
2260
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002261 if (window->fullscreen_handler)
2262 count = ARRAY_LENGTH(entries);
2263 else
2264 count = ARRAY_LENGTH(entries) - 1;
2265
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002266 input_get_position(input, &x, &y);
2267 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002268 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002269}
2270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002271static int
2272frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002273 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002274{
2275 return frame_get_pointer_image_for_location(data, input);
2276}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002277
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002278static int
2279frame_motion_handler(struct widget *widget,
2280 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002281 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002282{
2283 return frame_get_pointer_image_for_location(data, input);
2284}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002285
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002286static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002287frame_button_handler(struct widget *widget,
2288 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002289 uint32_t button, enum wl_pointer_button_state state,
2290 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002291
2292{
2293 struct frame *frame = data;
2294 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002295 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002296 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002297
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002298 if (window->type != TYPE_TOPLEVEL)
2299 return;
2300
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002301 location = theme_get_location(display->theme, input->sx, input->sy,
2302 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002303 frame->widget->allocation.height,
2304 window->type == TYPE_MAXIMIZED ?
2305 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002306
Daniel Stone4dbadb12012-05-30 16:31:51 +01002307 if (window->display->shell && button == BTN_LEFT &&
2308 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002309 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002310 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002311 if (!window->shell_surface)
2312 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002313 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002314 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002315 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002316 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002317 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002318 case THEME_LOCATION_RESIZING_TOP:
2319 case THEME_LOCATION_RESIZING_BOTTOM:
2320 case THEME_LOCATION_RESIZING_LEFT:
2321 case THEME_LOCATION_RESIZING_RIGHT:
2322 case THEME_LOCATION_RESIZING_TOP_LEFT:
2323 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2324 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2325 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002326 if (!window->shell_surface)
2327 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002328 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002329
Pekka Paalanen99436862012-11-19 17:15:59 +02002330 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002331 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002332 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002333 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002334 break;
2335 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002336 } else if (button == BTN_RIGHT &&
2337 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002338 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002339 }
2340}
2341
2342struct widget *
2343frame_create(struct window *window, void *data)
2344{
2345 struct frame *frame;
2346
2347 frame = malloc(sizeof *frame);
2348 memset(frame, 0, sizeof *frame);
2349
2350 frame->widget = window_add_widget(window, frame);
2351 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002352
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002353 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2354 widget_set_resize_handler(frame->widget, frame_resize_handler);
2355 widget_set_enter_handler(frame->widget, frame_enter_handler);
2356 widget_set_motion_handler(frame->widget, frame_motion_handler);
2357 widget_set_button_handler(frame->widget, frame_button_handler);
2358
Martin Minarik1998b152012-05-10 02:04:35 +02002359 /* Create empty list for frame buttons */
2360 wl_list_init(&frame->buttons_list);
2361
2362 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2363 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2364
2365 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2366 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2367
2368 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2369 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2370
2371 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2372 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2373
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002374 window->frame = frame;
2375
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002376 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002377}
2378
Kristian Høgsberga1627922012-06-20 17:30:03 -04002379void
2380frame_set_child_size(struct widget *widget, int child_width, int child_height)
2381{
2382 struct display *display = widget->window->display;
2383 struct theme *t = display->theme;
2384 int decoration_width, decoration_height;
2385 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002386 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002387
2388 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002389 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002390 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002391 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002392
2393 width = child_width + decoration_width;
2394 height = child_height + decoration_height;
2395 } else {
2396 width = child_width;
2397 height = child_height;
2398 }
2399
2400 window_schedule_resize(widget->window, width, height);
2401}
2402
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002403static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002404frame_destroy(struct frame *frame)
2405{
Martin Minarik1998b152012-05-10 02:04:35 +02002406 struct frame_button *button, *tmp;
2407
2408 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2409 frame_button_destroy(button);
2410
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002411 /* frame->child must be destroyed by the application */
2412 widget_destroy(frame->widget);
2413 free(frame);
2414}
2415
2416static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002417input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002418 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002419{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002420 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002421 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002422
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002423 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002424 return;
2425
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002426 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002427 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002428 widget = old;
2429 if (input->grab)
2430 widget = input->grab;
2431 if (widget->leave_handler)
2432 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002433 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002434 }
2435
2436 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002437 widget = focus;
2438 if (input->grab)
2439 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002440 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002441 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002442 cursor = widget->enter_handler(focus, input, x, y,
2443 widget->user_data);
2444 else
2445 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002446
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002447 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002448 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002449}
2450
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002451void
2452input_grab(struct input *input, struct widget *widget, uint32_t button)
2453{
2454 input->grab = widget;
2455 input->grab_button = button;
2456}
2457
2458void
2459input_ungrab(struct input *input)
2460{
2461 struct widget *widget;
2462
2463 input->grab = NULL;
2464 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002465 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002466 input->sx, input->sy);
2467 input_set_focus_widget(input, widget, input->sx, input->sy);
2468 }
2469}
2470
2471static void
2472input_remove_pointer_focus(struct input *input)
2473{
2474 struct window *window = input->pointer_focus;
2475
2476 if (!window)
2477 return;
2478
2479 input_set_focus_widget(input, NULL, 0, 0);
2480
2481 input->pointer_focus = NULL;
2482 input->current_cursor = CURSOR_UNSET;
2483}
2484
2485static void
2486pointer_handle_enter(void *data, struct wl_pointer *pointer,
2487 uint32_t serial, struct wl_surface *surface,
2488 wl_fixed_t sx_w, wl_fixed_t sy_w)
2489{
2490 struct input *input = data;
2491 struct window *window;
2492 struct widget *widget;
2493 float sx = wl_fixed_to_double(sx_w);
2494 float sy = wl_fixed_to_double(sy_w);
2495
2496 if (!surface) {
2497 /* enter event for a window we've just destroyed */
2498 return;
2499 }
2500
2501 input->display->serial = serial;
2502 input->pointer_enter_serial = serial;
2503 input->pointer_focus = wl_surface_get_user_data(surface);
2504 window = input->pointer_focus;
2505
Pekka Paalanen99436862012-11-19 17:15:59 +02002506 if (window->resizing) {
2507 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002508 /* Schedule a redraw to free the pool */
2509 window_schedule_redraw(window);
2510 }
2511
2512 input->sx = sx;
2513 input->sy = sy;
2514
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002515 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002516 input_set_focus_widget(input, widget, sx, sy);
2517}
2518
2519static void
2520pointer_handle_leave(void *data, struct wl_pointer *pointer,
2521 uint32_t serial, struct wl_surface *surface)
2522{
2523 struct input *input = data;
2524
2525 input->display->serial = serial;
2526 input_remove_pointer_focus(input);
2527}
2528
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002529static void
Daniel Stone37816df2012-05-16 18:45:18 +01002530pointer_handle_motion(void *data, struct wl_pointer *pointer,
2531 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002532{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002533 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002534 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002535 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002536 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002537 float sx = wl_fixed_to_double(sx_w);
2538 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002539
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002540 input->sx = sx;
2541 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002542
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002543 if (!window)
2544 return;
2545
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002546 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002547 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002548 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002549 }
2550
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002551 if (input->grab)
2552 widget = input->grab;
2553 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002554 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002555 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002556 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002557 input, time, sx, sy,
2558 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002559 else
2560 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002561
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002562 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002563}
2564
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002565static void
Daniel Stone37816df2012-05-16 18:45:18 +01002566pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002567 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002568{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002569 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002570 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002571 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002572
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002573 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002574 if (input->focus_widget && input->grab == NULL &&
2575 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002576 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002577
Neil Roberts6b28aad2012-01-23 19:11:18 +00002578 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002579 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002580 (*widget->button_handler)(widget,
2581 input, time,
2582 button, state,
2583 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002584
Daniel Stone4dbadb12012-05-30 16:31:51 +01002585 if (input->grab && input->grab_button == button &&
2586 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002587 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002588}
2589
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002590static void
Daniel Stone37816df2012-05-16 18:45:18 +01002591pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002592 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002593{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002594 struct input *input = data;
2595 struct widget *widget;
2596
2597 widget = input->focus_widget;
2598 if (input->grab)
2599 widget = input->grab;
2600 if (widget && widget->axis_handler)
2601 (*widget->axis_handler)(widget,
2602 input, time,
2603 axis, value,
2604 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002605}
2606
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002607static const struct wl_pointer_listener pointer_listener = {
2608 pointer_handle_enter,
2609 pointer_handle_leave,
2610 pointer_handle_motion,
2611 pointer_handle_button,
2612 pointer_handle_axis,
2613};
2614
2615static void
2616input_remove_keyboard_focus(struct input *input)
2617{
2618 struct window *window = input->keyboard_focus;
2619 struct itimerspec its;
2620
2621 its.it_interval.tv_sec = 0;
2622 its.it_interval.tv_nsec = 0;
2623 its.it_value.tv_sec = 0;
2624 its.it_value.tv_nsec = 0;
2625 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2626
2627 if (!window)
2628 return;
2629
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002630 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002631 if (window->keyboard_focus_handler)
2632 (*window->keyboard_focus_handler)(window, NULL,
2633 window->user_data);
2634
2635 input->keyboard_focus = NULL;
2636}
2637
2638static void
2639keyboard_repeat_func(struct task *task, uint32_t events)
2640{
2641 struct input *input =
2642 container_of(task, struct input, repeat_task);
2643 struct window *window = input->keyboard_focus;
2644 uint64_t exp;
2645
2646 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2647 /* If we change the timer between the fd becoming
2648 * readable and getting here, there'll be nothing to
2649 * read and we get EAGAIN. */
2650 return;
2651
2652 if (window && window->key_handler) {
2653 (*window->key_handler)(window, input, input->repeat_time,
2654 input->repeat_key, input->repeat_sym,
2655 WL_KEYBOARD_KEY_STATE_PRESSED,
2656 window->user_data);
2657 }
2658}
2659
2660static void
2661keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2662 uint32_t format, int fd, uint32_t size)
2663{
2664 struct input *input = data;
2665 char *map_str;
2666
2667 if (!data) {
2668 close(fd);
2669 return;
2670 }
2671
2672 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2673 close(fd);
2674 return;
2675 }
2676
2677 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2678 if (map_str == MAP_FAILED) {
2679 close(fd);
2680 return;
2681 }
2682
2683 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2684 map_str,
2685 XKB_KEYMAP_FORMAT_TEXT_V1,
2686 0);
2687 munmap(map_str, size);
2688 close(fd);
2689
2690 if (!input->xkb.keymap) {
2691 fprintf(stderr, "failed to compile keymap\n");
2692 return;
2693 }
2694
2695 input->xkb.state = xkb_state_new(input->xkb.keymap);
2696 if (!input->xkb.state) {
2697 fprintf(stderr, "failed to create XKB state\n");
2698 xkb_map_unref(input->xkb.keymap);
2699 input->xkb.keymap = NULL;
2700 return;
2701 }
2702
2703 input->xkb.control_mask =
2704 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2705 input->xkb.alt_mask =
2706 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2707 input->xkb.shift_mask =
2708 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2709}
2710
2711static void
2712keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2713 uint32_t serial, struct wl_surface *surface,
2714 struct wl_array *keys)
2715{
2716 struct input *input = data;
2717 struct window *window;
2718
2719 input->display->serial = serial;
2720 input->keyboard_focus = wl_surface_get_user_data(surface);
2721
2722 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002723 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002724 if (window->keyboard_focus_handler)
2725 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002726 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002727}
2728
2729static void
2730keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2731 uint32_t serial, struct wl_surface *surface)
2732{
2733 struct input *input = data;
2734
2735 input->display->serial = serial;
2736 input_remove_keyboard_focus(input);
2737}
2738
Scott Moreau210d0792012-03-22 10:47:01 -06002739static void
Daniel Stone37816df2012-05-16 18:45:18 +01002740keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002741 uint32_t serial, uint32_t time, uint32_t key,
2742 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002743{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002744 struct input *input = data;
2745 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002746 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002747 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002748 const xkb_keysym_t *syms;
2749 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002750 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002751
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002752 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002753 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002754 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002755 return;
2756
Daniel Stone97f68542012-05-30 16:32:01 +01002757 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002758
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002759 sym = XKB_KEY_NoSymbol;
2760 if (num_syms == 1)
2761 sym = syms[0];
2762
2763 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002764 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002765 window_set_maximized(window,
2766 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002767 } else if (sym == XKB_KEY_F11 &&
2768 window->fullscreen_handler &&
2769 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2770 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002771 } else if (sym == XKB_KEY_F4 &&
2772 input->modifiers == MOD_ALT_MASK &&
2773 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2774 if (window->close_handler)
2775 window->close_handler(window->parent,
2776 window->user_data);
2777 else
2778 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002779 } else if (window->key_handler) {
2780 (*window->key_handler)(window, input, time, key,
2781 sym, state, window->user_data);
2782 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002783
2784 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2785 key == input->repeat_key) {
2786 its.it_interval.tv_sec = 0;
2787 its.it_interval.tv_nsec = 0;
2788 its.it_value.tv_sec = 0;
2789 its.it_value.tv_nsec = 0;
2790 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2791 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2792 input->repeat_sym = sym;
2793 input->repeat_key = key;
2794 input->repeat_time = time;
2795 its.it_interval.tv_sec = 0;
2796 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2797 its.it_value.tv_sec = 0;
2798 its.it_value.tv_nsec = 400 * 1000 * 1000;
2799 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2800 }
2801}
2802
2803static void
Daniel Stone351eb612012-05-31 15:27:47 -04002804keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2805 uint32_t serial, uint32_t mods_depressed,
2806 uint32_t mods_latched, uint32_t mods_locked,
2807 uint32_t group)
2808{
2809 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002810 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002811
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002812 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2813 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002814 mask = xkb_state_serialize_mods(input->xkb.state,
2815 XKB_STATE_DEPRESSED |
2816 XKB_STATE_LATCHED);
2817 input->modifiers = 0;
2818 if (mask & input->xkb.control_mask)
2819 input->modifiers |= MOD_CONTROL_MASK;
2820 if (mask & input->xkb.alt_mask)
2821 input->modifiers |= MOD_ALT_MASK;
2822 if (mask & input->xkb.shift_mask)
2823 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002824}
2825
Daniel Stone37816df2012-05-16 18:45:18 +01002826static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002827 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002828 keyboard_handle_enter,
2829 keyboard_handle_leave,
2830 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002831 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002832};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002833
2834static void
Daniel Stone37816df2012-05-16 18:45:18 +01002835seat_handle_capabilities(void *data, struct wl_seat *seat,
2836 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002837{
Daniel Stone37816df2012-05-16 18:45:18 +01002838 struct input *input = data;
2839
2840 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2841 input->pointer = wl_seat_get_pointer(seat);
2842 wl_pointer_set_user_data(input->pointer, input);
2843 wl_pointer_add_listener(input->pointer, &pointer_listener,
2844 input);
2845 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2846 wl_pointer_destroy(input->pointer);
2847 input->pointer = NULL;
2848 }
2849
2850 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2851 input->keyboard = wl_seat_get_keyboard(seat);
2852 wl_keyboard_set_user_data(input->keyboard, input);
2853 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2854 input);
2855 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2856 wl_keyboard_destroy(input->keyboard);
2857 input->keyboard = NULL;
2858 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002859}
2860
Daniel Stone37816df2012-05-16 18:45:18 +01002861static const struct wl_seat_listener seat_listener = {
2862 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002863};
2864
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002865void
2866input_get_position(struct input *input, int32_t *x, int32_t *y)
2867{
2868 *x = input->sx;
2869 *y = input->sy;
2870}
2871
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002872struct display *
2873input_get_display(struct input *input)
2874{
2875 return input->display;
2876}
2877
Daniel Stone37816df2012-05-16 18:45:18 +01002878struct wl_seat *
2879input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002880{
Daniel Stone37816df2012-05-16 18:45:18 +01002881 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002882}
2883
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002884uint32_t
2885input_get_modifiers(struct input *input)
2886{
2887 return input->modifiers;
2888}
2889
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002890struct widget *
2891input_get_focus_widget(struct input *input)
2892{
2893 return input->focus_widget;
2894}
2895
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002896struct data_offer {
2897 struct wl_data_offer *offer;
2898 struct input *input;
2899 struct wl_array types;
2900 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002901
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002902 struct task io_task;
2903 int fd;
2904 data_func_t func;
2905 int32_t x, y;
2906 void *user_data;
2907};
2908
2909static void
2910data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2911{
2912 struct data_offer *offer = data;
2913 char **p;
2914
2915 p = wl_array_add(&offer->types, sizeof *p);
2916 *p = strdup(type);
2917}
2918
2919static const struct wl_data_offer_listener data_offer_listener = {
2920 data_offer_offer,
2921};
2922
2923static void
2924data_offer_destroy(struct data_offer *offer)
2925{
2926 char **p;
2927
2928 offer->refcount--;
2929 if (offer->refcount == 0) {
2930 wl_data_offer_destroy(offer->offer);
2931 for (p = offer->types.data; *p; p++)
2932 free(*p);
2933 wl_array_release(&offer->types);
2934 free(offer);
2935 }
2936}
2937
2938static void
2939data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002940 struct wl_data_device *data_device,
2941 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002942{
2943 struct data_offer *offer;
2944
2945 offer = malloc(sizeof *offer);
2946
2947 wl_array_init(&offer->types);
2948 offer->refcount = 1;
2949 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002950 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002951 wl_data_offer_add_listener(offer->offer,
2952 &data_offer_listener, offer);
2953}
2954
2955static void
2956data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002957 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002958 wl_fixed_t x_w, wl_fixed_t y_w,
2959 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002960{
2961 struct input *input = data;
2962 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002963 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002964 float x = wl_fixed_to_double(x_w);
2965 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002966 char **p;
2967
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002968 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002969 window = wl_surface_get_user_data(surface);
2970 input->pointer_focus = window;
2971
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002972 if (offer) {
2973 input->drag_offer = wl_data_offer_get_user_data(offer);
2974
2975 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2976 *p = NULL;
2977
2978 types_data = input->drag_offer->types.data;
2979 } else {
2980 input->drag_offer = NULL;
2981 types_data = NULL;
2982 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002983
2984 window = input->pointer_focus;
2985 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002986 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002987 window->user_data);
2988}
2989
2990static void
2991data_device_leave(void *data, struct wl_data_device *data_device)
2992{
2993 struct input *input = data;
2994
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002995 if (input->drag_offer) {
2996 data_offer_destroy(input->drag_offer);
2997 input->drag_offer = NULL;
2998 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002999}
3000
3001static void
3002data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003003 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003004{
3005 struct input *input = data;
3006 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003007 float x = wl_fixed_to_double(x_w);
3008 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003009 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003010
3011 input->sx = x;
3012 input->sy = y;
3013
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003014 if (input->drag_offer)
3015 types_data = input->drag_offer->types.data;
3016 else
3017 types_data = NULL;
3018
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003019 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003020 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003021 window->user_data);
3022}
3023
3024static void
3025data_device_drop(void *data, struct wl_data_device *data_device)
3026{
3027 struct input *input = data;
3028 struct window *window = input->pointer_focus;
3029
3030 if (window->drop_handler)
3031 window->drop_handler(window, input,
3032 input->sx, input->sy, window->user_data);
3033}
3034
3035static void
3036data_device_selection(void *data,
3037 struct wl_data_device *wl_data_device,
3038 struct wl_data_offer *offer)
3039{
3040 struct input *input = data;
3041 char **p;
3042
3043 if (input->selection_offer)
3044 data_offer_destroy(input->selection_offer);
3045
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003046 if (offer) {
3047 input->selection_offer = wl_data_offer_get_user_data(offer);
3048 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3049 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003050 } else {
3051 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003052 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003053}
3054
3055static const struct wl_data_device_listener data_device_listener = {
3056 data_device_data_offer,
3057 data_device_enter,
3058 data_device_leave,
3059 data_device_motion,
3060 data_device_drop,
3061 data_device_selection
3062};
3063
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003064static void
3065input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003066{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003067 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003068 struct wl_cursor *cursor;
3069 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003070
Daniel Stone80972742012-11-07 17:51:39 +11003071 if (!input->pointer)
3072 return;
3073
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003074 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003075 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003076 return;
3077
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003078 if (index >= (int) cursor->image_count) {
3079 fprintf(stderr, "cursor index out of range\n");
3080 return;
3081 }
3082
3083 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003084 buffer = wl_cursor_image_get_buffer(image);
3085 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003086 return;
3087
Kristian Høgsbergae277372012-08-01 09:41:08 -04003088 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003089 input->pointer_surface,
3090 image->hotspot_x, image->hotspot_y);
3091 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3092 wl_surface_damage(input->pointer_surface, 0, 0,
3093 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003094 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003095}
3096
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003097static const struct wl_callback_listener pointer_surface_listener;
3098
3099static void
3100pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3101 uint32_t time)
3102{
3103 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003104 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003105 int i;
3106
3107 if (callback) {
3108 assert(callback == input->cursor_frame_cb);
3109 wl_callback_destroy(callback);
3110 input->cursor_frame_cb = NULL;
3111 }
3112
Daniel Stone80972742012-11-07 17:51:39 +11003113 if (!input->pointer)
3114 return;
3115
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003116 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003117 wl_pointer_set_cursor(input->pointer,
3118 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003119 NULL, 0, 0);
3120 return;
3121 }
3122
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003123 if (input->current_cursor == CURSOR_UNSET)
3124 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003125 cursor = input->display->cursors[input->current_cursor];
3126 if (!cursor)
3127 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003128
3129 /* FIXME We don't have the current time on the first call so we set
3130 * the animation start to the time of the first frame callback. */
3131 if (time == 0)
3132 input->cursor_anim_start = 0;
3133 else if (input->cursor_anim_start == 0)
3134 input->cursor_anim_start = time;
3135
3136 if (time == 0 || input->cursor_anim_start == 0)
3137 i = 0;
3138 else
3139 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3140
Pekka Paalanenbc106382012-10-10 12:49:31 +03003141 if (cursor->image_count > 1) {
3142 input->cursor_frame_cb =
3143 wl_surface_frame(input->pointer_surface);
3144 wl_callback_add_listener(input->cursor_frame_cb,
3145 &pointer_surface_listener, input);
3146 }
3147
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003148 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003149}
3150
3151static const struct wl_callback_listener pointer_surface_listener = {
3152 pointer_surface_frame_callback
3153};
3154
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003155void
3156input_set_pointer_image(struct input *input, int pointer)
3157{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003158 int force = 0;
3159
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003160 if (!input->pointer)
3161 return;
3162
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003163 if (input->pointer_enter_serial > input->cursor_serial)
3164 force = 1;
3165
3166 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003167 return;
3168
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003169 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003170 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003171 if (!input->cursor_frame_cb)
3172 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003173 else if (force) {
3174 /* The current frame callback may be stuck if, for instance,
3175 * the set cursor request was processed by the server after
3176 * this client lost the focus. In this case the cursor surface
3177 * might not be mapped and the frame callback wouldn't ever
3178 * complete. Send a set_cursor and attach to try to map the
3179 * cursor surface again so that the callback will finish */
3180 input_set_pointer_image_index(input, 0);
3181 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003182}
3183
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003184struct wl_data_device *
3185input_get_data_device(struct input *input)
3186{
3187 return input->data_device;
3188}
3189
3190void
3191input_set_selection(struct input *input,
3192 struct wl_data_source *source, uint32_t time)
3193{
3194 wl_data_device_set_selection(input->data_device, source, time);
3195}
3196
3197void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003198input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003199{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003200 wl_data_offer_accept(input->drag_offer->offer,
3201 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003202}
3203
3204static void
3205offer_io_func(struct task *task, uint32_t events)
3206{
3207 struct data_offer *offer =
3208 container_of(task, struct data_offer, io_task);
3209 unsigned int len;
3210 char buffer[4096];
3211
3212 len = read(offer->fd, buffer, sizeof buffer);
3213 offer->func(buffer, len,
3214 offer->x, offer->y, offer->user_data);
3215
3216 if (len == 0) {
3217 close(offer->fd);
3218 data_offer_destroy(offer);
3219 }
3220}
3221
3222static void
3223data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3224 data_func_t func, void *user_data)
3225{
3226 int p[2];
3227
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003228 if (pipe2(p, O_CLOEXEC) == -1)
3229 return;
3230
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003231 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3232 close(p[1]);
3233
3234 offer->io_task.run = offer_io_func;
3235 offer->fd = p[0];
3236 offer->func = func;
3237 offer->refcount++;
3238 offer->user_data = user_data;
3239
3240 display_watch_fd(offer->input->display,
3241 offer->fd, EPOLLIN, &offer->io_task);
3242}
3243
3244void
3245input_receive_drag_data(struct input *input, const char *mime_type,
3246 data_func_t func, void *data)
3247{
3248 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3249 input->drag_offer->x = input->sx;
3250 input->drag_offer->y = input->sy;
3251}
3252
3253int
3254input_receive_selection_data(struct input *input, const char *mime_type,
3255 data_func_t func, void *data)
3256{
3257 char **p;
3258
3259 if (input->selection_offer == NULL)
3260 return -1;
3261
3262 for (p = input->selection_offer->types.data; *p; p++)
3263 if (strcmp(mime_type, *p) == 0)
3264 break;
3265
3266 if (*p == NULL)
3267 return -1;
3268
3269 data_offer_receive_data(input->selection_offer,
3270 mime_type, func, data);
3271 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003272}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003273
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003274int
3275input_receive_selection_data_to_fd(struct input *input,
3276 const char *mime_type, int fd)
3277{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003278 if (input->selection_offer)
3279 wl_data_offer_receive(input->selection_offer->offer,
3280 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003281
3282 return 0;
3283}
3284
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003285void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003286window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003287{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003288 if (!window->shell_surface)
3289 return;
3290
Daniel Stone37816df2012-05-16 18:45:18 +01003291 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003292}
3293
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003294static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003295surface_set_synchronized(struct surface *surface)
3296{
3297 if (!surface->subsurface)
3298 return;
3299
3300 if (surface->synchronized)
3301 return;
3302
3303 wl_subsurface_set_sync(surface->subsurface);
3304 surface->synchronized = 1;
3305}
3306
3307static void
3308surface_set_synchronized_default(struct surface *surface)
3309{
3310 if (!surface->subsurface)
3311 return;
3312
3313 if (surface->synchronized == surface->synchronized_default)
3314 return;
3315
3316 if (surface->synchronized_default)
3317 wl_subsurface_set_sync(surface->subsurface);
3318 else
3319 wl_subsurface_set_desync(surface->subsurface);
3320
3321 surface->synchronized = surface->synchronized_default;
3322}
3323
3324static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003325surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003326{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003327 struct widget *widget = surface->widget;
3328 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003329
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003330 if (surface->input_region) {
3331 wl_region_destroy(surface->input_region);
3332 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003333 }
3334
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003335 if (surface->opaque_region)
3336 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003337
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003338 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003339
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003340 if (widget->resize_handler)
3341 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003342 widget->allocation.width,
3343 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003344 widget->user_data);
3345
Pekka Paalanen35e82632013-04-25 13:57:48 +03003346 if (surface->subsurface &&
3347 (surface->allocation.x != widget->allocation.x ||
3348 surface->allocation.y != widget->allocation.y)) {
3349 wl_subsurface_set_position(surface->subsurface,
3350 widget->allocation.x,
3351 widget->allocation.y);
3352 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003353 if (surface->allocation.width != widget->allocation.width ||
3354 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003355 window_schedule_redraw(widget->window);
3356 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003357 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003358
3359 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003360 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003361 widget->allocation.width,
3362 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003363}
3364
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003365static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003366hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3367{
3368 /*
3369 * This hack should be removed, when EGL respects
3370 * eglSwapInterval(0).
3371 *
3372 * If this window has sub-surfaces, especially a free-running
3373 * EGL-widget, we need to post the parent surface once with
3374 * all the old state to guarantee, that the EGL-widget will
3375 * receive its frame callback soon. Otherwise, a forced call
3376 * to eglSwapBuffers may end up blocking, waiting for a frame
3377 * event that will never come, because we will commit the parent
3378 * surface with all new state only after eglSwapBuffers returns.
3379 *
3380 * This assumes, that:
3381 * 1. When the EGL widget's resize hook is called, it pauses.
3382 * 2. When the EGL widget's redraw hook is called, it forces a
3383 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3384 * In a single threaded application condition 1 is a no-op.
3385 *
3386 * XXX: This should actually be after the surface_resize() calls,
3387 * but cannot, because then it would commit the incomplete state
3388 * accumulated from the widget resize hooks.
3389 */
3390 if (window->subsurface_list.next != &window->main_surface->link ||
3391 window->subsurface_list.prev != &window->main_surface->link)
3392 wl_surface_commit(window->main_surface->surface);
3393}
3394
3395static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003396idle_resize(struct window *window)
3397{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003398 struct surface *surface;
3399
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003400 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003401 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003402
Pekka Paalanene9297f82013-04-25 13:57:51 +03003403 hack_prevent_EGL_sub_surface_deadlock(window);
3404
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003405 widget_set_allocation(window->main_surface->widget,
3406 window->pending_allocation.x,
3407 window->pending_allocation.y,
3408 window->pending_allocation.width,
3409 window->pending_allocation.height);
3410
3411 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003412
3413 /* The main surface is in the list, too. Main surface's
3414 * resize_handler is responsible for calling widget_set_allocation()
3415 * on all sub-surface root widgets, so they will be resized
3416 * properly.
3417 */
3418 wl_list_for_each(surface, &window->subsurface_list, link) {
3419 if (surface == window->main_surface)
3420 continue;
3421
3422 surface_set_synchronized(surface);
3423 surface_resize(surface);
3424 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003425}
3426
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003427void
3428window_schedule_resize(struct window *window, int width, int height)
3429{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003430 window->pending_allocation.x = 0;
3431 window->pending_allocation.y = 0;
3432 window->pending_allocation.width = width;
3433 window->pending_allocation.height = height;
3434
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003435 if (window->min_allocation.width == 0)
3436 window->min_allocation = window->pending_allocation;
3437 if (window->pending_allocation.width < window->min_allocation.width)
3438 window->pending_allocation.width = window->min_allocation.width;
3439 if (window->pending_allocation.height < window->min_allocation.height)
3440 window->pending_allocation.height = window->min_allocation.height;
3441
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003442 window->resize_needed = 1;
3443 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003444}
3445
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003446void
3447widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3448{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003449 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003450}
3451
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003452static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003453handle_ping(void *data, struct wl_shell_surface *shell_surface,
3454 uint32_t serial)
3455{
3456 wl_shell_surface_pong(shell_surface, serial);
3457}
3458
3459static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003460handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003461 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003462{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003463 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003464
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003465 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003466 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003467}
3468
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003469static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003470menu_destroy(struct menu *menu)
3471{
3472 widget_destroy(menu->widget);
3473 window_destroy(menu->window);
3474 free(menu);
3475}
3476
3477static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003478handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3479{
3480 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003481 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003482
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003483 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003484 * device. Or just use wl_callback. And this really needs to
3485 * be a window vfunc that the menu can set. And we need the
3486 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003487
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003488 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003489 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003490}
3491
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003492static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003493 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003494 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003495 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003496};
3497
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003498void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003499window_get_allocation(struct window *window,
3500 struct rectangle *allocation)
3501{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003502 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003503}
3504
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003505static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003506widget_redraw(struct widget *widget)
3507{
3508 struct widget *child;
3509
3510 if (widget->redraw_handler)
3511 widget->redraw_handler(widget, widget->user_data);
3512 wl_list_for_each(child, &widget->child_list, link)
3513 widget_redraw(child);
3514}
3515
3516static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003517frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3518{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003519 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003520
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003521 assert(callback == surface->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003522 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003523 surface->frame_cb = NULL;
3524
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003525 surface->last_time = time;
3526
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003527 if (surface->redraw_needed || surface->window->redraw_needed)
3528 window_schedule_redraw_task(surface->window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003529}
3530
3531static const struct wl_callback_listener listener = {
3532 frame_callback
3533};
3534
3535static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003536surface_redraw(struct surface *surface)
3537{
3538 if (!surface->window->redraw_needed && !surface->redraw_needed)
3539 return;
3540
3541 /* Whole-window redraw forces a redraw even if the previous has
3542 * not yet hit the screen.
3543 */
3544 if (surface->frame_cb) {
3545 if (!surface->window->redraw_needed)
3546 return;
3547
3548 wl_callback_destroy(surface->frame_cb);
3549 }
3550
3551 surface->frame_cb = wl_surface_frame(surface->surface);
3552 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3553
3554 surface->redraw_needed = 0;
3555 widget_redraw(surface->widget);
3556}
3557
3558static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003559idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003560{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003561 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003562 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003563
Pekka Paalaneneebff542013-04-25 13:57:52 +03003564 wl_list_init(&window->redraw_task.link);
3565 window->redraw_task_scheduled = 0;
3566
3567 if (window->resize_needed) {
3568 /* throttle resizing to the main surface display */
3569 if (window->main_surface->frame_cb)
3570 return;
3571
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003572 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003573 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003574
Pekka Paalanen35e82632013-04-25 13:57:48 +03003575 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003576 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003577
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003578 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003579 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003580
3581 wl_list_for_each(surface, &window->subsurface_list, link)
3582 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003583}
3584
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003585static void
3586window_schedule_redraw_task(struct window *window)
3587{
3588 if (window->configure_requests)
3589 return;
3590 if (!window->redraw_task_scheduled) {
3591 window->redraw_task.run = idle_redraw;
3592 display_defer(window->display, &window->redraw_task);
3593 window->redraw_task_scheduled = 1;
3594 }
3595}
3596
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003597void
3598window_schedule_redraw(struct window *window)
3599{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003600 struct surface *surface;
3601
3602 wl_list_for_each(surface, &window->subsurface_list, link)
3603 surface->redraw_needed = 1;
3604
3605 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003606}
3607
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003608int
3609window_is_fullscreen(struct window *window)
3610{
3611 return window->type == TYPE_FULLSCREEN;
3612}
3613
MoD063a8822013-03-02 23:43:57 +00003614static void
3615configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3616{
3617 struct window *window = data;
3618
3619 wl_callback_destroy(callback);
3620 window->configure_requests--;
3621
3622 if (!window->configure_requests)
3623 window_schedule_redraw(window);
3624}
3625
3626static struct wl_callback_listener configure_request_listener = {
3627 configure_request_completed,
3628};
3629
3630static void
3631window_defer_redraw_until_configure(struct window* window)
3632{
3633 struct wl_callback *callback;
3634
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003635 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003636 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003637 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003638 }
3639
3640 callback = wl_display_sync(window->display->display);
3641 wl_callback_add_listener(callback, &configure_request_listener, window);
3642 window->configure_requests++;
3643}
3644
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003645void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003646window_set_fullscreen(struct window *window, int fullscreen)
3647{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003648 if (!window->display->shell)
3649 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003650
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003651 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003652 return;
3653
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003654 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003655 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003656 if (window->type == TYPE_TOPLEVEL) {
3657 window->saved_allocation = window->main_surface->allocation;
3658 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003659 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003660 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003661 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003662 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003663 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003664 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003665 if (window->saved_type == TYPE_MAXIMIZED) {
3666 window_set_maximized(window, 1);
3667 } else {
3668 window->type = TYPE_TOPLEVEL;
3669 wl_shell_surface_set_toplevel(window->shell_surface);
3670 window_schedule_resize(window,
3671 window->saved_allocation.width,
3672 window->saved_allocation.height);
3673 }
3674
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003675 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003676}
3677
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003678void
3679window_set_fullscreen_method(struct window *window,
3680 enum wl_shell_surface_fullscreen_method method)
3681{
3682 window->fullscreen_method = method;
3683}
3684
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003685int
3686window_is_maximized(struct window *window)
3687{
3688 return window->type == TYPE_MAXIMIZED;
3689}
3690
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003691void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003692window_set_maximized(struct window *window, int maximized)
3693{
3694 if (!window->display->shell)
3695 return;
3696
3697 if ((window->type == TYPE_MAXIMIZED) == maximized)
3698 return;
3699
3700 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003701 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003702 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3703 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003704 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003705 } else if (window->type == TYPE_FULLSCREEN) {
3706 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3707 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003708 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003709 } else {
3710 wl_shell_surface_set_toplevel(window->shell_surface);
3711 window->type = TYPE_TOPLEVEL;
3712 window_schedule_resize(window,
3713 window->saved_allocation.width,
3714 window->saved_allocation.height);
3715 }
3716}
3717
3718void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003719window_set_user_data(struct window *window, void *data)
3720{
3721 window->user_data = data;
3722}
3723
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003724void *
3725window_get_user_data(struct window *window)
3726{
3727 return window->user_data;
3728}
3729
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003730void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003731window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003732 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003733{
3734 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003735}
3736
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003737void
3738window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003739 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003740{
3741 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003742}
3743
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003744void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003745window_set_data_handler(struct window *window, window_data_handler_t handler)
3746{
3747 window->data_handler = handler;
3748}
3749
3750void
3751window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3752{
3753 window->drop_handler = handler;
3754}
3755
3756void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003757window_set_close_handler(struct window *window,
3758 window_close_handler_t handler)
3759{
3760 window->close_handler = handler;
3761}
3762
3763void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003764window_set_fullscreen_handler(struct window *window,
3765 window_fullscreen_handler_t handler)
3766{
3767 window->fullscreen_handler = handler;
3768}
3769
3770void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003771window_set_output_handler(struct window *window,
3772 window_output_handler_t handler)
3773{
3774 window->output_handler = handler;
3775}
3776
3777void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003778window_set_title(struct window *window, const char *title)
3779{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003780 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003781 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003782 if (window->shell_surface)
3783 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003784}
3785
3786const char *
3787window_get_title(struct window *window)
3788{
3789 return window->title;
3790}
3791
3792void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003793window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3794{
3795 struct text_cursor_position *text_cursor_position =
3796 window->display->text_cursor_position;
3797
Scott Moreau9295ce02012-06-01 12:46:10 -06003798 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003799 return;
3800
3801 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003802 window->main_surface->surface,
3803 wl_fixed_from_int(x),
3804 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003805}
3806
3807void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003808window_damage(struct window *window, int32_t x, int32_t y,
3809 int32_t width, int32_t height)
3810{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003811 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003812}
3813
Casey Dahlin9074db52012-04-19 22:50:09 -04003814static void
3815surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003816 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003817{
Rob Bradford7507b572012-05-15 17:55:34 +01003818 struct window *window = data;
3819 struct output *output;
3820 struct output *output_found = NULL;
3821 struct window_output *window_output;
3822
3823 wl_list_for_each(output, &window->display->output_list, link) {
3824 if (output->output == wl_output) {
3825 output_found = output;
3826 break;
3827 }
3828 }
3829
3830 if (!output_found)
3831 return;
3832
3833 window_output = malloc (sizeof *window_output);
3834 window_output->output = output_found;
3835
3836 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003837
3838 if (window->output_handler)
3839 window->output_handler(window, output_found, 1,
3840 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003841}
3842
3843static void
3844surface_leave(void *data,
3845 struct wl_surface *wl_surface, struct wl_output *output)
3846{
Rob Bradford7507b572012-05-15 17:55:34 +01003847 struct window *window = data;
3848 struct window_output *window_output;
3849 struct window_output *window_output_found = NULL;
3850
3851 wl_list_for_each(window_output, &window->window_output_list, link) {
3852 if (window_output->output->output == output) {
3853 window_output_found = window_output;
3854 break;
3855 }
3856 }
3857
3858 if (window_output_found) {
3859 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003860
3861 if (window->output_handler)
3862 window->output_handler(window, window_output->output,
3863 0, window->user_data);
3864
Rob Bradford7507b572012-05-15 17:55:34 +01003865 free(window_output_found);
3866 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003867}
3868
3869static const struct wl_surface_listener surface_listener = {
3870 surface_enter,
3871 surface_leave
3872};
3873
Pekka Paalanen4e373742013-02-13 16:17:13 +02003874static struct surface *
3875surface_create(struct window *window)
3876{
3877 struct display *display = window->display;
3878 struct surface *surface;
3879
3880 surface = calloc(1, sizeof *surface);
3881 if (!surface)
3882 return NULL;
3883
3884 surface->window = window;
3885 surface->surface = wl_compositor_create_surface(display->compositor);
3886 wl_surface_add_listener(surface->surface, &surface_listener, window);
3887
Pekka Paalanen35e82632013-04-25 13:57:48 +03003888 wl_list_insert(&window->subsurface_list, &surface->link);
3889
Pekka Paalanen4e373742013-02-13 16:17:13 +02003890 return surface;
3891}
3892
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003893static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003894window_create_internal(struct display *display,
3895 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003896{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003897 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003898 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003899
3900 window = malloc(sizeof *window);
3901 if (window == NULL)
3902 return NULL;
3903
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003904 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003905 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003906 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003907 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003908
3909 surface = surface_create(window);
3910 window->main_surface = surface;
3911
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003912 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003913 window->shell_surface =
3914 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003915 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003916 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003917
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003918 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003919 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00003920 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003921
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003922 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003923#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003924 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003925#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003926 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003927#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003928 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003929 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003930
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003931 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003932 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003933 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003934
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003935 if (window->shell_surface) {
3936 wl_shell_surface_set_user_data(window->shell_surface, window);
3937 wl_shell_surface_add_listener(window->shell_surface,
3938 &shell_surface_listener, window);
3939 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003940
Rob Bradford7507b572012-05-15 17:55:34 +01003941 wl_list_init (&window->window_output_list);
3942
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003943 return window;
3944}
3945
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003946struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003947window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003948{
3949 struct window *window;
3950
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003951 window = window_create_internal(display, NULL, TYPE_NONE);
3952 if (!window)
3953 return NULL;
3954
3955 return window;
3956}
3957
3958struct window *
3959window_create_custom(struct display *display)
3960{
3961 struct window *window;
3962
3963 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003964 if (!window)
3965 return NULL;
3966
3967 return window;
3968}
3969
3970struct window *
3971window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003972 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003973{
3974 struct window *window;
3975
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003976 window = window_create_internal(parent->display,
3977 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003978 if (!window)
3979 return NULL;
3980
3981 window->x = x;
3982 window->y = y;
3983
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003984 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003985 wl_shell_surface_set_transient(
3986 window->shell_surface,
3987 window->parent->main_surface->surface,
3988 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003989
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003990 return window;
3991}
3992
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003993static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003994menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003995{
3996 int next;
3997
3998 next = (sy - 8) / 20;
3999 if (menu->current != next) {
4000 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004001 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004002 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004003}
4004
4005static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004006menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004007 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004008 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004009{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004010 struct menu *menu = data;
4011
4012 if (widget == menu->widget)
4013 menu_set_item(data, y);
4014
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004015 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004016}
4017
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004018static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004019menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004020 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004021{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004022 struct menu *menu = data;
4023
4024 if (widget == menu->widget)
4025 menu_set_item(data, y);
4026
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004027 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004028}
4029
4030static void
4031menu_leave_handler(struct widget *widget, struct input *input, void *data)
4032{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004033 struct menu *menu = data;
4034
4035 if (widget == menu->widget)
4036 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004037}
4038
4039static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004040menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004041 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004042 uint32_t button, enum wl_pointer_button_state state,
4043 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004044
4045{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004046 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004047
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004048 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4049 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004050 /* Either relase after press-drag-release or
4051 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004052 menu->func(menu->window->parent,
4053 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004054 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004055 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004056 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004057 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004058 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004059}
4060
4061static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004062menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004063{
4064 cairo_t *cr;
4065 const int32_t r = 3, margin = 3;
4066 struct menu *menu = data;
4067 int32_t width, height, i;
4068
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004069 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004070 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4071 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4072 cairo_paint(cr);
4073
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004074 width = widget->allocation.width;
4075 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004076 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004077
4078 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004079 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4080 cairo_fill(cr);
4081
4082 for (i = 0; i < menu->count; i++) {
4083 if (i == menu->current) {
4084 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4085 cairo_rectangle(cr, margin, i * 20 + margin,
4086 width - 2 * margin, 20);
4087 cairo_fill(cr);
4088 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4089 cairo_move_to(cr, 10, i * 20 + 16);
4090 cairo_show_text(cr, menu->entries[i]);
4091 } else {
4092 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4093 cairo_move_to(cr, 10, i * 20 + 16);
4094 cairo_show_text(cr, menu->entries[i]);
4095 }
4096 }
4097
4098 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004099}
4100
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004101void
4102window_show_menu(struct display *display,
4103 struct input *input, uint32_t time, struct window *parent,
4104 int32_t x, int32_t y,
4105 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004106{
4107 struct window *window;
4108 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004109 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004110
4111 menu = malloc(sizeof *menu);
4112 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004113 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004114
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004115 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004116 if (!window) {
4117 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004118 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004119 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004120
4121 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004122 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004123 menu->entries = entries;
4124 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004125 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004126 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004127 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004128 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004129 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004130 window->type = TYPE_MENU;
4131 window->x = x;
4132 window->y = y;
4133
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004134 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004135 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004136 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004137 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004138 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004139
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004140 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004141 widget_set_enter_handler(menu->widget, menu_enter_handler);
4142 widget_set_leave_handler(menu->widget, menu_leave_handler);
4143 widget_set_motion_handler(menu->widget, menu_motion_handler);
4144 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004145
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004146 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004147 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004148}
4149
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004150void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004151window_set_buffer_type(struct window *window, enum window_buffer_type type)
4152{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004153 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004154}
4155
Pekka Paalanen35e82632013-04-25 13:57:48 +03004156struct widget *
4157window_add_subsurface(struct window *window, void *data,
4158 enum subsurface_mode default_mode)
4159{
4160 struct widget *widget;
4161 struct surface *surface;
4162 struct wl_surface *parent;
4163 struct wl_subcompositor *subcompo = window->display->subcompositor;
4164
4165 if (!subcompo)
4166 return NULL;
4167
4168 surface = surface_create(window);
4169 widget = widget_create(window, surface, data);
4170 wl_list_init(&widget->link);
4171 surface->widget = widget;
4172
4173 parent = window->main_surface->surface;
4174 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4175 surface->surface,
4176 parent);
4177 surface->synchronized = 1;
4178
4179 switch (default_mode) {
4180 case SUBSURFACE_SYNCHRONIZED:
4181 surface->synchronized_default = 1;
4182 break;
4183 case SUBSURFACE_DESYNCHRONIZED:
4184 surface->synchronized_default = 0;
4185 break;
4186 default:
4187 assert(!"bad enum subsurface_mode");
4188 }
4189
4190 return widget;
4191}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004192
4193static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004194display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004195 struct wl_output *wl_output,
4196 int x, int y,
4197 int physical_width,
4198 int physical_height,
4199 int subpixel,
4200 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004201 const char *model,
4202 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004203{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004204 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004205
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004206 output->allocation.x = x;
4207 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004208 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004209}
4210
4211static void
4212display_handle_mode(void *data,
4213 struct wl_output *wl_output,
4214 uint32_t flags,
4215 int width,
4216 int height,
4217 int refresh)
4218{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004219 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004220 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004221
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004222 if (flags & WL_OUTPUT_MODE_CURRENT) {
4223 output->allocation.width = width;
4224 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004225 if (display->output_configure_handler)
4226 (*display->output_configure_handler)(
4227 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004228 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004229}
4230
4231static const struct wl_output_listener output_listener = {
4232 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004233 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004234};
4235
4236static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004237display_add_output(struct display *d, uint32_t id)
4238{
4239 struct output *output;
4240
4241 output = malloc(sizeof *output);
4242 if (output == NULL)
4243 return;
4244
4245 memset(output, 0, sizeof *output);
4246 output->display = d;
4247 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004248 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004249 wl_list_insert(d->output_list.prev, &output->link);
4250
4251 wl_output_add_listener(output->output, &output_listener, output);
4252}
4253
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004254static void
4255output_destroy(struct output *output)
4256{
4257 if (output->destroy_handler)
4258 (*output->destroy_handler)(output, output->user_data);
4259
4260 wl_output_destroy(output->output);
4261 wl_list_remove(&output->link);
4262 free(output);
4263}
4264
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004265void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004266display_set_global_handler(struct display *display,
4267 display_global_handler_t handler)
4268{
4269 struct global *global;
4270
4271 display->global_handler = handler;
4272 if (!handler)
4273 return;
4274
4275 wl_list_for_each(global, &display->global_list, link)
4276 display->global_handler(display,
4277 global->name, global->interface,
4278 global->version, display->user_data);
4279}
4280
4281void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004282display_set_output_configure_handler(struct display *display,
4283 display_output_handler_t handler)
4284{
4285 struct output *output;
4286
4287 display->output_configure_handler = handler;
4288 if (!handler)
4289 return;
4290
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004291 wl_list_for_each(output, &display->output_list, link) {
4292 if (output->allocation.width == 0 &&
4293 output->allocation.height == 0)
4294 continue;
4295
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004296 (*display->output_configure_handler)(output,
4297 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004298 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004299}
4300
4301void
4302output_set_user_data(struct output *output, void *data)
4303{
4304 output->user_data = data;
4305}
4306
4307void *
4308output_get_user_data(struct output *output)
4309{
4310 return output->user_data;
4311}
4312
4313void
4314output_set_destroy_handler(struct output *output,
4315 display_output_handler_t handler)
4316{
4317 output->destroy_handler = handler;
4318 /* FIXME: implement this, once we have way to remove outputs */
4319}
4320
4321void
Scott Moreau4e072362012-09-29 02:03:11 -06004322output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004323{
Scott Moreau4e072362012-09-29 02:03:11 -06004324 struct rectangle allocation = output->allocation;
4325
4326 switch (output->transform) {
4327 case WL_OUTPUT_TRANSFORM_90:
4328 case WL_OUTPUT_TRANSFORM_270:
4329 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4330 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4331 /* Swap width and height */
4332 allocation.width = output->allocation.height;
4333 allocation.height = output->allocation.width;
4334 break;
4335 }
4336
4337 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004338}
4339
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004340struct wl_output *
4341output_get_wl_output(struct output *output)
4342{
4343 return output->output;
4344}
4345
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004346enum wl_output_transform
4347output_get_transform(struct output *output)
4348{
4349 return output->transform;
4350}
4351
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004352static void
Daniel Stone97f68542012-05-30 16:32:01 +01004353fini_xkb(struct input *input)
4354{
4355 xkb_state_unref(input->xkb.state);
4356 xkb_map_unref(input->xkb.keymap);
4357}
4358
4359static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004360display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004361{
4362 struct input *input;
4363
4364 input = malloc(sizeof *input);
4365 if (input == NULL)
4366 return;
4367
4368 memset(input, 0, sizeof *input);
4369 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004370 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004371 input->pointer_focus = NULL;
4372 input->keyboard_focus = NULL;
4373 wl_list_insert(d->input_list.prev, &input->link);
4374
Daniel Stone37816df2012-05-16 18:45:18 +01004375 wl_seat_add_listener(input->seat, &seat_listener, input);
4376 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004377
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004378 input->data_device =
4379 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004380 input->seat);
4381 wl_data_device_add_listener(input->data_device, &data_device_listener,
4382 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004383
4384 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004385
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004386 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4387 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004388 input->repeat_task.run = keyboard_repeat_func;
4389 display_watch_fd(d, input->repeat_timer_fd,
4390 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004391}
4392
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004393static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004394input_destroy(struct input *input)
4395{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004396 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004397 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004398
4399 if (input->drag_offer)
4400 data_offer_destroy(input->drag_offer);
4401
4402 if (input->selection_offer)
4403 data_offer_destroy(input->selection_offer);
4404
4405 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004406 fini_xkb(input);
4407
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004408 wl_surface_destroy(input->pointer_surface);
4409
Pekka Paalanene1207c72011-12-16 12:02:09 +02004410 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004411 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004412 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004413 free(input);
4414}
4415
4416static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004417init_workspace_manager(struct display *d, uint32_t id)
4418{
4419 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004420 wl_registry_bind(d->registry, id,
4421 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004422 if (d->workspace_manager != NULL)
4423 workspace_manager_add_listener(d->workspace_manager,
4424 &workspace_manager_listener,
4425 d);
4426}
4427
4428static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004429registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4430 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004431{
4432 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004433 struct global *global;
4434
4435 global = malloc(sizeof *global);
4436 global->name = id;
4437 global->interface = strdup(interface);
4438 global->version = version;
4439 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004440
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004441 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004442 d->compositor = wl_registry_bind(registry, id,
4443 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004444 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004445 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004446 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004447 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004448 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004449 d->shell = wl_registry_bind(registry,
4450 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004451 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004452 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004453 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4454 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004455 wl_registry_bind(registry, id,
4456 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004457 } else if (strcmp(interface, "text_cursor_position") == 0) {
4458 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004459 wl_registry_bind(registry, id,
4460 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004461 } else if (strcmp(interface, "workspace_manager") == 0) {
4462 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004463 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4464 d->subcompositor =
4465 wl_registry_bind(registry, id,
4466 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004467 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004468
4469 if (d->global_handler)
4470 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004471}
4472
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004473static void
4474registry_handle_global_remove(void *data, struct wl_registry *registry,
4475 uint32_t name)
4476{
4477 struct display *d = data;
4478 struct global *global;
4479 struct global *tmp;
4480
4481 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4482 if (global->name != name)
4483 continue;
4484
4485 /* XXX: Should destroy bound globals, and call
4486 * the counterpart of display::global_handler
4487 */
4488 wl_list_remove(&global->link);
4489 free(global->interface);
4490 free(global);
4491 }
4492}
4493
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004494void *
4495display_bind(struct display *display, uint32_t name,
4496 const struct wl_interface *interface, uint32_t version)
4497{
4498 return wl_registry_bind(display->registry, name, interface, version);
4499}
4500
4501static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004502 registry_handle_global,
4503 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004504};
4505
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004506#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004507static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004508init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004509{
4510 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004511 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004512
Rob Clark6396ed32012-03-11 19:48:41 -05004513#ifdef USE_CAIRO_GLESV2
4514# define GL_BIT EGL_OPENGL_ES2_BIT
4515#else
4516# define GL_BIT EGL_OPENGL_BIT
4517#endif
4518
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004519 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004520 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004521 EGL_RED_SIZE, 1,
4522 EGL_GREEN_SIZE, 1,
4523 EGL_BLUE_SIZE, 1,
4524 EGL_ALPHA_SIZE, 1,
4525 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004526 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004527 EGL_NONE
4528 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004529
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004530#ifdef USE_CAIRO_GLESV2
4531 static const EGLint context_attribs[] = {
4532 EGL_CONTEXT_CLIENT_VERSION, 2,
4533 EGL_NONE
4534 };
4535 EGLint api = EGL_OPENGL_ES_API;
4536#else
4537 EGLint *context_attribs = NULL;
4538 EGLint api = EGL_OPENGL_API;
4539#endif
4540
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004541 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004542 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004543 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004544 return -1;
4545 }
4546
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004547 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004548 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004549 return -1;
4550 }
4551
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004552 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4553 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004554 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004555 return -1;
4556 }
4557
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004558 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004559 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004560 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004561 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004562 return -1;
4563 }
4564
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004565 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004566 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004567 return -1;
4568 }
4569
Benjamin Franzke0c991632011-09-27 21:57:31 +02004570 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4571 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004572 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004573 return -1;
4574 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004575
4576 return 0;
4577}
4578
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004579static void
4580fini_egl(struct display *display)
4581{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004582 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004583
4584 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4585 EGL_NO_CONTEXT);
4586
4587 eglTerminate(display->dpy);
4588 eglReleaseThread();
4589}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004590#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004591
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004592static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004593init_dummy_surface(struct display *display)
4594{
4595 int len;
4596 void *data;
4597
4598 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4599 data = malloc(len);
4600 display->dummy_surface =
4601 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4602 1, 1, len);
4603 display->dummy_surface_data = data;
4604}
4605
4606static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004607handle_display_data(struct task *task, uint32_t events)
4608{
4609 struct display *display =
4610 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004611 struct epoll_event ep;
4612 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004613
4614 display->display_fd_events = events;
4615
4616 if (events & EPOLLERR || events & EPOLLHUP) {
4617 display_exit(display);
4618 return;
4619 }
4620
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004621 if (events & EPOLLIN) {
4622 ret = wl_display_dispatch(display->display);
4623 if (ret == -1) {
4624 display_exit(display);
4625 return;
4626 }
4627 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004628
4629 if (events & EPOLLOUT) {
4630 ret = wl_display_flush(display->display);
4631 if (ret == 0) {
4632 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4633 ep.data.ptr = &display->display_task;
4634 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4635 display->display_fd, &ep);
4636 } else if (ret == -1 && errno != EAGAIN) {
4637 display_exit(display);
4638 return;
4639 }
4640 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004641}
4642
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004643static void
4644log_handler(const char *format, va_list args)
4645{
4646 vfprintf(stderr, format, args);
4647}
4648
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004649struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004650display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004651{
4652 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004653
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004654 wl_log_set_handler_client(log_handler);
4655
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004656 d = malloc(sizeof *d);
4657 if (d == NULL)
4658 return NULL;
4659
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004660 memset(d, 0, sizeof *d);
4661
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004662 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004663 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004664 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004665 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004666 return NULL;
4667 }
4668
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004669 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004670 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004671 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004672 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4673 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004674
4675 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004676 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004677 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004678 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004679
Daniel Stone97f68542012-05-30 16:32:01 +01004680 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004681 if (d->xkb_context == NULL) {
4682 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004683 return NULL;
4684 }
4685
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004686 d->workspace = 0;
4687 d->workspace_count = 1;
4688
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004689 d->registry = wl_display_get_registry(d->display);
4690 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004691
4692 if (wl_display_dispatch(d->display) < 0) {
4693 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4694 return NULL;
4695 }
4696
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004697#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004698 if (init_egl(d) < 0)
4699 fprintf(stderr, "EGL does not seem to work, "
4700 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004701#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004702
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004703 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004704
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004705 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004706
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004707 wl_list_init(&d->window_list);
4708
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004709 init_dummy_surface(d);
4710
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004711 return d;
4712}
4713
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004714static void
4715display_destroy_outputs(struct display *display)
4716{
4717 struct output *tmp;
4718 struct output *output;
4719
4720 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4721 output_destroy(output);
4722}
4723
Pekka Paalanene1207c72011-12-16 12:02:09 +02004724static void
4725display_destroy_inputs(struct display *display)
4726{
4727 struct input *tmp;
4728 struct input *input;
4729
4730 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4731 input_destroy(input);
4732}
4733
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004734void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004735display_destroy(struct display *display)
4736{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004737 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004738 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4739 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004740
4741 if (!wl_list_empty(&display->deferred_list))
4742 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4743
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004744 cairo_surface_destroy(display->dummy_surface);
4745 free(display->dummy_surface_data);
4746
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004747 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004748 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004749
Daniel Stone97f68542012-05-30 16:32:01 +01004750 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004751
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004752 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004753 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004754
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004755#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004756 if (display->argb_device)
4757 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004758#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004759
Pekka Paalanen35e82632013-04-25 13:57:48 +03004760 if (display->subcompositor)
4761 wl_subcompositor_destroy(display->subcompositor);
4762
Pekka Paalanenc2052982011-12-16 11:41:32 +02004763 if (display->shell)
4764 wl_shell_destroy(display->shell);
4765
4766 if (display->shm)
4767 wl_shm_destroy(display->shm);
4768
4769 if (display->data_device_manager)
4770 wl_data_device_manager_destroy(display->data_device_manager);
4771
4772 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004773 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004774
4775 close(display->epoll_fd);
4776
U. Artie Eoff44874d92012-10-02 21:12:35 -07004777 if (!(display->display_fd_events & EPOLLERR) &&
4778 !(display->display_fd_events & EPOLLHUP))
4779 wl_display_flush(display->display);
4780
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004781 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004782 free(display);
4783}
4784
4785void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004786display_set_user_data(struct display *display, void *data)
4787{
4788 display->user_data = data;
4789}
4790
4791void *
4792display_get_user_data(struct display *display)
4793{
4794 return display->user_data;
4795}
4796
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004797struct wl_display *
4798display_get_display(struct display *display)
4799{
4800 return display->display;
4801}
4802
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004803struct output *
4804display_get_output(struct display *display)
4805{
4806 return container_of(display->output_list.next, struct output, link);
4807}
4808
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004809struct wl_compositor *
4810display_get_compositor(struct display *display)
4811{
4812 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004813}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004814
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004815uint32_t
4816display_get_serial(struct display *display)
4817{
4818 return display->serial;
4819}
4820
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004821EGLDisplay
4822display_get_egl_display(struct display *d)
4823{
4824 return d->dpy;
4825}
4826
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004827struct wl_data_source *
4828display_create_data_source(struct display *display)
4829{
4830 return wl_data_device_manager_create_data_source(display->data_device_manager);
4831}
4832
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004833EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004834display_get_argb_egl_config(struct display *d)
4835{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004836 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004837}
4838
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004839struct wl_shell *
4840display_get_shell(struct display *display)
4841{
4842 return display->shell;
4843}
4844
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004845int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004846display_acquire_window_surface(struct display *display,
4847 struct window *window,
4848 EGLContext ctx)
4849{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004850 struct surface *surface = window->main_surface;
4851
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004852 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004853 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004854
Pekka Paalanen6f41b072013-02-20 13:39:17 +02004855 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004856 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004857}
4858
4859void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004860display_release_window_surface(struct display *display,
4861 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004862{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004863 struct surface *surface = window->main_surface;
4864
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004865 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004866 return;
4867
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004868 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004869}
4870
4871void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004872display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004873{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004874 wl_list_insert(&display->deferred_list, &task->link);
4875}
4876
4877void
4878display_watch_fd(struct display *display,
4879 int fd, uint32_t events, struct task *task)
4880{
4881 struct epoll_event ep;
4882
4883 ep.events = events;
4884 ep.data.ptr = task;
4885 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4886}
4887
4888void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004889display_unwatch_fd(struct display *display, int fd)
4890{
4891 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4892}
4893
4894void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004895display_run(struct display *display)
4896{
4897 struct task *task;
4898 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004899 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004900
Pekka Paalanen826d7952011-12-15 10:14:07 +02004901 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004902 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004903 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004904 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004905 struct task, link);
4906 wl_list_remove(&task->link);
4907 task->run(task, 0);
4908 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004909
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004910 wl_display_dispatch_pending(display->display);
4911
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004912 if (!display->running)
4913 break;
4914
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004915 ret = wl_display_flush(display->display);
4916 if (ret < 0 && errno == EAGAIN) {
4917 ep[0].events =
4918 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4919 ep[0].data.ptr = &display->display_task;
4920
4921 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4922 display->display_fd, &ep[0]);
4923 } else if (ret < 0) {
4924 break;
4925 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004926
4927 count = epoll_wait(display->epoll_fd,
4928 ep, ARRAY_LENGTH(ep), -1);
4929 for (i = 0; i < count; i++) {
4930 task = ep[i].data.ptr;
4931 task->run(task, ep[i].events);
4932 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004933 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004934}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004935
4936void
4937display_exit(struct display *display)
4938{
4939 display->running = 0;
4940}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004941
4942void
4943keysym_modifiers_add(struct wl_array *modifiers_map,
4944 const char *name)
4945{
4946 size_t len = strlen(name) + 1;
4947 char *p;
4948
4949 p = wl_array_add(modifiers_map, len);
4950
4951 if (p == NULL)
4952 return;
4953
4954 strncpy(p, name, len);
4955}
4956
4957static xkb_mod_index_t
4958keysym_modifiers_get_index(struct wl_array *modifiers_map,
4959 const char *name)
4960{
4961 xkb_mod_index_t index = 0;
4962 char *p = modifiers_map->data;
4963
4964 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4965 if (strcmp(p, name) == 0)
4966 return index;
4967
4968 index++;
4969 p += strlen(p) + 1;
4970 }
4971
4972 return XKB_MOD_INVALID;
4973}
4974
4975xkb_mod_mask_t
4976keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4977 const char *name)
4978{
4979 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4980
4981 if (index == XKB_MOD_INVALID)
4982 return XKB_MOD_INVALID;
4983
4984 return 1 << index;
4985}