blob: de9e8d10bd340af24aca68e5a38dde7bc4bbcb00 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen99436862012-11-19 17:15:59 +02003 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040024#define _GNU_SOURCE
25
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026#include "../config.h"
27
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040042
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040043#include <pixman.h>
44
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050046#include <wayland-egl.h>
47
Rob Clark6396ed32012-03-11 19:48:41 -050048#ifdef USE_CAIRO_GLESV2
49#include <GLES2/gl2.h>
50#include <GLES2/gl2ext.h>
51#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050053#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <EGL/egl.h>
55#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040057#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030058#else /* HAVE_CAIRO_EGL */
59typedef void *EGLDisplay;
60typedef void *EGLConfig;
61typedef void *EGLContext;
62#define EGL_NO_DISPLAY ((EGLDisplay)0)
63#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050064
Daniel Stone9d4f0302012-02-15 16:33:21 +000065#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030066#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040067
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050068#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020069#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040070#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060071#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020072#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030073#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050074
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050075#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050076
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070077struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030078
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040079struct global {
80 uint32_t name;
81 char *interface;
82 uint32_t version;
83 struct wl_list link;
84};
85
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050086struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050087 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040088 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050089 struct wl_compositor *compositor;
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 Paalanen99436862012-11-19 17:15:59 +0200156 * If resize_hint is non-zero, the user is doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157 * Returns the Cairo surface to draw to.
158 */
159 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanen99436862012-11-19 17:15:59 +0200160 int width, int height, int resize_hint);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161
162 /*
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
166 */
167 void (*swap)(struct toysurface *base,
168 struct rectangle *server_allocation);
169
170 /*
171 * Make the toysurface current with the given EGL context.
172 * Returns 0 on success, and negative of failure.
173 */
174 int (*acquire)(struct toysurface *base, EGLContext ctx);
175
176 /*
177 * Release the toysurface from the EGL context, returning control
178 * to Cairo.
179 */
180 void (*release)(struct toysurface *base);
181
182 /*
183 * Destroy the toysurface, including the Cairo surface, any
184 * backing storage, and the Wayland protocol objects.
185 */
186 void (*destroy)(struct toysurface *base);
187};
188
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500189struct window {
190 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500191 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100192 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500193 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200194 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500195 struct wl_region *input_region;
196 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500197 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500198 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400199 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500200 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500201 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400202 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400203 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400204 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400205 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400206 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400207 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400208 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400209 int focus_count;
210
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400211 enum window_buffer_type buffer_type;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +0200212 enum wl_output_transform buffer_transform;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200213 struct toysurface *toysurface;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400214 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500215
Pekka Paalanen99436862012-11-19 17:15:59 +0200216 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400217
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500218 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500219 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400220 window_data_handler_t data_handler;
221 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500222 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400223 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200224 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400225
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300226 struct wl_callback *frame_cb;
227
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200228 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500229 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500230
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500231 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400232 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500233};
234
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500235struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500236 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300237 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500238 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400239 struct wl_list link;
240 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500241 widget_resize_handler_t resize_handler;
242 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500243 widget_enter_handler_t enter_handler;
244 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500245 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500246 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200247 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400248 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500249 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300250 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400251};
252
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400253struct input {
254 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100255 struct wl_seat *seat;
256 struct wl_pointer *pointer;
257 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400258 struct window *pointer_focus;
259 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300260 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300261 uint32_t cursor_anim_start;
262 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300263 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400264 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400265 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400266 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400267 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400268 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400269
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500270 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500271 struct widget *grab;
272 uint32_t grab_button;
273
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400274 struct wl_data_device *data_device;
275 struct data_offer *drag_offer;
276 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100277
278 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100279 struct xkb_keymap *keymap;
280 struct xkb_state *state;
281 xkb_mod_mask_t control_mask;
282 xkb_mod_mask_t alt_mask;
283 xkb_mod_mask_t shift_mask;
284 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400285
286 struct task repeat_task;
287 int repeat_timer_fd;
288 uint32_t repeat_sym;
289 uint32_t repeat_key;
290 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400291};
292
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500293struct output {
294 struct display *display;
295 struct wl_output *output;
296 struct rectangle allocation;
297 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600298 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200299
300 display_output_handler_t destroy_handler;
301 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500302};
303
Martin Minarik1998b152012-05-10 02:04:35 +0200304enum frame_button_action {
305 FRAME_BUTTON_NULL = 0,
306 FRAME_BUTTON_ICON = 1,
307 FRAME_BUTTON_CLOSE = 2,
308 FRAME_BUTTON_MINIMIZE = 3,
309 FRAME_BUTTON_MAXIMIZE = 4,
310};
311
312enum frame_button_pointer {
313 FRAME_BUTTON_DEFAULT = 0,
314 FRAME_BUTTON_OVER = 1,
315 FRAME_BUTTON_ACTIVE = 2,
316};
317
318enum frame_button_align {
319 FRAME_BUTTON_RIGHT = 0,
320 FRAME_BUTTON_LEFT = 1,
321};
322
323enum frame_button_decoration {
324 FRAME_BUTTON_NONE = 0,
325 FRAME_BUTTON_FANCY = 1,
326};
327
328struct frame_button {
329 struct widget *widget;
330 struct frame *frame;
331 cairo_surface_t *icon;
332 enum frame_button_action type;
333 enum frame_button_pointer state;
334 struct wl_list link; /* buttons_list */
335 enum frame_button_align align;
336 enum frame_button_decoration decoration;
337};
338
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500339struct frame {
340 struct widget *widget;
341 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200342 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500343};
344
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500345struct menu {
346 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500347 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500348 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500349 const char **entries;
350 uint32_t time;
351 int current;
352 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400353 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500354 menu_func_t func;
355};
356
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300357struct tooltip {
358 struct widget *parent;
359 struct window *window;
360 struct widget *widget;
361 char *entry;
362 struct task tooltip_task;
363 int tooltip_fd;
364 float x, y;
365};
366
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700367struct shm_pool {
368 struct wl_shm_pool *pool;
369 size_t size;
370 size_t used;
371 void *data;
372};
373
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400374enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300375 CURSOR_DEFAULT = 100,
376 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400377};
378
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500379enum window_location {
380 WINDOW_INTERIOR = 0,
381 WINDOW_RESIZING_TOP = 1,
382 WINDOW_RESIZING_BOTTOM = 2,
383 WINDOW_RESIZING_LEFT = 4,
384 WINDOW_RESIZING_TOP_LEFT = 5,
385 WINDOW_RESIZING_BOTTOM_LEFT = 6,
386 WINDOW_RESIZING_RIGHT = 8,
387 WINDOW_RESIZING_TOP_RIGHT = 9,
388 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
389 WINDOW_RESIZING_MASK = 15,
390 WINDOW_EXTERIOR = 16,
391 WINDOW_TITLEBAR = 17,
392 WINDOW_CLIENT_AREA = 18,
393};
394
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200395static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500397#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200399struct egl_window_surface {
400 struct toysurface base;
401 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100402 struct display *display;
403 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200404 struct wl_egl_window *egl_window;
405 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100406};
407
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200408static struct egl_window_surface *
409to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100410{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200411 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100412}
413
414static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200415egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanen99436862012-11-19 17:15:59 +0200416 int width, int height, int resize_hint)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100417{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200418 struct egl_window_surface *surface = to_egl_window_surface(base);
419
420 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
421 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
422
423 return cairo_surface_reference(surface->cairo_surface);
424}
425
426static void
427egl_window_surface_swap(struct toysurface *base,
428 struct rectangle *server_allocation)
429{
430 struct egl_window_surface *surface = to_egl_window_surface(base);
431
432 cairo_gl_surface_swapbuffers(surface->cairo_surface);
433 wl_egl_window_get_attached_size(surface->egl_window,
434 &server_allocation->width,
435 &server_allocation->height);
436}
437
438static int
439egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
440{
441 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200442 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100443
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200444 device = cairo_surface_get_device(surface->cairo_surface);
445 if (!device)
446 return -1;
447
448 if (!ctx) {
449 if (device == surface->display->argb_device)
450 ctx = surface->display->argb_ctx;
451 else
452 assert(0);
453 }
454
455 cairo_device_flush(device);
456 cairo_device_acquire(device);
457 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
458 surface->egl_surface, ctx))
459 fprintf(stderr, "failed to make surface current\n");
460
461 return 0;
462}
463
464static void
465egl_window_surface_release(struct toysurface *base)
466{
467 struct egl_window_surface *surface = to_egl_window_surface(base);
468 cairo_device_t *device;
469
470 device = cairo_surface_get_device(surface->cairo_surface);
471 if (!device)
472 return;
473
474 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
475 surface->display->argb_ctx))
476 fprintf(stderr, "failed to make context current\n");
477
478 cairo_device_release(device);
479}
480
481static void
482egl_window_surface_destroy(struct toysurface *base)
483{
484 struct egl_window_surface *surface = to_egl_window_surface(base);
485 struct display *d = surface->display;
486
487 cairo_surface_destroy(surface->cairo_surface);
488 eglDestroySurface(d->dpy, surface->egl_surface);
489 wl_egl_window_destroy(surface->egl_window);
490 surface->surface = NULL;
491
492 free(surface);
493}
494
495static struct toysurface *
496egl_window_surface_create(struct display *display,
497 struct wl_surface *wl_surface,
498 uint32_t flags,
499 struct rectangle *rectangle)
500{
501 struct egl_window_surface *surface;
502
Pekka Paalanenb3627362012-11-19 17:16:00 +0200503 if (display->dpy == EGL_NO_DISPLAY)
504 return NULL;
505
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200506 surface = calloc(1, sizeof *surface);
507 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100508 return NULL;
509
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200510 surface->base.prepare = egl_window_surface_prepare;
511 surface->base.swap = egl_window_surface_swap;
512 surface->base.acquire = egl_window_surface_acquire;
513 surface->base.release = egl_window_surface_release;
514 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100515
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200516 surface->display = display;
517 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400518
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519 surface->egl_window = wl_egl_window_create(surface->surface,
520 rectangle->width,
521 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100522
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 surface->egl_surface = eglCreateWindowSurface(display->dpy,
524 display->argb_config,
525 surface->egl_window,
526 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100527
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200528 surface->cairo_surface =
529 cairo_gl_surface_create_for_egl(display->argb_device,
530 surface->egl_surface,
531 rectangle->width,
532 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100533
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100535}
536
Pekka Paalanenb3627362012-11-19 17:16:00 +0200537#else
538
539static struct toysurface *
540egl_window_surface_create(struct display *display,
541 struct wl_surface *wl_surface,
542 uint32_t flags,
543 struct rectangle *rectangle)
544{
545 return NULL;
546}
547
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548#endif
549
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200550struct shm_surface_data {
551 struct wl_buffer *buffer;
552 struct shm_pool *pool;
553};
554
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400555struct wl_buffer *
556display_get_buffer_for_surface(struct display *display,
557 cairo_surface_t *surface)
558{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200559 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200561 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562
563 return data->buffer;
564}
565
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500566static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700567shm_pool_destroy(struct shm_pool *pool);
568
569static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400570shm_surface_data_destroy(void *p)
571{
572 struct shm_surface_data *data = p;
573
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200574 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700575 if (data->pool)
576 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300577
578 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400579}
580
Kristian Høgsberg16626282012-04-03 11:21:27 -0400581static struct wl_shm_pool *
582make_shm_pool(struct display *display, int size, void **data)
583{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400584 struct wl_shm_pool *pool;
585 int fd;
586
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300587 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400588 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300589 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
590 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400591 return NULL;
592 }
593
594 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400595 if (*data == MAP_FAILED) {
596 fprintf(stderr, "mmap failed: %m\n");
597 close(fd);
598 return NULL;
599 }
600
601 pool = wl_shm_create_pool(display->shm, fd, size);
602
603 close(fd);
604
605 return pool;
606}
607
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700608static struct shm_pool *
609shm_pool_create(struct display *display, size_t size)
610{
611 struct shm_pool *pool = malloc(sizeof *pool);
612
613 if (!pool)
614 return NULL;
615
616 pool->pool = make_shm_pool(display, size, &pool->data);
617 if (!pool->pool) {
618 free(pool);
619 return NULL;
620 }
621
622 pool->size = size;
623 pool->used = 0;
624
625 return pool;
626}
627
628static void *
629shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
630{
631 if (pool->used + size > pool->size)
632 return NULL;
633
634 *offset = pool->used;
635 pool->used += size;
636
637 return (char *) pool->data + *offset;
638}
639
640/* destroy the pool. this does not unmap the memory though */
641static void
642shm_pool_destroy(struct shm_pool *pool)
643{
644 munmap(pool->data, pool->size);
645 wl_shm_pool_destroy(pool->pool);
646 free(pool);
647}
648
649/* Start allocating from the beginning of the pool again */
650static void
651shm_pool_reset(struct shm_pool *pool)
652{
653 pool->used = 0;
654}
655
656static int
657data_length_for_shm_surface(struct rectangle *rect)
658{
659 int stride;
660
661 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
662 rect->width);
663 return stride * rect->height;
664}
665
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500666static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700667display_create_shm_surface_from_pool(struct display *display,
668 struct rectangle *rectangle,
669 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670{
671 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400672 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400673 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700674 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400675 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400676
677 data = malloc(sizeof *data);
678 if (data == NULL)
679 return NULL;
680
681 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
682 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700683 length = stride * rectangle->height;
684 data->pool = NULL;
685 map = shm_pool_allocate(pool, length, &offset);
686
687 if (!map) {
688 free(data);
689 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400690 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400691
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400692 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400693 CAIRO_FORMAT_ARGB32,
694 rectangle->width,
695 rectangle->height,
696 stride);
697
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200698 cairo_surface_set_user_data(surface, &shm_surface_data_key,
699 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400700
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400701 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500702 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400703 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500704 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400705
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200706 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
707 rectangle->width,
708 rectangle->height,
709 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400710
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700711 return surface;
712}
713
714static cairo_surface_t *
715display_create_shm_surface(struct display *display,
716 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200717 struct shm_pool *alternate_pool,
718 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700719{
720 struct shm_surface_data *data;
721 struct shm_pool *pool;
722 cairo_surface_t *surface;
723
Pekka Paalanen99436862012-11-19 17:15:59 +0200724 if (alternate_pool) {
725 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700726 surface = display_create_shm_surface_from_pool(display,
727 rectangle,
728 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200729 alternate_pool);
730 if (surface) {
731 data = cairo_surface_get_user_data(surface,
732 &shm_surface_data_key);
733 goto out;
734 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700735 }
736
737 pool = shm_pool_create(display,
738 data_length_for_shm_surface(rectangle));
739 if (!pool)
740 return NULL;
741
742 surface =
743 display_create_shm_surface_from_pool(display, rectangle,
744 flags, pool);
745
746 if (!surface) {
747 shm_pool_destroy(pool);
748 return NULL;
749 }
750
751 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200752 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700753 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400754
Pekka Paalanen99436862012-11-19 17:15:59 +0200755out:
756 if (data_ret)
757 *data_ret = data;
758
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400759 return surface;
760}
761
nobled7b87cb02011-02-01 18:51:47 +0000762static int
763check_size(struct rectangle *rect)
764{
765 if (rect->width && rect->height)
766 return 0;
767
768 fprintf(stderr, "tried to create surface of "
769 "width: %d, height: %d\n", rect->width, rect->height);
770 return -1;
771}
772
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400773cairo_surface_t *
774display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100775 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400776 struct rectangle *rectangle,
777 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400778{
nobled7b87cb02011-02-01 18:51:47 +0000779 if (check_size(rectangle) < 0)
780 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200781
782 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200783 return display_create_shm_surface(display, rectangle, flags,
784 NULL, NULL);
785}
786
Pekka Paalanena4eda732012-11-19 17:16:02 +0200787struct shm_surface_leaf {
788 cairo_surface_t *cairo_surface;
789 /* 'data' is automatically destroyed, when 'cairo_surface' is */
790 struct shm_surface_data *data;
791
792 struct shm_pool *resize_pool;
793 int busy;
794};
795
796static void
797shm_surface_leaf_release(struct shm_surface_leaf *leaf)
798{
799 if (leaf->cairo_surface)
800 cairo_surface_destroy(leaf->cairo_surface);
801 /* leaf->data already destroyed via cairo private */
802
803 if (leaf->resize_pool)
804 shm_pool_destroy(leaf->resize_pool);
805}
806
Pekka Paalanen99436862012-11-19 17:15:59 +0200807struct shm_surface {
808 struct toysurface base;
809 struct display *display;
810 struct wl_surface *surface;
811 uint32_t flags;
812 int dx, dy;
813
Pekka Paalanena4eda732012-11-19 17:16:02 +0200814 struct shm_surface_leaf leaf[2];
815 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200816};
817
818static struct shm_surface *
819to_shm_surface(struct toysurface *base)
820{
821 return container_of(base, struct shm_surface, base);
822}
823
Pekka Paalanena4eda732012-11-19 17:16:02 +0200824static void
825shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
826{
827 struct shm_surface_leaf *leaf = data;
828
829 leaf->busy = 0;
830}
831
832static const struct wl_buffer_listener shm_surface_buffer_listener = {
833 shm_surface_buffer_release
834};
835
Pekka Paalanen99436862012-11-19 17:15:59 +0200836static cairo_surface_t *
837shm_surface_prepare(struct toysurface *base, int dx, int dy,
838 int width, int height, int resize_hint)
839{
840 struct shm_surface *surface = to_shm_surface(base);
841 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200842 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200843
844 surface->dx = dx;
845 surface->dy = dy;
846
Pekka Paalanena4eda732012-11-19 17:16:02 +0200847 /* pick a free buffer from the two */
848 if (!surface->leaf[0].busy)
849 leaf = &surface->leaf[0];
850 else if (!surface->leaf[1].busy)
851 leaf = &surface->leaf[1];
852 else {
853 fprintf(stderr, "%s: both buffers are held by the server.\n",
854 __func__);
855 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200856 }
857
Pekka Paalanena4eda732012-11-19 17:16:02 +0200858 if (!resize_hint && leaf->resize_pool) {
859 cairo_surface_destroy(leaf->cairo_surface);
860 leaf->cairo_surface = NULL;
861 shm_pool_destroy(leaf->resize_pool);
862 leaf->resize_pool = NULL;
863 }
864
865 if (leaf->cairo_surface &&
866 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
867 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200868 goto out;
869
Pekka Paalanena4eda732012-11-19 17:16:02 +0200870 if (leaf->cairo_surface)
871 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200872
Pekka Paalanena4eda732012-11-19 17:16:02 +0200873 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200874 /* Create a big pool to allocate from, while continuously
875 * resizing. Mmapping a new pool in the server
876 * is relatively expensive, so reusing a pool performs
877 * better, but may temporarily reserve unneeded memory.
878 */
879 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200880 leaf->resize_pool = shm_pool_create(surface->display,
881 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200882 }
883
Pekka Paalanena4eda732012-11-19 17:16:02 +0200884 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200885 display_create_shm_surface(surface->display, &rect,
886 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200887 leaf->resize_pool,
888 &leaf->data);
889 wl_buffer_add_listener(leaf->data->buffer,
890 &shm_surface_buffer_listener, leaf);
Pekka Paalanen99436862012-11-19 17:15:59 +0200891
892out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200893 surface->current = leaf;
894
895 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200896}
897
898static void
899shm_surface_swap(struct toysurface *base,
900 struct rectangle *server_allocation)
901{
902 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200903 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200904
905 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200906 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200907 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200908 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200909
Pekka Paalanena4eda732012-11-19 17:16:02 +0200910 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200911 surface->dx, surface->dy);
912 wl_surface_damage(surface->surface, 0, 0,
913 server_allocation->width, server_allocation->height);
914 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200915
916 leaf->busy = 1;
917 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200918}
919
920static int
921shm_surface_acquire(struct toysurface *base, EGLContext ctx)
922{
923 return -1;
924}
925
926static void
927shm_surface_release(struct toysurface *base)
928{
929}
930
931static void
932shm_surface_destroy(struct toysurface *base)
933{
934 struct shm_surface *surface = to_shm_surface(base);
935
Pekka Paalanena4eda732012-11-19 17:16:02 +0200936 shm_surface_leaf_release(&surface->leaf[0]);
937 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200938
939 free(surface);
940}
941
942static struct toysurface *
943shm_surface_create(struct display *display, struct wl_surface *wl_surface,
944 uint32_t flags, struct rectangle *rectangle)
945{
946 struct shm_surface *surface;
947
948 surface = calloc(1, sizeof *surface);
949 if (!surface)
950 return NULL;
951
952 surface->base.prepare = shm_surface_prepare;
953 surface->base.swap = shm_surface_swap;
954 surface->base.acquire = shm_surface_acquire;
955 surface->base.release = shm_surface_release;
956 surface->base.destroy = shm_surface_destroy;
957
958 surface->display = display;
959 surface->surface = wl_surface;
960 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200961
962 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400963}
964
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200965/*
966 * The following correspondences between file names and cursors was copied
967 * from: https://bugs.kde.org/attachment.cgi?id=67313
968 */
969
970static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300971 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200972 "sw-resize"
973};
974
975static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300976 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200977 "se-resize"
978};
979
980static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300981 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200982 "s-resize"
983};
984
985static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300986 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200987 "closedhand",
988 "208530c400c041818281048008011002"
989};
990
991static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300992 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200993 "default",
994 "top_left_arrow",
995 "left-arrow"
996};
997
998static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300999 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001000 "w-resize"
1001};
1002
1003static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001004 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001005 "e-resize"
1006};
1007
1008static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001009 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001010 "nw-resize"
1011};
1012
1013static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001014 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001015 "ne-resize"
1016};
1017
1018static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001019 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001020 "n-resize"
1021};
1022
1023static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001024 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001025 "ibeam",
1026 "text"
1027};
1028
1029static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001030 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001031 "pointer",
1032 "pointing_hand",
1033 "e29285e634086352946a0e7090d73106"
1034};
1035
1036static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001037 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001038 "wait",
1039 "0426c94ea35c87780ff01dc239897213"
1040};
1041
1042struct cursor_alternatives {
1043 const char **names;
1044 size_t count;
1045};
1046
1047static const struct cursor_alternatives cursors[] = {
1048 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1049 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1050 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1051 {grabbings, ARRAY_LENGTH(grabbings)},
1052 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1053 {left_sides, ARRAY_LENGTH(left_sides)},
1054 {right_sides, ARRAY_LENGTH(right_sides)},
1055 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1056 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1057 {top_sides, ARRAY_LENGTH(top_sides)},
1058 {xterms, ARRAY_LENGTH(xterms)},
1059 {hand1s, ARRAY_LENGTH(hand1s)},
1060 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001061};
1062
1063static void
1064create_cursors(struct display *display)
1065{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001066 char *config_file;
1067 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001068 unsigned int i, j;
1069 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001070 struct config_key shell_keys[] = {
1071 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1072 };
1073 struct config_section cs[] = {
1074 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1075 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001076
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001077 config_file = config_file_path("weston.ini");
1078 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1079 free(config_file);
1080
1081 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001082 display->cursors =
1083 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1084
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001085 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001086 cursor = NULL;
1087 for (j = 0; !cursor && j < cursors[i].count; ++j)
1088 cursor = wl_cursor_theme_get_cursor(
1089 display->cursor_theme, cursors[i].names[j]);
1090
1091 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001092 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001093 cursors[i].names[0]);
1094
1095 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001096 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001097}
1098
1099static void
1100destroy_cursors(struct display *display)
1101{
1102 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001103 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001104}
1105
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001106struct wl_cursor_image *
1107display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001108{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001109 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001110
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001111 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001112}
1113
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001114static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001115window_get_resize_dx_dy(struct window *window, int *x, int *y)
1116{
1117 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1118 *x = window->server_allocation.width - window->allocation.width;
1119 else
1120 *x = 0;
1121
1122 if (window->resize_edges & WINDOW_RESIZING_TOP)
1123 *y = window->server_allocation.height -
1124 window->allocation.height;
1125 else
1126 *y = 0;
1127
1128 window->resize_edges = 0;
1129}
1130
1131static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001132window_attach_surface(struct window *window)
1133{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001134 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001135
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001136 if (window->type == TYPE_NONE) {
1137 window->type = TYPE_TOPLEVEL;
1138 if (display->shell)
1139 wl_shell_surface_set_toplevel(window->shell_surface);
1140 }
1141
Pekka Paalanen512dde82012-10-10 12:49:27 +03001142 if (window->opaque_region) {
1143 wl_surface_set_opaque_region(window->surface,
1144 window->opaque_region);
1145 wl_region_destroy(window->opaque_region);
1146 window->opaque_region = NULL;
1147 }
1148
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001149 if (window->input_region) {
1150 wl_surface_set_input_region(window->surface,
1151 window->input_region);
1152 wl_region_destroy(window->input_region);
1153 window->input_region = NULL;
1154 }
1155
Pekka Paalanen99436862012-11-19 17:15:59 +02001156 window->toysurface->swap(window->toysurface,
1157 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001158}
1159
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001160int
1161window_has_focus(struct window *window)
1162{
1163 return window->focus_count > 0;
1164}
1165
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001166static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001167window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001168{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001169 if (!window->cairo_surface)
1170 return;
1171
1172 window_attach_surface(window);
1173 cairo_surface_destroy(window->cairo_surface);
1174 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001175}
1176
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001177struct display *
1178window_get_display(struct window *window)
1179{
1180 return window->display;
1181}
1182
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001183static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001184window_create_surface(struct window *window)
1185{
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001186 struct rectangle allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001187 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001188 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001189
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001190 if (!window->transparent)
1191 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001192
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001193 switch (window->buffer_transform) {
1194 case WL_OUTPUT_TRANSFORM_90:
1195 case WL_OUTPUT_TRANSFORM_270:
1196 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1197 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1198 allocation.width = window->allocation.height;
1199 allocation.height = window->allocation.width;
1200 break;
1201 default:
1202 break;
1203 }
1204
Pekka Paalanenb3627362012-11-19 17:16:00 +02001205 if (!window->toysurface &&
1206 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1207 window->display->dpy) {
1208 window->toysurface =
1209 egl_window_surface_create(window->display,
1210 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001211 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001212 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001213
Pekka Paalanenb3627362012-11-19 17:16:00 +02001214 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001215 window->toysurface = shm_surface_create(window->display,
1216 window->surface, flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001217 &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001218
Pekka Paalanen99436862012-11-19 17:15:59 +02001219 window_get_resize_dx_dy(window, &dx, &dy);
1220 window->cairo_surface =
1221 window->toysurface->prepare(window->toysurface, dx, dy,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001222 allocation.width,
1223 allocation.height,
Pekka Paalanen99436862012-11-19 17:15:59 +02001224 window->resizing);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001225}
1226
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001227int
1228window_get_buffer_transform(struct window *window)
1229{
1230 return window->buffer_transform;
1231}
1232
1233void
1234window_set_buffer_transform(struct window *window,
1235 enum wl_output_transform transform)
1236{
1237 window->buffer_transform = transform;
1238 wl_surface_set_buffer_transform(window->surface, transform);
1239}
1240
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001241static void frame_destroy(struct frame *frame);
1242
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001243void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001244window_destroy(struct window *window)
1245{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001246 struct display *display = window->display;
1247 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001248 struct window_output *window_output;
1249 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001250
1251 if (window->redraw_scheduled)
1252 wl_list_remove(&window->redraw_task.link);
1253
1254 wl_list_for_each(input, &display->input_list, link) {
1255 if (input->pointer_focus == window)
1256 input->pointer_focus = NULL;
1257 if (input->keyboard_focus == window)
1258 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001259 if (input->focus_widget &&
1260 input->focus_widget->window == window)
1261 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001262 }
1263
Rob Bradford7507b572012-05-15 17:55:34 +01001264 wl_list_for_each_safe(window_output, window_output_tmp,
1265 &window->window_output_list, link) {
1266 free (window_output);
1267 }
1268
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001269 if (window->input_region)
1270 wl_region_destroy(window->input_region);
1271 if (window->opaque_region)
1272 wl_region_destroy(window->opaque_region);
1273
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001274 if (window->frame)
1275 frame_destroy(window->frame);
1276
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001277 if (window->shell_surface)
1278 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001279 wl_surface_destroy(window->surface);
1280 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001281
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001282 if (window->toysurface)
1283 window->toysurface->destroy(window->toysurface);
1284
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001285 if (window->frame_cb)
1286 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001287 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001288 free(window);
1289}
1290
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001291static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001292widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001293{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001294 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001295
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001296 wl_list_for_each(child, &widget->child_list, link) {
1297 target = widget_find_widget(child, x, y);
1298 if (target)
1299 return target;
1300 }
1301
1302 if (widget->allocation.x <= x &&
1303 x < widget->allocation.x + widget->allocation.width &&
1304 widget->allocation.y <= y &&
1305 y < widget->allocation.y + widget->allocation.height) {
1306 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001307 }
1308
1309 return NULL;
1310}
1311
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001312static struct widget *
1313widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001314{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001315 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001316
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001317 widget = malloc(sizeof *widget);
1318 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001319 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001320 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001321 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001322 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001323 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001324 widget->tooltip = NULL;
1325 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001326
1327 return widget;
1328}
1329
1330struct widget *
1331window_add_widget(struct window *window, void *data)
1332{
1333 window->widget = widget_create(window, data);
1334 wl_list_init(&window->widget->link);
1335
1336 return window->widget;
1337}
1338
1339struct widget *
1340widget_add_widget(struct widget *parent, void *data)
1341{
1342 struct widget *widget;
1343
1344 widget = widget_create(parent->window, data);
1345 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001346
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001347 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001348}
1349
1350void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001351widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001352{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001353 struct display *display = widget->window->display;
1354 struct input *input;
1355
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001356 if (widget->tooltip) {
1357 free(widget->tooltip);
1358 widget->tooltip = NULL;
1359 }
1360
Pekka Paalanene156fb62012-01-19 13:51:38 +02001361 wl_list_for_each(input, &display->input_list, link) {
1362 if (input->focus_widget == widget)
1363 input->focus_widget = NULL;
1364 }
1365
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001366 wl_list_remove(&widget->link);
1367 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001368}
1369
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001370void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001371widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001372{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001373 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001374}
1375
1376void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001377widget_set_size(struct widget *widget, int32_t width, int32_t height)
1378{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001379 widget->allocation.width = width;
1380 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001381}
1382
1383void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001384widget_set_allocation(struct widget *widget,
1385 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001386{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001387 widget->allocation.x = x;
1388 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001389 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001390}
1391
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001392void
1393widget_set_transparent(struct widget *widget, int transparent)
1394{
1395 widget->opaque = !transparent;
1396}
1397
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001398void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001399widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001400{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001401 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001402}
1403
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001404void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001405widget_set_resize_handler(struct widget *widget,
1406 widget_resize_handler_t handler)
1407{
1408 widget->resize_handler = handler;
1409}
1410
1411void
1412widget_set_redraw_handler(struct widget *widget,
1413 widget_redraw_handler_t handler)
1414{
1415 widget->redraw_handler = handler;
1416}
1417
1418void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001419widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001420{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001421 widget->enter_handler = handler;
1422}
1423
1424void
1425widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1426{
1427 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001428}
1429
1430void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001431widget_set_motion_handler(struct widget *widget,
1432 widget_motion_handler_t handler)
1433{
1434 widget->motion_handler = handler;
1435}
1436
1437void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001438widget_set_button_handler(struct widget *widget,
1439 widget_button_handler_t handler)
1440{
1441 widget->button_handler = handler;
1442}
1443
1444void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001445widget_set_axis_handler(struct widget *widget,
1446 widget_axis_handler_t handler)
1447{
1448 widget->axis_handler = handler;
1449}
1450
1451void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001452widget_schedule_redraw(struct widget *widget)
1453{
1454 window_schedule_redraw(widget->window);
1455}
1456
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001457cairo_surface_t *
1458window_get_surface(struct window *window)
1459{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001460 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001461}
1462
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001463struct wl_surface *
1464window_get_wl_surface(struct window *window)
1465{
1466 return window->surface;
1467}
1468
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001469struct wl_shell_surface *
1470window_get_wl_shell_surface(struct window *window)
1471{
1472 return window->shell_surface;
1473}
1474
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001475static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001476tooltip_redraw_handler(struct widget *widget, void *data)
1477{
1478 cairo_t *cr;
1479 const int32_t r = 3;
1480 struct tooltip *tooltip = data;
1481 int32_t width, height;
1482 struct window *window = widget->window;
1483
1484 cr = cairo_create(window->cairo_surface);
1485 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1486 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1487 cairo_paint(cr);
1488
1489 width = window->allocation.width;
1490 height = window->allocation.height;
1491 rounded_rect(cr, 0, 0, width, height, r);
1492
1493 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1494 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1495 cairo_fill(cr);
1496
1497 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1498 cairo_move_to(cr, 10, 16);
1499 cairo_show_text(cr, tooltip->entry);
1500 cairo_destroy(cr);
1501}
1502
1503static cairo_text_extents_t
1504get_text_extents(struct tooltip *tooltip)
1505{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001506 cairo_t *cr;
1507 cairo_text_extents_t extents;
1508
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001509 /* Use the dummy_surface because tooltip's surface was not
1510 * created yet, and parent does not have a valid surface
1511 * outside repaint, either.
1512 */
1513 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001514 cairo_text_extents(cr, tooltip->entry, &extents);
1515 cairo_destroy(cr);
1516
1517 return extents;
1518}
1519
1520static int
1521window_create_tooltip(struct tooltip *tooltip)
1522{
1523 struct widget *parent = tooltip->parent;
1524 struct display *display = parent->window->display;
1525 struct window *window;
1526 const int offset_y = 27;
1527 const int margin = 3;
1528 cairo_text_extents_t extents;
1529
1530 if (tooltip->widget)
1531 return 0;
1532
1533 window = window_create_transient(display, parent->window, tooltip->x,
1534 tooltip->y + offset_y,
1535 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1536 if (!window)
1537 return -1;
1538
1539 tooltip->window = window;
1540 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1541
1542 extents = get_text_extents(tooltip);
1543 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1544 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1545
1546 return 0;
1547}
1548
1549void
1550widget_destroy_tooltip(struct widget *parent)
1551{
1552 struct tooltip *tooltip = parent->tooltip;
1553
1554 parent->tooltip_count = 0;
1555 if (!tooltip)
1556 return;
1557
1558 if (tooltip->widget) {
1559 widget_destroy(tooltip->widget);
1560 window_destroy(tooltip->window);
1561 tooltip->widget = NULL;
1562 tooltip->window = NULL;
1563 }
1564
1565 close(tooltip->tooltip_fd);
1566 free(tooltip->entry);
1567 free(tooltip);
1568 parent->tooltip = NULL;
1569}
1570
1571static void
1572tooltip_func(struct task *task, uint32_t events)
1573{
1574 struct tooltip *tooltip =
1575 container_of(task, struct tooltip, tooltip_task);
1576 uint64_t exp;
1577
Martin Olsson8df662a2012-07-08 03:03:47 +02001578 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1579 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001580 window_create_tooltip(tooltip);
1581}
1582
1583#define TOOLTIP_TIMEOUT 500
1584static int
1585tooltip_timer_reset(struct tooltip *tooltip)
1586{
1587 struct itimerspec its;
1588
1589 its.it_interval.tv_sec = 0;
1590 its.it_interval.tv_nsec = 0;
1591 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1592 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1593 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1594 fprintf(stderr, "could not set timerfd\n: %m");
1595 return -1;
1596 }
1597
1598 return 0;
1599}
1600
1601int
1602widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1603{
1604 struct tooltip *tooltip = parent->tooltip;
1605
1606 parent->tooltip_count++;
1607 if (tooltip) {
1608 tooltip->x = x;
1609 tooltip->y = y;
1610 tooltip_timer_reset(tooltip);
1611 return 0;
1612 }
1613
1614 /* the handler might be triggered too fast via input device motion, so
1615 * we need this check here to make sure tooltip is fully initialized */
1616 if (parent->tooltip_count > 1)
1617 return 0;
1618
1619 tooltip = malloc(sizeof *tooltip);
1620 if (!tooltip)
1621 return -1;
1622
1623 parent->tooltip = tooltip;
1624 tooltip->parent = parent;
1625 tooltip->widget = NULL;
1626 tooltip->window = NULL;
1627 tooltip->x = x;
1628 tooltip->y = y;
1629 tooltip->entry = strdup(entry);
1630 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1631 if (tooltip->tooltip_fd < 0) {
1632 fprintf(stderr, "could not create timerfd\n: %m");
1633 return -1;
1634 }
1635
1636 tooltip->tooltip_task.run = tooltip_func;
1637 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1638 EPOLLIN, &tooltip->tooltip_task);
1639 tooltip_timer_reset(tooltip);
1640
1641 return 0;
1642}
1643
1644static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001645workspace_manager_state(void *data,
1646 struct workspace_manager *workspace_manager,
1647 uint32_t current,
1648 uint32_t count)
1649{
1650 struct display *display = data;
1651
1652 display->workspace = current;
1653 display->workspace_count = count;
1654}
1655
1656static const struct workspace_manager_listener workspace_manager_listener = {
1657 workspace_manager_state
1658};
1659
1660static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001661frame_resize_handler(struct widget *widget,
1662 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001663{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001664 struct frame *frame = data;
1665 struct widget *child = frame->child;
1666 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001667 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001668 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001669 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001670 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001671 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001672 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001673
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001674 switch (widget->window->type) {
1675 case TYPE_FULLSCREEN:
1676 decoration_width = 0;
1677 decoration_height = 0;
1678
1679 allocation.x = 0;
1680 allocation.y = 0;
1681 allocation.width = width;
1682 allocation.height = height;
1683 opaque_margin = 0;
1684
1685 wl_list_for_each(button, &frame->buttons_list, link)
1686 button->widget->opaque = 1;
1687 break;
1688 case TYPE_MAXIMIZED:
1689 decoration_width = t->width * 2;
1690 decoration_height = t->width + t->titlebar_height;
1691
1692 allocation.x = t->width;
1693 allocation.y = t->titlebar_height;
1694 allocation.width = width - decoration_width;
1695 allocation.height = height - decoration_height;
1696
1697 opaque_margin = 0;
1698
1699 wl_list_for_each(button, &frame->buttons_list, link)
1700 button->widget->opaque = 0;
1701 break;
1702 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001703 decoration_width = (t->width + t->margin) * 2;
1704 decoration_height = t->width +
1705 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001706
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001707 allocation.x = t->width + t->margin;
1708 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001709 allocation.width = width - decoration_width;
1710 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001711
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001712 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001713
1714 wl_list_for_each(button, &frame->buttons_list, link)
1715 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001716 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001717 }
1718
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001719 widget_set_allocation(child, allocation.x, allocation.y,
1720 allocation.width, allocation.height);
1721
1722 if (child->resize_handler)
1723 child->resize_handler(child,
1724 allocation.width,
1725 allocation.height,
1726 child->user_data);
1727
Scott Moreauf7e498c2012-05-14 11:39:29 -06001728 width = child->allocation.width + decoration_width;
1729 height = child->allocation.height + decoration_height;
1730
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001731 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1732
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001733 widget->window->input_region =
1734 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001735 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001736 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001737 shadow_margin, shadow_margin,
1738 width - 2 * shadow_margin,
1739 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001740 } else {
1741 wl_region_add(widget->window->input_region,
1742 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001743 }
1744
Scott Moreauf7e498c2012-05-14 11:39:29 -06001745 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001746
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001747 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001748 wl_region_add(widget->window->opaque_region,
1749 opaque_margin, opaque_margin,
1750 widget->allocation.width - 2 * opaque_margin,
1751 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001752
1753 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001754 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1755 x_l = t->width + shadow_margin;
1756 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001757 wl_list_for_each(button, &frame->buttons_list, link) {
1758 const int button_padding = 4;
1759 w = cairo_image_surface_get_width(button->icon);
1760 h = cairo_image_surface_get_height(button->icon);
1761
1762 if (button->decoration == FRAME_BUTTON_FANCY)
1763 w += 10;
1764
1765 if (button->align == FRAME_BUTTON_LEFT) {
1766 widget_set_allocation(button->widget,
1767 x_l, y , w + 1, h + 1);
1768 x_l += w;
1769 x_l += button_padding;
1770 } else {
1771 x_r -= w;
1772 widget_set_allocation(button->widget,
1773 x_r, y , w + 1, h + 1);
1774 x_r -= button_padding;
1775 }
1776 }
1777}
1778
1779static int
1780frame_button_enter_handler(struct widget *widget,
1781 struct input *input, float x, float y, void *data)
1782{
1783 struct frame_button *frame_button = data;
1784
1785 widget_schedule_redraw(frame_button->widget);
1786 frame_button->state = FRAME_BUTTON_OVER;
1787
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001788 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001789}
1790
1791static void
1792frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1793{
1794 struct frame_button *frame_button = data;
1795
1796 widget_schedule_redraw(frame_button->widget);
1797 frame_button->state = FRAME_BUTTON_DEFAULT;
1798}
1799
1800static void
1801frame_button_button_handler(struct widget *widget,
1802 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001803 uint32_t button,
1804 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001805{
1806 struct frame_button *frame_button = data;
1807 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001808 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001809
1810 if (button != BTN_LEFT)
1811 return;
1812
1813 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001814 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001815 frame_button->state = FRAME_BUTTON_ACTIVE;
1816 widget_schedule_redraw(frame_button->widget);
1817
1818 if (frame_button->type == FRAME_BUTTON_ICON)
1819 window_show_frame_menu(window, input, time);
1820 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001821 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001822 frame_button->state = FRAME_BUTTON_DEFAULT;
1823 widget_schedule_redraw(frame_button->widget);
1824 break;
1825 }
1826
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001827 if (!was_pressed)
1828 return;
1829
Martin Minarik1998b152012-05-10 02:04:35 +02001830 switch (frame_button->type) {
1831 case FRAME_BUTTON_CLOSE:
1832 if (window->close_handler)
1833 window->close_handler(window->parent,
1834 window->user_data);
1835 else
1836 display_exit(window->display);
1837 break;
1838 case FRAME_BUTTON_MINIMIZE:
1839 fprintf(stderr,"Minimize stub\n");
1840 break;
1841 case FRAME_BUTTON_MAXIMIZE:
1842 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1843 break;
1844 default:
1845 /* Unknown operation */
1846 break;
1847 }
1848}
1849
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001850static int
1851frame_button_motion_handler(struct widget *widget,
1852 struct input *input, uint32_t time,
1853 float x, float y, void *data)
1854{
1855 struct frame_button *frame_button = data;
1856 enum frame_button_pointer previous_button_state = frame_button->state;
1857
1858 /* only track state for a pressed button */
1859 if (input->grab != widget)
1860 return CURSOR_LEFT_PTR;
1861
1862 if (x > widget->allocation.x &&
1863 x < (widget->allocation.x + widget->allocation.width) &&
1864 y > widget->allocation.y &&
1865 y < (widget->allocation.y + widget->allocation.height)) {
1866 frame_button->state = FRAME_BUTTON_ACTIVE;
1867 } else {
1868 frame_button->state = FRAME_BUTTON_DEFAULT;
1869 }
1870
1871 if (frame_button->state != previous_button_state)
1872 widget_schedule_redraw(frame_button->widget);
1873
1874 return CURSOR_LEFT_PTR;
1875}
1876
Martin Minarik1998b152012-05-10 02:04:35 +02001877static void
1878frame_button_redraw_handler(struct widget *widget, void *data)
1879{
1880 struct frame_button *frame_button = data;
1881 cairo_t *cr;
1882 int width, height, x, y;
1883 struct window *window = widget->window;
1884
1885 x = widget->allocation.x;
1886 y = widget->allocation.y;
1887 width = widget->allocation.width;
1888 height = widget->allocation.height;
1889
1890 if (!width)
1891 return;
1892 if (!height)
1893 return;
1894 if (widget->opaque)
1895 return;
1896
1897 cr = cairo_create(window->cairo_surface);
1898
1899 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1900 cairo_set_line_width(cr, 1);
1901
1902 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1903 cairo_rectangle (cr, x, y, 25, 16);
1904
1905 cairo_stroke_preserve(cr);
1906
1907 switch (frame_button->state) {
1908 case FRAME_BUTTON_DEFAULT:
1909 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1910 break;
1911 case FRAME_BUTTON_OVER:
1912 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1913 break;
1914 case FRAME_BUTTON_ACTIVE:
1915 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1916 break;
1917 }
1918
1919 cairo_fill (cr);
1920
1921 x += 4;
1922 }
1923
1924 cairo_set_source_surface(cr, frame_button->icon, x, y);
1925 cairo_paint(cr);
1926
1927 cairo_destroy(cr);
1928}
1929
1930static struct widget *
1931frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1932 enum frame_button_align align, enum frame_button_decoration style)
1933{
1934 struct frame_button *frame_button;
1935 const char *icon = data;
1936
1937 frame_button = malloc (sizeof *frame_button);
1938 memset(frame_button, 0, sizeof *frame_button);
1939
1940 frame_button->icon = cairo_image_surface_create_from_png(icon);
1941 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1942 frame_button->frame = frame;
1943 frame_button->type = type;
1944 frame_button->align = align;
1945 frame_button->decoration = style;
1946
1947 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1948
1949 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1950 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1951 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1952 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001953 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001954 return frame_button->widget;
1955}
1956
1957static void
1958frame_button_destroy(struct frame_button *frame_button)
1959{
1960 widget_destroy(frame_button->widget);
1961 wl_list_remove(&frame_button->link);
1962 cairo_surface_destroy(frame_button->icon);
1963 free(frame_button);
1964
1965 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001966}
1967
1968static void
1969frame_redraw_handler(struct widget *widget, void *data)
1970{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001971 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001972 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001973 struct theme *t = window->display->theme;
1974 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001975
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001976 if (window->type == TYPE_FULLSCREEN)
1977 return;
1978
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001979 cr = cairo_create(window->cairo_surface);
1980
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001981 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001982 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001983 if (window->type == TYPE_MAXIMIZED)
1984 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001985 theme_render_frame(t, cr, widget->allocation.width,
1986 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001987
1988 cairo_destroy(cr);
1989}
1990
1991static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001992frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001993{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001994 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001995 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001996 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001997
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001998 location = theme_get_location(t, input->sx, input->sy,
1999 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002000 frame->widget->allocation.height,
2001 window->type == TYPE_MAXIMIZED ?
2002 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002003
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002004 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002005 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002006 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002007 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002008 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002009 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002010 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002011 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002012 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002013 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002014 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002015 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002016 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002017 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002018 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002019 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002020 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002021 case THEME_LOCATION_EXTERIOR:
2022 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002023 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002024 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002025 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002026}
2027
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002028static void
2029frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002030{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002031 struct display *display;
2032
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002033 switch (index) {
2034 case 0: /* close */
2035 if (window->close_handler)
2036 window->close_handler(window->parent,
2037 window->user_data);
2038 else
2039 display_exit(window->display);
2040 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002041 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002042 display = window->display;
2043 if (display->workspace > 0)
2044 workspace_manager_move_surface(display->workspace_manager,
2045 window->surface,
2046 display->workspace - 1);
2047 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002048 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002049 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002050 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002051 workspace_manager_move_surface(display->workspace_manager,
2052 window->surface,
2053 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002054 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002055 case 3: /* fullscreen */
2056 /* we don't have a way to get out of fullscreen for now */
2057 if (window->fullscreen_handler)
2058 window->fullscreen_handler(window, window->user_data);
2059 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002060 }
2061}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002062
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002063void
2064window_show_frame_menu(struct window *window,
2065 struct input *input, uint32_t time)
2066{
2067 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002068 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002069
2070 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002071 "Close",
2072 "Move to workspace above", "Move to workspace below",
2073 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002074 };
2075
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002076 if (window->fullscreen_handler)
2077 count = ARRAY_LENGTH(entries);
2078 else
2079 count = ARRAY_LENGTH(entries) - 1;
2080
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002081 input_get_position(input, &x, &y);
2082 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002083 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002084}
2085
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002086static int
2087frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002088 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002089{
2090 return frame_get_pointer_image_for_location(data, input);
2091}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002092
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002093static int
2094frame_motion_handler(struct widget *widget,
2095 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002096 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002097{
2098 return frame_get_pointer_image_for_location(data, input);
2099}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002100
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002101static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002102frame_button_handler(struct widget *widget,
2103 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002104 uint32_t button, enum wl_pointer_button_state state,
2105 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002106
2107{
2108 struct frame *frame = data;
2109 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002110 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002111 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002112
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002113 location = theme_get_location(display->theme, input->sx, input->sy,
2114 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002115 frame->widget->allocation.height,
2116 window->type == TYPE_MAXIMIZED ?
2117 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002118
Daniel Stone4dbadb12012-05-30 16:31:51 +01002119 if (window->display->shell && button == BTN_LEFT &&
2120 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002121 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002122 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002123 if (!window->shell_surface)
2124 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002125 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002126 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002127 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002128 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002129 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002130 case THEME_LOCATION_RESIZING_TOP:
2131 case THEME_LOCATION_RESIZING_BOTTOM:
2132 case THEME_LOCATION_RESIZING_LEFT:
2133 case THEME_LOCATION_RESIZING_RIGHT:
2134 case THEME_LOCATION_RESIZING_TOP_LEFT:
2135 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2136 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2137 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002138 if (!window->shell_surface)
2139 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002140 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002141
Pekka Paalanen99436862012-11-19 17:15:59 +02002142 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002143 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002144 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002145 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002146 break;
2147 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002148 } else if (button == BTN_RIGHT &&
2149 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002150 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002151 }
2152}
2153
2154struct widget *
2155frame_create(struct window *window, void *data)
2156{
2157 struct frame *frame;
2158
2159 frame = malloc(sizeof *frame);
2160 memset(frame, 0, sizeof *frame);
2161
2162 frame->widget = window_add_widget(window, frame);
2163 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002164
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002165 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2166 widget_set_resize_handler(frame->widget, frame_resize_handler);
2167 widget_set_enter_handler(frame->widget, frame_enter_handler);
2168 widget_set_motion_handler(frame->widget, frame_motion_handler);
2169 widget_set_button_handler(frame->widget, frame_button_handler);
2170
Martin Minarik1998b152012-05-10 02:04:35 +02002171 /* Create empty list for frame buttons */
2172 wl_list_init(&frame->buttons_list);
2173
2174 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2175 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2176
2177 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2178 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2179
2180 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2181 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2182
2183 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2184 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2185
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002186 window->frame = frame;
2187
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002188 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002189}
2190
Kristian Høgsberga1627922012-06-20 17:30:03 -04002191void
2192frame_set_child_size(struct widget *widget, int child_width, int child_height)
2193{
2194 struct display *display = widget->window->display;
2195 struct theme *t = display->theme;
2196 int decoration_width, decoration_height;
2197 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002198 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002199
2200 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002201 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002202 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002203 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002204
2205 width = child_width + decoration_width;
2206 height = child_height + decoration_height;
2207 } else {
2208 width = child_width;
2209 height = child_height;
2210 }
2211
2212 window_schedule_resize(widget->window, width, height);
2213}
2214
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002215static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002216frame_destroy(struct frame *frame)
2217{
Martin Minarik1998b152012-05-10 02:04:35 +02002218 struct frame_button *button, *tmp;
2219
2220 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2221 frame_button_destroy(button);
2222
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002223 /* frame->child must be destroyed by the application */
2224 widget_destroy(frame->widget);
2225 free(frame);
2226}
2227
2228static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002229input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002230 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002231{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002232 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002233 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002234
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002235 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002236 return;
2237
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002238 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002239 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002240 widget = old;
2241 if (input->grab)
2242 widget = input->grab;
2243 if (widget->leave_handler)
2244 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002245 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002246 }
2247
2248 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002249 widget = focus;
2250 if (input->grab)
2251 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002252 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002253 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002254 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002255 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002256
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002257 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002258 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002259}
2260
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002261void
2262input_grab(struct input *input, struct widget *widget, uint32_t button)
2263{
2264 input->grab = widget;
2265 input->grab_button = button;
2266}
2267
2268void
2269input_ungrab(struct input *input)
2270{
2271 struct widget *widget;
2272
2273 input->grab = NULL;
2274 if (input->pointer_focus) {
2275 widget = widget_find_widget(input->pointer_focus->widget,
2276 input->sx, input->sy);
2277 input_set_focus_widget(input, widget, input->sx, input->sy);
2278 }
2279}
2280
2281static void
2282input_remove_pointer_focus(struct input *input)
2283{
2284 struct window *window = input->pointer_focus;
2285
2286 if (!window)
2287 return;
2288
2289 input_set_focus_widget(input, NULL, 0, 0);
2290
2291 input->pointer_focus = NULL;
2292 input->current_cursor = CURSOR_UNSET;
2293}
2294
2295static void
2296pointer_handle_enter(void *data, struct wl_pointer *pointer,
2297 uint32_t serial, struct wl_surface *surface,
2298 wl_fixed_t sx_w, wl_fixed_t sy_w)
2299{
2300 struct input *input = data;
2301 struct window *window;
2302 struct widget *widget;
2303 float sx = wl_fixed_to_double(sx_w);
2304 float sy = wl_fixed_to_double(sy_w);
2305
2306 if (!surface) {
2307 /* enter event for a window we've just destroyed */
2308 return;
2309 }
2310
2311 input->display->serial = serial;
2312 input->pointer_enter_serial = serial;
2313 input->pointer_focus = wl_surface_get_user_data(surface);
2314 window = input->pointer_focus;
2315
Pekka Paalanen99436862012-11-19 17:15:59 +02002316 if (window->resizing) {
2317 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002318 /* Schedule a redraw to free the pool */
2319 window_schedule_redraw(window);
2320 }
2321
2322 input->sx = sx;
2323 input->sy = sy;
2324
2325 widget = widget_find_widget(window->widget, sx, sy);
2326 input_set_focus_widget(input, widget, sx, sy);
2327}
2328
2329static void
2330pointer_handle_leave(void *data, struct wl_pointer *pointer,
2331 uint32_t serial, struct wl_surface *surface)
2332{
2333 struct input *input = data;
2334
2335 input->display->serial = serial;
2336 input_remove_pointer_focus(input);
2337}
2338
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002339static void
Daniel Stone37816df2012-05-16 18:45:18 +01002340pointer_handle_motion(void *data, struct wl_pointer *pointer,
2341 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002342{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002343 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002344 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002345 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002346 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002347 float sx = wl_fixed_to_double(sx_w);
2348 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002349
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002350 input->sx = sx;
2351 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002352
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002353 if (!window)
2354 return;
2355
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002356 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002357 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002358 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002359 }
2360
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002361 if (input->grab)
2362 widget = input->grab;
2363 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002364 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002365 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002366 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002367 input, time, sx, sy,
2368 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002369
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002370 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002371}
2372
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002373static void
Daniel Stone37816df2012-05-16 18:45:18 +01002374pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002375 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002376{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002377 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002378 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002379 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002380
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002381 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002382 if (input->focus_widget && input->grab == NULL &&
2383 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002384 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002385
Neil Roberts6b28aad2012-01-23 19:11:18 +00002386 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002387 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002388 (*widget->button_handler)(widget,
2389 input, time,
2390 button, state,
2391 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002392
Daniel Stone4dbadb12012-05-30 16:31:51 +01002393 if (input->grab && input->grab_button == button &&
2394 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002395 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002396}
2397
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002398static void
Daniel Stone37816df2012-05-16 18:45:18 +01002399pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002400 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002401{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002402 struct input *input = data;
2403 struct widget *widget;
2404
2405 widget = input->focus_widget;
2406 if (input->grab)
2407 widget = input->grab;
2408 if (widget && widget->axis_handler)
2409 (*widget->axis_handler)(widget,
2410 input, time,
2411 axis, value,
2412 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002413}
2414
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002415static const struct wl_pointer_listener pointer_listener = {
2416 pointer_handle_enter,
2417 pointer_handle_leave,
2418 pointer_handle_motion,
2419 pointer_handle_button,
2420 pointer_handle_axis,
2421};
2422
2423static void
2424input_remove_keyboard_focus(struct input *input)
2425{
2426 struct window *window = input->keyboard_focus;
2427 struct itimerspec its;
2428
2429 its.it_interval.tv_sec = 0;
2430 its.it_interval.tv_nsec = 0;
2431 its.it_value.tv_sec = 0;
2432 its.it_value.tv_nsec = 0;
2433 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2434
2435 if (!window)
2436 return;
2437
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002438 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002439 if (window->keyboard_focus_handler)
2440 (*window->keyboard_focus_handler)(window, NULL,
2441 window->user_data);
2442
2443 input->keyboard_focus = NULL;
2444}
2445
2446static void
2447keyboard_repeat_func(struct task *task, uint32_t events)
2448{
2449 struct input *input =
2450 container_of(task, struct input, repeat_task);
2451 struct window *window = input->keyboard_focus;
2452 uint64_t exp;
2453
2454 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2455 /* If we change the timer between the fd becoming
2456 * readable and getting here, there'll be nothing to
2457 * read and we get EAGAIN. */
2458 return;
2459
2460 if (window && window->key_handler) {
2461 (*window->key_handler)(window, input, input->repeat_time,
2462 input->repeat_key, input->repeat_sym,
2463 WL_KEYBOARD_KEY_STATE_PRESSED,
2464 window->user_data);
2465 }
2466}
2467
2468static void
2469keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2470 uint32_t format, int fd, uint32_t size)
2471{
2472 struct input *input = data;
2473 char *map_str;
2474
2475 if (!data) {
2476 close(fd);
2477 return;
2478 }
2479
2480 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2481 close(fd);
2482 return;
2483 }
2484
2485 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2486 if (map_str == MAP_FAILED) {
2487 close(fd);
2488 return;
2489 }
2490
2491 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2492 map_str,
2493 XKB_KEYMAP_FORMAT_TEXT_V1,
2494 0);
2495 munmap(map_str, size);
2496 close(fd);
2497
2498 if (!input->xkb.keymap) {
2499 fprintf(stderr, "failed to compile keymap\n");
2500 return;
2501 }
2502
2503 input->xkb.state = xkb_state_new(input->xkb.keymap);
2504 if (!input->xkb.state) {
2505 fprintf(stderr, "failed to create XKB state\n");
2506 xkb_map_unref(input->xkb.keymap);
2507 input->xkb.keymap = NULL;
2508 return;
2509 }
2510
2511 input->xkb.control_mask =
2512 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2513 input->xkb.alt_mask =
2514 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2515 input->xkb.shift_mask =
2516 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2517}
2518
2519static void
2520keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2521 uint32_t serial, struct wl_surface *surface,
2522 struct wl_array *keys)
2523{
2524 struct input *input = data;
2525 struct window *window;
2526
2527 input->display->serial = serial;
2528 input->keyboard_focus = wl_surface_get_user_data(surface);
2529
2530 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002531 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002532 if (window->keyboard_focus_handler)
2533 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002534 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002535}
2536
2537static void
2538keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2539 uint32_t serial, struct wl_surface *surface)
2540{
2541 struct input *input = data;
2542
2543 input->display->serial = serial;
2544 input_remove_keyboard_focus(input);
2545}
2546
Scott Moreau210d0792012-03-22 10:47:01 -06002547static void
Daniel Stone37816df2012-05-16 18:45:18 +01002548keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002549 uint32_t serial, uint32_t time, uint32_t key,
2550 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002551{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002552 struct input *input = data;
2553 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002554 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002555 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002556 const xkb_keysym_t *syms;
2557 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002558 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002559
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002560 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002561 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002562 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002563 return;
2564
Daniel Stone97f68542012-05-30 16:32:01 +01002565 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002566
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002567 sym = XKB_KEY_NoSymbol;
2568 if (num_syms == 1)
2569 sym = syms[0];
2570
2571 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002572 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002573 window_set_maximized(window,
2574 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002575 } else if (sym == XKB_KEY_F11 &&
2576 window->fullscreen_handler &&
2577 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2578 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002579 } else if (sym == XKB_KEY_F4 &&
2580 input->modifiers == MOD_ALT_MASK &&
2581 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2582 if (window->close_handler)
2583 window->close_handler(window->parent,
2584 window->user_data);
2585 else
2586 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002587 } else if (window->key_handler) {
2588 (*window->key_handler)(window, input, time, key,
2589 sym, state, window->user_data);
2590 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002591
2592 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2593 key == input->repeat_key) {
2594 its.it_interval.tv_sec = 0;
2595 its.it_interval.tv_nsec = 0;
2596 its.it_value.tv_sec = 0;
2597 its.it_value.tv_nsec = 0;
2598 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2599 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2600 input->repeat_sym = sym;
2601 input->repeat_key = key;
2602 input->repeat_time = time;
2603 its.it_interval.tv_sec = 0;
2604 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2605 its.it_value.tv_sec = 0;
2606 its.it_value.tv_nsec = 400 * 1000 * 1000;
2607 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2608 }
2609}
2610
2611static void
Daniel Stone351eb612012-05-31 15:27:47 -04002612keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2613 uint32_t serial, uint32_t mods_depressed,
2614 uint32_t mods_latched, uint32_t mods_locked,
2615 uint32_t group)
2616{
2617 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002618 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002619
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002620 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2621 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002622 mask = xkb_state_serialize_mods(input->xkb.state,
2623 XKB_STATE_DEPRESSED |
2624 XKB_STATE_LATCHED);
2625 input->modifiers = 0;
2626 if (mask & input->xkb.control_mask)
2627 input->modifiers |= MOD_CONTROL_MASK;
2628 if (mask & input->xkb.alt_mask)
2629 input->modifiers |= MOD_ALT_MASK;
2630 if (mask & input->xkb.shift_mask)
2631 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002632}
2633
Daniel Stone37816df2012-05-16 18:45:18 +01002634static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002635 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002636 keyboard_handle_enter,
2637 keyboard_handle_leave,
2638 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002639 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002640};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002641
2642static void
Daniel Stone37816df2012-05-16 18:45:18 +01002643seat_handle_capabilities(void *data, struct wl_seat *seat,
2644 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002645{
Daniel Stone37816df2012-05-16 18:45:18 +01002646 struct input *input = data;
2647
2648 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2649 input->pointer = wl_seat_get_pointer(seat);
2650 wl_pointer_set_user_data(input->pointer, input);
2651 wl_pointer_add_listener(input->pointer, &pointer_listener,
2652 input);
2653 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2654 wl_pointer_destroy(input->pointer);
2655 input->pointer = NULL;
2656 }
2657
2658 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2659 input->keyboard = wl_seat_get_keyboard(seat);
2660 wl_keyboard_set_user_data(input->keyboard, input);
2661 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2662 input);
2663 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2664 wl_keyboard_destroy(input->keyboard);
2665 input->keyboard = NULL;
2666 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002667}
2668
Daniel Stone37816df2012-05-16 18:45:18 +01002669static const struct wl_seat_listener seat_listener = {
2670 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002671};
2672
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002673void
2674input_get_position(struct input *input, int32_t *x, int32_t *y)
2675{
2676 *x = input->sx;
2677 *y = input->sy;
2678}
2679
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002680struct display *
2681input_get_display(struct input *input)
2682{
2683 return input->display;
2684}
2685
Daniel Stone37816df2012-05-16 18:45:18 +01002686struct wl_seat *
2687input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002688{
Daniel Stone37816df2012-05-16 18:45:18 +01002689 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002690}
2691
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002692uint32_t
2693input_get_modifiers(struct input *input)
2694{
2695 return input->modifiers;
2696}
2697
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002698struct widget *
2699input_get_focus_widget(struct input *input)
2700{
2701 return input->focus_widget;
2702}
2703
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002704struct data_offer {
2705 struct wl_data_offer *offer;
2706 struct input *input;
2707 struct wl_array types;
2708 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002709
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002710 struct task io_task;
2711 int fd;
2712 data_func_t func;
2713 int32_t x, y;
2714 void *user_data;
2715};
2716
2717static void
2718data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2719{
2720 struct data_offer *offer = data;
2721 char **p;
2722
2723 p = wl_array_add(&offer->types, sizeof *p);
2724 *p = strdup(type);
2725}
2726
2727static const struct wl_data_offer_listener data_offer_listener = {
2728 data_offer_offer,
2729};
2730
2731static void
2732data_offer_destroy(struct data_offer *offer)
2733{
2734 char **p;
2735
2736 offer->refcount--;
2737 if (offer->refcount == 0) {
2738 wl_data_offer_destroy(offer->offer);
2739 for (p = offer->types.data; *p; p++)
2740 free(*p);
2741 wl_array_release(&offer->types);
2742 free(offer);
2743 }
2744}
2745
2746static void
2747data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002748 struct wl_data_device *data_device,
2749 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002750{
2751 struct data_offer *offer;
2752
2753 offer = malloc(sizeof *offer);
2754
2755 wl_array_init(&offer->types);
2756 offer->refcount = 1;
2757 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002758 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002759 wl_data_offer_add_listener(offer->offer,
2760 &data_offer_listener, offer);
2761}
2762
2763static void
2764data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002765 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002766 wl_fixed_t x_w, wl_fixed_t y_w,
2767 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002768{
2769 struct input *input = data;
2770 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002771 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002772 float x = wl_fixed_to_double(x_w);
2773 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002774 char **p;
2775
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002776 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002777 window = wl_surface_get_user_data(surface);
2778 input->pointer_focus = window;
2779
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002780 if (offer) {
2781 input->drag_offer = wl_data_offer_get_user_data(offer);
2782
2783 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2784 *p = NULL;
2785
2786 types_data = input->drag_offer->types.data;
2787 } else {
2788 input->drag_offer = NULL;
2789 types_data = NULL;
2790 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002791
2792 window = input->pointer_focus;
2793 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002794 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002795 window->user_data);
2796}
2797
2798static void
2799data_device_leave(void *data, struct wl_data_device *data_device)
2800{
2801 struct input *input = data;
2802
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002803 if (input->drag_offer) {
2804 data_offer_destroy(input->drag_offer);
2805 input->drag_offer = NULL;
2806 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002807}
2808
2809static void
2810data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002811 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002812{
2813 struct input *input = data;
2814 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002815 float x = wl_fixed_to_double(x_w);
2816 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002817 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002818
2819 input->sx = x;
2820 input->sy = y;
2821
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002822 if (input->drag_offer)
2823 types_data = input->drag_offer->types.data;
2824 else
2825 types_data = NULL;
2826
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002827 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002828 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002829 window->user_data);
2830}
2831
2832static void
2833data_device_drop(void *data, struct wl_data_device *data_device)
2834{
2835 struct input *input = data;
2836 struct window *window = input->pointer_focus;
2837
2838 if (window->drop_handler)
2839 window->drop_handler(window, input,
2840 input->sx, input->sy, window->user_data);
2841}
2842
2843static void
2844data_device_selection(void *data,
2845 struct wl_data_device *wl_data_device,
2846 struct wl_data_offer *offer)
2847{
2848 struct input *input = data;
2849 char **p;
2850
2851 if (input->selection_offer)
2852 data_offer_destroy(input->selection_offer);
2853
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002854 if (offer) {
2855 input->selection_offer = wl_data_offer_get_user_data(offer);
2856 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2857 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002858 } else {
2859 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002860 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002861}
2862
2863static const struct wl_data_device_listener data_device_listener = {
2864 data_device_data_offer,
2865 data_device_enter,
2866 data_device_leave,
2867 data_device_motion,
2868 data_device_drop,
2869 data_device_selection
2870};
2871
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002872static void
2873input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002874{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002875 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002876 struct wl_cursor *cursor;
2877 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002878
Daniel Stone80972742012-11-07 17:51:39 +11002879 if (!input->pointer)
2880 return;
2881
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002882 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002883 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002884 return;
2885
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002886 if (index >= (int) cursor->image_count) {
2887 fprintf(stderr, "cursor index out of range\n");
2888 return;
2889 }
2890
2891 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002892 buffer = wl_cursor_image_get_buffer(image);
2893 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002894 return;
2895
Kristian Høgsbergae277372012-08-01 09:41:08 -04002896 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002897 input->pointer_surface,
2898 image->hotspot_x, image->hotspot_y);
2899 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2900 wl_surface_damage(input->pointer_surface, 0, 0,
2901 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002902 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002903}
2904
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002905static const struct wl_callback_listener pointer_surface_listener;
2906
2907static void
2908pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2909 uint32_t time)
2910{
2911 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002912 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002913 int i;
2914
2915 if (callback) {
2916 assert(callback == input->cursor_frame_cb);
2917 wl_callback_destroy(callback);
2918 input->cursor_frame_cb = NULL;
2919 }
2920
Daniel Stone80972742012-11-07 17:51:39 +11002921 if (!input->pointer)
2922 return;
2923
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002924 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002925 wl_pointer_set_cursor(input->pointer,
2926 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002927 NULL, 0, 0);
2928 return;
2929 }
2930
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002931 if (input->current_cursor == CURSOR_UNSET)
2932 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002933 cursor = input->display->cursors[input->current_cursor];
2934 if (!cursor)
2935 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002936
2937 /* FIXME We don't have the current time on the first call so we set
2938 * the animation start to the time of the first frame callback. */
2939 if (time == 0)
2940 input->cursor_anim_start = 0;
2941 else if (input->cursor_anim_start == 0)
2942 input->cursor_anim_start = time;
2943
2944 if (time == 0 || input->cursor_anim_start == 0)
2945 i = 0;
2946 else
2947 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2948
Pekka Paalanenbc106382012-10-10 12:49:31 +03002949 if (cursor->image_count > 1) {
2950 input->cursor_frame_cb =
2951 wl_surface_frame(input->pointer_surface);
2952 wl_callback_add_listener(input->cursor_frame_cb,
2953 &pointer_surface_listener, input);
2954 }
2955
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002956 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002957}
2958
2959static const struct wl_callback_listener pointer_surface_listener = {
2960 pointer_surface_frame_callback
2961};
2962
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002963void
2964input_set_pointer_image(struct input *input, int pointer)
2965{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002966 int force = 0;
2967
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002968 if (!input->pointer)
2969 return;
2970
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002971 if (input->pointer_enter_serial > input->cursor_serial)
2972 force = 1;
2973
2974 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002975 return;
2976
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002977 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002978 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002979 if (!input->cursor_frame_cb)
2980 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002981 else if (force) {
2982 /* The current frame callback may be stuck if, for instance,
2983 * the set cursor request was processed by the server after
2984 * this client lost the focus. In this case the cursor surface
2985 * might not be mapped and the frame callback wouldn't ever
2986 * complete. Send a set_cursor and attach to try to map the
2987 * cursor surface again so that the callback will finish */
2988 input_set_pointer_image_index(input, 0);
2989 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002990}
2991
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002992struct wl_data_device *
2993input_get_data_device(struct input *input)
2994{
2995 return input->data_device;
2996}
2997
2998void
2999input_set_selection(struct input *input,
3000 struct wl_data_source *source, uint32_t time)
3001{
3002 wl_data_device_set_selection(input->data_device, source, time);
3003}
3004
3005void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003006input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003007{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003008 wl_data_offer_accept(input->drag_offer->offer,
3009 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003010}
3011
3012static void
3013offer_io_func(struct task *task, uint32_t events)
3014{
3015 struct data_offer *offer =
3016 container_of(task, struct data_offer, io_task);
3017 unsigned int len;
3018 char buffer[4096];
3019
3020 len = read(offer->fd, buffer, sizeof buffer);
3021 offer->func(buffer, len,
3022 offer->x, offer->y, offer->user_data);
3023
3024 if (len == 0) {
3025 close(offer->fd);
3026 data_offer_destroy(offer);
3027 }
3028}
3029
3030static void
3031data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3032 data_func_t func, void *user_data)
3033{
3034 int p[2];
3035
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003036 if (pipe2(p, O_CLOEXEC) == -1)
3037 return;
3038
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003039 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3040 close(p[1]);
3041
3042 offer->io_task.run = offer_io_func;
3043 offer->fd = p[0];
3044 offer->func = func;
3045 offer->refcount++;
3046 offer->user_data = user_data;
3047
3048 display_watch_fd(offer->input->display,
3049 offer->fd, EPOLLIN, &offer->io_task);
3050}
3051
3052void
3053input_receive_drag_data(struct input *input, const char *mime_type,
3054 data_func_t func, void *data)
3055{
3056 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3057 input->drag_offer->x = input->sx;
3058 input->drag_offer->y = input->sy;
3059}
3060
3061int
3062input_receive_selection_data(struct input *input, const char *mime_type,
3063 data_func_t func, void *data)
3064{
3065 char **p;
3066
3067 if (input->selection_offer == NULL)
3068 return -1;
3069
3070 for (p = input->selection_offer->types.data; *p; p++)
3071 if (strcmp(mime_type, *p) == 0)
3072 break;
3073
3074 if (*p == NULL)
3075 return -1;
3076
3077 data_offer_receive_data(input->selection_offer,
3078 mime_type, func, data);
3079 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003080}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003081
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003082int
3083input_receive_selection_data_to_fd(struct input *input,
3084 const char *mime_type, int fd)
3085{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003086 if (input->selection_offer)
3087 wl_data_offer_receive(input->selection_offer->offer,
3088 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003089
3090 return 0;
3091}
3092
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003093void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003094window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003095{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003096 if (!window->shell_surface)
3097 return;
3098
Daniel Stone37816df2012-05-16 18:45:18 +01003099 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003100}
3101
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003102static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003103idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003104{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003105 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003106 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003107
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003108 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003109 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003110 widget_set_allocation(widget,
3111 window->pending_allocation.x,
3112 window->pending_allocation.y,
3113 window->pending_allocation.width,
3114 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003115
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003116 if (window->input_region) {
3117 wl_region_destroy(window->input_region);
3118 window->input_region = NULL;
3119 }
3120
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003121 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003122 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003123
3124 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003125
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003126 if (widget->resize_handler)
3127 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003128 widget->allocation.width,
3129 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003130 widget->user_data);
3131
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003132 if (window->allocation.width != widget->allocation.width ||
3133 window->allocation.height != widget->allocation.height) {
3134 window->allocation = widget->allocation;
3135 window_schedule_redraw(window);
3136 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003137
3138 if (widget->opaque)
3139 wl_region_add(window->opaque_region, 0, 0,
3140 widget->allocation.width,
3141 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003142}
3143
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003144void
3145window_schedule_resize(struct window *window, int width, int height)
3146{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003147 window->pending_allocation.x = 0;
3148 window->pending_allocation.y = 0;
3149 window->pending_allocation.width = width;
3150 window->pending_allocation.height = height;
3151
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003152 if (window->min_allocation.width == 0)
3153 window->min_allocation = window->pending_allocation;
3154 if (window->pending_allocation.width < window->min_allocation.width)
3155 window->pending_allocation.width = window->min_allocation.width;
3156 if (window->pending_allocation.height < window->min_allocation.height)
3157 window->pending_allocation.height = window->min_allocation.height;
3158
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003159 window->resize_needed = 1;
3160 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003161}
3162
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003163void
3164widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3165{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003166 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003167}
3168
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003169static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003170handle_ping(void *data, struct wl_shell_surface *shell_surface,
3171 uint32_t serial)
3172{
3173 wl_shell_surface_pong(shell_surface, serial);
3174}
3175
3176static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003177handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003178 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003179{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003180 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003181
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003182 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003183 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003184}
3185
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003186static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003187menu_destroy(struct menu *menu)
3188{
3189 widget_destroy(menu->widget);
3190 window_destroy(menu->window);
3191 free(menu);
3192}
3193
3194static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003195handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3196{
3197 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003198 struct menu *menu = window->widget->user_data;
3199
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003200 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003201 * device. Or just use wl_callback. And this really needs to
3202 * be a window vfunc that the menu can set. And we need the
3203 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003204
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003205 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003206 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003207 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003208}
3209
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003210static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003211 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003212 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003213 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003214};
3215
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003216void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003217window_get_allocation(struct window *window,
3218 struct rectangle *allocation)
3219{
3220 *allocation = window->allocation;
3221}
3222
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003223static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003224widget_redraw(struct widget *widget)
3225{
3226 struct widget *child;
3227
3228 if (widget->redraw_handler)
3229 widget->redraw_handler(widget, widget->user_data);
3230 wl_list_for_each(child, &widget->child_list, link)
3231 widget_redraw(child);
3232}
3233
3234static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003235frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3236{
3237 struct window *window = data;
3238
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003239 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003240 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003241 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003242 window->redraw_scheduled = 0;
3243 if (window->redraw_needed)
3244 window_schedule_redraw(window);
3245}
3246
3247static const struct wl_callback_listener listener = {
3248 frame_callback
3249};
3250
3251static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003252idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003253{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003254 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003255
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003256 if (window->resize_needed)
3257 idle_resize(window);
3258
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003259 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003260 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003261 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003262 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003263
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003264 window->frame_cb = wl_surface_frame(window->surface);
3265 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003266 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003267}
3268
3269void
3270window_schedule_redraw(struct window *window)
3271{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003272 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003273 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003274 window->redraw_task.run = idle_redraw;
3275 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003276 window->redraw_scheduled = 1;
3277 }
3278}
3279
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003280int
3281window_is_fullscreen(struct window *window)
3282{
3283 return window->type == TYPE_FULLSCREEN;
3284}
3285
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003286void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003287window_set_fullscreen(struct window *window, int fullscreen)
3288{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003289 if (!window->display->shell)
3290 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003291
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003292 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003293 return;
3294
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003295 if (fullscreen) {
3296 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003297 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003298 wl_shell_surface_set_fullscreen(window->shell_surface,
3299 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3300 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003301 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003302 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003303 wl_shell_surface_set_toplevel(window->shell_surface);
3304 window_schedule_resize(window,
3305 window->saved_allocation.width,
3306 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003307 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003308}
3309
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003310int
3311window_is_maximized(struct window *window)
3312{
3313 return window->type == TYPE_MAXIMIZED;
3314}
3315
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003316void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003317window_set_maximized(struct window *window, int maximized)
3318{
3319 if (!window->display->shell)
3320 return;
3321
3322 if ((window->type == TYPE_MAXIMIZED) == maximized)
3323 return;
3324
3325 if (window->type == TYPE_TOPLEVEL) {
3326 window->saved_allocation = window->allocation;
3327 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3328 window->type = TYPE_MAXIMIZED;
3329 } else {
3330 wl_shell_surface_set_toplevel(window->shell_surface);
3331 window->type = TYPE_TOPLEVEL;
3332 window_schedule_resize(window,
3333 window->saved_allocation.width,
3334 window->saved_allocation.height);
3335 }
3336}
3337
3338void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003339window_set_user_data(struct window *window, void *data)
3340{
3341 window->user_data = data;
3342}
3343
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003344void *
3345window_get_user_data(struct window *window)
3346{
3347 return window->user_data;
3348}
3349
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003350void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003351window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003352 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003353{
3354 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003355}
3356
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003357void
3358window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003359 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003360{
3361 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003362}
3363
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003364void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003365window_set_data_handler(struct window *window, window_data_handler_t handler)
3366{
3367 window->data_handler = handler;
3368}
3369
3370void
3371window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3372{
3373 window->drop_handler = handler;
3374}
3375
3376void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003377window_set_close_handler(struct window *window,
3378 window_close_handler_t handler)
3379{
3380 window->close_handler = handler;
3381}
3382
3383void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003384window_set_fullscreen_handler(struct window *window,
3385 window_fullscreen_handler_t handler)
3386{
3387 window->fullscreen_handler = handler;
3388}
3389
3390void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003391window_set_output_handler(struct window *window,
3392 window_output_handler_t handler)
3393{
3394 window->output_handler = handler;
3395}
3396
3397void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003398window_set_title(struct window *window, const char *title)
3399{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003400 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003401 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003402 if (window->shell_surface)
3403 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003404}
3405
3406const char *
3407window_get_title(struct window *window)
3408{
3409 return window->title;
3410}
3411
3412void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003413window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3414{
3415 struct text_cursor_position *text_cursor_position =
3416 window->display->text_cursor_position;
3417
Scott Moreau9295ce02012-06-01 12:46:10 -06003418 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003419 return;
3420
3421 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003422 window->surface,
3423 wl_fixed_from_int(x),
3424 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003425}
3426
3427void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003428window_damage(struct window *window, int32_t x, int32_t y,
3429 int32_t width, int32_t height)
3430{
3431 wl_surface_damage(window->surface, x, y, width, height);
3432}
3433
Casey Dahlin9074db52012-04-19 22:50:09 -04003434static void
3435surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003436 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003437{
Rob Bradford7507b572012-05-15 17:55:34 +01003438 struct window *window = data;
3439 struct output *output;
3440 struct output *output_found = NULL;
3441 struct window_output *window_output;
3442
3443 wl_list_for_each(output, &window->display->output_list, link) {
3444 if (output->output == wl_output) {
3445 output_found = output;
3446 break;
3447 }
3448 }
3449
3450 if (!output_found)
3451 return;
3452
3453 window_output = malloc (sizeof *window_output);
3454 window_output->output = output_found;
3455
3456 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003457
3458 if (window->output_handler)
3459 window->output_handler(window, output_found, 1,
3460 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003461}
3462
3463static void
3464surface_leave(void *data,
3465 struct wl_surface *wl_surface, struct wl_output *output)
3466{
Rob Bradford7507b572012-05-15 17:55:34 +01003467 struct window *window = data;
3468 struct window_output *window_output;
3469 struct window_output *window_output_found = NULL;
3470
3471 wl_list_for_each(window_output, &window->window_output_list, link) {
3472 if (window_output->output->output == output) {
3473 window_output_found = window_output;
3474 break;
3475 }
3476 }
3477
3478 if (window_output_found) {
3479 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003480
3481 if (window->output_handler)
3482 window->output_handler(window, window_output->output,
3483 0, window->user_data);
3484
Rob Bradford7507b572012-05-15 17:55:34 +01003485 free(window_output_found);
3486 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003487}
3488
3489static const struct wl_surface_listener surface_listener = {
3490 surface_enter,
3491 surface_leave
3492};
3493
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003494static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003495window_create_internal(struct display *display,
3496 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003497{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003498 struct window *window;
3499
3500 window = malloc(sizeof *window);
3501 if (window == NULL)
3502 return NULL;
3503
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003504 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003505 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003506 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003507 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003508 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003509 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003510 window->shell_surface =
3511 wl_shell_get_shell_surface(display->shell,
3512 window->surface);
3513 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003514 window->allocation.x = 0;
3515 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003516 window->allocation.width = 0;
3517 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003518 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003519 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003520 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003521 window->input_region = NULL;
3522 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003523
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003524 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003525#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003526 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003527#else
3528 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3529#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003530 else
3531 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003532
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003533 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003534 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003535 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003536
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003537 if (window->shell_surface) {
3538 wl_shell_surface_set_user_data(window->shell_surface, window);
3539 wl_shell_surface_add_listener(window->shell_surface,
3540 &shell_surface_listener, window);
3541 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003542
Rob Bradford7507b572012-05-15 17:55:34 +01003543 wl_list_init (&window->window_output_list);
3544
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003545 return window;
3546}
3547
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003548struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003549window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003550{
3551 struct window *window;
3552
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003553 window = window_create_internal(display, NULL, TYPE_NONE);
3554 if (!window)
3555 return NULL;
3556
3557 return window;
3558}
3559
3560struct window *
3561window_create_custom(struct display *display)
3562{
3563 struct window *window;
3564
3565 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003566 if (!window)
3567 return NULL;
3568
3569 return window;
3570}
3571
3572struct window *
3573window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003574 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003575{
3576 struct window *window;
3577
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003578 window = window_create_internal(parent->display,
3579 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003580 if (!window)
3581 return NULL;
3582
3583 window->x = x;
3584 window->y = y;
3585
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003586 if (display->shell)
3587 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003588 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003589 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003590
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003591 return window;
3592}
3593
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003594static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003595menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003596{
3597 int next;
3598
3599 next = (sy - 8) / 20;
3600 if (menu->current != next) {
3601 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003602 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003603 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003604}
3605
3606static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003607menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003608 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003609 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003610{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003611 struct menu *menu = data;
3612
3613 if (widget == menu->widget)
3614 menu_set_item(data, y);
3615
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003616 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003617}
3618
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003619static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003620menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003621 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003622{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003623 struct menu *menu = data;
3624
3625 if (widget == menu->widget)
3626 menu_set_item(data, y);
3627
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003628 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003629}
3630
3631static void
3632menu_leave_handler(struct widget *widget, struct input *input, void *data)
3633{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003634 struct menu *menu = data;
3635
3636 if (widget == menu->widget)
3637 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003638}
3639
3640static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003641menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003642 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003643 uint32_t button, enum wl_pointer_button_state state,
3644 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003645
3646{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003647 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003648
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003649 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3650 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003651 /* Either relase after press-drag-release or
3652 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003653 menu->func(menu->window->parent,
3654 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003655 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003656 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003657 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003658 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003659 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003660}
3661
3662static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003663menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003664{
3665 cairo_t *cr;
3666 const int32_t r = 3, margin = 3;
3667 struct menu *menu = data;
3668 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003669 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003670
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003671 cr = cairo_create(window->cairo_surface);
3672 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3673 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3674 cairo_paint(cr);
3675
3676 width = window->allocation.width;
3677 height = window->allocation.height;
3678 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003679
3680 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003681 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3682 cairo_fill(cr);
3683
3684 for (i = 0; i < menu->count; i++) {
3685 if (i == menu->current) {
3686 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3687 cairo_rectangle(cr, margin, i * 20 + margin,
3688 width - 2 * margin, 20);
3689 cairo_fill(cr);
3690 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3691 cairo_move_to(cr, 10, i * 20 + 16);
3692 cairo_show_text(cr, menu->entries[i]);
3693 } else {
3694 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3695 cairo_move_to(cr, 10, i * 20 + 16);
3696 cairo_show_text(cr, menu->entries[i]);
3697 }
3698 }
3699
3700 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003701}
3702
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003703void
3704window_show_menu(struct display *display,
3705 struct input *input, uint32_t time, struct window *parent,
3706 int32_t x, int32_t y,
3707 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003708{
3709 struct window *window;
3710 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003711 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003712
3713 menu = malloc(sizeof *menu);
3714 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003715 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003716
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003717 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003718 if (!window) {
3719 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003720 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003721 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003722
3723 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003724 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003725 menu->entries = entries;
3726 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003727 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003728 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003729 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003730 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003731 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003732 window->type = TYPE_MENU;
3733 window->x = x;
3734 window->y = y;
3735
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003736 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003737 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003738 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003739 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003740 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003741
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003742 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003743 widget_set_enter_handler(menu->widget, menu_enter_handler);
3744 widget_set_leave_handler(menu->widget, menu_leave_handler);
3745 widget_set_motion_handler(menu->widget, menu_motion_handler);
3746 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003747
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003748 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003749 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003750}
3751
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003752void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003753window_set_buffer_type(struct window *window, enum window_buffer_type type)
3754{
3755 window->buffer_type = type;
3756}
3757
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003758
3759static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003760display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003761 struct wl_output *wl_output,
3762 int x, int y,
3763 int physical_width,
3764 int physical_height,
3765 int subpixel,
3766 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003767 const char *model,
3768 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003769{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003770 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003771
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003772 output->allocation.x = x;
3773 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003774 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003775}
3776
3777static void
3778display_handle_mode(void *data,
3779 struct wl_output *wl_output,
3780 uint32_t flags,
3781 int width,
3782 int height,
3783 int refresh)
3784{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003785 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003786 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003787
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003788 if (flags & WL_OUTPUT_MODE_CURRENT) {
3789 output->allocation.width = width;
3790 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003791 if (display->output_configure_handler)
3792 (*display->output_configure_handler)(
3793 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003794 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003795}
3796
3797static const struct wl_output_listener output_listener = {
3798 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003799 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003800};
3801
3802static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003803display_add_output(struct display *d, uint32_t id)
3804{
3805 struct output *output;
3806
3807 output = malloc(sizeof *output);
3808 if (output == NULL)
3809 return;
3810
3811 memset(output, 0, sizeof *output);
3812 output->display = d;
3813 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003814 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003815 wl_list_insert(d->output_list.prev, &output->link);
3816
3817 wl_output_add_listener(output->output, &output_listener, output);
3818}
3819
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003820static void
3821output_destroy(struct output *output)
3822{
3823 if (output->destroy_handler)
3824 (*output->destroy_handler)(output, output->user_data);
3825
3826 wl_output_destroy(output->output);
3827 wl_list_remove(&output->link);
3828 free(output);
3829}
3830
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003831void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003832display_set_global_handler(struct display *display,
3833 display_global_handler_t handler)
3834{
3835 struct global *global;
3836
3837 display->global_handler = handler;
3838 if (!handler)
3839 return;
3840
3841 wl_list_for_each(global, &display->global_list, link)
3842 display->global_handler(display,
3843 global->name, global->interface,
3844 global->version, display->user_data);
3845}
3846
3847void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003848display_set_output_configure_handler(struct display *display,
3849 display_output_handler_t handler)
3850{
3851 struct output *output;
3852
3853 display->output_configure_handler = handler;
3854 if (!handler)
3855 return;
3856
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003857 wl_list_for_each(output, &display->output_list, link) {
3858 if (output->allocation.width == 0 &&
3859 output->allocation.height == 0)
3860 continue;
3861
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003862 (*display->output_configure_handler)(output,
3863 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003864 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003865}
3866
3867void
3868output_set_user_data(struct output *output, void *data)
3869{
3870 output->user_data = data;
3871}
3872
3873void *
3874output_get_user_data(struct output *output)
3875{
3876 return output->user_data;
3877}
3878
3879void
3880output_set_destroy_handler(struct output *output,
3881 display_output_handler_t handler)
3882{
3883 output->destroy_handler = handler;
3884 /* FIXME: implement this, once we have way to remove outputs */
3885}
3886
3887void
Scott Moreau4e072362012-09-29 02:03:11 -06003888output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003889{
Scott Moreau4e072362012-09-29 02:03:11 -06003890 struct rectangle allocation = output->allocation;
3891
3892 switch (output->transform) {
3893 case WL_OUTPUT_TRANSFORM_90:
3894 case WL_OUTPUT_TRANSFORM_270:
3895 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3896 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3897 /* Swap width and height */
3898 allocation.width = output->allocation.height;
3899 allocation.height = output->allocation.width;
3900 break;
3901 }
3902
3903 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003904}
3905
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003906struct wl_output *
3907output_get_wl_output(struct output *output)
3908{
3909 return output->output;
3910}
3911
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003912enum wl_output_transform
3913output_get_transform(struct output *output)
3914{
3915 return output->transform;
3916}
3917
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003918static void
Daniel Stone97f68542012-05-30 16:32:01 +01003919fini_xkb(struct input *input)
3920{
3921 xkb_state_unref(input->xkb.state);
3922 xkb_map_unref(input->xkb.keymap);
3923}
3924
3925static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003926display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003927{
3928 struct input *input;
3929
3930 input = malloc(sizeof *input);
3931 if (input == NULL)
3932 return;
3933
3934 memset(input, 0, sizeof *input);
3935 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003936 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003937 input->pointer_focus = NULL;
3938 input->keyboard_focus = NULL;
3939 wl_list_insert(d->input_list.prev, &input->link);
3940
Daniel Stone37816df2012-05-16 18:45:18 +01003941 wl_seat_add_listener(input->seat, &seat_listener, input);
3942 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003943
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003944 input->data_device =
3945 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003946 input->seat);
3947 wl_data_device_add_listener(input->data_device, &data_device_listener,
3948 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003949
3950 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003951
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003952 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3953 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003954 input->repeat_task.run = keyboard_repeat_func;
3955 display_watch_fd(d, input->repeat_timer_fd,
3956 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003957}
3958
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003959static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003960input_destroy(struct input *input)
3961{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003962 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003963 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003964
3965 if (input->drag_offer)
3966 data_offer_destroy(input->drag_offer);
3967
3968 if (input->selection_offer)
3969 data_offer_destroy(input->selection_offer);
3970
3971 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003972 fini_xkb(input);
3973
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003974 wl_surface_destroy(input->pointer_surface);
3975
Pekka Paalanene1207c72011-12-16 12:02:09 +02003976 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003977 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003978 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003979 free(input);
3980}
3981
3982static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003983init_workspace_manager(struct display *d, uint32_t id)
3984{
3985 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003986 wl_registry_bind(d->registry, id,
3987 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003988 if (d->workspace_manager != NULL)
3989 workspace_manager_add_listener(d->workspace_manager,
3990 &workspace_manager_listener,
3991 d);
3992}
3993
3994static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003995registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3996 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003997{
3998 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003999 struct global *global;
4000
4001 global = malloc(sizeof *global);
4002 global->name = id;
4003 global->interface = strdup(interface);
4004 global->version = version;
4005 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004006
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004007 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004008 d->compositor = wl_registry_bind(registry, id,
4009 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004010 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004011 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004012 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004013 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004014 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004015 d->shell = wl_registry_bind(registry,
4016 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004017 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004018 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004019 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4020 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004021 wl_registry_bind(registry, id,
4022 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004023 } else if (strcmp(interface, "text_cursor_position") == 0) {
4024 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004025 wl_registry_bind(registry, id,
4026 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004027 } else if (strcmp(interface, "workspace_manager") == 0) {
4028 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004029 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004030
4031 if (d->global_handler)
4032 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004033}
4034
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004035void *
4036display_bind(struct display *display, uint32_t name,
4037 const struct wl_interface *interface, uint32_t version)
4038{
4039 return wl_registry_bind(display->registry, name, interface, version);
4040}
4041
4042static const struct wl_registry_listener registry_listener = {
4043 registry_handle_global
4044};
4045
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004046#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004047static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004048init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004049{
4050 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004051 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004052
Rob Clark6396ed32012-03-11 19:48:41 -05004053#ifdef USE_CAIRO_GLESV2
4054# define GL_BIT EGL_OPENGL_ES2_BIT
4055#else
4056# define GL_BIT EGL_OPENGL_BIT
4057#endif
4058
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004059 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004060 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004061 EGL_RED_SIZE, 1,
4062 EGL_GREEN_SIZE, 1,
4063 EGL_BLUE_SIZE, 1,
4064 EGL_ALPHA_SIZE, 1,
4065 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004066 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004067 EGL_NONE
4068 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004069
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004070#ifdef USE_CAIRO_GLESV2
4071 static const EGLint context_attribs[] = {
4072 EGL_CONTEXT_CLIENT_VERSION, 2,
4073 EGL_NONE
4074 };
4075 EGLint api = EGL_OPENGL_ES_API;
4076#else
4077 EGLint *context_attribs = NULL;
4078 EGLint api = EGL_OPENGL_API;
4079#endif
4080
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004081 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004082 if (!eglInitialize(d->dpy, &major, &minor)) {
4083 fprintf(stderr, "failed to initialize display\n");
4084 return -1;
4085 }
4086
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004087 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004088 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
4089 return -1;
4090 }
4091
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004092 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4093 &d->argb_config, 1, &n) || n != 1) {
4094 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004095 return -1;
4096 }
4097
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004098 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004099 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004100 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05004101 fprintf(stderr, "failed to create context\n");
4102 return -1;
4103 }
4104
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004105 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01004106 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004107 return -1;
4108 }
4109
Benjamin Franzke0c991632011-09-27 21:57:31 +02004110 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4111 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4112 fprintf(stderr, "failed to get cairo egl argb device\n");
4113 return -1;
4114 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004115
4116 return 0;
4117}
4118
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004119static void
4120fini_egl(struct display *display)
4121{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004122 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004123
4124 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4125 EGL_NO_CONTEXT);
4126
4127 eglTerminate(display->dpy);
4128 eglReleaseThread();
4129}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004130#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004131
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004132static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004133init_dummy_surface(struct display *display)
4134{
4135 int len;
4136 void *data;
4137
4138 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4139 data = malloc(len);
4140 display->dummy_surface =
4141 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4142 1, 1, len);
4143 display->dummy_surface_data = data;
4144}
4145
4146static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004147handle_display_data(struct task *task, uint32_t events)
4148{
4149 struct display *display =
4150 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004151 struct epoll_event ep;
4152 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004153
4154 display->display_fd_events = events;
4155
4156 if (events & EPOLLERR || events & EPOLLHUP) {
4157 display_exit(display);
4158 return;
4159 }
4160
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004161 if (events & EPOLLIN) {
4162 ret = wl_display_dispatch(display->display);
4163 if (ret == -1) {
4164 display_exit(display);
4165 return;
4166 }
4167 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004168
4169 if (events & EPOLLOUT) {
4170 ret = wl_display_flush(display->display);
4171 if (ret == 0) {
4172 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4173 ep.data.ptr = &display->display_task;
4174 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4175 display->display_fd, &ep);
4176 } else if (ret == -1 && errno != EAGAIN) {
4177 display_exit(display);
4178 return;
4179 }
4180 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004181}
4182
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004183struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004184display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004185{
4186 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004187
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004188 d = malloc(sizeof *d);
4189 if (d == NULL)
4190 return NULL;
4191
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004192 memset(d, 0, sizeof *d);
4193
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004194 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004195 if (d->display == NULL) {
4196 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004197 return NULL;
4198 }
4199
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004200 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004201 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004202 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004203 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4204 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004205
4206 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004207 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004208 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004209 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004210
Daniel Stone97f68542012-05-30 16:32:01 +01004211 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004212 if (d->xkb_context == NULL) {
4213 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004214 return NULL;
4215 }
4216
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004217 d->workspace = 0;
4218 d->workspace_count = 1;
4219
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004220 d->registry = wl_display_get_registry(d->display);
4221 wl_registry_add_listener(d->registry, &registry_listener, d);
4222 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004223#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004224 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004225 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004226#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004227
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004228 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004229
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004230 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004231
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004232 wl_list_init(&d->window_list);
4233
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004234 init_dummy_surface(d);
4235
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004236 return d;
4237}
4238
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004239static void
4240display_destroy_outputs(struct display *display)
4241{
4242 struct output *tmp;
4243 struct output *output;
4244
4245 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4246 output_destroy(output);
4247}
4248
Pekka Paalanene1207c72011-12-16 12:02:09 +02004249static void
4250display_destroy_inputs(struct display *display)
4251{
4252 struct input *tmp;
4253 struct input *input;
4254
4255 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4256 input_destroy(input);
4257}
4258
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004259void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004260display_destroy(struct display *display)
4261{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004262 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004263 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4264 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004265
4266 if (!wl_list_empty(&display->deferred_list))
4267 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4268
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004269 cairo_surface_destroy(display->dummy_surface);
4270 free(display->dummy_surface_data);
4271
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004272 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004273 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004274
Daniel Stone97f68542012-05-30 16:32:01 +01004275 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004276
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004277 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004278 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004279
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004280#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004281 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004282#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004283
Pekka Paalanenc2052982011-12-16 11:41:32 +02004284 if (display->shell)
4285 wl_shell_destroy(display->shell);
4286
4287 if (display->shm)
4288 wl_shm_destroy(display->shm);
4289
4290 if (display->data_device_manager)
4291 wl_data_device_manager_destroy(display->data_device_manager);
4292
4293 wl_compositor_destroy(display->compositor);
4294
4295 close(display->epoll_fd);
4296
U. Artie Eoff44874d92012-10-02 21:12:35 -07004297 if (!(display->display_fd_events & EPOLLERR) &&
4298 !(display->display_fd_events & EPOLLHUP))
4299 wl_display_flush(display->display);
4300
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004301 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004302 free(display);
4303}
4304
4305void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004306display_set_user_data(struct display *display, void *data)
4307{
4308 display->user_data = data;
4309}
4310
4311void *
4312display_get_user_data(struct display *display)
4313{
4314 return display->user_data;
4315}
4316
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004317struct wl_display *
4318display_get_display(struct display *display)
4319{
4320 return display->display;
4321}
4322
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004323struct output *
4324display_get_output(struct display *display)
4325{
4326 return container_of(display->output_list.next, struct output, link);
4327}
4328
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004329struct wl_compositor *
4330display_get_compositor(struct display *display)
4331{
4332 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004333}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004334
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004335uint32_t
4336display_get_serial(struct display *display)
4337{
4338 return display->serial;
4339}
4340
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004341EGLDisplay
4342display_get_egl_display(struct display *d)
4343{
4344 return d->dpy;
4345}
4346
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004347struct wl_data_source *
4348display_create_data_source(struct display *display)
4349{
4350 return wl_data_device_manager_create_data_source(display->data_device_manager);
4351}
4352
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004353EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004354display_get_argb_egl_config(struct display *d)
4355{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004356 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004357}
4358
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004359struct wl_shell *
4360display_get_shell(struct display *display)
4361{
4362 return display->shell;
4363}
4364
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004365int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004366display_acquire_window_surface(struct display *display,
4367 struct window *window,
4368 EGLContext ctx)
4369{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004370 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004371 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004372
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004373 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004374}
4375
4376void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004377display_release_window_surface(struct display *display,
4378 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004379{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004380 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004381 return;
4382
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004383 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004384}
4385
4386void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004387display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004388{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004389 wl_list_insert(&display->deferred_list, &task->link);
4390}
4391
4392void
4393display_watch_fd(struct display *display,
4394 int fd, uint32_t events, struct task *task)
4395{
4396 struct epoll_event ep;
4397
4398 ep.events = events;
4399 ep.data.ptr = task;
4400 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4401}
4402
4403void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004404display_unwatch_fd(struct display *display, int fd)
4405{
4406 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4407}
4408
4409void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004410display_run(struct display *display)
4411{
4412 struct task *task;
4413 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004414 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004415
Pekka Paalanen826d7952011-12-15 10:14:07 +02004416 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004417 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004418 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004419 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004420 struct task, link);
4421 wl_list_remove(&task->link);
4422 task->run(task, 0);
4423 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004424
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004425 wl_display_dispatch_pending(display->display);
4426
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004427 if (!display->running)
4428 break;
4429
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004430 ret = wl_display_flush(display->display);
4431 if (ret < 0 && errno == EAGAIN) {
4432 ep[0].events =
4433 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4434 ep[0].data.ptr = &display->display_task;
4435
4436 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4437 display->display_fd, &ep[0]);
4438 } else if (ret < 0) {
4439 break;
4440 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004441
4442 count = epoll_wait(display->epoll_fd,
4443 ep, ARRAY_LENGTH(ep), -1);
4444 for (i = 0; i < count; i++) {
4445 task = ep[i].data.ptr;
4446 task->run(task, ep[i].events);
4447 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004448 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004449}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004450
4451void
4452display_exit(struct display *display)
4453{
4454 display->running = 0;
4455}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004456
4457void
4458keysym_modifiers_add(struct wl_array *modifiers_map,
4459 const char *name)
4460{
4461 size_t len = strlen(name) + 1;
4462 char *p;
4463
4464 p = wl_array_add(modifiers_map, len);
4465
4466 if (p == NULL)
4467 return;
4468
4469 strncpy(p, name, len);
4470}
4471
4472static xkb_mod_index_t
4473keysym_modifiers_get_index(struct wl_array *modifiers_map,
4474 const char *name)
4475{
4476 xkb_mod_index_t index = 0;
4477 char *p = modifiers_map->data;
4478
4479 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4480 if (strcmp(p, name) == 0)
4481 return index;
4482
4483 index++;
4484 p += strlen(p) + 1;
4485 }
4486
4487 return XKB_MOD_INVALID;
4488}
4489
4490xkb_mod_mask_t
4491keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4492 const char *name)
4493{
4494 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4495
4496 if (index == XKB_MOD_INVALID)
4497 return XKB_MOD_INVALID;
4498
4499 return 1 << index;
4500}