blob: 627f1e82550f527f7ec3c600342a3f62cbaa0e34 [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 Paalanen97777442013-05-22 10:20:05 +0300423/* #define DEBUG */
424
425#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300426
427static void
428debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
429__attribute__ ((format (printf, 4, 5)));
430
431static void
432debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
433{
434 va_list ap;
435 struct timeval tv;
436
437 gettimeofday(&tv, NULL);
438 fprintf(stderr, "%8ld.%03ld ",
439 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
440
441 if (proxy)
442 fprintf(stderr, "%s@%d ",
443 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
444
445 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
446 fprintf(stderr, "%s ", func);
447
448 va_start(ap, fmt);
449 vfprintf(stderr, fmt, ap);
450 va_end(ap);
451}
452
453#define DBG(fmt, ...) \
454 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
455
456#define DBG_OBJ(obj, fmt, ...) \
457 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
458
459#else
460
461#define DBG(...) do {} while (0)
462#define DBG_OBJ(...) do {} while (0)
463
464#endif
465
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500466#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400467
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200468struct egl_window_surface {
469 struct toysurface base;
470 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100471 struct display *display;
472 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200473 struct wl_egl_window *egl_window;
474 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100475};
476
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200477static struct egl_window_surface *
478to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100479{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200480 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100481}
482
483static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200484egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200485 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100486{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200487 struct egl_window_surface *surface = to_egl_window_surface(base);
488
489 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
490 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
491
492 return cairo_surface_reference(surface->cairo_surface);
493}
494
495static void
496egl_window_surface_swap(struct toysurface *base,
497 struct rectangle *server_allocation)
498{
499 struct egl_window_surface *surface = to_egl_window_surface(base);
500
501 cairo_gl_surface_swapbuffers(surface->cairo_surface);
502 wl_egl_window_get_attached_size(surface->egl_window,
503 &server_allocation->width,
504 &server_allocation->height);
505}
506
507static int
508egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
509{
510 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200511 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100512
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200513 device = cairo_surface_get_device(surface->cairo_surface);
514 if (!device)
515 return -1;
516
517 if (!ctx) {
518 if (device == surface->display->argb_device)
519 ctx = surface->display->argb_ctx;
520 else
521 assert(0);
522 }
523
524 cairo_device_flush(device);
525 cairo_device_acquire(device);
526 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
527 surface->egl_surface, ctx))
528 fprintf(stderr, "failed to make surface current\n");
529
530 return 0;
531}
532
533static void
534egl_window_surface_release(struct toysurface *base)
535{
536 struct egl_window_surface *surface = to_egl_window_surface(base);
537 cairo_device_t *device;
538
539 device = cairo_surface_get_device(surface->cairo_surface);
540 if (!device)
541 return;
542
543 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
544 surface->display->argb_ctx))
545 fprintf(stderr, "failed to make context current\n");
546
547 cairo_device_release(device);
548}
549
550static void
551egl_window_surface_destroy(struct toysurface *base)
552{
553 struct egl_window_surface *surface = to_egl_window_surface(base);
554 struct display *d = surface->display;
555
556 cairo_surface_destroy(surface->cairo_surface);
557 eglDestroySurface(d->dpy, surface->egl_surface);
558 wl_egl_window_destroy(surface->egl_window);
559 surface->surface = NULL;
560
561 free(surface);
562}
563
564static struct toysurface *
565egl_window_surface_create(struct display *display,
566 struct wl_surface *wl_surface,
567 uint32_t flags,
568 struct rectangle *rectangle)
569{
570 struct egl_window_surface *surface;
571
Pekka Paalanenb3627362012-11-19 17:16:00 +0200572 if (display->dpy == EGL_NO_DISPLAY)
573 return NULL;
574
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200575 surface = calloc(1, sizeof *surface);
576 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100577 return NULL;
578
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200579 surface->base.prepare = egl_window_surface_prepare;
580 surface->base.swap = egl_window_surface_swap;
581 surface->base.acquire = egl_window_surface_acquire;
582 surface->base.release = egl_window_surface_release;
583 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100584
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200585 surface->display = display;
586 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400587
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200588 surface->egl_window = wl_egl_window_create(surface->surface,
589 rectangle->width,
590 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100591
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200592 surface->egl_surface = eglCreateWindowSurface(display->dpy,
593 display->argb_config,
594 surface->egl_window,
595 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100596
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200597 surface->cairo_surface =
598 cairo_gl_surface_create_for_egl(display->argb_device,
599 surface->egl_surface,
600 rectangle->width,
601 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100602
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200603 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100604}
605
Pekka Paalanenb3627362012-11-19 17:16:00 +0200606#else
607
608static struct toysurface *
609egl_window_surface_create(struct display *display,
610 struct wl_surface *wl_surface,
611 uint32_t flags,
612 struct rectangle *rectangle)
613{
614 return NULL;
615}
616
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617#endif
618
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200619struct shm_surface_data {
620 struct wl_buffer *buffer;
621 struct shm_pool *pool;
622};
623
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624struct wl_buffer *
625display_get_buffer_for_surface(struct display *display,
626 cairo_surface_t *surface)
627{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200628 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200630 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631
632 return data->buffer;
633}
634
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500635static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700636shm_pool_destroy(struct shm_pool *pool);
637
638static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400639shm_surface_data_destroy(void *p)
640{
641 struct shm_surface_data *data = p;
642
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200643 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700644 if (data->pool)
645 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300646
647 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400648}
649
Kristian Høgsberg16626282012-04-03 11:21:27 -0400650static struct wl_shm_pool *
651make_shm_pool(struct display *display, int size, void **data)
652{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400653 struct wl_shm_pool *pool;
654 int fd;
655
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300656 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400657 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300658 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
659 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400660 return NULL;
661 }
662
663 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400664 if (*data == MAP_FAILED) {
665 fprintf(stderr, "mmap failed: %m\n");
666 close(fd);
667 return NULL;
668 }
669
670 pool = wl_shm_create_pool(display->shm, fd, size);
671
672 close(fd);
673
674 return pool;
675}
676
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700677static struct shm_pool *
678shm_pool_create(struct display *display, size_t size)
679{
680 struct shm_pool *pool = malloc(sizeof *pool);
681
682 if (!pool)
683 return NULL;
684
685 pool->pool = make_shm_pool(display, size, &pool->data);
686 if (!pool->pool) {
687 free(pool);
688 return NULL;
689 }
690
691 pool->size = size;
692 pool->used = 0;
693
694 return pool;
695}
696
697static void *
698shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
699{
700 if (pool->used + size > pool->size)
701 return NULL;
702
703 *offset = pool->used;
704 pool->used += size;
705
706 return (char *) pool->data + *offset;
707}
708
709/* destroy the pool. this does not unmap the memory though */
710static void
711shm_pool_destroy(struct shm_pool *pool)
712{
713 munmap(pool->data, pool->size);
714 wl_shm_pool_destroy(pool->pool);
715 free(pool);
716}
717
718/* Start allocating from the beginning of the pool again */
719static void
720shm_pool_reset(struct shm_pool *pool)
721{
722 pool->used = 0;
723}
724
725static int
726data_length_for_shm_surface(struct rectangle *rect)
727{
728 int stride;
729
730 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
731 rect->width);
732 return stride * rect->height;
733}
734
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500735static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700736display_create_shm_surface_from_pool(struct display *display,
737 struct rectangle *rectangle,
738 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400739{
740 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400741 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400742 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700743 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400744 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400745
746 data = malloc(sizeof *data);
747 if (data == NULL)
748 return NULL;
749
750 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
751 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700752 length = stride * rectangle->height;
753 data->pool = NULL;
754 map = shm_pool_allocate(pool, length, &offset);
755
756 if (!map) {
757 free(data);
758 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400759 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400760
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400761 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400762 CAIRO_FORMAT_ARGB32,
763 rectangle->width,
764 rectangle->height,
765 stride);
766
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200767 cairo_surface_set_user_data(surface, &shm_surface_data_key,
768 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400769
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400770 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500771 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400772 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500773 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400774
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200775 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
776 rectangle->width,
777 rectangle->height,
778 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400779
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700780 return surface;
781}
782
783static cairo_surface_t *
784display_create_shm_surface(struct display *display,
785 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200786 struct shm_pool *alternate_pool,
787 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700788{
789 struct shm_surface_data *data;
790 struct shm_pool *pool;
791 cairo_surface_t *surface;
792
Pekka Paalanen99436862012-11-19 17:15:59 +0200793 if (alternate_pool) {
794 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700795 surface = display_create_shm_surface_from_pool(display,
796 rectangle,
797 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200798 alternate_pool);
799 if (surface) {
800 data = cairo_surface_get_user_data(surface,
801 &shm_surface_data_key);
802 goto out;
803 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700804 }
805
806 pool = shm_pool_create(display,
807 data_length_for_shm_surface(rectangle));
808 if (!pool)
809 return NULL;
810
811 surface =
812 display_create_shm_surface_from_pool(display, rectangle,
813 flags, pool);
814
815 if (!surface) {
816 shm_pool_destroy(pool);
817 return NULL;
818 }
819
820 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200821 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700822 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823
Pekka Paalanen99436862012-11-19 17:15:59 +0200824out:
825 if (data_ret)
826 *data_ret = data;
827
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400828 return surface;
829}
830
nobled7b87cb02011-02-01 18:51:47 +0000831static int
832check_size(struct rectangle *rect)
833{
834 if (rect->width && rect->height)
835 return 0;
836
837 fprintf(stderr, "tried to create surface of "
838 "width: %d, height: %d\n", rect->width, rect->height);
839 return -1;
840}
841
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400842cairo_surface_t *
843display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100844 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400845 struct rectangle *rectangle,
846 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847{
nobled7b87cb02011-02-01 18:51:47 +0000848 if (check_size(rectangle) < 0)
849 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200850
851 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200852 return display_create_shm_surface(display, rectangle, flags,
853 NULL, NULL);
854}
855
Pekka Paalanena4eda732012-11-19 17:16:02 +0200856struct shm_surface_leaf {
857 cairo_surface_t *cairo_surface;
858 /* 'data' is automatically destroyed, when 'cairo_surface' is */
859 struct shm_surface_data *data;
860
861 struct shm_pool *resize_pool;
862 int busy;
863};
864
865static void
866shm_surface_leaf_release(struct shm_surface_leaf *leaf)
867{
868 if (leaf->cairo_surface)
869 cairo_surface_destroy(leaf->cairo_surface);
870 /* leaf->data already destroyed via cairo private */
871
872 if (leaf->resize_pool)
873 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200874
875 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200876}
877
Pekka Paalanenaef02542013-04-25 13:57:47 +0300878#define MAX_LEAVES 3
879
Pekka Paalanen99436862012-11-19 17:15:59 +0200880struct shm_surface {
881 struct toysurface base;
882 struct display *display;
883 struct wl_surface *surface;
884 uint32_t flags;
885 int dx, dy;
886
Pekka Paalanenaef02542013-04-25 13:57:47 +0300887 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200888 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200889};
890
891static struct shm_surface *
892to_shm_surface(struct toysurface *base)
893{
894 return container_of(base, struct shm_surface, base);
895}
896
Pekka Paalanena4eda732012-11-19 17:16:02 +0200897static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300898shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
899{
900#ifdef DEBUG
901 struct shm_surface_leaf *leaf;
902 char bufs[MAX_LEAVES + 1];
903 int i;
904
905 for (i = 0; i < MAX_LEAVES; i++) {
906 leaf = &surface->leaf[i];
907
908 if (leaf->busy)
909 bufs[i] = 'b';
910 else if (leaf->cairo_surface)
911 bufs[i] = 'a';
912 else
913 bufs[i] = ' ';
914 }
915
916 bufs[MAX_LEAVES] = '\0';
917 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
918#endif
919}
920
921static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200922shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
923{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200924 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300925 struct shm_surface_leaf *leaf;
926 int i;
927 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300928
929 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200930
Pekka Paalanenaef02542013-04-25 13:57:47 +0300931 for (i = 0; i < MAX_LEAVES; i++) {
932 leaf = &surface->leaf[i];
933 if (leaf->data && leaf->data->buffer == buffer) {
934 leaf->busy = 0;
935 break;
936 }
937 }
938 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200939
Pekka Paalanenaef02542013-04-25 13:57:47 +0300940 /* Leave one free leaf with storage, release others */
941 free_found = 0;
942 for (i = 0; i < MAX_LEAVES; i++) {
943 leaf = &surface->leaf[i];
944
945 if (!leaf->cairo_surface || leaf->busy)
946 continue;
947
948 if (!free_found)
949 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300950 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300951 shm_surface_leaf_release(leaf);
952 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300953
Pekka Paalanen97777442013-05-22 10:20:05 +0300954 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200955}
956
957static const struct wl_buffer_listener shm_surface_buffer_listener = {
958 shm_surface_buffer_release
959};
960
Pekka Paalanen99436862012-11-19 17:15:59 +0200961static cairo_surface_t *
962shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200963 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200964{
Pekka Paalanenec076692012-11-30 13:37:27 +0200965 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200966 struct shm_surface *surface = to_shm_surface(base);
967 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanenaef02542013-04-25 13:57:47 +0300968 struct shm_surface_leaf *leaf = NULL;
969 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200970
971 surface->dx = dx;
972 surface->dy = dy;
973
Pekka Paalanenaef02542013-04-25 13:57:47 +0300974 /* pick a free buffer, preferrably one that already has storage */
975 for (i = 0; i < MAX_LEAVES; i++) {
976 if (surface->leaf[i].busy)
977 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200978
Pekka Paalanenaef02542013-04-25 13:57:47 +0300979 if (!leaf || surface->leaf[i].cairo_surface)
980 leaf = &surface->leaf[i];
981 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300982 DBG_OBJ(surface->surface, "pick leaf %d\n",
983 (int)(leaf - &surface->leaf[0]));
984
Pekka Paalanenaef02542013-04-25 13:57:47 +0300985 if (!leaf) {
986 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +0200987 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +0300988 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200989 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200990 }
991
Pekka Paalanena4eda732012-11-19 17:16:02 +0200992 if (!resize_hint && leaf->resize_pool) {
993 cairo_surface_destroy(leaf->cairo_surface);
994 leaf->cairo_surface = NULL;
995 shm_pool_destroy(leaf->resize_pool);
996 leaf->resize_pool = NULL;
997 }
998
999 if (leaf->cairo_surface &&
1000 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1001 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001002 goto out;
1003
Pekka Paalanena4eda732012-11-19 17:16:02 +02001004 if (leaf->cairo_surface)
1005 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001006
Pekka Paalanena4eda732012-11-19 17:16:02 +02001007 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001008 /* Create a big pool to allocate from, while continuously
1009 * resizing. Mmapping a new pool in the server
1010 * is relatively expensive, so reusing a pool performs
1011 * better, but may temporarily reserve unneeded memory.
1012 */
1013 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001014 leaf->resize_pool = shm_pool_create(surface->display,
1015 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001016 }
1017
Pekka Paalanena4eda732012-11-19 17:16:02 +02001018 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001019 display_create_shm_surface(surface->display, &rect,
1020 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001021 leaf->resize_pool,
1022 &leaf->data);
1023 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001024 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001025
1026out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001027 surface->current = leaf;
1028
1029 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001030}
1031
1032static void
1033shm_surface_swap(struct toysurface *base,
1034 struct rectangle *server_allocation)
1035{
1036 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001037 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001038
1039 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001040 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001041 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001042 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001043
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001045 surface->dx, surface->dy);
1046 wl_surface_damage(surface->surface, 0, 0,
1047 server_allocation->width, server_allocation->height);
1048 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049
Pekka Paalanen71233882013-04-25 13:57:53 +03001050 DBG_OBJ(surface->surface, "leaf %d busy\n",
1051 (int)(leaf - &surface->leaf[0]));
1052
Pekka Paalanena4eda732012-11-19 17:16:02 +02001053 leaf->busy = 1;
1054 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001055}
1056
1057static int
1058shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1059{
1060 return -1;
1061}
1062
1063static void
1064shm_surface_release(struct toysurface *base)
1065{
1066}
1067
1068static void
1069shm_surface_destroy(struct toysurface *base)
1070{
1071 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001072 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001073
Pekka Paalanenaef02542013-04-25 13:57:47 +03001074 for (i = 0; i < MAX_LEAVES; i++)
1075 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
1077 free(surface);
1078}
1079
1080static struct toysurface *
1081shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1082 uint32_t flags, struct rectangle *rectangle)
1083{
1084 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001085 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001086
1087 surface = calloc(1, sizeof *surface);
1088 if (!surface)
1089 return NULL;
1090
1091 surface->base.prepare = shm_surface_prepare;
1092 surface->base.swap = shm_surface_swap;
1093 surface->base.acquire = shm_surface_acquire;
1094 surface->base.release = shm_surface_release;
1095 surface->base.destroy = shm_surface_destroy;
1096
1097 surface->display = display;
1098 surface->surface = wl_surface;
1099 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001100
1101 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001102}
1103
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001104/*
1105 * The following correspondences between file names and cursors was copied
1106 * from: https://bugs.kde.org/attachment.cgi?id=67313
1107 */
1108
1109static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001110 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001111 "sw-resize",
1112 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001113};
1114
1115static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001116 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001117 "se-resize",
1118 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001119};
1120
1121static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001122 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001123 "s-resize",
1124 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001125};
1126
1127static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001128 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001129 "closedhand",
1130 "208530c400c041818281048008011002"
1131};
1132
1133static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001134 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001135 "default",
1136 "top_left_arrow",
1137 "left-arrow"
1138};
1139
1140static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001141 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001142 "w-resize",
1143 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001144};
1145
1146static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001147 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001148 "e-resize",
1149 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001150};
1151
1152static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001153 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001154 "nw-resize",
1155 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001156};
1157
1158static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001159 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001160 "ne-resize",
1161 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001162};
1163
1164static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001165 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001166 "n-resize",
1167 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001168};
1169
1170static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001171 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001172 "ibeam",
1173 "text"
1174};
1175
1176static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001177 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001178 "pointer",
1179 "pointing_hand",
1180 "e29285e634086352946a0e7090d73106"
1181};
1182
1183static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001184 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001185 "wait",
1186 "0426c94ea35c87780ff01dc239897213"
1187};
1188
1189struct cursor_alternatives {
1190 const char **names;
1191 size_t count;
1192};
1193
1194static const struct cursor_alternatives cursors[] = {
1195 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1196 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1197 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1198 {grabbings, ARRAY_LENGTH(grabbings)},
1199 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1200 {left_sides, ARRAY_LENGTH(left_sides)},
1201 {right_sides, ARRAY_LENGTH(right_sides)},
1202 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1203 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1204 {top_sides, ARRAY_LENGTH(top_sides)},
1205 {xterms, ARRAY_LENGTH(xterms)},
1206 {hand1s, ARRAY_LENGTH(hand1s)},
1207 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208};
1209
1210static void
1211create_cursors(struct display *display)
1212{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001213 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001214 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001215 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001216 unsigned int i, j;
1217 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001218 struct config_key shell_keys[] = {
1219 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001220 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001221 };
1222 struct config_section cs[] = {
1223 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1224 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001225
Ossama Othmana50e6e42013-05-14 09:48:26 -07001226 config_fd = open_config_file("weston.ini");
1227 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1228 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001229
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001230 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001231 display->cursors =
1232 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1233
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001234 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235 cursor = NULL;
1236 for (j = 0; !cursor && j < cursors[i].count; ++j)
1237 cursor = wl_cursor_theme_get_cursor(
1238 display->cursor_theme, cursors[i].names[j]);
1239
1240 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001241 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242 cursors[i].names[0]);
1243
1244 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001245 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001246}
1247
1248static void
1249destroy_cursors(struct display *display)
1250{
1251 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001252 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001253}
1254
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001255struct wl_cursor_image *
1256display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001260 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261}
1262
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001263static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001264surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001265{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001266 if (!surface->cairo_surface)
1267 return;
1268
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001269 if (surface->opaque_region) {
1270 wl_surface_set_opaque_region(surface->surface,
1271 surface->opaque_region);
1272 wl_region_destroy(surface->opaque_region);
1273 surface->opaque_region = NULL;
1274 }
1275
1276 if (surface->input_region) {
1277 wl_surface_set_input_region(surface->surface,
1278 surface->input_region);
1279 wl_region_destroy(surface->input_region);
1280 surface->input_region = NULL;
1281 }
1282
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001283 surface->toysurface->swap(surface->toysurface,
1284 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001285
Pekka Paalanen89dee002013-02-13 16:17:20 +02001286 cairo_surface_destroy(surface->cairo_surface);
1287 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001288}
1289
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001290int
1291window_has_focus(struct window *window)
1292{
1293 return window->focus_count > 0;
1294}
1295
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001296static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001297window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001298{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001299 struct surface *surface;
1300
Pekka Paalanen89dee002013-02-13 16:17:20 +02001301 if (window->type == TYPE_NONE) {
1302 window->type = TYPE_TOPLEVEL;
1303 if (window->shell_surface)
1304 wl_shell_surface_set_toplevel(window->shell_surface);
1305 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001306
Pekka Paalanen35e82632013-04-25 13:57:48 +03001307 wl_list_for_each(surface, &window->subsurface_list, link) {
1308 if (surface == window->main_surface)
1309 continue;
1310
1311 surface_flush(surface);
1312 }
1313
Pekka Paalanen89dee002013-02-13 16:17:20 +02001314 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001315}
1316
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001317struct display *
1318window_get_display(struct window *window)
1319{
1320 return window->display;
1321}
1322
Pekka Paalanen89dee002013-02-13 16:17:20 +02001323static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001324surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001325{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001326 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001327 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001328
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001329 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001330 case WL_OUTPUT_TRANSFORM_90:
1331 case WL_OUTPUT_TRANSFORM_270:
1332 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1333 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001334 allocation.width = surface->allocation.height;
1335 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001336 break;
1337 default:
1338 break;
1339 }
1340
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001341 if (!surface->toysurface && display->dpy &&
1342 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001343 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001344 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001345 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001346 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001347 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001348 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001349
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001350 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001351 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352 surface->surface,
1353 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001354
Pekka Paalanen89dee002013-02-13 16:17:20 +02001355 surface->cairo_surface = surface->toysurface->prepare(
1356 surface->toysurface, dx, dy,
1357 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001358}
1359
1360static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001361window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001362{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001363 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001364 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001365 int dx = 0;
1366 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001367
Pekka Paalanenec076692012-11-30 13:37:27 +02001368 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001369 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001370
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001371 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1372 dx = surface->server_allocation.width -
1373 surface->allocation.width;
1374
1375 if (window->resize_edges & WINDOW_RESIZING_TOP)
1376 dy = surface->server_allocation.height -
1377 surface->allocation.height;
1378
1379 window->resize_edges = 0;
1380
Pekka Paalanen89dee002013-02-13 16:17:20 +02001381 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001382}
1383
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001384int
1385window_get_buffer_transform(struct window *window)
1386{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001387 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001388}
1389
1390void
1391window_set_buffer_transform(struct window *window,
1392 enum wl_output_transform transform)
1393{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001394 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001395 wl_surface_set_buffer_transform(window->main_surface->surface,
1396 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001397}
1398
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001399static void frame_destroy(struct frame *frame);
1400
Pekka Paalanen4e373742013-02-13 16:17:13 +02001401static void
1402surface_destroy(struct surface *surface)
1403{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001404 if (surface->frame_cb)
1405 wl_callback_destroy(surface->frame_cb);
1406
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001407 if (surface->input_region)
1408 wl_region_destroy(surface->input_region);
1409
1410 if (surface->opaque_region)
1411 wl_region_destroy(surface->opaque_region);
1412
Pekka Paalanen35e82632013-04-25 13:57:48 +03001413 if (surface->subsurface)
1414 wl_subsurface_destroy(surface->subsurface);
1415
Pekka Paalanen4e373742013-02-13 16:17:13 +02001416 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001417
1418 if (surface->toysurface)
1419 surface->toysurface->destroy(surface->toysurface);
1420
Pekka Paalanen35e82632013-04-25 13:57:48 +03001421 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001422 free(surface);
1423}
1424
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001425void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001426window_destroy(struct window *window)
1427{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001428 struct display *display = window->display;
1429 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001430 struct window_output *window_output;
1431 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001432
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001433 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001434
1435 wl_list_for_each(input, &display->input_list, link) {
1436 if (input->pointer_focus == window)
1437 input->pointer_focus = NULL;
1438 if (input->keyboard_focus == window)
1439 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001440 if (input->focus_widget &&
1441 input->focus_widget->window == window)
1442 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001443 }
1444
Rob Bradford7507b572012-05-15 17:55:34 +01001445 wl_list_for_each_safe(window_output, window_output_tmp,
1446 &window->window_output_list, link) {
1447 free (window_output);
1448 }
1449
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001450 if (window->frame)
1451 frame_destroy(window->frame);
1452
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001453 if (window->shell_surface)
1454 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001455
1456 surface_destroy(window->main_surface);
1457
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001458 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001459
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001460 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001461 free(window);
1462}
1463
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001464static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001465widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001466{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001467 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001468
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001469 wl_list_for_each(child, &widget->child_list, link) {
1470 target = widget_find_widget(child, x, y);
1471 if (target)
1472 return target;
1473 }
1474
1475 if (widget->allocation.x <= x &&
1476 x < widget->allocation.x + widget->allocation.width &&
1477 widget->allocation.y <= y &&
1478 y < widget->allocation.y + widget->allocation.height) {
1479 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001480 }
1481
1482 return NULL;
1483}
1484
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001485static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001486window_find_widget(struct window *window, int32_t x, int32_t y)
1487{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001488 struct surface *surface;
1489 struct widget *widget;
1490
1491 wl_list_for_each(surface, &window->subsurface_list, link) {
1492 widget = widget_find_widget(surface->widget, x, y);
1493 if (widget)
1494 return widget;
1495 }
1496
1497 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001498}
1499
1500static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001501widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001502{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001503 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001504
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001505 widget = malloc(sizeof *widget);
1506 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001507 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001508 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001509 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001510 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001511 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001512 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001513 widget->tooltip = NULL;
1514 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001515 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001516
1517 return widget;
1518}
1519
1520struct widget *
1521window_add_widget(struct window *window, void *data)
1522{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001523 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001524
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001525 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001526 wl_list_init(&widget->link);
1527 window->main_surface->widget = widget;
1528
1529 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001530}
1531
1532struct widget *
1533widget_add_widget(struct widget *parent, void *data)
1534{
1535 struct widget *widget;
1536
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001537 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001538 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001539
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001540 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001541}
1542
1543void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001544widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001545{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001546 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001547 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001548 struct input *input;
1549
Pekka Paalanen35e82632013-04-25 13:57:48 +03001550 /* Destroy the sub-surface along with the root widget */
1551 if (surface->widget == widget && surface->subsurface)
1552 surface_destroy(widget->surface);
1553
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001554 if (widget->tooltip) {
1555 free(widget->tooltip);
1556 widget->tooltip = NULL;
1557 }
1558
Pekka Paalanene156fb62012-01-19 13:51:38 +02001559 wl_list_for_each(input, &display->input_list, link) {
1560 if (input->focus_widget == widget)
1561 input->focus_widget = NULL;
1562 }
1563
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001564 wl_list_remove(&widget->link);
1565 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001566}
1567
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001569widget_set_default_cursor(struct widget *widget, int cursor)
1570{
1571 widget->default_cursor = cursor;
1572}
1573
1574void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001575widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001576{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001577 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001578}
1579
1580void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001581widget_set_size(struct widget *widget, int32_t width, int32_t height)
1582{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001583 widget->allocation.width = width;
1584 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001585}
1586
1587void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001588widget_set_allocation(struct widget *widget,
1589 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001590{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001591 widget->allocation.x = x;
1592 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001593 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001594}
1595
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001596void
1597widget_set_transparent(struct widget *widget, int transparent)
1598{
1599 widget->opaque = !transparent;
1600}
1601
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001602void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001603widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001604{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001605 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001606}
1607
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001608static cairo_surface_t *
1609widget_get_cairo_surface(struct widget *widget)
1610{
1611 struct surface *surface = widget->surface;
1612 struct window *window = widget->window;
1613
1614 if (!surface->cairo_surface) {
1615 if (surface == window->main_surface)
1616 window_create_main_surface(window);
1617 else
1618 surface_create_surface(surface, 0, 0, 0);
1619 }
1620
1621 return surface->cairo_surface;
1622}
1623
1624cairo_t *
1625widget_cairo_create(struct widget *widget)
1626{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001627 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001628 cairo_surface_t *cairo_surface;
1629 cairo_t *cr;
1630
1631 cairo_surface = widget_get_cairo_surface(widget);
1632 cr = cairo_create(cairo_surface);
1633
Pekka Paalanen35e82632013-04-25 13:57:48 +03001634 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1635
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001636 return cr;
1637}
1638
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001639struct wl_surface *
1640widget_get_wl_surface(struct widget *widget)
1641{
1642 return widget->surface->surface;
1643}
1644
1645uint32_t
1646widget_get_last_time(struct widget *widget)
1647{
1648 return widget->surface->last_time;
1649}
1650
1651void
1652widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1653{
1654 struct wl_compositor *comp = widget->window->display->compositor;
1655 struct surface *surface = widget->surface;
1656
1657 if (!surface->input_region)
1658 surface->input_region = wl_compositor_create_region(comp);
1659
1660 if (rect) {
1661 wl_region_add(surface->input_region,
1662 rect->x, rect->y, rect->width, rect->height);
1663 }
1664}
1665
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001666void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001667widget_set_resize_handler(struct widget *widget,
1668 widget_resize_handler_t handler)
1669{
1670 widget->resize_handler = handler;
1671}
1672
1673void
1674widget_set_redraw_handler(struct widget *widget,
1675 widget_redraw_handler_t handler)
1676{
1677 widget->redraw_handler = handler;
1678}
1679
1680void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001681widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001682{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001683 widget->enter_handler = handler;
1684}
1685
1686void
1687widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1688{
1689 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001690}
1691
1692void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001693widget_set_motion_handler(struct widget *widget,
1694 widget_motion_handler_t handler)
1695{
1696 widget->motion_handler = handler;
1697}
1698
1699void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001700widget_set_button_handler(struct widget *widget,
1701 widget_button_handler_t handler)
1702{
1703 widget->button_handler = handler;
1704}
1705
1706void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001707widget_set_axis_handler(struct widget *widget,
1708 widget_axis_handler_t handler)
1709{
1710 widget->axis_handler = handler;
1711}
1712
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001713static void
1714window_schedule_redraw_task(struct window *window);
1715
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001716void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001717widget_schedule_redraw(struct widget *widget)
1718{
Pekka Paalanen71233882013-04-25 13:57:53 +03001719 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001720 widget->surface->redraw_needed = 1;
1721 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001722}
1723
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001724cairo_surface_t *
1725window_get_surface(struct window *window)
1726{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001727 cairo_surface_t *cairo_surface;
1728
1729 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1730
1731 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001732}
1733
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001734struct wl_surface *
1735window_get_wl_surface(struct window *window)
1736{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001737 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001738}
1739
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001740struct wl_shell_surface *
1741window_get_wl_shell_surface(struct window *window)
1742{
1743 return window->shell_surface;
1744}
1745
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001746static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001747tooltip_redraw_handler(struct widget *widget, void *data)
1748{
1749 cairo_t *cr;
1750 const int32_t r = 3;
1751 struct tooltip *tooltip = data;
1752 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001753
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001754 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001755 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1756 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1757 cairo_paint(cr);
1758
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001759 width = widget->allocation.width;
1760 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001761 rounded_rect(cr, 0, 0, width, height, r);
1762
1763 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1764 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1765 cairo_fill(cr);
1766
1767 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1768 cairo_move_to(cr, 10, 16);
1769 cairo_show_text(cr, tooltip->entry);
1770 cairo_destroy(cr);
1771}
1772
1773static cairo_text_extents_t
1774get_text_extents(struct tooltip *tooltip)
1775{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001776 cairo_t *cr;
1777 cairo_text_extents_t extents;
1778
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001779 /* Use the dummy_surface because tooltip's surface was not
1780 * created yet, and parent does not have a valid surface
1781 * outside repaint, either.
1782 */
1783 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001784 cairo_text_extents(cr, tooltip->entry, &extents);
1785 cairo_destroy(cr);
1786
1787 return extents;
1788}
1789
1790static int
1791window_create_tooltip(struct tooltip *tooltip)
1792{
1793 struct widget *parent = tooltip->parent;
1794 struct display *display = parent->window->display;
1795 struct window *window;
1796 const int offset_y = 27;
1797 const int margin = 3;
1798 cairo_text_extents_t extents;
1799
1800 if (tooltip->widget)
1801 return 0;
1802
1803 window = window_create_transient(display, parent->window, tooltip->x,
1804 tooltip->y + offset_y,
1805 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1806 if (!window)
1807 return -1;
1808
1809 tooltip->window = window;
1810 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1811
1812 extents = get_text_extents(tooltip);
1813 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1814 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1815
1816 return 0;
1817}
1818
1819void
1820widget_destroy_tooltip(struct widget *parent)
1821{
1822 struct tooltip *tooltip = parent->tooltip;
1823
1824 parent->tooltip_count = 0;
1825 if (!tooltip)
1826 return;
1827
1828 if (tooltip->widget) {
1829 widget_destroy(tooltip->widget);
1830 window_destroy(tooltip->window);
1831 tooltip->widget = NULL;
1832 tooltip->window = NULL;
1833 }
1834
1835 close(tooltip->tooltip_fd);
1836 free(tooltip->entry);
1837 free(tooltip);
1838 parent->tooltip = NULL;
1839}
1840
1841static void
1842tooltip_func(struct task *task, uint32_t events)
1843{
1844 struct tooltip *tooltip =
1845 container_of(task, struct tooltip, tooltip_task);
1846 uint64_t exp;
1847
Martin Olsson8df662a2012-07-08 03:03:47 +02001848 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1849 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001850 window_create_tooltip(tooltip);
1851}
1852
1853#define TOOLTIP_TIMEOUT 500
1854static int
1855tooltip_timer_reset(struct tooltip *tooltip)
1856{
1857 struct itimerspec its;
1858
1859 its.it_interval.tv_sec = 0;
1860 its.it_interval.tv_nsec = 0;
1861 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1862 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1863 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1864 fprintf(stderr, "could not set timerfd\n: %m");
1865 return -1;
1866 }
1867
1868 return 0;
1869}
1870
1871int
1872widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1873{
1874 struct tooltip *tooltip = parent->tooltip;
1875
1876 parent->tooltip_count++;
1877 if (tooltip) {
1878 tooltip->x = x;
1879 tooltip->y = y;
1880 tooltip_timer_reset(tooltip);
1881 return 0;
1882 }
1883
1884 /* the handler might be triggered too fast via input device motion, so
1885 * we need this check here to make sure tooltip is fully initialized */
1886 if (parent->tooltip_count > 1)
1887 return 0;
1888
1889 tooltip = malloc(sizeof *tooltip);
1890 if (!tooltip)
1891 return -1;
1892
1893 parent->tooltip = tooltip;
1894 tooltip->parent = parent;
1895 tooltip->widget = NULL;
1896 tooltip->window = NULL;
1897 tooltip->x = x;
1898 tooltip->y = y;
1899 tooltip->entry = strdup(entry);
1900 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1901 if (tooltip->tooltip_fd < 0) {
1902 fprintf(stderr, "could not create timerfd\n: %m");
1903 return -1;
1904 }
1905
1906 tooltip->tooltip_task.run = tooltip_func;
1907 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1908 EPOLLIN, &tooltip->tooltip_task);
1909 tooltip_timer_reset(tooltip);
1910
1911 return 0;
1912}
1913
1914static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001915workspace_manager_state(void *data,
1916 struct workspace_manager *workspace_manager,
1917 uint32_t current,
1918 uint32_t count)
1919{
1920 struct display *display = data;
1921
1922 display->workspace = current;
1923 display->workspace_count = count;
1924}
1925
1926static const struct workspace_manager_listener workspace_manager_listener = {
1927 workspace_manager_state
1928};
1929
1930static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001931frame_resize_handler(struct widget *widget,
1932 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001933{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001934 struct frame *frame = data;
1935 struct widget *child = frame->child;
1936 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001937 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001938 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001939 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001940 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001941 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001942 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001943 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001944
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001945 switch (widget->window->type) {
1946 case TYPE_FULLSCREEN:
1947 decoration_width = 0;
1948 decoration_height = 0;
1949
1950 allocation.x = 0;
1951 allocation.y = 0;
1952 allocation.width = width;
1953 allocation.height = height;
1954 opaque_margin = 0;
1955
1956 wl_list_for_each(button, &frame->buttons_list, link)
1957 button->widget->opaque = 1;
1958 break;
1959 case TYPE_MAXIMIZED:
1960 decoration_width = t->width * 2;
1961 decoration_height = t->width + t->titlebar_height;
1962
1963 allocation.x = t->width;
1964 allocation.y = t->titlebar_height;
1965 allocation.width = width - decoration_width;
1966 allocation.height = height - decoration_height;
1967
1968 opaque_margin = 0;
1969
1970 wl_list_for_each(button, &frame->buttons_list, link)
1971 button->widget->opaque = 0;
1972 break;
1973 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001974 decoration_width = (t->width + t->margin) * 2;
1975 decoration_height = t->width +
1976 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001977
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001978 allocation.x = t->width + t->margin;
1979 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001980 allocation.width = width - decoration_width;
1981 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001982
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001983 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001984
1985 wl_list_for_each(button, &frame->buttons_list, link)
1986 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001987 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001988 }
1989
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001990 widget_set_allocation(child, allocation.x, allocation.y,
1991 allocation.width, allocation.height);
1992
1993 if (child->resize_handler)
1994 child->resize_handler(child,
1995 allocation.width,
1996 allocation.height,
1997 child->user_data);
1998
Scott Moreauf7e498c2012-05-14 11:39:29 -06001999 width = child->allocation.width + decoration_width;
2000 height = child->allocation.height + decoration_height;
2001
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002002 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2003
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002004 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002005 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002006 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002007 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002008 shadow_margin, shadow_margin,
2009 width - 2 * shadow_margin,
2010 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002011 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002012 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002013 }
2014
Scott Moreauf7e498c2012-05-14 11:39:29 -06002015 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002016
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002017 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002018 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002019 opaque_margin, opaque_margin,
2020 widget->allocation.width - 2 * opaque_margin,
2021 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002022
2023 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002024 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2025 x_l = t->width + shadow_margin;
2026 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002027 wl_list_for_each(button, &frame->buttons_list, link) {
2028 const int button_padding = 4;
2029 w = cairo_image_surface_get_width(button->icon);
2030 h = cairo_image_surface_get_height(button->icon);
2031
2032 if (button->decoration == FRAME_BUTTON_FANCY)
2033 w += 10;
2034
2035 if (button->align == FRAME_BUTTON_LEFT) {
2036 widget_set_allocation(button->widget,
2037 x_l, y , w + 1, h + 1);
2038 x_l += w;
2039 x_l += button_padding;
2040 } else {
2041 x_r -= w;
2042 widget_set_allocation(button->widget,
2043 x_r, y , w + 1, h + 1);
2044 x_r -= button_padding;
2045 }
2046 }
2047}
2048
2049static int
2050frame_button_enter_handler(struct widget *widget,
2051 struct input *input, float x, float y, void *data)
2052{
2053 struct frame_button *frame_button = data;
2054
2055 widget_schedule_redraw(frame_button->widget);
2056 frame_button->state = FRAME_BUTTON_OVER;
2057
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002058 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002059}
2060
2061static void
2062frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2063{
2064 struct frame_button *frame_button = data;
2065
2066 widget_schedule_redraw(frame_button->widget);
2067 frame_button->state = FRAME_BUTTON_DEFAULT;
2068}
2069
2070static void
2071frame_button_button_handler(struct widget *widget,
2072 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002073 uint32_t button,
2074 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002075{
2076 struct frame_button *frame_button = data;
2077 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002078 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002079
2080 if (button != BTN_LEFT)
2081 return;
2082
2083 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002084 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002085 frame_button->state = FRAME_BUTTON_ACTIVE;
2086 widget_schedule_redraw(frame_button->widget);
2087
2088 if (frame_button->type == FRAME_BUTTON_ICON)
2089 window_show_frame_menu(window, input, time);
2090 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002091 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002092 frame_button->state = FRAME_BUTTON_DEFAULT;
2093 widget_schedule_redraw(frame_button->widget);
2094 break;
2095 }
2096
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002097 if (!was_pressed)
2098 return;
2099
Martin Minarik1998b152012-05-10 02:04:35 +02002100 switch (frame_button->type) {
2101 case FRAME_BUTTON_CLOSE:
2102 if (window->close_handler)
2103 window->close_handler(window->parent,
2104 window->user_data);
2105 else
2106 display_exit(window->display);
2107 break;
2108 case FRAME_BUTTON_MINIMIZE:
2109 fprintf(stderr,"Minimize stub\n");
2110 break;
2111 case FRAME_BUTTON_MAXIMIZE:
2112 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2113 break;
2114 default:
2115 /* Unknown operation */
2116 break;
2117 }
2118}
2119
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002120static int
2121frame_button_motion_handler(struct widget *widget,
2122 struct input *input, uint32_t time,
2123 float x, float y, void *data)
2124{
2125 struct frame_button *frame_button = data;
2126 enum frame_button_pointer previous_button_state = frame_button->state;
2127
2128 /* only track state for a pressed button */
2129 if (input->grab != widget)
2130 return CURSOR_LEFT_PTR;
2131
2132 if (x > widget->allocation.x &&
2133 x < (widget->allocation.x + widget->allocation.width) &&
2134 y > widget->allocation.y &&
2135 y < (widget->allocation.y + widget->allocation.height)) {
2136 frame_button->state = FRAME_BUTTON_ACTIVE;
2137 } else {
2138 frame_button->state = FRAME_BUTTON_DEFAULT;
2139 }
2140
2141 if (frame_button->state != previous_button_state)
2142 widget_schedule_redraw(frame_button->widget);
2143
2144 return CURSOR_LEFT_PTR;
2145}
2146
Martin Minarik1998b152012-05-10 02:04:35 +02002147static void
2148frame_button_redraw_handler(struct widget *widget, void *data)
2149{
2150 struct frame_button *frame_button = data;
2151 cairo_t *cr;
2152 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002153
2154 x = widget->allocation.x;
2155 y = widget->allocation.y;
2156 width = widget->allocation.width;
2157 height = widget->allocation.height;
2158
2159 if (!width)
2160 return;
2161 if (!height)
2162 return;
2163 if (widget->opaque)
2164 return;
2165
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002166 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002167
2168 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2169 cairo_set_line_width(cr, 1);
2170
2171 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2172 cairo_rectangle (cr, x, y, 25, 16);
2173
2174 cairo_stroke_preserve(cr);
2175
2176 switch (frame_button->state) {
2177 case FRAME_BUTTON_DEFAULT:
2178 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2179 break;
2180 case FRAME_BUTTON_OVER:
2181 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2182 break;
2183 case FRAME_BUTTON_ACTIVE:
2184 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2185 break;
2186 }
2187
2188 cairo_fill (cr);
2189
2190 x += 4;
2191 }
2192
2193 cairo_set_source_surface(cr, frame_button->icon, x, y);
2194 cairo_paint(cr);
2195
2196 cairo_destroy(cr);
2197}
2198
2199static struct widget *
2200frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2201 enum frame_button_align align, enum frame_button_decoration style)
2202{
2203 struct frame_button *frame_button;
2204 const char *icon = data;
2205
2206 frame_button = malloc (sizeof *frame_button);
2207 memset(frame_button, 0, sizeof *frame_button);
2208
2209 frame_button->icon = cairo_image_surface_create_from_png(icon);
2210 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2211 frame_button->frame = frame;
2212 frame_button->type = type;
2213 frame_button->align = align;
2214 frame_button->decoration = style;
2215
2216 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2217
2218 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2219 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2220 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2221 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002222 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002223 return frame_button->widget;
2224}
2225
2226static void
2227frame_button_destroy(struct frame_button *frame_button)
2228{
2229 widget_destroy(frame_button->widget);
2230 wl_list_remove(&frame_button->link);
2231 cairo_surface_destroy(frame_button->icon);
2232 free(frame_button);
2233
2234 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002235}
2236
2237static void
2238frame_redraw_handler(struct widget *widget, void *data)
2239{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002240 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002241 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002242 struct theme *t = window->display->theme;
2243 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002244
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002245 if (window->type == TYPE_FULLSCREEN)
2246 return;
2247
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002248 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002249
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002250 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002251 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002252 if (window->type == TYPE_MAXIMIZED)
2253 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002254 theme_render_frame(t, cr, widget->allocation.width,
2255 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002256
2257 cairo_destroy(cr);
2258}
2259
2260static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002261frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002263 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002264 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002265 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002266
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002267 if (window->type != TYPE_TOPLEVEL)
2268 return CURSOR_LEFT_PTR;
2269
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002270 location = theme_get_location(t, input->sx, input->sy,
2271 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002272 frame->widget->allocation.height,
2273 window->type == TYPE_MAXIMIZED ?
2274 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002275
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002276 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002277 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002278 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002279 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002280 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002281 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002282 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002283 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002284 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002285 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002286 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002287 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002288 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002289 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002290 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002291 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002292 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002293 case THEME_LOCATION_EXTERIOR:
2294 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002295 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002296 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002297 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002298}
2299
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002300static void
2301frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002302{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002303 struct display *display;
2304
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002305 switch (index) {
2306 case 0: /* close */
2307 if (window->close_handler)
2308 window->close_handler(window->parent,
2309 window->user_data);
2310 else
2311 display_exit(window->display);
2312 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002313 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002314 display = window->display;
2315 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002316 workspace_manager_move_surface(
2317 display->workspace_manager,
2318 window->main_surface->surface,
2319 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002320 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002321 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002322 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002323 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002324 workspace_manager_move_surface(
2325 display->workspace_manager,
2326 window->main_surface->surface,
2327 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002328 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002329 case 3: /* fullscreen */
2330 /* we don't have a way to get out of fullscreen for now */
2331 if (window->fullscreen_handler)
2332 window->fullscreen_handler(window, window->user_data);
2333 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002334 }
2335}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002336
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002337void
2338window_show_frame_menu(struct window *window,
2339 struct input *input, uint32_t time)
2340{
2341 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002342 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002343
2344 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002345 "Close",
2346 "Move to workspace above", "Move to workspace below",
2347 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002348 };
2349
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002350 if (window->fullscreen_handler)
2351 count = ARRAY_LENGTH(entries);
2352 else
2353 count = ARRAY_LENGTH(entries) - 1;
2354
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002355 input_get_position(input, &x, &y);
2356 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002357 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002358}
2359
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002360static int
2361frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002362 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002363{
2364 return frame_get_pointer_image_for_location(data, input);
2365}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002366
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002367static int
2368frame_motion_handler(struct widget *widget,
2369 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002370 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002371{
2372 return frame_get_pointer_image_for_location(data, input);
2373}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002374
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002375static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002376frame_button_handler(struct widget *widget,
2377 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002378 uint32_t button, enum wl_pointer_button_state state,
2379 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002380
2381{
2382 struct frame *frame = data;
2383 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002384 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002385 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002386
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002387 if (window->type != TYPE_TOPLEVEL)
2388 return;
2389
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002390 location = theme_get_location(display->theme, input->sx, input->sy,
2391 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002392 frame->widget->allocation.height,
2393 window->type == TYPE_MAXIMIZED ?
2394 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002395
Daniel Stone4dbadb12012-05-30 16:31:51 +01002396 if (window->display->shell && button == BTN_LEFT &&
2397 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002398 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002399 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002400 if (!window->shell_surface)
2401 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002402 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002403 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002404 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002405 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002406 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002407 case THEME_LOCATION_RESIZING_TOP:
2408 case THEME_LOCATION_RESIZING_BOTTOM:
2409 case THEME_LOCATION_RESIZING_LEFT:
2410 case THEME_LOCATION_RESIZING_RIGHT:
2411 case THEME_LOCATION_RESIZING_TOP_LEFT:
2412 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2413 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2414 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002415 if (!window->shell_surface)
2416 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002417 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002418
Pekka Paalanen99436862012-11-19 17:15:59 +02002419 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002420 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002421 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002422 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002423 break;
2424 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002425 } else if (button == BTN_RIGHT &&
2426 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002427 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002428 }
2429}
2430
2431struct widget *
2432frame_create(struct window *window, void *data)
2433{
2434 struct frame *frame;
2435
2436 frame = malloc(sizeof *frame);
2437 memset(frame, 0, sizeof *frame);
2438
2439 frame->widget = window_add_widget(window, frame);
2440 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002441
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002442 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2443 widget_set_resize_handler(frame->widget, frame_resize_handler);
2444 widget_set_enter_handler(frame->widget, frame_enter_handler);
2445 widget_set_motion_handler(frame->widget, frame_motion_handler);
2446 widget_set_button_handler(frame->widget, frame_button_handler);
2447
Martin Minarik1998b152012-05-10 02:04:35 +02002448 /* Create empty list for frame buttons */
2449 wl_list_init(&frame->buttons_list);
2450
2451 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2452 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2453
2454 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2455 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2456
2457 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2458 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2459
2460 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2461 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2462
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002463 window->frame = frame;
2464
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002465 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002466}
2467
Kristian Høgsberga1627922012-06-20 17:30:03 -04002468void
2469frame_set_child_size(struct widget *widget, int child_width, int child_height)
2470{
2471 struct display *display = widget->window->display;
2472 struct theme *t = display->theme;
2473 int decoration_width, decoration_height;
2474 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002475 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002476
2477 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002478 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002479 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002480 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002481
2482 width = child_width + decoration_width;
2483 height = child_height + decoration_height;
2484 } else {
2485 width = child_width;
2486 height = child_height;
2487 }
2488
2489 window_schedule_resize(widget->window, width, height);
2490}
2491
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002492static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002493frame_destroy(struct frame *frame)
2494{
Martin Minarik1998b152012-05-10 02:04:35 +02002495 struct frame_button *button, *tmp;
2496
2497 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2498 frame_button_destroy(button);
2499
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002500 /* frame->child must be destroyed by the application */
2501 widget_destroy(frame->widget);
2502 free(frame);
2503}
2504
2505static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002506input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002507 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002508{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002509 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002510 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002511
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002512 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002513 return;
2514
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002515 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002516 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002517 widget = old;
2518 if (input->grab)
2519 widget = input->grab;
2520 if (widget->leave_handler)
2521 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002522 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002523 }
2524
2525 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002526 widget = focus;
2527 if (input->grab)
2528 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002529 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002530 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002531 cursor = widget->enter_handler(focus, input, x, y,
2532 widget->user_data);
2533 else
2534 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002535
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002536 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002537 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002538}
2539
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002540void
2541input_grab(struct input *input, struct widget *widget, uint32_t button)
2542{
2543 input->grab = widget;
2544 input->grab_button = button;
2545}
2546
2547void
2548input_ungrab(struct input *input)
2549{
2550 struct widget *widget;
2551
2552 input->grab = NULL;
2553 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002554 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002555 input->sx, input->sy);
2556 input_set_focus_widget(input, widget, input->sx, input->sy);
2557 }
2558}
2559
2560static void
2561input_remove_pointer_focus(struct input *input)
2562{
2563 struct window *window = input->pointer_focus;
2564
2565 if (!window)
2566 return;
2567
2568 input_set_focus_widget(input, NULL, 0, 0);
2569
2570 input->pointer_focus = NULL;
2571 input->current_cursor = CURSOR_UNSET;
2572}
2573
2574static void
2575pointer_handle_enter(void *data, struct wl_pointer *pointer,
2576 uint32_t serial, struct wl_surface *surface,
2577 wl_fixed_t sx_w, wl_fixed_t sy_w)
2578{
2579 struct input *input = data;
2580 struct window *window;
2581 struct widget *widget;
2582 float sx = wl_fixed_to_double(sx_w);
2583 float sy = wl_fixed_to_double(sy_w);
2584
2585 if (!surface) {
2586 /* enter event for a window we've just destroyed */
2587 return;
2588 }
2589
2590 input->display->serial = serial;
2591 input->pointer_enter_serial = serial;
2592 input->pointer_focus = wl_surface_get_user_data(surface);
2593 window = input->pointer_focus;
2594
Pekka Paalanen99436862012-11-19 17:15:59 +02002595 if (window->resizing) {
2596 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002597 /* Schedule a redraw to free the pool */
2598 window_schedule_redraw(window);
2599 }
2600
2601 input->sx = sx;
2602 input->sy = sy;
2603
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002604 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002605 input_set_focus_widget(input, widget, sx, sy);
2606}
2607
2608static void
2609pointer_handle_leave(void *data, struct wl_pointer *pointer,
2610 uint32_t serial, struct wl_surface *surface)
2611{
2612 struct input *input = data;
2613
2614 input->display->serial = serial;
2615 input_remove_pointer_focus(input);
2616}
2617
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002618static void
Daniel Stone37816df2012-05-16 18:45:18 +01002619pointer_handle_motion(void *data, struct wl_pointer *pointer,
2620 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002621{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002622 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002623 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002624 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002625 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002626 float sx = wl_fixed_to_double(sx_w);
2627 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002628
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002629 input->sx = sx;
2630 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002631
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002632 if (!window)
2633 return;
2634
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002635 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002636 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002637 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002638 }
2639
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002640 if (input->grab)
2641 widget = input->grab;
2642 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002643 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002644 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002645 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002646 input, time, sx, sy,
2647 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002648 else
2649 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002650
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002651 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002652}
2653
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002654static void
Daniel Stone37816df2012-05-16 18:45:18 +01002655pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002656 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002657{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002658 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002659 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002660 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002661
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002662 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002663 if (input->focus_widget && input->grab == NULL &&
2664 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002665 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002666
Neil Roberts6b28aad2012-01-23 19:11:18 +00002667 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002668 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002669 (*widget->button_handler)(widget,
2670 input, time,
2671 button, state,
2672 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002673
Daniel Stone4dbadb12012-05-30 16:31:51 +01002674 if (input->grab && input->grab_button == button &&
2675 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002676 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002677}
2678
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002679static void
Daniel Stone37816df2012-05-16 18:45:18 +01002680pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002681 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002682{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002683 struct input *input = data;
2684 struct widget *widget;
2685
2686 widget = input->focus_widget;
2687 if (input->grab)
2688 widget = input->grab;
2689 if (widget && widget->axis_handler)
2690 (*widget->axis_handler)(widget,
2691 input, time,
2692 axis, value,
2693 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002694}
2695
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002696static const struct wl_pointer_listener pointer_listener = {
2697 pointer_handle_enter,
2698 pointer_handle_leave,
2699 pointer_handle_motion,
2700 pointer_handle_button,
2701 pointer_handle_axis,
2702};
2703
2704static void
2705input_remove_keyboard_focus(struct input *input)
2706{
2707 struct window *window = input->keyboard_focus;
2708 struct itimerspec its;
2709
2710 its.it_interval.tv_sec = 0;
2711 its.it_interval.tv_nsec = 0;
2712 its.it_value.tv_sec = 0;
2713 its.it_value.tv_nsec = 0;
2714 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2715
2716 if (!window)
2717 return;
2718
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002719 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002720 if (window->keyboard_focus_handler)
2721 (*window->keyboard_focus_handler)(window, NULL,
2722 window->user_data);
2723
2724 input->keyboard_focus = NULL;
2725}
2726
2727static void
2728keyboard_repeat_func(struct task *task, uint32_t events)
2729{
2730 struct input *input =
2731 container_of(task, struct input, repeat_task);
2732 struct window *window = input->keyboard_focus;
2733 uint64_t exp;
2734
2735 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2736 /* If we change the timer between the fd becoming
2737 * readable and getting here, there'll be nothing to
2738 * read and we get EAGAIN. */
2739 return;
2740
2741 if (window && window->key_handler) {
2742 (*window->key_handler)(window, input, input->repeat_time,
2743 input->repeat_key, input->repeat_sym,
2744 WL_KEYBOARD_KEY_STATE_PRESSED,
2745 window->user_data);
2746 }
2747}
2748
2749static void
2750keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2751 uint32_t format, int fd, uint32_t size)
2752{
2753 struct input *input = data;
2754 char *map_str;
2755
2756 if (!data) {
2757 close(fd);
2758 return;
2759 }
2760
2761 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2762 close(fd);
2763 return;
2764 }
2765
2766 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2767 if (map_str == MAP_FAILED) {
2768 close(fd);
2769 return;
2770 }
2771
2772 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2773 map_str,
2774 XKB_KEYMAP_FORMAT_TEXT_V1,
2775 0);
2776 munmap(map_str, size);
2777 close(fd);
2778
2779 if (!input->xkb.keymap) {
2780 fprintf(stderr, "failed to compile keymap\n");
2781 return;
2782 }
2783
2784 input->xkb.state = xkb_state_new(input->xkb.keymap);
2785 if (!input->xkb.state) {
2786 fprintf(stderr, "failed to create XKB state\n");
2787 xkb_map_unref(input->xkb.keymap);
2788 input->xkb.keymap = NULL;
2789 return;
2790 }
2791
2792 input->xkb.control_mask =
2793 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2794 input->xkb.alt_mask =
2795 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2796 input->xkb.shift_mask =
2797 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2798}
2799
2800static void
2801keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2802 uint32_t serial, struct wl_surface *surface,
2803 struct wl_array *keys)
2804{
2805 struct input *input = data;
2806 struct window *window;
2807
2808 input->display->serial = serial;
2809 input->keyboard_focus = wl_surface_get_user_data(surface);
2810
2811 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002812 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002813 if (window->keyboard_focus_handler)
2814 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002815 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002816}
2817
2818static void
2819keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2820 uint32_t serial, struct wl_surface *surface)
2821{
2822 struct input *input = data;
2823
2824 input->display->serial = serial;
2825 input_remove_keyboard_focus(input);
2826}
2827
Scott Moreau210d0792012-03-22 10:47:01 -06002828static void
Daniel Stone37816df2012-05-16 18:45:18 +01002829keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002830 uint32_t serial, uint32_t time, uint32_t key,
2831 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002832{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002833 struct input *input = data;
2834 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002835 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002836 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002837 const xkb_keysym_t *syms;
2838 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002839 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002840
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002841 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002842 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002843 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002844 return;
2845
Daniel Stone97f68542012-05-30 16:32:01 +01002846 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002847
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002848 sym = XKB_KEY_NoSymbol;
2849 if (num_syms == 1)
2850 sym = syms[0];
2851
2852 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002853 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002854 window_set_maximized(window,
2855 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002856 } else if (sym == XKB_KEY_F11 &&
2857 window->fullscreen_handler &&
2858 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2859 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002860 } else if (sym == XKB_KEY_F4 &&
2861 input->modifiers == MOD_ALT_MASK &&
2862 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2863 if (window->close_handler)
2864 window->close_handler(window->parent,
2865 window->user_data);
2866 else
2867 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002868 } else if (window->key_handler) {
2869 (*window->key_handler)(window, input, time, key,
2870 sym, state, window->user_data);
2871 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002872
2873 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2874 key == input->repeat_key) {
2875 its.it_interval.tv_sec = 0;
2876 its.it_interval.tv_nsec = 0;
2877 its.it_value.tv_sec = 0;
2878 its.it_value.tv_nsec = 0;
2879 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2880 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2881 input->repeat_sym = sym;
2882 input->repeat_key = key;
2883 input->repeat_time = time;
2884 its.it_interval.tv_sec = 0;
2885 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2886 its.it_value.tv_sec = 0;
2887 its.it_value.tv_nsec = 400 * 1000 * 1000;
2888 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2889 }
2890}
2891
2892static void
Daniel Stone351eb612012-05-31 15:27:47 -04002893keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2894 uint32_t serial, uint32_t mods_depressed,
2895 uint32_t mods_latched, uint32_t mods_locked,
2896 uint32_t group)
2897{
2898 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002899 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002900
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002901 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2902 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002903 mask = xkb_state_serialize_mods(input->xkb.state,
2904 XKB_STATE_DEPRESSED |
2905 XKB_STATE_LATCHED);
2906 input->modifiers = 0;
2907 if (mask & input->xkb.control_mask)
2908 input->modifiers |= MOD_CONTROL_MASK;
2909 if (mask & input->xkb.alt_mask)
2910 input->modifiers |= MOD_ALT_MASK;
2911 if (mask & input->xkb.shift_mask)
2912 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002913}
2914
Daniel Stone37816df2012-05-16 18:45:18 +01002915static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002916 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002917 keyboard_handle_enter,
2918 keyboard_handle_leave,
2919 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002920 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002921};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002922
2923static void
Daniel Stone37816df2012-05-16 18:45:18 +01002924seat_handle_capabilities(void *data, struct wl_seat *seat,
2925 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002926{
Daniel Stone37816df2012-05-16 18:45:18 +01002927 struct input *input = data;
2928
2929 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2930 input->pointer = wl_seat_get_pointer(seat);
2931 wl_pointer_set_user_data(input->pointer, input);
2932 wl_pointer_add_listener(input->pointer, &pointer_listener,
2933 input);
2934 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2935 wl_pointer_destroy(input->pointer);
2936 input->pointer = NULL;
2937 }
2938
2939 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2940 input->keyboard = wl_seat_get_keyboard(seat);
2941 wl_keyboard_set_user_data(input->keyboard, input);
2942 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2943 input);
2944 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2945 wl_keyboard_destroy(input->keyboard);
2946 input->keyboard = NULL;
2947 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002948}
2949
Daniel Stone37816df2012-05-16 18:45:18 +01002950static const struct wl_seat_listener seat_listener = {
2951 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002952};
2953
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002954void
2955input_get_position(struct input *input, int32_t *x, int32_t *y)
2956{
2957 *x = input->sx;
2958 *y = input->sy;
2959}
2960
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002961struct display *
2962input_get_display(struct input *input)
2963{
2964 return input->display;
2965}
2966
Daniel Stone37816df2012-05-16 18:45:18 +01002967struct wl_seat *
2968input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002969{
Daniel Stone37816df2012-05-16 18:45:18 +01002970 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002971}
2972
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002973uint32_t
2974input_get_modifiers(struct input *input)
2975{
2976 return input->modifiers;
2977}
2978
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002979struct widget *
2980input_get_focus_widget(struct input *input)
2981{
2982 return input->focus_widget;
2983}
2984
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002985struct data_offer {
2986 struct wl_data_offer *offer;
2987 struct input *input;
2988 struct wl_array types;
2989 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002990
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002991 struct task io_task;
2992 int fd;
2993 data_func_t func;
2994 int32_t x, y;
2995 void *user_data;
2996};
2997
2998static void
2999data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3000{
3001 struct data_offer *offer = data;
3002 char **p;
3003
3004 p = wl_array_add(&offer->types, sizeof *p);
3005 *p = strdup(type);
3006}
3007
3008static const struct wl_data_offer_listener data_offer_listener = {
3009 data_offer_offer,
3010};
3011
3012static void
3013data_offer_destroy(struct data_offer *offer)
3014{
3015 char **p;
3016
3017 offer->refcount--;
3018 if (offer->refcount == 0) {
3019 wl_data_offer_destroy(offer->offer);
3020 for (p = offer->types.data; *p; p++)
3021 free(*p);
3022 wl_array_release(&offer->types);
3023 free(offer);
3024 }
3025}
3026
3027static void
3028data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003029 struct wl_data_device *data_device,
3030 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003031{
3032 struct data_offer *offer;
3033
3034 offer = malloc(sizeof *offer);
3035
3036 wl_array_init(&offer->types);
3037 offer->refcount = 1;
3038 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003039 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003040 wl_data_offer_add_listener(offer->offer,
3041 &data_offer_listener, offer);
3042}
3043
3044static void
3045data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003046 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003047 wl_fixed_t x_w, wl_fixed_t y_w,
3048 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003049{
3050 struct input *input = data;
3051 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003052 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003053 float x = wl_fixed_to_double(x_w);
3054 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003055 char **p;
3056
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003057 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003058 window = wl_surface_get_user_data(surface);
3059 input->pointer_focus = window;
3060
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003061 if (offer) {
3062 input->drag_offer = wl_data_offer_get_user_data(offer);
3063
3064 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3065 *p = NULL;
3066
3067 types_data = input->drag_offer->types.data;
3068 } else {
3069 input->drag_offer = NULL;
3070 types_data = NULL;
3071 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003072
3073 window = input->pointer_focus;
3074 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003075 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003076 window->user_data);
3077}
3078
3079static void
3080data_device_leave(void *data, struct wl_data_device *data_device)
3081{
3082 struct input *input = data;
3083
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003084 if (input->drag_offer) {
3085 data_offer_destroy(input->drag_offer);
3086 input->drag_offer = NULL;
3087 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003088}
3089
3090static void
3091data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003092 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003093{
3094 struct input *input = data;
3095 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003096 float x = wl_fixed_to_double(x_w);
3097 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003098 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003099
3100 input->sx = x;
3101 input->sy = y;
3102
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003103 if (input->drag_offer)
3104 types_data = input->drag_offer->types.data;
3105 else
3106 types_data = NULL;
3107
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003108 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003109 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003110 window->user_data);
3111}
3112
3113static void
3114data_device_drop(void *data, struct wl_data_device *data_device)
3115{
3116 struct input *input = data;
3117 struct window *window = input->pointer_focus;
3118
3119 if (window->drop_handler)
3120 window->drop_handler(window, input,
3121 input->sx, input->sy, window->user_data);
3122}
3123
3124static void
3125data_device_selection(void *data,
3126 struct wl_data_device *wl_data_device,
3127 struct wl_data_offer *offer)
3128{
3129 struct input *input = data;
3130 char **p;
3131
3132 if (input->selection_offer)
3133 data_offer_destroy(input->selection_offer);
3134
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003135 if (offer) {
3136 input->selection_offer = wl_data_offer_get_user_data(offer);
3137 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3138 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003139 } else {
3140 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003141 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003142}
3143
3144static const struct wl_data_device_listener data_device_listener = {
3145 data_device_data_offer,
3146 data_device_enter,
3147 data_device_leave,
3148 data_device_motion,
3149 data_device_drop,
3150 data_device_selection
3151};
3152
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003153static void
3154input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003155{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003156 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003157 struct wl_cursor *cursor;
3158 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003159
Daniel Stone80972742012-11-07 17:51:39 +11003160 if (!input->pointer)
3161 return;
3162
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003163 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003164 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003165 return;
3166
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003167 if (index >= (int) cursor->image_count) {
3168 fprintf(stderr, "cursor index out of range\n");
3169 return;
3170 }
3171
3172 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003173 buffer = wl_cursor_image_get_buffer(image);
3174 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003175 return;
3176
Kristian Høgsbergae277372012-08-01 09:41:08 -04003177 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003178 input->pointer_surface,
3179 image->hotspot_x, image->hotspot_y);
3180 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3181 wl_surface_damage(input->pointer_surface, 0, 0,
3182 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003183 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003184}
3185
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003186static const struct wl_callback_listener pointer_surface_listener;
3187
3188static void
3189pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3190 uint32_t time)
3191{
3192 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003193 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003194 int i;
3195
3196 if (callback) {
3197 assert(callback == input->cursor_frame_cb);
3198 wl_callback_destroy(callback);
3199 input->cursor_frame_cb = NULL;
3200 }
3201
Daniel Stone80972742012-11-07 17:51:39 +11003202 if (!input->pointer)
3203 return;
3204
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003205 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003206 wl_pointer_set_cursor(input->pointer,
3207 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003208 NULL, 0, 0);
3209 return;
3210 }
3211
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003212 if (input->current_cursor == CURSOR_UNSET)
3213 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003214 cursor = input->display->cursors[input->current_cursor];
3215 if (!cursor)
3216 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003217
3218 /* FIXME We don't have the current time on the first call so we set
3219 * the animation start to the time of the first frame callback. */
3220 if (time == 0)
3221 input->cursor_anim_start = 0;
3222 else if (input->cursor_anim_start == 0)
3223 input->cursor_anim_start = time;
3224
3225 if (time == 0 || input->cursor_anim_start == 0)
3226 i = 0;
3227 else
3228 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3229
Pekka Paalanenbc106382012-10-10 12:49:31 +03003230 if (cursor->image_count > 1) {
3231 input->cursor_frame_cb =
3232 wl_surface_frame(input->pointer_surface);
3233 wl_callback_add_listener(input->cursor_frame_cb,
3234 &pointer_surface_listener, input);
3235 }
3236
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003237 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003238}
3239
3240static const struct wl_callback_listener pointer_surface_listener = {
3241 pointer_surface_frame_callback
3242};
3243
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003244void
3245input_set_pointer_image(struct input *input, int pointer)
3246{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003247 int force = 0;
3248
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003249 if (!input->pointer)
3250 return;
3251
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003252 if (input->pointer_enter_serial > input->cursor_serial)
3253 force = 1;
3254
3255 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003256 return;
3257
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003258 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003259 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003260 if (!input->cursor_frame_cb)
3261 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003262 else if (force) {
3263 /* The current frame callback may be stuck if, for instance,
3264 * the set cursor request was processed by the server after
3265 * this client lost the focus. In this case the cursor surface
3266 * might not be mapped and the frame callback wouldn't ever
3267 * complete. Send a set_cursor and attach to try to map the
3268 * cursor surface again so that the callback will finish */
3269 input_set_pointer_image_index(input, 0);
3270 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003271}
3272
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003273struct wl_data_device *
3274input_get_data_device(struct input *input)
3275{
3276 return input->data_device;
3277}
3278
3279void
3280input_set_selection(struct input *input,
3281 struct wl_data_source *source, uint32_t time)
3282{
3283 wl_data_device_set_selection(input->data_device, source, time);
3284}
3285
3286void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003287input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003288{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003289 wl_data_offer_accept(input->drag_offer->offer,
3290 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003291}
3292
3293static void
3294offer_io_func(struct task *task, uint32_t events)
3295{
3296 struct data_offer *offer =
3297 container_of(task, struct data_offer, io_task);
3298 unsigned int len;
3299 char buffer[4096];
3300
3301 len = read(offer->fd, buffer, sizeof buffer);
3302 offer->func(buffer, len,
3303 offer->x, offer->y, offer->user_data);
3304
3305 if (len == 0) {
3306 close(offer->fd);
3307 data_offer_destroy(offer);
3308 }
3309}
3310
3311static void
3312data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3313 data_func_t func, void *user_data)
3314{
3315 int p[2];
3316
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003317 if (pipe2(p, O_CLOEXEC) == -1)
3318 return;
3319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003320 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3321 close(p[1]);
3322
3323 offer->io_task.run = offer_io_func;
3324 offer->fd = p[0];
3325 offer->func = func;
3326 offer->refcount++;
3327 offer->user_data = user_data;
3328
3329 display_watch_fd(offer->input->display,
3330 offer->fd, EPOLLIN, &offer->io_task);
3331}
3332
3333void
3334input_receive_drag_data(struct input *input, const char *mime_type,
3335 data_func_t func, void *data)
3336{
3337 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3338 input->drag_offer->x = input->sx;
3339 input->drag_offer->y = input->sy;
3340}
3341
3342int
3343input_receive_selection_data(struct input *input, const char *mime_type,
3344 data_func_t func, void *data)
3345{
3346 char **p;
3347
3348 if (input->selection_offer == NULL)
3349 return -1;
3350
3351 for (p = input->selection_offer->types.data; *p; p++)
3352 if (strcmp(mime_type, *p) == 0)
3353 break;
3354
3355 if (*p == NULL)
3356 return -1;
3357
3358 data_offer_receive_data(input->selection_offer,
3359 mime_type, func, data);
3360 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003361}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003362
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003363int
3364input_receive_selection_data_to_fd(struct input *input,
3365 const char *mime_type, int fd)
3366{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003367 if (input->selection_offer)
3368 wl_data_offer_receive(input->selection_offer->offer,
3369 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003370
3371 return 0;
3372}
3373
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003374void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003375window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003376{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003377 if (!window->shell_surface)
3378 return;
3379
Daniel Stone37816df2012-05-16 18:45:18 +01003380 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003381}
3382
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003383static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003384surface_set_synchronized(struct surface *surface)
3385{
3386 if (!surface->subsurface)
3387 return;
3388
3389 if (surface->synchronized)
3390 return;
3391
3392 wl_subsurface_set_sync(surface->subsurface);
3393 surface->synchronized = 1;
3394}
3395
3396static void
3397surface_set_synchronized_default(struct surface *surface)
3398{
3399 if (!surface->subsurface)
3400 return;
3401
3402 if (surface->synchronized == surface->synchronized_default)
3403 return;
3404
3405 if (surface->synchronized_default)
3406 wl_subsurface_set_sync(surface->subsurface);
3407 else
3408 wl_subsurface_set_desync(surface->subsurface);
3409
3410 surface->synchronized = surface->synchronized_default;
3411}
3412
3413static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003414surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003415{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003416 struct widget *widget = surface->widget;
3417 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003418
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003419 if (surface->input_region) {
3420 wl_region_destroy(surface->input_region);
3421 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003422 }
3423
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003424 if (surface->opaque_region)
3425 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003426
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003427 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003428
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003429 if (widget->resize_handler)
3430 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003431 widget->allocation.width,
3432 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003433 widget->user_data);
3434
Pekka Paalanen35e82632013-04-25 13:57:48 +03003435 if (surface->subsurface &&
3436 (surface->allocation.x != widget->allocation.x ||
3437 surface->allocation.y != widget->allocation.y)) {
3438 wl_subsurface_set_position(surface->subsurface,
3439 widget->allocation.x,
3440 widget->allocation.y);
3441 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003442 if (surface->allocation.width != widget->allocation.width ||
3443 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003444 window_schedule_redraw(widget->window);
3445 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003446 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003447
3448 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003449 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003450 widget->allocation.width,
3451 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003452}
3453
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003454static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003455hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3456{
3457 /*
3458 * This hack should be removed, when EGL respects
3459 * eglSwapInterval(0).
3460 *
3461 * If this window has sub-surfaces, especially a free-running
3462 * EGL-widget, we need to post the parent surface once with
3463 * all the old state to guarantee, that the EGL-widget will
3464 * receive its frame callback soon. Otherwise, a forced call
3465 * to eglSwapBuffers may end up blocking, waiting for a frame
3466 * event that will never come, because we will commit the parent
3467 * surface with all new state only after eglSwapBuffers returns.
3468 *
3469 * This assumes, that:
3470 * 1. When the EGL widget's resize hook is called, it pauses.
3471 * 2. When the EGL widget's redraw hook is called, it forces a
3472 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3473 * In a single threaded application condition 1 is a no-op.
3474 *
3475 * XXX: This should actually be after the surface_resize() calls,
3476 * but cannot, because then it would commit the incomplete state
3477 * accumulated from the widget resize hooks.
3478 */
3479 if (window->subsurface_list.next != &window->main_surface->link ||
3480 window->subsurface_list.prev != &window->main_surface->link)
3481 wl_surface_commit(window->main_surface->surface);
3482}
3483
3484static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003485idle_resize(struct window *window)
3486{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003487 struct surface *surface;
3488
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003489 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003490 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003491
Pekka Paalanen71233882013-04-25 13:57:53 +03003492 DBG("from %dx%d to %dx%d\n",
3493 window->main_surface->server_allocation.width,
3494 window->main_surface->server_allocation.height,
3495 window->pending_allocation.width,
3496 window->pending_allocation.height);
3497
Pekka Paalanene9297f82013-04-25 13:57:51 +03003498 hack_prevent_EGL_sub_surface_deadlock(window);
3499
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003500 widget_set_allocation(window->main_surface->widget,
3501 window->pending_allocation.x,
3502 window->pending_allocation.y,
3503 window->pending_allocation.width,
3504 window->pending_allocation.height);
3505
3506 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003507
3508 /* The main surface is in the list, too. Main surface's
3509 * resize_handler is responsible for calling widget_set_allocation()
3510 * on all sub-surface root widgets, so they will be resized
3511 * properly.
3512 */
3513 wl_list_for_each(surface, &window->subsurface_list, link) {
3514 if (surface == window->main_surface)
3515 continue;
3516
3517 surface_set_synchronized(surface);
3518 surface_resize(surface);
3519 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003520}
3521
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003522void
3523window_schedule_resize(struct window *window, int width, int height)
3524{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003525 window->pending_allocation.x = 0;
3526 window->pending_allocation.y = 0;
3527 window->pending_allocation.width = width;
3528 window->pending_allocation.height = height;
3529
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003530 if (window->min_allocation.width == 0)
3531 window->min_allocation = window->pending_allocation;
3532 if (window->pending_allocation.width < window->min_allocation.width)
3533 window->pending_allocation.width = window->min_allocation.width;
3534 if (window->pending_allocation.height < window->min_allocation.height)
3535 window->pending_allocation.height = window->min_allocation.height;
3536
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003537 window->resize_needed = 1;
3538 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003539}
3540
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003541void
3542widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3543{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003544 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003545}
3546
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003547static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003548handle_ping(void *data, struct wl_shell_surface *shell_surface,
3549 uint32_t serial)
3550{
3551 wl_shell_surface_pong(shell_surface, serial);
3552}
3553
3554static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003555handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003556 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003557{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003558 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003559
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003560 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003561 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003562}
3563
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003564static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003565menu_destroy(struct menu *menu)
3566{
3567 widget_destroy(menu->widget);
3568 window_destroy(menu->window);
3569 free(menu);
3570}
3571
3572static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003573handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3574{
3575 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003576 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003577
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003578 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003579 * device. Or just use wl_callback. And this really needs to
3580 * be a window vfunc that the menu can set. And we need the
3581 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003582
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003583 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003584 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003585}
3586
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003587static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003588 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003589 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003590 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003591};
3592
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003593void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003594window_get_allocation(struct window *window,
3595 struct rectangle *allocation)
3596{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003597 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003598}
3599
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003600static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003601widget_redraw(struct widget *widget)
3602{
3603 struct widget *child;
3604
3605 if (widget->redraw_handler)
3606 widget->redraw_handler(widget, widget->user_data);
3607 wl_list_for_each(child, &widget->child_list, link)
3608 widget_redraw(child);
3609}
3610
3611static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003612frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3613{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003614 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003615
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003616 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003617 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003618 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003619 surface->frame_cb = NULL;
3620
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003621 surface->last_time = time;
3622
Pekka Paalanen71233882013-04-25 13:57:53 +03003623 if (surface->redraw_needed || surface->window->redraw_needed) {
3624 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003625 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003626 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003627}
3628
3629static const struct wl_callback_listener listener = {
3630 frame_callback
3631};
3632
3633static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003634surface_redraw(struct surface *surface)
3635{
Pekka Paalanen71233882013-04-25 13:57:53 +03003636 DBG_OBJ(surface->surface, "begin\n");
3637
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003638 if (!surface->window->redraw_needed && !surface->redraw_needed)
3639 return;
3640
3641 /* Whole-window redraw forces a redraw even if the previous has
3642 * not yet hit the screen.
3643 */
3644 if (surface->frame_cb) {
3645 if (!surface->window->redraw_needed)
3646 return;
3647
Pekka Paalanen71233882013-04-25 13:57:53 +03003648 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003649 wl_callback_destroy(surface->frame_cb);
3650 }
3651
3652 surface->frame_cb = wl_surface_frame(surface->surface);
3653 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003654 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003655
3656 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003657 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003658 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003659 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003660}
3661
3662static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003663idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003664{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003665 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003666 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003667
Pekka Paalanen71233882013-04-25 13:57:53 +03003668 DBG(" --------- \n");
3669
Pekka Paalaneneebff542013-04-25 13:57:52 +03003670 wl_list_init(&window->redraw_task.link);
3671 window->redraw_task_scheduled = 0;
3672
3673 if (window->resize_needed) {
3674 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003675 if (window->main_surface->frame_cb) {
3676 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003677 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003678 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003679
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003680 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003681 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003682
Pekka Paalanen35e82632013-04-25 13:57:48 +03003683 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003684 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003685
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003686 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003687 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003688
3689 wl_list_for_each(surface, &window->subsurface_list, link)
3690 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003691}
3692
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003693static void
3694window_schedule_redraw_task(struct window *window)
3695{
3696 if (window->configure_requests)
3697 return;
3698 if (!window->redraw_task_scheduled) {
3699 window->redraw_task.run = idle_redraw;
3700 display_defer(window->display, &window->redraw_task);
3701 window->redraw_task_scheduled = 1;
3702 }
3703}
3704
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003705void
3706window_schedule_redraw(struct window *window)
3707{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003708 struct surface *surface;
3709
Pekka Paalanen71233882013-04-25 13:57:53 +03003710 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3711
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003712 wl_list_for_each(surface, &window->subsurface_list, link)
3713 surface->redraw_needed = 1;
3714
3715 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003716}
3717
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003718int
3719window_is_fullscreen(struct window *window)
3720{
3721 return window->type == TYPE_FULLSCREEN;
3722}
3723
MoD063a8822013-03-02 23:43:57 +00003724static void
3725configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3726{
3727 struct window *window = data;
3728
3729 wl_callback_destroy(callback);
3730 window->configure_requests--;
3731
3732 if (!window->configure_requests)
3733 window_schedule_redraw(window);
3734}
3735
3736static struct wl_callback_listener configure_request_listener = {
3737 configure_request_completed,
3738};
3739
3740static void
3741window_defer_redraw_until_configure(struct window* window)
3742{
3743 struct wl_callback *callback;
3744
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003745 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003746 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003747 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003748 }
3749
3750 callback = wl_display_sync(window->display->display);
3751 wl_callback_add_listener(callback, &configure_request_listener, window);
3752 window->configure_requests++;
3753}
3754
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003755void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003756window_set_fullscreen(struct window *window, int fullscreen)
3757{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003758 if (!window->display->shell)
3759 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003760
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003761 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003762 return;
3763
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003764 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003765 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003766 if (window->type == TYPE_TOPLEVEL) {
3767 window->saved_allocation = window->main_surface->allocation;
3768 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003769 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003770 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003771 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003772 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003773 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003774 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003775 if (window->saved_type == TYPE_MAXIMIZED) {
3776 window_set_maximized(window, 1);
3777 } else {
3778 window->type = TYPE_TOPLEVEL;
3779 wl_shell_surface_set_toplevel(window->shell_surface);
3780 window_schedule_resize(window,
3781 window->saved_allocation.width,
3782 window->saved_allocation.height);
3783 }
3784
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003785 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003786}
3787
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003788void
3789window_set_fullscreen_method(struct window *window,
3790 enum wl_shell_surface_fullscreen_method method)
3791{
3792 window->fullscreen_method = method;
3793}
3794
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003795int
3796window_is_maximized(struct window *window)
3797{
3798 return window->type == TYPE_MAXIMIZED;
3799}
3800
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003801void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003802window_set_maximized(struct window *window, int maximized)
3803{
3804 if (!window->display->shell)
3805 return;
3806
3807 if ((window->type == TYPE_MAXIMIZED) == maximized)
3808 return;
3809
3810 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003811 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003812 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3813 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003814 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003815 } else if (window->type == TYPE_FULLSCREEN) {
3816 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3817 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003818 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003819 } else {
3820 wl_shell_surface_set_toplevel(window->shell_surface);
3821 window->type = TYPE_TOPLEVEL;
3822 window_schedule_resize(window,
3823 window->saved_allocation.width,
3824 window->saved_allocation.height);
3825 }
3826}
3827
3828void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003829window_set_user_data(struct window *window, void *data)
3830{
3831 window->user_data = data;
3832}
3833
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003834void *
3835window_get_user_data(struct window *window)
3836{
3837 return window->user_data;
3838}
3839
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003840void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003841window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003842 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003843{
3844 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003845}
3846
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003847void
3848window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003849 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003850{
3851 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003852}
3853
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003854void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003855window_set_data_handler(struct window *window, window_data_handler_t handler)
3856{
3857 window->data_handler = handler;
3858}
3859
3860void
3861window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3862{
3863 window->drop_handler = handler;
3864}
3865
3866void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003867window_set_close_handler(struct window *window,
3868 window_close_handler_t handler)
3869{
3870 window->close_handler = handler;
3871}
3872
3873void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003874window_set_fullscreen_handler(struct window *window,
3875 window_fullscreen_handler_t handler)
3876{
3877 window->fullscreen_handler = handler;
3878}
3879
3880void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003881window_set_output_handler(struct window *window,
3882 window_output_handler_t handler)
3883{
3884 window->output_handler = handler;
3885}
3886
3887void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003888window_set_title(struct window *window, const char *title)
3889{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003890 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003891 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003892 if (window->shell_surface)
3893 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003894}
3895
3896const char *
3897window_get_title(struct window *window)
3898{
3899 return window->title;
3900}
3901
3902void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003903window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3904{
3905 struct text_cursor_position *text_cursor_position =
3906 window->display->text_cursor_position;
3907
Scott Moreau9295ce02012-06-01 12:46:10 -06003908 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003909 return;
3910
3911 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003912 window->main_surface->surface,
3913 wl_fixed_from_int(x),
3914 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003915}
3916
3917void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003918window_damage(struct window *window, int32_t x, int32_t y,
3919 int32_t width, int32_t height)
3920{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003921 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003922}
3923
Casey Dahlin9074db52012-04-19 22:50:09 -04003924static void
3925surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003926 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003927{
Rob Bradford7507b572012-05-15 17:55:34 +01003928 struct window *window = data;
3929 struct output *output;
3930 struct output *output_found = NULL;
3931 struct window_output *window_output;
3932
3933 wl_list_for_each(output, &window->display->output_list, link) {
3934 if (output->output == wl_output) {
3935 output_found = output;
3936 break;
3937 }
3938 }
3939
3940 if (!output_found)
3941 return;
3942
3943 window_output = malloc (sizeof *window_output);
3944 window_output->output = output_found;
3945
3946 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003947
3948 if (window->output_handler)
3949 window->output_handler(window, output_found, 1,
3950 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003951}
3952
3953static void
3954surface_leave(void *data,
3955 struct wl_surface *wl_surface, struct wl_output *output)
3956{
Rob Bradford7507b572012-05-15 17:55:34 +01003957 struct window *window = data;
3958 struct window_output *window_output;
3959 struct window_output *window_output_found = NULL;
3960
3961 wl_list_for_each(window_output, &window->window_output_list, link) {
3962 if (window_output->output->output == output) {
3963 window_output_found = window_output;
3964 break;
3965 }
3966 }
3967
3968 if (window_output_found) {
3969 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003970
3971 if (window->output_handler)
3972 window->output_handler(window, window_output->output,
3973 0, window->user_data);
3974
Rob Bradford7507b572012-05-15 17:55:34 +01003975 free(window_output_found);
3976 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003977}
3978
3979static const struct wl_surface_listener surface_listener = {
3980 surface_enter,
3981 surface_leave
3982};
3983
Pekka Paalanen4e373742013-02-13 16:17:13 +02003984static struct surface *
3985surface_create(struct window *window)
3986{
3987 struct display *display = window->display;
3988 struct surface *surface;
3989
3990 surface = calloc(1, sizeof *surface);
3991 if (!surface)
3992 return NULL;
3993
3994 surface->window = window;
3995 surface->surface = wl_compositor_create_surface(display->compositor);
3996 wl_surface_add_listener(surface->surface, &surface_listener, window);
3997
Pekka Paalanen35e82632013-04-25 13:57:48 +03003998 wl_list_insert(&window->subsurface_list, &surface->link);
3999
Pekka Paalanen4e373742013-02-13 16:17:13 +02004000 return surface;
4001}
4002
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004003static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004004window_create_internal(struct display *display,
4005 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004006{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004007 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004008 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004009
4010 window = malloc(sizeof *window);
4011 if (window == NULL)
4012 return NULL;
4013
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004014 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004015 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004016 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004017 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004018
4019 surface = surface_create(window);
4020 window->main_surface = surface;
4021
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004022 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004023 window->shell_surface =
4024 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004025 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004026 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004027
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004028 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004029 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004030 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004031
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004032 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004033#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004034 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004035#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004036 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004037#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004038 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004039 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004040
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004041 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004042 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004043 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004044
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004045 if (window->shell_surface) {
4046 wl_shell_surface_set_user_data(window->shell_surface, window);
4047 wl_shell_surface_add_listener(window->shell_surface,
4048 &shell_surface_listener, window);
4049 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004050
Rob Bradford7507b572012-05-15 17:55:34 +01004051 wl_list_init (&window->window_output_list);
4052
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004053 return window;
4054}
4055
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004056struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004057window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004058{
4059 struct window *window;
4060
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004061 window = window_create_internal(display, NULL, TYPE_NONE);
4062 if (!window)
4063 return NULL;
4064
4065 return window;
4066}
4067
4068struct window *
4069window_create_custom(struct display *display)
4070{
4071 struct window *window;
4072
4073 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004074 if (!window)
4075 return NULL;
4076
4077 return window;
4078}
4079
4080struct window *
4081window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004082 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004083{
4084 struct window *window;
4085
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004086 window = window_create_internal(parent->display,
4087 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004088 if (!window)
4089 return NULL;
4090
4091 window->x = x;
4092 window->y = y;
4093
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004094 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004095 wl_shell_surface_set_transient(
4096 window->shell_surface,
4097 window->parent->main_surface->surface,
4098 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004099
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004100 return window;
4101}
4102
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004103static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004104menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004105{
4106 int next;
4107
4108 next = (sy - 8) / 20;
4109 if (menu->current != next) {
4110 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004111 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004112 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004113}
4114
4115static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004116menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004117 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004118 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004119{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004120 struct menu *menu = data;
4121
4122 if (widget == menu->widget)
4123 menu_set_item(data, y);
4124
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004125 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004126}
4127
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004128static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004129menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004130 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004131{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004132 struct menu *menu = data;
4133
4134 if (widget == menu->widget)
4135 menu_set_item(data, y);
4136
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004137 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004138}
4139
4140static void
4141menu_leave_handler(struct widget *widget, struct input *input, void *data)
4142{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004143 struct menu *menu = data;
4144
4145 if (widget == menu->widget)
4146 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004147}
4148
4149static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004150menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004151 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004152 uint32_t button, enum wl_pointer_button_state state,
4153 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004154
4155{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004156 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004157
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004158 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4159 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004160 /* Either relase after press-drag-release or
4161 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004162 menu->func(menu->window->parent,
4163 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004164 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004165 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004166 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004167 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004168 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004169}
4170
4171static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004172menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004173{
4174 cairo_t *cr;
4175 const int32_t r = 3, margin = 3;
4176 struct menu *menu = data;
4177 int32_t width, height, i;
4178
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004179 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004180 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4181 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4182 cairo_paint(cr);
4183
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004184 width = widget->allocation.width;
4185 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004186 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004187
4188 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004189 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4190 cairo_fill(cr);
4191
4192 for (i = 0; i < menu->count; i++) {
4193 if (i == menu->current) {
4194 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4195 cairo_rectangle(cr, margin, i * 20 + margin,
4196 width - 2 * margin, 20);
4197 cairo_fill(cr);
4198 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4199 cairo_move_to(cr, 10, i * 20 + 16);
4200 cairo_show_text(cr, menu->entries[i]);
4201 } else {
4202 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4203 cairo_move_to(cr, 10, i * 20 + 16);
4204 cairo_show_text(cr, menu->entries[i]);
4205 }
4206 }
4207
4208 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004209}
4210
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004211void
4212window_show_menu(struct display *display,
4213 struct input *input, uint32_t time, struct window *parent,
4214 int32_t x, int32_t y,
4215 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004216{
4217 struct window *window;
4218 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004219 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004220
4221 menu = malloc(sizeof *menu);
4222 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004223 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004224
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004225 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004226 if (!window) {
4227 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004228 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004229 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004230
4231 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004232 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004233 menu->entries = entries;
4234 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004235 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004236 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004237 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004238 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004239 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004240 window->type = TYPE_MENU;
4241 window->x = x;
4242 window->y = y;
4243
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004244 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004245 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004246 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004247 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004248 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004249
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004250 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004251 widget_set_enter_handler(menu->widget, menu_enter_handler);
4252 widget_set_leave_handler(menu->widget, menu_leave_handler);
4253 widget_set_motion_handler(menu->widget, menu_motion_handler);
4254 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004255
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004256 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004257 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004258}
4259
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004260void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004261window_set_buffer_type(struct window *window, enum window_buffer_type type)
4262{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004263 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004264}
4265
Pekka Paalanen35e82632013-04-25 13:57:48 +03004266struct widget *
4267window_add_subsurface(struct window *window, void *data,
4268 enum subsurface_mode default_mode)
4269{
4270 struct widget *widget;
4271 struct surface *surface;
4272 struct wl_surface *parent;
4273 struct wl_subcompositor *subcompo = window->display->subcompositor;
4274
4275 if (!subcompo)
4276 return NULL;
4277
4278 surface = surface_create(window);
4279 widget = widget_create(window, surface, data);
4280 wl_list_init(&widget->link);
4281 surface->widget = widget;
4282
4283 parent = window->main_surface->surface;
4284 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4285 surface->surface,
4286 parent);
4287 surface->synchronized = 1;
4288
4289 switch (default_mode) {
4290 case SUBSURFACE_SYNCHRONIZED:
4291 surface->synchronized_default = 1;
4292 break;
4293 case SUBSURFACE_DESYNCHRONIZED:
4294 surface->synchronized_default = 0;
4295 break;
4296 default:
4297 assert(!"bad enum subsurface_mode");
4298 }
4299
4300 return widget;
4301}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004302
4303static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004304display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004305 struct wl_output *wl_output,
4306 int x, int y,
4307 int physical_width,
4308 int physical_height,
4309 int subpixel,
4310 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004311 const char *model,
4312 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004313{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004314 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004315
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004316 output->allocation.x = x;
4317 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004318 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004319}
4320
4321static void
4322display_handle_mode(void *data,
4323 struct wl_output *wl_output,
4324 uint32_t flags,
4325 int width,
4326 int height,
4327 int refresh)
4328{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004329 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004330 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004331
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004332 if (flags & WL_OUTPUT_MODE_CURRENT) {
4333 output->allocation.width = width;
4334 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004335 if (display->output_configure_handler)
4336 (*display->output_configure_handler)(
4337 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004338 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004339}
4340
4341static const struct wl_output_listener output_listener = {
4342 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004343 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004344};
4345
4346static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004347display_add_output(struct display *d, uint32_t id)
4348{
4349 struct output *output;
4350
4351 output = malloc(sizeof *output);
4352 if (output == NULL)
4353 return;
4354
4355 memset(output, 0, sizeof *output);
4356 output->display = d;
4357 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004358 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004359 wl_list_insert(d->output_list.prev, &output->link);
4360
4361 wl_output_add_listener(output->output, &output_listener, output);
4362}
4363
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004364static void
4365output_destroy(struct output *output)
4366{
4367 if (output->destroy_handler)
4368 (*output->destroy_handler)(output, output->user_data);
4369
4370 wl_output_destroy(output->output);
4371 wl_list_remove(&output->link);
4372 free(output);
4373}
4374
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004375void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004376display_set_global_handler(struct display *display,
4377 display_global_handler_t handler)
4378{
4379 struct global *global;
4380
4381 display->global_handler = handler;
4382 if (!handler)
4383 return;
4384
4385 wl_list_for_each(global, &display->global_list, link)
4386 display->global_handler(display,
4387 global->name, global->interface,
4388 global->version, display->user_data);
4389}
4390
4391void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004392display_set_output_configure_handler(struct display *display,
4393 display_output_handler_t handler)
4394{
4395 struct output *output;
4396
4397 display->output_configure_handler = handler;
4398 if (!handler)
4399 return;
4400
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004401 wl_list_for_each(output, &display->output_list, link) {
4402 if (output->allocation.width == 0 &&
4403 output->allocation.height == 0)
4404 continue;
4405
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004406 (*display->output_configure_handler)(output,
4407 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004408 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004409}
4410
4411void
4412output_set_user_data(struct output *output, void *data)
4413{
4414 output->user_data = data;
4415}
4416
4417void *
4418output_get_user_data(struct output *output)
4419{
4420 return output->user_data;
4421}
4422
4423void
4424output_set_destroy_handler(struct output *output,
4425 display_output_handler_t handler)
4426{
4427 output->destroy_handler = handler;
4428 /* FIXME: implement this, once we have way to remove outputs */
4429}
4430
4431void
Scott Moreau4e072362012-09-29 02:03:11 -06004432output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004433{
Scott Moreau4e072362012-09-29 02:03:11 -06004434 struct rectangle allocation = output->allocation;
4435
4436 switch (output->transform) {
4437 case WL_OUTPUT_TRANSFORM_90:
4438 case WL_OUTPUT_TRANSFORM_270:
4439 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4440 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4441 /* Swap width and height */
4442 allocation.width = output->allocation.height;
4443 allocation.height = output->allocation.width;
4444 break;
4445 }
4446
4447 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004448}
4449
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004450struct wl_output *
4451output_get_wl_output(struct output *output)
4452{
4453 return output->output;
4454}
4455
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004456enum wl_output_transform
4457output_get_transform(struct output *output)
4458{
4459 return output->transform;
4460}
4461
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004462static void
Daniel Stone97f68542012-05-30 16:32:01 +01004463fini_xkb(struct input *input)
4464{
4465 xkb_state_unref(input->xkb.state);
4466 xkb_map_unref(input->xkb.keymap);
4467}
4468
4469static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004470display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004471{
4472 struct input *input;
4473
4474 input = malloc(sizeof *input);
4475 if (input == NULL)
4476 return;
4477
4478 memset(input, 0, sizeof *input);
4479 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004480 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004481 input->pointer_focus = NULL;
4482 input->keyboard_focus = NULL;
4483 wl_list_insert(d->input_list.prev, &input->link);
4484
Daniel Stone37816df2012-05-16 18:45:18 +01004485 wl_seat_add_listener(input->seat, &seat_listener, input);
4486 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004487
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004488 input->data_device =
4489 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004490 input->seat);
4491 wl_data_device_add_listener(input->data_device, &data_device_listener,
4492 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004493
4494 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004495
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004496 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4497 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004498 input->repeat_task.run = keyboard_repeat_func;
4499 display_watch_fd(d, input->repeat_timer_fd,
4500 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004501}
4502
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004503static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004504input_destroy(struct input *input)
4505{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004506 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004507 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004508
4509 if (input->drag_offer)
4510 data_offer_destroy(input->drag_offer);
4511
4512 if (input->selection_offer)
4513 data_offer_destroy(input->selection_offer);
4514
4515 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004516 fini_xkb(input);
4517
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004518 wl_surface_destroy(input->pointer_surface);
4519
Pekka Paalanene1207c72011-12-16 12:02:09 +02004520 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004521 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004522 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004523 free(input);
4524}
4525
4526static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004527init_workspace_manager(struct display *d, uint32_t id)
4528{
4529 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004530 wl_registry_bind(d->registry, id,
4531 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004532 if (d->workspace_manager != NULL)
4533 workspace_manager_add_listener(d->workspace_manager,
4534 &workspace_manager_listener,
4535 d);
4536}
4537
4538static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004539registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4540 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004541{
4542 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004543 struct global *global;
4544
4545 global = malloc(sizeof *global);
4546 global->name = id;
4547 global->interface = strdup(interface);
4548 global->version = version;
4549 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004550
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004551 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004552 d->compositor = wl_registry_bind(registry, id,
4553 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004554 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004555 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004556 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004557 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004558 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004559 d->shell = wl_registry_bind(registry,
4560 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004561 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004562 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004563 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4564 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004565 wl_registry_bind(registry, id,
4566 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004567 } else if (strcmp(interface, "text_cursor_position") == 0) {
4568 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004569 wl_registry_bind(registry, id,
4570 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004571 } else if (strcmp(interface, "workspace_manager") == 0) {
4572 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004573 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4574 d->subcompositor =
4575 wl_registry_bind(registry, id,
4576 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004577 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004578
4579 if (d->global_handler)
4580 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004581}
4582
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004583static void
4584registry_handle_global_remove(void *data, struct wl_registry *registry,
4585 uint32_t name)
4586{
4587 struct display *d = data;
4588 struct global *global;
4589 struct global *tmp;
4590
4591 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4592 if (global->name != name)
4593 continue;
4594
4595 /* XXX: Should destroy bound globals, and call
4596 * the counterpart of display::global_handler
4597 */
4598 wl_list_remove(&global->link);
4599 free(global->interface);
4600 free(global);
4601 }
4602}
4603
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004604void *
4605display_bind(struct display *display, uint32_t name,
4606 const struct wl_interface *interface, uint32_t version)
4607{
4608 return wl_registry_bind(display->registry, name, interface, version);
4609}
4610
4611static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004612 registry_handle_global,
4613 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004614};
4615
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004616#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004617static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004618init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004619{
4620 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004621 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004622
Rob Clark6396ed32012-03-11 19:48:41 -05004623#ifdef USE_CAIRO_GLESV2
4624# define GL_BIT EGL_OPENGL_ES2_BIT
4625#else
4626# define GL_BIT EGL_OPENGL_BIT
4627#endif
4628
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004629 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004630 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004631 EGL_RED_SIZE, 1,
4632 EGL_GREEN_SIZE, 1,
4633 EGL_BLUE_SIZE, 1,
4634 EGL_ALPHA_SIZE, 1,
4635 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004636 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004637 EGL_NONE
4638 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004639
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004640#ifdef USE_CAIRO_GLESV2
4641 static const EGLint context_attribs[] = {
4642 EGL_CONTEXT_CLIENT_VERSION, 2,
4643 EGL_NONE
4644 };
4645 EGLint api = EGL_OPENGL_ES_API;
4646#else
4647 EGLint *context_attribs = NULL;
4648 EGLint api = EGL_OPENGL_API;
4649#endif
4650
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004651 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004652 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004653 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004654 return -1;
4655 }
4656
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004657 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004658 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004659 return -1;
4660 }
4661
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004662 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4663 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004664 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004665 return -1;
4666 }
4667
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004668 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004669 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004670 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004671 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004672 return -1;
4673 }
4674
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004675 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004676 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004677 return -1;
4678 }
4679
Benjamin Franzke0c991632011-09-27 21:57:31 +02004680 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4681 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004682 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004683 return -1;
4684 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004685
4686 return 0;
4687}
4688
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004689static void
4690fini_egl(struct display *display)
4691{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004692 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004693
4694 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4695 EGL_NO_CONTEXT);
4696
4697 eglTerminate(display->dpy);
4698 eglReleaseThread();
4699}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004700#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004701
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004702static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004703init_dummy_surface(struct display *display)
4704{
4705 int len;
4706 void *data;
4707
4708 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4709 data = malloc(len);
4710 display->dummy_surface =
4711 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4712 1, 1, len);
4713 display->dummy_surface_data = data;
4714}
4715
4716static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004717handle_display_data(struct task *task, uint32_t events)
4718{
4719 struct display *display =
4720 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004721 struct epoll_event ep;
4722 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004723
4724 display->display_fd_events = events;
4725
4726 if (events & EPOLLERR || events & EPOLLHUP) {
4727 display_exit(display);
4728 return;
4729 }
4730
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004731 if (events & EPOLLIN) {
4732 ret = wl_display_dispatch(display->display);
4733 if (ret == -1) {
4734 display_exit(display);
4735 return;
4736 }
4737 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004738
4739 if (events & EPOLLOUT) {
4740 ret = wl_display_flush(display->display);
4741 if (ret == 0) {
4742 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4743 ep.data.ptr = &display->display_task;
4744 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4745 display->display_fd, &ep);
4746 } else if (ret == -1 && errno != EAGAIN) {
4747 display_exit(display);
4748 return;
4749 }
4750 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004751}
4752
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004753static void
4754log_handler(const char *format, va_list args)
4755{
4756 vfprintf(stderr, format, args);
4757}
4758
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004759struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004760display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004761{
4762 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004763
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004764 wl_log_set_handler_client(log_handler);
4765
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004766 d = malloc(sizeof *d);
4767 if (d == NULL)
4768 return NULL;
4769
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004770 memset(d, 0, sizeof *d);
4771
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004772 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004773 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004774 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004775 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004776 return NULL;
4777 }
4778
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004779 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004780 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004781 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004782 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4783 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004784
4785 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004786 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004787 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004788 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004789
Daniel Stone97f68542012-05-30 16:32:01 +01004790 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004791 if (d->xkb_context == NULL) {
4792 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004793 return NULL;
4794 }
4795
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004796 d->workspace = 0;
4797 d->workspace_count = 1;
4798
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004799 d->registry = wl_display_get_registry(d->display);
4800 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004801
4802 if (wl_display_dispatch(d->display) < 0) {
4803 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4804 return NULL;
4805 }
4806
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004807#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004808 if (init_egl(d) < 0)
4809 fprintf(stderr, "EGL does not seem to work, "
4810 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004811#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004812
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004813 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004814
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004815 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004816
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004817 wl_list_init(&d->window_list);
4818
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004819 init_dummy_surface(d);
4820
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004821 return d;
4822}
4823
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004824static void
4825display_destroy_outputs(struct display *display)
4826{
4827 struct output *tmp;
4828 struct output *output;
4829
4830 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4831 output_destroy(output);
4832}
4833
Pekka Paalanene1207c72011-12-16 12:02:09 +02004834static void
4835display_destroy_inputs(struct display *display)
4836{
4837 struct input *tmp;
4838 struct input *input;
4839
4840 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4841 input_destroy(input);
4842}
4843
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004844void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004845display_destroy(struct display *display)
4846{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004847 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004848 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4849 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004850
4851 if (!wl_list_empty(&display->deferred_list))
4852 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4853
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004854 cairo_surface_destroy(display->dummy_surface);
4855 free(display->dummy_surface_data);
4856
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004857 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004858 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004859
Daniel Stone97f68542012-05-30 16:32:01 +01004860 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004861
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004862 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004863 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004864
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004865#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004866 if (display->argb_device)
4867 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004868#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004869
Pekka Paalanen35e82632013-04-25 13:57:48 +03004870 if (display->subcompositor)
4871 wl_subcompositor_destroy(display->subcompositor);
4872
Pekka Paalanenc2052982011-12-16 11:41:32 +02004873 if (display->shell)
4874 wl_shell_destroy(display->shell);
4875
4876 if (display->shm)
4877 wl_shm_destroy(display->shm);
4878
4879 if (display->data_device_manager)
4880 wl_data_device_manager_destroy(display->data_device_manager);
4881
4882 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004883 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004884
4885 close(display->epoll_fd);
4886
U. Artie Eoff44874d92012-10-02 21:12:35 -07004887 if (!(display->display_fd_events & EPOLLERR) &&
4888 !(display->display_fd_events & EPOLLHUP))
4889 wl_display_flush(display->display);
4890
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004891 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004892 free(display);
4893}
4894
4895void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004896display_set_user_data(struct display *display, void *data)
4897{
4898 display->user_data = data;
4899}
4900
4901void *
4902display_get_user_data(struct display *display)
4903{
4904 return display->user_data;
4905}
4906
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004907struct wl_display *
4908display_get_display(struct display *display)
4909{
4910 return display->display;
4911}
4912
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004913struct output *
4914display_get_output(struct display *display)
4915{
4916 return container_of(display->output_list.next, struct output, link);
4917}
4918
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004919struct wl_compositor *
4920display_get_compositor(struct display *display)
4921{
4922 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004923}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004924
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004925uint32_t
4926display_get_serial(struct display *display)
4927{
4928 return display->serial;
4929}
4930
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004931EGLDisplay
4932display_get_egl_display(struct display *d)
4933{
4934 return d->dpy;
4935}
4936
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004937struct wl_data_source *
4938display_create_data_source(struct display *display)
4939{
4940 return wl_data_device_manager_create_data_source(display->data_device_manager);
4941}
4942
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004943EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004944display_get_argb_egl_config(struct display *d)
4945{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004946 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004947}
4948
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004949struct wl_shell *
4950display_get_shell(struct display *display)
4951{
4952 return display->shell;
4953}
4954
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004955int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004956display_acquire_window_surface(struct display *display,
4957 struct window *window,
4958 EGLContext ctx)
4959{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004960 struct surface *surface = window->main_surface;
4961
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004962 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004963 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004964
Pekka Paalanen6f41b072013-02-20 13:39:17 +02004965 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004966 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004967}
4968
4969void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004970display_release_window_surface(struct display *display,
4971 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004972{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004973 struct surface *surface = window->main_surface;
4974
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004975 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004976 return;
4977
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004978 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004979}
4980
4981void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004982display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004983{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004984 wl_list_insert(&display->deferred_list, &task->link);
4985}
4986
4987void
4988display_watch_fd(struct display *display,
4989 int fd, uint32_t events, struct task *task)
4990{
4991 struct epoll_event ep;
4992
4993 ep.events = events;
4994 ep.data.ptr = task;
4995 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4996}
4997
4998void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004999display_unwatch_fd(struct display *display, int fd)
5000{
5001 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5002}
5003
5004void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005005display_run(struct display *display)
5006{
5007 struct task *task;
5008 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005009 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005010
Pekka Paalanen826d7952011-12-15 10:14:07 +02005011 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005012 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005013 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005014 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005015 struct task, link);
5016 wl_list_remove(&task->link);
5017 task->run(task, 0);
5018 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005019
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005020 wl_display_dispatch_pending(display->display);
5021
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005022 if (!display->running)
5023 break;
5024
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005025 ret = wl_display_flush(display->display);
5026 if (ret < 0 && errno == EAGAIN) {
5027 ep[0].events =
5028 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5029 ep[0].data.ptr = &display->display_task;
5030
5031 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5032 display->display_fd, &ep[0]);
5033 } else if (ret < 0) {
5034 break;
5035 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005036
5037 count = epoll_wait(display->epoll_fd,
5038 ep, ARRAY_LENGTH(ep), -1);
5039 for (i = 0; i < count; i++) {
5040 task = ep[i].data.ptr;
5041 task->run(task, ep[i].events);
5042 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005043 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005044}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005045
5046void
5047display_exit(struct display *display)
5048{
5049 display->running = 0;
5050}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005051
5052void
5053keysym_modifiers_add(struct wl_array *modifiers_map,
5054 const char *name)
5055{
5056 size_t len = strlen(name) + 1;
5057 char *p;
5058
5059 p = wl_array_add(modifiers_map, len);
5060
5061 if (p == NULL)
5062 return;
5063
5064 strncpy(p, name, len);
5065}
5066
5067static xkb_mod_index_t
5068keysym_modifiers_get_index(struct wl_array *modifiers_map,
5069 const char *name)
5070{
5071 xkb_mod_index_t index = 0;
5072 char *p = modifiers_map->data;
5073
5074 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5075 if (strcmp(p, name) == 0)
5076 return index;
5077
5078 index++;
5079 p += strlen(p) + 1;
5080 }
5081
5082 return XKB_MOD_INVALID;
5083}
5084
5085xkb_mod_mask_t
5086keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5087 const char *name)
5088{
5089 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5090
5091 if (index == XKB_MOD_INVALID)
5092 return XKB_MOD_INVALID;
5093
5094 return 1 << index;
5095}