blob: 40c0ef41d3264de40a04bfab1cd165c59e4b59e8 [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>
Pekka Paalanen71233882013-04-25 13:57:53 +030031#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033#include <fcntl.h>
34#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040035#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020037#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050038#include <time.h>
39#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040040#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040041#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030042#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040043
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040069#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060070#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020071#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030072#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050073
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050074#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050075
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070076struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030077
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040078struct global {
79 uint32_t name;
80 char *interface;
81 uint32_t version;
82 struct wl_list link;
83};
84
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050085struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050086 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040087 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050088 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030089 struct wl_subcompositor *subcompositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040090 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040091 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040092 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060093 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020094 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040095 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050096 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020098 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040099 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400100
101 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700102 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400103 struct task display_task;
104
105 int epoll_fd;
106 struct wl_list deferred_list;
107
Pekka Paalanen826d7952011-12-15 10:14:07 +0200108 int running;
109
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400110 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400111 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500113 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400114
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400115 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400116
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300117 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300118 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400119
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400121 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133};
134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400136 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400138 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500139 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500141 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_CUSTOM
143};
Rob Bradford7507b572012-05-15 17:55:34 +0100144
145struct window_output {
146 struct output *output;
147 struct wl_list link;
148};
149
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150struct toysurface {
151 /*
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
155 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200156 * If flags has SURFACE_HINT_RESIZE set, the user is
157 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200158 * Returns the Cairo surface to draw to.
159 */
160 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200161 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200162
163 /*
164 * Post the surface to the server, returning the server allocation
165 * rectangle. The Cairo surface from prepare() must be destroyed
166 * after calling this.
167 */
168 void (*swap)(struct toysurface *base,
169 struct rectangle *server_allocation);
170
171 /*
172 * Make the toysurface current with the given EGL context.
173 * Returns 0 on success, and negative of failure.
174 */
175 int (*acquire)(struct toysurface *base, EGLContext ctx);
176
177 /*
178 * Release the toysurface from the EGL context, returning control
179 * to Cairo.
180 */
181 void (*release)(struct toysurface *base);
182
183 /*
184 * Destroy the toysurface, including the Cairo surface, any
185 * backing storage, and the Wayland protocol objects.
186 */
187 void (*destroy)(struct toysurface *base);
188};
189
Pekka Paalanen4e373742013-02-13 16:17:13 +0200190struct surface {
191 struct window *window;
192
193 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300194 struct wl_subsurface *subsurface;
195 int synchronized;
196 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200198 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300199 int redraw_needed;
200 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300201 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200202
203 struct rectangle allocation;
204 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200205
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200206 struct wl_region *input_region;
207 struct wl_region *opaque_region;
208
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209 enum window_buffer_type buffer_type;
210 enum wl_output_transform buffer_transform;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200211
212 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300213
214 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200215};
216
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500217struct window {
218 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500219 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100220 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500221 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200222 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400223 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500224 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500225 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400226 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400227 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300228 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400229 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400230 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100231 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400232 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400233 int focus_count;
234
Pekka Paalanen99436862012-11-19 17:15:59 +0200235 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200236 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000237 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400238
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500239 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500240 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400241 window_data_handler_t data_handler;
242 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500243 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400244 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200245 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400246
Pekka Paalanen4e373742013-02-13 16:17:13 +0200247 struct surface *main_surface;
248 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300249
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200250 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500251
Pekka Paalanen35e82632013-04-25 13:57:48 +0300252 /* struct surface::link, contains also main_surface */
253 struct wl_list subsurface_list;
254
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500255 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400256 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500257};
258
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500259struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500260 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200261 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300262 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500263 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400264 struct wl_list link;
265 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500266 widget_resize_handler_t resize_handler;
267 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500268 widget_enter_handler_t enter_handler;
269 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500270 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500271 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200272 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400273 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500274 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300275 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500276 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400277};
278
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400279struct input {
280 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100281 struct wl_seat *seat;
282 struct wl_pointer *pointer;
283 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400284 struct window *pointer_focus;
285 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300286 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300287 uint32_t cursor_anim_start;
288 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300289 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400290 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400291 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400292 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400293 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400294 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400295
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500296 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500297 struct widget *grab;
298 uint32_t grab_button;
299
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400300 struct wl_data_device *data_device;
301 struct data_offer *drag_offer;
302 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100303
304 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100305 struct xkb_keymap *keymap;
306 struct xkb_state *state;
307 xkb_mod_mask_t control_mask;
308 xkb_mod_mask_t alt_mask;
309 xkb_mod_mask_t shift_mask;
310 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400311
312 struct task repeat_task;
313 int repeat_timer_fd;
314 uint32_t repeat_sym;
315 uint32_t repeat_key;
316 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400317};
318
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500319struct output {
320 struct display *display;
321 struct wl_output *output;
322 struct rectangle allocation;
323 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600324 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200325
326 display_output_handler_t destroy_handler;
327 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500328};
329
Martin Minarik1998b152012-05-10 02:04:35 +0200330enum frame_button_action {
331 FRAME_BUTTON_NULL = 0,
332 FRAME_BUTTON_ICON = 1,
333 FRAME_BUTTON_CLOSE = 2,
334 FRAME_BUTTON_MINIMIZE = 3,
335 FRAME_BUTTON_MAXIMIZE = 4,
336};
337
338enum frame_button_pointer {
339 FRAME_BUTTON_DEFAULT = 0,
340 FRAME_BUTTON_OVER = 1,
341 FRAME_BUTTON_ACTIVE = 2,
342};
343
344enum frame_button_align {
345 FRAME_BUTTON_RIGHT = 0,
346 FRAME_BUTTON_LEFT = 1,
347};
348
349enum frame_button_decoration {
350 FRAME_BUTTON_NONE = 0,
351 FRAME_BUTTON_FANCY = 1,
352};
353
354struct frame_button {
355 struct widget *widget;
356 struct frame *frame;
357 cairo_surface_t *icon;
358 enum frame_button_action type;
359 enum frame_button_pointer state;
360 struct wl_list link; /* buttons_list */
361 enum frame_button_align align;
362 enum frame_button_decoration decoration;
363};
364
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500365struct frame {
366 struct widget *widget;
367 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200368 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500369};
370
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500371struct menu {
372 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500373 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500374 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500375 const char **entries;
376 uint32_t time;
377 int current;
378 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400379 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500380 menu_func_t func;
381};
382
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300383struct tooltip {
384 struct widget *parent;
385 struct window *window;
386 struct widget *widget;
387 char *entry;
388 struct task tooltip_task;
389 int tooltip_fd;
390 float x, y;
391};
392
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700393struct shm_pool {
394 struct wl_shm_pool *pool;
395 size_t size;
396 size_t used;
397 void *data;
398};
399
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400400enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300401 CURSOR_DEFAULT = 100,
402 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400403};
404
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500405enum window_location {
406 WINDOW_INTERIOR = 0,
407 WINDOW_RESIZING_TOP = 1,
408 WINDOW_RESIZING_BOTTOM = 2,
409 WINDOW_RESIZING_LEFT = 4,
410 WINDOW_RESIZING_TOP_LEFT = 5,
411 WINDOW_RESIZING_BOTTOM_LEFT = 6,
412 WINDOW_RESIZING_RIGHT = 8,
413 WINDOW_RESIZING_TOP_RIGHT = 9,
414 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
415 WINDOW_RESIZING_MASK = 15,
416 WINDOW_EXTERIOR = 16,
417 WINDOW_TITLEBAR = 17,
418 WINDOW_CLIENT_AREA = 18,
419};
420
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200421static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422
Pekka Paalanen71233882013-04-25 13:57:53 +0300423#if 0
424
425static void
426debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
427__attribute__ ((format (printf, 4, 5)));
428
429static void
430debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
431{
432 va_list ap;
433 struct timeval tv;
434
435 gettimeofday(&tv, NULL);
436 fprintf(stderr, "%8ld.%03ld ",
437 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
438
439 if (proxy)
440 fprintf(stderr, "%s@%d ",
441 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
442
443 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
444 fprintf(stderr, "%s ", func);
445
446 va_start(ap, fmt);
447 vfprintf(stderr, fmt, ap);
448 va_end(ap);
449}
450
451#define DBG(fmt, ...) \
452 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
453
454#define DBG_OBJ(obj, fmt, ...) \
455 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
456
457#else
458
459#define DBG(...) do {} while (0)
460#define DBG_OBJ(...) do {} while (0)
461
462#endif
463
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500464#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400465
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200466struct egl_window_surface {
467 struct toysurface base;
468 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100469 struct display *display;
470 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200471 struct wl_egl_window *egl_window;
472 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100473};
474
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200475static struct egl_window_surface *
476to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100477{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200478 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100479}
480
481static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200482egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200483 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100484{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200485 struct egl_window_surface *surface = to_egl_window_surface(base);
486
487 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
488 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
489
490 return cairo_surface_reference(surface->cairo_surface);
491}
492
493static void
494egl_window_surface_swap(struct toysurface *base,
495 struct rectangle *server_allocation)
496{
497 struct egl_window_surface *surface = to_egl_window_surface(base);
498
499 cairo_gl_surface_swapbuffers(surface->cairo_surface);
500 wl_egl_window_get_attached_size(surface->egl_window,
501 &server_allocation->width,
502 &server_allocation->height);
503}
504
505static int
506egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
507{
508 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200509 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100510
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511 device = cairo_surface_get_device(surface->cairo_surface);
512 if (!device)
513 return -1;
514
515 if (!ctx) {
516 if (device == surface->display->argb_device)
517 ctx = surface->display->argb_ctx;
518 else
519 assert(0);
520 }
521
522 cairo_device_flush(device);
523 cairo_device_acquire(device);
524 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
525 surface->egl_surface, ctx))
526 fprintf(stderr, "failed to make surface current\n");
527
528 return 0;
529}
530
531static void
532egl_window_surface_release(struct toysurface *base)
533{
534 struct egl_window_surface *surface = to_egl_window_surface(base);
535 cairo_device_t *device;
536
537 device = cairo_surface_get_device(surface->cairo_surface);
538 if (!device)
539 return;
540
541 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
542 surface->display->argb_ctx))
543 fprintf(stderr, "failed to make context current\n");
544
545 cairo_device_release(device);
546}
547
548static void
549egl_window_surface_destroy(struct toysurface *base)
550{
551 struct egl_window_surface *surface = to_egl_window_surface(base);
552 struct display *d = surface->display;
553
554 cairo_surface_destroy(surface->cairo_surface);
555 eglDestroySurface(d->dpy, surface->egl_surface);
556 wl_egl_window_destroy(surface->egl_window);
557 surface->surface = NULL;
558
559 free(surface);
560}
561
562static struct toysurface *
563egl_window_surface_create(struct display *display,
564 struct wl_surface *wl_surface,
565 uint32_t flags,
566 struct rectangle *rectangle)
567{
568 struct egl_window_surface *surface;
569
Pekka Paalanenb3627362012-11-19 17:16:00 +0200570 if (display->dpy == EGL_NO_DISPLAY)
571 return NULL;
572
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200573 surface = calloc(1, sizeof *surface);
574 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100575 return NULL;
576
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200577 surface->base.prepare = egl_window_surface_prepare;
578 surface->base.swap = egl_window_surface_swap;
579 surface->base.acquire = egl_window_surface_acquire;
580 surface->base.release = egl_window_surface_release;
581 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100582
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200583 surface->display = display;
584 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400585
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200586 surface->egl_window = wl_egl_window_create(surface->surface,
587 rectangle->width,
588 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100589
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200590 surface->egl_surface = eglCreateWindowSurface(display->dpy,
591 display->argb_config,
592 surface->egl_window,
593 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100594
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200595 surface->cairo_surface =
596 cairo_gl_surface_create_for_egl(display->argb_device,
597 surface->egl_surface,
598 rectangle->width,
599 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100600
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200601 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100602}
603
Pekka Paalanenb3627362012-11-19 17:16:00 +0200604#else
605
606static struct toysurface *
607egl_window_surface_create(struct display *display,
608 struct wl_surface *wl_surface,
609 uint32_t flags,
610 struct rectangle *rectangle)
611{
612 return NULL;
613}
614
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615#endif
616
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200617struct shm_surface_data {
618 struct wl_buffer *buffer;
619 struct shm_pool *pool;
620};
621
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622struct wl_buffer *
623display_get_buffer_for_surface(struct display *display,
624 cairo_surface_t *surface)
625{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200626 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200628 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629
630 return data->buffer;
631}
632
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500633static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700634shm_pool_destroy(struct shm_pool *pool);
635
636static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637shm_surface_data_destroy(void *p)
638{
639 struct shm_surface_data *data = p;
640
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200641 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700642 if (data->pool)
643 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300644
645 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400646}
647
Kristian Høgsberg16626282012-04-03 11:21:27 -0400648static struct wl_shm_pool *
649make_shm_pool(struct display *display, int size, void **data)
650{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400651 struct wl_shm_pool *pool;
652 int fd;
653
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300654 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400655 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300656 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
657 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400658 return NULL;
659 }
660
661 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400662 if (*data == MAP_FAILED) {
663 fprintf(stderr, "mmap failed: %m\n");
664 close(fd);
665 return NULL;
666 }
667
668 pool = wl_shm_create_pool(display->shm, fd, size);
669
670 close(fd);
671
672 return pool;
673}
674
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700675static struct shm_pool *
676shm_pool_create(struct display *display, size_t size)
677{
678 struct shm_pool *pool = malloc(sizeof *pool);
679
680 if (!pool)
681 return NULL;
682
683 pool->pool = make_shm_pool(display, size, &pool->data);
684 if (!pool->pool) {
685 free(pool);
686 return NULL;
687 }
688
689 pool->size = size;
690 pool->used = 0;
691
692 return pool;
693}
694
695static void *
696shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
697{
698 if (pool->used + size > pool->size)
699 return NULL;
700
701 *offset = pool->used;
702 pool->used += size;
703
704 return (char *) pool->data + *offset;
705}
706
707/* destroy the pool. this does not unmap the memory though */
708static void
709shm_pool_destroy(struct shm_pool *pool)
710{
711 munmap(pool->data, pool->size);
712 wl_shm_pool_destroy(pool->pool);
713 free(pool);
714}
715
716/* Start allocating from the beginning of the pool again */
717static void
718shm_pool_reset(struct shm_pool *pool)
719{
720 pool->used = 0;
721}
722
723static int
724data_length_for_shm_surface(struct rectangle *rect)
725{
726 int stride;
727
728 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
729 rect->width);
730 return stride * rect->height;
731}
732
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500733static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700734display_create_shm_surface_from_pool(struct display *display,
735 struct rectangle *rectangle,
736 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400737{
738 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400739 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400740 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700741 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400742 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400743
744 data = malloc(sizeof *data);
745 if (data == NULL)
746 return NULL;
747
748 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
749 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700750 length = stride * rectangle->height;
751 data->pool = NULL;
752 map = shm_pool_allocate(pool, length, &offset);
753
754 if (!map) {
755 free(data);
756 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400757 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400758
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400759 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400760 CAIRO_FORMAT_ARGB32,
761 rectangle->width,
762 rectangle->height,
763 stride);
764
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200765 cairo_surface_set_user_data(surface, &shm_surface_data_key,
766 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400767
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400768 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500769 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400770 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500771 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400772
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200773 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
774 rectangle->width,
775 rectangle->height,
776 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400777
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700778 return surface;
779}
780
781static cairo_surface_t *
782display_create_shm_surface(struct display *display,
783 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200784 struct shm_pool *alternate_pool,
785 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700786{
787 struct shm_surface_data *data;
788 struct shm_pool *pool;
789 cairo_surface_t *surface;
790
Pekka Paalanen99436862012-11-19 17:15:59 +0200791 if (alternate_pool) {
792 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700793 surface = display_create_shm_surface_from_pool(display,
794 rectangle,
795 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200796 alternate_pool);
797 if (surface) {
798 data = cairo_surface_get_user_data(surface,
799 &shm_surface_data_key);
800 goto out;
801 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700802 }
803
804 pool = shm_pool_create(display,
805 data_length_for_shm_surface(rectangle));
806 if (!pool)
807 return NULL;
808
809 surface =
810 display_create_shm_surface_from_pool(display, rectangle,
811 flags, pool);
812
813 if (!surface) {
814 shm_pool_destroy(pool);
815 return NULL;
816 }
817
818 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200819 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700820 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821
Pekka Paalanen99436862012-11-19 17:15:59 +0200822out:
823 if (data_ret)
824 *data_ret = data;
825
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826 return surface;
827}
828
nobled7b87cb02011-02-01 18:51:47 +0000829static int
830check_size(struct rectangle *rect)
831{
832 if (rect->width && rect->height)
833 return 0;
834
835 fprintf(stderr, "tried to create surface of "
836 "width: %d, height: %d\n", rect->width, rect->height);
837 return -1;
838}
839
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400840cairo_surface_t *
841display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100842 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400843 struct rectangle *rectangle,
844 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400845{
nobled7b87cb02011-02-01 18:51:47 +0000846 if (check_size(rectangle) < 0)
847 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200848
849 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200850 return display_create_shm_surface(display, rectangle, flags,
851 NULL, NULL);
852}
853
Pekka Paalanena4eda732012-11-19 17:16:02 +0200854struct shm_surface_leaf {
855 cairo_surface_t *cairo_surface;
856 /* 'data' is automatically destroyed, when 'cairo_surface' is */
857 struct shm_surface_data *data;
858
859 struct shm_pool *resize_pool;
860 int busy;
861};
862
863static void
864shm_surface_leaf_release(struct shm_surface_leaf *leaf)
865{
866 if (leaf->cairo_surface)
867 cairo_surface_destroy(leaf->cairo_surface);
868 /* leaf->data already destroyed via cairo private */
869
870 if (leaf->resize_pool)
871 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200872
873 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200874}
875
Pekka Paalanenaef02542013-04-25 13:57:47 +0300876#define MAX_LEAVES 3
877
Pekka Paalanen99436862012-11-19 17:15:59 +0200878struct shm_surface {
879 struct toysurface base;
880 struct display *display;
881 struct wl_surface *surface;
882 uint32_t flags;
883 int dx, dy;
884
Pekka Paalanenaef02542013-04-25 13:57:47 +0300885 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200886 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200887};
888
889static struct shm_surface *
890to_shm_surface(struct toysurface *base)
891{
892 return container_of(base, struct shm_surface, base);
893}
894
Pekka Paalanena4eda732012-11-19 17:16:02 +0200895static void
896shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
897{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200898 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300899 struct shm_surface_leaf *leaf;
900 int i;
901 int free_found;
Pekka Paalanen71233882013-04-25 13:57:53 +0300902 int available = MAX_LEAVES;
903 char bufs[MAX_LEAVES + 1];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200904
Pekka Paalanenaef02542013-04-25 13:57:47 +0300905 for (i = 0; i < MAX_LEAVES; i++) {
906 leaf = &surface->leaf[i];
907 if (leaf->data && leaf->data->buffer == buffer) {
908 leaf->busy = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +0300909 available = i;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300910 break;
911 }
912 }
913 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200914
Pekka Paalanenaef02542013-04-25 13:57:47 +0300915 /* Leave one free leaf with storage, release others */
916 free_found = 0;
917 for (i = 0; i < MAX_LEAVES; i++) {
918 leaf = &surface->leaf[i];
919
Pekka Paalanen71233882013-04-25 13:57:53 +0300920 if (leaf->busy)
921 bufs[i] = 'b';
922 else if (leaf->cairo_surface)
923 bufs[i] = 'a';
924 else
925 bufs[i] = ' ';
926
Pekka Paalanenaef02542013-04-25 13:57:47 +0300927 if (!leaf->cairo_surface || leaf->busy)
928 continue;
929
930 if (!free_found)
931 free_found = 1;
Pekka Paalanen71233882013-04-25 13:57:53 +0300932 else {
Pekka Paalanenaef02542013-04-25 13:57:47 +0300933 shm_surface_leaf_release(leaf);
Pekka Paalanen71233882013-04-25 13:57:53 +0300934 bufs[i] = '*';
935 }
Pekka Paalanenaef02542013-04-25 13:57:47 +0300936 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300937
938 bufs[MAX_LEAVES] = '\0';
939 DBG_OBJ(surface->surface, "leaf %d released, leaves [%s]\n",
940 available, bufs);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200941}
942
943static const struct wl_buffer_listener shm_surface_buffer_listener = {
944 shm_surface_buffer_release
945};
946
Pekka Paalanen99436862012-11-19 17:15:59 +0200947static cairo_surface_t *
948shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200949 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200950{
Pekka Paalanenec076692012-11-30 13:37:27 +0200951 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200952 struct shm_surface *surface = to_shm_surface(base);
953 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanenaef02542013-04-25 13:57:47 +0300954 struct shm_surface_leaf *leaf = NULL;
955 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200956
957 surface->dx = dx;
958 surface->dy = dy;
959
Pekka Paalanenaef02542013-04-25 13:57:47 +0300960 /* pick a free buffer, preferrably one that already has storage */
961 for (i = 0; i < MAX_LEAVES; i++) {
962 if (surface->leaf[i].busy)
963 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200964
Pekka Paalanenaef02542013-04-25 13:57:47 +0300965 if (!leaf || surface->leaf[i].cairo_surface)
966 leaf = &surface->leaf[i];
967 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300968 DBG_OBJ(surface->surface, "pick leaf %d\n",
969 (int)(leaf - &surface->leaf[0]));
970
Pekka Paalanenaef02542013-04-25 13:57:47 +0300971 if (!leaf) {
972 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +0200973 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +0300974 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200975 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200976 }
977
Pekka Paalanena4eda732012-11-19 17:16:02 +0200978 if (!resize_hint && leaf->resize_pool) {
979 cairo_surface_destroy(leaf->cairo_surface);
980 leaf->cairo_surface = NULL;
981 shm_pool_destroy(leaf->resize_pool);
982 leaf->resize_pool = NULL;
983 }
984
985 if (leaf->cairo_surface &&
986 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
987 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200988 goto out;
989
Pekka Paalanena4eda732012-11-19 17:16:02 +0200990 if (leaf->cairo_surface)
991 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200992
Pekka Paalanena4eda732012-11-19 17:16:02 +0200993 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200994 /* Create a big pool to allocate from, while continuously
995 * resizing. Mmapping a new pool in the server
996 * is relatively expensive, so reusing a pool performs
997 * better, but may temporarily reserve unneeded memory.
998 */
999 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001000 leaf->resize_pool = shm_pool_create(surface->display,
1001 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001002 }
1003
Pekka Paalanena4eda732012-11-19 17:16:02 +02001004 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001005 display_create_shm_surface(surface->display, &rect,
1006 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001007 leaf->resize_pool,
1008 &leaf->data);
1009 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001010 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001011
1012out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001013 surface->current = leaf;
1014
1015 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001016}
1017
1018static void
1019shm_surface_swap(struct toysurface *base,
1020 struct rectangle *server_allocation)
1021{
1022 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001023 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001024
1025 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001026 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001027 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001028 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001029
Pekka Paalanena4eda732012-11-19 17:16:02 +02001030 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001031 surface->dx, surface->dy);
1032 wl_surface_damage(surface->surface, 0, 0,
1033 server_allocation->width, server_allocation->height);
1034 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001035
Pekka Paalanen71233882013-04-25 13:57:53 +03001036 DBG_OBJ(surface->surface, "leaf %d busy\n",
1037 (int)(leaf - &surface->leaf[0]));
1038
Pekka Paalanena4eda732012-11-19 17:16:02 +02001039 leaf->busy = 1;
1040 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001041}
1042
1043static int
1044shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1045{
1046 return -1;
1047}
1048
1049static void
1050shm_surface_release(struct toysurface *base)
1051{
1052}
1053
1054static void
1055shm_surface_destroy(struct toysurface *base)
1056{
1057 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001058 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001059
Pekka Paalanenaef02542013-04-25 13:57:47 +03001060 for (i = 0; i < MAX_LEAVES; i++)
1061 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001062
1063 free(surface);
1064}
1065
1066static struct toysurface *
1067shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1068 uint32_t flags, struct rectangle *rectangle)
1069{
1070 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001071 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001072
1073 surface = calloc(1, sizeof *surface);
1074 if (!surface)
1075 return NULL;
1076
1077 surface->base.prepare = shm_surface_prepare;
1078 surface->base.swap = shm_surface_swap;
1079 surface->base.acquire = shm_surface_acquire;
1080 surface->base.release = shm_surface_release;
1081 surface->base.destroy = shm_surface_destroy;
1082
1083 surface->display = display;
1084 surface->surface = wl_surface;
1085 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001086
1087 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001088}
1089
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001090/*
1091 * The following correspondences between file names and cursors was copied
1092 * from: https://bugs.kde.org/attachment.cgi?id=67313
1093 */
1094
1095static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001096 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001097 "sw-resize",
1098 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001099};
1100
1101static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001102 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001103 "se-resize",
1104 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001105};
1106
1107static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001108 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001109 "s-resize",
1110 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001111};
1112
1113static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001114 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001115 "closedhand",
1116 "208530c400c041818281048008011002"
1117};
1118
1119static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001120 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001121 "default",
1122 "top_left_arrow",
1123 "left-arrow"
1124};
1125
1126static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001127 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001128 "w-resize",
1129 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001130};
1131
1132static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001133 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001134 "e-resize",
1135 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001136};
1137
1138static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001139 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001140 "nw-resize",
1141 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001142};
1143
1144static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001145 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001146 "ne-resize",
1147 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001148};
1149
1150static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001151 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001152 "n-resize",
1153 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001154};
1155
1156static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001157 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001158 "ibeam",
1159 "text"
1160};
1161
1162static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001163 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001164 "pointer",
1165 "pointing_hand",
1166 "e29285e634086352946a0e7090d73106"
1167};
1168
1169static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001170 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001171 "wait",
1172 "0426c94ea35c87780ff01dc239897213"
1173};
1174
1175struct cursor_alternatives {
1176 const char **names;
1177 size_t count;
1178};
1179
1180static const struct cursor_alternatives cursors[] = {
1181 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1182 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1183 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1184 {grabbings, ARRAY_LENGTH(grabbings)},
1185 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1186 {left_sides, ARRAY_LENGTH(left_sides)},
1187 {right_sides, ARRAY_LENGTH(right_sides)},
1188 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1189 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1190 {top_sides, ARRAY_LENGTH(top_sides)},
1191 {xterms, ARRAY_LENGTH(xterms)},
1192 {hand1s, ARRAY_LENGTH(hand1s)},
1193 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001194};
1195
1196static void
1197create_cursors(struct display *display)
1198{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001199 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001200 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001201 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001202 unsigned int i, j;
1203 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001204 struct config_key shell_keys[] = {
1205 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001206 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001207 };
1208 struct config_section cs[] = {
1209 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1210 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001211
Ossama Othmana50e6e42013-05-14 09:48:26 -07001212 config_fd = open_config_file("weston.ini");
1213 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1214 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001215
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001216 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001217 display->cursors =
1218 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1219
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001220 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001221 cursor = NULL;
1222 for (j = 0; !cursor && j < cursors[i].count; ++j)
1223 cursor = wl_cursor_theme_get_cursor(
1224 display->cursor_theme, cursors[i].names[j]);
1225
1226 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001227 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001228 cursors[i].names[0]);
1229
1230 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001231 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232}
1233
1234static void
1235destroy_cursors(struct display *display)
1236{
1237 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001238 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239}
1240
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001241struct wl_cursor_image *
1242display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001243{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001244 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001245
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001246 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001247}
1248
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001249static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001250surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001251{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001252 if (!surface->cairo_surface)
1253 return;
1254
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001255 if (surface->opaque_region) {
1256 wl_surface_set_opaque_region(surface->surface,
1257 surface->opaque_region);
1258 wl_region_destroy(surface->opaque_region);
1259 surface->opaque_region = NULL;
1260 }
1261
1262 if (surface->input_region) {
1263 wl_surface_set_input_region(surface->surface,
1264 surface->input_region);
1265 wl_region_destroy(surface->input_region);
1266 surface->input_region = NULL;
1267 }
1268
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001269 surface->toysurface->swap(surface->toysurface,
1270 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001271
Pekka Paalanen89dee002013-02-13 16:17:20 +02001272 cairo_surface_destroy(surface->cairo_surface);
1273 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001274}
1275
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001276int
1277window_has_focus(struct window *window)
1278{
1279 return window->focus_count > 0;
1280}
1281
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001282static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001283window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001284{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001285 struct surface *surface;
1286
Pekka Paalanen89dee002013-02-13 16:17:20 +02001287 if (window->type == TYPE_NONE) {
1288 window->type = TYPE_TOPLEVEL;
1289 if (window->shell_surface)
1290 wl_shell_surface_set_toplevel(window->shell_surface);
1291 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001292
Pekka Paalanen35e82632013-04-25 13:57:48 +03001293 wl_list_for_each(surface, &window->subsurface_list, link) {
1294 if (surface == window->main_surface)
1295 continue;
1296
1297 surface_flush(surface);
1298 }
1299
Pekka Paalanen89dee002013-02-13 16:17:20 +02001300 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001301}
1302
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001303struct display *
1304window_get_display(struct window *window)
1305{
1306 return window->display;
1307}
1308
Pekka Paalanen89dee002013-02-13 16:17:20 +02001309static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001310surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001311{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001312 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001313 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001314
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001315 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001316 case WL_OUTPUT_TRANSFORM_90:
1317 case WL_OUTPUT_TRANSFORM_270:
1318 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1319 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001320 allocation.width = surface->allocation.height;
1321 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001322 break;
1323 default:
1324 break;
1325 }
1326
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001327 if (!surface->toysurface && display->dpy &&
1328 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001329 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001330 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001331 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001332 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001333 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001334 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001335
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001336 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001337 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001338 surface->surface,
1339 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001340
Pekka Paalanen89dee002013-02-13 16:17:20 +02001341 surface->cairo_surface = surface->toysurface->prepare(
1342 surface->toysurface, dx, dy,
1343 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001344}
1345
1346static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001347window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001348{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001349 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001350 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001351 int dx = 0;
1352 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001353
Pekka Paalanenec076692012-11-30 13:37:27 +02001354 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001355 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001356
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001357 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1358 dx = surface->server_allocation.width -
1359 surface->allocation.width;
1360
1361 if (window->resize_edges & WINDOW_RESIZING_TOP)
1362 dy = surface->server_allocation.height -
1363 surface->allocation.height;
1364
1365 window->resize_edges = 0;
1366
Pekka Paalanen89dee002013-02-13 16:17:20 +02001367 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001368}
1369
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001370int
1371window_get_buffer_transform(struct window *window)
1372{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001373 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001374}
1375
1376void
1377window_set_buffer_transform(struct window *window,
1378 enum wl_output_transform transform)
1379{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001380 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001381 wl_surface_set_buffer_transform(window->main_surface->surface,
1382 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001383}
1384
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001385static void frame_destroy(struct frame *frame);
1386
Pekka Paalanen4e373742013-02-13 16:17:13 +02001387static void
1388surface_destroy(struct surface *surface)
1389{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001390 if (surface->frame_cb)
1391 wl_callback_destroy(surface->frame_cb);
1392
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001393 if (surface->input_region)
1394 wl_region_destroy(surface->input_region);
1395
1396 if (surface->opaque_region)
1397 wl_region_destroy(surface->opaque_region);
1398
Pekka Paalanen35e82632013-04-25 13:57:48 +03001399 if (surface->subsurface)
1400 wl_subsurface_destroy(surface->subsurface);
1401
Pekka Paalanen4e373742013-02-13 16:17:13 +02001402 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001403
1404 if (surface->toysurface)
1405 surface->toysurface->destroy(surface->toysurface);
1406
Pekka Paalanen35e82632013-04-25 13:57:48 +03001407 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001408 free(surface);
1409}
1410
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001411void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001412window_destroy(struct window *window)
1413{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001414 struct display *display = window->display;
1415 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001416 struct window_output *window_output;
1417 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001418
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001419 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001420
1421 wl_list_for_each(input, &display->input_list, link) {
1422 if (input->pointer_focus == window)
1423 input->pointer_focus = NULL;
1424 if (input->keyboard_focus == window)
1425 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001426 if (input->focus_widget &&
1427 input->focus_widget->window == window)
1428 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001429 }
1430
Rob Bradford7507b572012-05-15 17:55:34 +01001431 wl_list_for_each_safe(window_output, window_output_tmp,
1432 &window->window_output_list, link) {
1433 free (window_output);
1434 }
1435
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001436 if (window->frame)
1437 frame_destroy(window->frame);
1438
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001439 if (window->shell_surface)
1440 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001441
1442 surface_destroy(window->main_surface);
1443
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001444 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001445
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001446 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001447 free(window);
1448}
1449
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001450static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001451widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001452{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001453 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001454
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001455 wl_list_for_each(child, &widget->child_list, link) {
1456 target = widget_find_widget(child, x, y);
1457 if (target)
1458 return target;
1459 }
1460
1461 if (widget->allocation.x <= x &&
1462 x < widget->allocation.x + widget->allocation.width &&
1463 widget->allocation.y <= y &&
1464 y < widget->allocation.y + widget->allocation.height) {
1465 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001466 }
1467
1468 return NULL;
1469}
1470
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001471static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001472window_find_widget(struct window *window, int32_t x, int32_t y)
1473{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001474 struct surface *surface;
1475 struct widget *widget;
1476
1477 wl_list_for_each(surface, &window->subsurface_list, link) {
1478 widget = widget_find_widget(surface->widget, x, y);
1479 if (widget)
1480 return widget;
1481 }
1482
1483 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001484}
1485
1486static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001487widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001488{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001489 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001490
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001491 widget = malloc(sizeof *widget);
1492 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001493 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001494 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001495 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001496 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001497 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001498 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001499 widget->tooltip = NULL;
1500 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001501 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001502
1503 return widget;
1504}
1505
1506struct widget *
1507window_add_widget(struct window *window, void *data)
1508{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001509 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001510
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001511 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001512 wl_list_init(&widget->link);
1513 window->main_surface->widget = widget;
1514
1515 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001516}
1517
1518struct widget *
1519widget_add_widget(struct widget *parent, void *data)
1520{
1521 struct widget *widget;
1522
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001523 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001524 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001525
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001526 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001527}
1528
1529void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001530widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001531{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001532 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001533 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001534 struct input *input;
1535
Pekka Paalanen35e82632013-04-25 13:57:48 +03001536 /* Destroy the sub-surface along with the root widget */
1537 if (surface->widget == widget && surface->subsurface)
1538 surface_destroy(widget->surface);
1539
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001540 if (widget->tooltip) {
1541 free(widget->tooltip);
1542 widget->tooltip = NULL;
1543 }
1544
Pekka Paalanene156fb62012-01-19 13:51:38 +02001545 wl_list_for_each(input, &display->input_list, link) {
1546 if (input->focus_widget == widget)
1547 input->focus_widget = NULL;
1548 }
1549
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001550 wl_list_remove(&widget->link);
1551 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001552}
1553
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001554void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001555widget_set_default_cursor(struct widget *widget, int cursor)
1556{
1557 widget->default_cursor = cursor;
1558}
1559
1560void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001561widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001562{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001563 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001564}
1565
1566void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001567widget_set_size(struct widget *widget, int32_t width, int32_t height)
1568{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001569 widget->allocation.width = width;
1570 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001571}
1572
1573void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001574widget_set_allocation(struct widget *widget,
1575 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001576{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001577 widget->allocation.x = x;
1578 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001579 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001580}
1581
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001582void
1583widget_set_transparent(struct widget *widget, int transparent)
1584{
1585 widget->opaque = !transparent;
1586}
1587
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001588void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001589widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001590{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001591 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001592}
1593
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001594static cairo_surface_t *
1595widget_get_cairo_surface(struct widget *widget)
1596{
1597 struct surface *surface = widget->surface;
1598 struct window *window = widget->window;
1599
1600 if (!surface->cairo_surface) {
1601 if (surface == window->main_surface)
1602 window_create_main_surface(window);
1603 else
1604 surface_create_surface(surface, 0, 0, 0);
1605 }
1606
1607 return surface->cairo_surface;
1608}
1609
1610cairo_t *
1611widget_cairo_create(struct widget *widget)
1612{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001613 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001614 cairo_surface_t *cairo_surface;
1615 cairo_t *cr;
1616
1617 cairo_surface = widget_get_cairo_surface(widget);
1618 cr = cairo_create(cairo_surface);
1619
Pekka Paalanen35e82632013-04-25 13:57:48 +03001620 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1621
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001622 return cr;
1623}
1624
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001625struct wl_surface *
1626widget_get_wl_surface(struct widget *widget)
1627{
1628 return widget->surface->surface;
1629}
1630
1631uint32_t
1632widget_get_last_time(struct widget *widget)
1633{
1634 return widget->surface->last_time;
1635}
1636
1637void
1638widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1639{
1640 struct wl_compositor *comp = widget->window->display->compositor;
1641 struct surface *surface = widget->surface;
1642
1643 if (!surface->input_region)
1644 surface->input_region = wl_compositor_create_region(comp);
1645
1646 if (rect) {
1647 wl_region_add(surface->input_region,
1648 rect->x, rect->y, rect->width, rect->height);
1649 }
1650}
1651
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001652void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001653widget_set_resize_handler(struct widget *widget,
1654 widget_resize_handler_t handler)
1655{
1656 widget->resize_handler = handler;
1657}
1658
1659void
1660widget_set_redraw_handler(struct widget *widget,
1661 widget_redraw_handler_t handler)
1662{
1663 widget->redraw_handler = handler;
1664}
1665
1666void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001667widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001668{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001669 widget->enter_handler = handler;
1670}
1671
1672void
1673widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1674{
1675 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001676}
1677
1678void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001679widget_set_motion_handler(struct widget *widget,
1680 widget_motion_handler_t handler)
1681{
1682 widget->motion_handler = handler;
1683}
1684
1685void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001686widget_set_button_handler(struct widget *widget,
1687 widget_button_handler_t handler)
1688{
1689 widget->button_handler = handler;
1690}
1691
1692void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001693widget_set_axis_handler(struct widget *widget,
1694 widget_axis_handler_t handler)
1695{
1696 widget->axis_handler = handler;
1697}
1698
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001699static void
1700window_schedule_redraw_task(struct window *window);
1701
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001702void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001703widget_schedule_redraw(struct widget *widget)
1704{
Pekka Paalanen71233882013-04-25 13:57:53 +03001705 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001706 widget->surface->redraw_needed = 1;
1707 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001708}
1709
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001710cairo_surface_t *
1711window_get_surface(struct window *window)
1712{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001713 cairo_surface_t *cairo_surface;
1714
1715 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1716
1717 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001718}
1719
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001720struct wl_surface *
1721window_get_wl_surface(struct window *window)
1722{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001723 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001724}
1725
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001726struct wl_shell_surface *
1727window_get_wl_shell_surface(struct window *window)
1728{
1729 return window->shell_surface;
1730}
1731
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001732static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001733tooltip_redraw_handler(struct widget *widget, void *data)
1734{
1735 cairo_t *cr;
1736 const int32_t r = 3;
1737 struct tooltip *tooltip = data;
1738 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001739
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001740 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001741 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1742 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1743 cairo_paint(cr);
1744
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001745 width = widget->allocation.width;
1746 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001747 rounded_rect(cr, 0, 0, width, height, r);
1748
1749 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1750 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1751 cairo_fill(cr);
1752
1753 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1754 cairo_move_to(cr, 10, 16);
1755 cairo_show_text(cr, tooltip->entry);
1756 cairo_destroy(cr);
1757}
1758
1759static cairo_text_extents_t
1760get_text_extents(struct tooltip *tooltip)
1761{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001762 cairo_t *cr;
1763 cairo_text_extents_t extents;
1764
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001765 /* Use the dummy_surface because tooltip's surface was not
1766 * created yet, and parent does not have a valid surface
1767 * outside repaint, either.
1768 */
1769 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001770 cairo_text_extents(cr, tooltip->entry, &extents);
1771 cairo_destroy(cr);
1772
1773 return extents;
1774}
1775
1776static int
1777window_create_tooltip(struct tooltip *tooltip)
1778{
1779 struct widget *parent = tooltip->parent;
1780 struct display *display = parent->window->display;
1781 struct window *window;
1782 const int offset_y = 27;
1783 const int margin = 3;
1784 cairo_text_extents_t extents;
1785
1786 if (tooltip->widget)
1787 return 0;
1788
1789 window = window_create_transient(display, parent->window, tooltip->x,
1790 tooltip->y + offset_y,
1791 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1792 if (!window)
1793 return -1;
1794
1795 tooltip->window = window;
1796 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1797
1798 extents = get_text_extents(tooltip);
1799 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1800 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1801
1802 return 0;
1803}
1804
1805void
1806widget_destroy_tooltip(struct widget *parent)
1807{
1808 struct tooltip *tooltip = parent->tooltip;
1809
1810 parent->tooltip_count = 0;
1811 if (!tooltip)
1812 return;
1813
1814 if (tooltip->widget) {
1815 widget_destroy(tooltip->widget);
1816 window_destroy(tooltip->window);
1817 tooltip->widget = NULL;
1818 tooltip->window = NULL;
1819 }
1820
1821 close(tooltip->tooltip_fd);
1822 free(tooltip->entry);
1823 free(tooltip);
1824 parent->tooltip = NULL;
1825}
1826
1827static void
1828tooltip_func(struct task *task, uint32_t events)
1829{
1830 struct tooltip *tooltip =
1831 container_of(task, struct tooltip, tooltip_task);
1832 uint64_t exp;
1833
Martin Olsson8df662a2012-07-08 03:03:47 +02001834 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1835 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001836 window_create_tooltip(tooltip);
1837}
1838
1839#define TOOLTIP_TIMEOUT 500
1840static int
1841tooltip_timer_reset(struct tooltip *tooltip)
1842{
1843 struct itimerspec its;
1844
1845 its.it_interval.tv_sec = 0;
1846 its.it_interval.tv_nsec = 0;
1847 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1848 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1849 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1850 fprintf(stderr, "could not set timerfd\n: %m");
1851 return -1;
1852 }
1853
1854 return 0;
1855}
1856
1857int
1858widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1859{
1860 struct tooltip *tooltip = parent->tooltip;
1861
1862 parent->tooltip_count++;
1863 if (tooltip) {
1864 tooltip->x = x;
1865 tooltip->y = y;
1866 tooltip_timer_reset(tooltip);
1867 return 0;
1868 }
1869
1870 /* the handler might be triggered too fast via input device motion, so
1871 * we need this check here to make sure tooltip is fully initialized */
1872 if (parent->tooltip_count > 1)
1873 return 0;
1874
1875 tooltip = malloc(sizeof *tooltip);
1876 if (!tooltip)
1877 return -1;
1878
1879 parent->tooltip = tooltip;
1880 tooltip->parent = parent;
1881 tooltip->widget = NULL;
1882 tooltip->window = NULL;
1883 tooltip->x = x;
1884 tooltip->y = y;
1885 tooltip->entry = strdup(entry);
1886 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1887 if (tooltip->tooltip_fd < 0) {
1888 fprintf(stderr, "could not create timerfd\n: %m");
1889 return -1;
1890 }
1891
1892 tooltip->tooltip_task.run = tooltip_func;
1893 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1894 EPOLLIN, &tooltip->tooltip_task);
1895 tooltip_timer_reset(tooltip);
1896
1897 return 0;
1898}
1899
1900static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001901workspace_manager_state(void *data,
1902 struct workspace_manager *workspace_manager,
1903 uint32_t current,
1904 uint32_t count)
1905{
1906 struct display *display = data;
1907
1908 display->workspace = current;
1909 display->workspace_count = count;
1910}
1911
1912static const struct workspace_manager_listener workspace_manager_listener = {
1913 workspace_manager_state
1914};
1915
1916static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001917frame_resize_handler(struct widget *widget,
1918 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001919{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001920 struct frame *frame = data;
1921 struct widget *child = frame->child;
1922 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001923 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001924 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001925 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001926 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001927 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001928 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001929 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001930
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001931 switch (widget->window->type) {
1932 case TYPE_FULLSCREEN:
1933 decoration_width = 0;
1934 decoration_height = 0;
1935
1936 allocation.x = 0;
1937 allocation.y = 0;
1938 allocation.width = width;
1939 allocation.height = height;
1940 opaque_margin = 0;
1941
1942 wl_list_for_each(button, &frame->buttons_list, link)
1943 button->widget->opaque = 1;
1944 break;
1945 case TYPE_MAXIMIZED:
1946 decoration_width = t->width * 2;
1947 decoration_height = t->width + t->titlebar_height;
1948
1949 allocation.x = t->width;
1950 allocation.y = t->titlebar_height;
1951 allocation.width = width - decoration_width;
1952 allocation.height = height - decoration_height;
1953
1954 opaque_margin = 0;
1955
1956 wl_list_for_each(button, &frame->buttons_list, link)
1957 button->widget->opaque = 0;
1958 break;
1959 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001960 decoration_width = (t->width + t->margin) * 2;
1961 decoration_height = t->width +
1962 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001963
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001964 allocation.x = t->width + t->margin;
1965 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001966 allocation.width = width - decoration_width;
1967 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001968
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001969 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001970
1971 wl_list_for_each(button, &frame->buttons_list, link)
1972 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001973 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001974 }
1975
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001976 widget_set_allocation(child, allocation.x, allocation.y,
1977 allocation.width, allocation.height);
1978
1979 if (child->resize_handler)
1980 child->resize_handler(child,
1981 allocation.width,
1982 allocation.height,
1983 child->user_data);
1984
Scott Moreauf7e498c2012-05-14 11:39:29 -06001985 width = child->allocation.width + decoration_width;
1986 height = child->allocation.height + decoration_height;
1987
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001988 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1989
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001990 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001991 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001992 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001993 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001994 shadow_margin, shadow_margin,
1995 width - 2 * shadow_margin,
1996 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001997 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001998 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001999 }
2000
Scott Moreauf7e498c2012-05-14 11:39:29 -06002001 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002002
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002003 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002004 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002005 opaque_margin, opaque_margin,
2006 widget->allocation.width - 2 * opaque_margin,
2007 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002008
2009 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002010 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2011 x_l = t->width + shadow_margin;
2012 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002013 wl_list_for_each(button, &frame->buttons_list, link) {
2014 const int button_padding = 4;
2015 w = cairo_image_surface_get_width(button->icon);
2016 h = cairo_image_surface_get_height(button->icon);
2017
2018 if (button->decoration == FRAME_BUTTON_FANCY)
2019 w += 10;
2020
2021 if (button->align == FRAME_BUTTON_LEFT) {
2022 widget_set_allocation(button->widget,
2023 x_l, y , w + 1, h + 1);
2024 x_l += w;
2025 x_l += button_padding;
2026 } else {
2027 x_r -= w;
2028 widget_set_allocation(button->widget,
2029 x_r, y , w + 1, h + 1);
2030 x_r -= button_padding;
2031 }
2032 }
2033}
2034
2035static int
2036frame_button_enter_handler(struct widget *widget,
2037 struct input *input, float x, float y, void *data)
2038{
2039 struct frame_button *frame_button = data;
2040
2041 widget_schedule_redraw(frame_button->widget);
2042 frame_button->state = FRAME_BUTTON_OVER;
2043
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002044 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002045}
2046
2047static void
2048frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2049{
2050 struct frame_button *frame_button = data;
2051
2052 widget_schedule_redraw(frame_button->widget);
2053 frame_button->state = FRAME_BUTTON_DEFAULT;
2054}
2055
2056static void
2057frame_button_button_handler(struct widget *widget,
2058 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002059 uint32_t button,
2060 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002061{
2062 struct frame_button *frame_button = data;
2063 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002064 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002065
2066 if (button != BTN_LEFT)
2067 return;
2068
2069 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002070 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002071 frame_button->state = FRAME_BUTTON_ACTIVE;
2072 widget_schedule_redraw(frame_button->widget);
2073
2074 if (frame_button->type == FRAME_BUTTON_ICON)
2075 window_show_frame_menu(window, input, time);
2076 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002077 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002078 frame_button->state = FRAME_BUTTON_DEFAULT;
2079 widget_schedule_redraw(frame_button->widget);
2080 break;
2081 }
2082
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002083 if (!was_pressed)
2084 return;
2085
Martin Minarik1998b152012-05-10 02:04:35 +02002086 switch (frame_button->type) {
2087 case FRAME_BUTTON_CLOSE:
2088 if (window->close_handler)
2089 window->close_handler(window->parent,
2090 window->user_data);
2091 else
2092 display_exit(window->display);
2093 break;
2094 case FRAME_BUTTON_MINIMIZE:
2095 fprintf(stderr,"Minimize stub\n");
2096 break;
2097 case FRAME_BUTTON_MAXIMIZE:
2098 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2099 break;
2100 default:
2101 /* Unknown operation */
2102 break;
2103 }
2104}
2105
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002106static int
2107frame_button_motion_handler(struct widget *widget,
2108 struct input *input, uint32_t time,
2109 float x, float y, void *data)
2110{
2111 struct frame_button *frame_button = data;
2112 enum frame_button_pointer previous_button_state = frame_button->state;
2113
2114 /* only track state for a pressed button */
2115 if (input->grab != widget)
2116 return CURSOR_LEFT_PTR;
2117
2118 if (x > widget->allocation.x &&
2119 x < (widget->allocation.x + widget->allocation.width) &&
2120 y > widget->allocation.y &&
2121 y < (widget->allocation.y + widget->allocation.height)) {
2122 frame_button->state = FRAME_BUTTON_ACTIVE;
2123 } else {
2124 frame_button->state = FRAME_BUTTON_DEFAULT;
2125 }
2126
2127 if (frame_button->state != previous_button_state)
2128 widget_schedule_redraw(frame_button->widget);
2129
2130 return CURSOR_LEFT_PTR;
2131}
2132
Martin Minarik1998b152012-05-10 02:04:35 +02002133static void
2134frame_button_redraw_handler(struct widget *widget, void *data)
2135{
2136 struct frame_button *frame_button = data;
2137 cairo_t *cr;
2138 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002139
2140 x = widget->allocation.x;
2141 y = widget->allocation.y;
2142 width = widget->allocation.width;
2143 height = widget->allocation.height;
2144
2145 if (!width)
2146 return;
2147 if (!height)
2148 return;
2149 if (widget->opaque)
2150 return;
2151
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002152 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002153
2154 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2155 cairo_set_line_width(cr, 1);
2156
2157 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2158 cairo_rectangle (cr, x, y, 25, 16);
2159
2160 cairo_stroke_preserve(cr);
2161
2162 switch (frame_button->state) {
2163 case FRAME_BUTTON_DEFAULT:
2164 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2165 break;
2166 case FRAME_BUTTON_OVER:
2167 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2168 break;
2169 case FRAME_BUTTON_ACTIVE:
2170 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2171 break;
2172 }
2173
2174 cairo_fill (cr);
2175
2176 x += 4;
2177 }
2178
2179 cairo_set_source_surface(cr, frame_button->icon, x, y);
2180 cairo_paint(cr);
2181
2182 cairo_destroy(cr);
2183}
2184
2185static struct widget *
2186frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2187 enum frame_button_align align, enum frame_button_decoration style)
2188{
2189 struct frame_button *frame_button;
2190 const char *icon = data;
2191
2192 frame_button = malloc (sizeof *frame_button);
2193 memset(frame_button, 0, sizeof *frame_button);
2194
2195 frame_button->icon = cairo_image_surface_create_from_png(icon);
2196 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2197 frame_button->frame = frame;
2198 frame_button->type = type;
2199 frame_button->align = align;
2200 frame_button->decoration = style;
2201
2202 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2203
2204 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2205 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2206 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2207 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002208 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002209 return frame_button->widget;
2210}
2211
2212static void
2213frame_button_destroy(struct frame_button *frame_button)
2214{
2215 widget_destroy(frame_button->widget);
2216 wl_list_remove(&frame_button->link);
2217 cairo_surface_destroy(frame_button->icon);
2218 free(frame_button);
2219
2220 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002221}
2222
2223static void
2224frame_redraw_handler(struct widget *widget, void *data)
2225{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002227 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002228 struct theme *t = window->display->theme;
2229 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002230
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002231 if (window->type == TYPE_FULLSCREEN)
2232 return;
2233
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002234 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002235
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002236 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002237 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002238 if (window->type == TYPE_MAXIMIZED)
2239 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002240 theme_render_frame(t, cr, widget->allocation.width,
2241 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002242
2243 cairo_destroy(cr);
2244}
2245
2246static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002247frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002248{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002249 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002250 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002251 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002252
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002253 if (window->type != TYPE_TOPLEVEL)
2254 return CURSOR_LEFT_PTR;
2255
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 location = theme_get_location(t, input->sx, input->sy,
2257 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002258 frame->widget->allocation.height,
2259 window->type == TYPE_MAXIMIZED ?
2260 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002261
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002263 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002264 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002265 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002266 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002267 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002268 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002269 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002270 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002271 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002272 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002273 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002274 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002275 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002276 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002277 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002278 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002279 case THEME_LOCATION_EXTERIOR:
2280 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002281 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002282 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002283 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002284}
2285
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002286static void
2287frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002288{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002289 struct display *display;
2290
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002291 switch (index) {
2292 case 0: /* close */
2293 if (window->close_handler)
2294 window->close_handler(window->parent,
2295 window->user_data);
2296 else
2297 display_exit(window->display);
2298 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002299 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002300 display = window->display;
2301 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002302 workspace_manager_move_surface(
2303 display->workspace_manager,
2304 window->main_surface->surface,
2305 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002306 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002307 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002308 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002309 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002310 workspace_manager_move_surface(
2311 display->workspace_manager,
2312 window->main_surface->surface,
2313 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002314 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002315 case 3: /* fullscreen */
2316 /* we don't have a way to get out of fullscreen for now */
2317 if (window->fullscreen_handler)
2318 window->fullscreen_handler(window, window->user_data);
2319 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002320 }
2321}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002322
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002323void
2324window_show_frame_menu(struct window *window,
2325 struct input *input, uint32_t time)
2326{
2327 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002328 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002329
2330 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002331 "Close",
2332 "Move to workspace above", "Move to workspace below",
2333 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002334 };
2335
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002336 if (window->fullscreen_handler)
2337 count = ARRAY_LENGTH(entries);
2338 else
2339 count = ARRAY_LENGTH(entries) - 1;
2340
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002341 input_get_position(input, &x, &y);
2342 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002343 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002344}
2345
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002346static int
2347frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002348 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002349{
2350 return frame_get_pointer_image_for_location(data, input);
2351}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002352
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002353static int
2354frame_motion_handler(struct widget *widget,
2355 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002356 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002357{
2358 return frame_get_pointer_image_for_location(data, input);
2359}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002360
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002361static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002362frame_button_handler(struct widget *widget,
2363 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002364 uint32_t button, enum wl_pointer_button_state state,
2365 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002366
2367{
2368 struct frame *frame = data;
2369 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002370 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002371 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002372
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002373 if (window->type != TYPE_TOPLEVEL)
2374 return;
2375
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002376 location = theme_get_location(display->theme, input->sx, input->sy,
2377 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002378 frame->widget->allocation.height,
2379 window->type == TYPE_MAXIMIZED ?
2380 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002381
Daniel Stone4dbadb12012-05-30 16:31:51 +01002382 if (window->display->shell && button == BTN_LEFT &&
2383 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002384 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002385 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002386 if (!window->shell_surface)
2387 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002388 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002389 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002390 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002391 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002392 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002393 case THEME_LOCATION_RESIZING_TOP:
2394 case THEME_LOCATION_RESIZING_BOTTOM:
2395 case THEME_LOCATION_RESIZING_LEFT:
2396 case THEME_LOCATION_RESIZING_RIGHT:
2397 case THEME_LOCATION_RESIZING_TOP_LEFT:
2398 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2399 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2400 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002401 if (!window->shell_surface)
2402 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002403 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002404
Pekka Paalanen99436862012-11-19 17:15:59 +02002405 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002406 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002407 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002408 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002409 break;
2410 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002411 } else if (button == BTN_RIGHT &&
2412 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002413 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002414 }
2415}
2416
2417struct widget *
2418frame_create(struct window *window, void *data)
2419{
2420 struct frame *frame;
2421
2422 frame = malloc(sizeof *frame);
2423 memset(frame, 0, sizeof *frame);
2424
2425 frame->widget = window_add_widget(window, frame);
2426 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002427
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002428 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2429 widget_set_resize_handler(frame->widget, frame_resize_handler);
2430 widget_set_enter_handler(frame->widget, frame_enter_handler);
2431 widget_set_motion_handler(frame->widget, frame_motion_handler);
2432 widget_set_button_handler(frame->widget, frame_button_handler);
2433
Martin Minarik1998b152012-05-10 02:04:35 +02002434 /* Create empty list for frame buttons */
2435 wl_list_init(&frame->buttons_list);
2436
2437 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2438 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2439
2440 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2441 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2442
2443 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2444 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2445
2446 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2447 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2448
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002449 window->frame = frame;
2450
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002451 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002452}
2453
Kristian Høgsberga1627922012-06-20 17:30:03 -04002454void
2455frame_set_child_size(struct widget *widget, int child_width, int child_height)
2456{
2457 struct display *display = widget->window->display;
2458 struct theme *t = display->theme;
2459 int decoration_width, decoration_height;
2460 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002461 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002462
2463 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002464 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002465 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002466 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002467
2468 width = child_width + decoration_width;
2469 height = child_height + decoration_height;
2470 } else {
2471 width = child_width;
2472 height = child_height;
2473 }
2474
2475 window_schedule_resize(widget->window, width, height);
2476}
2477
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002478static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002479frame_destroy(struct frame *frame)
2480{
Martin Minarik1998b152012-05-10 02:04:35 +02002481 struct frame_button *button, *tmp;
2482
2483 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2484 frame_button_destroy(button);
2485
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002486 /* frame->child must be destroyed by the application */
2487 widget_destroy(frame->widget);
2488 free(frame);
2489}
2490
2491static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002492input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002493 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002494{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002495 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002496 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002497
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002498 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002499 return;
2500
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002501 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002502 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002503 widget = old;
2504 if (input->grab)
2505 widget = input->grab;
2506 if (widget->leave_handler)
2507 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002508 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002509 }
2510
2511 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002512 widget = focus;
2513 if (input->grab)
2514 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002515 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002516 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002517 cursor = widget->enter_handler(focus, input, x, y,
2518 widget->user_data);
2519 else
2520 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002521
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002522 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002523 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002524}
2525
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002526void
2527input_grab(struct input *input, struct widget *widget, uint32_t button)
2528{
2529 input->grab = widget;
2530 input->grab_button = button;
2531}
2532
2533void
2534input_ungrab(struct input *input)
2535{
2536 struct widget *widget;
2537
2538 input->grab = NULL;
2539 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002540 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002541 input->sx, input->sy);
2542 input_set_focus_widget(input, widget, input->sx, input->sy);
2543 }
2544}
2545
2546static void
2547input_remove_pointer_focus(struct input *input)
2548{
2549 struct window *window = input->pointer_focus;
2550
2551 if (!window)
2552 return;
2553
2554 input_set_focus_widget(input, NULL, 0, 0);
2555
2556 input->pointer_focus = NULL;
2557 input->current_cursor = CURSOR_UNSET;
2558}
2559
2560static void
2561pointer_handle_enter(void *data, struct wl_pointer *pointer,
2562 uint32_t serial, struct wl_surface *surface,
2563 wl_fixed_t sx_w, wl_fixed_t sy_w)
2564{
2565 struct input *input = data;
2566 struct window *window;
2567 struct widget *widget;
2568 float sx = wl_fixed_to_double(sx_w);
2569 float sy = wl_fixed_to_double(sy_w);
2570
2571 if (!surface) {
2572 /* enter event for a window we've just destroyed */
2573 return;
2574 }
2575
2576 input->display->serial = serial;
2577 input->pointer_enter_serial = serial;
2578 input->pointer_focus = wl_surface_get_user_data(surface);
2579 window = input->pointer_focus;
2580
Pekka Paalanen99436862012-11-19 17:15:59 +02002581 if (window->resizing) {
2582 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002583 /* Schedule a redraw to free the pool */
2584 window_schedule_redraw(window);
2585 }
2586
2587 input->sx = sx;
2588 input->sy = sy;
2589
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002590 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002591 input_set_focus_widget(input, widget, sx, sy);
2592}
2593
2594static void
2595pointer_handle_leave(void *data, struct wl_pointer *pointer,
2596 uint32_t serial, struct wl_surface *surface)
2597{
2598 struct input *input = data;
2599
2600 input->display->serial = serial;
2601 input_remove_pointer_focus(input);
2602}
2603
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002604static void
Daniel Stone37816df2012-05-16 18:45:18 +01002605pointer_handle_motion(void *data, struct wl_pointer *pointer,
2606 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002607{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002608 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002609 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002610 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002611 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002612 float sx = wl_fixed_to_double(sx_w);
2613 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002614
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002615 input->sx = sx;
2616 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002617
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002618 if (!window)
2619 return;
2620
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002621 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002622 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002623 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002624 }
2625
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002626 if (input->grab)
2627 widget = input->grab;
2628 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002629 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002630 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002631 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002632 input, time, sx, sy,
2633 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002634 else
2635 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002636
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002637 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002638}
2639
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002640static void
Daniel Stone37816df2012-05-16 18:45:18 +01002641pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002642 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002643{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002644 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002645 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002646 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002647
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002648 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002649 if (input->focus_widget && input->grab == NULL &&
2650 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002651 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002652
Neil Roberts6b28aad2012-01-23 19:11:18 +00002653 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002654 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002655 (*widget->button_handler)(widget,
2656 input, time,
2657 button, state,
2658 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002659
Daniel Stone4dbadb12012-05-30 16:31:51 +01002660 if (input->grab && input->grab_button == button &&
2661 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002662 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002663}
2664
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002665static void
Daniel Stone37816df2012-05-16 18:45:18 +01002666pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002667 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002668{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002669 struct input *input = data;
2670 struct widget *widget;
2671
2672 widget = input->focus_widget;
2673 if (input->grab)
2674 widget = input->grab;
2675 if (widget && widget->axis_handler)
2676 (*widget->axis_handler)(widget,
2677 input, time,
2678 axis, value,
2679 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002680}
2681
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002682static const struct wl_pointer_listener pointer_listener = {
2683 pointer_handle_enter,
2684 pointer_handle_leave,
2685 pointer_handle_motion,
2686 pointer_handle_button,
2687 pointer_handle_axis,
2688};
2689
2690static void
2691input_remove_keyboard_focus(struct input *input)
2692{
2693 struct window *window = input->keyboard_focus;
2694 struct itimerspec its;
2695
2696 its.it_interval.tv_sec = 0;
2697 its.it_interval.tv_nsec = 0;
2698 its.it_value.tv_sec = 0;
2699 its.it_value.tv_nsec = 0;
2700 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2701
2702 if (!window)
2703 return;
2704
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002705 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002706 if (window->keyboard_focus_handler)
2707 (*window->keyboard_focus_handler)(window, NULL,
2708 window->user_data);
2709
2710 input->keyboard_focus = NULL;
2711}
2712
2713static void
2714keyboard_repeat_func(struct task *task, uint32_t events)
2715{
2716 struct input *input =
2717 container_of(task, struct input, repeat_task);
2718 struct window *window = input->keyboard_focus;
2719 uint64_t exp;
2720
2721 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2722 /* If we change the timer between the fd becoming
2723 * readable and getting here, there'll be nothing to
2724 * read and we get EAGAIN. */
2725 return;
2726
2727 if (window && window->key_handler) {
2728 (*window->key_handler)(window, input, input->repeat_time,
2729 input->repeat_key, input->repeat_sym,
2730 WL_KEYBOARD_KEY_STATE_PRESSED,
2731 window->user_data);
2732 }
2733}
2734
2735static void
2736keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2737 uint32_t format, int fd, uint32_t size)
2738{
2739 struct input *input = data;
2740 char *map_str;
2741
2742 if (!data) {
2743 close(fd);
2744 return;
2745 }
2746
2747 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2748 close(fd);
2749 return;
2750 }
2751
2752 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2753 if (map_str == MAP_FAILED) {
2754 close(fd);
2755 return;
2756 }
2757
2758 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2759 map_str,
2760 XKB_KEYMAP_FORMAT_TEXT_V1,
2761 0);
2762 munmap(map_str, size);
2763 close(fd);
2764
2765 if (!input->xkb.keymap) {
2766 fprintf(stderr, "failed to compile keymap\n");
2767 return;
2768 }
2769
2770 input->xkb.state = xkb_state_new(input->xkb.keymap);
2771 if (!input->xkb.state) {
2772 fprintf(stderr, "failed to create XKB state\n");
2773 xkb_map_unref(input->xkb.keymap);
2774 input->xkb.keymap = NULL;
2775 return;
2776 }
2777
2778 input->xkb.control_mask =
2779 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2780 input->xkb.alt_mask =
2781 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2782 input->xkb.shift_mask =
2783 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2784}
2785
2786static void
2787keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2788 uint32_t serial, struct wl_surface *surface,
2789 struct wl_array *keys)
2790{
2791 struct input *input = data;
2792 struct window *window;
2793
2794 input->display->serial = serial;
2795 input->keyboard_focus = wl_surface_get_user_data(surface);
2796
2797 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002798 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002799 if (window->keyboard_focus_handler)
2800 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002801 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002802}
2803
2804static void
2805keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2806 uint32_t serial, struct wl_surface *surface)
2807{
2808 struct input *input = data;
2809
2810 input->display->serial = serial;
2811 input_remove_keyboard_focus(input);
2812}
2813
Scott Moreau210d0792012-03-22 10:47:01 -06002814static void
Daniel Stone37816df2012-05-16 18:45:18 +01002815keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002816 uint32_t serial, uint32_t time, uint32_t key,
2817 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002818{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002819 struct input *input = data;
2820 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002821 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002822 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002823 const xkb_keysym_t *syms;
2824 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002825 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002826
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002827 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002828 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002829 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002830 return;
2831
Daniel Stone97f68542012-05-30 16:32:01 +01002832 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002833
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002834 sym = XKB_KEY_NoSymbol;
2835 if (num_syms == 1)
2836 sym = syms[0];
2837
2838 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002839 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002840 window_set_maximized(window,
2841 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002842 } else if (sym == XKB_KEY_F11 &&
2843 window->fullscreen_handler &&
2844 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2845 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002846 } else if (sym == XKB_KEY_F4 &&
2847 input->modifiers == MOD_ALT_MASK &&
2848 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2849 if (window->close_handler)
2850 window->close_handler(window->parent,
2851 window->user_data);
2852 else
2853 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002854 } else if (window->key_handler) {
2855 (*window->key_handler)(window, input, time, key,
2856 sym, state, window->user_data);
2857 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002858
2859 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2860 key == input->repeat_key) {
2861 its.it_interval.tv_sec = 0;
2862 its.it_interval.tv_nsec = 0;
2863 its.it_value.tv_sec = 0;
2864 its.it_value.tv_nsec = 0;
2865 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2866 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2867 input->repeat_sym = sym;
2868 input->repeat_key = key;
2869 input->repeat_time = time;
2870 its.it_interval.tv_sec = 0;
2871 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2872 its.it_value.tv_sec = 0;
2873 its.it_value.tv_nsec = 400 * 1000 * 1000;
2874 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2875 }
2876}
2877
2878static void
Daniel Stone351eb612012-05-31 15:27:47 -04002879keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2880 uint32_t serial, uint32_t mods_depressed,
2881 uint32_t mods_latched, uint32_t mods_locked,
2882 uint32_t group)
2883{
2884 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002885 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002886
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002887 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2888 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002889 mask = xkb_state_serialize_mods(input->xkb.state,
2890 XKB_STATE_DEPRESSED |
2891 XKB_STATE_LATCHED);
2892 input->modifiers = 0;
2893 if (mask & input->xkb.control_mask)
2894 input->modifiers |= MOD_CONTROL_MASK;
2895 if (mask & input->xkb.alt_mask)
2896 input->modifiers |= MOD_ALT_MASK;
2897 if (mask & input->xkb.shift_mask)
2898 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002899}
2900
Daniel Stone37816df2012-05-16 18:45:18 +01002901static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002902 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002903 keyboard_handle_enter,
2904 keyboard_handle_leave,
2905 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002906 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002907};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002908
2909static void
Daniel Stone37816df2012-05-16 18:45:18 +01002910seat_handle_capabilities(void *data, struct wl_seat *seat,
2911 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002912{
Daniel Stone37816df2012-05-16 18:45:18 +01002913 struct input *input = data;
2914
2915 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2916 input->pointer = wl_seat_get_pointer(seat);
2917 wl_pointer_set_user_data(input->pointer, input);
2918 wl_pointer_add_listener(input->pointer, &pointer_listener,
2919 input);
2920 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2921 wl_pointer_destroy(input->pointer);
2922 input->pointer = NULL;
2923 }
2924
2925 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2926 input->keyboard = wl_seat_get_keyboard(seat);
2927 wl_keyboard_set_user_data(input->keyboard, input);
2928 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2929 input);
2930 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2931 wl_keyboard_destroy(input->keyboard);
2932 input->keyboard = NULL;
2933 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002934}
2935
Daniel Stone37816df2012-05-16 18:45:18 +01002936static const struct wl_seat_listener seat_listener = {
2937 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002938};
2939
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002940void
2941input_get_position(struct input *input, int32_t *x, int32_t *y)
2942{
2943 *x = input->sx;
2944 *y = input->sy;
2945}
2946
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002947struct display *
2948input_get_display(struct input *input)
2949{
2950 return input->display;
2951}
2952
Daniel Stone37816df2012-05-16 18:45:18 +01002953struct wl_seat *
2954input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002955{
Daniel Stone37816df2012-05-16 18:45:18 +01002956 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002957}
2958
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002959uint32_t
2960input_get_modifiers(struct input *input)
2961{
2962 return input->modifiers;
2963}
2964
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002965struct widget *
2966input_get_focus_widget(struct input *input)
2967{
2968 return input->focus_widget;
2969}
2970
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002971struct data_offer {
2972 struct wl_data_offer *offer;
2973 struct input *input;
2974 struct wl_array types;
2975 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002976
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002977 struct task io_task;
2978 int fd;
2979 data_func_t func;
2980 int32_t x, y;
2981 void *user_data;
2982};
2983
2984static void
2985data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2986{
2987 struct data_offer *offer = data;
2988 char **p;
2989
2990 p = wl_array_add(&offer->types, sizeof *p);
2991 *p = strdup(type);
2992}
2993
2994static const struct wl_data_offer_listener data_offer_listener = {
2995 data_offer_offer,
2996};
2997
2998static void
2999data_offer_destroy(struct data_offer *offer)
3000{
3001 char **p;
3002
3003 offer->refcount--;
3004 if (offer->refcount == 0) {
3005 wl_data_offer_destroy(offer->offer);
3006 for (p = offer->types.data; *p; p++)
3007 free(*p);
3008 wl_array_release(&offer->types);
3009 free(offer);
3010 }
3011}
3012
3013static void
3014data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003015 struct wl_data_device *data_device,
3016 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003017{
3018 struct data_offer *offer;
3019
3020 offer = malloc(sizeof *offer);
3021
3022 wl_array_init(&offer->types);
3023 offer->refcount = 1;
3024 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003025 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003026 wl_data_offer_add_listener(offer->offer,
3027 &data_offer_listener, offer);
3028}
3029
3030static void
3031data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003032 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003033 wl_fixed_t x_w, wl_fixed_t y_w,
3034 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003035{
3036 struct input *input = data;
3037 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003038 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003039 float x = wl_fixed_to_double(x_w);
3040 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003041 char **p;
3042
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003043 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003044 window = wl_surface_get_user_data(surface);
3045 input->pointer_focus = window;
3046
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003047 if (offer) {
3048 input->drag_offer = wl_data_offer_get_user_data(offer);
3049
3050 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3051 *p = NULL;
3052
3053 types_data = input->drag_offer->types.data;
3054 } else {
3055 input->drag_offer = NULL;
3056 types_data = NULL;
3057 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003058
3059 window = input->pointer_focus;
3060 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003061 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003062 window->user_data);
3063}
3064
3065static void
3066data_device_leave(void *data, struct wl_data_device *data_device)
3067{
3068 struct input *input = data;
3069
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003070 if (input->drag_offer) {
3071 data_offer_destroy(input->drag_offer);
3072 input->drag_offer = NULL;
3073 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003074}
3075
3076static void
3077data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003078 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003079{
3080 struct input *input = data;
3081 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003082 float x = wl_fixed_to_double(x_w);
3083 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003084 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003085
3086 input->sx = x;
3087 input->sy = y;
3088
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003089 if (input->drag_offer)
3090 types_data = input->drag_offer->types.data;
3091 else
3092 types_data = NULL;
3093
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003094 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003095 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003096 window->user_data);
3097}
3098
3099static void
3100data_device_drop(void *data, struct wl_data_device *data_device)
3101{
3102 struct input *input = data;
3103 struct window *window = input->pointer_focus;
3104
3105 if (window->drop_handler)
3106 window->drop_handler(window, input,
3107 input->sx, input->sy, window->user_data);
3108}
3109
3110static void
3111data_device_selection(void *data,
3112 struct wl_data_device *wl_data_device,
3113 struct wl_data_offer *offer)
3114{
3115 struct input *input = data;
3116 char **p;
3117
3118 if (input->selection_offer)
3119 data_offer_destroy(input->selection_offer);
3120
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003121 if (offer) {
3122 input->selection_offer = wl_data_offer_get_user_data(offer);
3123 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3124 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003125 } else {
3126 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003127 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003128}
3129
3130static const struct wl_data_device_listener data_device_listener = {
3131 data_device_data_offer,
3132 data_device_enter,
3133 data_device_leave,
3134 data_device_motion,
3135 data_device_drop,
3136 data_device_selection
3137};
3138
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003139static void
3140input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003141{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003142 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003143 struct wl_cursor *cursor;
3144 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003145
Daniel Stone80972742012-11-07 17:51:39 +11003146 if (!input->pointer)
3147 return;
3148
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003149 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003150 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003151 return;
3152
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003153 if (index >= (int) cursor->image_count) {
3154 fprintf(stderr, "cursor index out of range\n");
3155 return;
3156 }
3157
3158 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003159 buffer = wl_cursor_image_get_buffer(image);
3160 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003161 return;
3162
Kristian Høgsbergae277372012-08-01 09:41:08 -04003163 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003164 input->pointer_surface,
3165 image->hotspot_x, image->hotspot_y);
3166 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3167 wl_surface_damage(input->pointer_surface, 0, 0,
3168 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003169 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003170}
3171
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003172static const struct wl_callback_listener pointer_surface_listener;
3173
3174static void
3175pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3176 uint32_t time)
3177{
3178 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003179 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003180 int i;
3181
3182 if (callback) {
3183 assert(callback == input->cursor_frame_cb);
3184 wl_callback_destroy(callback);
3185 input->cursor_frame_cb = NULL;
3186 }
3187
Daniel Stone80972742012-11-07 17:51:39 +11003188 if (!input->pointer)
3189 return;
3190
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003191 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003192 wl_pointer_set_cursor(input->pointer,
3193 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003194 NULL, 0, 0);
3195 return;
3196 }
3197
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003198 if (input->current_cursor == CURSOR_UNSET)
3199 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003200 cursor = input->display->cursors[input->current_cursor];
3201 if (!cursor)
3202 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003203
3204 /* FIXME We don't have the current time on the first call so we set
3205 * the animation start to the time of the first frame callback. */
3206 if (time == 0)
3207 input->cursor_anim_start = 0;
3208 else if (input->cursor_anim_start == 0)
3209 input->cursor_anim_start = time;
3210
3211 if (time == 0 || input->cursor_anim_start == 0)
3212 i = 0;
3213 else
3214 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3215
Pekka Paalanenbc106382012-10-10 12:49:31 +03003216 if (cursor->image_count > 1) {
3217 input->cursor_frame_cb =
3218 wl_surface_frame(input->pointer_surface);
3219 wl_callback_add_listener(input->cursor_frame_cb,
3220 &pointer_surface_listener, input);
3221 }
3222
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003223 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003224}
3225
3226static const struct wl_callback_listener pointer_surface_listener = {
3227 pointer_surface_frame_callback
3228};
3229
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003230void
3231input_set_pointer_image(struct input *input, int pointer)
3232{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003233 int force = 0;
3234
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003235 if (!input->pointer)
3236 return;
3237
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003238 if (input->pointer_enter_serial > input->cursor_serial)
3239 force = 1;
3240
3241 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003242 return;
3243
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003244 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003245 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003246 if (!input->cursor_frame_cb)
3247 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003248 else if (force) {
3249 /* The current frame callback may be stuck if, for instance,
3250 * the set cursor request was processed by the server after
3251 * this client lost the focus. In this case the cursor surface
3252 * might not be mapped and the frame callback wouldn't ever
3253 * complete. Send a set_cursor and attach to try to map the
3254 * cursor surface again so that the callback will finish */
3255 input_set_pointer_image_index(input, 0);
3256 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003257}
3258
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003259struct wl_data_device *
3260input_get_data_device(struct input *input)
3261{
3262 return input->data_device;
3263}
3264
3265void
3266input_set_selection(struct input *input,
3267 struct wl_data_source *source, uint32_t time)
3268{
3269 wl_data_device_set_selection(input->data_device, source, time);
3270}
3271
3272void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003273input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003274{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003275 wl_data_offer_accept(input->drag_offer->offer,
3276 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003277}
3278
3279static void
3280offer_io_func(struct task *task, uint32_t events)
3281{
3282 struct data_offer *offer =
3283 container_of(task, struct data_offer, io_task);
3284 unsigned int len;
3285 char buffer[4096];
3286
3287 len = read(offer->fd, buffer, sizeof buffer);
3288 offer->func(buffer, len,
3289 offer->x, offer->y, offer->user_data);
3290
3291 if (len == 0) {
3292 close(offer->fd);
3293 data_offer_destroy(offer);
3294 }
3295}
3296
3297static void
3298data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3299 data_func_t func, void *user_data)
3300{
3301 int p[2];
3302
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003303 if (pipe2(p, O_CLOEXEC) == -1)
3304 return;
3305
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003306 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3307 close(p[1]);
3308
3309 offer->io_task.run = offer_io_func;
3310 offer->fd = p[0];
3311 offer->func = func;
3312 offer->refcount++;
3313 offer->user_data = user_data;
3314
3315 display_watch_fd(offer->input->display,
3316 offer->fd, EPOLLIN, &offer->io_task);
3317}
3318
3319void
3320input_receive_drag_data(struct input *input, const char *mime_type,
3321 data_func_t func, void *data)
3322{
3323 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3324 input->drag_offer->x = input->sx;
3325 input->drag_offer->y = input->sy;
3326}
3327
3328int
3329input_receive_selection_data(struct input *input, const char *mime_type,
3330 data_func_t func, void *data)
3331{
3332 char **p;
3333
3334 if (input->selection_offer == NULL)
3335 return -1;
3336
3337 for (p = input->selection_offer->types.data; *p; p++)
3338 if (strcmp(mime_type, *p) == 0)
3339 break;
3340
3341 if (*p == NULL)
3342 return -1;
3343
3344 data_offer_receive_data(input->selection_offer,
3345 mime_type, func, data);
3346 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003347}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003348
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003349int
3350input_receive_selection_data_to_fd(struct input *input,
3351 const char *mime_type, int fd)
3352{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003353 if (input->selection_offer)
3354 wl_data_offer_receive(input->selection_offer->offer,
3355 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003356
3357 return 0;
3358}
3359
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003360void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003361window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003362{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003363 if (!window->shell_surface)
3364 return;
3365
Daniel Stone37816df2012-05-16 18:45:18 +01003366 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003367}
3368
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003369static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003370surface_set_synchronized(struct surface *surface)
3371{
3372 if (!surface->subsurface)
3373 return;
3374
3375 if (surface->synchronized)
3376 return;
3377
3378 wl_subsurface_set_sync(surface->subsurface);
3379 surface->synchronized = 1;
3380}
3381
3382static void
3383surface_set_synchronized_default(struct surface *surface)
3384{
3385 if (!surface->subsurface)
3386 return;
3387
3388 if (surface->synchronized == surface->synchronized_default)
3389 return;
3390
3391 if (surface->synchronized_default)
3392 wl_subsurface_set_sync(surface->subsurface);
3393 else
3394 wl_subsurface_set_desync(surface->subsurface);
3395
3396 surface->synchronized = surface->synchronized_default;
3397}
3398
3399static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003400surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003401{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003402 struct widget *widget = surface->widget;
3403 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003404
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003405 if (surface->input_region) {
3406 wl_region_destroy(surface->input_region);
3407 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003408 }
3409
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003410 if (surface->opaque_region)
3411 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003412
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003413 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003414
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003415 if (widget->resize_handler)
3416 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003417 widget->allocation.width,
3418 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003419 widget->user_data);
3420
Pekka Paalanen35e82632013-04-25 13:57:48 +03003421 if (surface->subsurface &&
3422 (surface->allocation.x != widget->allocation.x ||
3423 surface->allocation.y != widget->allocation.y)) {
3424 wl_subsurface_set_position(surface->subsurface,
3425 widget->allocation.x,
3426 widget->allocation.y);
3427 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003428 if (surface->allocation.width != widget->allocation.width ||
3429 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003430 window_schedule_redraw(widget->window);
3431 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003432 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003433
3434 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003435 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003436 widget->allocation.width,
3437 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003438}
3439
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003440static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003441hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3442{
3443 /*
3444 * This hack should be removed, when EGL respects
3445 * eglSwapInterval(0).
3446 *
3447 * If this window has sub-surfaces, especially a free-running
3448 * EGL-widget, we need to post the parent surface once with
3449 * all the old state to guarantee, that the EGL-widget will
3450 * receive its frame callback soon. Otherwise, a forced call
3451 * to eglSwapBuffers may end up blocking, waiting for a frame
3452 * event that will never come, because we will commit the parent
3453 * surface with all new state only after eglSwapBuffers returns.
3454 *
3455 * This assumes, that:
3456 * 1. When the EGL widget's resize hook is called, it pauses.
3457 * 2. When the EGL widget's redraw hook is called, it forces a
3458 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3459 * In a single threaded application condition 1 is a no-op.
3460 *
3461 * XXX: This should actually be after the surface_resize() calls,
3462 * but cannot, because then it would commit the incomplete state
3463 * accumulated from the widget resize hooks.
3464 */
3465 if (window->subsurface_list.next != &window->main_surface->link ||
3466 window->subsurface_list.prev != &window->main_surface->link)
3467 wl_surface_commit(window->main_surface->surface);
3468}
3469
3470static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003471idle_resize(struct window *window)
3472{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003473 struct surface *surface;
3474
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003475 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003476 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003477
Pekka Paalanen71233882013-04-25 13:57:53 +03003478 DBG("from %dx%d to %dx%d\n",
3479 window->main_surface->server_allocation.width,
3480 window->main_surface->server_allocation.height,
3481 window->pending_allocation.width,
3482 window->pending_allocation.height);
3483
Pekka Paalanene9297f82013-04-25 13:57:51 +03003484 hack_prevent_EGL_sub_surface_deadlock(window);
3485
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003486 widget_set_allocation(window->main_surface->widget,
3487 window->pending_allocation.x,
3488 window->pending_allocation.y,
3489 window->pending_allocation.width,
3490 window->pending_allocation.height);
3491
3492 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003493
3494 /* The main surface is in the list, too. Main surface's
3495 * resize_handler is responsible for calling widget_set_allocation()
3496 * on all sub-surface root widgets, so they will be resized
3497 * properly.
3498 */
3499 wl_list_for_each(surface, &window->subsurface_list, link) {
3500 if (surface == window->main_surface)
3501 continue;
3502
3503 surface_set_synchronized(surface);
3504 surface_resize(surface);
3505 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003506}
3507
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003508void
3509window_schedule_resize(struct window *window, int width, int height)
3510{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003511 window->pending_allocation.x = 0;
3512 window->pending_allocation.y = 0;
3513 window->pending_allocation.width = width;
3514 window->pending_allocation.height = height;
3515
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003516 if (window->min_allocation.width == 0)
3517 window->min_allocation = window->pending_allocation;
3518 if (window->pending_allocation.width < window->min_allocation.width)
3519 window->pending_allocation.width = window->min_allocation.width;
3520 if (window->pending_allocation.height < window->min_allocation.height)
3521 window->pending_allocation.height = window->min_allocation.height;
3522
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003523 window->resize_needed = 1;
3524 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003525}
3526
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003527void
3528widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3529{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003530 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003531}
3532
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003533static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003534handle_ping(void *data, struct wl_shell_surface *shell_surface,
3535 uint32_t serial)
3536{
3537 wl_shell_surface_pong(shell_surface, serial);
3538}
3539
3540static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003541handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003542 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003543{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003544 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003545
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003546 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003547 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003548}
3549
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003550static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003551menu_destroy(struct menu *menu)
3552{
3553 widget_destroy(menu->widget);
3554 window_destroy(menu->window);
3555 free(menu);
3556}
3557
3558static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003559handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3560{
3561 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003562 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003563
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003564 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003565 * device. Or just use wl_callback. And this really needs to
3566 * be a window vfunc that the menu can set. And we need the
3567 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003568
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003569 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003570 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003571}
3572
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003573static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003574 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003575 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003576 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003577};
3578
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003579void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003580window_get_allocation(struct window *window,
3581 struct rectangle *allocation)
3582{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003583 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003584}
3585
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003586static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003587widget_redraw(struct widget *widget)
3588{
3589 struct widget *child;
3590
3591 if (widget->redraw_handler)
3592 widget->redraw_handler(widget, widget->user_data);
3593 wl_list_for_each(child, &widget->child_list, link)
3594 widget_redraw(child);
3595}
3596
3597static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003598frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3599{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003600 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003601
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003602 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003603 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003604 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003605 surface->frame_cb = NULL;
3606
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003607 surface->last_time = time;
3608
Pekka Paalanen71233882013-04-25 13:57:53 +03003609 if (surface->redraw_needed || surface->window->redraw_needed) {
3610 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003611 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003612 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003613}
3614
3615static const struct wl_callback_listener listener = {
3616 frame_callback
3617};
3618
3619static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003620surface_redraw(struct surface *surface)
3621{
Pekka Paalanen71233882013-04-25 13:57:53 +03003622 DBG_OBJ(surface->surface, "begin\n");
3623
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003624 if (!surface->window->redraw_needed && !surface->redraw_needed)
3625 return;
3626
3627 /* Whole-window redraw forces a redraw even if the previous has
3628 * not yet hit the screen.
3629 */
3630 if (surface->frame_cb) {
3631 if (!surface->window->redraw_needed)
3632 return;
3633
Pekka Paalanen71233882013-04-25 13:57:53 +03003634 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003635 wl_callback_destroy(surface->frame_cb);
3636 }
3637
3638 surface->frame_cb = wl_surface_frame(surface->surface);
3639 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003640 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003641
3642 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003643 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003644 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003645 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003646}
3647
3648static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003649idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003650{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003651 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003652 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003653
Pekka Paalanen71233882013-04-25 13:57:53 +03003654 DBG(" --------- \n");
3655
Pekka Paalaneneebff542013-04-25 13:57:52 +03003656 wl_list_init(&window->redraw_task.link);
3657 window->redraw_task_scheduled = 0;
3658
3659 if (window->resize_needed) {
3660 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003661 if (window->main_surface->frame_cb) {
3662 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003663 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003664 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003665
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003666 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003667 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003668
Pekka Paalanen35e82632013-04-25 13:57:48 +03003669 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003670 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003671
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003672 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003673 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003674
3675 wl_list_for_each(surface, &window->subsurface_list, link)
3676 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003677}
3678
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003679static void
3680window_schedule_redraw_task(struct window *window)
3681{
3682 if (window->configure_requests)
3683 return;
3684 if (!window->redraw_task_scheduled) {
3685 window->redraw_task.run = idle_redraw;
3686 display_defer(window->display, &window->redraw_task);
3687 window->redraw_task_scheduled = 1;
3688 }
3689}
3690
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003691void
3692window_schedule_redraw(struct window *window)
3693{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003694 struct surface *surface;
3695
Pekka Paalanen71233882013-04-25 13:57:53 +03003696 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3697
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003698 wl_list_for_each(surface, &window->subsurface_list, link)
3699 surface->redraw_needed = 1;
3700
3701 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003702}
3703
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003704int
3705window_is_fullscreen(struct window *window)
3706{
3707 return window->type == TYPE_FULLSCREEN;
3708}
3709
MoD063a8822013-03-02 23:43:57 +00003710static void
3711configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3712{
3713 struct window *window = data;
3714
3715 wl_callback_destroy(callback);
3716 window->configure_requests--;
3717
3718 if (!window->configure_requests)
3719 window_schedule_redraw(window);
3720}
3721
3722static struct wl_callback_listener configure_request_listener = {
3723 configure_request_completed,
3724};
3725
3726static void
3727window_defer_redraw_until_configure(struct window* window)
3728{
3729 struct wl_callback *callback;
3730
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003731 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003732 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003733 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003734 }
3735
3736 callback = wl_display_sync(window->display->display);
3737 wl_callback_add_listener(callback, &configure_request_listener, window);
3738 window->configure_requests++;
3739}
3740
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003741void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003742window_set_fullscreen(struct window *window, int fullscreen)
3743{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003744 if (!window->display->shell)
3745 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003746
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003747 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003748 return;
3749
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003750 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003751 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003752 if (window->type == TYPE_TOPLEVEL) {
3753 window->saved_allocation = window->main_surface->allocation;
3754 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003755 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003756 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003757 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003758 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003759 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003760 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003761 if (window->saved_type == TYPE_MAXIMIZED) {
3762 window_set_maximized(window, 1);
3763 } else {
3764 window->type = TYPE_TOPLEVEL;
3765 wl_shell_surface_set_toplevel(window->shell_surface);
3766 window_schedule_resize(window,
3767 window->saved_allocation.width,
3768 window->saved_allocation.height);
3769 }
3770
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003771 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003772}
3773
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003774void
3775window_set_fullscreen_method(struct window *window,
3776 enum wl_shell_surface_fullscreen_method method)
3777{
3778 window->fullscreen_method = method;
3779}
3780
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003781int
3782window_is_maximized(struct window *window)
3783{
3784 return window->type == TYPE_MAXIMIZED;
3785}
3786
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003787void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003788window_set_maximized(struct window *window, int maximized)
3789{
3790 if (!window->display->shell)
3791 return;
3792
3793 if ((window->type == TYPE_MAXIMIZED) == maximized)
3794 return;
3795
3796 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003797 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003798 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3799 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003800 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003801 } else if (window->type == TYPE_FULLSCREEN) {
3802 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3803 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003804 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003805 } else {
3806 wl_shell_surface_set_toplevel(window->shell_surface);
3807 window->type = TYPE_TOPLEVEL;
3808 window_schedule_resize(window,
3809 window->saved_allocation.width,
3810 window->saved_allocation.height);
3811 }
3812}
3813
3814void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003815window_set_user_data(struct window *window, void *data)
3816{
3817 window->user_data = data;
3818}
3819
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003820void *
3821window_get_user_data(struct window *window)
3822{
3823 return window->user_data;
3824}
3825
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003826void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003827window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003828 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003829{
3830 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003831}
3832
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003833void
3834window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003835 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003836{
3837 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003838}
3839
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003840void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003841window_set_data_handler(struct window *window, window_data_handler_t handler)
3842{
3843 window->data_handler = handler;
3844}
3845
3846void
3847window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3848{
3849 window->drop_handler = handler;
3850}
3851
3852void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003853window_set_close_handler(struct window *window,
3854 window_close_handler_t handler)
3855{
3856 window->close_handler = handler;
3857}
3858
3859void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003860window_set_fullscreen_handler(struct window *window,
3861 window_fullscreen_handler_t handler)
3862{
3863 window->fullscreen_handler = handler;
3864}
3865
3866void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003867window_set_output_handler(struct window *window,
3868 window_output_handler_t handler)
3869{
3870 window->output_handler = handler;
3871}
3872
3873void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003874window_set_title(struct window *window, const char *title)
3875{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003876 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003877 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003878 if (window->shell_surface)
3879 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003880}
3881
3882const char *
3883window_get_title(struct window *window)
3884{
3885 return window->title;
3886}
3887
3888void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003889window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3890{
3891 struct text_cursor_position *text_cursor_position =
3892 window->display->text_cursor_position;
3893
Scott Moreau9295ce02012-06-01 12:46:10 -06003894 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003895 return;
3896
3897 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003898 window->main_surface->surface,
3899 wl_fixed_from_int(x),
3900 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003901}
3902
3903void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003904window_damage(struct window *window, int32_t x, int32_t y,
3905 int32_t width, int32_t height)
3906{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003907 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003908}
3909
Casey Dahlin9074db52012-04-19 22:50:09 -04003910static void
3911surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003912 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003913{
Rob Bradford7507b572012-05-15 17:55:34 +01003914 struct window *window = data;
3915 struct output *output;
3916 struct output *output_found = NULL;
3917 struct window_output *window_output;
3918
3919 wl_list_for_each(output, &window->display->output_list, link) {
3920 if (output->output == wl_output) {
3921 output_found = output;
3922 break;
3923 }
3924 }
3925
3926 if (!output_found)
3927 return;
3928
3929 window_output = malloc (sizeof *window_output);
3930 window_output->output = output_found;
3931
3932 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003933
3934 if (window->output_handler)
3935 window->output_handler(window, output_found, 1,
3936 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003937}
3938
3939static void
3940surface_leave(void *data,
3941 struct wl_surface *wl_surface, struct wl_output *output)
3942{
Rob Bradford7507b572012-05-15 17:55:34 +01003943 struct window *window = data;
3944 struct window_output *window_output;
3945 struct window_output *window_output_found = NULL;
3946
3947 wl_list_for_each(window_output, &window->window_output_list, link) {
3948 if (window_output->output->output == output) {
3949 window_output_found = window_output;
3950 break;
3951 }
3952 }
3953
3954 if (window_output_found) {
3955 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003956
3957 if (window->output_handler)
3958 window->output_handler(window, window_output->output,
3959 0, window->user_data);
3960
Rob Bradford7507b572012-05-15 17:55:34 +01003961 free(window_output_found);
3962 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003963}
3964
3965static const struct wl_surface_listener surface_listener = {
3966 surface_enter,
3967 surface_leave
3968};
3969
Pekka Paalanen4e373742013-02-13 16:17:13 +02003970static struct surface *
3971surface_create(struct window *window)
3972{
3973 struct display *display = window->display;
3974 struct surface *surface;
3975
3976 surface = calloc(1, sizeof *surface);
3977 if (!surface)
3978 return NULL;
3979
3980 surface->window = window;
3981 surface->surface = wl_compositor_create_surface(display->compositor);
3982 wl_surface_add_listener(surface->surface, &surface_listener, window);
3983
Pekka Paalanen35e82632013-04-25 13:57:48 +03003984 wl_list_insert(&window->subsurface_list, &surface->link);
3985
Pekka Paalanen4e373742013-02-13 16:17:13 +02003986 return surface;
3987}
3988
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003989static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003990window_create_internal(struct display *display,
3991 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003992{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003993 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003994 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003995
3996 window = malloc(sizeof *window);
3997 if (window == NULL)
3998 return NULL;
3999
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004000 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004001 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004002 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004003 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004004
4005 surface = surface_create(window);
4006 window->main_surface = surface;
4007
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004008 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004009 window->shell_surface =
4010 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004011 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004012 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004013
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004014 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004015 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004016 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004017
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004018 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004019#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004020 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004021#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004022 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004023#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004024 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004025 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004026
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004027 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004028 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004029 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004030
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004031 if (window->shell_surface) {
4032 wl_shell_surface_set_user_data(window->shell_surface, window);
4033 wl_shell_surface_add_listener(window->shell_surface,
4034 &shell_surface_listener, window);
4035 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004036
Rob Bradford7507b572012-05-15 17:55:34 +01004037 wl_list_init (&window->window_output_list);
4038
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004039 return window;
4040}
4041
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004042struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004043window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004044{
4045 struct window *window;
4046
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004047 window = window_create_internal(display, NULL, TYPE_NONE);
4048 if (!window)
4049 return NULL;
4050
4051 return window;
4052}
4053
4054struct window *
4055window_create_custom(struct display *display)
4056{
4057 struct window *window;
4058
4059 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004060 if (!window)
4061 return NULL;
4062
4063 return window;
4064}
4065
4066struct window *
4067window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004068 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004069{
4070 struct window *window;
4071
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004072 window = window_create_internal(parent->display,
4073 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004074 if (!window)
4075 return NULL;
4076
4077 window->x = x;
4078 window->y = y;
4079
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004080 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004081 wl_shell_surface_set_transient(
4082 window->shell_surface,
4083 window->parent->main_surface->surface,
4084 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004085
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004086 return window;
4087}
4088
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004089static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004090menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004091{
4092 int next;
4093
4094 next = (sy - 8) / 20;
4095 if (menu->current != next) {
4096 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004097 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004098 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004099}
4100
4101static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004102menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004103 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004104 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004105{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004106 struct menu *menu = data;
4107
4108 if (widget == menu->widget)
4109 menu_set_item(data, y);
4110
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004111 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004112}
4113
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004114static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004115menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004116 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004117{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004118 struct menu *menu = data;
4119
4120 if (widget == menu->widget)
4121 menu_set_item(data, y);
4122
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004123 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004124}
4125
4126static void
4127menu_leave_handler(struct widget *widget, struct input *input, void *data)
4128{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004129 struct menu *menu = data;
4130
4131 if (widget == menu->widget)
4132 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004133}
4134
4135static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004136menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004137 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004138 uint32_t button, enum wl_pointer_button_state state,
4139 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004140
4141{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004142 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004143
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004144 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4145 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004146 /* Either relase after press-drag-release or
4147 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004148 menu->func(menu->window->parent,
4149 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004150 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004151 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004152 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004153 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004154 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004155}
4156
4157static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004158menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004159{
4160 cairo_t *cr;
4161 const int32_t r = 3, margin = 3;
4162 struct menu *menu = data;
4163 int32_t width, height, i;
4164
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004165 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004166 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4167 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4168 cairo_paint(cr);
4169
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004170 width = widget->allocation.width;
4171 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004172 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004173
4174 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004175 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4176 cairo_fill(cr);
4177
4178 for (i = 0; i < menu->count; i++) {
4179 if (i == menu->current) {
4180 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4181 cairo_rectangle(cr, margin, i * 20 + margin,
4182 width - 2 * margin, 20);
4183 cairo_fill(cr);
4184 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4185 cairo_move_to(cr, 10, i * 20 + 16);
4186 cairo_show_text(cr, menu->entries[i]);
4187 } else {
4188 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4189 cairo_move_to(cr, 10, i * 20 + 16);
4190 cairo_show_text(cr, menu->entries[i]);
4191 }
4192 }
4193
4194 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004195}
4196
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004197void
4198window_show_menu(struct display *display,
4199 struct input *input, uint32_t time, struct window *parent,
4200 int32_t x, int32_t y,
4201 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004202{
4203 struct window *window;
4204 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004205 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004206
4207 menu = malloc(sizeof *menu);
4208 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004209 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004210
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004211 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004212 if (!window) {
4213 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004214 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004215 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004216
4217 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004218 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004219 menu->entries = entries;
4220 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004221 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004222 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004223 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004224 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004225 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004226 window->type = TYPE_MENU;
4227 window->x = x;
4228 window->y = y;
4229
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004230 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004231 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004232 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004233 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004234 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004235
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004236 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004237 widget_set_enter_handler(menu->widget, menu_enter_handler);
4238 widget_set_leave_handler(menu->widget, menu_leave_handler);
4239 widget_set_motion_handler(menu->widget, menu_motion_handler);
4240 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004241
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004242 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004243 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004244}
4245
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004246void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004247window_set_buffer_type(struct window *window, enum window_buffer_type type)
4248{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004249 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004250}
4251
Pekka Paalanen35e82632013-04-25 13:57:48 +03004252struct widget *
4253window_add_subsurface(struct window *window, void *data,
4254 enum subsurface_mode default_mode)
4255{
4256 struct widget *widget;
4257 struct surface *surface;
4258 struct wl_surface *parent;
4259 struct wl_subcompositor *subcompo = window->display->subcompositor;
4260
4261 if (!subcompo)
4262 return NULL;
4263
4264 surface = surface_create(window);
4265 widget = widget_create(window, surface, data);
4266 wl_list_init(&widget->link);
4267 surface->widget = widget;
4268
4269 parent = window->main_surface->surface;
4270 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4271 surface->surface,
4272 parent);
4273 surface->synchronized = 1;
4274
4275 switch (default_mode) {
4276 case SUBSURFACE_SYNCHRONIZED:
4277 surface->synchronized_default = 1;
4278 break;
4279 case SUBSURFACE_DESYNCHRONIZED:
4280 surface->synchronized_default = 0;
4281 break;
4282 default:
4283 assert(!"bad enum subsurface_mode");
4284 }
4285
4286 return widget;
4287}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004288
4289static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004290display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004291 struct wl_output *wl_output,
4292 int x, int y,
4293 int physical_width,
4294 int physical_height,
4295 int subpixel,
4296 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004297 const char *model,
4298 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004299{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004300 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004301
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004302 output->allocation.x = x;
4303 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004304 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004305}
4306
4307static void
4308display_handle_mode(void *data,
4309 struct wl_output *wl_output,
4310 uint32_t flags,
4311 int width,
4312 int height,
4313 int refresh)
4314{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004315 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004316 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004317
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004318 if (flags & WL_OUTPUT_MODE_CURRENT) {
4319 output->allocation.width = width;
4320 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004321 if (display->output_configure_handler)
4322 (*display->output_configure_handler)(
4323 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004324 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004325}
4326
4327static const struct wl_output_listener output_listener = {
4328 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004329 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004330};
4331
4332static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004333display_add_output(struct display *d, uint32_t id)
4334{
4335 struct output *output;
4336
4337 output = malloc(sizeof *output);
4338 if (output == NULL)
4339 return;
4340
4341 memset(output, 0, sizeof *output);
4342 output->display = d;
4343 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004344 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004345 wl_list_insert(d->output_list.prev, &output->link);
4346
4347 wl_output_add_listener(output->output, &output_listener, output);
4348}
4349
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004350static void
4351output_destroy(struct output *output)
4352{
4353 if (output->destroy_handler)
4354 (*output->destroy_handler)(output, output->user_data);
4355
4356 wl_output_destroy(output->output);
4357 wl_list_remove(&output->link);
4358 free(output);
4359}
4360
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004361void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004362display_set_global_handler(struct display *display,
4363 display_global_handler_t handler)
4364{
4365 struct global *global;
4366
4367 display->global_handler = handler;
4368 if (!handler)
4369 return;
4370
4371 wl_list_for_each(global, &display->global_list, link)
4372 display->global_handler(display,
4373 global->name, global->interface,
4374 global->version, display->user_data);
4375}
4376
4377void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004378display_set_output_configure_handler(struct display *display,
4379 display_output_handler_t handler)
4380{
4381 struct output *output;
4382
4383 display->output_configure_handler = handler;
4384 if (!handler)
4385 return;
4386
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004387 wl_list_for_each(output, &display->output_list, link) {
4388 if (output->allocation.width == 0 &&
4389 output->allocation.height == 0)
4390 continue;
4391
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004392 (*display->output_configure_handler)(output,
4393 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004394 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004395}
4396
4397void
4398output_set_user_data(struct output *output, void *data)
4399{
4400 output->user_data = data;
4401}
4402
4403void *
4404output_get_user_data(struct output *output)
4405{
4406 return output->user_data;
4407}
4408
4409void
4410output_set_destroy_handler(struct output *output,
4411 display_output_handler_t handler)
4412{
4413 output->destroy_handler = handler;
4414 /* FIXME: implement this, once we have way to remove outputs */
4415}
4416
4417void
Scott Moreau4e072362012-09-29 02:03:11 -06004418output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004419{
Scott Moreau4e072362012-09-29 02:03:11 -06004420 struct rectangle allocation = output->allocation;
4421
4422 switch (output->transform) {
4423 case WL_OUTPUT_TRANSFORM_90:
4424 case WL_OUTPUT_TRANSFORM_270:
4425 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4426 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4427 /* Swap width and height */
4428 allocation.width = output->allocation.height;
4429 allocation.height = output->allocation.width;
4430 break;
4431 }
4432
4433 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004434}
4435
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004436struct wl_output *
4437output_get_wl_output(struct output *output)
4438{
4439 return output->output;
4440}
4441
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004442enum wl_output_transform
4443output_get_transform(struct output *output)
4444{
4445 return output->transform;
4446}
4447
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004448static void
Daniel Stone97f68542012-05-30 16:32:01 +01004449fini_xkb(struct input *input)
4450{
4451 xkb_state_unref(input->xkb.state);
4452 xkb_map_unref(input->xkb.keymap);
4453}
4454
4455static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004456display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004457{
4458 struct input *input;
4459
4460 input = malloc(sizeof *input);
4461 if (input == NULL)
4462 return;
4463
4464 memset(input, 0, sizeof *input);
4465 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004466 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004467 input->pointer_focus = NULL;
4468 input->keyboard_focus = NULL;
4469 wl_list_insert(d->input_list.prev, &input->link);
4470
Daniel Stone37816df2012-05-16 18:45:18 +01004471 wl_seat_add_listener(input->seat, &seat_listener, input);
4472 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004473
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004474 input->data_device =
4475 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004476 input->seat);
4477 wl_data_device_add_listener(input->data_device, &data_device_listener,
4478 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004479
4480 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004481
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004482 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4483 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004484 input->repeat_task.run = keyboard_repeat_func;
4485 display_watch_fd(d, input->repeat_timer_fd,
4486 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004487}
4488
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004489static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004490input_destroy(struct input *input)
4491{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004492 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004493 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004494
4495 if (input->drag_offer)
4496 data_offer_destroy(input->drag_offer);
4497
4498 if (input->selection_offer)
4499 data_offer_destroy(input->selection_offer);
4500
4501 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004502 fini_xkb(input);
4503
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004504 wl_surface_destroy(input->pointer_surface);
4505
Pekka Paalanene1207c72011-12-16 12:02:09 +02004506 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004507 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004508 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004509 free(input);
4510}
4511
4512static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004513init_workspace_manager(struct display *d, uint32_t id)
4514{
4515 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004516 wl_registry_bind(d->registry, id,
4517 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004518 if (d->workspace_manager != NULL)
4519 workspace_manager_add_listener(d->workspace_manager,
4520 &workspace_manager_listener,
4521 d);
4522}
4523
4524static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004525registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4526 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004527{
4528 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004529 struct global *global;
4530
4531 global = malloc(sizeof *global);
4532 global->name = id;
4533 global->interface = strdup(interface);
4534 global->version = version;
4535 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004536
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004537 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004538 d->compositor = wl_registry_bind(registry, id,
4539 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004540 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004541 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004542 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004543 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004544 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004545 d->shell = wl_registry_bind(registry,
4546 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004547 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004548 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004549 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4550 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004551 wl_registry_bind(registry, id,
4552 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004553 } else if (strcmp(interface, "text_cursor_position") == 0) {
4554 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004555 wl_registry_bind(registry, id,
4556 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004557 } else if (strcmp(interface, "workspace_manager") == 0) {
4558 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004559 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4560 d->subcompositor =
4561 wl_registry_bind(registry, id,
4562 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004563 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004564
4565 if (d->global_handler)
4566 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004567}
4568
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004569static void
4570registry_handle_global_remove(void *data, struct wl_registry *registry,
4571 uint32_t name)
4572{
4573 struct display *d = data;
4574 struct global *global;
4575 struct global *tmp;
4576
4577 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4578 if (global->name != name)
4579 continue;
4580
4581 /* XXX: Should destroy bound globals, and call
4582 * the counterpart of display::global_handler
4583 */
4584 wl_list_remove(&global->link);
4585 free(global->interface);
4586 free(global);
4587 }
4588}
4589
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004590void *
4591display_bind(struct display *display, uint32_t name,
4592 const struct wl_interface *interface, uint32_t version)
4593{
4594 return wl_registry_bind(display->registry, name, interface, version);
4595}
4596
4597static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004598 registry_handle_global,
4599 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004600};
4601
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004602#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004603static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004604init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004605{
4606 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004607 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004608
Rob Clark6396ed32012-03-11 19:48:41 -05004609#ifdef USE_CAIRO_GLESV2
4610# define GL_BIT EGL_OPENGL_ES2_BIT
4611#else
4612# define GL_BIT EGL_OPENGL_BIT
4613#endif
4614
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004615 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004616 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004617 EGL_RED_SIZE, 1,
4618 EGL_GREEN_SIZE, 1,
4619 EGL_BLUE_SIZE, 1,
4620 EGL_ALPHA_SIZE, 1,
4621 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004622 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004623 EGL_NONE
4624 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004625
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004626#ifdef USE_CAIRO_GLESV2
4627 static const EGLint context_attribs[] = {
4628 EGL_CONTEXT_CLIENT_VERSION, 2,
4629 EGL_NONE
4630 };
4631 EGLint api = EGL_OPENGL_ES_API;
4632#else
4633 EGLint *context_attribs = NULL;
4634 EGLint api = EGL_OPENGL_API;
4635#endif
4636
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004637 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004638 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004639 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004640 return -1;
4641 }
4642
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004643 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004644 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004645 return -1;
4646 }
4647
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004648 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4649 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004650 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004651 return -1;
4652 }
4653
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004654 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004655 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004656 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004657 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004658 return -1;
4659 }
4660
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004661 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004662 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004663 return -1;
4664 }
4665
Benjamin Franzke0c991632011-09-27 21:57:31 +02004666 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4667 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004668 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004669 return -1;
4670 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004671
4672 return 0;
4673}
4674
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004675static void
4676fini_egl(struct display *display)
4677{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004678 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004679
4680 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4681 EGL_NO_CONTEXT);
4682
4683 eglTerminate(display->dpy);
4684 eglReleaseThread();
4685}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004686#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004687
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004688static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004689init_dummy_surface(struct display *display)
4690{
4691 int len;
4692 void *data;
4693
4694 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4695 data = malloc(len);
4696 display->dummy_surface =
4697 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4698 1, 1, len);
4699 display->dummy_surface_data = data;
4700}
4701
4702static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004703handle_display_data(struct task *task, uint32_t events)
4704{
4705 struct display *display =
4706 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004707 struct epoll_event ep;
4708 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004709
4710 display->display_fd_events = events;
4711
4712 if (events & EPOLLERR || events & EPOLLHUP) {
4713 display_exit(display);
4714 return;
4715 }
4716
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004717 if (events & EPOLLIN) {
4718 ret = wl_display_dispatch(display->display);
4719 if (ret == -1) {
4720 display_exit(display);
4721 return;
4722 }
4723 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004724
4725 if (events & EPOLLOUT) {
4726 ret = wl_display_flush(display->display);
4727 if (ret == 0) {
4728 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4729 ep.data.ptr = &display->display_task;
4730 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4731 display->display_fd, &ep);
4732 } else if (ret == -1 && errno != EAGAIN) {
4733 display_exit(display);
4734 return;
4735 }
4736 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004737}
4738
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004739static void
4740log_handler(const char *format, va_list args)
4741{
4742 vfprintf(stderr, format, args);
4743}
4744
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004745struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004746display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004747{
4748 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004749
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004750 wl_log_set_handler_client(log_handler);
4751
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004752 d = malloc(sizeof *d);
4753 if (d == NULL)
4754 return NULL;
4755
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004756 memset(d, 0, sizeof *d);
4757
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004758 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004759 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004760 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004761 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004762 return NULL;
4763 }
4764
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004765 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004766 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004767 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004768 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4769 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004770
4771 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004772 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004773 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004774 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004775
Daniel Stone97f68542012-05-30 16:32:01 +01004776 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004777 if (d->xkb_context == NULL) {
4778 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004779 return NULL;
4780 }
4781
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004782 d->workspace = 0;
4783 d->workspace_count = 1;
4784
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004785 d->registry = wl_display_get_registry(d->display);
4786 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004787
4788 if (wl_display_dispatch(d->display) < 0) {
4789 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4790 return NULL;
4791 }
4792
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004793#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004794 if (init_egl(d) < 0)
4795 fprintf(stderr, "EGL does not seem to work, "
4796 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004797#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004798
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004799 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004800
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004801 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004802
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004803 wl_list_init(&d->window_list);
4804
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004805 init_dummy_surface(d);
4806
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004807 return d;
4808}
4809
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004810static void
4811display_destroy_outputs(struct display *display)
4812{
4813 struct output *tmp;
4814 struct output *output;
4815
4816 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4817 output_destroy(output);
4818}
4819
Pekka Paalanene1207c72011-12-16 12:02:09 +02004820static void
4821display_destroy_inputs(struct display *display)
4822{
4823 struct input *tmp;
4824 struct input *input;
4825
4826 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4827 input_destroy(input);
4828}
4829
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004830void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004831display_destroy(struct display *display)
4832{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004833 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004834 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4835 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004836
4837 if (!wl_list_empty(&display->deferred_list))
4838 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4839
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004840 cairo_surface_destroy(display->dummy_surface);
4841 free(display->dummy_surface_data);
4842
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004843 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004844 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004845
Daniel Stone97f68542012-05-30 16:32:01 +01004846 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004847
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004848 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004849 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004850
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004851#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004852 if (display->argb_device)
4853 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004854#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004855
Pekka Paalanen35e82632013-04-25 13:57:48 +03004856 if (display->subcompositor)
4857 wl_subcompositor_destroy(display->subcompositor);
4858
Pekka Paalanenc2052982011-12-16 11:41:32 +02004859 if (display->shell)
4860 wl_shell_destroy(display->shell);
4861
4862 if (display->shm)
4863 wl_shm_destroy(display->shm);
4864
4865 if (display->data_device_manager)
4866 wl_data_device_manager_destroy(display->data_device_manager);
4867
4868 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004869 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004870
4871 close(display->epoll_fd);
4872
U. Artie Eoff44874d92012-10-02 21:12:35 -07004873 if (!(display->display_fd_events & EPOLLERR) &&
4874 !(display->display_fd_events & EPOLLHUP))
4875 wl_display_flush(display->display);
4876
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004877 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004878 free(display);
4879}
4880
4881void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004882display_set_user_data(struct display *display, void *data)
4883{
4884 display->user_data = data;
4885}
4886
4887void *
4888display_get_user_data(struct display *display)
4889{
4890 return display->user_data;
4891}
4892
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004893struct wl_display *
4894display_get_display(struct display *display)
4895{
4896 return display->display;
4897}
4898
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004899struct output *
4900display_get_output(struct display *display)
4901{
4902 return container_of(display->output_list.next, struct output, link);
4903}
4904
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004905struct wl_compositor *
4906display_get_compositor(struct display *display)
4907{
4908 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004909}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004910
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004911uint32_t
4912display_get_serial(struct display *display)
4913{
4914 return display->serial;
4915}
4916
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004917EGLDisplay
4918display_get_egl_display(struct display *d)
4919{
4920 return d->dpy;
4921}
4922
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004923struct wl_data_source *
4924display_create_data_source(struct display *display)
4925{
4926 return wl_data_device_manager_create_data_source(display->data_device_manager);
4927}
4928
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004929EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004930display_get_argb_egl_config(struct display *d)
4931{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004932 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004933}
4934
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004935struct wl_shell *
4936display_get_shell(struct display *display)
4937{
4938 return display->shell;
4939}
4940
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004941int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004942display_acquire_window_surface(struct display *display,
4943 struct window *window,
4944 EGLContext ctx)
4945{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004946 struct surface *surface = window->main_surface;
4947
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004948 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004949 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004950
Pekka Paalanen6f41b072013-02-20 13:39:17 +02004951 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004952 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004953}
4954
4955void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004956display_release_window_surface(struct display *display,
4957 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004958{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004959 struct surface *surface = window->main_surface;
4960
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004961 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004962 return;
4963
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004964 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004965}
4966
4967void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004968display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004969{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004970 wl_list_insert(&display->deferred_list, &task->link);
4971}
4972
4973void
4974display_watch_fd(struct display *display,
4975 int fd, uint32_t events, struct task *task)
4976{
4977 struct epoll_event ep;
4978
4979 ep.events = events;
4980 ep.data.ptr = task;
4981 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4982}
4983
4984void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004985display_unwatch_fd(struct display *display, int fd)
4986{
4987 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4988}
4989
4990void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004991display_run(struct display *display)
4992{
4993 struct task *task;
4994 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004995 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004996
Pekka Paalanen826d7952011-12-15 10:14:07 +02004997 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004998 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004999 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005000 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005001 struct task, link);
5002 wl_list_remove(&task->link);
5003 task->run(task, 0);
5004 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005005
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005006 wl_display_dispatch_pending(display->display);
5007
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005008 if (!display->running)
5009 break;
5010
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005011 ret = wl_display_flush(display->display);
5012 if (ret < 0 && errno == EAGAIN) {
5013 ep[0].events =
5014 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5015 ep[0].data.ptr = &display->display_task;
5016
5017 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5018 display->display_fd, &ep[0]);
5019 } else if (ret < 0) {
5020 break;
5021 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005022
5023 count = epoll_wait(display->epoll_fd,
5024 ep, ARRAY_LENGTH(ep), -1);
5025 for (i = 0; i < count; i++) {
5026 task = ep[i].data.ptr;
5027 task->run(task, ep[i].events);
5028 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005029 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005030}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005031
5032void
5033display_exit(struct display *display)
5034{
5035 display->running = 0;
5036}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005037
5038void
5039keysym_modifiers_add(struct wl_array *modifiers_map,
5040 const char *name)
5041{
5042 size_t len = strlen(name) + 1;
5043 char *p;
5044
5045 p = wl_array_add(modifiers_map, len);
5046
5047 if (p == NULL)
5048 return;
5049
5050 strncpy(p, name, len);
5051}
5052
5053static xkb_mod_index_t
5054keysym_modifiers_get_index(struct wl_array *modifiers_map,
5055 const char *name)
5056{
5057 xkb_mod_index_t index = 0;
5058 char *p = modifiers_map->data;
5059
5060 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5061 if (strcmp(p, name) == 0)
5062 return index;
5063
5064 index++;
5065 p += strlen(p) + 1;
5066 }
5067
5068 return XKB_MOD_INVALID;
5069}
5070
5071xkb_mod_mask_t
5072keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5073 const char *name)
5074{
5075 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5076
5077 if (index == XKB_MOD_INVALID)
5078 return XKB_MOD_INVALID;
5079
5080 return 1 << index;
5081}