blob: 1562957982af65c23dde07d8e54ee6a01d1165f6 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040024#define _GNU_SOURCE
25
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026#include "../config.h"
27
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030031#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033#include <fcntl.h>
34#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040035#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020037#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050038#include <time.h>
39#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040040#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040041#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030042#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040043
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040069#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060070#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020071#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030072#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050073
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050074#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050075
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070076struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030077
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040078struct global {
79 uint32_t name;
80 char *interface;
81 uint32_t version;
82 struct wl_list link;
83};
84
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050085struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050086 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040087 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050088 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030089 struct wl_subcompositor *subcompositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040090 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040091 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040092 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060093 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020094 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040095 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050096 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020098 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040099 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400100
101 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700102 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400103 struct task display_task;
104
105 int epoll_fd;
106 struct wl_list deferred_list;
107
Pekka Paalanen826d7952011-12-15 10:14:07 +0200108 int running;
109
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400110 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400111 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400112 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500113 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400114
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400115 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400116
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300117 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300118 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400119
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400121 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133};
134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400136 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400138 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500139 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500141 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_CUSTOM
143};
Rob Bradford7507b572012-05-15 17:55:34 +0100144
145struct window_output {
146 struct output *output;
147 struct wl_list link;
148};
149
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150struct toysurface {
151 /*
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
155 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200156 * If flags has SURFACE_HINT_RESIZE set, the user is
157 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200158 * Returns the Cairo surface to draw to.
159 */
160 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200161 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200162
163 /*
164 * Post the surface to the server, returning the server allocation
165 * rectangle. The Cairo surface from prepare() must be destroyed
166 * after calling this.
167 */
168 void (*swap)(struct toysurface *base,
169 struct rectangle *server_allocation);
170
171 /*
172 * Make the toysurface current with the given EGL context.
173 * Returns 0 on success, and negative of failure.
174 */
175 int (*acquire)(struct toysurface *base, EGLContext ctx);
176
177 /*
178 * Release the toysurface from the EGL context, returning control
179 * to Cairo.
180 */
181 void (*release)(struct toysurface *base);
182
183 /*
184 * Destroy the toysurface, including the Cairo surface, any
185 * backing storage, and the Wayland protocol objects.
186 */
187 void (*destroy)(struct toysurface *base);
188};
189
Pekka Paalanen4e373742013-02-13 16:17:13 +0200190struct surface {
191 struct window *window;
192
193 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300194 struct wl_subsurface *subsurface;
195 int synchronized;
196 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200198 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300199 int redraw_needed;
200 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300201 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200202
203 struct rectangle allocation;
204 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200205
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200206 struct wl_region *input_region;
207 struct wl_region *opaque_region;
208
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209 enum window_buffer_type buffer_type;
210 enum wl_output_transform buffer_transform;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200211
212 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300213
214 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200215};
216
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500217struct window {
218 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500219 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100220 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500221 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200222 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400223 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500224 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500225 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400226 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400227 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300228 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400229 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400230 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100231 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400232 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400233 int focus_count;
234
Pekka Paalanen99436862012-11-19 17:15:59 +0200235 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200236 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000237 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400238
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500239 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500240 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400241 window_data_handler_t data_handler;
242 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500243 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400244 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200245 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400246
Pekka Paalanen4e373742013-02-13 16:17:13 +0200247 struct surface *main_surface;
248 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300249
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200250 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500251
Pekka Paalanen35e82632013-04-25 13:57:48 +0300252 /* struct surface::link, contains also main_surface */
253 struct wl_list subsurface_list;
254
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500255 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400256 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500257};
258
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500259struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500260 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200261 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300262 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500263 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400264 struct wl_list link;
265 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500266 widget_resize_handler_t resize_handler;
267 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500268 widget_enter_handler_t enter_handler;
269 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500270 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500271 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200272 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400273 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500274 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300275 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500276 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400277};
278
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400279struct input {
280 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100281 struct wl_seat *seat;
282 struct wl_pointer *pointer;
283 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400284 struct window *pointer_focus;
285 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300286 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300287 uint32_t cursor_anim_start;
288 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300289 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400290 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400291 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400292 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400293 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400294 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400295
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500296 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500297 struct widget *grab;
298 uint32_t grab_button;
299
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400300 struct wl_data_device *data_device;
301 struct data_offer *drag_offer;
302 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100303
304 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100305 struct xkb_keymap *keymap;
306 struct xkb_state *state;
307 xkb_mod_mask_t control_mask;
308 xkb_mod_mask_t alt_mask;
309 xkb_mod_mask_t shift_mask;
310 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400311
312 struct task repeat_task;
313 int repeat_timer_fd;
314 uint32_t repeat_sym;
315 uint32_t repeat_key;
316 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400317};
318
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500319struct output {
320 struct display *display;
321 struct wl_output *output;
322 struct rectangle allocation;
323 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600324 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200325
326 display_output_handler_t destroy_handler;
327 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500328};
329
Martin Minarik1998b152012-05-10 02:04:35 +0200330enum frame_button_action {
331 FRAME_BUTTON_NULL = 0,
332 FRAME_BUTTON_ICON = 1,
333 FRAME_BUTTON_CLOSE = 2,
334 FRAME_BUTTON_MINIMIZE = 3,
335 FRAME_BUTTON_MAXIMIZE = 4,
336};
337
338enum frame_button_pointer {
339 FRAME_BUTTON_DEFAULT = 0,
340 FRAME_BUTTON_OVER = 1,
341 FRAME_BUTTON_ACTIVE = 2,
342};
343
344enum frame_button_align {
345 FRAME_BUTTON_RIGHT = 0,
346 FRAME_BUTTON_LEFT = 1,
347};
348
349enum frame_button_decoration {
350 FRAME_BUTTON_NONE = 0,
351 FRAME_BUTTON_FANCY = 1,
352};
353
354struct frame_button {
355 struct widget *widget;
356 struct frame *frame;
357 cairo_surface_t *icon;
358 enum frame_button_action type;
359 enum frame_button_pointer state;
360 struct wl_list link; /* buttons_list */
361 enum frame_button_align align;
362 enum frame_button_decoration decoration;
363};
364
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500365struct frame {
366 struct widget *widget;
367 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200368 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500369};
370
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500371struct menu {
372 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500373 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500374 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500375 const char **entries;
376 uint32_t time;
377 int current;
378 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400379 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500380 menu_func_t func;
381};
382
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300383struct tooltip {
384 struct widget *parent;
385 struct window *window;
386 struct widget *widget;
387 char *entry;
388 struct task tooltip_task;
389 int tooltip_fd;
390 float x, y;
391};
392
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700393struct shm_pool {
394 struct wl_shm_pool *pool;
395 size_t size;
396 size_t used;
397 void *data;
398};
399
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400400enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300401 CURSOR_DEFAULT = 100,
402 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400403};
404
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500405enum window_location {
406 WINDOW_INTERIOR = 0,
407 WINDOW_RESIZING_TOP = 1,
408 WINDOW_RESIZING_BOTTOM = 2,
409 WINDOW_RESIZING_LEFT = 4,
410 WINDOW_RESIZING_TOP_LEFT = 5,
411 WINDOW_RESIZING_BOTTOM_LEFT = 6,
412 WINDOW_RESIZING_RIGHT = 8,
413 WINDOW_RESIZING_TOP_RIGHT = 9,
414 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
415 WINDOW_RESIZING_MASK = 15,
416 WINDOW_EXTERIOR = 16,
417 WINDOW_TITLEBAR = 17,
418 WINDOW_CLIENT_AREA = 18,
419};
420
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200421static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422
Pekka Paalanen71233882013-04-25 13:57:53 +0300423#if 0
424
425static void
426debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
427__attribute__ ((format (printf, 4, 5)));
428
429static void
430debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
431{
432 va_list ap;
433 struct timeval tv;
434
435 gettimeofday(&tv, NULL);
436 fprintf(stderr, "%8ld.%03ld ",
437 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
438
439 if (proxy)
440 fprintf(stderr, "%s@%d ",
441 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
442
443 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
444 fprintf(stderr, "%s ", func);
445
446 va_start(ap, fmt);
447 vfprintf(stderr, fmt, ap);
448 va_end(ap);
449}
450
451#define DBG(fmt, ...) \
452 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
453
454#define DBG_OBJ(obj, fmt, ...) \
455 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
456
457#else
458
459#define DBG(...) do {} while (0)
460#define DBG_OBJ(...) do {} while (0)
461
462#endif
463
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500464#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400465
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200466struct egl_window_surface {
467 struct toysurface base;
468 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100469 struct display *display;
470 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200471 struct wl_egl_window *egl_window;
472 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100473};
474
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200475static struct egl_window_surface *
476to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100477{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200478 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100479}
480
481static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200482egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200483 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100484{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200485 struct egl_window_surface *surface = to_egl_window_surface(base);
486
487 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
488 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
489
490 return cairo_surface_reference(surface->cairo_surface);
491}
492
493static void
494egl_window_surface_swap(struct toysurface *base,
495 struct rectangle *server_allocation)
496{
497 struct egl_window_surface *surface = to_egl_window_surface(base);
498
499 cairo_gl_surface_swapbuffers(surface->cairo_surface);
500 wl_egl_window_get_attached_size(surface->egl_window,
501 &server_allocation->width,
502 &server_allocation->height);
503}
504
505static int
506egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
507{
508 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200509 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100510
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511 device = cairo_surface_get_device(surface->cairo_surface);
512 if (!device)
513 return -1;
514
515 if (!ctx) {
516 if (device == surface->display->argb_device)
517 ctx = surface->display->argb_ctx;
518 else
519 assert(0);
520 }
521
522 cairo_device_flush(device);
523 cairo_device_acquire(device);
524 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
525 surface->egl_surface, ctx))
526 fprintf(stderr, "failed to make surface current\n");
527
528 return 0;
529}
530
531static void
532egl_window_surface_release(struct toysurface *base)
533{
534 struct egl_window_surface *surface = to_egl_window_surface(base);
535 cairo_device_t *device;
536
537 device = cairo_surface_get_device(surface->cairo_surface);
538 if (!device)
539 return;
540
541 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
542 surface->display->argb_ctx))
543 fprintf(stderr, "failed to make context current\n");
544
545 cairo_device_release(device);
546}
547
548static void
549egl_window_surface_destroy(struct toysurface *base)
550{
551 struct egl_window_surface *surface = to_egl_window_surface(base);
552 struct display *d = surface->display;
553
554 cairo_surface_destroy(surface->cairo_surface);
555 eglDestroySurface(d->dpy, surface->egl_surface);
556 wl_egl_window_destroy(surface->egl_window);
557 surface->surface = NULL;
558
559 free(surface);
560}
561
562static struct toysurface *
563egl_window_surface_create(struct display *display,
564 struct wl_surface *wl_surface,
565 uint32_t flags,
566 struct rectangle *rectangle)
567{
568 struct egl_window_surface *surface;
569
Pekka Paalanenb3627362012-11-19 17:16:00 +0200570 if (display->dpy == EGL_NO_DISPLAY)
571 return NULL;
572
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200573 surface = calloc(1, sizeof *surface);
574 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100575 return NULL;
576
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200577 surface->base.prepare = egl_window_surface_prepare;
578 surface->base.swap = egl_window_surface_swap;
579 surface->base.acquire = egl_window_surface_acquire;
580 surface->base.release = egl_window_surface_release;
581 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100582
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200583 surface->display = display;
584 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400585
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200586 surface->egl_window = wl_egl_window_create(surface->surface,
587 rectangle->width,
588 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100589
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200590 surface->egl_surface = eglCreateWindowSurface(display->dpy,
591 display->argb_config,
592 surface->egl_window,
593 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100594
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200595 surface->cairo_surface =
596 cairo_gl_surface_create_for_egl(display->argb_device,
597 surface->egl_surface,
598 rectangle->width,
599 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100600
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200601 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100602}
603
Pekka Paalanenb3627362012-11-19 17:16:00 +0200604#else
605
606static struct toysurface *
607egl_window_surface_create(struct display *display,
608 struct wl_surface *wl_surface,
609 uint32_t flags,
610 struct rectangle *rectangle)
611{
612 return NULL;
613}
614
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615#endif
616
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200617struct shm_surface_data {
618 struct wl_buffer *buffer;
619 struct shm_pool *pool;
620};
621
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622struct wl_buffer *
623display_get_buffer_for_surface(struct display *display,
624 cairo_surface_t *surface)
625{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200626 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200628 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629
630 return data->buffer;
631}
632
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500633static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700634shm_pool_destroy(struct shm_pool *pool);
635
636static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637shm_surface_data_destroy(void *p)
638{
639 struct shm_surface_data *data = p;
640
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200641 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700642 if (data->pool)
643 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300644
645 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400646}
647
Kristian Høgsberg16626282012-04-03 11:21:27 -0400648static struct wl_shm_pool *
649make_shm_pool(struct display *display, int size, void **data)
650{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400651 struct wl_shm_pool *pool;
652 int fd;
653
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300654 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400655 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300656 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
657 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400658 return NULL;
659 }
660
661 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400662 if (*data == MAP_FAILED) {
663 fprintf(stderr, "mmap failed: %m\n");
664 close(fd);
665 return NULL;
666 }
667
668 pool = wl_shm_create_pool(display->shm, fd, size);
669
670 close(fd);
671
672 return pool;
673}
674
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700675static struct shm_pool *
676shm_pool_create(struct display *display, size_t size)
677{
678 struct shm_pool *pool = malloc(sizeof *pool);
679
680 if (!pool)
681 return NULL;
682
683 pool->pool = make_shm_pool(display, size, &pool->data);
684 if (!pool->pool) {
685 free(pool);
686 return NULL;
687 }
688
689 pool->size = size;
690 pool->used = 0;
691
692 return pool;
693}
694
695static void *
696shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
697{
698 if (pool->used + size > pool->size)
699 return NULL;
700
701 *offset = pool->used;
702 pool->used += size;
703
704 return (char *) pool->data + *offset;
705}
706
707/* destroy the pool. this does not unmap the memory though */
708static void
709shm_pool_destroy(struct shm_pool *pool)
710{
711 munmap(pool->data, pool->size);
712 wl_shm_pool_destroy(pool->pool);
713 free(pool);
714}
715
716/* Start allocating from the beginning of the pool again */
717static void
718shm_pool_reset(struct shm_pool *pool)
719{
720 pool->used = 0;
721}
722
723static int
724data_length_for_shm_surface(struct rectangle *rect)
725{
726 int stride;
727
728 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
729 rect->width);
730 return stride * rect->height;
731}
732
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500733static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700734display_create_shm_surface_from_pool(struct display *display,
735 struct rectangle *rectangle,
736 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400737{
738 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400739 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400740 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700741 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400742 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400743
744 data = malloc(sizeof *data);
745 if (data == NULL)
746 return NULL;
747
748 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
749 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700750 length = stride * rectangle->height;
751 data->pool = NULL;
752 map = shm_pool_allocate(pool, length, &offset);
753
754 if (!map) {
755 free(data);
756 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400757 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400758
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400759 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400760 CAIRO_FORMAT_ARGB32,
761 rectangle->width,
762 rectangle->height,
763 stride);
764
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200765 cairo_surface_set_user_data(surface, &shm_surface_data_key,
766 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400767
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400768 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500769 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400770 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500771 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400772
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200773 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
774 rectangle->width,
775 rectangle->height,
776 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400777
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700778 return surface;
779}
780
781static cairo_surface_t *
782display_create_shm_surface(struct display *display,
783 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200784 struct shm_pool *alternate_pool,
785 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700786{
787 struct shm_surface_data *data;
788 struct shm_pool *pool;
789 cairo_surface_t *surface;
790
Pekka Paalanen99436862012-11-19 17:15:59 +0200791 if (alternate_pool) {
792 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700793 surface = display_create_shm_surface_from_pool(display,
794 rectangle,
795 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200796 alternate_pool);
797 if (surface) {
798 data = cairo_surface_get_user_data(surface,
799 &shm_surface_data_key);
800 goto out;
801 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700802 }
803
804 pool = shm_pool_create(display,
805 data_length_for_shm_surface(rectangle));
806 if (!pool)
807 return NULL;
808
809 surface =
810 display_create_shm_surface_from_pool(display, rectangle,
811 flags, pool);
812
813 if (!surface) {
814 shm_pool_destroy(pool);
815 return NULL;
816 }
817
818 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200819 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700820 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821
Pekka Paalanen99436862012-11-19 17:15:59 +0200822out:
823 if (data_ret)
824 *data_ret = data;
825
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826 return surface;
827}
828
nobled7b87cb02011-02-01 18:51:47 +0000829static int
830check_size(struct rectangle *rect)
831{
832 if (rect->width && rect->height)
833 return 0;
834
835 fprintf(stderr, "tried to create surface of "
836 "width: %d, height: %d\n", rect->width, rect->height);
837 return -1;
838}
839
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400840cairo_surface_t *
841display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100842 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400843 struct rectangle *rectangle,
844 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400845{
nobled7b87cb02011-02-01 18:51:47 +0000846 if (check_size(rectangle) < 0)
847 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200848
849 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200850 return display_create_shm_surface(display, rectangle, flags,
851 NULL, NULL);
852}
853
Pekka Paalanena4eda732012-11-19 17:16:02 +0200854struct shm_surface_leaf {
855 cairo_surface_t *cairo_surface;
856 /* 'data' is automatically destroyed, when 'cairo_surface' is */
857 struct shm_surface_data *data;
858
859 struct shm_pool *resize_pool;
860 int busy;
861};
862
863static void
864shm_surface_leaf_release(struct shm_surface_leaf *leaf)
865{
866 if (leaf->cairo_surface)
867 cairo_surface_destroy(leaf->cairo_surface);
868 /* leaf->data already destroyed via cairo private */
869
870 if (leaf->resize_pool)
871 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200872
873 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200874}
875
Pekka Paalanenaef02542013-04-25 13:57:47 +0300876#define MAX_LEAVES 3
877
Pekka Paalanen99436862012-11-19 17:15:59 +0200878struct shm_surface {
879 struct toysurface base;
880 struct display *display;
881 struct wl_surface *surface;
882 uint32_t flags;
883 int dx, dy;
884
Pekka Paalanenaef02542013-04-25 13:57:47 +0300885 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200886 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200887};
888
889static struct shm_surface *
890to_shm_surface(struct toysurface *base)
891{
892 return container_of(base, struct shm_surface, base);
893}
894
Pekka Paalanena4eda732012-11-19 17:16:02 +0200895static void
896shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
897{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200898 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300899 struct shm_surface_leaf *leaf;
900 int i;
901 int free_found;
Pekka Paalanen71233882013-04-25 13:57:53 +0300902 int available = MAX_LEAVES;
903 char bufs[MAX_LEAVES + 1];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200904
Pekka Paalanenaef02542013-04-25 13:57:47 +0300905 for (i = 0; i < MAX_LEAVES; i++) {
906 leaf = &surface->leaf[i];
907 if (leaf->data && leaf->data->buffer == buffer) {
908 leaf->busy = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +0300909 available = i;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300910 break;
911 }
912 }
913 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200914
Pekka Paalanenaef02542013-04-25 13:57:47 +0300915 /* Leave one free leaf with storage, release others */
916 free_found = 0;
917 for (i = 0; i < MAX_LEAVES; i++) {
918 leaf = &surface->leaf[i];
919
Pekka Paalanen71233882013-04-25 13:57:53 +0300920 if (leaf->busy)
921 bufs[i] = 'b';
922 else if (leaf->cairo_surface)
923 bufs[i] = 'a';
924 else
925 bufs[i] = ' ';
926
Pekka Paalanenaef02542013-04-25 13:57:47 +0300927 if (!leaf->cairo_surface || leaf->busy)
928 continue;
929
930 if (!free_found)
931 free_found = 1;
Pekka Paalanen71233882013-04-25 13:57:53 +0300932 else {
Pekka Paalanenaef02542013-04-25 13:57:47 +0300933 shm_surface_leaf_release(leaf);
Pekka Paalanen71233882013-04-25 13:57:53 +0300934 bufs[i] = '*';
935 }
Pekka Paalanenaef02542013-04-25 13:57:47 +0300936 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300937
938 bufs[MAX_LEAVES] = '\0';
939 DBG_OBJ(surface->surface, "leaf %d released, leaves [%s]\n",
940 available, bufs);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200941}
942
943static const struct wl_buffer_listener shm_surface_buffer_listener = {
944 shm_surface_buffer_release
945};
946
Pekka Paalanen99436862012-11-19 17:15:59 +0200947static cairo_surface_t *
948shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200949 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200950{
Pekka Paalanenec076692012-11-30 13:37:27 +0200951 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200952 struct shm_surface *surface = to_shm_surface(base);
953 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanenaef02542013-04-25 13:57:47 +0300954 struct shm_surface_leaf *leaf = NULL;
955 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +0200956
957 surface->dx = dx;
958 surface->dy = dy;
959
Pekka Paalanenaef02542013-04-25 13:57:47 +0300960 /* pick a free buffer, preferrably one that already has storage */
961 for (i = 0; i < MAX_LEAVES; i++) {
962 if (surface->leaf[i].busy)
963 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200964
Pekka Paalanenaef02542013-04-25 13:57:47 +0300965 if (!leaf || surface->leaf[i].cairo_surface)
966 leaf = &surface->leaf[i];
967 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300968 DBG_OBJ(surface->surface, "pick leaf %d\n",
969 (int)(leaf - &surface->leaf[0]));
970
Pekka Paalanenaef02542013-04-25 13:57:47 +0300971 if (!leaf) {
972 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +0200973 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +0300974 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200975 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200976 }
977
Pekka Paalanena4eda732012-11-19 17:16:02 +0200978 if (!resize_hint && leaf->resize_pool) {
979 cairo_surface_destroy(leaf->cairo_surface);
980 leaf->cairo_surface = NULL;
981 shm_pool_destroy(leaf->resize_pool);
982 leaf->resize_pool = NULL;
983 }
984
985 if (leaf->cairo_surface &&
986 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
987 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200988 goto out;
989
Pekka Paalanena4eda732012-11-19 17:16:02 +0200990 if (leaf->cairo_surface)
991 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200992
Pekka Paalanena4eda732012-11-19 17:16:02 +0200993 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200994 /* Create a big pool to allocate from, while continuously
995 * resizing. Mmapping a new pool in the server
996 * is relatively expensive, so reusing a pool performs
997 * better, but may temporarily reserve unneeded memory.
998 */
999 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001000 leaf->resize_pool = shm_pool_create(surface->display,
1001 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001002 }
1003
Pekka Paalanena4eda732012-11-19 17:16:02 +02001004 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001005 display_create_shm_surface(surface->display, &rect,
1006 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001007 leaf->resize_pool,
1008 &leaf->data);
1009 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001010 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001011
1012out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001013 surface->current = leaf;
1014
1015 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001016}
1017
1018static void
1019shm_surface_swap(struct toysurface *base,
1020 struct rectangle *server_allocation)
1021{
1022 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001023 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001024
1025 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001026 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001027 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001028 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001029
Pekka Paalanena4eda732012-11-19 17:16:02 +02001030 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001031 surface->dx, surface->dy);
1032 wl_surface_damage(surface->surface, 0, 0,
1033 server_allocation->width, server_allocation->height);
1034 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001035
Pekka Paalanen71233882013-04-25 13:57:53 +03001036 DBG_OBJ(surface->surface, "leaf %d busy\n",
1037 (int)(leaf - &surface->leaf[0]));
1038
Pekka Paalanena4eda732012-11-19 17:16:02 +02001039 leaf->busy = 1;
1040 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001041}
1042
1043static int
1044shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1045{
1046 return -1;
1047}
1048
1049static void
1050shm_surface_release(struct toysurface *base)
1051{
1052}
1053
1054static void
1055shm_surface_destroy(struct toysurface *base)
1056{
1057 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001058 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001059
Pekka Paalanenaef02542013-04-25 13:57:47 +03001060 for (i = 0; i < MAX_LEAVES; i++)
1061 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001062
1063 free(surface);
1064}
1065
1066static struct toysurface *
1067shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1068 uint32_t flags, struct rectangle *rectangle)
1069{
1070 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001071 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001072
1073 surface = calloc(1, sizeof *surface);
1074 if (!surface)
1075 return NULL;
1076
1077 surface->base.prepare = shm_surface_prepare;
1078 surface->base.swap = shm_surface_swap;
1079 surface->base.acquire = shm_surface_acquire;
1080 surface->base.release = shm_surface_release;
1081 surface->base.destroy = shm_surface_destroy;
1082
1083 surface->display = display;
1084 surface->surface = wl_surface;
1085 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001086
1087 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001088}
1089
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001090/*
1091 * The following correspondences between file names and cursors was copied
1092 * from: https://bugs.kde.org/attachment.cgi?id=67313
1093 */
1094
1095static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001096 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001097 "sw-resize"
1098};
1099
1100static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001101 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001102 "se-resize"
1103};
1104
1105static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001106 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001107 "s-resize"
1108};
1109
1110static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001111 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001112 "closedhand",
1113 "208530c400c041818281048008011002"
1114};
1115
1116static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001117 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001118 "default",
1119 "top_left_arrow",
1120 "left-arrow"
1121};
1122
1123static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001124 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001125 "w-resize"
1126};
1127
1128static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001129 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001130 "e-resize"
1131};
1132
1133static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001134 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001135 "nw-resize"
1136};
1137
1138static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001139 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001140 "ne-resize"
1141};
1142
1143static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001144 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001145 "n-resize"
1146};
1147
1148static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001149 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001150 "ibeam",
1151 "text"
1152};
1153
1154static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001155 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001156 "pointer",
1157 "pointing_hand",
1158 "e29285e634086352946a0e7090d73106"
1159};
1160
1161static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001162 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001163 "wait",
1164 "0426c94ea35c87780ff01dc239897213"
1165};
1166
1167struct cursor_alternatives {
1168 const char **names;
1169 size_t count;
1170};
1171
1172static const struct cursor_alternatives cursors[] = {
1173 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1174 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1175 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1176 {grabbings, ARRAY_LENGTH(grabbings)},
1177 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1178 {left_sides, ARRAY_LENGTH(left_sides)},
1179 {right_sides, ARRAY_LENGTH(right_sides)},
1180 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1181 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1182 {top_sides, ARRAY_LENGTH(top_sides)},
1183 {xterms, ARRAY_LENGTH(xterms)},
1184 {hand1s, ARRAY_LENGTH(hand1s)},
1185 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001186};
1187
1188static void
1189create_cursors(struct display *display)
1190{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001191 char *config_file;
1192 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001193 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001194 unsigned int i, j;
1195 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001196 struct config_key shell_keys[] = {
1197 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001198 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001199 };
1200 struct config_section cs[] = {
1201 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1202 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001203
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001204 config_file = config_file_path("weston.ini");
1205 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1206 free(config_file);
1207
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001208 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001209 display->cursors =
1210 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1211
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001212 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001213 cursor = NULL;
1214 for (j = 0; !cursor && j < cursors[i].count; ++j)
1215 cursor = wl_cursor_theme_get_cursor(
1216 display->cursor_theme, cursors[i].names[j]);
1217
1218 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001219 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001220 cursors[i].names[0]);
1221
1222 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001223 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001224}
1225
1226static void
1227destroy_cursors(struct display *display)
1228{
1229 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001230 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001231}
1232
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001233struct wl_cursor_image *
1234display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001235{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001237
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001238 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001239}
1240
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001241static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001242surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001243{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001244 if (!surface->cairo_surface)
1245 return;
1246
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001247 if (surface->opaque_region) {
1248 wl_surface_set_opaque_region(surface->surface,
1249 surface->opaque_region);
1250 wl_region_destroy(surface->opaque_region);
1251 surface->opaque_region = NULL;
1252 }
1253
1254 if (surface->input_region) {
1255 wl_surface_set_input_region(surface->surface,
1256 surface->input_region);
1257 wl_region_destroy(surface->input_region);
1258 surface->input_region = NULL;
1259 }
1260
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001261 surface->toysurface->swap(surface->toysurface,
1262 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001263
Pekka Paalanen89dee002013-02-13 16:17:20 +02001264 cairo_surface_destroy(surface->cairo_surface);
1265 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001266}
1267
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001268int
1269window_has_focus(struct window *window)
1270{
1271 return window->focus_count > 0;
1272}
1273
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001274static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001275window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001276{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001277 struct surface *surface;
1278
Pekka Paalanen89dee002013-02-13 16:17:20 +02001279 if (window->type == TYPE_NONE) {
1280 window->type = TYPE_TOPLEVEL;
1281 if (window->shell_surface)
1282 wl_shell_surface_set_toplevel(window->shell_surface);
1283 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001284
Pekka Paalanen35e82632013-04-25 13:57:48 +03001285 wl_list_for_each(surface, &window->subsurface_list, link) {
1286 if (surface == window->main_surface)
1287 continue;
1288
1289 surface_flush(surface);
1290 }
1291
Pekka Paalanen89dee002013-02-13 16:17:20 +02001292 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001293}
1294
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001295struct display *
1296window_get_display(struct window *window)
1297{
1298 return window->display;
1299}
1300
Pekka Paalanen89dee002013-02-13 16:17:20 +02001301static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001302surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001303{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001304 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001305 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001306
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001307 switch (surface->buffer_transform) {
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001308 case WL_OUTPUT_TRANSFORM_90:
1309 case WL_OUTPUT_TRANSFORM_270:
1310 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1311 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001312 allocation.width = surface->allocation.height;
1313 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001314 break;
1315 default:
1316 break;
1317 }
1318
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001319 if (!surface->toysurface && display->dpy &&
1320 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001321 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001322 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001323 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001324 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001325 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001326 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001327
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001328 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001329 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001330 surface->surface,
1331 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001332
Pekka Paalanen89dee002013-02-13 16:17:20 +02001333 surface->cairo_surface = surface->toysurface->prepare(
1334 surface->toysurface, dx, dy,
1335 allocation.width, allocation.height, flags);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001336}
1337
1338static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001339window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001340{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001341 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001342 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001343 int dx = 0;
1344 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001345
Pekka Paalanenec076692012-11-30 13:37:27 +02001346 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001347 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001348
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001349 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1350 dx = surface->server_allocation.width -
1351 surface->allocation.width;
1352
1353 if (window->resize_edges & WINDOW_RESIZING_TOP)
1354 dy = surface->server_allocation.height -
1355 surface->allocation.height;
1356
1357 window->resize_edges = 0;
1358
Pekka Paalanen89dee002013-02-13 16:17:20 +02001359 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001360}
1361
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001362int
1363window_get_buffer_transform(struct window *window)
1364{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001365 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001366}
1367
1368void
1369window_set_buffer_transform(struct window *window,
1370 enum wl_output_transform transform)
1371{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001372 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001373 wl_surface_set_buffer_transform(window->main_surface->surface,
1374 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001375}
1376
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001377static void frame_destroy(struct frame *frame);
1378
Pekka Paalanen4e373742013-02-13 16:17:13 +02001379static void
1380surface_destroy(struct surface *surface)
1381{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001382 if (surface->frame_cb)
1383 wl_callback_destroy(surface->frame_cb);
1384
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001385 if (surface->input_region)
1386 wl_region_destroy(surface->input_region);
1387
1388 if (surface->opaque_region)
1389 wl_region_destroy(surface->opaque_region);
1390
Pekka Paalanen35e82632013-04-25 13:57:48 +03001391 if (surface->subsurface)
1392 wl_subsurface_destroy(surface->subsurface);
1393
Pekka Paalanen4e373742013-02-13 16:17:13 +02001394 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001395
1396 if (surface->toysurface)
1397 surface->toysurface->destroy(surface->toysurface);
1398
Pekka Paalanen35e82632013-04-25 13:57:48 +03001399 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001400 free(surface);
1401}
1402
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001403void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001404window_destroy(struct window *window)
1405{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001406 struct display *display = window->display;
1407 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001408 struct window_output *window_output;
1409 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001410
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001411 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001412
1413 wl_list_for_each(input, &display->input_list, link) {
1414 if (input->pointer_focus == window)
1415 input->pointer_focus = NULL;
1416 if (input->keyboard_focus == window)
1417 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001418 if (input->focus_widget &&
1419 input->focus_widget->window == window)
1420 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001421 }
1422
Rob Bradford7507b572012-05-15 17:55:34 +01001423 wl_list_for_each_safe(window_output, window_output_tmp,
1424 &window->window_output_list, link) {
1425 free (window_output);
1426 }
1427
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001428 if (window->frame)
1429 frame_destroy(window->frame);
1430
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001431 if (window->shell_surface)
1432 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001433
1434 surface_destroy(window->main_surface);
1435
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001436 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001437
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001438 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001439 free(window);
1440}
1441
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001442static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001443widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001444{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001445 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001446
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001447 wl_list_for_each(child, &widget->child_list, link) {
1448 target = widget_find_widget(child, x, y);
1449 if (target)
1450 return target;
1451 }
1452
1453 if (widget->allocation.x <= x &&
1454 x < widget->allocation.x + widget->allocation.width &&
1455 widget->allocation.y <= y &&
1456 y < widget->allocation.y + widget->allocation.height) {
1457 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001458 }
1459
1460 return NULL;
1461}
1462
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001463static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001464window_find_widget(struct window *window, int32_t x, int32_t y)
1465{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001466 struct surface *surface;
1467 struct widget *widget;
1468
1469 wl_list_for_each(surface, &window->subsurface_list, link) {
1470 widget = widget_find_widget(surface->widget, x, y);
1471 if (widget)
1472 return widget;
1473 }
1474
1475 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001476}
1477
1478static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001479widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001480{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001481 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001482
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001483 widget = malloc(sizeof *widget);
1484 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001485 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001486 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001487 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001488 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001489 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001490 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001491 widget->tooltip = NULL;
1492 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001493 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001494
1495 return widget;
1496}
1497
1498struct widget *
1499window_add_widget(struct window *window, void *data)
1500{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001501 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001502
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001503 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001504 wl_list_init(&widget->link);
1505 window->main_surface->widget = widget;
1506
1507 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001508}
1509
1510struct widget *
1511widget_add_widget(struct widget *parent, void *data)
1512{
1513 struct widget *widget;
1514
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001515 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001516 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001517
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001518 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001519}
1520
1521void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001522widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001523{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001524 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001525 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001526 struct input *input;
1527
Pekka Paalanen35e82632013-04-25 13:57:48 +03001528 /* Destroy the sub-surface along with the root widget */
1529 if (surface->widget == widget && surface->subsurface)
1530 surface_destroy(widget->surface);
1531
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001532 if (widget->tooltip) {
1533 free(widget->tooltip);
1534 widget->tooltip = NULL;
1535 }
1536
Pekka Paalanene156fb62012-01-19 13:51:38 +02001537 wl_list_for_each(input, &display->input_list, link) {
1538 if (input->focus_widget == widget)
1539 input->focus_widget = NULL;
1540 }
1541
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001542 wl_list_remove(&widget->link);
1543 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001544}
1545
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001546void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001547widget_set_default_cursor(struct widget *widget, int cursor)
1548{
1549 widget->default_cursor = cursor;
1550}
1551
1552void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001553widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001554{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001555 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001556}
1557
1558void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001559widget_set_size(struct widget *widget, int32_t width, int32_t height)
1560{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001561 widget->allocation.width = width;
1562 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001563}
1564
1565void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001566widget_set_allocation(struct widget *widget,
1567 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001569 widget->allocation.x = x;
1570 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001571 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001572}
1573
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001574void
1575widget_set_transparent(struct widget *widget, int transparent)
1576{
1577 widget->opaque = !transparent;
1578}
1579
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001580void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001581widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001582{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001583 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001584}
1585
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001586static cairo_surface_t *
1587widget_get_cairo_surface(struct widget *widget)
1588{
1589 struct surface *surface = widget->surface;
1590 struct window *window = widget->window;
1591
1592 if (!surface->cairo_surface) {
1593 if (surface == window->main_surface)
1594 window_create_main_surface(window);
1595 else
1596 surface_create_surface(surface, 0, 0, 0);
1597 }
1598
1599 return surface->cairo_surface;
1600}
1601
1602cairo_t *
1603widget_cairo_create(struct widget *widget)
1604{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001605 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001606 cairo_surface_t *cairo_surface;
1607 cairo_t *cr;
1608
1609 cairo_surface = widget_get_cairo_surface(widget);
1610 cr = cairo_create(cairo_surface);
1611
Pekka Paalanen35e82632013-04-25 13:57:48 +03001612 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1613
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001614 return cr;
1615}
1616
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001617struct wl_surface *
1618widget_get_wl_surface(struct widget *widget)
1619{
1620 return widget->surface->surface;
1621}
1622
1623uint32_t
1624widget_get_last_time(struct widget *widget)
1625{
1626 return widget->surface->last_time;
1627}
1628
1629void
1630widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1631{
1632 struct wl_compositor *comp = widget->window->display->compositor;
1633 struct surface *surface = widget->surface;
1634
1635 if (!surface->input_region)
1636 surface->input_region = wl_compositor_create_region(comp);
1637
1638 if (rect) {
1639 wl_region_add(surface->input_region,
1640 rect->x, rect->y, rect->width, rect->height);
1641 }
1642}
1643
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001644void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001645widget_set_resize_handler(struct widget *widget,
1646 widget_resize_handler_t handler)
1647{
1648 widget->resize_handler = handler;
1649}
1650
1651void
1652widget_set_redraw_handler(struct widget *widget,
1653 widget_redraw_handler_t handler)
1654{
1655 widget->redraw_handler = handler;
1656}
1657
1658void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001659widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001660{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001661 widget->enter_handler = handler;
1662}
1663
1664void
1665widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1666{
1667 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001668}
1669
1670void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001671widget_set_motion_handler(struct widget *widget,
1672 widget_motion_handler_t handler)
1673{
1674 widget->motion_handler = handler;
1675}
1676
1677void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001678widget_set_button_handler(struct widget *widget,
1679 widget_button_handler_t handler)
1680{
1681 widget->button_handler = handler;
1682}
1683
1684void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001685widget_set_axis_handler(struct widget *widget,
1686 widget_axis_handler_t handler)
1687{
1688 widget->axis_handler = handler;
1689}
1690
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001691static void
1692window_schedule_redraw_task(struct window *window);
1693
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001694void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001695widget_schedule_redraw(struct widget *widget)
1696{
Pekka Paalanen71233882013-04-25 13:57:53 +03001697 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001698 widget->surface->redraw_needed = 1;
1699 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001700}
1701
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001702cairo_surface_t *
1703window_get_surface(struct window *window)
1704{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001705 cairo_surface_t *cairo_surface;
1706
1707 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1708
1709 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001710}
1711
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001712struct wl_surface *
1713window_get_wl_surface(struct window *window)
1714{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001715 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001716}
1717
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001718struct wl_shell_surface *
1719window_get_wl_shell_surface(struct window *window)
1720{
1721 return window->shell_surface;
1722}
1723
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001724static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001725tooltip_redraw_handler(struct widget *widget, void *data)
1726{
1727 cairo_t *cr;
1728 const int32_t r = 3;
1729 struct tooltip *tooltip = data;
1730 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001731
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001732 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001733 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1734 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1735 cairo_paint(cr);
1736
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001737 width = widget->allocation.width;
1738 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001739 rounded_rect(cr, 0, 0, width, height, r);
1740
1741 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1742 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1743 cairo_fill(cr);
1744
1745 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1746 cairo_move_to(cr, 10, 16);
1747 cairo_show_text(cr, tooltip->entry);
1748 cairo_destroy(cr);
1749}
1750
1751static cairo_text_extents_t
1752get_text_extents(struct tooltip *tooltip)
1753{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001754 cairo_t *cr;
1755 cairo_text_extents_t extents;
1756
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001757 /* Use the dummy_surface because tooltip's surface was not
1758 * created yet, and parent does not have a valid surface
1759 * outside repaint, either.
1760 */
1761 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001762 cairo_text_extents(cr, tooltip->entry, &extents);
1763 cairo_destroy(cr);
1764
1765 return extents;
1766}
1767
1768static int
1769window_create_tooltip(struct tooltip *tooltip)
1770{
1771 struct widget *parent = tooltip->parent;
1772 struct display *display = parent->window->display;
1773 struct window *window;
1774 const int offset_y = 27;
1775 const int margin = 3;
1776 cairo_text_extents_t extents;
1777
1778 if (tooltip->widget)
1779 return 0;
1780
1781 window = window_create_transient(display, parent->window, tooltip->x,
1782 tooltip->y + offset_y,
1783 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1784 if (!window)
1785 return -1;
1786
1787 tooltip->window = window;
1788 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1789
1790 extents = get_text_extents(tooltip);
1791 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1792 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1793
1794 return 0;
1795}
1796
1797void
1798widget_destroy_tooltip(struct widget *parent)
1799{
1800 struct tooltip *tooltip = parent->tooltip;
1801
1802 parent->tooltip_count = 0;
1803 if (!tooltip)
1804 return;
1805
1806 if (tooltip->widget) {
1807 widget_destroy(tooltip->widget);
1808 window_destroy(tooltip->window);
1809 tooltip->widget = NULL;
1810 tooltip->window = NULL;
1811 }
1812
1813 close(tooltip->tooltip_fd);
1814 free(tooltip->entry);
1815 free(tooltip);
1816 parent->tooltip = NULL;
1817}
1818
1819static void
1820tooltip_func(struct task *task, uint32_t events)
1821{
1822 struct tooltip *tooltip =
1823 container_of(task, struct tooltip, tooltip_task);
1824 uint64_t exp;
1825
Martin Olsson8df662a2012-07-08 03:03:47 +02001826 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1827 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001828 window_create_tooltip(tooltip);
1829}
1830
1831#define TOOLTIP_TIMEOUT 500
1832static int
1833tooltip_timer_reset(struct tooltip *tooltip)
1834{
1835 struct itimerspec its;
1836
1837 its.it_interval.tv_sec = 0;
1838 its.it_interval.tv_nsec = 0;
1839 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1840 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1841 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1842 fprintf(stderr, "could not set timerfd\n: %m");
1843 return -1;
1844 }
1845
1846 return 0;
1847}
1848
1849int
1850widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1851{
1852 struct tooltip *tooltip = parent->tooltip;
1853
1854 parent->tooltip_count++;
1855 if (tooltip) {
1856 tooltip->x = x;
1857 tooltip->y = y;
1858 tooltip_timer_reset(tooltip);
1859 return 0;
1860 }
1861
1862 /* the handler might be triggered too fast via input device motion, so
1863 * we need this check here to make sure tooltip is fully initialized */
1864 if (parent->tooltip_count > 1)
1865 return 0;
1866
1867 tooltip = malloc(sizeof *tooltip);
1868 if (!tooltip)
1869 return -1;
1870
1871 parent->tooltip = tooltip;
1872 tooltip->parent = parent;
1873 tooltip->widget = NULL;
1874 tooltip->window = NULL;
1875 tooltip->x = x;
1876 tooltip->y = y;
1877 tooltip->entry = strdup(entry);
1878 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1879 if (tooltip->tooltip_fd < 0) {
1880 fprintf(stderr, "could not create timerfd\n: %m");
1881 return -1;
1882 }
1883
1884 tooltip->tooltip_task.run = tooltip_func;
1885 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1886 EPOLLIN, &tooltip->tooltip_task);
1887 tooltip_timer_reset(tooltip);
1888
1889 return 0;
1890}
1891
1892static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001893workspace_manager_state(void *data,
1894 struct workspace_manager *workspace_manager,
1895 uint32_t current,
1896 uint32_t count)
1897{
1898 struct display *display = data;
1899
1900 display->workspace = current;
1901 display->workspace_count = count;
1902}
1903
1904static const struct workspace_manager_listener workspace_manager_listener = {
1905 workspace_manager_state
1906};
1907
1908static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001909frame_resize_handler(struct widget *widget,
1910 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001911{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001912 struct frame *frame = data;
1913 struct widget *child = frame->child;
1914 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001915 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001916 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02001917 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001918 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001919 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001920 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001921 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001922
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001923 switch (widget->window->type) {
1924 case TYPE_FULLSCREEN:
1925 decoration_width = 0;
1926 decoration_height = 0;
1927
1928 allocation.x = 0;
1929 allocation.y = 0;
1930 allocation.width = width;
1931 allocation.height = height;
1932 opaque_margin = 0;
1933
1934 wl_list_for_each(button, &frame->buttons_list, link)
1935 button->widget->opaque = 1;
1936 break;
1937 case TYPE_MAXIMIZED:
1938 decoration_width = t->width * 2;
1939 decoration_height = t->width + t->titlebar_height;
1940
1941 allocation.x = t->width;
1942 allocation.y = t->titlebar_height;
1943 allocation.width = width - decoration_width;
1944 allocation.height = height - decoration_height;
1945
1946 opaque_margin = 0;
1947
1948 wl_list_for_each(button, &frame->buttons_list, link)
1949 button->widget->opaque = 0;
1950 break;
1951 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001952 decoration_width = (t->width + t->margin) * 2;
1953 decoration_height = t->width +
1954 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001955
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001956 allocation.x = t->width + t->margin;
1957 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001958 allocation.width = width - decoration_width;
1959 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001960
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001961 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001962
1963 wl_list_for_each(button, &frame->buttons_list, link)
1964 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001965 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001966 }
1967
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001968 widget_set_allocation(child, allocation.x, allocation.y,
1969 allocation.width, allocation.height);
1970
1971 if (child->resize_handler)
1972 child->resize_handler(child,
1973 allocation.width,
1974 allocation.height,
1975 child->user_data);
1976
Scott Moreauf7e498c2012-05-14 11:39:29 -06001977 width = child->allocation.width + decoration_width;
1978 height = child->allocation.height + decoration_height;
1979
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001980 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1981
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001982 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001983 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001984 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001985 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001986 shadow_margin, shadow_margin,
1987 width - 2 * shadow_margin,
1988 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001989 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001990 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001991 }
1992
Scott Moreauf7e498c2012-05-14 11:39:29 -06001993 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001994
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001995 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001996 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001997 opaque_margin, opaque_margin,
1998 widget->allocation.width - 2 * opaque_margin,
1999 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002000
2001 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002002 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2003 x_l = t->width + shadow_margin;
2004 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002005 wl_list_for_each(button, &frame->buttons_list, link) {
2006 const int button_padding = 4;
2007 w = cairo_image_surface_get_width(button->icon);
2008 h = cairo_image_surface_get_height(button->icon);
2009
2010 if (button->decoration == FRAME_BUTTON_FANCY)
2011 w += 10;
2012
2013 if (button->align == FRAME_BUTTON_LEFT) {
2014 widget_set_allocation(button->widget,
2015 x_l, y , w + 1, h + 1);
2016 x_l += w;
2017 x_l += button_padding;
2018 } else {
2019 x_r -= w;
2020 widget_set_allocation(button->widget,
2021 x_r, y , w + 1, h + 1);
2022 x_r -= button_padding;
2023 }
2024 }
2025}
2026
2027static int
2028frame_button_enter_handler(struct widget *widget,
2029 struct input *input, float x, float y, void *data)
2030{
2031 struct frame_button *frame_button = data;
2032
2033 widget_schedule_redraw(frame_button->widget);
2034 frame_button->state = FRAME_BUTTON_OVER;
2035
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002036 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002037}
2038
2039static void
2040frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2041{
2042 struct frame_button *frame_button = data;
2043
2044 widget_schedule_redraw(frame_button->widget);
2045 frame_button->state = FRAME_BUTTON_DEFAULT;
2046}
2047
2048static void
2049frame_button_button_handler(struct widget *widget,
2050 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002051 uint32_t button,
2052 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002053{
2054 struct frame_button *frame_button = data;
2055 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002056 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002057
2058 if (button != BTN_LEFT)
2059 return;
2060
2061 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002062 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002063 frame_button->state = FRAME_BUTTON_ACTIVE;
2064 widget_schedule_redraw(frame_button->widget);
2065
2066 if (frame_button->type == FRAME_BUTTON_ICON)
2067 window_show_frame_menu(window, input, time);
2068 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002069 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002070 frame_button->state = FRAME_BUTTON_DEFAULT;
2071 widget_schedule_redraw(frame_button->widget);
2072 break;
2073 }
2074
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002075 if (!was_pressed)
2076 return;
2077
Martin Minarik1998b152012-05-10 02:04:35 +02002078 switch (frame_button->type) {
2079 case FRAME_BUTTON_CLOSE:
2080 if (window->close_handler)
2081 window->close_handler(window->parent,
2082 window->user_data);
2083 else
2084 display_exit(window->display);
2085 break;
2086 case FRAME_BUTTON_MINIMIZE:
2087 fprintf(stderr,"Minimize stub\n");
2088 break;
2089 case FRAME_BUTTON_MAXIMIZE:
2090 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2091 break;
2092 default:
2093 /* Unknown operation */
2094 break;
2095 }
2096}
2097
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002098static int
2099frame_button_motion_handler(struct widget *widget,
2100 struct input *input, uint32_t time,
2101 float x, float y, void *data)
2102{
2103 struct frame_button *frame_button = data;
2104 enum frame_button_pointer previous_button_state = frame_button->state;
2105
2106 /* only track state for a pressed button */
2107 if (input->grab != widget)
2108 return CURSOR_LEFT_PTR;
2109
2110 if (x > widget->allocation.x &&
2111 x < (widget->allocation.x + widget->allocation.width) &&
2112 y > widget->allocation.y &&
2113 y < (widget->allocation.y + widget->allocation.height)) {
2114 frame_button->state = FRAME_BUTTON_ACTIVE;
2115 } else {
2116 frame_button->state = FRAME_BUTTON_DEFAULT;
2117 }
2118
2119 if (frame_button->state != previous_button_state)
2120 widget_schedule_redraw(frame_button->widget);
2121
2122 return CURSOR_LEFT_PTR;
2123}
2124
Martin Minarik1998b152012-05-10 02:04:35 +02002125static void
2126frame_button_redraw_handler(struct widget *widget, void *data)
2127{
2128 struct frame_button *frame_button = data;
2129 cairo_t *cr;
2130 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002131
2132 x = widget->allocation.x;
2133 y = widget->allocation.y;
2134 width = widget->allocation.width;
2135 height = widget->allocation.height;
2136
2137 if (!width)
2138 return;
2139 if (!height)
2140 return;
2141 if (widget->opaque)
2142 return;
2143
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002144 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002145
2146 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2147 cairo_set_line_width(cr, 1);
2148
2149 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2150 cairo_rectangle (cr, x, y, 25, 16);
2151
2152 cairo_stroke_preserve(cr);
2153
2154 switch (frame_button->state) {
2155 case FRAME_BUTTON_DEFAULT:
2156 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2157 break;
2158 case FRAME_BUTTON_OVER:
2159 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2160 break;
2161 case FRAME_BUTTON_ACTIVE:
2162 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2163 break;
2164 }
2165
2166 cairo_fill (cr);
2167
2168 x += 4;
2169 }
2170
2171 cairo_set_source_surface(cr, frame_button->icon, x, y);
2172 cairo_paint(cr);
2173
2174 cairo_destroy(cr);
2175}
2176
2177static struct widget *
2178frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2179 enum frame_button_align align, enum frame_button_decoration style)
2180{
2181 struct frame_button *frame_button;
2182 const char *icon = data;
2183
2184 frame_button = malloc (sizeof *frame_button);
2185 memset(frame_button, 0, sizeof *frame_button);
2186
2187 frame_button->icon = cairo_image_surface_create_from_png(icon);
2188 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2189 frame_button->frame = frame;
2190 frame_button->type = type;
2191 frame_button->align = align;
2192 frame_button->decoration = style;
2193
2194 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2195
2196 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2197 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2198 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2199 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002200 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002201 return frame_button->widget;
2202}
2203
2204static void
2205frame_button_destroy(struct frame_button *frame_button)
2206{
2207 widget_destroy(frame_button->widget);
2208 wl_list_remove(&frame_button->link);
2209 cairo_surface_destroy(frame_button->icon);
2210 free(frame_button);
2211
2212 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002213}
2214
2215static void
2216frame_redraw_handler(struct widget *widget, void *data)
2217{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002218 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002220 struct theme *t = window->display->theme;
2221 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002223 if (window->type == TYPE_FULLSCREEN)
2224 return;
2225
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002226 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002227
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002228 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002229 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002230 if (window->type == TYPE_MAXIMIZED)
2231 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002232 theme_render_frame(t, cr, widget->allocation.width,
2233 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002234
2235 cairo_destroy(cr);
2236}
2237
2238static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002239frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002240{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002241 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002242 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002243 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002244
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002245 if (window->type != TYPE_TOPLEVEL)
2246 return CURSOR_LEFT_PTR;
2247
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002248 location = theme_get_location(t, input->sx, input->sy,
2249 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002250 frame->widget->allocation.height,
2251 window->type == TYPE_MAXIMIZED ?
2252 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002253
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002254 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002255 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002256 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002257 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002258 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002259 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002260 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002261 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002262 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002263 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002264 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002265 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002266 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002267 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002268 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002269 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002270 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002271 case THEME_LOCATION_EXTERIOR:
2272 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002273 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002274 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002275 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002276}
2277
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002278static void
2279frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002280{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002281 struct display *display;
2282
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002283 switch (index) {
2284 case 0: /* close */
2285 if (window->close_handler)
2286 window->close_handler(window->parent,
2287 window->user_data);
2288 else
2289 display_exit(window->display);
2290 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002291 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002292 display = window->display;
2293 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002294 workspace_manager_move_surface(
2295 display->workspace_manager,
2296 window->main_surface->surface,
2297 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002298 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002299 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002300 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002301 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002302 workspace_manager_move_surface(
2303 display->workspace_manager,
2304 window->main_surface->surface,
2305 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002306 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002307 case 3: /* fullscreen */
2308 /* we don't have a way to get out of fullscreen for now */
2309 if (window->fullscreen_handler)
2310 window->fullscreen_handler(window, window->user_data);
2311 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002312 }
2313}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002314
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002315void
2316window_show_frame_menu(struct window *window,
2317 struct input *input, uint32_t time)
2318{
2319 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002320 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002321
2322 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002323 "Close",
2324 "Move to workspace above", "Move to workspace below",
2325 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002326 };
2327
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002328 if (window->fullscreen_handler)
2329 count = ARRAY_LENGTH(entries);
2330 else
2331 count = ARRAY_LENGTH(entries) - 1;
2332
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002333 input_get_position(input, &x, &y);
2334 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002335 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002336}
2337
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002338static int
2339frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002340 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341{
2342 return frame_get_pointer_image_for_location(data, input);
2343}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002344
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002345static int
2346frame_motion_handler(struct widget *widget,
2347 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002348 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002349{
2350 return frame_get_pointer_image_for_location(data, input);
2351}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002352
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002353static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002354frame_button_handler(struct widget *widget,
2355 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002356 uint32_t button, enum wl_pointer_button_state state,
2357 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002358
2359{
2360 struct frame *frame = data;
2361 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002362 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002363 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002364
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002365 if (window->type != TYPE_TOPLEVEL)
2366 return;
2367
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002368 location = theme_get_location(display->theme, input->sx, input->sy,
2369 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002370 frame->widget->allocation.height,
2371 window->type == TYPE_MAXIMIZED ?
2372 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002373
Daniel Stone4dbadb12012-05-30 16:31:51 +01002374 if (window->display->shell && button == BTN_LEFT &&
2375 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002376 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002377 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002378 if (!window->shell_surface)
2379 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002380 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002381 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002382 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002383 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002384 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002385 case THEME_LOCATION_RESIZING_TOP:
2386 case THEME_LOCATION_RESIZING_BOTTOM:
2387 case THEME_LOCATION_RESIZING_LEFT:
2388 case THEME_LOCATION_RESIZING_RIGHT:
2389 case THEME_LOCATION_RESIZING_TOP_LEFT:
2390 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2391 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2392 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002393 if (!window->shell_surface)
2394 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002395 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002396
Pekka Paalanen99436862012-11-19 17:15:59 +02002397 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002398 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002399 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002400 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002401 break;
2402 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002403 } else if (button == BTN_RIGHT &&
2404 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002405 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002406 }
2407}
2408
2409struct widget *
2410frame_create(struct window *window, void *data)
2411{
2412 struct frame *frame;
2413
2414 frame = malloc(sizeof *frame);
2415 memset(frame, 0, sizeof *frame);
2416
2417 frame->widget = window_add_widget(window, frame);
2418 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002419
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002420 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2421 widget_set_resize_handler(frame->widget, frame_resize_handler);
2422 widget_set_enter_handler(frame->widget, frame_enter_handler);
2423 widget_set_motion_handler(frame->widget, frame_motion_handler);
2424 widget_set_button_handler(frame->widget, frame_button_handler);
2425
Martin Minarik1998b152012-05-10 02:04:35 +02002426 /* Create empty list for frame buttons */
2427 wl_list_init(&frame->buttons_list);
2428
2429 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2430 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2431
2432 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2433 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2434
2435 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2436 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2437
2438 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2439 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2440
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002441 window->frame = frame;
2442
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002443 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002444}
2445
Kristian Høgsberga1627922012-06-20 17:30:03 -04002446void
2447frame_set_child_size(struct widget *widget, int child_width, int child_height)
2448{
2449 struct display *display = widget->window->display;
2450 struct theme *t = display->theme;
2451 int decoration_width, decoration_height;
2452 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002453 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002454
2455 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002456 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002457 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002458 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002459
2460 width = child_width + decoration_width;
2461 height = child_height + decoration_height;
2462 } else {
2463 width = child_width;
2464 height = child_height;
2465 }
2466
2467 window_schedule_resize(widget->window, width, height);
2468}
2469
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002470static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002471frame_destroy(struct frame *frame)
2472{
Martin Minarik1998b152012-05-10 02:04:35 +02002473 struct frame_button *button, *tmp;
2474
2475 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2476 frame_button_destroy(button);
2477
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002478 /* frame->child must be destroyed by the application */
2479 widget_destroy(frame->widget);
2480 free(frame);
2481}
2482
2483static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002484input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002485 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002486{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002487 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002488 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002489
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002490 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002491 return;
2492
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002493 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002494 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002495 widget = old;
2496 if (input->grab)
2497 widget = input->grab;
2498 if (widget->leave_handler)
2499 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002500 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002501 }
2502
2503 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002504 widget = focus;
2505 if (input->grab)
2506 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002507 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002508 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002509 cursor = widget->enter_handler(focus, input, x, y,
2510 widget->user_data);
2511 else
2512 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002513
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002514 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002515 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002516}
2517
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002518void
2519input_grab(struct input *input, struct widget *widget, uint32_t button)
2520{
2521 input->grab = widget;
2522 input->grab_button = button;
2523}
2524
2525void
2526input_ungrab(struct input *input)
2527{
2528 struct widget *widget;
2529
2530 input->grab = NULL;
2531 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002532 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002533 input->sx, input->sy);
2534 input_set_focus_widget(input, widget, input->sx, input->sy);
2535 }
2536}
2537
2538static void
2539input_remove_pointer_focus(struct input *input)
2540{
2541 struct window *window = input->pointer_focus;
2542
2543 if (!window)
2544 return;
2545
2546 input_set_focus_widget(input, NULL, 0, 0);
2547
2548 input->pointer_focus = NULL;
2549 input->current_cursor = CURSOR_UNSET;
2550}
2551
2552static void
2553pointer_handle_enter(void *data, struct wl_pointer *pointer,
2554 uint32_t serial, struct wl_surface *surface,
2555 wl_fixed_t sx_w, wl_fixed_t sy_w)
2556{
2557 struct input *input = data;
2558 struct window *window;
2559 struct widget *widget;
2560 float sx = wl_fixed_to_double(sx_w);
2561 float sy = wl_fixed_to_double(sy_w);
2562
2563 if (!surface) {
2564 /* enter event for a window we've just destroyed */
2565 return;
2566 }
2567
2568 input->display->serial = serial;
2569 input->pointer_enter_serial = serial;
2570 input->pointer_focus = wl_surface_get_user_data(surface);
2571 window = input->pointer_focus;
2572
Pekka Paalanen99436862012-11-19 17:15:59 +02002573 if (window->resizing) {
2574 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002575 /* Schedule a redraw to free the pool */
2576 window_schedule_redraw(window);
2577 }
2578
2579 input->sx = sx;
2580 input->sy = sy;
2581
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002582 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002583 input_set_focus_widget(input, widget, sx, sy);
2584}
2585
2586static void
2587pointer_handle_leave(void *data, struct wl_pointer *pointer,
2588 uint32_t serial, struct wl_surface *surface)
2589{
2590 struct input *input = data;
2591
2592 input->display->serial = serial;
2593 input_remove_pointer_focus(input);
2594}
2595
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002596static void
Daniel Stone37816df2012-05-16 18:45:18 +01002597pointer_handle_motion(void *data, struct wl_pointer *pointer,
2598 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002599{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002600 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002601 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002602 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002603 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002604 float sx = wl_fixed_to_double(sx_w);
2605 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002606
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002607 input->sx = sx;
2608 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002609
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002610 if (!window)
2611 return;
2612
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002613 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002614 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002615 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002616 }
2617
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002618 if (input->grab)
2619 widget = input->grab;
2620 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002621 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002622 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002623 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002624 input, time, sx, sy,
2625 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002626 else
2627 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002628
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002629 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002630}
2631
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002632static void
Daniel Stone37816df2012-05-16 18:45:18 +01002633pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002634 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002635{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002636 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002637 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002638 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002639
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002640 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002641 if (input->focus_widget && input->grab == NULL &&
2642 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002643 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002644
Neil Roberts6b28aad2012-01-23 19:11:18 +00002645 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002646 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002647 (*widget->button_handler)(widget,
2648 input, time,
2649 button, state,
2650 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002651
Daniel Stone4dbadb12012-05-30 16:31:51 +01002652 if (input->grab && input->grab_button == button &&
2653 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002654 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002655}
2656
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002657static void
Daniel Stone37816df2012-05-16 18:45:18 +01002658pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002659 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002660{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002661 struct input *input = data;
2662 struct widget *widget;
2663
2664 widget = input->focus_widget;
2665 if (input->grab)
2666 widget = input->grab;
2667 if (widget && widget->axis_handler)
2668 (*widget->axis_handler)(widget,
2669 input, time,
2670 axis, value,
2671 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002672}
2673
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002674static const struct wl_pointer_listener pointer_listener = {
2675 pointer_handle_enter,
2676 pointer_handle_leave,
2677 pointer_handle_motion,
2678 pointer_handle_button,
2679 pointer_handle_axis,
2680};
2681
2682static void
2683input_remove_keyboard_focus(struct input *input)
2684{
2685 struct window *window = input->keyboard_focus;
2686 struct itimerspec its;
2687
2688 its.it_interval.tv_sec = 0;
2689 its.it_interval.tv_nsec = 0;
2690 its.it_value.tv_sec = 0;
2691 its.it_value.tv_nsec = 0;
2692 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2693
2694 if (!window)
2695 return;
2696
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002697 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002698 if (window->keyboard_focus_handler)
2699 (*window->keyboard_focus_handler)(window, NULL,
2700 window->user_data);
2701
2702 input->keyboard_focus = NULL;
2703}
2704
2705static void
2706keyboard_repeat_func(struct task *task, uint32_t events)
2707{
2708 struct input *input =
2709 container_of(task, struct input, repeat_task);
2710 struct window *window = input->keyboard_focus;
2711 uint64_t exp;
2712
2713 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2714 /* If we change the timer between the fd becoming
2715 * readable and getting here, there'll be nothing to
2716 * read and we get EAGAIN. */
2717 return;
2718
2719 if (window && window->key_handler) {
2720 (*window->key_handler)(window, input, input->repeat_time,
2721 input->repeat_key, input->repeat_sym,
2722 WL_KEYBOARD_KEY_STATE_PRESSED,
2723 window->user_data);
2724 }
2725}
2726
2727static void
2728keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2729 uint32_t format, int fd, uint32_t size)
2730{
2731 struct input *input = data;
2732 char *map_str;
2733
2734 if (!data) {
2735 close(fd);
2736 return;
2737 }
2738
2739 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2740 close(fd);
2741 return;
2742 }
2743
2744 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2745 if (map_str == MAP_FAILED) {
2746 close(fd);
2747 return;
2748 }
2749
2750 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2751 map_str,
2752 XKB_KEYMAP_FORMAT_TEXT_V1,
2753 0);
2754 munmap(map_str, size);
2755 close(fd);
2756
2757 if (!input->xkb.keymap) {
2758 fprintf(stderr, "failed to compile keymap\n");
2759 return;
2760 }
2761
2762 input->xkb.state = xkb_state_new(input->xkb.keymap);
2763 if (!input->xkb.state) {
2764 fprintf(stderr, "failed to create XKB state\n");
2765 xkb_map_unref(input->xkb.keymap);
2766 input->xkb.keymap = NULL;
2767 return;
2768 }
2769
2770 input->xkb.control_mask =
2771 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2772 input->xkb.alt_mask =
2773 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2774 input->xkb.shift_mask =
2775 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2776}
2777
2778static void
2779keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2780 uint32_t serial, struct wl_surface *surface,
2781 struct wl_array *keys)
2782{
2783 struct input *input = data;
2784 struct window *window;
2785
2786 input->display->serial = serial;
2787 input->keyboard_focus = wl_surface_get_user_data(surface);
2788
2789 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002790 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002791 if (window->keyboard_focus_handler)
2792 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002793 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002794}
2795
2796static void
2797keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2798 uint32_t serial, struct wl_surface *surface)
2799{
2800 struct input *input = data;
2801
2802 input->display->serial = serial;
2803 input_remove_keyboard_focus(input);
2804}
2805
Scott Moreau210d0792012-03-22 10:47:01 -06002806static void
Daniel Stone37816df2012-05-16 18:45:18 +01002807keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002808 uint32_t serial, uint32_t time, uint32_t key,
2809 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002810{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002811 struct input *input = data;
2812 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002813 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002814 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002815 const xkb_keysym_t *syms;
2816 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002817 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002818
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002819 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002820 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002821 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002822 return;
2823
Daniel Stone97f68542012-05-30 16:32:01 +01002824 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002825
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002826 sym = XKB_KEY_NoSymbol;
2827 if (num_syms == 1)
2828 sym = syms[0];
2829
2830 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002831 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002832 window_set_maximized(window,
2833 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002834 } else if (sym == XKB_KEY_F11 &&
2835 window->fullscreen_handler &&
2836 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2837 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002838 } else if (sym == XKB_KEY_F4 &&
2839 input->modifiers == MOD_ALT_MASK &&
2840 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2841 if (window->close_handler)
2842 window->close_handler(window->parent,
2843 window->user_data);
2844 else
2845 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002846 } else if (window->key_handler) {
2847 (*window->key_handler)(window, input, time, key,
2848 sym, state, window->user_data);
2849 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002850
2851 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2852 key == input->repeat_key) {
2853 its.it_interval.tv_sec = 0;
2854 its.it_interval.tv_nsec = 0;
2855 its.it_value.tv_sec = 0;
2856 its.it_value.tv_nsec = 0;
2857 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2858 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2859 input->repeat_sym = sym;
2860 input->repeat_key = key;
2861 input->repeat_time = time;
2862 its.it_interval.tv_sec = 0;
2863 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2864 its.it_value.tv_sec = 0;
2865 its.it_value.tv_nsec = 400 * 1000 * 1000;
2866 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2867 }
2868}
2869
2870static void
Daniel Stone351eb612012-05-31 15:27:47 -04002871keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2872 uint32_t serial, uint32_t mods_depressed,
2873 uint32_t mods_latched, uint32_t mods_locked,
2874 uint32_t group)
2875{
2876 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002877 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002878
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002879 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2880 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002881 mask = xkb_state_serialize_mods(input->xkb.state,
2882 XKB_STATE_DEPRESSED |
2883 XKB_STATE_LATCHED);
2884 input->modifiers = 0;
2885 if (mask & input->xkb.control_mask)
2886 input->modifiers |= MOD_CONTROL_MASK;
2887 if (mask & input->xkb.alt_mask)
2888 input->modifiers |= MOD_ALT_MASK;
2889 if (mask & input->xkb.shift_mask)
2890 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002891}
2892
Daniel Stone37816df2012-05-16 18:45:18 +01002893static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002894 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002895 keyboard_handle_enter,
2896 keyboard_handle_leave,
2897 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002898 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002899};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002900
2901static void
Daniel Stone37816df2012-05-16 18:45:18 +01002902seat_handle_capabilities(void *data, struct wl_seat *seat,
2903 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002904{
Daniel Stone37816df2012-05-16 18:45:18 +01002905 struct input *input = data;
2906
2907 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2908 input->pointer = wl_seat_get_pointer(seat);
2909 wl_pointer_set_user_data(input->pointer, input);
2910 wl_pointer_add_listener(input->pointer, &pointer_listener,
2911 input);
2912 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2913 wl_pointer_destroy(input->pointer);
2914 input->pointer = NULL;
2915 }
2916
2917 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2918 input->keyboard = wl_seat_get_keyboard(seat);
2919 wl_keyboard_set_user_data(input->keyboard, input);
2920 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2921 input);
2922 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2923 wl_keyboard_destroy(input->keyboard);
2924 input->keyboard = NULL;
2925 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002926}
2927
Daniel Stone37816df2012-05-16 18:45:18 +01002928static const struct wl_seat_listener seat_listener = {
2929 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002930};
2931
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002932void
2933input_get_position(struct input *input, int32_t *x, int32_t *y)
2934{
2935 *x = input->sx;
2936 *y = input->sy;
2937}
2938
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002939struct display *
2940input_get_display(struct input *input)
2941{
2942 return input->display;
2943}
2944
Daniel Stone37816df2012-05-16 18:45:18 +01002945struct wl_seat *
2946input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002947{
Daniel Stone37816df2012-05-16 18:45:18 +01002948 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002949}
2950
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002951uint32_t
2952input_get_modifiers(struct input *input)
2953{
2954 return input->modifiers;
2955}
2956
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002957struct widget *
2958input_get_focus_widget(struct input *input)
2959{
2960 return input->focus_widget;
2961}
2962
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002963struct data_offer {
2964 struct wl_data_offer *offer;
2965 struct input *input;
2966 struct wl_array types;
2967 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002968
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002969 struct task io_task;
2970 int fd;
2971 data_func_t func;
2972 int32_t x, y;
2973 void *user_data;
2974};
2975
2976static void
2977data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2978{
2979 struct data_offer *offer = data;
2980 char **p;
2981
2982 p = wl_array_add(&offer->types, sizeof *p);
2983 *p = strdup(type);
2984}
2985
2986static const struct wl_data_offer_listener data_offer_listener = {
2987 data_offer_offer,
2988};
2989
2990static void
2991data_offer_destroy(struct data_offer *offer)
2992{
2993 char **p;
2994
2995 offer->refcount--;
2996 if (offer->refcount == 0) {
2997 wl_data_offer_destroy(offer->offer);
2998 for (p = offer->types.data; *p; p++)
2999 free(*p);
3000 wl_array_release(&offer->types);
3001 free(offer);
3002 }
3003}
3004
3005static void
3006data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003007 struct wl_data_device *data_device,
3008 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003009{
3010 struct data_offer *offer;
3011
3012 offer = malloc(sizeof *offer);
3013
3014 wl_array_init(&offer->types);
3015 offer->refcount = 1;
3016 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003017 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003018 wl_data_offer_add_listener(offer->offer,
3019 &data_offer_listener, offer);
3020}
3021
3022static void
3023data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003024 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003025 wl_fixed_t x_w, wl_fixed_t y_w,
3026 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003027{
3028 struct input *input = data;
3029 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003030 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003031 float x = wl_fixed_to_double(x_w);
3032 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003033 char **p;
3034
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003035 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003036 window = wl_surface_get_user_data(surface);
3037 input->pointer_focus = window;
3038
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003039 if (offer) {
3040 input->drag_offer = wl_data_offer_get_user_data(offer);
3041
3042 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3043 *p = NULL;
3044
3045 types_data = input->drag_offer->types.data;
3046 } else {
3047 input->drag_offer = NULL;
3048 types_data = NULL;
3049 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003050
3051 window = input->pointer_focus;
3052 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003053 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003054 window->user_data);
3055}
3056
3057static void
3058data_device_leave(void *data, struct wl_data_device *data_device)
3059{
3060 struct input *input = data;
3061
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003062 if (input->drag_offer) {
3063 data_offer_destroy(input->drag_offer);
3064 input->drag_offer = NULL;
3065 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003066}
3067
3068static void
3069data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003070 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003071{
3072 struct input *input = data;
3073 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003074 float x = wl_fixed_to_double(x_w);
3075 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003076 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003077
3078 input->sx = x;
3079 input->sy = y;
3080
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003081 if (input->drag_offer)
3082 types_data = input->drag_offer->types.data;
3083 else
3084 types_data = NULL;
3085
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003086 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003087 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003088 window->user_data);
3089}
3090
3091static void
3092data_device_drop(void *data, struct wl_data_device *data_device)
3093{
3094 struct input *input = data;
3095 struct window *window = input->pointer_focus;
3096
3097 if (window->drop_handler)
3098 window->drop_handler(window, input,
3099 input->sx, input->sy, window->user_data);
3100}
3101
3102static void
3103data_device_selection(void *data,
3104 struct wl_data_device *wl_data_device,
3105 struct wl_data_offer *offer)
3106{
3107 struct input *input = data;
3108 char **p;
3109
3110 if (input->selection_offer)
3111 data_offer_destroy(input->selection_offer);
3112
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003113 if (offer) {
3114 input->selection_offer = wl_data_offer_get_user_data(offer);
3115 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3116 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003117 } else {
3118 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003119 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003120}
3121
3122static const struct wl_data_device_listener data_device_listener = {
3123 data_device_data_offer,
3124 data_device_enter,
3125 data_device_leave,
3126 data_device_motion,
3127 data_device_drop,
3128 data_device_selection
3129};
3130
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003131static void
3132input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003133{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003134 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003135 struct wl_cursor *cursor;
3136 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003137
Daniel Stone80972742012-11-07 17:51:39 +11003138 if (!input->pointer)
3139 return;
3140
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003141 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003142 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003143 return;
3144
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003145 if (index >= (int) cursor->image_count) {
3146 fprintf(stderr, "cursor index out of range\n");
3147 return;
3148 }
3149
3150 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003151 buffer = wl_cursor_image_get_buffer(image);
3152 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003153 return;
3154
Kristian Høgsbergae277372012-08-01 09:41:08 -04003155 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003156 input->pointer_surface,
3157 image->hotspot_x, image->hotspot_y);
3158 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3159 wl_surface_damage(input->pointer_surface, 0, 0,
3160 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003161 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003162}
3163
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003164static const struct wl_callback_listener pointer_surface_listener;
3165
3166static void
3167pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3168 uint32_t time)
3169{
3170 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003171 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003172 int i;
3173
3174 if (callback) {
3175 assert(callback == input->cursor_frame_cb);
3176 wl_callback_destroy(callback);
3177 input->cursor_frame_cb = NULL;
3178 }
3179
Daniel Stone80972742012-11-07 17:51:39 +11003180 if (!input->pointer)
3181 return;
3182
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003183 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003184 wl_pointer_set_cursor(input->pointer,
3185 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003186 NULL, 0, 0);
3187 return;
3188 }
3189
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003190 if (input->current_cursor == CURSOR_UNSET)
3191 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003192 cursor = input->display->cursors[input->current_cursor];
3193 if (!cursor)
3194 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003195
3196 /* FIXME We don't have the current time on the first call so we set
3197 * the animation start to the time of the first frame callback. */
3198 if (time == 0)
3199 input->cursor_anim_start = 0;
3200 else if (input->cursor_anim_start == 0)
3201 input->cursor_anim_start = time;
3202
3203 if (time == 0 || input->cursor_anim_start == 0)
3204 i = 0;
3205 else
3206 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3207
Pekka Paalanenbc106382012-10-10 12:49:31 +03003208 if (cursor->image_count > 1) {
3209 input->cursor_frame_cb =
3210 wl_surface_frame(input->pointer_surface);
3211 wl_callback_add_listener(input->cursor_frame_cb,
3212 &pointer_surface_listener, input);
3213 }
3214
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003215 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003216}
3217
3218static const struct wl_callback_listener pointer_surface_listener = {
3219 pointer_surface_frame_callback
3220};
3221
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003222void
3223input_set_pointer_image(struct input *input, int pointer)
3224{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003225 int force = 0;
3226
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003227 if (!input->pointer)
3228 return;
3229
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003230 if (input->pointer_enter_serial > input->cursor_serial)
3231 force = 1;
3232
3233 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003234 return;
3235
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003236 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003237 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003238 if (!input->cursor_frame_cb)
3239 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003240 else if (force) {
3241 /* The current frame callback may be stuck if, for instance,
3242 * the set cursor request was processed by the server after
3243 * this client lost the focus. In this case the cursor surface
3244 * might not be mapped and the frame callback wouldn't ever
3245 * complete. Send a set_cursor and attach to try to map the
3246 * cursor surface again so that the callback will finish */
3247 input_set_pointer_image_index(input, 0);
3248 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003249}
3250
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003251struct wl_data_device *
3252input_get_data_device(struct input *input)
3253{
3254 return input->data_device;
3255}
3256
3257void
3258input_set_selection(struct input *input,
3259 struct wl_data_source *source, uint32_t time)
3260{
3261 wl_data_device_set_selection(input->data_device, source, time);
3262}
3263
3264void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003265input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003266{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003267 wl_data_offer_accept(input->drag_offer->offer,
3268 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003269}
3270
3271static void
3272offer_io_func(struct task *task, uint32_t events)
3273{
3274 struct data_offer *offer =
3275 container_of(task, struct data_offer, io_task);
3276 unsigned int len;
3277 char buffer[4096];
3278
3279 len = read(offer->fd, buffer, sizeof buffer);
3280 offer->func(buffer, len,
3281 offer->x, offer->y, offer->user_data);
3282
3283 if (len == 0) {
3284 close(offer->fd);
3285 data_offer_destroy(offer);
3286 }
3287}
3288
3289static void
3290data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3291 data_func_t func, void *user_data)
3292{
3293 int p[2];
3294
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003295 if (pipe2(p, O_CLOEXEC) == -1)
3296 return;
3297
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003298 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3299 close(p[1]);
3300
3301 offer->io_task.run = offer_io_func;
3302 offer->fd = p[0];
3303 offer->func = func;
3304 offer->refcount++;
3305 offer->user_data = user_data;
3306
3307 display_watch_fd(offer->input->display,
3308 offer->fd, EPOLLIN, &offer->io_task);
3309}
3310
3311void
3312input_receive_drag_data(struct input *input, const char *mime_type,
3313 data_func_t func, void *data)
3314{
3315 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3316 input->drag_offer->x = input->sx;
3317 input->drag_offer->y = input->sy;
3318}
3319
3320int
3321input_receive_selection_data(struct input *input, const char *mime_type,
3322 data_func_t func, void *data)
3323{
3324 char **p;
3325
3326 if (input->selection_offer == NULL)
3327 return -1;
3328
3329 for (p = input->selection_offer->types.data; *p; p++)
3330 if (strcmp(mime_type, *p) == 0)
3331 break;
3332
3333 if (*p == NULL)
3334 return -1;
3335
3336 data_offer_receive_data(input->selection_offer,
3337 mime_type, func, data);
3338 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003339}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003340
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003341int
3342input_receive_selection_data_to_fd(struct input *input,
3343 const char *mime_type, int fd)
3344{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003345 if (input->selection_offer)
3346 wl_data_offer_receive(input->selection_offer->offer,
3347 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003348
3349 return 0;
3350}
3351
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003352void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003353window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003354{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003355 if (!window->shell_surface)
3356 return;
3357
Daniel Stone37816df2012-05-16 18:45:18 +01003358 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003359}
3360
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003361static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003362surface_set_synchronized(struct surface *surface)
3363{
3364 if (!surface->subsurface)
3365 return;
3366
3367 if (surface->synchronized)
3368 return;
3369
3370 wl_subsurface_set_sync(surface->subsurface);
3371 surface->synchronized = 1;
3372}
3373
3374static void
3375surface_set_synchronized_default(struct surface *surface)
3376{
3377 if (!surface->subsurface)
3378 return;
3379
3380 if (surface->synchronized == surface->synchronized_default)
3381 return;
3382
3383 if (surface->synchronized_default)
3384 wl_subsurface_set_sync(surface->subsurface);
3385 else
3386 wl_subsurface_set_desync(surface->subsurface);
3387
3388 surface->synchronized = surface->synchronized_default;
3389}
3390
3391static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003392surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003393{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003394 struct widget *widget = surface->widget;
3395 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003396
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003397 if (surface->input_region) {
3398 wl_region_destroy(surface->input_region);
3399 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003400 }
3401
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003402 if (surface->opaque_region)
3403 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003404
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003405 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003406
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003407 if (widget->resize_handler)
3408 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003409 widget->allocation.width,
3410 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003411 widget->user_data);
3412
Pekka Paalanen35e82632013-04-25 13:57:48 +03003413 if (surface->subsurface &&
3414 (surface->allocation.x != widget->allocation.x ||
3415 surface->allocation.y != widget->allocation.y)) {
3416 wl_subsurface_set_position(surface->subsurface,
3417 widget->allocation.x,
3418 widget->allocation.y);
3419 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003420 if (surface->allocation.width != widget->allocation.width ||
3421 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003422 window_schedule_redraw(widget->window);
3423 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003424 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003425
3426 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003427 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003428 widget->allocation.width,
3429 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003430}
3431
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003432static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003433hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3434{
3435 /*
3436 * This hack should be removed, when EGL respects
3437 * eglSwapInterval(0).
3438 *
3439 * If this window has sub-surfaces, especially a free-running
3440 * EGL-widget, we need to post the parent surface once with
3441 * all the old state to guarantee, that the EGL-widget will
3442 * receive its frame callback soon. Otherwise, a forced call
3443 * to eglSwapBuffers may end up blocking, waiting for a frame
3444 * event that will never come, because we will commit the parent
3445 * surface with all new state only after eglSwapBuffers returns.
3446 *
3447 * This assumes, that:
3448 * 1. When the EGL widget's resize hook is called, it pauses.
3449 * 2. When the EGL widget's redraw hook is called, it forces a
3450 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3451 * In a single threaded application condition 1 is a no-op.
3452 *
3453 * XXX: This should actually be after the surface_resize() calls,
3454 * but cannot, because then it would commit the incomplete state
3455 * accumulated from the widget resize hooks.
3456 */
3457 if (window->subsurface_list.next != &window->main_surface->link ||
3458 window->subsurface_list.prev != &window->main_surface->link)
3459 wl_surface_commit(window->main_surface->surface);
3460}
3461
3462static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003463idle_resize(struct window *window)
3464{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003465 struct surface *surface;
3466
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003467 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003468 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003469
Pekka Paalanen71233882013-04-25 13:57:53 +03003470 DBG("from %dx%d to %dx%d\n",
3471 window->main_surface->server_allocation.width,
3472 window->main_surface->server_allocation.height,
3473 window->pending_allocation.width,
3474 window->pending_allocation.height);
3475
Pekka Paalanene9297f82013-04-25 13:57:51 +03003476 hack_prevent_EGL_sub_surface_deadlock(window);
3477
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003478 widget_set_allocation(window->main_surface->widget,
3479 window->pending_allocation.x,
3480 window->pending_allocation.y,
3481 window->pending_allocation.width,
3482 window->pending_allocation.height);
3483
3484 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003485
3486 /* The main surface is in the list, too. Main surface's
3487 * resize_handler is responsible for calling widget_set_allocation()
3488 * on all sub-surface root widgets, so they will be resized
3489 * properly.
3490 */
3491 wl_list_for_each(surface, &window->subsurface_list, link) {
3492 if (surface == window->main_surface)
3493 continue;
3494
3495 surface_set_synchronized(surface);
3496 surface_resize(surface);
3497 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003498}
3499
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003500void
3501window_schedule_resize(struct window *window, int width, int height)
3502{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003503 window->pending_allocation.x = 0;
3504 window->pending_allocation.y = 0;
3505 window->pending_allocation.width = width;
3506 window->pending_allocation.height = height;
3507
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003508 if (window->min_allocation.width == 0)
3509 window->min_allocation = window->pending_allocation;
3510 if (window->pending_allocation.width < window->min_allocation.width)
3511 window->pending_allocation.width = window->min_allocation.width;
3512 if (window->pending_allocation.height < window->min_allocation.height)
3513 window->pending_allocation.height = window->min_allocation.height;
3514
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003515 window->resize_needed = 1;
3516 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003517}
3518
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003519void
3520widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3521{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003522 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003523}
3524
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003525static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003526handle_ping(void *data, struct wl_shell_surface *shell_surface,
3527 uint32_t serial)
3528{
3529 wl_shell_surface_pong(shell_surface, serial);
3530}
3531
3532static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003533handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003534 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003535{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003536 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003537
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003538 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003539 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003540}
3541
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003542static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003543menu_destroy(struct menu *menu)
3544{
3545 widget_destroy(menu->widget);
3546 window_destroy(menu->window);
3547 free(menu);
3548}
3549
3550static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003551handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3552{
3553 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003554 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003555
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003556 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003557 * device. Or just use wl_callback. And this really needs to
3558 * be a window vfunc that the menu can set. And we need the
3559 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003560
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003561 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003562 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003563}
3564
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003565static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003566 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003567 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003568 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003569};
3570
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003571void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003572window_get_allocation(struct window *window,
3573 struct rectangle *allocation)
3574{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003575 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003576}
3577
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003578static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003579widget_redraw(struct widget *widget)
3580{
3581 struct widget *child;
3582
3583 if (widget->redraw_handler)
3584 widget->redraw_handler(widget, widget->user_data);
3585 wl_list_for_each(child, &widget->child_list, link)
3586 widget_redraw(child);
3587}
3588
3589static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003590frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3591{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003592 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003593
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003594 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003595 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003596 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003597 surface->frame_cb = NULL;
3598
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003599 surface->last_time = time;
3600
Pekka Paalanen71233882013-04-25 13:57:53 +03003601 if (surface->redraw_needed || surface->window->redraw_needed) {
3602 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003603 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003604 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003605}
3606
3607static const struct wl_callback_listener listener = {
3608 frame_callback
3609};
3610
3611static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003612surface_redraw(struct surface *surface)
3613{
Pekka Paalanen71233882013-04-25 13:57:53 +03003614 DBG_OBJ(surface->surface, "begin\n");
3615
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003616 if (!surface->window->redraw_needed && !surface->redraw_needed)
3617 return;
3618
3619 /* Whole-window redraw forces a redraw even if the previous has
3620 * not yet hit the screen.
3621 */
3622 if (surface->frame_cb) {
3623 if (!surface->window->redraw_needed)
3624 return;
3625
Pekka Paalanen71233882013-04-25 13:57:53 +03003626 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003627 wl_callback_destroy(surface->frame_cb);
3628 }
3629
3630 surface->frame_cb = wl_surface_frame(surface->surface);
3631 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003632 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003633
3634 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003635 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003636 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003637 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003638}
3639
3640static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003641idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003642{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003643 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003644 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003645
Pekka Paalanen71233882013-04-25 13:57:53 +03003646 DBG(" --------- \n");
3647
Pekka Paalaneneebff542013-04-25 13:57:52 +03003648 wl_list_init(&window->redraw_task.link);
3649 window->redraw_task_scheduled = 0;
3650
3651 if (window->resize_needed) {
3652 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003653 if (window->main_surface->frame_cb) {
3654 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003655 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003656 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003657
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003658 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003659 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003660
Pekka Paalanen35e82632013-04-25 13:57:48 +03003661 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003662 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003663
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003664 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003665 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003666
3667 wl_list_for_each(surface, &window->subsurface_list, link)
3668 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003669}
3670
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003671static void
3672window_schedule_redraw_task(struct window *window)
3673{
3674 if (window->configure_requests)
3675 return;
3676 if (!window->redraw_task_scheduled) {
3677 window->redraw_task.run = idle_redraw;
3678 display_defer(window->display, &window->redraw_task);
3679 window->redraw_task_scheduled = 1;
3680 }
3681}
3682
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003683void
3684window_schedule_redraw(struct window *window)
3685{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003686 struct surface *surface;
3687
Pekka Paalanen71233882013-04-25 13:57:53 +03003688 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3689
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003690 wl_list_for_each(surface, &window->subsurface_list, link)
3691 surface->redraw_needed = 1;
3692
3693 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003694}
3695
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003696int
3697window_is_fullscreen(struct window *window)
3698{
3699 return window->type == TYPE_FULLSCREEN;
3700}
3701
MoD063a8822013-03-02 23:43:57 +00003702static void
3703configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3704{
3705 struct window *window = data;
3706
3707 wl_callback_destroy(callback);
3708 window->configure_requests--;
3709
3710 if (!window->configure_requests)
3711 window_schedule_redraw(window);
3712}
3713
3714static struct wl_callback_listener configure_request_listener = {
3715 configure_request_completed,
3716};
3717
3718static void
3719window_defer_redraw_until_configure(struct window* window)
3720{
3721 struct wl_callback *callback;
3722
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003723 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003724 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003725 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003726 }
3727
3728 callback = wl_display_sync(window->display->display);
3729 wl_callback_add_listener(callback, &configure_request_listener, window);
3730 window->configure_requests++;
3731}
3732
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003733void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003734window_set_fullscreen(struct window *window, int fullscreen)
3735{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003736 if (!window->display->shell)
3737 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003738
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003739 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003740 return;
3741
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003742 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003743 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003744 if (window->type == TYPE_TOPLEVEL) {
3745 window->saved_allocation = window->main_surface->allocation;
3746 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003747 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003748 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003749 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003750 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003751 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003752 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003753 if (window->saved_type == TYPE_MAXIMIZED) {
3754 window_set_maximized(window, 1);
3755 } else {
3756 window->type = TYPE_TOPLEVEL;
3757 wl_shell_surface_set_toplevel(window->shell_surface);
3758 window_schedule_resize(window,
3759 window->saved_allocation.width,
3760 window->saved_allocation.height);
3761 }
3762
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003763 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003764}
3765
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003766void
3767window_set_fullscreen_method(struct window *window,
3768 enum wl_shell_surface_fullscreen_method method)
3769{
3770 window->fullscreen_method = method;
3771}
3772
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003773int
3774window_is_maximized(struct window *window)
3775{
3776 return window->type == TYPE_MAXIMIZED;
3777}
3778
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003779void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003780window_set_maximized(struct window *window, int maximized)
3781{
3782 if (!window->display->shell)
3783 return;
3784
3785 if ((window->type == TYPE_MAXIMIZED) == maximized)
3786 return;
3787
3788 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003789 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003790 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3791 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003792 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003793 } else if (window->type == TYPE_FULLSCREEN) {
3794 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3795 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003796 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003797 } else {
3798 wl_shell_surface_set_toplevel(window->shell_surface);
3799 window->type = TYPE_TOPLEVEL;
3800 window_schedule_resize(window,
3801 window->saved_allocation.width,
3802 window->saved_allocation.height);
3803 }
3804}
3805
3806void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003807window_set_user_data(struct window *window, void *data)
3808{
3809 window->user_data = data;
3810}
3811
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003812void *
3813window_get_user_data(struct window *window)
3814{
3815 return window->user_data;
3816}
3817
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003818void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003819window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003820 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003821{
3822 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003823}
3824
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003825void
3826window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003827 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003828{
3829 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003830}
3831
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003832void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003833window_set_data_handler(struct window *window, window_data_handler_t handler)
3834{
3835 window->data_handler = handler;
3836}
3837
3838void
3839window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3840{
3841 window->drop_handler = handler;
3842}
3843
3844void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003845window_set_close_handler(struct window *window,
3846 window_close_handler_t handler)
3847{
3848 window->close_handler = handler;
3849}
3850
3851void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003852window_set_fullscreen_handler(struct window *window,
3853 window_fullscreen_handler_t handler)
3854{
3855 window->fullscreen_handler = handler;
3856}
3857
3858void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003859window_set_output_handler(struct window *window,
3860 window_output_handler_t handler)
3861{
3862 window->output_handler = handler;
3863}
3864
3865void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003866window_set_title(struct window *window, const char *title)
3867{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003868 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003869 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003870 if (window->shell_surface)
3871 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003872}
3873
3874const char *
3875window_get_title(struct window *window)
3876{
3877 return window->title;
3878}
3879
3880void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003881window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3882{
3883 struct text_cursor_position *text_cursor_position =
3884 window->display->text_cursor_position;
3885
Scott Moreau9295ce02012-06-01 12:46:10 -06003886 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003887 return;
3888
3889 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003890 window->main_surface->surface,
3891 wl_fixed_from_int(x),
3892 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003893}
3894
3895void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003896window_damage(struct window *window, int32_t x, int32_t y,
3897 int32_t width, int32_t height)
3898{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003899 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003900}
3901
Casey Dahlin9074db52012-04-19 22:50:09 -04003902static void
3903surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003904 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003905{
Rob Bradford7507b572012-05-15 17:55:34 +01003906 struct window *window = data;
3907 struct output *output;
3908 struct output *output_found = NULL;
3909 struct window_output *window_output;
3910
3911 wl_list_for_each(output, &window->display->output_list, link) {
3912 if (output->output == wl_output) {
3913 output_found = output;
3914 break;
3915 }
3916 }
3917
3918 if (!output_found)
3919 return;
3920
3921 window_output = malloc (sizeof *window_output);
3922 window_output->output = output_found;
3923
3924 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003925
3926 if (window->output_handler)
3927 window->output_handler(window, output_found, 1,
3928 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003929}
3930
3931static void
3932surface_leave(void *data,
3933 struct wl_surface *wl_surface, struct wl_output *output)
3934{
Rob Bradford7507b572012-05-15 17:55:34 +01003935 struct window *window = data;
3936 struct window_output *window_output;
3937 struct window_output *window_output_found = NULL;
3938
3939 wl_list_for_each(window_output, &window->window_output_list, link) {
3940 if (window_output->output->output == output) {
3941 window_output_found = window_output;
3942 break;
3943 }
3944 }
3945
3946 if (window_output_found) {
3947 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003948
3949 if (window->output_handler)
3950 window->output_handler(window, window_output->output,
3951 0, window->user_data);
3952
Rob Bradford7507b572012-05-15 17:55:34 +01003953 free(window_output_found);
3954 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003955}
3956
3957static const struct wl_surface_listener surface_listener = {
3958 surface_enter,
3959 surface_leave
3960};
3961
Pekka Paalanen4e373742013-02-13 16:17:13 +02003962static struct surface *
3963surface_create(struct window *window)
3964{
3965 struct display *display = window->display;
3966 struct surface *surface;
3967
3968 surface = calloc(1, sizeof *surface);
3969 if (!surface)
3970 return NULL;
3971
3972 surface->window = window;
3973 surface->surface = wl_compositor_create_surface(display->compositor);
3974 wl_surface_add_listener(surface->surface, &surface_listener, window);
3975
Pekka Paalanen35e82632013-04-25 13:57:48 +03003976 wl_list_insert(&window->subsurface_list, &surface->link);
3977
Pekka Paalanen4e373742013-02-13 16:17:13 +02003978 return surface;
3979}
3980
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003981static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003982window_create_internal(struct display *display,
3983 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003984{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003985 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003986 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003987
3988 window = malloc(sizeof *window);
3989 if (window == NULL)
3990 return NULL;
3991
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003992 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003993 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003994 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003995 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02003996
3997 surface = surface_create(window);
3998 window->main_surface = surface;
3999
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004000 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004001 window->shell_surface =
4002 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004003 surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004004 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004005
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004006 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004007 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004008 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004009
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004010 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004011#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004012 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004013#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004014 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004015#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004016 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004017 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004018
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004019 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004020 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004021 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004022
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004023 if (window->shell_surface) {
4024 wl_shell_surface_set_user_data(window->shell_surface, window);
4025 wl_shell_surface_add_listener(window->shell_surface,
4026 &shell_surface_listener, window);
4027 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004028
Rob Bradford7507b572012-05-15 17:55:34 +01004029 wl_list_init (&window->window_output_list);
4030
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004031 return window;
4032}
4033
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004034struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004035window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004036{
4037 struct window *window;
4038
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004039 window = window_create_internal(display, NULL, TYPE_NONE);
4040 if (!window)
4041 return NULL;
4042
4043 return window;
4044}
4045
4046struct window *
4047window_create_custom(struct display *display)
4048{
4049 struct window *window;
4050
4051 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004052 if (!window)
4053 return NULL;
4054
4055 return window;
4056}
4057
4058struct window *
4059window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004060 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004061{
4062 struct window *window;
4063
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004064 window = window_create_internal(parent->display,
4065 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004066 if (!window)
4067 return NULL;
4068
4069 window->x = x;
4070 window->y = y;
4071
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004072 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004073 wl_shell_surface_set_transient(
4074 window->shell_surface,
4075 window->parent->main_surface->surface,
4076 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004077
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004078 return window;
4079}
4080
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004081static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004082menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004083{
4084 int next;
4085
4086 next = (sy - 8) / 20;
4087 if (menu->current != next) {
4088 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004089 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004090 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004091}
4092
4093static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004094menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004095 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004096 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004097{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004098 struct menu *menu = data;
4099
4100 if (widget == menu->widget)
4101 menu_set_item(data, y);
4102
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004103 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004104}
4105
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004106static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004107menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004108 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004109{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004110 struct menu *menu = data;
4111
4112 if (widget == menu->widget)
4113 menu_set_item(data, y);
4114
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004115 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004116}
4117
4118static void
4119menu_leave_handler(struct widget *widget, struct input *input, void *data)
4120{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004121 struct menu *menu = data;
4122
4123 if (widget == menu->widget)
4124 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004125}
4126
4127static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004128menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004129 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004130 uint32_t button, enum wl_pointer_button_state state,
4131 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004132
4133{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004134 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004135
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004136 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4137 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004138 /* Either relase after press-drag-release or
4139 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004140 menu->func(menu->window->parent,
4141 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004142 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004143 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004144 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004145 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004146 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004147}
4148
4149static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004150menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004151{
4152 cairo_t *cr;
4153 const int32_t r = 3, margin = 3;
4154 struct menu *menu = data;
4155 int32_t width, height, i;
4156
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004157 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004158 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4159 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4160 cairo_paint(cr);
4161
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004162 width = widget->allocation.width;
4163 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004164 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004165
4166 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004167 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4168 cairo_fill(cr);
4169
4170 for (i = 0; i < menu->count; i++) {
4171 if (i == menu->current) {
4172 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4173 cairo_rectangle(cr, margin, i * 20 + margin,
4174 width - 2 * margin, 20);
4175 cairo_fill(cr);
4176 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4177 cairo_move_to(cr, 10, i * 20 + 16);
4178 cairo_show_text(cr, menu->entries[i]);
4179 } else {
4180 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4181 cairo_move_to(cr, 10, i * 20 + 16);
4182 cairo_show_text(cr, menu->entries[i]);
4183 }
4184 }
4185
4186 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004187}
4188
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004189void
4190window_show_menu(struct display *display,
4191 struct input *input, uint32_t time, struct window *parent,
4192 int32_t x, int32_t y,
4193 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004194{
4195 struct window *window;
4196 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004197 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004198
4199 menu = malloc(sizeof *menu);
4200 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004201 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004202
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004203 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004204 if (!window) {
4205 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004206 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004207 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004208
4209 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004210 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004211 menu->entries = entries;
4212 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004213 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004214 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004215 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004216 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004217 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004218 window->type = TYPE_MENU;
4219 window->x = x;
4220 window->y = y;
4221
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004222 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004223 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004224 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004225 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004226 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004227
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004228 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004229 widget_set_enter_handler(menu->widget, menu_enter_handler);
4230 widget_set_leave_handler(menu->widget, menu_leave_handler);
4231 widget_set_motion_handler(menu->widget, menu_motion_handler);
4232 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004233
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004234 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004235 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004236}
4237
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004238void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004239window_set_buffer_type(struct window *window, enum window_buffer_type type)
4240{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004241 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004242}
4243
Pekka Paalanen35e82632013-04-25 13:57:48 +03004244struct widget *
4245window_add_subsurface(struct window *window, void *data,
4246 enum subsurface_mode default_mode)
4247{
4248 struct widget *widget;
4249 struct surface *surface;
4250 struct wl_surface *parent;
4251 struct wl_subcompositor *subcompo = window->display->subcompositor;
4252
4253 if (!subcompo)
4254 return NULL;
4255
4256 surface = surface_create(window);
4257 widget = widget_create(window, surface, data);
4258 wl_list_init(&widget->link);
4259 surface->widget = widget;
4260
4261 parent = window->main_surface->surface;
4262 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4263 surface->surface,
4264 parent);
4265 surface->synchronized = 1;
4266
4267 switch (default_mode) {
4268 case SUBSURFACE_SYNCHRONIZED:
4269 surface->synchronized_default = 1;
4270 break;
4271 case SUBSURFACE_DESYNCHRONIZED:
4272 surface->synchronized_default = 0;
4273 break;
4274 default:
4275 assert(!"bad enum subsurface_mode");
4276 }
4277
4278 return widget;
4279}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004280
4281static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004282display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004283 struct wl_output *wl_output,
4284 int x, int y,
4285 int physical_width,
4286 int physical_height,
4287 int subpixel,
4288 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004289 const char *model,
4290 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004291{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004292 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004293
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004294 output->allocation.x = x;
4295 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004296 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004297}
4298
4299static void
4300display_handle_mode(void *data,
4301 struct wl_output *wl_output,
4302 uint32_t flags,
4303 int width,
4304 int height,
4305 int refresh)
4306{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004307 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004308 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004309
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004310 if (flags & WL_OUTPUT_MODE_CURRENT) {
4311 output->allocation.width = width;
4312 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004313 if (display->output_configure_handler)
4314 (*display->output_configure_handler)(
4315 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004316 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004317}
4318
4319static const struct wl_output_listener output_listener = {
4320 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004321 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004322};
4323
4324static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004325display_add_output(struct display *d, uint32_t id)
4326{
4327 struct output *output;
4328
4329 output = malloc(sizeof *output);
4330 if (output == NULL)
4331 return;
4332
4333 memset(output, 0, sizeof *output);
4334 output->display = d;
4335 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004336 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004337 wl_list_insert(d->output_list.prev, &output->link);
4338
4339 wl_output_add_listener(output->output, &output_listener, output);
4340}
4341
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004342static void
4343output_destroy(struct output *output)
4344{
4345 if (output->destroy_handler)
4346 (*output->destroy_handler)(output, output->user_data);
4347
4348 wl_output_destroy(output->output);
4349 wl_list_remove(&output->link);
4350 free(output);
4351}
4352
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004353void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004354display_set_global_handler(struct display *display,
4355 display_global_handler_t handler)
4356{
4357 struct global *global;
4358
4359 display->global_handler = handler;
4360 if (!handler)
4361 return;
4362
4363 wl_list_for_each(global, &display->global_list, link)
4364 display->global_handler(display,
4365 global->name, global->interface,
4366 global->version, display->user_data);
4367}
4368
4369void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004370display_set_output_configure_handler(struct display *display,
4371 display_output_handler_t handler)
4372{
4373 struct output *output;
4374
4375 display->output_configure_handler = handler;
4376 if (!handler)
4377 return;
4378
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004379 wl_list_for_each(output, &display->output_list, link) {
4380 if (output->allocation.width == 0 &&
4381 output->allocation.height == 0)
4382 continue;
4383
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004384 (*display->output_configure_handler)(output,
4385 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004386 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004387}
4388
4389void
4390output_set_user_data(struct output *output, void *data)
4391{
4392 output->user_data = data;
4393}
4394
4395void *
4396output_get_user_data(struct output *output)
4397{
4398 return output->user_data;
4399}
4400
4401void
4402output_set_destroy_handler(struct output *output,
4403 display_output_handler_t handler)
4404{
4405 output->destroy_handler = handler;
4406 /* FIXME: implement this, once we have way to remove outputs */
4407}
4408
4409void
Scott Moreau4e072362012-09-29 02:03:11 -06004410output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004411{
Scott Moreau4e072362012-09-29 02:03:11 -06004412 struct rectangle allocation = output->allocation;
4413
4414 switch (output->transform) {
4415 case WL_OUTPUT_TRANSFORM_90:
4416 case WL_OUTPUT_TRANSFORM_270:
4417 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4418 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4419 /* Swap width and height */
4420 allocation.width = output->allocation.height;
4421 allocation.height = output->allocation.width;
4422 break;
4423 }
4424
4425 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004426}
4427
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004428struct wl_output *
4429output_get_wl_output(struct output *output)
4430{
4431 return output->output;
4432}
4433
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004434enum wl_output_transform
4435output_get_transform(struct output *output)
4436{
4437 return output->transform;
4438}
4439
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004440static void
Daniel Stone97f68542012-05-30 16:32:01 +01004441fini_xkb(struct input *input)
4442{
4443 xkb_state_unref(input->xkb.state);
4444 xkb_map_unref(input->xkb.keymap);
4445}
4446
4447static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004448display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004449{
4450 struct input *input;
4451
4452 input = malloc(sizeof *input);
4453 if (input == NULL)
4454 return;
4455
4456 memset(input, 0, sizeof *input);
4457 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004458 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004459 input->pointer_focus = NULL;
4460 input->keyboard_focus = NULL;
4461 wl_list_insert(d->input_list.prev, &input->link);
4462
Daniel Stone37816df2012-05-16 18:45:18 +01004463 wl_seat_add_listener(input->seat, &seat_listener, input);
4464 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004465
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004466 input->data_device =
4467 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004468 input->seat);
4469 wl_data_device_add_listener(input->data_device, &data_device_listener,
4470 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004471
4472 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004473
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004474 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4475 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004476 input->repeat_task.run = keyboard_repeat_func;
4477 display_watch_fd(d, input->repeat_timer_fd,
4478 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004479}
4480
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004481static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004482input_destroy(struct input *input)
4483{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004484 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004485 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004486
4487 if (input->drag_offer)
4488 data_offer_destroy(input->drag_offer);
4489
4490 if (input->selection_offer)
4491 data_offer_destroy(input->selection_offer);
4492
4493 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004494 fini_xkb(input);
4495
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004496 wl_surface_destroy(input->pointer_surface);
4497
Pekka Paalanene1207c72011-12-16 12:02:09 +02004498 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004499 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004500 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004501 free(input);
4502}
4503
4504static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004505init_workspace_manager(struct display *d, uint32_t id)
4506{
4507 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004508 wl_registry_bind(d->registry, id,
4509 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004510 if (d->workspace_manager != NULL)
4511 workspace_manager_add_listener(d->workspace_manager,
4512 &workspace_manager_listener,
4513 d);
4514}
4515
4516static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004517registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4518 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004519{
4520 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004521 struct global *global;
4522
4523 global = malloc(sizeof *global);
4524 global->name = id;
4525 global->interface = strdup(interface);
4526 global->version = version;
4527 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004528
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004529 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004530 d->compositor = wl_registry_bind(registry, id,
4531 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004532 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004533 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004534 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004535 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004536 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004537 d->shell = wl_registry_bind(registry,
4538 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004539 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004540 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004541 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4542 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004543 wl_registry_bind(registry, id,
4544 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004545 } else if (strcmp(interface, "text_cursor_position") == 0) {
4546 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004547 wl_registry_bind(registry, id,
4548 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004549 } else if (strcmp(interface, "workspace_manager") == 0) {
4550 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004551 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4552 d->subcompositor =
4553 wl_registry_bind(registry, id,
4554 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004555 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004556
4557 if (d->global_handler)
4558 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004559}
4560
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004561static void
4562registry_handle_global_remove(void *data, struct wl_registry *registry,
4563 uint32_t name)
4564{
4565 struct display *d = data;
4566 struct global *global;
4567 struct global *tmp;
4568
4569 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4570 if (global->name != name)
4571 continue;
4572
4573 /* XXX: Should destroy bound globals, and call
4574 * the counterpart of display::global_handler
4575 */
4576 wl_list_remove(&global->link);
4577 free(global->interface);
4578 free(global);
4579 }
4580}
4581
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004582void *
4583display_bind(struct display *display, uint32_t name,
4584 const struct wl_interface *interface, uint32_t version)
4585{
4586 return wl_registry_bind(display->registry, name, interface, version);
4587}
4588
4589static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004590 registry_handle_global,
4591 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004592};
4593
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004594#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004595static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004596init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004597{
4598 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004599 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004600
Rob Clark6396ed32012-03-11 19:48:41 -05004601#ifdef USE_CAIRO_GLESV2
4602# define GL_BIT EGL_OPENGL_ES2_BIT
4603#else
4604# define GL_BIT EGL_OPENGL_BIT
4605#endif
4606
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004607 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004608 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004609 EGL_RED_SIZE, 1,
4610 EGL_GREEN_SIZE, 1,
4611 EGL_BLUE_SIZE, 1,
4612 EGL_ALPHA_SIZE, 1,
4613 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004614 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004615 EGL_NONE
4616 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004617
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004618#ifdef USE_CAIRO_GLESV2
4619 static const EGLint context_attribs[] = {
4620 EGL_CONTEXT_CLIENT_VERSION, 2,
4621 EGL_NONE
4622 };
4623 EGLint api = EGL_OPENGL_ES_API;
4624#else
4625 EGLint *context_attribs = NULL;
4626 EGLint api = EGL_OPENGL_API;
4627#endif
4628
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004629 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004630 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004631 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004632 return -1;
4633 }
4634
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004635 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004636 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004637 return -1;
4638 }
4639
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004640 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4641 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004642 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004643 return -1;
4644 }
4645
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004646 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004647 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004648 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004649 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004650 return -1;
4651 }
4652
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004653 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004654 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004655 return -1;
4656 }
4657
Benjamin Franzke0c991632011-09-27 21:57:31 +02004658 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4659 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004660 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004661 return -1;
4662 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004663
4664 return 0;
4665}
4666
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004667static void
4668fini_egl(struct display *display)
4669{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004670 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004671
4672 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4673 EGL_NO_CONTEXT);
4674
4675 eglTerminate(display->dpy);
4676 eglReleaseThread();
4677}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004678#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004679
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004680static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004681init_dummy_surface(struct display *display)
4682{
4683 int len;
4684 void *data;
4685
4686 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4687 data = malloc(len);
4688 display->dummy_surface =
4689 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4690 1, 1, len);
4691 display->dummy_surface_data = data;
4692}
4693
4694static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004695handle_display_data(struct task *task, uint32_t events)
4696{
4697 struct display *display =
4698 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004699 struct epoll_event ep;
4700 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004701
4702 display->display_fd_events = events;
4703
4704 if (events & EPOLLERR || events & EPOLLHUP) {
4705 display_exit(display);
4706 return;
4707 }
4708
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004709 if (events & EPOLLIN) {
4710 ret = wl_display_dispatch(display->display);
4711 if (ret == -1) {
4712 display_exit(display);
4713 return;
4714 }
4715 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004716
4717 if (events & EPOLLOUT) {
4718 ret = wl_display_flush(display->display);
4719 if (ret == 0) {
4720 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4721 ep.data.ptr = &display->display_task;
4722 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4723 display->display_fd, &ep);
4724 } else if (ret == -1 && errno != EAGAIN) {
4725 display_exit(display);
4726 return;
4727 }
4728 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004729}
4730
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004731static void
4732log_handler(const char *format, va_list args)
4733{
4734 vfprintf(stderr, format, args);
4735}
4736
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004737struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004738display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004739{
4740 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004741
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004742 wl_log_set_handler_client(log_handler);
4743
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004744 d = malloc(sizeof *d);
4745 if (d == NULL)
4746 return NULL;
4747
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004748 memset(d, 0, sizeof *d);
4749
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004750 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004751 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004752 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004753 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004754 return NULL;
4755 }
4756
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004757 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004758 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004759 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004760 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4761 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004762
4763 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004764 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004765 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004766 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004767
Daniel Stone97f68542012-05-30 16:32:01 +01004768 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004769 if (d->xkb_context == NULL) {
4770 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004771 return NULL;
4772 }
4773
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004774 d->workspace = 0;
4775 d->workspace_count = 1;
4776
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004777 d->registry = wl_display_get_registry(d->display);
4778 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004779
4780 if (wl_display_dispatch(d->display) < 0) {
4781 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4782 return NULL;
4783 }
4784
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004785#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004786 if (init_egl(d) < 0)
4787 fprintf(stderr, "EGL does not seem to work, "
4788 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004789#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004790
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004791 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004792
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004793 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004794
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004795 wl_list_init(&d->window_list);
4796
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004797 init_dummy_surface(d);
4798
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004799 return d;
4800}
4801
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004802static void
4803display_destroy_outputs(struct display *display)
4804{
4805 struct output *tmp;
4806 struct output *output;
4807
4808 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4809 output_destroy(output);
4810}
4811
Pekka Paalanene1207c72011-12-16 12:02:09 +02004812static void
4813display_destroy_inputs(struct display *display)
4814{
4815 struct input *tmp;
4816 struct input *input;
4817
4818 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4819 input_destroy(input);
4820}
4821
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004822void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004823display_destroy(struct display *display)
4824{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004825 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004826 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4827 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004828
4829 if (!wl_list_empty(&display->deferred_list))
4830 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4831
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004832 cairo_surface_destroy(display->dummy_surface);
4833 free(display->dummy_surface_data);
4834
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004835 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004836 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004837
Daniel Stone97f68542012-05-30 16:32:01 +01004838 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004839
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004840 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004841 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004842
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004843#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004844 if (display->argb_device)
4845 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004846#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004847
Pekka Paalanen35e82632013-04-25 13:57:48 +03004848 if (display->subcompositor)
4849 wl_subcompositor_destroy(display->subcompositor);
4850
Pekka Paalanenc2052982011-12-16 11:41:32 +02004851 if (display->shell)
4852 wl_shell_destroy(display->shell);
4853
4854 if (display->shm)
4855 wl_shm_destroy(display->shm);
4856
4857 if (display->data_device_manager)
4858 wl_data_device_manager_destroy(display->data_device_manager);
4859
4860 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004861 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004862
4863 close(display->epoll_fd);
4864
U. Artie Eoff44874d92012-10-02 21:12:35 -07004865 if (!(display->display_fd_events & EPOLLERR) &&
4866 !(display->display_fd_events & EPOLLHUP))
4867 wl_display_flush(display->display);
4868
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004869 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004870 free(display);
4871}
4872
4873void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004874display_set_user_data(struct display *display, void *data)
4875{
4876 display->user_data = data;
4877}
4878
4879void *
4880display_get_user_data(struct display *display)
4881{
4882 return display->user_data;
4883}
4884
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004885struct wl_display *
4886display_get_display(struct display *display)
4887{
4888 return display->display;
4889}
4890
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004891struct output *
4892display_get_output(struct display *display)
4893{
4894 return container_of(display->output_list.next, struct output, link);
4895}
4896
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004897struct wl_compositor *
4898display_get_compositor(struct display *display)
4899{
4900 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004901}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004902
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004903uint32_t
4904display_get_serial(struct display *display)
4905{
4906 return display->serial;
4907}
4908
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004909EGLDisplay
4910display_get_egl_display(struct display *d)
4911{
4912 return d->dpy;
4913}
4914
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004915struct wl_data_source *
4916display_create_data_source(struct display *display)
4917{
4918 return wl_data_device_manager_create_data_source(display->data_device_manager);
4919}
4920
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004921EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004922display_get_argb_egl_config(struct display *d)
4923{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004924 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004925}
4926
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004927struct wl_shell *
4928display_get_shell(struct display *display)
4929{
4930 return display->shell;
4931}
4932
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004933int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004934display_acquire_window_surface(struct display *display,
4935 struct window *window,
4936 EGLContext ctx)
4937{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004938 struct surface *surface = window->main_surface;
4939
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004940 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004941 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004942
Pekka Paalanen6f41b072013-02-20 13:39:17 +02004943 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004944 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004945}
4946
4947void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004948display_release_window_surface(struct display *display,
4949 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004950{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004951 struct surface *surface = window->main_surface;
4952
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004953 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004954 return;
4955
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004956 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004957}
4958
4959void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004960display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004961{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004962 wl_list_insert(&display->deferred_list, &task->link);
4963}
4964
4965void
4966display_watch_fd(struct display *display,
4967 int fd, uint32_t events, struct task *task)
4968{
4969 struct epoll_event ep;
4970
4971 ep.events = events;
4972 ep.data.ptr = task;
4973 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4974}
4975
4976void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004977display_unwatch_fd(struct display *display, int fd)
4978{
4979 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4980}
4981
4982void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004983display_run(struct display *display)
4984{
4985 struct task *task;
4986 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004987 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004988
Pekka Paalanen826d7952011-12-15 10:14:07 +02004989 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004990 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004991 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004992 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004993 struct task, link);
4994 wl_list_remove(&task->link);
4995 task->run(task, 0);
4996 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004997
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004998 wl_display_dispatch_pending(display->display);
4999
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005000 if (!display->running)
5001 break;
5002
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005003 ret = wl_display_flush(display->display);
5004 if (ret < 0 && errno == EAGAIN) {
5005 ep[0].events =
5006 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5007 ep[0].data.ptr = &display->display_task;
5008
5009 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5010 display->display_fd, &ep[0]);
5011 } else if (ret < 0) {
5012 break;
5013 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005014
5015 count = epoll_wait(display->epoll_fd,
5016 ep, ARRAY_LENGTH(ep), -1);
5017 for (i = 0; i < count; i++) {
5018 task = ep[i].data.ptr;
5019 task->run(task, ep[i].events);
5020 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005021 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005022}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005023
5024void
5025display_exit(struct display *display)
5026{
5027 display->running = 0;
5028}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005029
5030void
5031keysym_modifiers_add(struct wl_array *modifiers_map,
5032 const char *name)
5033{
5034 size_t len = strlen(name) + 1;
5035 char *p;
5036
5037 p = wl_array_add(modifiers_map, len);
5038
5039 if (p == NULL)
5040 return;
5041
5042 strncpy(p, name, len);
5043}
5044
5045static xkb_mod_index_t
5046keysym_modifiers_get_index(struct wl_array *modifiers_map,
5047 const char *name)
5048{
5049 xkb_mod_index_t index = 0;
5050 char *p = modifiers_map->data;
5051
5052 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5053 if (strcmp(p, name) == 0)
5054 return index;
5055
5056 index++;
5057 p += strlen(p) + 1;
5058 }
5059
5060 return XKB_MOD_INVALID;
5061}
5062
5063xkb_mod_mask_t
5064keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5065 const char *name)
5066{
5067 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5068
5069 if (index == XKB_MOD_INVALID)
5070 return XKB_MOD_INVALID;
5071
5072 return 1 << index;
5073}